picture.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  1. /*
  2. *
  3. * Copyright © 2000 SuSE, Inc.
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and its
  6. * documentation for any purpose is hereby granted without fee, provided that
  7. * the above copyright notice appear in all copies and that both that
  8. * copyright notice and this permission notice appear in supporting
  9. * documentation, and that the name of SuSE not be used in advertising or
  10. * publicity pertaining to distribution of the software without specific,
  11. * written prior permission. SuSE makes no representations about the
  12. * suitability of this software for any purpose. It is provided "as is"
  13. * without express or implied warranty.
  14. *
  15. * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
  17. * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  18. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  19. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  20. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21. *
  22. * Author: Keith Packard, SuSE, Inc.
  23. */
  24. #ifdef HAVE_DIX_CONFIG_H
  25. #include <dix-config.h>
  26. #endif
  27. #include "misc.h"
  28. #include "scrnintstr.h"
  29. #include "os.h"
  30. #include "regionstr.h"
  31. #include "validate.h"
  32. #include "windowstr.h"
  33. #include "input.h"
  34. #include "resource.h"
  35. #include "colormapst.h"
  36. #include "cursorstr.h"
  37. #include "dixstruct.h"
  38. #include "gcstruct.h"
  39. #include "servermd.h"
  40. #include "picturestr.h"
  41. _X_EXPORT int PictureScreenPrivateIndex = -1;
  42. int PictureWindowPrivateIndex;
  43. int PictureGeneration;
  44. RESTYPE PictureType;
  45. RESTYPE PictFormatType;
  46. RESTYPE GlyphSetType;
  47. int PictureCmapPolicy = PictureCmapPolicyDefault;
  48. /* Picture Private machinery */
  49. static int picturePrivateCount;
  50. void
  51. ResetPicturePrivateIndex(void)
  52. {
  53. picturePrivateCount = 0;
  54. }
  55. Bool
  56. PictureDestroyWindow(WindowPtr pWindow)
  57. {
  58. ScreenPtr pScreen = pWindow->drawable.pScreen;
  59. PicturePtr pPicture;
  60. PictureScreenPtr ps = GetPictureScreen(pScreen);
  61. Bool ret;
  62. while ((pPicture = GetPictureWindow(pWindow))) {
  63. SetPictureWindow(pWindow, pPicture->pNext);
  64. if (pPicture->id)
  65. FreeResource(pPicture->id, PictureType);
  66. FreePicture((pointer) pPicture, pPicture->id);
  67. }
  68. pScreen->DestroyWindow = ps->DestroyWindow;
  69. ret = (*pScreen->DestroyWindow) (pWindow);
  70. ps->DestroyWindow = pScreen->DestroyWindow;
  71. pScreen->DestroyWindow = PictureDestroyWindow;
  72. return ret;
  73. }
  74. Bool
  75. PictureCloseScreen(int index, ScreenPtr pScreen)
  76. {
  77. PictureScreenPtr ps = GetPictureScreen(pScreen);
  78. Bool ret;
  79. int n;
  80. pScreen->CloseScreen = ps->CloseScreen;
  81. ret = (*pScreen->CloseScreen) (index, pScreen);
  82. PictureResetFilters(pScreen);
  83. for (n = 0; n < ps->nformats; n++)
  84. if (ps->formats[n].type == PictTypeIndexed)
  85. (*ps->CloseIndexed) (pScreen, &ps->formats[n]);
  86. GlyphUninit(pScreen);
  87. SetPictureScreen(pScreen, 0);
  88. if (ps->PicturePrivateSizes)
  89. free(ps->PicturePrivateSizes);
  90. free(ps->formats);
  91. free(ps);
  92. return ret;
  93. }
  94. void
  95. PictureStoreColors(ColormapPtr pColormap, int ndef, xColorItem * pdef)
  96. {
  97. ScreenPtr pScreen = pColormap->pScreen;
  98. PictureScreenPtr ps = GetPictureScreen(pScreen);
  99. pScreen->StoreColors = ps->StoreColors;
  100. (*pScreen->StoreColors) (pColormap, ndef, pdef);
  101. ps->StoreColors = pScreen->StoreColors;
  102. pScreen->StoreColors = PictureStoreColors;
  103. if (pColormap->class == PseudoColor || pColormap->class == GrayScale) {
  104. PictFormatPtr format = ps->formats;
  105. int nformats = ps->nformats;
  106. while (nformats--) {
  107. if (format->type == PictTypeIndexed &&
  108. format->index.pColormap == pColormap) {
  109. (*ps->UpdateIndexed) (pScreen, format, ndef, pdef);
  110. break;
  111. }
  112. format++;
  113. }
  114. }
  115. }
  116. static int
  117. visualDepth(ScreenPtr pScreen, VisualPtr pVisual)
  118. {
  119. int d, v;
  120. DepthPtr pDepth;
  121. for (d = 0; d < pScreen->numDepths; d++) {
  122. pDepth = &pScreen->allowedDepths[d];
  123. for (v = 0; v < pDepth->numVids; v++)
  124. if (pDepth->vids[v] == pVisual->vid)
  125. return pDepth->depth;
  126. }
  127. return 0;
  128. }
  129. typedef struct _formatInit {
  130. CARD32 format;
  131. CARD8 depth;
  132. } FormatInitRec, *FormatInitPtr;
  133. static int
  134. addFormat(FormatInitRec formats[256], int nformat, CARD32 format, CARD8 depth)
  135. {
  136. int n;
  137. for (n = 0; n < nformat; n++)
  138. if (formats[n].format == format && formats[n].depth == depth)
  139. return nformat;
  140. formats[nformat].format = format;
  141. formats[nformat].depth = depth;
  142. return ++nformat;
  143. }
  144. #define Mask(n) ((n) == 32 ? 0xffffffff : ((1 << (n))-1))
  145. PictFormatPtr
  146. PictureCreateDefaultFormats(ScreenPtr pScreen, int *nformatp)
  147. {
  148. int nformats, f;
  149. PictFormatPtr pFormats;
  150. FormatInitRec formats[1024];
  151. CARD32 format;
  152. CARD8 depth;
  153. VisualPtr pVisual;
  154. int v;
  155. int bpp;
  156. int type;
  157. int r, g, b;
  158. int d;
  159. DepthPtr pDepth;
  160. nformats = 0;
  161. /* formats required by protocol */
  162. formats[nformats].format = PICT_a1;
  163. formats[nformats].depth = 1;
  164. nformats++;
  165. formats[nformats].format = PICT_FORMAT(BitsPerPixel(8),
  166. PICT_TYPE_A, 8, 0, 0, 0);
  167. formats[nformats].depth = 8;
  168. nformats++;
  169. formats[nformats].format = PICT_FORMAT(BitsPerPixel(4),
  170. PICT_TYPE_A, 4, 0, 0, 0);
  171. formats[nformats].depth = 4;
  172. nformats++;
  173. formats[nformats].format = PICT_a8r8g8b8;
  174. formats[nformats].depth = 32;
  175. nformats++;
  176. formats[nformats].format = PICT_x8r8g8b8;
  177. formats[nformats].depth = 32;
  178. nformats++;
  179. /* now look through the depths and visuals adding other formats */
  180. for (v = 0; v < pScreen->numVisuals; v++) {
  181. pVisual = &pScreen->visuals[v];
  182. depth = visualDepth(pScreen, pVisual);
  183. if (!depth)
  184. continue;
  185. bpp = BitsPerPixel(depth);
  186. switch (pVisual->class) {
  187. case DirectColor:
  188. case TrueColor:
  189. r = Ones(pVisual->redMask);
  190. g = Ones(pVisual->greenMask);
  191. b = Ones(pVisual->blueMask);
  192. type = PICT_TYPE_OTHER;
  193. /*
  194. * Current rendering code supports only two direct formats,
  195. * fields must be packed together at the bottom of the pixel
  196. * and must be either RGB or BGR
  197. */
  198. if (pVisual->offsetBlue == 0 &&
  199. pVisual->offsetGreen == b && pVisual->offsetRed == b + g) {
  200. type = PICT_TYPE_ARGB;
  201. }
  202. else if (pVisual->offsetRed == 0 &&
  203. pVisual->offsetGreen == r &&
  204. pVisual->offsetBlue == r + g) {
  205. type = PICT_TYPE_ABGR;
  206. }
  207. if (type != PICT_TYPE_OTHER) {
  208. format = PICT_FORMAT(bpp, type, 0, r, g, b);
  209. nformats = addFormat(formats, nformats, format, depth);
  210. }
  211. break;
  212. case StaticColor:
  213. case PseudoColor:
  214. format = PICT_VISFORMAT(bpp, PICT_TYPE_COLOR, v);
  215. nformats = addFormat(formats, nformats, format, depth);
  216. break;
  217. case StaticGray:
  218. case GrayScale:
  219. format = PICT_VISFORMAT(bpp, PICT_TYPE_GRAY, v);
  220. nformats = addFormat(formats, nformats, format, depth);
  221. break;
  222. }
  223. }
  224. /*
  225. * Walk supported depths and add useful Direct formats
  226. */
  227. for (d = 0; d < pScreen->numDepths; d++) {
  228. pDepth = &pScreen->allowedDepths[d];
  229. bpp = BitsPerPixel(pDepth->depth);
  230. switch (bpp) {
  231. case 16:
  232. /* depth 12 formats */
  233. if (pDepth->depth >= 12) {
  234. nformats = addFormat(formats, nformats,
  235. PICT_x4r4g4b4, pDepth->depth);
  236. nformats = addFormat(formats, nformats,
  237. PICT_x4b4g4r4, pDepth->depth);
  238. }
  239. /* depth 15 formats */
  240. if (pDepth->depth >= 15) {
  241. nformats = addFormat(formats, nformats,
  242. PICT_x1r5g5b5, pDepth->depth);
  243. nformats = addFormat(formats, nformats,
  244. PICT_x1b5g5r5, pDepth->depth);
  245. }
  246. /* depth 16 formats */
  247. if (pDepth->depth >= 16) {
  248. nformats = addFormat(formats, nformats,
  249. PICT_a1r5g5b5, pDepth->depth);
  250. nformats = addFormat(formats, nformats,
  251. PICT_a1b5g5r5, pDepth->depth);
  252. nformats = addFormat(formats, nformats,
  253. PICT_r5g6b5, pDepth->depth);
  254. nformats = addFormat(formats, nformats,
  255. PICT_b5g6r5, pDepth->depth);
  256. nformats = addFormat(formats, nformats,
  257. PICT_a4r4g4b4, pDepth->depth);
  258. nformats = addFormat(formats, nformats,
  259. PICT_a4b4g4r4, pDepth->depth);
  260. }
  261. break;
  262. case 24:
  263. if (pDepth->depth >= 24) {
  264. nformats = addFormat(formats, nformats,
  265. PICT_r8g8b8, pDepth->depth);
  266. nformats = addFormat(formats, nformats,
  267. PICT_b8g8r8, pDepth->depth);
  268. }
  269. break;
  270. case 32:
  271. if (pDepth->depth >= 24) {
  272. nformats = addFormat(formats, nformats,
  273. PICT_x8r8g8b8, pDepth->depth);
  274. nformats = addFormat(formats, nformats,
  275. PICT_x8b8g8r8, pDepth->depth);
  276. }
  277. break;
  278. }
  279. }
  280. pFormats = malloc(nformats * sizeof(PictFormatRec));
  281. if (!pFormats)
  282. return 0;
  283. memset(pFormats, '\0', nformats * sizeof(PictFormatRec));
  284. for (f = 0; f < nformats; f++) {
  285. pFormats[f].id = FakeClientID(0);
  286. pFormats[f].depth = formats[f].depth;
  287. format = formats[f].format;
  288. pFormats[f].format = format;
  289. switch (PICT_FORMAT_TYPE(format)) {
  290. case PICT_TYPE_ARGB:
  291. pFormats[f].type = PictTypeDirect;
  292. pFormats[f].direct.alphaMask = Mask (PICT_FORMAT_A(format));
  293. if (pFormats[f].direct.alphaMask)
  294. pFormats[f].direct.alpha = (PICT_FORMAT_R(format) +
  295. PICT_FORMAT_G(format) +
  296. PICT_FORMAT_B(format));
  297. pFormats[f].direct.redMask = Mask (PICT_FORMAT_R(format));
  298. pFormats[f].direct.red = (PICT_FORMAT_G(format) +
  299. PICT_FORMAT_B(format));
  300. pFormats[f].direct.greenMask = Mask (PICT_FORMAT_G(format));
  301. pFormats[f].direct.green = PICT_FORMAT_B(format);
  302. pFormats[f].direct.blueMask = Mask (PICT_FORMAT_B(format));
  303. pFormats[f].direct.blue = 0;
  304. break;
  305. case PICT_TYPE_ABGR:
  306. pFormats[f].type = PictTypeDirect;
  307. pFormats[f].direct.alphaMask = Mask (PICT_FORMAT_A(format));
  308. if (pFormats[f].direct.alphaMask)
  309. pFormats[f].direct.alpha = (PICT_FORMAT_B(format) +
  310. PICT_FORMAT_G(format) +
  311. PICT_FORMAT_R(format));
  312. pFormats[f].direct.blueMask = Mask (PICT_FORMAT_B(format));
  313. pFormats[f].direct.blue = (PICT_FORMAT_G(format) +
  314. PICT_FORMAT_R(format));
  315. pFormats[f].direct.greenMask = Mask (PICT_FORMAT_G(format));
  316. pFormats[f].direct.green = PICT_FORMAT_R(format);
  317. pFormats[f].direct.redMask = Mask (PICT_FORMAT_R(format));
  318. pFormats[f].direct.red = 0;
  319. break;
  320. case PICT_TYPE_A:
  321. pFormats[f].type = PictTypeDirect;
  322. pFormats[f].direct.alpha = 0;
  323. pFormats[f].direct.alphaMask = Mask (PICT_FORMAT_A(format));
  324. /* remaining fields already set to zero */
  325. break;
  326. case PICT_TYPE_COLOR:
  327. case PICT_TYPE_GRAY:
  328. pFormats[f].type = PictTypeIndexed;
  329. pFormats[f].index.vid =
  330. pScreen->visuals[PICT_FORMAT_VIS(format)].vid;
  331. break;
  332. }
  333. }
  334. *nformatp = nformats;
  335. return pFormats;
  336. }
  337. static VisualPtr
  338. PictureFindVisual(ScreenPtr pScreen, VisualID visual)
  339. {
  340. int i;
  341. VisualPtr pVisual;
  342. for (i = 0, pVisual = pScreen->visuals;
  343. i < pScreen->numVisuals; i++, pVisual++) {
  344. if (pVisual->vid == visual)
  345. return pVisual;
  346. }
  347. return 0;
  348. }
  349. Bool
  350. PictureInitIndexedFormats(ScreenPtr pScreen)
  351. {
  352. PictureScreenPtr ps = GetPictureScreenIfSet(pScreen);
  353. PictFormatPtr format;
  354. int nformat;
  355. if (!ps)
  356. return FALSE;
  357. format = ps->formats;
  358. nformat = ps->nformats;
  359. while (nformat--) {
  360. if (format->type == PictTypeIndexed && !format->index.pColormap) {
  361. if (format->index.vid == pScreen->rootVisual)
  362. format->index.pColormap =
  363. (ColormapPtr) LookupIDByType(pScreen->defColormap,
  364. RT_COLORMAP);
  365. else {
  366. VisualPtr pVisual;
  367. pVisual = PictureFindVisual(pScreen, format->index.vid);
  368. if (CreateColormap(FakeClientID(0), pScreen,
  369. pVisual,
  370. &format->index.pColormap, AllocNone,
  371. 0) != Success) {
  372. return FALSE;
  373. }
  374. }
  375. if (!(*ps->InitIndexed) (pScreen, format))
  376. return FALSE;
  377. }
  378. format++;
  379. }
  380. return TRUE;
  381. }
  382. Bool
  383. PictureFinishInit(void)
  384. {
  385. int s;
  386. for (s = 0; s < screenInfo.numScreens; s++) {
  387. if (!GlyphFinishInit(screenInfo.screens[s]))
  388. return FALSE;
  389. if (!PictureInitIndexedFormats(screenInfo.screens[s]))
  390. return FALSE;
  391. (void) AnimCurInit(screenInfo.screens[s]);
  392. }
  393. return TRUE;
  394. }
  395. _X_EXPORT Bool
  396. PictureSetSubpixelOrder(ScreenPtr pScreen, int subpixel)
  397. {
  398. PictureScreenPtr ps = GetPictureScreenIfSet(pScreen);
  399. if (!ps)
  400. return FALSE;
  401. ps->subpixel = subpixel;
  402. return TRUE;
  403. }
  404. _X_EXPORT int
  405. PictureGetSubpixelOrder(ScreenPtr pScreen)
  406. {
  407. PictureScreenPtr ps = GetPictureScreenIfSet(pScreen);
  408. if (!ps)
  409. return SubPixelUnknown;
  410. return ps->subpixel;
  411. }
  412. PictFormatPtr
  413. PictureMatchVisual(ScreenPtr pScreen, int depth, VisualPtr pVisual)
  414. {
  415. PictureScreenPtr ps = GetPictureScreenIfSet(pScreen);
  416. PictFormatPtr format;
  417. int nformat;
  418. int type;
  419. if (!ps)
  420. return 0;
  421. format = ps->formats;
  422. nformat = ps->nformats;
  423. switch (pVisual->class) {
  424. case StaticGray:
  425. case GrayScale:
  426. case StaticColor:
  427. case PseudoColor:
  428. type = PictTypeIndexed;
  429. break;
  430. case TrueColor:
  431. case DirectColor:
  432. type = PictTypeDirect;
  433. break;
  434. default:
  435. return 0;
  436. }
  437. while (nformat--) {
  438. if (format->depth == depth && format->type == type) {
  439. if (type == PictTypeIndexed) {
  440. if (format->index.vid == pVisual->vid)
  441. return format;
  442. }
  443. else {
  444. if (format->direct.redMask << format->direct.red ==
  445. pVisual->redMask &&
  446. format->direct.greenMask << format->direct.green ==
  447. pVisual->greenMask &&
  448. format->direct.blueMask << format->direct.blue ==
  449. pVisual->blueMask) {
  450. return format;
  451. }
  452. }
  453. }
  454. format++;
  455. }
  456. return 0;
  457. }
  458. PictFormatPtr
  459. PictureMatchFormat(ScreenPtr pScreen, int depth, CARD32 f)
  460. {
  461. PictureScreenPtr ps = GetPictureScreenIfSet(pScreen);
  462. PictFormatPtr format;
  463. int nformat;
  464. if (!ps)
  465. return 0;
  466. format = ps->formats;
  467. nformat = ps->nformats;
  468. while (nformat--) {
  469. if (format->depth == depth && format->format == (f & 0xffffff))
  470. return format;
  471. format++;
  472. }
  473. return 0;
  474. }
  475. int
  476. PictureParseCmapPolicy(const char *name)
  477. {
  478. if (strcmp(name, "default") == 0)
  479. return PictureCmapPolicyDefault;
  480. else if (strcmp(name, "mono") == 0)
  481. return PictureCmapPolicyMono;
  482. else if (strcmp(name, "gray") == 0)
  483. return PictureCmapPolicyGray;
  484. else if (strcmp(name, "color") == 0)
  485. return PictureCmapPolicyColor;
  486. else if (strcmp(name, "all") == 0)
  487. return PictureCmapPolicyAll;
  488. else
  489. return PictureCmapPolicyInvalid;
  490. }
  491. _X_EXPORT Bool
  492. PictureInit(ScreenPtr pScreen, PictFormatPtr formats, int nformats)
  493. {
  494. PictureScreenPtr ps;
  495. int n;
  496. CARD32 type, a, r, g, b;
  497. if (PictureGeneration != serverGeneration) {
  498. PictureType = CreateNewResourceType(FreePicture);
  499. if (!PictureType)
  500. return FALSE;
  501. PictFormatType = CreateNewResourceType(FreePictFormat);
  502. if (!PictFormatType)
  503. return FALSE;
  504. GlyphSetType = CreateNewResourceType(FreeGlyphSet);
  505. if (!GlyphSetType)
  506. return FALSE;
  507. PictureScreenPrivateIndex = AllocateScreenPrivateIndex();
  508. if (PictureScreenPrivateIndex < 0)
  509. return FALSE;
  510. PictureWindowPrivateIndex = AllocateWindowPrivateIndex();
  511. PictureGeneration = serverGeneration;
  512. #ifdef XResExtension
  513. RegisterResourceName(PictureType, "PICTURE");
  514. RegisterResourceName(PictFormatType, "PICTFORMAT");
  515. RegisterResourceName(GlyphSetType, "GLYPHSET");
  516. #endif
  517. }
  518. if (!AllocateWindowPrivate(pScreen, PictureWindowPrivateIndex, 0))
  519. return FALSE;
  520. if (!formats) {
  521. formats = PictureCreateDefaultFormats(pScreen, &nformats);
  522. if (!formats)
  523. return FALSE;
  524. }
  525. for (n = 0; n < nformats; n++) {
  526. if (!AddResource
  527. (formats[n].id, PictFormatType, (pointer) (formats + n))) {
  528. free(formats);
  529. return FALSE;
  530. }
  531. if (formats[n].type == PictTypeIndexed) {
  532. VisualPtr pVisual =
  533. PictureFindVisual(pScreen, formats[n].index.vid);
  534. if ((pVisual->class | DynamicClass) == PseudoColor)
  535. type = PICT_TYPE_COLOR;
  536. else
  537. type = PICT_TYPE_GRAY;
  538. a = r = g = b = 0;
  539. }
  540. else {
  541. if ((formats[n].direct.redMask |
  542. formats[n].direct.blueMask | formats[n].direct.greenMask) == 0)
  543. type = PICT_TYPE_A;
  544. else if (formats[n].direct.red > formats[n].direct.blue)
  545. type = PICT_TYPE_ARGB;
  546. else
  547. type = PICT_TYPE_ABGR;
  548. a = Ones(formats[n].direct.alphaMask);
  549. r = Ones(formats[n].direct.redMask);
  550. g = Ones(formats[n].direct.greenMask);
  551. b = Ones(formats[n].direct.blueMask);
  552. }
  553. formats[n].format = PICT_FORMAT(0, type, a, r, g, b);
  554. }
  555. ps = malloc(sizeof(PictureScreenRec));
  556. if (!ps) {
  557. free(formats);
  558. return FALSE;
  559. }
  560. SetPictureScreen(pScreen, ps);
  561. if (!GlyphInit(pScreen)) {
  562. SetPictureScreen(pScreen, 0);
  563. free(formats);
  564. free(ps);
  565. return FALSE;
  566. }
  567. ps->totalPictureSize = sizeof(PictureRec);
  568. ps->PicturePrivateSizes = 0;
  569. ps->PicturePrivateLen = 0;
  570. ps->formats = formats;
  571. ps->fallback = formats;
  572. ps->nformats = nformats;
  573. ps->filters = 0;
  574. ps->nfilters = 0;
  575. ps->filterAliases = 0;
  576. ps->nfilterAliases = 0;
  577. ps->subpixel = SubPixelUnknown;
  578. ps->CloseScreen = pScreen->CloseScreen;
  579. ps->DestroyWindow = pScreen->DestroyWindow;
  580. ps->StoreColors = pScreen->StoreColors;
  581. pScreen->DestroyWindow = PictureDestroyWindow;
  582. pScreen->CloseScreen = PictureCloseScreen;
  583. pScreen->StoreColors = PictureStoreColors;
  584. if (!PictureSetDefaultFilters(pScreen)) {
  585. PictureResetFilters(pScreen);
  586. SetPictureScreen(pScreen, 0);
  587. free(formats);
  588. free(ps);
  589. return FALSE;
  590. }
  591. return TRUE;
  592. }
  593. void
  594. SetPictureToDefaults(PicturePtr pPicture)
  595. {
  596. pPicture->refcnt = 1;
  597. pPicture->repeat = 0;
  598. pPicture->graphicsExposures = FALSE;
  599. pPicture->subWindowMode = ClipByChildren;
  600. pPicture->polyEdge = PolyEdgeSharp;
  601. pPicture->polyMode = PolyModePrecise;
  602. pPicture->freeCompClip = FALSE;
  603. pPicture->clientClipType = CT_NONE;
  604. pPicture->componentAlpha = FALSE;
  605. pPicture->repeatType = RepeatNone;
  606. pPicture->alphaMap = 0;
  607. pPicture->alphaOrigin.x = 0;
  608. pPicture->alphaOrigin.y = 0;
  609. pPicture->clipOrigin.x = 0;
  610. pPicture->clipOrigin.y = 0;
  611. pPicture->clientClip = 0;
  612. pPicture->transform = 0;
  613. pPicture->dither = None;
  614. pPicture->filter = PictureGetFilterId(FilterNearest, -1, TRUE);
  615. pPicture->filter_params = 0;
  616. pPicture->filter_nparams = 0;
  617. pPicture->serialNumber = GC_CHANGE_SERIAL_BIT;
  618. pPicture->stateChanges = (1 << (CPLastBit + 1)) - 1;
  619. pPicture->pSourcePict = 0;
  620. }
  621. PicturePtr
  622. AllocatePicture(ScreenPtr pScreen)
  623. {
  624. PictureScreenPtr ps = GetPictureScreen(pScreen);
  625. PicturePtr pPicture;
  626. char *ptr;
  627. DevUnion *ppriv;
  628. unsigned int *sizes;
  629. unsigned int size;
  630. int i;
  631. pPicture = malloc(ps->totalPictureSize);
  632. if (!pPicture)
  633. return 0;
  634. ppriv = (DevUnion *) (pPicture + 1);
  635. pPicture->devPrivates = ppriv;
  636. sizes = ps->PicturePrivateSizes;
  637. ptr = (char *) (ppriv + ps->PicturePrivateLen);
  638. for (i = ps->PicturePrivateLen; --i >= 0; ppriv++, sizes++) {
  639. if ((size = *sizes)) {
  640. ppriv->ptr = (pointer) ptr;
  641. ptr += size;
  642. }
  643. else
  644. ppriv->ptr = (pointer) NULL;
  645. }
  646. return pPicture;
  647. }
  648. PicturePtr
  649. CreatePicture(Picture pid,
  650. DrawablePtr pDrawable,
  651. PictFormatPtr pFormat,
  652. Mask vmask, XID *vlist, ClientPtr client, int *error)
  653. {
  654. PicturePtr pPicture;
  655. PictureScreenPtr ps = GetPictureScreen(pDrawable->pScreen);
  656. pPicture = AllocatePicture(pDrawable->pScreen);
  657. if (!pPicture) {
  658. *error = BadAlloc;
  659. return 0;
  660. }
  661. pPicture->id = pid;
  662. pPicture->pDrawable = pDrawable;
  663. pPicture->pFormat = pFormat;
  664. pPicture->format = pFormat->format | (pDrawable->bitsPerPixel << 24);
  665. if (pDrawable->type == DRAWABLE_PIXMAP) {
  666. ++((PixmapPtr) pDrawable)->refcnt;
  667. pPicture->pNext = 0;
  668. }
  669. else {
  670. pPicture->pNext = GetPictureWindow(((WindowPtr) pDrawable));
  671. SetPictureWindow(((WindowPtr) pDrawable), pPicture);
  672. }
  673. SetPictureToDefaults(pPicture);
  674. if (vmask)
  675. *error = ChangePicture(pPicture, vmask, vlist, 0, client);
  676. else
  677. *error = Success;
  678. if (*error == Success)
  679. *error = (*ps->CreatePicture) (pPicture);
  680. if (*error != Success) {
  681. FreePicture(pPicture, (XID) 0);
  682. pPicture = 0;
  683. }
  684. return pPicture;
  685. }
  686. static CARD32
  687. xRenderColorToCard32(xRenderColor c)
  688. {
  689. return
  690. (c.alpha >> 8 << 24) |
  691. (c.red >> 8 << 16) | (c.green & 0xff00) | (c.blue >> 8);
  692. }
  693. static unsigned int
  694. premultiply(unsigned int x)
  695. {
  696. unsigned int a = x >> 24;
  697. unsigned int t = (x & 0xff00ff) * a + 0x800080;
  698. t = (t + ((t >> 8) & 0xff00ff)) >> 8;
  699. t &= 0xff00ff;
  700. x = ((x >> 8) & 0xff) * a + 0x80;
  701. x = (x + ((x >> 8) & 0xff));
  702. x &= 0xff00;
  703. x |= t | (a << 24);
  704. return x;
  705. }
  706. static unsigned int
  707. INTERPOLATE_PIXEL_256(unsigned int x, unsigned int a,
  708. unsigned int y, unsigned int b)
  709. {
  710. CARD32 t = (x & 0xff00ff) * a + (y & 0xff00ff) * b;
  711. t >>= 8;
  712. t &= 0xff00ff;
  713. x = ((x >> 8) & 0xff00ff) * a + ((y >> 8) & 0xff00ff) * b;
  714. x &= 0xff00ff00;
  715. x |= t;
  716. return x;
  717. }
  718. static void
  719. initGradientColorTable(SourcePictPtr pGradient, int *error)
  720. {
  721. int begin_pos, end_pos;
  722. xFixed incr, dpos;
  723. int pos, current_stop;
  724. PictGradientStopPtr stops = pGradient->linear.stops;
  725. int nstops = pGradient->linear.nstops;
  726. /* The position where the gradient begins and ends */
  727. begin_pos = (stops[0].x * PICT_GRADIENT_STOPTABLE_SIZE) >> 16;
  728. end_pos = (stops[nstops - 1].x * PICT_GRADIENT_STOPTABLE_SIZE) >> 16;
  729. pos = 0; /* The position in the color table. */
  730. /* Up to first point */
  731. while (pos <= begin_pos) {
  732. pGradient->linear.colorTable[pos] =
  733. xRenderColorToCard32(stops[0].color);
  734. ++pos;
  735. }
  736. incr = (1 << 16) / PICT_GRADIENT_STOPTABLE_SIZE; /* the double increment. */
  737. dpos = incr * pos; /* The position in terms of 0-1. */
  738. current_stop = 0; /* We always interpolate between current and current + 1. */
  739. /* Gradient area */
  740. while (pos < end_pos) {
  741. unsigned int current_color =
  742. xRenderColorToCard32(stops[current_stop].color);
  743. unsigned int next_color =
  744. xRenderColorToCard32(stops[current_stop + 1].color);
  745. int dist = (int) (256 * (dpos - stops[current_stop].x)
  746. / (stops[current_stop + 1].x -
  747. stops[current_stop].x));
  748. int idist = 256 - dist;
  749. pGradient->linear.colorTable[pos] =
  750. premultiply(INTERPOLATE_PIXEL_256
  751. (current_color, idist, next_color, dist));
  752. ++pos;
  753. dpos += incr;
  754. if (dpos > stops[current_stop + 1].x)
  755. ++current_stop;
  756. }
  757. /* After last point */
  758. while (pos < PICT_GRADIENT_STOPTABLE_SIZE) {
  759. pGradient->linear.colorTable[pos] =
  760. xRenderColorToCard32(stops[nstops - 1].color);
  761. ++pos;
  762. }
  763. }
  764. static void
  765. initGradient(SourcePictPtr pGradient, int stopCount,
  766. xFixed * stopPoints, xRenderColor * stopColors, int *error)
  767. {
  768. int i;
  769. xFixed dpos;
  770. if (stopCount <= 0) {
  771. *error = BadValue;
  772. return;
  773. }
  774. dpos = -1;
  775. for (i = 0; i < stopCount; ++i) {
  776. if (stopPoints[i] <= dpos || stopPoints[i] > (1 << 16)) {
  777. *error = BadValue;
  778. return;
  779. }
  780. dpos = stopPoints[i];
  781. }
  782. pGradient->linear.stops = malloc(stopCount * sizeof(PictGradientStop));
  783. if (!pGradient->linear.stops) {
  784. *error = BadAlloc;
  785. return;
  786. }
  787. pGradient->linear.nstops = stopCount;
  788. for (i = 0; i < stopCount; ++i) {
  789. pGradient->linear.stops[i].x = stopPoints[i];
  790. pGradient->linear.stops[i].color = stopColors[i];
  791. }
  792. initGradientColorTable(pGradient, error);
  793. }
  794. static PicturePtr
  795. createSourcePicture(void)
  796. {
  797. PicturePtr pPicture;
  798. pPicture = malloc(sizeof(PictureRec));
  799. pPicture->pDrawable = 0;
  800. pPicture->format = PICT_a8r8g8b8;
  801. pPicture->pFormat = 0;
  802. pPicture->pNext = 0;
  803. pPicture->devPrivates = 0;
  804. SetPictureToDefaults(pPicture);
  805. return pPicture;
  806. }
  807. PicturePtr
  808. CreateSolidPicture(Picture pid, xRenderColor * color, int *error)
  809. {
  810. PicturePtr pPicture;
  811. pPicture = createSourcePicture();
  812. if (!pPicture) {
  813. *error = BadAlloc;
  814. return 0;
  815. }
  816. pPicture->id = pid;
  817. pPicture->pSourcePict = malloc(sizeof(PictSolidFill));
  818. if (!pPicture->pSourcePict) {
  819. *error = BadAlloc;
  820. free(pPicture);
  821. return 0;
  822. }
  823. pPicture->pSourcePict->type = SourcePictTypeSolidFill;
  824. pPicture->pSourcePict->solidFill.color = xRenderColorToCard32(*color);
  825. return pPicture;
  826. }
  827. PicturePtr
  828. CreateLinearGradientPicture(Picture pid, xPointFixed * p1, xPointFixed * p2,
  829. int nStops, xFixed * stops, xRenderColor * colors,
  830. int *error)
  831. {
  832. PicturePtr pPicture;
  833. if (nStops < 2) {
  834. *error = BadValue;
  835. return 0;
  836. }
  837. pPicture = createSourcePicture();
  838. if (!pPicture) {
  839. *error = BadAlloc;
  840. return 0;
  841. }
  842. if (p1->x == p2->x && p1->y == p2->y) {
  843. *error = BadValue;
  844. free(pPicture);
  845. return 0;
  846. }
  847. pPicture->id = pid;
  848. pPicture->pSourcePict = malloc(sizeof(PictLinearGradient));
  849. if (!pPicture->pSourcePict) {
  850. *error = BadAlloc;
  851. free(pPicture);
  852. return 0;
  853. }
  854. pPicture->pSourcePict->linear.type = SourcePictTypeLinear;
  855. pPicture->pSourcePict->linear.p1 = *p1;
  856. pPicture->pSourcePict->linear.p2 = *p2;
  857. initGradient(pPicture->pSourcePict, nStops, stops, colors, error);
  858. if (*error) {
  859. free(pPicture);
  860. return 0;
  861. }
  862. return pPicture;
  863. }
  864. #define FixedToDouble(x) ((x)/65536.)
  865. PicturePtr
  866. CreateRadialGradientPicture(Picture pid, xPointFixed * inner,
  867. xPointFixed * outer, xFixed innerRadius,
  868. xFixed outerRadius, int nStops, xFixed * stops,
  869. xRenderColor * colors, int *error)
  870. {
  871. PicturePtr pPicture;
  872. PictRadialGradient *radial;
  873. if (nStops < 2) {
  874. *error = BadValue;
  875. return 0;
  876. }
  877. pPicture = createSourcePicture();
  878. if (!pPicture) {
  879. *error = BadAlloc;
  880. return 0;
  881. }
  882. {
  883. double dx = (double) (inner->x - outer->x);
  884. double dy = (double) (inner->y - outer->y);
  885. if (sqrt(dx * dx + dy * dy) + (double) (innerRadius) >
  886. (double) (outerRadius)) {
  887. *error = BadValue;
  888. free(pPicture);
  889. return 0;
  890. }
  891. }
  892. pPicture->id = pid;
  893. pPicture->pSourcePict = malloc(sizeof(PictRadialGradient));
  894. if (!pPicture->pSourcePict) {
  895. *error = BadAlloc;
  896. free(pPicture);
  897. return 0;
  898. }
  899. radial = &pPicture->pSourcePict->radial;
  900. radial->type = SourcePictTypeRadial;
  901. {
  902. double x = (double) innerRadius / (double) outerRadius;
  903. radial->dx = (outer->x - inner->x);
  904. radial->dy = (outer->y - inner->y);
  905. radial->fx = (inner->x) - x * radial->dx;
  906. radial->fy = (inner->y) - x * radial->dy;
  907. radial->m = 1. / (1 + x);
  908. radial->b = -x * radial->m;
  909. radial->dx /= 65536.;
  910. radial->dy /= 65536.;
  911. radial->fx /= 65536.;
  912. radial->fy /= 65536.;
  913. x = outerRadius / 65536.;
  914. radial->a = x * x - radial->dx * radial->dx - radial->dy * radial->dy;
  915. }
  916. initGradient(pPicture->pSourcePict, nStops, stops, colors, error);
  917. if (*error) {
  918. free(pPicture);
  919. return 0;
  920. }
  921. return pPicture;
  922. }
  923. PicturePtr
  924. CreateConicalGradientPicture(Picture pid, xPointFixed * center, xFixed angle,
  925. int nStops, xFixed * stops, xRenderColor * colors,
  926. int *error)
  927. {
  928. PicturePtr pPicture;
  929. if (nStops < 2) {
  930. *error = BadValue;
  931. return 0;
  932. }
  933. pPicture = createSourcePicture();
  934. if (!pPicture) {
  935. *error = BadAlloc;
  936. return 0;
  937. }
  938. pPicture->id = pid;
  939. pPicture->pSourcePict = malloc(sizeof(PictConicalGradient));
  940. if (!pPicture->pSourcePict) {
  941. *error = BadAlloc;
  942. free(pPicture);
  943. return 0;
  944. }
  945. pPicture->pSourcePict->conical.type = SourcePictTypeConical;
  946. pPicture->pSourcePict->conical.center = *center;
  947. pPicture->pSourcePict->conical.angle = angle;
  948. initGradient(pPicture->pSourcePict, nStops, stops, colors, error);
  949. if (*error) {
  950. free(pPicture);
  951. return 0;
  952. }
  953. return pPicture;
  954. }
  955. #define NEXT_VAL(_type) (vlist ? (_type) *vlist++ : (_type) ulist++->val)
  956. #define NEXT_PTR(_type) ((_type) ulist++->ptr)
  957. int
  958. ChangePicture(PicturePtr pPicture,
  959. Mask vmask, XID *vlist, DevUnion *ulist, ClientPtr client)
  960. {
  961. ScreenPtr pScreen = pPicture->pDrawable ? pPicture->pDrawable->pScreen : 0;
  962. PictureScreenPtr ps = pScreen ? GetPictureScreen(pScreen) : 0;
  963. BITS32 index2;
  964. int error = 0;
  965. BITS32 maskQ;
  966. pPicture->serialNumber |= GC_CHANGE_SERIAL_BIT;
  967. maskQ = vmask;
  968. while (vmask && !error) {
  969. index2 = (BITS32) lowbit(vmask);
  970. vmask &= ~index2;
  971. pPicture->stateChanges |= index2;
  972. switch (index2) {
  973. case CPRepeat:
  974. {
  975. unsigned int newr;
  976. newr = NEXT_VAL(unsigned int);
  977. if (newr <= RepeatReflect) {
  978. pPicture->repeat = (newr != RepeatNone);
  979. pPicture->repeatType = newr;
  980. }
  981. else {
  982. client->errorValue = newr;
  983. error = BadValue;
  984. }
  985. }
  986. break;
  987. case CPAlphaMap:
  988. {
  989. PicturePtr pAlpha;
  990. if (vlist) {
  991. Picture pid = NEXT_VAL(Picture);
  992. if (pid == None)
  993. pAlpha = 0;
  994. else {
  995. pAlpha = (PicturePtr) SecurityLookupIDByType(client,
  996. pid,
  997. PictureType,
  998. SecurityWriteAccess
  999. |
  1000. SecurityReadAccess);
  1001. if (!pAlpha) {
  1002. client->errorValue = pid;
  1003. error = BadPixmap;
  1004. break;
  1005. }
  1006. if (pAlpha->pDrawable == NULL ||
  1007. pAlpha->pDrawable->type != DRAWABLE_PIXMAP) {
  1008. client->errorValue = pid;
  1009. error = BadMatch;
  1010. break;
  1011. }
  1012. }
  1013. }
  1014. else
  1015. pAlpha = NEXT_PTR(PicturePtr);
  1016. if (!error) {
  1017. if (pAlpha && pAlpha->pDrawable->type == DRAWABLE_PIXMAP)
  1018. pAlpha->refcnt++;
  1019. if (pPicture->alphaMap)
  1020. FreePicture((pointer) pPicture->alphaMap, (XID) 0);
  1021. pPicture->alphaMap = pAlpha;
  1022. }
  1023. }
  1024. break;
  1025. case CPAlphaXOrigin:
  1026. pPicture->alphaOrigin.x = NEXT_VAL(INT16);
  1027. break;
  1028. case CPAlphaYOrigin:
  1029. pPicture->alphaOrigin.y = NEXT_VAL(INT16);
  1030. break;
  1031. case CPClipXOrigin:
  1032. pPicture->clipOrigin.x = NEXT_VAL(INT16);
  1033. break;
  1034. case CPClipYOrigin:
  1035. pPicture->clipOrigin.y = NEXT_VAL(INT16);
  1036. break;
  1037. case CPClipMask:
  1038. {
  1039. Pixmap pid;
  1040. PixmapPtr pPixmap;
  1041. int clipType;
  1042. if (!pScreen)
  1043. return BadDrawable;
  1044. if (vlist) {
  1045. pid = NEXT_VAL(Pixmap);
  1046. if (pid == None) {
  1047. clipType = CT_NONE;
  1048. pPixmap = NullPixmap;
  1049. }
  1050. else {
  1051. clipType = CT_PIXMAP;
  1052. pPixmap = (PixmapPtr) SecurityLookupIDByType(client,
  1053. pid,
  1054. RT_PIXMAP,
  1055. SecurityReadAccess);
  1056. if (!pPixmap) {
  1057. client->errorValue = pid;
  1058. error = BadPixmap;
  1059. break;
  1060. }
  1061. }
  1062. }
  1063. else {
  1064. pPixmap = NEXT_PTR(PixmapPtr);
  1065. if (pPixmap)
  1066. clipType = CT_PIXMAP;
  1067. else
  1068. clipType = CT_NONE;
  1069. }
  1070. if (pPixmap) {
  1071. if ((pPixmap->drawable.depth != 1) ||
  1072. (pPixmap->drawable.pScreen != pScreen)) {
  1073. error = BadMatch;
  1074. break;
  1075. }
  1076. else {
  1077. clipType = CT_PIXMAP;
  1078. pPixmap->refcnt++;
  1079. }
  1080. }
  1081. error = (*ps->ChangePictureClip) (pPicture, clipType,
  1082. (pointer) pPixmap, 0);
  1083. break;
  1084. }
  1085. case CPGraphicsExposure:
  1086. {
  1087. unsigned int newe;
  1088. newe = NEXT_VAL(unsigned int);
  1089. if (newe <= xTrue)
  1090. pPicture->graphicsExposures = newe;
  1091. else {
  1092. client->errorValue = newe;
  1093. error = BadValue;
  1094. }
  1095. }
  1096. break;
  1097. case CPSubwindowMode:
  1098. {
  1099. unsigned int news;
  1100. news = NEXT_VAL(unsigned int);
  1101. if (news == ClipByChildren || news == IncludeInferiors)
  1102. pPicture->subWindowMode = news;
  1103. else {
  1104. client->errorValue = news;
  1105. error = BadValue;
  1106. }
  1107. }
  1108. break;
  1109. case CPPolyEdge:
  1110. {
  1111. unsigned int newe;
  1112. newe = NEXT_VAL(unsigned int);
  1113. if (newe == PolyEdgeSharp || newe == PolyEdgeSmooth)
  1114. pPicture->polyEdge = newe;
  1115. else {
  1116. client->errorValue = newe;
  1117. error = BadValue;
  1118. }
  1119. }
  1120. break;
  1121. case CPPolyMode:
  1122. {
  1123. unsigned int newm;
  1124. newm = NEXT_VAL(unsigned int);
  1125. if (newm == PolyModePrecise || newm == PolyModeImprecise)
  1126. pPicture->polyMode = newm;
  1127. else {
  1128. client->errorValue = newm;
  1129. error = BadValue;
  1130. }
  1131. }
  1132. break;
  1133. case CPDither:
  1134. pPicture->dither = NEXT_VAL(Atom);
  1135. break;
  1136. case CPComponentAlpha:
  1137. {
  1138. unsigned int newca;
  1139. newca = NEXT_VAL(unsigned int);
  1140. if (newca <= xTrue)
  1141. pPicture->componentAlpha = newca;
  1142. else {
  1143. client->errorValue = newca;
  1144. error = BadValue;
  1145. }
  1146. }
  1147. break;
  1148. default:
  1149. client->errorValue = maskQ;
  1150. error = BadValue;
  1151. break;
  1152. }
  1153. }
  1154. if (ps)
  1155. (*ps->ChangePicture) (pPicture, maskQ);
  1156. return error;
  1157. }
  1158. int
  1159. SetPictureClipRects(PicturePtr pPicture,
  1160. int xOrigin, int yOrigin, int nRect, xRectangle *rects)
  1161. {
  1162. ScreenPtr pScreen = pPicture->pDrawable->pScreen;
  1163. PictureScreenPtr ps = GetPictureScreen(pScreen);
  1164. RegionPtr clientClip;
  1165. int result;
  1166. clientClip = RECTS_TO_REGION(nRect, rects, CT_UNSORTED);
  1167. if (!clientClip)
  1168. return BadAlloc;
  1169. result = (*ps->ChangePictureClip) (pPicture, CT_REGION,
  1170. (pointer) clientClip, 0);
  1171. if (result == Success) {
  1172. pPicture->clipOrigin.x = xOrigin;
  1173. pPicture->clipOrigin.y = yOrigin;
  1174. pPicture->stateChanges |= CPClipXOrigin | CPClipYOrigin | CPClipMask;
  1175. pPicture->serialNumber |= GC_CHANGE_SERIAL_BIT;
  1176. }
  1177. return result;
  1178. }
  1179. int
  1180. SetPictureClipRegion(PicturePtr pPicture,
  1181. int xOrigin, int yOrigin, RegionPtr pRegion)
  1182. {
  1183. ScreenPtr pScreen = pPicture->pDrawable->pScreen;
  1184. PictureScreenPtr ps = GetPictureScreen(pScreen);
  1185. RegionPtr clientClip;
  1186. int result;
  1187. int type;
  1188. if (pRegion) {
  1189. type = CT_REGION;
  1190. clientClip = REGION_CREATE(REGION_EXTENTS(pRegion),
  1191. REGION_NUM_RECTS(pRegion));
  1192. if (!clientClip)
  1193. return BadAlloc;
  1194. if (!REGION_COPY(clientClip, pRegion)) {
  1195. REGION_DESTROY(clientClip);
  1196. return BadAlloc;
  1197. }
  1198. }
  1199. else {
  1200. type = CT_NONE;
  1201. clientClip = 0;
  1202. }
  1203. result = (*ps->ChangePictureClip) (pPicture, type, (pointer) clientClip, 0);
  1204. if (result == Success) {
  1205. pPicture->clipOrigin.x = xOrigin;
  1206. pPicture->clipOrigin.y = yOrigin;
  1207. pPicture->stateChanges |= CPClipXOrigin | CPClipYOrigin | CPClipMask;
  1208. pPicture->serialNumber |= GC_CHANGE_SERIAL_BIT;
  1209. }
  1210. return result;
  1211. }
  1212. static Bool
  1213. transformIsIdentity(PictTransform * t)
  1214. {
  1215. return ((t->matrix[0][0] == t->matrix[1][1]) &&
  1216. (t->matrix[0][0] == t->matrix[2][2]) &&
  1217. (t->matrix[0][0] != 0) &&
  1218. (t->matrix[0][1] == 0) &&
  1219. (t->matrix[0][2] == 0) &&
  1220. (t->matrix[1][0] == 0) &&
  1221. (t->matrix[1][2] == 0) &&
  1222. (t->matrix[2][0] == 0) && (t->matrix[2][1] == 0));
  1223. }
  1224. int
  1225. SetPictureTransform(PicturePtr pPicture, PictTransform * transform)
  1226. {
  1227. if (transform && transformIsIdentity(transform))
  1228. transform = 0;
  1229. if (transform) {
  1230. if (!pPicture->transform) {
  1231. pPicture->transform =
  1232. malloc(sizeof(PictTransform));
  1233. if (!pPicture->transform)
  1234. return BadAlloc;
  1235. }
  1236. *pPicture->transform = *transform;
  1237. }
  1238. else {
  1239. if (pPicture->transform) {
  1240. free(pPicture->transform);
  1241. pPicture->transform = 0;
  1242. }
  1243. }
  1244. pPicture->serialNumber |= GC_CHANGE_SERIAL_BIT;
  1245. if (pPicture->pDrawable != NULL) {
  1246. int result;
  1247. PictureScreenPtr ps = GetPictureScreen(pPicture->pDrawable->pScreen);
  1248. result = (*ps->ChangePictureTransform) (pPicture, transform);
  1249. return result;
  1250. }
  1251. return Success;
  1252. }
  1253. static void
  1254. ValidateOnePicture(PicturePtr pPicture)
  1255. {
  1256. if (pPicture->pDrawable &&
  1257. pPicture->serialNumber != pPicture->pDrawable->serialNumber) {
  1258. PictureScreenPtr ps = GetPictureScreen(pPicture->pDrawable->pScreen);
  1259. (*ps->ValidatePicture) (pPicture, pPicture->stateChanges);
  1260. pPicture->stateChanges = 0;
  1261. pPicture->serialNumber = pPicture->pDrawable->serialNumber;
  1262. }
  1263. }
  1264. void
  1265. ValidatePicture(PicturePtr pPicture)
  1266. {
  1267. ValidateOnePicture(pPicture);
  1268. if (pPicture->alphaMap)
  1269. ValidateOnePicture(pPicture->alphaMap);
  1270. }
  1271. int
  1272. FreePicture(pointer value, XID pid)
  1273. {
  1274. PicturePtr pPicture = (PicturePtr) value;
  1275. if (--pPicture->refcnt == 0) {
  1276. if (pPicture->transform)
  1277. free(pPicture->transform);
  1278. if (!pPicture->pDrawable) {
  1279. if (pPicture->pSourcePict) {
  1280. if (pPicture->pSourcePict->type != SourcePictTypeSolidFill)
  1281. free(pPicture->pSourcePict->linear.stops);
  1282. free(pPicture->pSourcePict);
  1283. }
  1284. }
  1285. else {
  1286. ScreenPtr pScreen = pPicture->pDrawable->pScreen;
  1287. PictureScreenPtr ps = GetPictureScreen(pScreen);
  1288. if (pPicture->alphaMap)
  1289. FreePicture((pointer) pPicture->alphaMap, (XID) 0);
  1290. (*ps->DestroyPicture) (pPicture);
  1291. (*ps->DestroyPictureClip) (pPicture);
  1292. if (pPicture->pDrawable->type == DRAWABLE_WINDOW) {
  1293. WindowPtr pWindow = (WindowPtr) pPicture->pDrawable;
  1294. PicturePtr *pPrev;
  1295. for (pPrev =
  1296. (PicturePtr *) & ((pWindow)->
  1297. devPrivates[PictureWindowPrivateIndex].
  1298. ptr); *pPrev; pPrev = &(*pPrev)->pNext) {
  1299. if (*pPrev == pPicture) {
  1300. *pPrev = pPicture->pNext;
  1301. break;
  1302. }
  1303. }
  1304. }
  1305. else if (pPicture->pDrawable->type == DRAWABLE_PIXMAP) {
  1306. (*pScreen->DestroyPixmap) ((PixmapPtr) pPicture->pDrawable);
  1307. }
  1308. }
  1309. free(pPicture);
  1310. }
  1311. return Success;
  1312. }
  1313. int
  1314. FreePictFormat(pointer pPictFormat, XID pid)
  1315. {
  1316. return Success;
  1317. }
  1318. /**
  1319. * ReduceCompositeOp is used to choose simpler ops for cases where alpha
  1320. * channels are always one and so math on the alpha channel per pixel becomes
  1321. * unnecessary. It may also avoid destination reads sometimes if apps aren't
  1322. * being careful to avoid these cases.
  1323. */
  1324. static Bool
  1325. ReduceCompositeOp(CARD8 op, PicturePtr pSrc, PicturePtr pMask, PicturePtr pDst)
  1326. {
  1327. Bool no_src_alpha, no_dst_alpha;
  1328. no_src_alpha = PICT_FORMAT_COLOR(pSrc->format) &&
  1329. PICT_FORMAT_A(pSrc->format) == 0 &&
  1330. pSrc->alphaMap == NULL && pMask == NULL;
  1331. no_dst_alpha = PICT_FORMAT_COLOR(pDst->format) &&
  1332. PICT_FORMAT_A(pDst->format) == 0 && pDst->alphaMap == NULL;
  1333. /* TODO, maybe: Conjoint and Disjoint op reductions? */
  1334. /* Deal with simplifications where the source alpha is always 1. */
  1335. if (no_src_alpha) {
  1336. switch (op) {
  1337. case PictOpOver:
  1338. op = PictOpSrc;
  1339. break;
  1340. case PictOpInReverse:
  1341. op = PictOpDst;
  1342. break;
  1343. case PictOpOutReverse:
  1344. op = PictOpClear;
  1345. break;
  1346. case PictOpAtop:
  1347. op = PictOpIn;
  1348. break;
  1349. case PictOpAtopReverse:
  1350. op = PictOpOverReverse;
  1351. break;
  1352. case PictOpXor:
  1353. op = PictOpOut;
  1354. break;
  1355. default:
  1356. break;
  1357. }
  1358. }
  1359. /* Deal with simplifications when the destination alpha is always 1 */
  1360. if (no_dst_alpha) {
  1361. switch (op) {
  1362. case PictOpOverReverse:
  1363. op = PictOpDst;
  1364. break;
  1365. case PictOpIn:
  1366. op = PictOpSrc;
  1367. break;
  1368. case PictOpOut:
  1369. op = PictOpClear;
  1370. break;
  1371. case PictOpAtop:
  1372. op = PictOpOver;
  1373. break;
  1374. case PictOpXor:
  1375. op = PictOpOutReverse;
  1376. break;
  1377. default:
  1378. break;
  1379. }
  1380. }
  1381. /* Reduce some con/disjoint ops to the basic names. */
  1382. switch (op) {
  1383. case PictOpDisjointClear:
  1384. case PictOpConjointClear:
  1385. op = PictOpClear;
  1386. break;
  1387. case PictOpDisjointSrc:
  1388. case PictOpConjointSrc:
  1389. op = PictOpSrc;
  1390. break;
  1391. case PictOpDisjointDst:
  1392. case PictOpConjointDst:
  1393. op = PictOpDst;
  1394. break;
  1395. default:
  1396. break;
  1397. }
  1398. return op;
  1399. }
  1400. void
  1401. CompositePicture(CARD8 op,
  1402. PicturePtr pSrc,
  1403. PicturePtr pMask,
  1404. PicturePtr pDst,
  1405. INT16 xSrc,
  1406. INT16 ySrc,
  1407. INT16 xMask,
  1408. INT16 yMask,
  1409. INT16 xDst, INT16 yDst, CARD16 width, CARD16 height)
  1410. {
  1411. PictureScreenPtr ps = GetPictureScreen(pDst->pDrawable->pScreen);
  1412. ValidatePicture(pSrc);
  1413. if (pMask)
  1414. ValidatePicture(pMask);
  1415. ValidatePicture(pDst);
  1416. op = ReduceCompositeOp(op, pSrc, pMask, pDst);
  1417. if (op == PictOpDst)
  1418. return;
  1419. (*ps->Composite) (op,
  1420. pSrc,
  1421. pMask,
  1422. pDst,
  1423. xSrc, ySrc, xMask, yMask, xDst, yDst, width, height);
  1424. }
  1425. void
  1426. CompositeGlyphs(CARD8 op,
  1427. PicturePtr pSrc,
  1428. PicturePtr pDst,
  1429. PictFormatPtr maskFormat,
  1430. INT16 xSrc,
  1431. INT16 ySrc, int nlist, GlyphListPtr lists, GlyphPtr * glyphs)
  1432. {
  1433. PictureScreenPtr ps = GetPictureScreen(pDst->pDrawable->pScreen);
  1434. ValidatePicture(pSrc);
  1435. ValidatePicture(pDst);
  1436. (*ps->Glyphs) (op, pSrc, pDst, maskFormat, xSrc, ySrc, nlist, lists,
  1437. glyphs);
  1438. }
  1439. void
  1440. CompositeRects(CARD8 op,
  1441. PicturePtr pDst,
  1442. xRenderColor * color, int nRect, xRectangle *rects)
  1443. {
  1444. PictureScreenPtr ps = GetPictureScreen(pDst->pDrawable->pScreen);
  1445. ValidatePicture(pDst);
  1446. (*ps->CompositeRects) (op, pDst, color, nRect, rects);
  1447. }
  1448. void
  1449. CompositeTrapezoids(CARD8 op,
  1450. PicturePtr pSrc,
  1451. PicturePtr pDst,
  1452. PictFormatPtr maskFormat,
  1453. INT16 xSrc, INT16 ySrc, int ntrap, xTrapezoid * traps)
  1454. {
  1455. PictureScreenPtr ps = GetPictureScreen(pDst->pDrawable->pScreen);
  1456. ValidatePicture(pSrc);
  1457. ValidatePicture(pDst);
  1458. (*ps->Trapezoids) (op, pSrc, pDst, maskFormat, xSrc, ySrc, ntrap, traps);
  1459. }
  1460. void
  1461. CompositeTriangles(CARD8 op,
  1462. PicturePtr pSrc,
  1463. PicturePtr pDst,
  1464. PictFormatPtr maskFormat,
  1465. INT16 xSrc,
  1466. INT16 ySrc, int ntriangles, xTriangle * triangles)
  1467. {
  1468. PictureScreenPtr ps = GetPictureScreen(pDst->pDrawable->pScreen);
  1469. ValidatePicture(pSrc);
  1470. ValidatePicture(pDst);
  1471. (*ps->Triangles) (op, pSrc, pDst, maskFormat, xSrc, ySrc, ntriangles,
  1472. triangles);
  1473. }
  1474. void
  1475. CompositeTriStrip(CARD8 op,
  1476. PicturePtr pSrc,
  1477. PicturePtr pDst,
  1478. PictFormatPtr maskFormat,
  1479. INT16 xSrc, INT16 ySrc, int npoints, xPointFixed * points)
  1480. {
  1481. PictureScreenPtr ps = GetPictureScreen(pDst->pDrawable->pScreen);
  1482. ValidatePicture(pSrc);
  1483. ValidatePicture(pDst);
  1484. (*ps->TriStrip) (op, pSrc, pDst, maskFormat, xSrc, ySrc, npoints, points);
  1485. }
  1486. void
  1487. CompositeTriFan(CARD8 op,
  1488. PicturePtr pSrc,
  1489. PicturePtr pDst,
  1490. PictFormatPtr maskFormat,
  1491. INT16 xSrc, INT16 ySrc, int npoints, xPointFixed * points)
  1492. {
  1493. PictureScreenPtr ps = GetPictureScreen(pDst->pDrawable->pScreen);
  1494. ValidatePicture(pSrc);
  1495. ValidatePicture(pDst);
  1496. (*ps->TriFan) (op, pSrc, pDst, maskFormat, xSrc, ySrc, npoints, points);
  1497. }
  1498. void
  1499. AddTraps(PicturePtr pPicture, INT16 xOff, INT16 yOff, int ntrap, xTrap * traps)
  1500. {
  1501. PictureScreenPtr ps = GetPictureScreen(pPicture->pDrawable->pScreen);
  1502. ValidatePicture(pPicture);
  1503. (*ps->AddTraps) (pPicture, xOff, yOff, ntrap, traps);
  1504. }
  1505. #define MAX_FIXED_48_16 ((xFixed_48_16) 0x7fffffff)
  1506. #define MIN_FIXED_48_16 (-((xFixed_48_16) 1 << 31))
  1507. _X_EXPORT Bool
  1508. PictureTransformPoint3d(PictTransformPtr transform, PictVectorPtr vector)
  1509. {
  1510. PictVector result;
  1511. int i, j;
  1512. xFixed_32_32 partial;
  1513. xFixed_48_16 v;
  1514. for (j = 0; j < 3; j++) {
  1515. v = 0;
  1516. for (i = 0; i < 3; i++) {
  1517. partial = ((xFixed_48_16) transform->matrix[j][i] *
  1518. (xFixed_48_16) vector->vector[i]);
  1519. v += partial >> 16;
  1520. }
  1521. if (v > MAX_FIXED_48_16 || v < MIN_FIXED_48_16)
  1522. return FALSE;
  1523. result.vector[j] = (xFixed) v;
  1524. }
  1525. if (!result.vector[2])
  1526. return FALSE;
  1527. *vector = result;
  1528. return TRUE;
  1529. }
  1530. _X_EXPORT Bool
  1531. PictureTransformPoint(PictTransformPtr transform, PictVectorPtr vector)
  1532. {
  1533. PictVector result;
  1534. int i, j;
  1535. xFixed_32_32 partial;
  1536. xFixed_48_16 v;
  1537. for (j = 0; j < 3; j++) {
  1538. v = 0;
  1539. for (i = 0; i < 3; i++) {
  1540. partial = ((xFixed_48_16) transform->matrix[j][i] *
  1541. (xFixed_48_16) vector->vector[i]);
  1542. v += partial >> 16;
  1543. }
  1544. if (v > MAX_FIXED_48_16 || v < MIN_FIXED_48_16)
  1545. return FALSE;
  1546. result.vector[j] = (xFixed) v;
  1547. }
  1548. if (!result.vector[2])
  1549. return FALSE;
  1550. for (j = 0; j < 2; j++) {
  1551. partial = (xFixed_48_16) result.vector[j] << 16;
  1552. v = partial / result.vector[2];
  1553. if (v > MAX_FIXED_48_16 || v < MIN_FIXED_48_16)
  1554. return FALSE;
  1555. vector->vector[j] = (xFixed) v;
  1556. }
  1557. vector->vector[2] = xFixed1;
  1558. return TRUE;
  1559. }