aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/evas/src/modules/engines/software_ddraw/evas_outbuf.c
blob: 5a5f0a289a9694dcd8df4cb68e6c112a8fb032a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include "evas_common.h"
#include "evas_engine.h"


static Eina_List *ddpool = NULL;
static int ddsize = 0;
static int ddmemlimit = 10 * 1024 * 1024;
static int ddcountlimit = 32;

static DD_Output_Buffer *
_find_ddob(int depth, int w, int h, void *data)
{
   Eina_List        *l;
   Eina_List        *ddl;
   DD_Output_Buffer *ddob = NULL;
   DD_Output_Buffer *ddob2;
   int               sz;
   int               lbytes;
   int               bpp;

   bpp = depth / 8;
   if (bpp == 3) bpp = 4;
   lbytes = (((w * bpp) + 3) / 4) * 4;
   sz = lbytes * h;
   EINA_LIST_FOREACH(ddpool, l, ddob2)
     {
	if (ddob2->depth != depth)
	  continue;
	if (ddob2->psize == sz)
	  {
	     ddob = ddob2;
	     ddl = l;
	     goto have_ddob;
	  }
     }
   if (!ddob)
     return evas_software_ddraw_output_buffer_new(depth, w, h, data);

   have_ddob:
   ddpool = eina_list_remove_list(ddpool, ddl);
   ddob->width = w;
   ddob->height = h;
   ddob->pitch = lbytes;
   ddsize -= ddob->psize * (ddob->depth / 8);

   return ddob;
}

static void
_unfind_ddob(DD_Output_Buffer *ddob)
{
   ddpool = eina_list_prepend(ddpool, ddob);
   ddsize += ddob->psize * ddob->depth / 8;
   while ((ddsize > (ddmemlimit)) ||
          (eina_list_count(ddpool) > ddcountlimit))
     {
        Eina_List *xl;

        xl = eina_list_last(ddpool);
        if (!xl)
          {
             ddsize = 0;
             break;
          }
        ddob = xl->data;
        ddpool = eina_list_remove_list(ddpool, xl);
        evas_software_ddraw_output_buffer_free(ddob);
     }
}

static void
_clear_ddob(int sync)
{
   while (ddpool)
     {
	DD_Output_Buffer *ddob;

	ddob = ddpool->data;
	ddpool = eina_list_remove_list(ddpool, ddpool);
	evas_software_ddraw_output_buffer_free(ddob);
     }
   ddsize = 0;
}

void
evas_software_ddraw_outbuf_init(void)
{
}

void
evas_software_ddraw_outbuf_free(Outbuf *buf)
{
   if (!buf)
     return;

   evas_software_ddraw_shutdown(buf);
   free(buf);
}

Outbuf *
evas_software_ddraw_outbuf_setup(int          width,
                                 int          height,
                                 int          rotation,
                                 Outbuf_Depth depth,
                                 HWND         window,
                                 int          w_depth,
                                 int          fullscreen)
{
   Outbuf *buf;

   buf = (Outbuf *)calloc(1, sizeof(Outbuf));
   if (!buf)
      return NULL;

   buf->width = width;
   buf->height = height;
   buf->depth = depth;
   buf->rot = rotation;

   if (!evas_software_ddraw_init(window, w_depth, fullscreen, buf))
     {
        free(buf);
        return NULL;
     }

   {
      Gfx_Func_Convert  conv_func;
      DD_Output_Buffer *ddob;

      ddob = evas_software_ddraw_output_buffer_new(w_depth, 1, 1, NULL);

      conv_func = NULL;
      if (ddob)
        {
           if (evas_software_ddraw_masks_get(buf))
             {
                if ((rotation == 0) || (rotation == 180))
                  conv_func = evas_common_convert_func_get(0,
                                                           width,
                                                           height,
                                                           evas_software_ddraw_output_buffer_depth (ddob),
                                                           buf->priv.mask.r,
                                                           buf->priv.mask.g,
                                                           buf->priv.mask.b,
                                                           PAL_MODE_NONE,
                                                           rotation);
                else if ((rotation == 90) || (rotation == 270))
                  conv_func = evas_common_convert_func_get(0,
                                                           height,
                                                           width,
                                                           evas_software_ddraw_output_buffer_depth (ddob),
                                                           buf->priv.mask.r,
                                                           buf->priv.mask.g,
                                                           buf->priv.mask.b,
                                                           PAL_MODE_NONE,
                                                           rotation);
             }

           evas_software_ddraw_output_buffer_free(ddob);

           if (!conv_func)
             {
                ERR("DDraw engine Error"
                    " {"
                    "  At depth         %i:"
                    "  RGB format mask: %08x, %08x, %08x"
                    "  Not supported by and compiled in converters!"
                    " }",
                    buf->priv.dd.depth,
                    buf->priv.mask.r,
                    buf->priv.mask.g,
                    buf->priv.mask.b);
             }
        }
   }

   return buf;
}

