fbpict.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. *
  3. * Copyright © 2000 SuSE, Inc.
  4. * Copyright © 2007 Red Hat, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and its
  7. * documentation for any purpose is hereby granted without fee, provided that
  8. * the above copyright notice appear in all copies and that both that
  9. * copyright notice and this permission notice appear in supporting
  10. * documentation, and that the name of SuSE not be used in advertising or
  11. * publicity pertaining to distribution of the software without specific,
  12. * written prior permission. SuSE makes no representations about the
  13. * suitability of this software for any purpose. It is provided "as is"
  14. * without express or implied warranty.
  15. *
  16. * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
  18. * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  21. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22. *
  23. * Author: Keith Packard, SuSE, Inc.
  24. */
  25. #ifdef HAVE_DIX_CONFIG_H
  26. #include <dix-config.h>
  27. #endif
  28. #include <string.h>
  29. #include "fb.h"
  30. #include "picturestr.h"
  31. #include "mipict.h"
  32. #include "fbpict.h"
  33. void
  34. fbComposite(CARD8 op,
  35. PicturePtr pSrc,
  36. PicturePtr pMask,
  37. PicturePtr pDst,
  38. INT16 xSrc,
  39. INT16 ySrc,
  40. INT16 xMask,
  41. INT16 yMask, INT16 xDst, INT16 yDst, CARD16 width, CARD16 height)
  42. {
  43. pixman_image_t *src, *mask, *dest;
  44. int src_xoff, src_yoff;
  45. int msk_xoff, msk_yoff;
  46. int dst_xoff, dst_yoff;
  47. miCompositeSourceValidate(pSrc);
  48. if (pMask)
  49. miCompositeSourceValidate(pMask);
  50. src = image_from_pict(pSrc, FALSE, &src_xoff, &src_yoff);
  51. mask = image_from_pict(pMask, FALSE, &msk_xoff, &msk_yoff);
  52. dest = image_from_pict(pDst, TRUE, &dst_xoff, &dst_yoff);
  53. if (src && dest && !(pMask && !mask)) {
  54. pixman_image_composite(op, src, mask, dest,
  55. xSrc + src_xoff, ySrc + src_yoff,
  56. xMask + msk_xoff, yMask + msk_yoff,
  57. xDst + dst_xoff, yDst + dst_yoff, width, height);
  58. }
  59. free_pixman_pict(pSrc, src);
  60. free_pixman_pict(pMask, mask);
  61. free_pixman_pict(pDst, dest);
  62. }
  63. static pixman_glyph_cache_t *glyphCache;
  64. void
  65. fbDestroyGlyphCache(void)
  66. {
  67. if (glyphCache)
  68. {
  69. pixman_glyph_cache_destroy (glyphCache);
  70. glyphCache = NULL;
  71. }
  72. }
  73. void
  74. fbUnrealizeGlyph(ScreenPtr pScreen,
  75. GlyphPtr pGlyph)
  76. {
  77. if (glyphCache)
  78. pixman_glyph_cache_remove (glyphCache, pGlyph, NULL);
  79. }
  80. void
  81. fbGlyphs(CARD8 op,
  82. PicturePtr pSrc,
  83. PicturePtr pDst,
  84. PictFormatPtr maskFormat,
  85. INT16 xSrc,
  86. INT16 ySrc, int nlist,
  87. GlyphListPtr list,
  88. GlyphPtr *glyphs)
  89. {
  90. #define N_STACK_GLYPHS 512
  91. ScreenPtr pScreen = pDst->pDrawable->pScreen;
  92. pixman_glyph_t stack_glyphs[N_STACK_GLYPHS];
  93. pixman_glyph_t *pglyphs = stack_glyphs;
  94. pixman_image_t *srcImage, *dstImage;
  95. int srcXoff, srcYoff, dstXoff, dstYoff;
  96. GlyphPtr glyph;
  97. int n_glyphs;
  98. int x, y;
  99. int i, n;
  100. int xDst = list->xOff, yDst = list->yOff;
  101. miCompositeSourceValidate(pSrc);
  102. n_glyphs = 0;
  103. for (i = 0; i < nlist; ++i)
  104. n_glyphs += list[i].len;
  105. if (!glyphCache)
  106. glyphCache = pixman_glyph_cache_create();
  107. pixman_glyph_cache_freeze (glyphCache);
  108. if (n_glyphs > N_STACK_GLYPHS) {
  109. if (!(pglyphs = malloc (n_glyphs * sizeof (pixman_glyph_t))))
  110. goto out;
  111. }
  112. i = 0;
  113. x = y = 0;
  114. while (nlist--) {
  115. x += list->xOff;
  116. y += list->yOff;
  117. n = list->len;
  118. while (n--) {
  119. const void *g;
  120. glyph = *glyphs++;
  121. if (!(g = pixman_glyph_cache_lookup (glyphCache, glyph, NULL))) {
  122. pixman_image_t *glyphImage;
  123. PicturePtr pPicture;
  124. int xoff, yoff;
  125. pPicture = GetGlyphPicture(glyph, pScreen);
  126. if (!pPicture) {
  127. n_glyphs--;
  128. goto next;
  129. }
  130. if (!(glyphImage = image_from_pict(pPicture, FALSE, &xoff, &yoff)))
  131. goto out;
  132. g = pixman_glyph_cache_insert(glyphCache, glyph, NULL,
  133. glyph->info.x,
  134. glyph->info.y,
  135. glyphImage);
  136. free_pixman_pict(pPicture, glyphImage);
  137. if (!g)
  138. goto out;
  139. }
  140. pglyphs[i].x = x;
  141. pglyphs[i].y = y;
  142. pglyphs[i].glyph = g;
  143. i++;
  144. next:
  145. x += glyph->info.xOff;
  146. y += glyph->info.yOff;
  147. }
  148. list++;
  149. }
  150. if (!(srcImage = image_from_pict(pSrc, FALSE, &srcXoff, &srcYoff)))
  151. goto out;
  152. if (!(dstImage = image_from_pict(pDst, TRUE, &dstXoff, &dstYoff)))
  153. goto out_free_src;
  154. if (maskFormat) {
  155. pixman_format_code_t format;
  156. pixman_box32_t extents;
  157. format = maskFormat->format | (maskFormat->depth << 24);
  158. pixman_glyph_get_extents(glyphCache, n_glyphs, pglyphs, &extents);
  159. pixman_composite_glyphs(op, srcImage, dstImage, format,
  160. xSrc + srcXoff + extents.x1 - xDst, ySrc + srcYoff + extents.y1 - yDst,
  161. extents.x1, extents.y1,
  162. extents.x1 + dstXoff, extents.y1 + dstYoff,
  163. extents.x2 - extents.x1,
  164. extents.y2 - extents.y1,
  165. glyphCache, n_glyphs, pglyphs);
  166. }
  167. else {
  168. pixman_composite_glyphs_no_mask(op, srcImage, dstImage,
  169. xSrc + srcXoff - xDst, ySrc + srcYoff - yDst,
  170. dstXoff, dstYoff,
  171. glyphCache, n_glyphs, pglyphs);
  172. }
  173. free_pixman_pict(pDst, dstImage);
  174. out_free_src:
  175. free_pixman_pict(pSrc, srcImage);
  176. out:
  177. pixman_glyph_cache_thaw(glyphCache);
  178. if (pglyphs != stack_glyphs)
  179. free(pglyphs);
  180. }
  181. static pixman_image_t *
  182. create_solid_fill_image(PicturePtr pict)
  183. {
  184. PictSolidFill *solid = &pict->pSourcePict->solidFill;
  185. pixman_color_t color;
  186. CARD32 a, r, g, b;
  187. a = (solid->color & 0xff000000) >> 24;
  188. r = (solid->color & 0x00ff0000) >> 16;
  189. g = (solid->color & 0x0000ff00) >> 8;
  190. b = (solid->color & 0x000000ff) >> 0;
  191. color.alpha = (a << 8) | a;
  192. color.red = (r << 8) | r;
  193. color.green = (g << 8) | g;
  194. color.blue = (b << 8) | b;
  195. return pixman_image_create_solid_fill(&color);
  196. }
  197. static pixman_image_t *
  198. create_linear_gradient_image(PictGradient * gradient)
  199. {
  200. PictLinearGradient *linear = (PictLinearGradient *) gradient;
  201. pixman_point_fixed_t p1;
  202. pixman_point_fixed_t p2;
  203. p1.x = linear->p1.x;
  204. p1.y = linear->p1.y;
  205. p2.x = linear->p2.x;
  206. p2.y = linear->p2.y;
  207. return pixman_image_create_linear_gradient(&p1, &p2,
  208. (pixman_gradient_stop_t *)
  209. gradient->stops,
  210. gradient->nstops);
  211. }
  212. static pixman_image_t *
  213. create_radial_gradient_image(PictGradient * gradient)
  214. {
  215. PictRadialGradient *radial = (PictRadialGradient *) gradient;
  216. pixman_point_fixed_t c1;
  217. pixman_point_fixed_t c2;
  218. c1.x = radial->c1.x;
  219. c1.y = radial->c1.y;
  220. c2.x = radial->c2.x;
  221. c2.y = radial->c2.y;
  222. return pixman_image_create_radial_gradient(&c1, &c2, radial->c1.radius,
  223. radial->c2.radius,
  224. (pixman_gradient_stop_t *)
  225. gradient->stops,
  226. gradient->nstops);
  227. }
  228. static pixman_image_t *
  229. create_conical_gradient_image(PictGradient * gradient)
  230. {
  231. PictConicalGradient *conical = (PictConicalGradient *) gradient;
  232. pixman_point_fixed_t center;
  233. center.x = conical->center.x;
  234. center.y = conical->center.y;
  235. return pixman_image_create_conical_gradient(&center, conical->angle,
  236. (pixman_gradient_stop_t *)
  237. gradient->stops,
  238. gradient->nstops);
  239. }
  240. static pixman_image_t *
  241. create_bits_picture(PicturePtr pict, Bool has_clip, int *xoff, int *yoff)
  242. {
  243. PixmapPtr pixmap;
  244. FbBits *bits;
  245. FbStride stride;
  246. int bpp;
  247. pixman_image_t *image;
  248. fbGetDrawablePixmap(pict->pDrawable, pixmap, *xoff, *yoff);
  249. fbGetPixmapBitsData(pixmap, bits, stride, bpp);
  250. image = pixman_image_create_bits((pixman_format_code_t) pict->format,
  251. pixmap->drawable.width,
  252. pixmap->drawable.height, (uint32_t *) bits,
  253. stride * sizeof(FbStride));
  254. if (!image)
  255. return NULL;
  256. #ifdef FB_ACCESS_WRAPPER
  257. #if FB_SHIFT==5
  258. pixman_image_set_accessors(image,
  259. (pixman_read_memory_func_t) wfbReadMemory,
  260. (pixman_write_memory_func_t) wfbWriteMemory);
  261. #else
  262. #error The pixman library only works when FbBits is 32 bits wide
  263. #endif
  264. #endif
  265. /* pCompositeClip is undefined for source pictures, so
  266. * only set the clip region for pictures with drawables
  267. */
  268. if (has_clip) {
  269. if (pict->clientClipType != CT_NONE)
  270. pixman_image_set_has_client_clip(image, TRUE);
  271. if (*xoff || *yoff)
  272. pixman_region_translate(pict->pCompositeClip, *xoff, *yoff);
  273. pixman_image_set_clip_region(image, pict->pCompositeClip);
  274. if (*xoff || *yoff)
  275. pixman_region_translate(pict->pCompositeClip, -*xoff, -*yoff);
  276. }
  277. /* Indexed table */
  278. if (pict->pFormat->index.devPrivate)
  279. pixman_image_set_indexed(image, pict->pFormat->index.devPrivate);
  280. /* Add in drawable origin to position within the image */
  281. *xoff += pict->pDrawable->x;
  282. *yoff += pict->pDrawable->y;
  283. return image;
  284. }
  285. static pixman_image_t *image_from_pict_internal(PicturePtr pict, Bool has_clip,
  286. int *xoff, int *yoff,
  287. Bool is_alpha_map);
  288. static void
  289. set_image_properties(pixman_image_t * image, PicturePtr pict, Bool has_clip,
  290. int *xoff, int *yoff, Bool is_alpha_map)
  291. {
  292. pixman_repeat_t repeat;
  293. pixman_filter_t filter;
  294. if (pict->transform) {
  295. /* For source images, adjust the transform to account
  296. * for the drawable offset within the pixman image,
  297. * then set the offset to 0 as it will be used
  298. * to compute positions within the transformed image.
  299. */
  300. if (!has_clip) {
  301. struct pixman_transform adjusted;
  302. adjusted = *pict->transform;
  303. pixman_transform_translate(&adjusted,
  304. NULL,
  305. pixman_int_to_fixed(*xoff),
  306. pixman_int_to_fixed(*yoff));
  307. pixman_image_set_transform(image, &adjusted);
  308. *xoff = 0;
  309. *yoff = 0;
  310. }
  311. else
  312. pixman_image_set_transform(image, pict->transform);
  313. }
  314. switch (pict->repeatType) {
  315. default:
  316. case RepeatNone:
  317. repeat = PIXMAN_REPEAT_NONE;
  318. break;
  319. case RepeatPad:
  320. repeat = PIXMAN_REPEAT_PAD;
  321. break;
  322. case RepeatNormal:
  323. repeat = PIXMAN_REPEAT_NORMAL;
  324. break;
  325. case RepeatReflect:
  326. repeat = PIXMAN_REPEAT_REFLECT;
  327. break;
  328. }
  329. pixman_image_set_repeat(image, repeat);
  330. /* Fetch alpha map unless 'pict' is being used
  331. * as the alpha map for this operation
  332. */
  333. if (pict->alphaMap && !is_alpha_map) {
  334. int alpha_xoff, alpha_yoff;
  335. pixman_image_t *alpha_map =
  336. image_from_pict_internal(pict->alphaMap, FALSE, &alpha_xoff,
  337. &alpha_yoff, TRUE);
  338. pixman_image_set_alpha_map(image, alpha_map, pict->alphaOrigin.x,
  339. pict->alphaOrigin.y);
  340. free_pixman_pict(pict->alphaMap, alpha_map);
  341. }
  342. pixman_image_set_component_alpha(image, pict->componentAlpha);
  343. switch (pict->filter) {
  344. default:
  345. case PictFilterNearest:
  346. case PictFilterFast:
  347. filter = PIXMAN_FILTER_NEAREST;
  348. break;
  349. case PictFilterBilinear:
  350. case PictFilterGood:
  351. filter = PIXMAN_FILTER_BILINEAR;
  352. break;
  353. case PictFilterConvolution:
  354. filter = PIXMAN_FILTER_CONVOLUTION;
  355. break;
  356. }
  357. pixman_image_set_filter(image, filter,
  358. (pixman_fixed_t *) pict->filter_params,
  359. pict->filter_nparams);
  360. pixman_image_set_source_clipping(image, TRUE);
  361. }
  362. static pixman_image_t *
  363. image_from_pict_internal(PicturePtr pict, Bool has_clip, int *xoff, int *yoff,
  364. Bool is_alpha_map)
  365. {
  366. pixman_image_t *image = NULL;
  367. if (!pict)
  368. return NULL;
  369. if (pict->pDrawable) {
  370. image = create_bits_picture(pict, has_clip, xoff, yoff);
  371. }
  372. else if (pict->pSourcePict) {
  373. SourcePict *sp = pict->pSourcePict;
  374. if (sp->type == SourcePictTypeSolidFill) {
  375. image = create_solid_fill_image(pict);
  376. }
  377. else {
  378. PictGradient *gradient = &pict->pSourcePict->gradient;
  379. if (sp->type == SourcePictTypeLinear)
  380. image = create_linear_gradient_image(gradient);
  381. else if (sp->type == SourcePictTypeRadial)
  382. image = create_radial_gradient_image(gradient);
  383. else if (sp->type == SourcePictTypeConical)
  384. image = create_conical_gradient_image(gradient);
  385. }
  386. *xoff = *yoff = 0;
  387. }
  388. if (image)
  389. set_image_properties(image, pict, has_clip, xoff, yoff, is_alpha_map);
  390. return image;
  391. }
  392. pixman_image_t *
  393. image_from_pict(PicturePtr pict, Bool has_clip, int *xoff, int *yoff)
  394. {
  395. return image_from_pict_internal(pict, has_clip, xoff, yoff, FALSE);
  396. }
  397. void
  398. free_pixman_pict(PicturePtr pict, pixman_image_t * image)
  399. {
  400. if (image && pixman_image_unref(image) && pict->pDrawable)
  401. fbFinishAccess(pict->pDrawable);
  402. }
  403. Bool
  404. fbPictureInit(ScreenPtr pScreen, PictFormatPtr formats, int nformats)
  405. {
  406. PictureScreenPtr ps;
  407. if (!miPictureInit(pScreen, formats, nformats))
  408. return FALSE;
  409. ps = GetPictureScreen(pScreen);
  410. ps->Composite = fbComposite;
  411. ps->Glyphs = fbGlyphs;
  412. ps->UnrealizeGlyph = fbUnrealizeGlyph;
  413. ps->CompositeRects = miCompositeRects;
  414. ps->RasterizeTrapezoid = fbRasterizeTrapezoid;
  415. ps->Trapezoids = fbTrapezoids;
  416. ps->AddTraps = fbAddTraps;
  417. ps->AddTriangles = fbAddTriangles;
  418. ps->Triangles = fbTriangles;
  419. return TRUE;
  420. }