remove unneeded hack
[enigma2.git] / lib / gdi / gpixmap.cpp
1 #include <lib/gdi/gpixmap.h>
2 #include <lib/gdi/region.h>
3 #include <lib/gdi/accel.h>
4
5 gLookup::gLookup()
6         :size(0), lookup(0)
7 {
8 }
9
10 gLookup::gLookup(int size, const gPalette &pal, const gRGB &start, const gRGB &end)
11         :size(0), lookup(0)
12 {
13         build(size, pal, start, end);
14 }
15
16 void gLookup::build(int _size, const gPalette &pal, const gRGB &start, const gRGB &end)
17 {
18         if (lookup)
19         {
20                 delete [] lookup;
21                 lookup=0;
22                 size=0;
23         }
24         size=_size;
25         if (!size)
26                 return;
27         lookup=new gColor[size];
28         
29         for (int i=0; i<size; i++)
30         {
31                 gRGB col;
32                 if (i)
33                 {
34                         int rdiff=-start.r+end.r;
35                         int gdiff=-start.g+end.g;
36                         int bdiff=-start.b+end.b;
37                         int adiff=-start.a+end.a;
38                         rdiff*=i; rdiff/=(size-1);
39                         gdiff*=i; gdiff/=(size-1);
40                         bdiff*=i; bdiff/=(size-1);
41                         adiff*=i; adiff/=(size-1);
42                         col.r=start.r+rdiff;
43                         col.g=start.g+gdiff;
44                         col.b=start.b+bdiff;
45                         col.a=start.a+adiff;
46                 } else
47                         col=start;
48                 lookup[i]=pal.findColor(col);
49         }
50 }
51
52 gSurface::gSurface()
53 {
54         x = 0;
55         y = 0;
56         bpp = 0;
57         bypp = 0;
58         stride = 0;
59         data = 0;
60         data_phys = 0;
61         clut.colors = 0;
62         clut.data = 0;
63         type = 0;
64 }
65
66 gSurface::gSurface(eSize size, int _bpp, int accel)
67 {
68         x = size.width();
69         y = size.height();
70         bpp = _bpp;
71
72         switch (bpp)
73         {
74         case 8:
75                 bypp = 1;
76                 break;
77         case 15:
78         case 16:
79                 bypp = 2;
80                 break;
81         case 24:                // never use 24bit mode
82         case 32:
83                 bypp = 4;
84                 break;
85         default:
86                 bypp = (bpp+7)/8;
87         }
88
89         stride = x*bypp;
90         
91         data = 0;
92         data_phys = 0;
93         
94         if (accel)
95         {
96                 stride += 63;
97                 stride &=~63;
98                 
99                 if (gAccel::getInstance())
100                         eDebug("accel memory: %d", gAccel::getInstance()->accelAlloc(data, data_phys, y * stride));
101                 else
102                         eDebug("no accel available");
103         }
104         
105         clut.colors = 0;
106         clut.data = 0;
107
108         if (!data)
109                 data = malloc(y * stride);
110         
111         type = 1;
112 }
113
114 gSurface::~gSurface()
115 {
116         if (type)
117         {
118                 if (data_phys)
119                         gAccel::getInstance()->accelFree(data_phys);
120                 else
121                         free(data);
122
123                 delete[] clut.data;
124         }
125 }
126
127 gPixmap *gPixmap::lock()
128 {
129         contentlock.lock(1);
130         return this;
131 }
132
133 void gPixmap::unlock()
134 {
135         contentlock.unlock(1);
136 }
137
138 void gPixmap::fill(const gRegion &region, const gColor &color)
139 {
140         unsigned int i;
141         for (i=0; i<region.rects.size(); ++i)
142         {
143                 const eRect &area = region.rects[i];
144                 if ((area.height()<=0) || (area.width()<=0))
145                         continue;
146
147                 if (surface->bpp == 8)
148                 {
149                         for (int y=area.top(); y<area.bottom(); y++)
150                                 memset(((__u8*)surface->data)+y*surface->stride+area.left(), color.color, area.width());
151                 } else if (surface->bpp == 32)
152                 {
153                         __u32 col;
154
155                         if (surface->clut.data && color < surface->clut.colors)
156                                 col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
157                         else
158                                 col=0x10101*color;
159                         
160                         col^=0xFF000000;                        
161
162                         if (surface->data_phys && gAccel::getInstance())
163                                 if (!gAccel::getInstance()->fill(surface,  area, col))
164                                         continue;
165
166                         for (int y=area.top(); y<area.bottom(); y++)
167                         {
168                                 __u32 *dst=(__u32*)(((__u8*)surface->data)+y*surface->stride+area.left()*surface->bypp);
169                                 int x=area.width();
170                                 while (x--)
171                                         *dst++=col;
172                         }
173                 }       else
174                         eWarning("couldn't fill %d bpp", surface->bpp);
175         }
176 }
177
178 void gPixmap::blit(const gPixmap &src, ePoint pos, const gRegion &clip, int flag)
179 {
180         for (unsigned int i=0; i<clip.rects.size(); ++i)
181         {
182                 eRect area=eRect(pos, src.size());
183                 area&=clip.rects[i];
184                 area&=eRect(ePoint(0, 0), size());
185
186                 if ((area.width()<0) || (area.height()<0))
187                         continue;
188
189                 eRect srcarea=area;
190                 srcarea.moveBy(-pos.x(), -pos.y());
191                 
192                 if ((surface->data_phys && src.surface->data_phys) && (gAccel::getInstance()))
193                         if (!gAccel::getInstance()->blit(surface, src.surface, area.topLeft(), srcarea, flag))
194                                 continue;
195                 flag &= ~ blitAlphaBlend;
196                 
197                 if ((surface->bpp == 8) && (src.surface->bpp==8))
198                 {
199                         __u8 *srcptr=(__u8*)src.surface->data;
200                         __u8 *dstptr=(__u8*)surface->data;
201         
202                         srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
203                         dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
204                         for (int y=0; y<area.height(); y++)
205                         {
206                                 if (flag & blitAlphaTest)
207                                 {
208                       // no real alphatest yet
209                                         int width=area.width();
210                                         unsigned char *src=(unsigned char*)srcptr;
211                                         unsigned char *dst=(unsigned char*)dstptr;
212                                                 // use duff's device here!
213                                         while (width--)
214                                         {
215                                                 if (!*src)
216                                                 {
217                                                         src++;
218                                                         dst++;
219                                                 } else
220                                                         *dst++=*src++;
221                                         }
222                                 } else
223                                         memcpy(dstptr, srcptr, area.width()*surface->bypp);
224                                 srcptr+=src.surface->stride;
225                                 dstptr+=surface->stride;
226                         }
227                 } else if ((surface->bpp == 32) && (src.surface->bpp==32))
228                 {
229                         __u32 *srcptr=(__u32*)src.surface->data;
230                         __u32 *dstptr=(__u32*)surface->data;
231         
232                         srcptr+=srcarea.left()+srcarea.top()*src.surface->stride/4;
233                         dstptr+=area.left()+area.top()*surface->stride/4;
234                         for (int y=0; y<area.height(); y++)
235                         {
236                                 if (flag & blitAlphaTest)
237                                 {
238                                         int width=area.width();
239                                         unsigned long *src=(unsigned long*)srcptr;
240                                         unsigned long *dst=(unsigned long*)dstptr;
241
242                                         while (width--)
243                                         {
244                                                 if (!((*src)&0xFF000000))
245                                                 {
246                                                         src++;
247                                                         dst++;
248                                                 } else
249                                                         *dst++=*src++;
250                                         }
251                                 } else if (flag & blitAlphaBlend)
252                                 {
253                                         // uh oh. this is only until hardware accel is working.
254                                         
255                                         int width=area.width();
256                                                         // ARGB color space!
257                                         unsigned char *src=(unsigned char*)srcptr;
258                                         unsigned char *dst=(unsigned char*)dstptr;
259
260 #define BLEND(x, y, a) (y + ((x-y) * a)/256)
261                                         while (width--)
262                                         {
263                                                 unsigned char sa = src[3];
264                                                 unsigned char sr = src[2];
265                                                 unsigned char sg = src[1];
266                                                 unsigned char sb = src[0];
267
268                                                 unsigned char da = dst[3];
269                                                 unsigned char dr = dst[2];
270                                                 unsigned char dg = dst[1];
271                                                 unsigned char db = dst[0];
272                                                 
273                                                 dst[3] = BLEND(0xFF, da, sa);
274                                                 dst[2] = BLEND(sr, dr, sa);
275                                                 dst[1] = BLEND(sg, dg, sa);
276                                                 dst[0] = BLEND(sb, db, sa);
277                                                 
278                                                 src += 4; dst += 4;
279                                         }
280                                 } else
281                                         memcpy(dstptr, srcptr, area.width()*surface->bypp);
282                                 srcptr+=src.surface->stride/4;
283                                 dstptr+=surface->stride/4;
284                         }
285                 } else if ((surface->bpp == 32) && (src.surface->bpp==8))
286                 {       
287                         __u8 *srcptr=(__u8*)src.surface->data;
288                         __u8 *dstptr=(__u8*)surface->data; // !!
289                         __u32 pal[256];
290
291                         for (int i=0; i<256; ++i)
292                         {
293                                 if (src.surface->clut.data && (i<src.surface->clut.colors))
294                                         pal[i]=(src.surface->clut.data[i].a<<24)|(src.surface->clut.data[i].r<<16)|(src.surface->clut.data[i].g<<8)|(src.surface->clut.data[i].b);
295                                 else
296                                         pal[i]=0x010101*i;
297                                 pal[i]^=0xFF000000;
298                         }
299         
300                         srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride;
301                         dstptr+=area.left()*surface->bypp+area.top()*surface->stride;
302                         for (int y=0; y<area.height(); y++)
303                         {
304                                 if (flag & blitAlphaTest)
305                                 {
306                       // no real alphatest yet
307                                         int width=area.width();
308                                         unsigned char *src=(unsigned char*)srcptr;
309                                         __u32 *dst=(__u32*)dstptr;
310                                                 // use duff's device here!
311                                         while (width--)
312                                         {
313                                                 if (*src==0x01)
314                                                 {
315                                                         src++;
316                                                         dst++;
317                                                 } else
318                                                         *dst++=pal[*src++];
319                                         }
320                                 } else
321                                 {
322                                         int width=area.width();
323                                         unsigned char *src=(unsigned char*)srcptr;
324                                         __u32 *dst=(__u32*)dstptr;
325                                         while (width--)
326                                                 *dst++=pal[*src++];
327                                 }
328                                 srcptr+=src.surface->stride;
329                                 dstptr+=surface->stride;
330                         }
331                 } else
332                         eFatal("cannot blit %dbpp from %dbpp", surface->bpp, src.surface->bpp);
333         }
334 }
335
336 void gPixmap::mergePalette(const gPixmap &target)
337 {
338         if ((!surface->clut.colors) || (!target.surface->clut.colors))
339                 return;
340
341         gColor *lookup=new gColor[surface->clut.colors];
342
343         for (int i=0; i<surface->clut.colors; i++)
344                 lookup[i].color=target.surface->clut.findColor(surface->clut.data[i]);
345         
346         delete [] surface->clut.data;
347         surface->clut.colors=target.surface->clut.colors;
348         surface->clut.data=new gRGB[surface->clut.colors];
349         memcpy(surface->clut.data, target.surface->clut.data, sizeof(gRGB)*surface->clut.colors);
350
351         __u8 *dstptr=(__u8*)surface->data;
352
353         for (int ay=0; ay<surface->y; ay++)
354         {
355                 for (int ax=0; ax<surface->x; ax++)
356                         dstptr[ax]=lookup[dstptr[ax]];
357                 dstptr+=surface->stride;
358         }
359         
360         delete [] lookup;
361 }
362
363 static inline int sgn(int a)
364 {
365         if (a < 0)
366                 return -1;
367         else if (!a)
368                 return 0;
369         else
370                 return 1;
371 }
372
373 void gPixmap::line(const gRegion &clip, ePoint start, ePoint dst, gColor color)
374 {
375         __u8 *srf8 = 0;
376         __u32 *srf32 = 0; 
377         int stride = surface->stride;
378         
379         if (clip.rects.empty())
380                 return;
381                 
382         __u32 col;
383         if (surface->bpp == 8)
384         {
385                 srf8 = (__u8*)surface->data;
386         } else if (surface->bpp == 32)
387         {
388                 srf32 = (__u32*)surface->data;
389                 
390                 if (surface->clut.data && color < surface->clut.colors)
391                         col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b);
392                 else
393                         col=0x10101*color;
394                 col^=0xFF000000;                        
395         }
396         
397         int xa = start.x(), ya = start.y(), xb = dst.x(), yb = dst.y();
398         int dx, dy, x, y, s1, s2, e, temp, swap, i;
399         dy=abs(yb-ya);
400         dx=abs(xb-xa);
401         s1=sgn(xb-xa);
402         s2=sgn(yb-ya);
403         x=xa;
404         y=ya;
405         if (dy>dx)
406         {
407                 temp=dx;
408                 dx=dy;
409                 dy=temp;
410                 swap=1;
411         } else
412                 swap=0;
413         e = 2*dy-dx;
414         
415         int lasthit = 0;
416         for(i=1; i<=dx; i++)
417         {
418                                 /* i don't like this clipping loop, but the only */
419                                 /* other choice i see is to calculate the intersections */
420                                 /* before iterating through the pixels. */
421                                 
422                                 /* one could optimize this because of the ordering */
423                                 /* of the bands. */
424                                 
425                 lasthit = 0;
426                 int a = lasthit;
427                 
428                         /* if last pixel was invisble, first check bounding box */
429                 if (a == -1)
430                 {
431                                 /* check if we just got into the bbox again */
432                         if (clip.extends.contains(x, y))
433                                 lasthit = a = 0;
434                         else
435                                 goto fail;
436                 } else if (!clip.rects[a].contains(x, y))
437                 {
438                         do
439                         {
440                                 ++a;
441                                 if (a == clip.rects.size())
442                                         a = 0;
443                                 if (a == lasthit)
444                                 {
445                                         goto fail;
446                                         lasthit = -1;
447                                 }
448                         } while (!clip.rects[a].contains(x, y));
449                         lasthit = a;
450                 }
451                 
452                 if (srf8)
453                         srf8[y * stride + x] = color;
454                 if (srf32)
455                         srf32[y * stride/4 + x] = col;
456 fail:
457                 while (e>=0)
458                 {
459                         if (swap==1) x+=s1;
460                         else y+=s2;
461                         e-=2*dx;
462                 }
463     if (swap==1)
464         y+=s2;
465                 else
466                         x+=s1;
467                 e+=2*dy;
468         }
469 }
470
471 gColor gPalette::findColor(const gRGB &rgb) const
472 {
473                 /* grayscale? */
474         if (!data)
475                 return (rgb.r + rgb.g + rgb.b) / 3;
476         
477         int difference=1<<30, best_choice=0;
478         for (int t=0; t<colors; t++)
479         {
480                 int ttd;
481                 int td=(signed)(rgb.r-data[t].r); td*=td; td*=(255-data[t].a);
482                 ttd=td;
483                 if (ttd>=difference)
484                         continue;
485                 td=(signed)(rgb.g-data[t].g); td*=td; td*=(255-data[t].a);
486                 ttd+=td;
487                 if (ttd>=difference)
488                         continue;
489                 td=(signed)(rgb.b-data[t].b); td*=td; td*=(255-data[t].a);
490                 ttd+=td;
491                 if (ttd>=difference)
492                         continue;
493                 td=(signed)(rgb.a-data[t].a); td*=td; td*=255;
494                 ttd+=td;
495                 if (ttd>=difference)
496                         continue;
497                 if (!ttd)
498                         return t;
499                 difference=ttd;
500                 best_choice=t;
501         }
502         return best_choice;
503 }
504
505 DEFINE_REF(gPixmap);
506
507 gPixmap::~gPixmap()
508 {
509 }
510
511 gPixmap::gPixmap(gSurface *surface): surface(surface)
512 {
513 }
514
515 gPixmap::gPixmap(eSize size, int bpp, int accel)
516 {
517         surface = new gSurface(size, bpp, accel);
518 }
519