void
evas_software_ddraw_outbuf_reconfigure(Outbuf      *buf,
                                       int          width,
                                       int          height,
                                       int          rotation,
                                       Outbuf_Depth depth)
{
   if ((width == buf->width) && (height == buf->height) &&
       (rotation == buf->rot) && (depth == buf->depth))
     return;
   buf->width = width;
   buf->height = height;
   buf->rot = rotation;
   evas_software_ddraw_surface_resize(buf);
}

RGBA_Image *
evas_software_ddraw_outbuf_new_region_for_update(Outbuf *buf,
                                                 int     x,
                                                 int     y,
                                                 int     w,
                                                 int     h,
                                                 int    *cx,
                                                 int    *cy,
                                                 int    *cw,
                                                 int    *ch)
{
   RGBA_Image    *im;
   Outbuf_Region *obr;
   int            bpl = 0;
   int            alpha = 0;

   obr = calloc(1, sizeof(Outbuf_Region));
   obr->x = x;
   obr->y = y;
   obr->width = w;
   obr->height = h;
   *cx = 0;
   *cy = 0;
   *cw = w;
   *ch = h;

   if ((buf->rot == 0) &&
       (buf->priv.mask.r == 0xff0000) &&
       (buf->priv.mask.g == 0x00ff00) &&
       (buf->priv.mask.b == 0x0000ff))
     {
        obr->ddob = _find_ddob(buf->priv.dd.depth, w, h, NULL);
/*      obr->ddob = evas_software_x11_x_output_buffer_new(buf->priv.dd.disp, */
/*                                                         buf->priv.dd.vis, */
/*                                                         buf->priv.dd.depth, */
/*                                                         w, h, */
/*                                                         use_shm, */
/*                                                         NULL); */
        im = (RGBA_Image *)evas_cache_image_data(evas_common_image_cache_get(),
                                                 w, h,
                                                 (DATA32 *) evas_software_ddraw_output_buffer_data(obr->ddob, &bpl),
                                                 alpha, EVAS_COLORSPACE_ARGB8888);
        im->extended_info = obr;
     }
   else
     {
        im = (RGBA_Image *) evas_cache_image_empty(evas_common_image_cache_get());
        im->cache_entry.flags.alpha |= alpha ? 1 : 0;
        evas_cache_image_surface_alloc(&im->cache_entry, w, h);
        im->extended_info = obr;
        if ((buf->rot == 0) || (buf->rot == 180))
          obr->ddob = _find_ddob(buf->priv.dd.depth, w, h, NULL);
/*
          obr->ddob = evas_software_x11_x_output_buffer_new(buf->priv.dd.disp,
                                                           buf->priv.dd.vis,
                                                           buf->priv.dd.depth,
                                                           w, h,
                                                           use_shm,
                                                           NULL);
 */
        else if ((buf->rot == 90) || (buf->rot == 270))
          obr->ddob = _find_ddob(buf->priv.dd.depth, h, w, NULL);
/*
          obr->ddob = evas_software_x11_x_output_buffer_new(buf->priv.dd.disp,
                                                           buf->priv.dd.vis,
                                                           buf->priv.dd.depth,
                                                           h, w,
                                                           use_shm,
                                                           NULL);
 */
     }

   buf->priv.pending_writes = eina_list_append(buf->priv.pending_writes, im);
   return im;
}

