SkBlitter.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  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 "src/core/SkBlitter.h"
  8. #include "include/core/SkColor.h"
  9. #include "include/core/SkColorFilter.h"
  10. #include "include/core/SkString.h"
  11. #include "include/private/SkColorData.h"
  12. #include "include/private/SkTo.h"
  13. #include "src/core/SkAntiRun.h"
  14. #include "src/core/SkArenaAlloc.h"
  15. #include "src/core/SkMask.h"
  16. #include "src/core/SkMaskFilterBase.h"
  17. #include "src/core/SkPaintPriv.h"
  18. #include "src/core/SkReadBuffer.h"
  19. #include "src/core/SkRegionPriv.h"
  20. #include "src/core/SkTLazy.h"
  21. #include "src/core/SkUtils.h"
  22. #include "src/core/SkWriteBuffer.h"
  23. #include "src/core/SkXfermodeInterpretation.h"
  24. #include "src/shaders/SkShaderBase.h"
  25. SkBlitter::~SkBlitter() {}
  26. bool SkBlitter::isNullBlitter() const { return false; }
  27. const SkPixmap* SkBlitter::justAnOpaqueColor(uint32_t* value) {
  28. return nullptr;
  29. }
  30. /*
  31. void SkBlitter::blitH(int x, int y, int width) {
  32. SkDEBUGFAIL("unimplemented");
  33. }
  34. void SkBlitter::blitAntiH(int x, int y, const SkAlpha antialias[],
  35. const int16_t runs[]) {
  36. SkDEBUGFAIL("unimplemented");
  37. }
  38. */
  39. inline static SkAlpha ScalarToAlpha(SkScalar a) {
  40. SkAlpha alpha = (SkAlpha)(a * 255);
  41. return alpha > 247 ? 0xFF : alpha < 8 ? 0 : alpha;
  42. }
  43. void SkBlitter::blitFatAntiRect(const SkRect& rect) {
  44. SkIRect bounds = rect.roundOut();
  45. SkASSERT(bounds.width() >= 3);
  46. // skbug.com/7813
  47. // To ensure consistency of the threaded backend (a rect that's considered fat in the init-once
  48. // phase must also be considered fat in the draw phase), we have to deal with rects with small
  49. // heights because the horizontal tiling in the threaded backend may change the height.
  50. //
  51. // This also implies that we cannot do vertical tiling unless we can blit any rect (not just the
  52. // fat one.)
  53. if (bounds.height() == 0) {
  54. return;
  55. }
  56. int runSize = bounds.width() + 1; // +1 so we can set runs[bounds.width()] = 0
  57. void* storage = this->allocBlitMemory(runSize * (sizeof(int16_t) + sizeof(SkAlpha)));
  58. int16_t* runs = reinterpret_cast<int16_t*>(storage);
  59. SkAlpha* alphas = reinterpret_cast<SkAlpha*>(runs + runSize);
  60. runs[0] = 1;
  61. runs[1] = bounds.width() - 2;
  62. runs[bounds.width() - 1] = 1;
  63. runs[bounds.width()] = 0;
  64. SkScalar partialL = bounds.fLeft + 1 - rect.fLeft;
  65. SkScalar partialR = rect.fRight - (bounds.fRight - 1);
  66. SkScalar partialT = bounds.fTop + 1 - rect.fTop;
  67. SkScalar partialB = rect.fBottom - (bounds.fBottom - 1);
  68. if (bounds.height() == 1) {
  69. partialT = rect.fBottom - rect.fTop;
  70. }
  71. alphas[0] = ScalarToAlpha(partialL * partialT);
  72. alphas[1] = ScalarToAlpha(partialT);
  73. alphas[bounds.width() - 1] = ScalarToAlpha(partialR * partialT);
  74. this->blitAntiH(bounds.fLeft, bounds.fTop, alphas, runs);
  75. if (bounds.height() > 2) {
  76. this->blitAntiRect(bounds.fLeft, bounds.fTop + 1, bounds.width() - 2, bounds.height() - 2,
  77. ScalarToAlpha(partialL), ScalarToAlpha(partialR));
  78. }
  79. if (bounds.height() > 1) {
  80. alphas[0] = ScalarToAlpha(partialL * partialB);
  81. alphas[1] = ScalarToAlpha(partialB);
  82. alphas[bounds.width() - 1] = ScalarToAlpha(partialR * partialB);
  83. this->blitAntiH(bounds.fLeft, bounds.fBottom - 1, alphas, runs);
  84. }
  85. }
  86. void SkBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
  87. if (alpha == 255) {
  88. this->blitRect(x, y, 1, height);
  89. } else {
  90. int16_t runs[2];
  91. runs[0] = 1;
  92. runs[1] = 0;
  93. while (--height >= 0) {
  94. this->blitAntiH(x, y++, &alpha, runs);
  95. }
  96. }
  97. }
  98. void SkBlitter::blitRect(int x, int y, int width, int height) {
  99. SkASSERT(width > 0);
  100. while (--height >= 0) {
  101. this->blitH(x, y++, width);
  102. }
  103. }
  104. /// Default implementation doesn't check for easy optimizations
  105. /// such as alpha == 255; also uses blitV(), which some subclasses
  106. /// may not support.
  107. void SkBlitter::blitAntiRect(int x, int y, int width, int height,
  108. SkAlpha leftAlpha, SkAlpha rightAlpha) {
  109. if (leftAlpha > 0) { // we may send in x = -1 with leftAlpha = 0
  110. this->blitV(x, y, height, leftAlpha);
  111. }
  112. x++;
  113. if (width > 0) {
  114. this->blitRect(x, y, width, height);
  115. x += width;
  116. }
  117. if (rightAlpha > 0) {
  118. this->blitV(x, y, height, rightAlpha);
  119. }
  120. }
  121. //////////////////////////////////////////////////////////////////////////////
  122. static inline void bits_to_runs(SkBlitter* blitter, int x, int y,
  123. const uint8_t bits[],
  124. uint8_t left_mask, ptrdiff_t rowBytes,
  125. uint8_t right_mask) {
  126. int inFill = 0;
  127. int pos = 0;
  128. while (--rowBytes >= 0) {
  129. uint8_t b = *bits++ & left_mask;
  130. if (rowBytes == 0) {
  131. b &= right_mask;
  132. }
  133. for (uint8_t test = 0x80U; test != 0; test >>= 1) {
  134. if (b & test) {
  135. if (!inFill) {
  136. pos = x;
  137. inFill = true;
  138. }
  139. } else {
  140. if (inFill) {
  141. blitter->blitH(pos, y, x - pos);
  142. inFill = false;
  143. }
  144. }
  145. x += 1;
  146. }
  147. left_mask = 0xFFU;
  148. }
  149. // final cleanup
  150. if (inFill) {
  151. blitter->blitH(pos, y, x - pos);
  152. }
  153. }
  154. // maskBitCount is the number of 1's to place in the mask. It must be in the range between 1 and 8.
  155. static uint8_t generate_right_mask(int maskBitCount) {
  156. return static_cast<uint8_t>((0xFF00U >> maskBitCount) & 0xFF);
  157. }
  158. void SkBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
  159. SkASSERT(mask.fBounds.contains(clip));
  160. if (mask.fFormat == SkMask::kLCD16_Format) {
  161. return; // needs to be handled by subclass
  162. }
  163. if (mask.fFormat == SkMask::kBW_Format) {
  164. int cx = clip.fLeft;
  165. int cy = clip.fTop;
  166. int maskLeft = mask.fBounds.fLeft;
  167. int maskRowBytes = mask.fRowBytes;
  168. int height = clip.height();
  169. const uint8_t* bits = mask.getAddr1(cx, cy);
  170. SkDEBUGCODE(const uint8_t* endOfImage =
  171. mask.fImage + (mask.fBounds.height() - 1) * maskRowBytes
  172. + ((mask.fBounds.width() + 7) >> 3));
  173. if (cx == maskLeft && clip.fRight == mask.fBounds.fRight) {
  174. while (--height >= 0) {
  175. int affectedRightBit = mask.fBounds.width() - 1;
  176. ptrdiff_t rowBytes = (affectedRightBit >> 3) + 1;
  177. SkASSERT(bits + rowBytes <= endOfImage);
  178. U8CPU rightMask = generate_right_mask((affectedRightBit & 7) + 1);
  179. bits_to_runs(this, cx, cy, bits, 0xFF, rowBytes, rightMask);
  180. bits += maskRowBytes;
  181. cy += 1;
  182. }
  183. } else {
  184. // Bits is calculated as the offset into the mask at the point {cx, cy} therefore, all
  185. // addressing into the bit mask is relative to that point. Since this is an address
  186. // calculated from a arbitrary bit in that byte, calculate the left most bit.
  187. int bitsLeft = cx - ((cx - maskLeft) & 7);
  188. // Everything is relative to the bitsLeft.
  189. int leftEdge = cx - bitsLeft;
  190. SkASSERT(leftEdge >= 0);
  191. int rightEdge = clip.fRight - bitsLeft;
  192. SkASSERT(rightEdge > leftEdge);
  193. // Calculate left byte and mask
  194. const uint8_t* leftByte = bits;
  195. U8CPU leftMask = 0xFFU >> (leftEdge & 7);
  196. // Calculate right byte and mask
  197. int affectedRightBit = rightEdge - 1;
  198. const uint8_t* rightByte = bits + (affectedRightBit >> 3);
  199. U8CPU rightMask = generate_right_mask((affectedRightBit & 7) + 1);
  200. // leftByte and rightByte are byte locations therefore, to get a count of bytes the
  201. // code must add one.
  202. ptrdiff_t rowBytes = rightByte - leftByte + 1;
  203. while (--height >= 0) {
  204. SkASSERT(bits + rowBytes <= endOfImage);
  205. bits_to_runs(this, bitsLeft, cy, bits, leftMask, rowBytes, rightMask);
  206. bits += maskRowBytes;
  207. cy += 1;
  208. }
  209. }
  210. } else {
  211. int width = clip.width();
  212. SkAutoSTMalloc<64, int16_t> runStorage(width + 1);
  213. int16_t* runs = runStorage.get();
  214. const uint8_t* aa = mask.getAddr8(clip.fLeft, clip.fTop);
  215. sk_memset16((uint16_t*)runs, 1, width);
  216. runs[width] = 0;
  217. int height = clip.height();
  218. int y = clip.fTop;
  219. while (--height >= 0) {
  220. this->blitAntiH(clip.fLeft, y, aa, runs);
  221. aa += mask.fRowBytes;
  222. y += 1;
  223. }
  224. }
  225. }
  226. /////////////////////// these guys are not virtual, just a helpers
  227. void SkBlitter::blitMaskRegion(const SkMask& mask, const SkRegion& clip) {
  228. if (clip.quickReject(mask.fBounds)) {
  229. return;
  230. }
  231. SkRegion::Cliperator clipper(clip, mask.fBounds);
  232. while (!clipper.done()) {
  233. const SkIRect& cr = clipper.rect();
  234. this->blitMask(mask, cr);
  235. clipper.next();
  236. }
  237. }
  238. void SkBlitter::blitRectRegion(const SkIRect& rect, const SkRegion& clip) {
  239. SkRegion::Cliperator clipper(clip, rect);
  240. while (!clipper.done()) {
  241. const SkIRect& cr = clipper.rect();
  242. this->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height());
  243. clipper.next();
  244. }
  245. }
  246. void SkBlitter::blitRegion(const SkRegion& clip) {
  247. SkRegionPriv::VisitSpans(clip, [this](const SkIRect& r) {
  248. this->blitRect(r.left(), r.top(), r.width(), r.height());
  249. });
  250. }
  251. ///////////////////////////////////////////////////////////////////////////////
  252. void SkNullBlitter::blitH(int x, int y, int width) {}
  253. void SkNullBlitter::blitAntiH(int x, int y, const SkAlpha antialias[],
  254. const int16_t runs[]) {}
  255. void SkNullBlitter::blitV(int x, int y, int height, SkAlpha alpha) {}
  256. void SkNullBlitter::blitRect(int x, int y, int width, int height) {}
  257. void SkNullBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {}
  258. const SkPixmap* SkNullBlitter::justAnOpaqueColor(uint32_t* value) {
  259. return nullptr;
  260. }
  261. bool SkNullBlitter::isNullBlitter() const { return true; }
  262. ///////////////////////////////////////////////////////////////////////////////
  263. static int compute_anti_width(const int16_t runs[]) {
  264. int width = 0;
  265. for (;;) {
  266. int count = runs[0];
  267. SkASSERT(count >= 0);
  268. if (count == 0) {
  269. break;
  270. }
  271. width += count;
  272. runs += count;
  273. }
  274. return width;
  275. }
  276. static inline bool y_in_rect(int y, const SkIRect& rect) {
  277. return (unsigned)(y - rect.fTop) < (unsigned)rect.height();
  278. }
  279. static inline bool x_in_rect(int x, const SkIRect& rect) {
  280. return (unsigned)(x - rect.fLeft) < (unsigned)rect.width();
  281. }
  282. void SkRectClipBlitter::blitH(int left, int y, int width) {
  283. SkASSERT(width > 0);
  284. if (!y_in_rect(y, fClipRect)) {
  285. return;
  286. }
  287. int right = left + width;
  288. if (left < fClipRect.fLeft) {
  289. left = fClipRect.fLeft;
  290. }
  291. if (right > fClipRect.fRight) {
  292. right = fClipRect.fRight;
  293. }
  294. width = right - left;
  295. if (width > 0) {
  296. fBlitter->blitH(left, y, width);
  297. }
  298. }
  299. void SkRectClipBlitter::blitAntiH(int left, int y, const SkAlpha aa[],
  300. const int16_t runs[]) {
  301. if (!y_in_rect(y, fClipRect) || left >= fClipRect.fRight) {
  302. return;
  303. }
  304. int x0 = left;
  305. int x1 = left + compute_anti_width(runs);
  306. if (x1 <= fClipRect.fLeft) {
  307. return;
  308. }
  309. SkASSERT(x0 < x1);
  310. if (x0 < fClipRect.fLeft) {
  311. int dx = fClipRect.fLeft - x0;
  312. SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, dx);
  313. runs += dx;
  314. aa += dx;
  315. x0 = fClipRect.fLeft;
  316. }
  317. SkASSERT(x0 < x1 && runs[x1 - x0] == 0);
  318. if (x1 > fClipRect.fRight) {
  319. x1 = fClipRect.fRight;
  320. SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, x1 - x0);
  321. ((int16_t*)runs)[x1 - x0] = 0;
  322. }
  323. SkASSERT(x0 < x1 && runs[x1 - x0] == 0);
  324. SkASSERT(compute_anti_width(runs) == x1 - x0);
  325. fBlitter->blitAntiH(x0, y, aa, runs);
  326. }
  327. void SkRectClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
  328. SkASSERT(height > 0);
  329. if (!x_in_rect(x, fClipRect)) {
  330. return;
  331. }
  332. int y0 = y;
  333. int y1 = y + height;
  334. if (y0 < fClipRect.fTop) {
  335. y0 = fClipRect.fTop;
  336. }
  337. if (y1 > fClipRect.fBottom) {
  338. y1 = fClipRect.fBottom;
  339. }
  340. if (y0 < y1) {
  341. fBlitter->blitV(x, y0, y1 - y0, alpha);
  342. }
  343. }
  344. void SkRectClipBlitter::blitRect(int left, int y, int width, int height) {
  345. SkIRect r;
  346. r.set(left, y, left + width, y + height);
  347. if (r.intersect(fClipRect)) {
  348. fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
  349. }
  350. }
  351. void SkRectClipBlitter::blitAntiRect(int left, int y, int width, int height,
  352. SkAlpha leftAlpha, SkAlpha rightAlpha) {
  353. SkIRect r;
  354. // The *true* width of the rectangle blitted is width+2:
  355. r.set(left, y, left + width + 2, y + height);
  356. if (r.intersect(fClipRect)) {
  357. if (r.fLeft != left) {
  358. SkASSERT(r.fLeft > left);
  359. leftAlpha = 255;
  360. }
  361. if (r.fRight != left + width + 2) {
  362. SkASSERT(r.fRight < left + width + 2);
  363. rightAlpha = 255;
  364. }
  365. if (255 == leftAlpha && 255 == rightAlpha) {
  366. fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
  367. } else if (1 == r.width()) {
  368. if (r.fLeft == left) {
  369. fBlitter->blitV(r.fLeft, r.fTop, r.height(), leftAlpha);
  370. } else {
  371. SkASSERT(r.fLeft == left + width + 1);
  372. fBlitter->blitV(r.fLeft, r.fTop, r.height(), rightAlpha);
  373. }
  374. } else {
  375. fBlitter->blitAntiRect(r.fLeft, r.fTop, r.width() - 2, r.height(),
  376. leftAlpha, rightAlpha);
  377. }
  378. }
  379. }
  380. void SkRectClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
  381. SkASSERT(mask.fBounds.contains(clip));
  382. SkIRect r = clip;
  383. if (r.intersect(fClipRect)) {
  384. fBlitter->blitMask(mask, r);
  385. }
  386. }
  387. const SkPixmap* SkRectClipBlitter::justAnOpaqueColor(uint32_t* value) {
  388. return fBlitter->justAnOpaqueColor(value);
  389. }
  390. ///////////////////////////////////////////////////////////////////////////////
  391. void SkRgnClipBlitter::blitH(int x, int y, int width) {
  392. SkRegion::Spanerator span(*fRgn, y, x, x + width);
  393. int left, right;
  394. while (span.next(&left, &right)) {
  395. SkASSERT(left < right);
  396. fBlitter->blitH(left, y, right - left);
  397. }
  398. }
  399. void SkRgnClipBlitter::blitAntiH(int x, int y, const SkAlpha aa[],
  400. const int16_t runs[]) {
  401. int width = compute_anti_width(runs);
  402. SkRegion::Spanerator span(*fRgn, y, x, x + width);
  403. int left, right;
  404. SkDEBUGCODE(const SkIRect& bounds = fRgn->getBounds();)
  405. int prevRite = x;
  406. while (span.next(&left, &right)) {
  407. SkASSERT(x <= left);
  408. SkASSERT(left < right);
  409. SkASSERT(left >= bounds.fLeft && right <= bounds.fRight);
  410. SkAlphaRuns::Break((int16_t*)runs, (uint8_t*)aa, left - x, right - left);
  411. // now zero before left
  412. if (left > prevRite) {
  413. int index = prevRite - x;
  414. ((uint8_t*)aa)[index] = 0; // skip runs after right
  415. ((int16_t*)runs)[index] = SkToS16(left - prevRite);
  416. }
  417. prevRite = right;
  418. }
  419. if (prevRite > x) {
  420. ((int16_t*)runs)[prevRite - x] = 0;
  421. if (x < 0) {
  422. int skip = runs[0];
  423. SkASSERT(skip >= -x);
  424. aa += skip;
  425. runs += skip;
  426. x += skip;
  427. }
  428. fBlitter->blitAntiH(x, y, aa, runs);
  429. }
  430. }
  431. void SkRgnClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
  432. SkIRect bounds;
  433. bounds.set(x, y, x + 1, y + height);
  434. SkRegion::Cliperator iter(*fRgn, bounds);
  435. while (!iter.done()) {
  436. const SkIRect& r = iter.rect();
  437. SkASSERT(bounds.contains(r));
  438. fBlitter->blitV(x, r.fTop, r.height(), alpha);
  439. iter.next();
  440. }
  441. }
  442. void SkRgnClipBlitter::blitRect(int x, int y, int width, int height) {
  443. SkIRect bounds;
  444. bounds.set(x, y, x + width, y + height);
  445. SkRegion::Cliperator iter(*fRgn, bounds);
  446. while (!iter.done()) {
  447. const SkIRect& r = iter.rect();
  448. SkASSERT(bounds.contains(r));
  449. fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
  450. iter.next();
  451. }
  452. }
  453. void SkRgnClipBlitter::blitAntiRect(int x, int y, int width, int height,
  454. SkAlpha leftAlpha, SkAlpha rightAlpha) {
  455. // The *true* width of the rectangle to blit is width + 2
  456. SkIRect bounds;
  457. bounds.set(x, y, x + width + 2, y + height);
  458. SkRegion::Cliperator iter(*fRgn, bounds);
  459. while (!iter.done()) {
  460. const SkIRect& r = iter.rect();
  461. SkASSERT(bounds.contains(r));
  462. SkASSERT(r.fLeft >= x);
  463. SkASSERT(r.fRight <= x + width + 2);
  464. SkAlpha effectiveLeftAlpha = (r.fLeft == x) ? leftAlpha : 255;
  465. SkAlpha effectiveRightAlpha = (r.fRight == x + width + 2) ?
  466. rightAlpha : 255;
  467. if (255 == effectiveLeftAlpha && 255 == effectiveRightAlpha) {
  468. fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
  469. } else if (1 == r.width()) {
  470. if (r.fLeft == x) {
  471. fBlitter->blitV(r.fLeft, r.fTop, r.height(),
  472. effectiveLeftAlpha);
  473. } else {
  474. SkASSERT(r.fLeft == x + width + 1);
  475. fBlitter->blitV(r.fLeft, r.fTop, r.height(),
  476. effectiveRightAlpha);
  477. }
  478. } else {
  479. fBlitter->blitAntiRect(r.fLeft, r.fTop, r.width() - 2, r.height(),
  480. effectiveLeftAlpha, effectiveRightAlpha);
  481. }
  482. iter.next();
  483. }
  484. }
  485. void SkRgnClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
  486. SkASSERT(mask.fBounds.contains(clip));
  487. SkRegion::Cliperator iter(*fRgn, clip);
  488. const SkIRect& r = iter.rect();
  489. SkBlitter* blitter = fBlitter;
  490. while (!iter.done()) {
  491. blitter->blitMask(mask, r);
  492. iter.next();
  493. }
  494. }
  495. const SkPixmap* SkRgnClipBlitter::justAnOpaqueColor(uint32_t* value) {
  496. return fBlitter->justAnOpaqueColor(value);
  497. }
  498. ///////////////////////////////////////////////////////////////////////////////
  499. SkBlitter* SkBlitterClipper::apply(SkBlitter* blitter, const SkRegion* clip,
  500. const SkIRect* ir) {
  501. if (clip) {
  502. const SkIRect& clipR = clip->getBounds();
  503. if (clip->isEmpty() || (ir && !SkIRect::Intersects(clipR, *ir))) {
  504. blitter = &fNullBlitter;
  505. } else if (clip->isRect()) {
  506. if (ir == nullptr || !clipR.contains(*ir)) {
  507. fRectBlitter.init(blitter, clipR);
  508. blitter = &fRectBlitter;
  509. }
  510. } else {
  511. fRgnBlitter.init(blitter, clip);
  512. blitter = &fRgnBlitter;
  513. }
  514. }
  515. return blitter;
  516. }
  517. ///////////////////////////////////////////////////////////////////////////////
  518. #include "src/core/SkCoreBlitters.h"
  519. // hack for testing, not to be exposed to clients
  520. bool gSkForceRasterPipelineBlitter;
  521. bool SkBlitter::UseRasterPipelineBlitter(const SkPixmap& device, const SkPaint& paint,
  522. const SkMatrix& matrix) {
  523. if (gSkForceRasterPipelineBlitter) {
  524. return true;
  525. }
  526. #if 0 || defined(SK_FORCE_RASTER_PIPELINE_BLITTER)
  527. return true;
  528. #else
  529. const SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
  530. // The legacy blitters cannot handle any of these complex features (anymore).
  531. if (device.alphaType() == kUnpremul_SkAlphaType ||
  532. matrix.hasPerspective() ||
  533. paint.getColorFilter() ||
  534. paint.getBlendMode() > SkBlendMode::kLastCoeffMode ||
  535. paint.getFilterQuality() == kHigh_SkFilterQuality ||
  536. (mf && mf->getFormat() == SkMask::k3D_Format)) {
  537. return true;
  538. }
  539. // All the real legacy fast paths are for shaders and SrcOver.
  540. // Choosing SkRasterPipelineBlitter will also let us to hit its single-color memset path.
  541. if (!paint.getShader() && paint.getBlendMode() != SkBlendMode::kSrcOver) {
  542. return true;
  543. }
  544. auto cs = device.colorSpace();
  545. // We check (indirectly via makeContext()) later on if the shader can handle the colorspace
  546. // in legacy mode, so here we just focus on if a single color needs raster-pipeline.
  547. if (cs && !paint.getShader()) {
  548. if (!paint.getColor4f().fitsInBytes() || !cs->isSRGB()) {
  549. return true;
  550. }
  551. }
  552. // Only kN32 and 565 are handled by legacy blitters now, 565 mostly just for Android.
  553. return device.colorType() != kN32_SkColorType
  554. && device.colorType() != kRGB_565_SkColorType;
  555. #endif
  556. }
  557. SkBlitter* SkBlitter::Choose(const SkPixmap& device,
  558. const SkMatrix& matrix,
  559. const SkPaint& origPaint,
  560. SkArenaAlloc* alloc,
  561. bool drawCoverage) {
  562. SkASSERT(alloc);
  563. if (kUnknown_SkColorType == device.colorType()) {
  564. return alloc->make<SkNullBlitter>();
  565. }
  566. // We may tweak the original paint as we go.
  567. SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
  568. // We have the most fast-paths for SrcOver, so see if we can act like SrcOver.
  569. if (paint->getBlendMode() != SkBlendMode::kSrcOver) {
  570. switch (SkInterpretXfermode(*paint, SkColorTypeIsAlwaysOpaque(device.colorType()))) {
  571. case kSrcOver_SkXfermodeInterpretation:
  572. paint.writable()->setBlendMode(SkBlendMode::kSrcOver);
  573. break;
  574. case kSkipDrawing_SkXfermodeInterpretation:
  575. return alloc->make<SkNullBlitter>();
  576. default:
  577. break;
  578. }
  579. }
  580. // A Clear blend mode will ignore the entire color pipeline, as if Src mode with 0x00000000.
  581. if (paint->getBlendMode() == SkBlendMode::kClear) {
  582. SkPaint* p = paint.writable();
  583. p->setShader(nullptr);
  584. p->setColorFilter(nullptr);
  585. p->setBlendMode(SkBlendMode::kSrc);
  586. p->setColor(0x00000000);
  587. }
  588. if (drawCoverage) {
  589. if (device.colorType() == kAlpha_8_SkColorType) {
  590. SkASSERT(!paint->getShader());
  591. SkASSERT(paint->isSrcOver());
  592. return alloc->make<SkA8_Coverage_Blitter>(device, *paint);
  593. }
  594. return alloc->make<SkNullBlitter>();
  595. }
  596. if (paint->isDither() && !SkPaintPriv::ShouldDither(*paint, device.colorType())) {
  597. paint.writable()->setDither(false);
  598. }
  599. // We'll end here for many interesting cases: color spaces, color filters, most color types.
  600. if (UseRasterPipelineBlitter(device, *paint, matrix)) {
  601. auto blitter = SkCreateRasterPipelineBlitter(device, *paint, matrix, alloc);
  602. SkASSERT(blitter);
  603. return blitter;
  604. }
  605. // Everything but legacy kN32_SkColorType and kRGB_565_SkColorType should already be handled.
  606. SkASSERT(device.colorType() == kN32_SkColorType ||
  607. device.colorType() == kRGB_565_SkColorType);
  608. // And we should either have a shader, be blending with SrcOver, or both.
  609. SkASSERT(paint->getShader() || paint->getBlendMode() == SkBlendMode::kSrcOver);
  610. // Legacy blitters keep their shader state on a shader context.
  611. SkShaderBase::Context* shaderContext = nullptr;
  612. if (paint->getShader()) {
  613. shaderContext = as_SB(paint->getShader())->makeContext(
  614. {*paint, matrix, nullptr, device.colorType(), device.colorSpace()},
  615. alloc);
  616. // Creating the context isn't always possible... we'll just fall back to raster pipeline.
  617. if (!shaderContext) {
  618. auto blitter = SkCreateRasterPipelineBlitter(device, *paint, matrix, alloc);
  619. SkASSERT(blitter);
  620. return blitter;
  621. }
  622. }
  623. switch (device.colorType()) {
  624. case kN32_SkColorType:
  625. if (shaderContext) {
  626. return alloc->make<SkARGB32_Shader_Blitter>(device, *paint, shaderContext);
  627. } else if (paint->getColor() == SK_ColorBLACK) {
  628. return alloc->make<SkARGB32_Black_Blitter>(device, *paint);
  629. } else if (paint->getAlpha() == 0xFF) {
  630. return alloc->make<SkARGB32_Opaque_Blitter>(device, *paint);
  631. } else {
  632. return alloc->make<SkARGB32_Blitter>(device, *paint);
  633. }
  634. case kRGB_565_SkColorType:
  635. if (shaderContext && SkRGB565_Shader_Blitter::Supports(device, *paint)) {
  636. return alloc->make<SkRGB565_Shader_Blitter>(device, *paint, shaderContext);
  637. } else {
  638. return SkCreateRasterPipelineBlitter(device, *paint, matrix, alloc);
  639. }
  640. default:
  641. SkASSERT(false);
  642. return alloc->make<SkNullBlitter>();
  643. }
  644. }
  645. ///////////////////////////////////////////////////////////////////////////////
  646. SkShaderBlitter::SkShaderBlitter(const SkPixmap& device, const SkPaint& paint,
  647. SkShaderBase::Context* shaderContext)
  648. : INHERITED(device)
  649. , fShader(paint.getShader())
  650. , fShaderContext(shaderContext) {
  651. SkASSERT(fShader);
  652. SkASSERT(fShaderContext);
  653. fShader->ref();
  654. fShaderFlags = fShaderContext->getFlags();
  655. fConstInY = SkToBool(fShaderFlags & SkShaderBase::kConstInY32_Flag);
  656. }
  657. SkShaderBlitter::~SkShaderBlitter() {
  658. fShader->unref();
  659. }
  660. ///////////////////////////////////////////////////////////////////////////////////////////////////
  661. #ifdef SK_DEBUG
  662. void SkRectClipCheckBlitter::blitH(int x, int y, int width) {
  663. SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, width, 1)));
  664. fBlitter->blitH(x, y, width);
  665. }
  666. void SkRectClipCheckBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) {
  667. const int16_t* iter = runs;
  668. for (; *iter; iter += *iter)
  669. ;
  670. int width = iter - runs;
  671. SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, width, 1)));
  672. fBlitter->blitAntiH(x, y, aa, runs);
  673. }
  674. void SkRectClipCheckBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
  675. SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, 1, height)));
  676. fBlitter->blitV(x, y, height, alpha);
  677. }
  678. void SkRectClipCheckBlitter::blitRect(int x, int y, int width, int height) {
  679. SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, width, height)));
  680. fBlitter->blitRect(x, y, width, height);
  681. }
  682. void SkRectClipCheckBlitter::blitAntiRect(int x, int y, int width, int height,
  683. SkAlpha leftAlpha, SkAlpha rightAlpha) {
  684. bool skipLeft = !leftAlpha;
  685. bool skipRight = !rightAlpha;
  686. #ifdef SK_DEBUG
  687. SkIRect r = SkIRect::MakeXYWH(x + skipLeft, y, width + 2 - skipRight - skipLeft, height);
  688. SkASSERT(r.isEmpty() || fClipRect.contains(r));
  689. #endif
  690. fBlitter->blitAntiRect(x, y, width, height, leftAlpha, rightAlpha);
  691. }
  692. void SkRectClipCheckBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
  693. SkASSERT(mask.fBounds.contains(clip));
  694. SkASSERT(fClipRect.contains(clip));
  695. fBlitter->blitMask(mask, clip);
  696. }
  697. const SkPixmap* SkRectClipCheckBlitter::justAnOpaqueColor(uint32_t* value) {
  698. return fBlitter->justAnOpaqueColor(value);
  699. }
  700. void SkRectClipCheckBlitter::blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) {
  701. SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, 2, 1)));
  702. fBlitter->blitAntiH2(x, y, a0, a1);
  703. }
  704. void SkRectClipCheckBlitter::blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) {
  705. SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, 1, 2)));
  706. fBlitter->blitAntiV2(x, y, a0, a1);
  707. }
  708. #endif