SkRasterPipelineBlitter.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkColor.h"
  8. #include "include/core/SkColorFilter.h"
  9. #include "include/core/SkPaint.h"
  10. #include "include/core/SkShader.h"
  11. #include "include/private/SkTo.h"
  12. #include "src/core/SkArenaAlloc.h"
  13. #include "src/core/SkBlendModePriv.h"
  14. #include "src/core/SkBlitter.h"
  15. #include "src/core/SkColorSpacePriv.h"
  16. #include "src/core/SkColorSpaceXformSteps.h"
  17. #include "src/core/SkOpts.h"
  18. #include "src/core/SkRasterPipeline.h"
  19. #include "src/core/SkUtils.h"
  20. #include "src/shaders/SkShaderBase.h"
  21. class SkRasterPipelineBlitter final : public SkBlitter {
  22. public:
  23. // This is our common entrypoint for creating the blitter once we've sorted out shaders.
  24. static SkBlitter* Create(const SkPixmap&, const SkPaint&, SkArenaAlloc*,
  25. const SkRasterPipeline& shaderPipeline,
  26. bool is_opaque, bool is_constant);
  27. SkRasterPipelineBlitter(SkPixmap dst,
  28. SkBlendMode blend,
  29. SkArenaAlloc* alloc)
  30. : fDst(dst)
  31. , fBlend(blend)
  32. , fAlloc(alloc)
  33. , fColorPipeline(alloc)
  34. {}
  35. void blitH (int x, int y, int w) override;
  36. void blitAntiH (int x, int y, const SkAlpha[], const int16_t[]) override;
  37. void blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) override;
  38. void blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) override;
  39. void blitMask (const SkMask&, const SkIRect& clip) override;
  40. void blitRect (int x, int y, int width, int height) override;
  41. void blitV (int x, int y, int height, SkAlpha alpha) override;
  42. private:
  43. void append_load_dst (SkRasterPipeline*) const;
  44. void append_store (SkRasterPipeline*) const;
  45. SkPixmap fDst;
  46. SkBlendMode fBlend;
  47. SkArenaAlloc* fAlloc;
  48. SkRasterPipeline fColorPipeline;
  49. SkRasterPipeline_MemoryCtx
  50. fDstPtr = {nullptr,0}, // Always points to the top-left of fDst.
  51. fMaskPtr = {nullptr,0}; // Updated each call to blitMask().
  52. SkRasterPipeline_EmbossCtx fEmbossCtx; // Used only for k3D_Format masks.
  53. // We may be able to specialize blitH() or blitRect() into a memset.
  54. void (*fMemset2D)(SkPixmap*, int x,int y, int w,int h, uint64_t color) = nullptr;
  55. uint64_t fMemsetColor = 0; // Big enough for largest memsettable dst format, F16.
  56. // Built lazily on first use.
  57. std::function<void(size_t, size_t, size_t, size_t)> fBlitRect,
  58. fBlitAntiH,
  59. fBlitMaskA8,
  60. fBlitMaskLCD16,
  61. fBlitMask3D;
  62. // These values are pointed to by the blit pipelines above,
  63. // which allows us to adjust them from call to call.
  64. float fCurrentCoverage = 0.0f;
  65. float fDitherRate = 0.0f;
  66. typedef SkBlitter INHERITED;
  67. };
  68. SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
  69. const SkPaint& paint,
  70. const SkMatrix& ctm,
  71. SkArenaAlloc* alloc) {
  72. // For legacy to keep working, we need to sometimes still distinguish null dstCS from sRGB.
  73. #if 0
  74. SkColorSpace* dstCS = dst.colorSpace() ? dst.colorSpace()
  75. : sk_srgb_singleton();
  76. #else
  77. SkColorSpace* dstCS = dst.colorSpace();
  78. #endif
  79. SkColorType dstCT = dst.colorType();
  80. SkColor4f paintColor = paint.getColor4f();
  81. SkColorSpaceXformSteps(sk_srgb_singleton(), kUnpremul_SkAlphaType,
  82. dstCS, kUnpremul_SkAlphaType).apply(paintColor.vec());
  83. auto shader = as_SB(paint.getShader());
  84. SkRasterPipeline_<256> shaderPipeline;
  85. if (!shader) {
  86. // Having no shader makes things nice and easy... just use the paint color.
  87. shaderPipeline.append_constant_color(alloc, paintColor.premul().vec());
  88. bool is_opaque = paintColor.fA == 1.0f,
  89. is_constant = true;
  90. return SkRasterPipelineBlitter::Create(dst, paint, alloc,
  91. shaderPipeline, is_opaque, is_constant);
  92. }
  93. bool is_opaque = shader->isOpaque() && paintColor.fA == 1.0f;
  94. bool is_constant = shader->isConstant();
  95. if (shader->appendStages({&shaderPipeline, alloc, dstCT, dstCS, paint, nullptr, ctm})) {
  96. if (paintColor.fA != 1.0f) {
  97. shaderPipeline.append(SkRasterPipeline::scale_1_float,
  98. alloc->make<float>(paintColor.fA));
  99. }
  100. return SkRasterPipelineBlitter::Create(dst, paint, alloc,
  101. shaderPipeline, is_opaque, is_constant);
  102. }
  103. // The shader has opted out of drawing anything.
  104. return alloc->make<SkNullBlitter>();
  105. }
  106. SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst,
  107. const SkPaint& paint,
  108. const SkRasterPipeline& shaderPipeline,
  109. bool is_opaque,
  110. SkArenaAlloc* alloc) {
  111. bool is_constant = false; // If this were the case, it'd be better to just set a paint color.
  112. return SkRasterPipelineBlitter::Create(dst, paint, alloc,
  113. shaderPipeline, is_opaque, is_constant);
  114. }
  115. SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst,
  116. const SkPaint& paint,
  117. SkArenaAlloc* alloc,
  118. const SkRasterPipeline& shaderPipeline,
  119. bool is_opaque,
  120. bool is_constant) {
  121. auto blitter = alloc->make<SkRasterPipelineBlitter>(dst,
  122. paint.getBlendMode(),
  123. alloc);
  124. // Our job in this factory is to fill out the blitter's color pipeline.
  125. // This is the common front of the full blit pipelines, each constructed lazily on first use.
  126. // The full blit pipelines handle reading and writing the dst, blending, coverage, dithering.
  127. auto colorPipeline = &blitter->fColorPipeline;
  128. // Let's get the shader in first.
  129. colorPipeline->extend(shaderPipeline);
  130. // If there's a color filter it comes next.
  131. if (auto colorFilter = paint.getColorFilter()) {
  132. SkStageRec rec = {
  133. colorPipeline, alloc, dst.colorType(), dst.colorSpace(), paint, nullptr, SkMatrix::I()
  134. };
  135. colorFilter->appendStages(rec, is_opaque);
  136. is_opaque = is_opaque && (colorFilter->getFlags() & SkColorFilter::kAlphaUnchanged_Flag);
  137. }
  138. // Not all formats make sense to dither (think, F16). We set their dither rate
  139. // to zero. We need to decide if we're going to dither now to keep is_constant accurate.
  140. if (paint.isDither()) {
  141. switch (dst.info().colorType()) {
  142. default: blitter->fDitherRate = 0.0f; break;
  143. case kARGB_4444_SkColorType: blitter->fDitherRate = 1/15.0f; break;
  144. case kRGB_565_SkColorType: blitter->fDitherRate = 1/63.0f; break;
  145. case kGray_8_SkColorType:
  146. case kRGB_888x_SkColorType:
  147. case kRGBA_8888_SkColorType:
  148. case kBGRA_8888_SkColorType: blitter->fDitherRate = 1/255.0f; break;
  149. case kRGB_101010x_SkColorType:
  150. case kRGBA_1010102_SkColorType: blitter->fDitherRate = 1/1023.0f; break;
  151. }
  152. // TODO: for constant colors, we could try to measure the effect of dithering, and if
  153. // it has no value (i.e. all variations result in the same 32bit color, then we
  154. // could disable it (for speed, by not adding the stage).
  155. }
  156. is_constant = is_constant && (blitter->fDitherRate == 0.0f);
  157. // We're logically done here. The code between here and return blitter is all optimization.
  158. // A pipeline that's still constant here can collapse back into a constant color.
  159. if (is_constant) {
  160. SkColor4f constantColor;
  161. SkRasterPipeline_MemoryCtx constantColorPtr = { &constantColor, 0 };
  162. colorPipeline->append_gamut_clamp_if_normalized(dst.info());
  163. colorPipeline->append(SkRasterPipeline::store_f32, &constantColorPtr);
  164. colorPipeline->run(0,0,1,1);
  165. colorPipeline->reset();
  166. colorPipeline->append_constant_color(alloc, constantColor);
  167. is_opaque = constantColor.fA == 1.0f;
  168. }
  169. // We can strength-reduce SrcOver into Src when opaque.
  170. if (is_opaque && blitter->fBlend == SkBlendMode::kSrcOver) {
  171. blitter->fBlend = SkBlendMode::kSrc;
  172. }
  173. // When we're drawing a constant color in Src mode, we can sometimes just memset.
  174. // (The previous two optimizations help find more opportunities for this one.)
  175. if (is_constant && blitter->fBlend == SkBlendMode::kSrc) {
  176. // Run our color pipeline all the way through to produce what we'd memset when we can.
  177. // Not all blits can memset, so we need to keep colorPipeline too.
  178. SkRasterPipeline_<256> p;
  179. p.extend(*colorPipeline);
  180. p.append_gamut_clamp_if_normalized(dst.info());
  181. blitter->fDstPtr = SkRasterPipeline_MemoryCtx{&blitter->fMemsetColor, 0};
  182. blitter->append_store(&p);
  183. p.run(0,0,1,1);
  184. switch (blitter->fDst.shiftPerPixel()) {
  185. case 0: blitter->fMemset2D = [](SkPixmap* dst, int x,int y, int w,int h, uint64_t c) {
  186. void* p = dst->writable_addr(x,y);
  187. while (h --> 0) {
  188. memset(p, c, w);
  189. p = SkTAddOffset<void>(p, dst->rowBytes());
  190. }
  191. }; break;
  192. case 1: blitter->fMemset2D = [](SkPixmap* dst, int x,int y, int w,int h, uint64_t c) {
  193. SkOpts::rect_memset16(dst->writable_addr16(x,y), c, w, dst->rowBytes(), h);
  194. }; break;
  195. case 2: blitter->fMemset2D = [](SkPixmap* dst, int x,int y, int w,int h, uint64_t c) {
  196. SkOpts::rect_memset32(dst->writable_addr32(x,y), c, w, dst->rowBytes(), h);
  197. }; break;
  198. case 3: blitter->fMemset2D = [](SkPixmap* dst, int x,int y, int w,int h, uint64_t c) {
  199. SkOpts::rect_memset64(dst->writable_addr64(x,y), c, w, dst->rowBytes(), h);
  200. }; break;
  201. // TODO(F32)?
  202. }
  203. }
  204. blitter->fDstPtr = SkRasterPipeline_MemoryCtx{
  205. blitter->fDst.writable_addr(),
  206. blitter->fDst.rowBytesAsPixels(),
  207. };
  208. return blitter;
  209. }
  210. void SkRasterPipelineBlitter::append_load_dst(SkRasterPipeline* p) const {
  211. p->append_load_dst(fDst.info().colorType(), &fDstPtr);
  212. if (fDst.info().alphaType() == kUnpremul_SkAlphaType) {
  213. p->append(SkRasterPipeline::premul_dst);
  214. }
  215. }
  216. void SkRasterPipelineBlitter::append_store(SkRasterPipeline* p) const {
  217. if (fDst.info().alphaType() == kUnpremul_SkAlphaType) {
  218. p->append(SkRasterPipeline::unpremul);
  219. }
  220. if (fDitherRate > 0.0f) {
  221. p->append(SkRasterPipeline::dither, &fDitherRate);
  222. }
  223. p->append_store(fDst.info().colorType(), &fDstPtr);
  224. }
  225. void SkRasterPipelineBlitter::blitH(int x, int y, int w) {
  226. this->blitRect(x,y,w,1);
  227. }
  228. void SkRasterPipelineBlitter::blitRect(int x, int y, int w, int h) {
  229. if (fMemset2D) {
  230. fMemset2D(&fDst, x,y, w,h, fMemsetColor);
  231. return;
  232. }
  233. if (!fBlitRect) {
  234. SkRasterPipeline p(fAlloc);
  235. p.extend(fColorPipeline);
  236. p.append_gamut_clamp_if_normalized(fDst.info());
  237. if (fBlend == SkBlendMode::kSrcOver
  238. && (fDst.info().colorType() == kRGBA_8888_SkColorType ||
  239. fDst.info().colorType() == kBGRA_8888_SkColorType)
  240. && !fDst.colorSpace()
  241. && fDst.info().alphaType() != kUnpremul_SkAlphaType
  242. && fDitherRate == 0.0f) {
  243. if (fDst.info().colorType() == kBGRA_8888_SkColorType) {
  244. p.append(SkRasterPipeline::swap_rb);
  245. }
  246. p.append(SkRasterPipeline::srcover_rgba_8888, &fDstPtr);
  247. } else {
  248. if (fBlend != SkBlendMode::kSrc) {
  249. this->append_load_dst(&p);
  250. SkBlendMode_AppendStages(fBlend, &p);
  251. }
  252. this->append_store(&p);
  253. }
  254. fBlitRect = p.compile();
  255. }
  256. fBlitRect(x,y,w,h);
  257. }
  258. void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
  259. if (!fBlitAntiH) {
  260. SkRasterPipeline p(fAlloc);
  261. p.extend(fColorPipeline);
  262. p.append_gamut_clamp_if_normalized(fDst.info());
  263. if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/false)) {
  264. p.append(SkRasterPipeline::scale_1_float, &fCurrentCoverage);
  265. this->append_load_dst(&p);
  266. SkBlendMode_AppendStages(fBlend, &p);
  267. } else {
  268. this->append_load_dst(&p);
  269. SkBlendMode_AppendStages(fBlend, &p);
  270. p.append(SkRasterPipeline::lerp_1_float, &fCurrentCoverage);
  271. }
  272. this->append_store(&p);
  273. fBlitAntiH = p.compile();
  274. }
  275. for (int16_t run = *runs; run > 0; run = *runs) {
  276. switch (*aa) {
  277. case 0x00: break;
  278. case 0xff: this->blitH(x,y,run); break;
  279. default:
  280. fCurrentCoverage = *aa * (1/255.0f);
  281. fBlitAntiH(x,y,run,1);
  282. }
  283. x += run;
  284. runs += run;
  285. aa += run;
  286. }
  287. }
  288. void SkRasterPipelineBlitter::blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) {
  289. SkIRect clip = {x,y, x+2,y+1};
  290. uint8_t coverage[] = { (uint8_t)a0, (uint8_t)a1 };
  291. SkMask mask;
  292. mask.fImage = coverage;
  293. mask.fBounds = clip;
  294. mask.fRowBytes = 2;
  295. mask.fFormat = SkMask::kA8_Format;
  296. this->blitMask(mask, clip);
  297. }
  298. void SkRasterPipelineBlitter::blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) {
  299. SkIRect clip = {x,y, x+1,y+2};
  300. uint8_t coverage[] = { (uint8_t)a0, (uint8_t)a1 };
  301. SkMask mask;
  302. mask.fImage = coverage;
  303. mask.fBounds = clip;
  304. mask.fRowBytes = 1;
  305. mask.fFormat = SkMask::kA8_Format;
  306. this->blitMask(mask, clip);
  307. }
  308. void SkRasterPipelineBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
  309. SkIRect clip = {x,y, x+1,y+height};
  310. SkMask mask;
  311. mask.fImage = &alpha;
  312. mask.fBounds = clip;
  313. mask.fRowBytes = 0; // so we reuse the 1 "row" for all of height
  314. mask.fFormat = SkMask::kA8_Format;
  315. this->blitMask(mask, clip);
  316. }
  317. void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
  318. if (mask.fFormat == SkMask::kBW_Format) {
  319. // TODO: native BW masks?
  320. return INHERITED::blitMask(mask, clip);
  321. }
  322. // ARGB and SDF masks shouldn't make it here.
  323. SkASSERT(mask.fFormat == SkMask::kA8_Format
  324. || mask.fFormat == SkMask::kLCD16_Format
  325. || mask.fFormat == SkMask::k3D_Format);
  326. auto extract_mask_plane = [&mask](int plane, SkRasterPipeline_MemoryCtx* ctx) {
  327. // LCD is 16-bit per pixel; A8 and 3D are 8-bit per pixel.
  328. size_t bpp = mask.fFormat == SkMask::kLCD16_Format ? 2 : 1;
  329. // Select the right mask plane. Usually plane == 0 and this is just mask.fImage.
  330. auto ptr = (uintptr_t)mask.fImage
  331. + plane * mask.computeImageSize();
  332. // Update ctx to point "into" this current mask, but lined up with fDstPtr at (0,0).
  333. // This sort of trickery upsets UBSAN (pointer-overflow) so our ptr must be a uintptr_t.
  334. // mask.fRowBytes is a uint32_t, which would break our addressing math on 64-bit builds.
  335. size_t rowBytes = mask.fRowBytes;
  336. ctx->stride = rowBytes / bpp;
  337. ctx->pixels = (void*)(ptr - mask.fBounds.left() * bpp
  338. - mask.fBounds.top() * rowBytes);
  339. };
  340. extract_mask_plane(0, &fMaskPtr);
  341. if (mask.fFormat == SkMask::k3D_Format) {
  342. extract_mask_plane(1, &fEmbossCtx.mul);
  343. extract_mask_plane(2, &fEmbossCtx.add);
  344. }
  345. // Lazily build whichever pipeline we need, specialized for each mask format.
  346. if (mask.fFormat == SkMask::kA8_Format && !fBlitMaskA8) {
  347. SkRasterPipeline p(fAlloc);
  348. p.extend(fColorPipeline);
  349. p.append_gamut_clamp_if_normalized(fDst.info());
  350. if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/false)) {
  351. p.append(SkRasterPipeline::scale_u8, &fMaskPtr);
  352. this->append_load_dst(&p);
  353. SkBlendMode_AppendStages(fBlend, &p);
  354. } else {
  355. this->append_load_dst(&p);
  356. SkBlendMode_AppendStages(fBlend, &p);
  357. p.append(SkRasterPipeline::lerp_u8, &fMaskPtr);
  358. }
  359. this->append_store(&p);
  360. fBlitMaskA8 = p.compile();
  361. }
  362. if (mask.fFormat == SkMask::kLCD16_Format && !fBlitMaskLCD16) {
  363. SkRasterPipeline p(fAlloc);
  364. p.extend(fColorPipeline);
  365. p.append_gamut_clamp_if_normalized(fDst.info());
  366. if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/true)) {
  367. // Somewhat unusually, scale_565 needs dst loaded first.
  368. this->append_load_dst(&p);
  369. p.append(SkRasterPipeline::scale_565, &fMaskPtr);
  370. SkBlendMode_AppendStages(fBlend, &p);
  371. } else {
  372. this->append_load_dst(&p);
  373. SkBlendMode_AppendStages(fBlend, &p);
  374. p.append(SkRasterPipeline::lerp_565, &fMaskPtr);
  375. }
  376. this->append_store(&p);
  377. fBlitMaskLCD16 = p.compile();
  378. }
  379. if (mask.fFormat == SkMask::k3D_Format && !fBlitMask3D) {
  380. SkRasterPipeline p(fAlloc);
  381. p.extend(fColorPipeline);
  382. // This bit is where we differ from kA8_Format:
  383. p.append(SkRasterPipeline::emboss, &fEmbossCtx);
  384. // Now onward just as kA8.
  385. p.append_gamut_clamp_if_normalized(fDst.info());
  386. if (SkBlendMode_ShouldPreScaleCoverage(fBlend, /*rgb_coverage=*/false)) {
  387. p.append(SkRasterPipeline::scale_u8, &fMaskPtr);
  388. this->append_load_dst(&p);
  389. SkBlendMode_AppendStages(fBlend, &p);
  390. } else {
  391. this->append_load_dst(&p);
  392. SkBlendMode_AppendStages(fBlend, &p);
  393. p.append(SkRasterPipeline::lerp_u8, &fMaskPtr);
  394. }
  395. this->append_store(&p);
  396. fBlitMask3D = p.compile();
  397. }
  398. std::function<void(size_t,size_t,size_t,size_t)>* blitter = nullptr;
  399. switch (mask.fFormat) {
  400. case SkMask::kA8_Format: blitter = &fBlitMaskA8; break;
  401. case SkMask::kLCD16_Format: blitter = &fBlitMaskLCD16; break;
  402. case SkMask::k3D_Format: blitter = &fBlitMask3D; break;
  403. default:
  404. SkASSERT(false);
  405. return;
  406. }
  407. SkASSERT(blitter);
  408. (*blitter)(clip.left(),clip.top(), clip.width(),clip.height());
  409. }