void
evas_software_ddraw_outbuf_push_updated_region(Outbuf     *buf,
                                               RGBA_Image *update,
                                               int        x,
                                               int        y,
                                               int        w,
                                               int        h)
{
   Gfx_Func_Convert    conv_func;
   Outbuf_Region      *obr;
   DATA32             *src_data;
   void               *data;
   int                 bpl = 0;

   conv_func = NULL;
   obr = update->extended_info;

   if ((buf->rot == 0) || (buf->rot == 180))
     conv_func = evas_common_convert_func_get(0, w, h,
                                              evas_software_ddraw_output_buffer_depth(obr->ddob),
                                              buf->priv.mask.r,
                                              buf->priv.mask.g,
                                              buf->priv.mask.b,
                                              PAL_MODE_NONE,
                                              buf->rot);
   else if ((buf->rot == 90) || (buf->rot == 270))
     conv_func = evas_common_convert_func_get(0, h, w,
                                              evas_software_ddraw_output_buffer_depth(obr->ddob),
                                              buf->priv.mask.r,
                                              buf->priv.mask.g,
                                              buf->priv.mask.b,
                                              PAL_MODE_NONE, buf->rot);
   if (!conv_func) return;

   data = evas_software_ddraw_output_buffer_data(obr->ddob, &bpl);
   src_data = update->image.data;
   if (buf->rot == 0)
     {
	obr->x = x;
	obr->y = y;
     }
   else if (buf->rot == 90)
     {
	obr->x = y;
	obr->y = buf->width - x - w;
     }
   else if (buf->rot == 180)
     {
	obr->x = buf->width - x - w;
	obr->y = buf->height - y - h;
     }
   else if (buf->rot == 270)
     {
	obr->x = buf->height - y - h;
	obr->y = x;
     }
   if ((buf->rot == 0) || (buf->rot == 180))
     {
	obr->width = w;
	obr->height = h;
     }
   else if ((buf->rot == 90) || (buf->rot == 270))
     {
	obr->width = h;
	obr->height = w;
     }

   if (data != src_data)
     conv_func(src_data, data,
               0,
               bpl / ((evas_software_ddraw_output_buffer_depth(obr->ddob) / 8)) - obr->width,
               obr->width,
               obr->height,
               x,
               y,
               NULL);
}

void
evas_software_ddraw_outbuf_free_region_for_update(Outbuf     *buf,
                                                  RGBA_Image *update)
{
   /* no need to do anything - they are cleaned up on flush */
}

void
evas_software_ddraw_outbuf_flush(Outbuf *buf)
{
   Eina_List *l;
   RGBA_Image       *im;
   Outbuf_Region    *obr;
   void      *ddraw_data;
   int        ddraw_width;
   int        ddraw_height;
   int        ddraw_pitch;
   int        ddraw_depth;

   /* lock the back surface */
   if (!(ddraw_data = evas_software_ddraw_lock(buf,
                                               &ddraw_width,
                                               &ddraw_height,
                                               &ddraw_pitch,
                                               &ddraw_depth)))
     goto free_images;

   /* copy safely the images that need to be drawn onto the back surface */
   EINA_LIST_FOREACH(buf->priv.pending_writes, l, im)
     {
	DD_Output_Buffer *ddob;

        obr = im->extended_info;
        ddob = obr->ddob;
        evas_software_ddraw_output_buffer_paste(ddob,
                                                ddraw_data,
                                                ddraw_width,
                                                ddraw_height,
                                                ddraw_pitch,
                                                ddraw_depth,
						obr->x,
						obr->y);
     }

   /* unlock the back surface and flip the surface */
   evas_software_ddraw_unlock_and_flip(buf);

 free_images:
   while (buf->priv.prev_pending_writes)
     {
        im = buf->priv.prev_pending_writes->data;
        buf->priv.prev_pending_writes =
          eina_list_remove_list(buf->priv.prev_pending_writes,
                                buf->priv.prev_pending_writes);
        obr = im->extended_info;
        evas_cache_image_drop(&im->cache_entry);
        if (obr->ddob) _unfind_ddob(obr->ddob);
/*
	     if (obr->ddob) evas_software_x11_x_output_buffer_free(obr->ddob);
 */
        free(obr);
     }
   buf->priv.prev_pending_writes = buf->priv.pending_writes;
   buf->priv.pending_writes = NULL;

   evas_common_cpu_end_opt();
}

void
evas_software_ddraw_outbuf_idle_flush(Outbuf *buf)
{
   while (buf->priv.prev_pending_writes)
     {
        RGBA_Image *im;
        Outbuf_Region *obr;

        im = buf->priv.prev_pending_writes->data;
        buf->priv.prev_pending_writes =
          eina_list_remove_list(buf->priv.prev_pending_writes,
                                buf->priv.prev_pending_writes);
        obr = im->extended_info;
        evas_cache_image_drop((Image_Entry *)im);
        if (obr->ddob) _unfind_ddob(obr->ddob);
        free(obr);
     }
   _clear_ddob(0);
}

int
evas_software_ddraw_outbuf_width_get(Outbuf *buf)
{
   return buf->width;
}

int
evas_software_ddraw_outbuf_height_get(Outbuf *buf)
{
   return buf->height;
}

Outbuf_Depth
evas_software_ddraw_outbuf_depth_get(Outbuf *buf)
{
   return buf->depth;
}

int
evas_software_ddraw_outbuf_rot_get(Outbuf *buf)
{
   return buf->rot;
}