1/**************************************************************************
2 *
3 * Copyright 2009, VMware, Inc.
4 * All Rights Reserved.
5 * Copyright 2010 George Sapountzis <gsapountzis@gmail.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29#include "util/u_format.h"
30#include "util/u_memory.h"
31#include "util/u_inlines.h"
32#include "util/u_box.h"
33#include "pipe/p_context.h"
34#include "pipe-loader/pipe_loader.h"
35#include "state_tracker/drisw_api.h"
36#include "state_tracker/st_context.h"
37
38#include "dri_screen.h"
39#include "dri_context.h"
40#include "dri_drawable.h"
41#include "dri_helpers.h"
42#include "dri_query_renderer.h"
43
44DEBUG_GET_ONCE_BOOL_OPTION(swrast_no_present, "SWRAST_NO_PRESENT", FALSE);
45
46static inline void
47get_drawable_info(__DRIdrawable *dPriv, int *x, int *y, int *w, int *h)
48{
49   __DRIscreen *sPriv = dPriv->driScreenPriv;
50   const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
51
52   loader->getDrawableInfo(dPriv,
53                           x, y, w, h,
54                           dPriv->loaderPrivate);
55}
56
57static inline void
58put_image(__DRIdrawable *dPriv, void *data, unsigned width, unsigned height)
59{
60   __DRIscreen *sPriv = dPriv->driScreenPriv;
61   const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
62
63   loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
64                    0, 0, width, height,
65                    data, dPriv->loaderPrivate);
66}
67
68static inline void
69put_image2(__DRIdrawable *dPriv, void *data, int x, int y,
70           unsigned width, unsigned height, unsigned stride)
71{
72   __DRIscreen *sPriv = dPriv->driScreenPriv;
73   const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
74
75   loader->putImage2(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
76                     x, y, width, height, stride,
77                     data, dPriv->loaderPrivate);
78}
79
80static inline void
81put_image_shm(__DRIdrawable *dPriv, int shmid, char *shmaddr,
82              unsigned offset, unsigned offset_x, int x, int y,
83              unsigned width, unsigned height, unsigned stride)
84{
85   __DRIscreen *sPriv = dPriv->driScreenPriv;
86   const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
87
88   /* if we have the newer interface, don't have to add the offset_x here. */
89   if (loader->base.version > 4 && loader->putImageShm2)
90     loader->putImageShm2(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
91                          x, y, width, height, stride,
92                          shmid, shmaddr, offset, dPriv->loaderPrivate);
93   else
94     loader->putImageShm(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
95                         x, y, width, height, stride,
96                         shmid, shmaddr, offset + offset_x, dPriv->loaderPrivate);
97}
98
99static inline void
100get_image(__DRIdrawable *dPriv, int x, int y, int width, int height, void *data)
101{
102   __DRIscreen *sPriv = dPriv->driScreenPriv;
103   const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
104
105   loader->getImage(dPriv,
106                    x, y, width, height,
107                    data, dPriv->loaderPrivate);
108}
109
110static inline void
111get_image2(__DRIdrawable *dPriv, int x, int y, int width, int height, int stride, void *data)
112{
113   __DRIscreen *sPriv = dPriv->driScreenPriv;
114   const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
115
116   /* getImage2 support is only in version 3 or newer */
117   if (loader->base.version < 3)
118      return;
119
120   loader->getImage2(dPriv,
121                     x, y, width, height, stride,
122                     data, dPriv->loaderPrivate);
123}
124
125static inline bool
126get_image_shm(__DRIdrawable *dPriv, int x, int y, int width, int height,
127              struct pipe_resource *res)
128{
129   __DRIscreen *sPriv = dPriv->driScreenPriv;
130   const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
131   struct winsys_handle whandle;
132
133   whandle.type = WINSYS_HANDLE_TYPE_SHMID;
134
135   if (loader->base.version < 4 || !loader->getImageShm)
136      return FALSE;
137
138   if (!res->screen->resource_get_handle(res->screen, NULL, res, &whandle, PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE))
139      return FALSE;
140
141   loader->getImageShm(dPriv, x, y, width, height, whandle.handle, dPriv->loaderPrivate);
142   return TRUE;
143}
144
145static void
146drisw_update_drawable_info(struct dri_drawable *drawable)
147{
148   __DRIdrawable *dPriv = drawable->dPriv;
149   int x, y;
150
151   get_drawable_info(dPriv, &x, &y, &dPriv->w, &dPriv->h);
152}
153
154static void
155drisw_get_image(struct dri_drawable *drawable,
156                int x, int y, unsigned width, unsigned height, unsigned stride,
157                void *data)
158{
159   __DRIdrawable *dPriv = drawable->dPriv;
160   int draw_x, draw_y, draw_w, draw_h;
161
162   get_drawable_info(dPriv, &draw_x, &draw_y, &draw_w, &draw_h);
163   get_image2(dPriv, x, y, draw_w, draw_h, stride, data);
164}
165
166static void
167drisw_put_image(struct dri_drawable *drawable,
168                void *data, unsigned width, unsigned height)
169{
170   __DRIdrawable *dPriv = drawable->dPriv;
171
172   put_image(dPriv, data, width, height);
173}
174
175static void
176drisw_put_image2(struct dri_drawable *drawable,
177                 void *data, int x, int y, unsigned width, unsigned height,
178                 unsigned stride)
179{
180   __DRIdrawable *dPriv = drawable->dPriv;
181
182   put_image2(dPriv, data, x, y, width, height, stride);
183}
184
185static inline void
186drisw_put_image_shm(struct dri_drawable *drawable,
187                    int shmid, char *shmaddr, unsigned offset,
188                    unsigned offset_x,
189                    int x, int y, unsigned width, unsigned height,
190                    unsigned stride)
191{
192   __DRIdrawable *dPriv = drawable->dPriv;
193
194   put_image_shm(dPriv, shmid, shmaddr, offset, offset_x, x, y, width, height, stride);
195}
196
197static inline void
198drisw_present_texture(__DRIdrawable *dPriv,
199                      struct pipe_resource *ptex, struct pipe_box *sub_box)
200{
201   struct dri_drawable *drawable = dri_drawable(dPriv);
202   struct dri_screen *screen = dri_screen(drawable->sPriv);
203
204   if (screen->swrast_no_present)
205      return;
206
207   screen->base.screen->flush_frontbuffer(screen->base.screen, ptex, 0, 0, drawable, sub_box);
208}
209
210static inline void
211drisw_invalidate_drawable(__DRIdrawable *dPriv)
212{
213   struct dri_drawable *drawable = dri_drawable(dPriv);
214
215   drawable->texture_stamp = dPriv->lastStamp - 1;
216
217   p_atomic_inc(&drawable->base.stamp);
218}
219
220static inline void
221drisw_copy_to_front(__DRIdrawable * dPriv,
222                    struct pipe_resource *ptex)
223{
224   drisw_present_texture(dPriv, ptex, NULL);
225
226   drisw_invalidate_drawable(dPriv);
227}
228
229/*
230 * Backend functions for st_framebuffer interface and swap_buffers.
231 */
232
233static void
234drisw_swap_buffers(__DRIdrawable *dPriv)
235{
236   struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv);
237   struct dri_drawable *drawable = dri_drawable(dPriv);
238   struct pipe_resource *ptex;
239
240   if (!ctx)
241      return;
242
243   ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
244
245   if (ptex) {
246      if (ctx->pp)
247         pp_run(ctx->pp, ptex, ptex, drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL]);
248
249      ctx->st->flush(ctx->st, ST_FLUSH_FRONT, NULL);
250
251      drisw_copy_to_front(dPriv, ptex);
252   }
253}
254
255static void
256drisw_copy_sub_buffer(__DRIdrawable *dPriv, int x, int y,
257                      int w, int h)
258{
259   struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv);
260   struct dri_drawable *drawable = dri_drawable(dPriv);
261   struct pipe_resource *ptex;
262   struct pipe_box box;
263   if (!ctx)
264      return;
265
266   ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
267
268   if (ptex) {
269      if (ctx->pp && drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL])
270         pp_run(ctx->pp, ptex, ptex, drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL]);
271
272      ctx->st->flush(ctx->st, ST_FLUSH_FRONT, NULL);
273
274      u_box_2d(x, dPriv->h - y - h, w, h, &box);
275      drisw_present_texture(dPriv, ptex, &box);
276   }
277}
278
279static void
280drisw_flush_frontbuffer(struct dri_context *ctx,
281                        struct dri_drawable *drawable,
282                        enum st_attachment_type statt)
283{
284   struct pipe_resource *ptex;
285
286   if (!ctx)
287      return;
288
289   ptex = drawable->textures[statt];
290
291   if (ptex) {
292      drisw_copy_to_front(ctx->dPriv, ptex);
293   }
294}
295
296/**
297 * Allocate framebuffer attachments.
298 *
299 * During fixed-size operation, the function keeps allocating new attachments
300 * as they are requested. Unused attachments are not removed, not until the
301 * framebuffer is resized or destroyed.
302 */
303static void
304drisw_allocate_textures(struct dri_context *stctx,
305                        struct dri_drawable *drawable,
306                        const enum st_attachment_type *statts,
307                        unsigned count)
308{
309   struct dri_screen *screen = dri_screen(drawable->sPriv);
310   const __DRIswrastLoaderExtension *loader = drawable->dPriv->driScreenPriv->swrast_loader;
311   struct pipe_resource templ;
312   unsigned width, height;
313   boolean resized;
314   unsigned i;
315
316   width  = drawable->dPriv->w;
317   height = drawable->dPriv->h;
318
319   resized = (drawable->old_w != width ||
320              drawable->old_h != height);
321
322   /* remove outdated textures */
323   if (resized) {
324      for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
325         pipe_resource_reference(&drawable->textures[i], NULL);
326   }
327
328   memset(&templ, 0, sizeof(templ));
329   templ.target = screen->target;
330   templ.width0 = width;
331   templ.height0 = height;
332   templ.depth0 = 1;
333   templ.array_size = 1;
334   templ.last_level = 0;
335
336   for (i = 0; i < count; i++) {
337      enum pipe_format format;
338      unsigned bind;
339
340      /* the texture already exists or not requested */
341      if (drawable->textures[statts[i]])
342         continue;
343
344      dri_drawable_get_format(drawable, statts[i], &format, &bind);
345
346      /* if we don't do any present, no need for display targets */
347      if (statts[i] != ST_ATTACHMENT_DEPTH_STENCIL && !screen->swrast_no_present)
348         bind |= PIPE_BIND_DISPLAY_TARGET;
349
350      if (format == PIPE_FORMAT_NONE)
351         continue;
352
353      templ.format = format;
354      templ.bind = bind;
355
356      if (statts[i] == ST_ATTACHMENT_FRONT_LEFT &&
357          screen->base.screen->resource_create_front &&
358          loader->base.version >= 3) {
359         drawable->textures[statts[i]] =
360            screen->base.screen->resource_create_front(screen->base.screen, &templ, (const void *)drawable);
361      } else
362         drawable->textures[statts[i]] =
363            screen->base.screen->resource_create(screen->base.screen, &templ);
364   }
365
366   drawable->old_w = width;
367   drawable->old_h = height;
368}
369
370static void
371drisw_update_tex_buffer(struct dri_drawable *drawable,
372                        struct dri_context *ctx,
373                        struct pipe_resource *res)
374{
375   __DRIdrawable *dPriv = drawable->dPriv;
376
377   struct st_context *st_ctx = (struct st_context *)ctx->st;
378   struct pipe_context *pipe = st_ctx->pipe;
379   struct pipe_transfer *transfer;
380   char *map;
381   int x, y, w, h;
382   int ximage_stride, line;
383   int cpp = util_format_get_blocksize(res->format);
384
385   get_drawable_info(dPriv, &x, &y, &w, &h);
386
387   map = pipe_transfer_map(pipe, res,
388                           0, 0, // level, layer,
389                           PIPE_TRANSFER_WRITE,
390                           x, y, w, h, &transfer);
391
392   /* Copy the Drawable content to the mapped texture buffer */
393   if (!get_image_shm(dPriv, x, y, w, h, res))
394      get_image(dPriv, x, y, w, h, map);
395
396   /* The pipe transfer has a pitch rounded up to the nearest 64 pixels.
397      get_image() has a pitch rounded up to 4 bytes.  */
398   ximage_stride = ((w * cpp) + 3) & -4;
399   for (line = h-1; line; --line) {
400      memmove(&map[line * transfer->stride],
401              &map[line * ximage_stride],
402              ximage_stride);
403   }
404
405   pipe_transfer_unmap(pipe, transfer);
406}
407
408static __DRIimageExtension driSWImageExtension = {
409    .base = { __DRI_IMAGE, 6 },
410
411    .createImageFromRenderbuffer  = dri2_create_image_from_renderbuffer,
412    .createImageFromTexture = dri2_create_from_texture,
413    .destroyImage = dri2_destroy_image,
414};
415
416/*
417 * Backend function for init_screen.
418 */
419
420static const __DRIextension *drisw_screen_extensions[] = {
421   &driTexBufferExtension.base,
422   &dri2RendererQueryExtension.base,
423   &dri2ConfigQueryExtension.base,
424   &dri2FenceExtension.base,
425   &dri2NoErrorExtension.base,
426   &driSWImageExtension.base,
427   &dri2FlushControlExtension.base,
428   NULL
429};
430
431static const struct drisw_loader_funcs drisw_lf = {
432   .get_image = drisw_get_image,
433   .put_image = drisw_put_image,
434   .put_image2 = drisw_put_image2
435};
436
437static const struct drisw_loader_funcs drisw_shm_lf = {
438   .get_image = drisw_get_image,
439   .put_image = drisw_put_image,
440   .put_image2 = drisw_put_image2,
441   .put_image_shm = drisw_put_image_shm
442};
443
444static const __DRIconfig **
445drisw_init_screen(__DRIscreen * sPriv)
446{
447   const __DRIswrastLoaderExtension *loader = sPriv->swrast_loader;
448   const __DRIconfig **configs;
449   struct dri_screen *screen;
450   struct pipe_screen *pscreen = NULL;
451   const struct drisw_loader_funcs *lf = &drisw_lf;
452
453   screen = CALLOC_STRUCT(dri_screen);
454   if (!screen)
455      return NULL;
456
457   screen->sPriv = sPriv;
458   screen->fd = -1;
459
460   screen->swrast_no_present = debug_get_option_swrast_no_present();
461
462   sPriv->driverPrivate = (void *)screen;
463   sPriv->extensions = drisw_screen_extensions;
464   if (loader->base.version >= 4) {
465      if (loader->putImageShm)
466         lf = &drisw_shm_lf;
467   }
468
469   if (pipe_loader_sw_probe_dri(&screen->dev, lf)) {
470      dri_init_options(screen);
471
472      pscreen = pipe_loader_create_screen(screen->dev);
473   }
474
475   if (!pscreen)
476      goto fail;
477
478   configs = dri_init_screen_helper(screen, pscreen);
479   if (!configs)
480      goto fail;
481
482   screen->lookup_egl_image = dri2_lookup_egl_image;
483
484   return configs;
485fail:
486   dri_destroy_screen_helper(screen);
487   if (screen->dev)
488      pipe_loader_release(&screen->dev, 1);
489   FREE(screen);
490   return NULL;
491}
492
493static boolean
494drisw_create_buffer(__DRIscreen * sPriv,
495                    __DRIdrawable * dPriv,
496                    const struct gl_config * visual, boolean isPixmap)
497{
498   struct dri_drawable *drawable = NULL;
499
500   if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
501      return FALSE;
502
503   drawable = dPriv->driverPrivate;
504
505   drawable->allocate_textures = drisw_allocate_textures;
506   drawable->update_drawable_info = drisw_update_drawable_info;
507   drawable->flush_frontbuffer = drisw_flush_frontbuffer;
508   drawable->update_tex_buffer = drisw_update_tex_buffer;
509
510   return TRUE;
511}
512
513/**
514 * DRI driver virtual function table.
515 *
516 * DRI versions differ in their implementation of init_screen and swap_buffers.
517 */
518const struct __DriverAPIRec galliumsw_driver_api = {
519   .InitScreen = drisw_init_screen,
520   .DestroyScreen = dri_destroy_screen,
521   .CreateContext = dri_create_context,
522   .DestroyContext = dri_destroy_context,
523   .CreateBuffer = drisw_create_buffer,
524   .DestroyBuffer = dri_destroy_buffer,
525   .SwapBuffers = drisw_swap_buffers,
526   .MakeCurrent = dri_make_current,
527   .UnbindContext = dri_unbind_context,
528   .CopySubBuffer = drisw_copy_sub_buffer,
529};
530
531/* This is the table of extensions that the loader will dlsym() for. */
532const __DRIextension *galliumsw_driver_extensions[] = {
533    &driCoreExtension.base,
534    &driSWRastExtension.base,
535    &driCopySubBufferExtension.base,
536    &gallium_config_options.base,
537    NULL
538};
539
540/* vim: set sw=3 ts=8 sts=3 expandtab: */
541