mitrap.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. *
  3. * Copyright © 2002 Keith Packard, member of The XFree86 Project, 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 Keith Packard not be used in
  10. * advertising or publicity pertaining to distribution of the software without
  11. * specific, written prior permission. Keith Packard makes no
  12. * representations about the suitability of this software for any purpose. It
  13. * is provided "as is" without express or implied warranty.
  14. *
  15. * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  16. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  17. * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  18. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  19. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  20. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  21. * PERFORMANCE OF THIS SOFTWARE.
  22. */
  23. #ifdef HAVE_DIX_CONFIG_H
  24. #include <dix-config.h>
  25. #endif
  26. #include "scrnintstr.h"
  27. #include "gcstruct.h"
  28. #include "pixmapstr.h"
  29. #include "windowstr.h"
  30. #include "servermd.h"
  31. #include "mi.h"
  32. #include "picturestr.h"
  33. #include "mipict.h"
  34. PicturePtr
  35. miCreateAlphaPicture(ScreenPtr pScreen,
  36. PicturePtr pDst,
  37. PictFormatPtr pPictFormat, CARD16 width, CARD16 height)
  38. {
  39. PixmapPtr pPixmap;
  40. PicturePtr pPicture;
  41. GCPtr pGC;
  42. int error;
  43. xRectangle rect;
  44. if (width > 32767 || height > 32767)
  45. return 0;
  46. if (!pPictFormat) {
  47. if (pDst->polyEdge == PolyEdgeSharp)
  48. pPictFormat = PictureMatchFormat(pScreen, 1, PICT_a1);
  49. else
  50. pPictFormat = PictureMatchFormat(pScreen, 8, PICT_a8);
  51. if (!pPictFormat)
  52. return 0;
  53. }
  54. pPixmap = (*pScreen->CreatePixmap) (pScreen, width, height,
  55. pPictFormat->depth);
  56. if (!pPixmap)
  57. return 0;
  58. pGC = GetScratchGC(pPixmap->drawable.depth, pScreen);
  59. if (!pGC) {
  60. (*pScreen->DestroyPixmap) (pPixmap);
  61. return 0;
  62. }
  63. ValidateGC(&pPixmap->drawable, pGC);
  64. rect.x = 0;
  65. rect.y = 0;
  66. rect.width = width;
  67. rect.height = height;
  68. (*pGC->ops->PolyFillRect) (&pPixmap->drawable, pGC, 1, &rect);
  69. FreeScratchGC(pGC);
  70. pPicture = CreatePicture(0, &pPixmap->drawable, pPictFormat,
  71. 0, 0, serverClient, &error);
  72. (*pScreen->DestroyPixmap) (pPixmap);
  73. return pPicture;
  74. }
  75. static xFixed
  76. miLineFixedX(xLineFixed * l, xFixed y, Bool ceil)
  77. {
  78. xFixed dx = l->p2.x - l->p1.x;
  79. xFixed_32_32 ex = (xFixed_32_32) (y - l->p1.y) * dx;
  80. xFixed dy = l->p2.y - l->p1.y;
  81. if (ceil)
  82. ex += (dy - 1);
  83. return l->p1.x + (xFixed) (ex / dy);
  84. }
  85. void
  86. miTrapezoidBounds(int ntrap, xTrapezoid * traps, BoxPtr box)
  87. {
  88. box->y1 = MAXSHORT;
  89. box->y2 = MINSHORT;
  90. box->x1 = MAXSHORT;
  91. box->x2 = MINSHORT;
  92. for (; ntrap; ntrap--, traps++) {
  93. INT16 x1, y1, x2, y2;
  94. if (!xTrapezoidValid(traps))
  95. continue;
  96. y1 = xFixedToInt(traps->top);
  97. if (y1 < box->y1)
  98. box->y1 = y1;
  99. y2 = xFixedToInt(xFixedCeil(traps->bottom));
  100. if (y2 > box->y2)
  101. box->y2 = y2;
  102. x1 = xFixedToInt(min(miLineFixedX(&traps->left, traps->top, FALSE),
  103. miLineFixedX(&traps->left, traps->bottom, FALSE)));
  104. if (x1 < box->x1)
  105. box->x1 = x1;
  106. x2 = xFixedToInt(xFixedCeil
  107. (max
  108. (miLineFixedX(&traps->right, traps->top, TRUE),
  109. miLineFixedX(&traps->right, traps->bottom, TRUE))));
  110. if (x2 > box->x2)
  111. box->x2 = x2;
  112. }
  113. }
  114. void
  115. miTrapezoids(CARD8 op,
  116. PicturePtr pSrc,
  117. PicturePtr pDst,
  118. PictFormatPtr maskFormat,
  119. INT16 xSrc, INT16 ySrc, int ntrap, xTrapezoid * traps)
  120. {
  121. ScreenPtr pScreen = pDst->pDrawable->pScreen;
  122. PictureScreenPtr ps = GetPictureScreen(pScreen);
  123. /*
  124. * Check for solid alpha add
  125. */
  126. if (op == PictOpAdd && miIsSolidAlpha(pSrc)) {
  127. for (; ntrap; ntrap--, traps++)
  128. (*ps->RasterizeTrapezoid) (pDst, traps, 0, 0);
  129. }
  130. else if (maskFormat) {
  131. PicturePtr pPicture;
  132. BoxRec bounds;
  133. INT16 xDst, yDst;
  134. INT16 xRel, yRel;
  135. xDst = traps[0].left.p1.x >> 16;
  136. yDst = traps[0].left.p1.y >> 16;
  137. miTrapezoidBounds(ntrap, traps, &bounds);
  138. if (bounds.y1 >= bounds.y2 || bounds.x1 >= bounds.x2)
  139. return;
  140. pPicture = miCreateAlphaPicture(pScreen, pDst, maskFormat,
  141. bounds.x2 - bounds.x1,
  142. bounds.y2 - bounds.y1);
  143. if (!pPicture)
  144. return;
  145. for (; ntrap; ntrap--, traps++)
  146. (*ps->RasterizeTrapezoid) (pPicture, traps, -bounds.x1, -bounds.y1);
  147. xRel = bounds.x1 + xSrc - xDst;
  148. yRel = bounds.y1 + ySrc - yDst;
  149. CompositePicture(op, pSrc, pPicture, pDst,
  150. xRel, yRel, 0, 0, bounds.x1, bounds.y1,
  151. bounds.x2 - bounds.x1, bounds.y2 - bounds.y1);
  152. FreePicture(pPicture, 0);
  153. }
  154. else {
  155. if (pDst->polyEdge == PolyEdgeSharp)
  156. maskFormat = PictureMatchFormat(pScreen, 1, PICT_a1);
  157. else
  158. maskFormat = PictureMatchFormat(pScreen, 8, PICT_a8);
  159. for (; ntrap; ntrap--, traps++)
  160. miTrapezoids(op, pSrc, pDst, maskFormat, xSrc, ySrc, 1, traps);
  161. }
  162. }