nsSVGMaskFrame.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. // Main header first:
  6. #include "nsSVGMaskFrame.h"
  7. // Keep others in (case-insensitive) order:
  8. #include "gfx2DGlue.h"
  9. #include "gfxContext.h"
  10. #include "mozilla/gfx/2D.h"
  11. #include "mozilla/RefPtr.h"
  12. #include "nsSVGEffects.h"
  13. #include "mozilla/dom/SVGMaskElement.h"
  14. #ifdef BUILD_ARM_NEON
  15. #include "mozilla/arm.h"
  16. #include "nsSVGMaskFrameNEON.h"
  17. #endif
  18. using namespace mozilla;
  19. using namespace mozilla::dom;
  20. using namespace mozilla::gfx;
  21. // c = n / 255
  22. // c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4)) * 255 + 0.5
  23. static const uint8_t gsRGBToLinearRGBMap[256] = {
  24. 0, 0, 0, 0, 0, 0, 0, 1,
  25. 1, 1, 1, 1, 1, 1, 1, 1,
  26. 1, 1, 2, 2, 2, 2, 2, 2,
  27. 2, 2, 3, 3, 3, 3, 3, 3,
  28. 4, 4, 4, 4, 4, 5, 5, 5,
  29. 5, 6, 6, 6, 6, 7, 7, 7,
  30. 8, 8, 8, 8, 9, 9, 9, 10,
  31. 10, 10, 11, 11, 12, 12, 12, 13,
  32. 13, 13, 14, 14, 15, 15, 16, 16,
  33. 17, 17, 17, 18, 18, 19, 19, 20,
  34. 20, 21, 22, 22, 23, 23, 24, 24,
  35. 25, 25, 26, 27, 27, 28, 29, 29,
  36. 30, 30, 31, 32, 32, 33, 34, 35,
  37. 35, 36, 37, 37, 38, 39, 40, 41,
  38. 41, 42, 43, 44, 45, 45, 46, 47,
  39. 48, 49, 50, 51, 51, 52, 53, 54,
  40. 55, 56, 57, 58, 59, 60, 61, 62,
  41. 63, 64, 65, 66, 67, 68, 69, 70,
  42. 71, 72, 73, 74, 76, 77, 78, 79,
  43. 80, 81, 82, 84, 85, 86, 87, 88,
  44. 90, 91, 92, 93, 95, 96, 97, 99,
  45. 100, 101, 103, 104, 105, 107, 108, 109,
  46. 111, 112, 114, 115, 116, 118, 119, 121,
  47. 122, 124, 125, 127, 128, 130, 131, 133,
  48. 134, 136, 138, 139, 141, 142, 144, 146,
  49. 147, 149, 151, 152, 154, 156, 157, 159,
  50. 161, 163, 164, 166, 168, 170, 171, 173,
  51. 175, 177, 179, 181, 183, 184, 186, 188,
  52. 190, 192, 194, 196, 198, 200, 202, 204,
  53. 206, 208, 210, 212, 214, 216, 218, 220,
  54. 222, 224, 226, 229, 231, 233, 235, 237,
  55. 239, 242, 244, 246, 248, 250, 253, 255
  56. };
  57. static void
  58. ComputesRGBLuminanceMask(const uint8_t *aSourceData,
  59. int32_t aSourceStride,
  60. uint8_t *aDestData,
  61. int32_t aDestStride,
  62. const IntSize &aSize,
  63. float aOpacity)
  64. {
  65. #ifdef BUILD_ARM_NEON
  66. if (mozilla::supports_neon()) {
  67. ComputesRGBLuminanceMask_NEON(aSourceData, aSourceStride,
  68. aDestData, aDestStride,
  69. aSize, aOpacity);
  70. return;
  71. }
  72. #endif
  73. int32_t redFactor = 55 * aOpacity; // 255 * 0.2125 * opacity
  74. int32_t greenFactor = 183 * aOpacity; // 255 * 0.7154 * opacity
  75. int32_t blueFactor = 18 * aOpacity; // 255 * 0.0721
  76. int32_t sourceOffset = aSourceStride - 4 * aSize.width;
  77. const uint8_t *sourcePixel = aSourceData;
  78. int32_t destOffset = aDestStride - aSize.width;
  79. uint8_t *destPixel = aDestData;
  80. for (int32_t y = 0; y < aSize.height; y++) {
  81. for (int32_t x = 0; x < aSize.width; x++) {
  82. uint8_t a = sourcePixel[GFX_ARGB32_OFFSET_A];
  83. if (a) {
  84. *destPixel = (redFactor * sourcePixel[GFX_ARGB32_OFFSET_R] +
  85. greenFactor * sourcePixel[GFX_ARGB32_OFFSET_G] +
  86. blueFactor * sourcePixel[GFX_ARGB32_OFFSET_B]) >> 8;
  87. } else {
  88. *destPixel = 0;
  89. }
  90. sourcePixel += 4;
  91. destPixel++;
  92. }
  93. sourcePixel += sourceOffset;
  94. destPixel += destOffset;
  95. }
  96. }
  97. static void
  98. ComputeLinearRGBLuminanceMask(const uint8_t *aSourceData,
  99. int32_t aSourceStride,
  100. uint8_t *aDestData,
  101. int32_t aDestStride,
  102. const IntSize &aSize,
  103. float aOpacity)
  104. {
  105. int32_t redFactor = 55 * aOpacity; // 255 * 0.2125 * opacity
  106. int32_t greenFactor = 183 * aOpacity; // 255 * 0.7154 * opacity
  107. int32_t blueFactor = 18 * aOpacity; // 255 * 0.0721
  108. int32_t sourceOffset = aSourceStride - 4 * aSize.width;
  109. const uint8_t *sourcePixel = aSourceData;
  110. int32_t destOffset = aDestStride - aSize.width;
  111. uint8_t *destPixel = aDestData;
  112. for (int32_t y = 0; y < aSize.height; y++) {
  113. for (int32_t x = 0; x < aSize.width; x++) {
  114. uint8_t a = sourcePixel[GFX_ARGB32_OFFSET_A];
  115. // unpremultiply
  116. if (a) {
  117. if (a == 255) {
  118. /* sRGB -> linearRGB -> intensity */
  119. *destPixel =
  120. static_cast<uint8_t>
  121. ((gsRGBToLinearRGBMap[sourcePixel[GFX_ARGB32_OFFSET_R]] *
  122. redFactor +
  123. gsRGBToLinearRGBMap[sourcePixel[GFX_ARGB32_OFFSET_G]] *
  124. greenFactor +
  125. gsRGBToLinearRGBMap[sourcePixel[GFX_ARGB32_OFFSET_B]] *
  126. blueFactor) >> 8);
  127. } else {
  128. uint8_t tempPixel[4];
  129. tempPixel[GFX_ARGB32_OFFSET_B] =
  130. (255 * sourcePixel[GFX_ARGB32_OFFSET_B]) / a;
  131. tempPixel[GFX_ARGB32_OFFSET_G] =
  132. (255 * sourcePixel[GFX_ARGB32_OFFSET_G]) / a;
  133. tempPixel[GFX_ARGB32_OFFSET_R] =
  134. (255 * sourcePixel[GFX_ARGB32_OFFSET_R]) / a;
  135. /* sRGB -> linearRGB -> intensity */
  136. *destPixel =
  137. static_cast<uint8_t>
  138. (((gsRGBToLinearRGBMap[tempPixel[GFX_ARGB32_OFFSET_R]] *
  139. redFactor +
  140. gsRGBToLinearRGBMap[tempPixel[GFX_ARGB32_OFFSET_G]] *
  141. greenFactor +
  142. gsRGBToLinearRGBMap[tempPixel[GFX_ARGB32_OFFSET_B]] *
  143. blueFactor) >> 8) * (a / 255.0f));
  144. }
  145. } else {
  146. *destPixel = 0;
  147. }
  148. sourcePixel += 4;
  149. destPixel++;
  150. }
  151. sourcePixel += sourceOffset;
  152. destPixel += destOffset;
  153. }
  154. }
  155. static void
  156. ComputeAlphaMask(const uint8_t *aSourceData,
  157. int32_t aSourceStride,
  158. uint8_t *aDestData,
  159. int32_t aDestStride,
  160. const IntSize &aSize,
  161. float aOpacity)
  162. {
  163. int32_t sourceOffset = aSourceStride - 4 * aSize.width;
  164. const uint8_t *sourcePixel = aSourceData;
  165. int32_t destOffset = aDestStride - aSize.width;
  166. uint8_t *destPixel = aDestData;
  167. for (int32_t y = 0; y < aSize.height; y++) {
  168. for (int32_t x = 0; x < aSize.width; x++) {
  169. *destPixel = sourcePixel[GFX_ARGB32_OFFSET_A] * aOpacity;
  170. sourcePixel += 4;
  171. destPixel++;
  172. }
  173. sourcePixel += sourceOffset;
  174. destPixel += destOffset;
  175. }
  176. }
  177. //----------------------------------------------------------------------
  178. // Implementation
  179. nsIFrame*
  180. NS_NewSVGMaskFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
  181. {
  182. return new (aPresShell) nsSVGMaskFrame(aContext);
  183. }
  184. NS_IMPL_FRAMEARENA_HELPERS(nsSVGMaskFrame)
  185. already_AddRefed<SourceSurface>
  186. nsSVGMaskFrame::GetMaskForMaskedFrame(gfxContext* aContext,
  187. nsIFrame* aMaskedFrame,
  188. const gfxMatrix &aMatrix,
  189. float aOpacity,
  190. Matrix* aMaskTransform,
  191. uint8_t aMaskOp)
  192. {
  193. // If the flag is set when we get here, it means this mask frame
  194. // has already been used painting the current mask, and the document
  195. // has a mask reference loop.
  196. if (mInUse) {
  197. NS_WARNING("Mask loop detected!");
  198. return nullptr;
  199. }
  200. AutoMaskReferencer maskRef(this);
  201. gfxRect maskArea = GetMaskArea(aMaskedFrame);
  202. // Get the clip extents in device space:
  203. // Minimizing the mask surface extents (using both the current clip extents
  204. // and maskArea) is important for performance.
  205. aContext->Save();
  206. nsSVGUtils::SetClipRect(aContext, aMatrix, maskArea);
  207. aContext->SetMatrix(gfxMatrix());
  208. gfxRect maskSurfaceRect = aContext->GetClipExtents();
  209. maskSurfaceRect.RoundOut();
  210. aContext->Restore();
  211. bool resultOverflows;
  212. IntSize maskSurfaceSize =
  213. nsSVGUtils::ConvertToSurfaceSize(maskSurfaceRect.Size(), &resultOverflows);
  214. if (resultOverflows || maskSurfaceSize.IsEmpty()) {
  215. // XXXjwatt we should return an empty surface so we don't paint aMaskedFrame!
  216. return nullptr;
  217. }
  218. RefPtr<DrawTarget> maskDT =
  219. Factory::CreateDrawTarget(BackendType::CAIRO, maskSurfaceSize,
  220. SurfaceFormat::B8G8R8A8);
  221. if (!maskDT || !maskDT->IsValid()) {
  222. return nullptr;
  223. }
  224. gfxMatrix maskSurfaceMatrix =
  225. aContext->CurrentMatrix() * gfxMatrix::Translation(-maskSurfaceRect.TopLeft());
  226. RefPtr<gfxContext> tmpCtx = gfxContext::CreateOrNull(maskDT);
  227. MOZ_ASSERT(tmpCtx); // already checked the draw target above
  228. tmpCtx->SetMatrix(maskSurfaceMatrix);
  229. mMatrixForChildren = GetMaskTransform(aMaskedFrame) * aMatrix;
  230. for (nsIFrame* kid = mFrames.FirstChild(); kid;
  231. kid = kid->GetNextSibling()) {
  232. // The CTM of each frame referencing us can be different
  233. nsISVGChildFrame* SVGFrame = do_QueryFrame(kid);
  234. if (SVGFrame) {
  235. SVGFrame->NotifySVGChanged(nsISVGChildFrame::TRANSFORM_CHANGED);
  236. }
  237. gfxMatrix m = mMatrixForChildren;
  238. if (kid->GetContent()->IsSVGElement()) {
  239. m = static_cast<nsSVGElement*>(kid->GetContent())->
  240. PrependLocalTransformsTo(m, eUserSpaceToParent);
  241. }
  242. Unused << nsSVGUtils::PaintFrameWithEffects(kid, *tmpCtx, m);
  243. }
  244. RefPtr<SourceSurface> maskSnapshot = maskDT->Snapshot();
  245. if (!maskSnapshot) {
  246. return nullptr;
  247. }
  248. RefPtr<DataSourceSurface> maskSurface = maskSnapshot->GetDataSurface();
  249. DataSourceSurface::MappedSurface map;
  250. if (!maskSurface ||
  251. !maskSurface->Map(DataSourceSurface::MapType::READ, &map)) {
  252. return nullptr;
  253. }
  254. // Create alpha channel mask for output
  255. RefPtr<DataSourceSurface> destMaskSurface =
  256. Factory::CreateDataSourceSurface(maskSurfaceSize, SurfaceFormat::A8);
  257. if (!destMaskSurface) {
  258. return nullptr;
  259. }
  260. DataSourceSurface::MappedSurface destMap;
  261. if (!destMaskSurface->Map(DataSourceSurface::MapType::WRITE, &destMap)) {
  262. return nullptr;
  263. }
  264. uint8_t maskType;
  265. if (aMaskOp == NS_STYLE_MASK_MODE_MATCH_SOURCE) {
  266. maskType = StyleSVGReset()->mMaskType;
  267. } else {
  268. maskType = aMaskOp == NS_STYLE_MASK_MODE_LUMINANCE ?
  269. NS_STYLE_MASK_TYPE_LUMINANCE : NS_STYLE_MASK_TYPE_ALPHA;
  270. }
  271. if (maskType == NS_STYLE_MASK_TYPE_LUMINANCE) {
  272. if (StyleSVG()->mColorInterpolation ==
  273. NS_STYLE_COLOR_INTERPOLATION_LINEARRGB) {
  274. ComputeLinearRGBLuminanceMask(map.mData, map.mStride,
  275. destMap.mData, destMap.mStride,
  276. maskSurfaceSize, aOpacity);
  277. } else {
  278. ComputesRGBLuminanceMask(map.mData, map.mStride,
  279. destMap.mData, destMap.mStride,
  280. maskSurfaceSize, aOpacity);
  281. }
  282. } else {
  283. ComputeAlphaMask(map.mData, map.mStride,
  284. destMap.mData, destMap.mStride,
  285. maskSurfaceSize, aOpacity);
  286. }
  287. maskSurface->Unmap();
  288. destMaskSurface->Unmap();
  289. // Moz2D transforms in the opposite direction to Thebes
  290. if (!maskSurfaceMatrix.Invert()) {
  291. return nullptr;
  292. }
  293. *aMaskTransform = ToMatrix(maskSurfaceMatrix);
  294. return destMaskSurface.forget();
  295. }
  296. gfxRect
  297. nsSVGMaskFrame::GetMaskArea(nsIFrame* aMaskedFrame)
  298. {
  299. SVGMaskElement *maskElem = static_cast<SVGMaskElement*>(mContent);
  300. uint16_t units =
  301. maskElem->mEnumAttributes[SVGMaskElement::MASKUNITS].GetAnimValue();
  302. gfxRect bbox;
  303. if (units == SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
  304. bbox = nsSVGUtils::GetBBox(aMaskedFrame);
  305. }
  306. // Bounds in the user space of aMaskedFrame
  307. gfxRect maskArea = nsSVGUtils::GetRelativeRect(units,
  308. &maskElem->mLengthAttributes[SVGMaskElement::ATTR_X],
  309. bbox, aMaskedFrame);
  310. return maskArea;
  311. }
  312. nsresult
  313. nsSVGMaskFrame::AttributeChanged(int32_t aNameSpaceID,
  314. nsIAtom* aAttribute,
  315. int32_t aModType)
  316. {
  317. if (aNameSpaceID == kNameSpaceID_None &&
  318. (aAttribute == nsGkAtoms::x ||
  319. aAttribute == nsGkAtoms::y ||
  320. aAttribute == nsGkAtoms::width ||
  321. aAttribute == nsGkAtoms::height||
  322. aAttribute == nsGkAtoms::maskUnits ||
  323. aAttribute == nsGkAtoms::maskContentUnits)) {
  324. nsSVGEffects::InvalidateDirectRenderingObservers(this);
  325. }
  326. return nsSVGContainerFrame::AttributeChanged(aNameSpaceID,
  327. aAttribute, aModType);
  328. }
  329. #ifdef DEBUG
  330. void
  331. nsSVGMaskFrame::Init(nsIContent* aContent,
  332. nsContainerFrame* aParent,
  333. nsIFrame* aPrevInFlow)
  334. {
  335. NS_ASSERTION(aContent->IsSVGElement(nsGkAtoms::mask),
  336. "Content is not an SVG mask");
  337. nsSVGContainerFrame::Init(aContent, aParent, aPrevInFlow);
  338. }
  339. #endif /* DEBUG */
  340. nsIAtom *
  341. nsSVGMaskFrame::GetType() const
  342. {
  343. return nsGkAtoms::svgMaskFrame;
  344. }
  345. gfxMatrix
  346. nsSVGMaskFrame::GetCanvasTM()
  347. {
  348. return mMatrixForChildren;
  349. }
  350. gfxMatrix
  351. nsSVGMaskFrame::GetMaskTransform(nsIFrame* aMaskedFrame)
  352. {
  353. SVGMaskElement *content = static_cast<SVGMaskElement*>(mContent);
  354. nsSVGEnum* maskContentUnits =
  355. &content->mEnumAttributes[SVGMaskElement::MASKCONTENTUNITS];
  356. return nsSVGUtils::AdjustMatrixForUnits(gfxMatrix(), maskContentUnits,
  357. aMaskedFrame);
  358. }