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