SkBitmapProcState.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * Copyright 2011 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/SkImageEncoder.h"
  8. #include "include/core/SkPaint.h"
  9. #include "include/core/SkShader.h"
  10. #include "include/private/SkColorData.h"
  11. #include "include/private/SkMacros.h"
  12. #include "src/core/SkBitmapCache.h"
  13. #include "src/core/SkBitmapController.h"
  14. #include "src/core/SkBitmapProcState.h"
  15. #include "src/core/SkMipMap.h"
  16. #include "src/core/SkOpts.h"
  17. #include "src/core/SkResourceCache.h"
  18. #include "src/core/SkUtils.h"
  19. // One-stop-shop shader for,
  20. // - nearest-neighbor sampling (_nofilter_),
  21. // - clamp tiling in X and Y both (Clamp_),
  22. // - with at most a scale and translate matrix (_DX_),
  23. // - and no extra alpha applied (_opaque_),
  24. // - sampling from 8888 (_S32_) and drawing to 8888 (_S32_).
  25. static void Clamp_S32_opaque_D32_nofilter_DX_shaderproc(const void* sIn, int x, int y,
  26. SkPMColor* dst, int count) {
  27. const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
  28. SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask |
  29. SkMatrix::kScale_Mask)) == 0);
  30. SkASSERT(s.fAlphaScale == 256);
  31. const unsigned maxX = s.fPixmap.width() - 1;
  32. SkFractionalInt fx;
  33. int dstY;
  34. {
  35. const SkBitmapProcStateAutoMapper mapper(s, x, y);
  36. const unsigned maxY = s.fPixmap.height() - 1;
  37. dstY = SkClampMax(mapper.intY(), maxY);
  38. fx = mapper.fractionalIntX();
  39. }
  40. const SkPMColor* src = s.fPixmap.addr32(0, dstY);
  41. const SkFractionalInt dx = s.fInvSxFractionalInt;
  42. // Check if we're safely inside [0...maxX] so no need to clamp each computed index.
  43. //
  44. if ((uint64_t)SkFractionalIntToInt(fx) <= maxX &&
  45. (uint64_t)SkFractionalIntToInt(fx + dx * (count - 1)) <= maxX)
  46. {
  47. int count4 = count >> 2;
  48. for (int i = 0; i < count4; ++i) {
  49. SkPMColor src0 = src[SkFractionalIntToInt(fx)]; fx += dx;
  50. SkPMColor src1 = src[SkFractionalIntToInt(fx)]; fx += dx;
  51. SkPMColor src2 = src[SkFractionalIntToInt(fx)]; fx += dx;
  52. SkPMColor src3 = src[SkFractionalIntToInt(fx)]; fx += dx;
  53. dst[0] = src0;
  54. dst[1] = src1;
  55. dst[2] = src2;
  56. dst[3] = src3;
  57. dst += 4;
  58. }
  59. for (int i = (count4 << 2); i < count; ++i) {
  60. unsigned index = SkFractionalIntToInt(fx);
  61. SkASSERT(index <= maxX);
  62. *dst++ = src[index];
  63. fx += dx;
  64. }
  65. } else {
  66. for (int i = 0; i < count; ++i) {
  67. dst[i] = src[SkClampMax(SkFractionalIntToInt(fx), maxX)];
  68. fx += dx;
  69. }
  70. }
  71. }
  72. static void S32_alpha_D32_nofilter_DX(const SkBitmapProcState& s,
  73. const uint32_t* xy, int count, SkPMColor* colors) {
  74. SkASSERT(count > 0 && colors != nullptr);
  75. SkASSERT(s.fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask));
  76. SkASSERT(kNone_SkFilterQuality == s.fFilterQuality);
  77. SkASSERT(4 == s.fPixmap.info().bytesPerPixel());
  78. SkASSERT(s.fAlphaScale <= 256);
  79. // xy is a 32-bit y-coordinate, followed by 16-bit x-coordinates.
  80. unsigned y = *xy++;
  81. SkASSERT(y < (unsigned)s.fPixmap.height());
  82. auto row = (const SkPMColor*)( (const char*)s.fPixmap.addr() + y * s.fPixmap.rowBytes() );
  83. if (1 == s.fPixmap.width()) {
  84. sk_memset32(colors, SkAlphaMulQ(row[0], s.fAlphaScale), count);
  85. return;
  86. }
  87. // Step 4 xs == 2 uint32_t at a time.
  88. while (count >= 4) {
  89. uint32_t x01 = *xy++,
  90. x23 = *xy++;
  91. SkPMColor p0 = row[UNPACK_PRIMARY_SHORT (x01)];
  92. SkPMColor p1 = row[UNPACK_SECONDARY_SHORT(x01)];
  93. SkPMColor p2 = row[UNPACK_PRIMARY_SHORT (x23)];
  94. SkPMColor p3 = row[UNPACK_SECONDARY_SHORT(x23)];
  95. *colors++ = SkAlphaMulQ(p0, s.fAlphaScale);
  96. *colors++ = SkAlphaMulQ(p1, s.fAlphaScale);
  97. *colors++ = SkAlphaMulQ(p2, s.fAlphaScale);
  98. *colors++ = SkAlphaMulQ(p3, s.fAlphaScale);
  99. count -= 4;
  100. }
  101. // Step 1 x == 1 uint16_t at a time.
  102. auto x = (const uint16_t*)xy;
  103. while (count --> 0) {
  104. *colors++ = SkAlphaMulQ(row[*x++], s.fAlphaScale);
  105. }
  106. }
  107. SkBitmapProcInfo::SkBitmapProcInfo(const SkBitmapProvider& provider,
  108. SkTileMode tmx, SkTileMode tmy)
  109. : fProvider(provider)
  110. , fTileModeX(tmx)
  111. , fTileModeY(tmy)
  112. , fBMState(nullptr)
  113. {}
  114. SkBitmapProcInfo::~SkBitmapProcInfo() {}
  115. // true iff the matrix has a scale and no more than an optional translate.
  116. static bool matrix_only_scale_translate(const SkMatrix& m) {
  117. return (m.getType() & ~SkMatrix::kTranslate_Mask) == SkMatrix::kScale_Mask;
  118. }
  119. /**
  120. * For the purposes of drawing bitmaps, if a matrix is "almost" translate
  121. * go ahead and treat it as if it were, so that subsequent code can go fast.
  122. */
  123. static bool just_trans_general(const SkMatrix& matrix) {
  124. SkASSERT(matrix_only_scale_translate(matrix));
  125. const SkScalar tol = SK_Scalar1 / 32768;
  126. return SkScalarNearlyZero(matrix[SkMatrix::kMScaleX] - SK_Scalar1, tol)
  127. && SkScalarNearlyZero(matrix[SkMatrix::kMScaleY] - SK_Scalar1, tol);
  128. }
  129. /**
  130. * Determine if the matrix can be treated as integral-only-translate,
  131. * for the purpose of filtering.
  132. */
  133. static bool just_trans_integral(const SkMatrix& m) {
  134. static constexpr SkScalar tol = SK_Scalar1 / 256;
  135. return m.getType() <= SkMatrix::kTranslate_Mask
  136. && SkScalarNearlyEqual(m.getTranslateX(), SkScalarRoundToScalar(m.getTranslateX()), tol)
  137. && SkScalarNearlyEqual(m.getTranslateY(), SkScalarRoundToScalar(m.getTranslateY()), tol);
  138. }
  139. static bool valid_for_filtering(unsigned dimension) {
  140. // for filtering, width and height must fit in 14bits, since we use steal
  141. // 2 bits from each to store our 4bit subpixel data
  142. return (dimension & ~0x3FFF) == 0;
  143. }
  144. bool SkBitmapProcInfo::init(const SkMatrix& inv, const SkPaint& paint) {
  145. SkASSERT(inv.isScaleTranslate());
  146. fPixmap.reset();
  147. fInvMatrix = inv;
  148. fFilterQuality = paint.getFilterQuality();
  149. fBMState = SkBitmapController::RequestBitmap(fProvider, inv, paint.getFilterQuality(), &fAlloc);
  150. // Note : we allow the controller to return an empty (zero-dimension) result. Should we?
  151. if (nullptr == fBMState || fBMState->pixmap().info().isEmpty()) {
  152. return false;
  153. }
  154. fPixmap = fBMState->pixmap();
  155. fInvMatrix = fBMState->invMatrix();
  156. fRealInvMatrix = fBMState->invMatrix();
  157. fPaintColor = paint.getColor();
  158. fFilterQuality = fBMState->quality();
  159. SkASSERT(fFilterQuality <= kLow_SkFilterQuality);
  160. SkASSERT(fPixmap.addr());
  161. bool integral_translate_only = just_trans_integral(fInvMatrix);
  162. if (!integral_translate_only) {
  163. // Most of the scanline procs deal with "unit" texture coordinates, as this
  164. // makes it easy to perform tiling modes (repeat = (x & 0xFFFF)). To generate
  165. // those, we divide the matrix by its dimensions here.
  166. //
  167. // We don't do this if we're either trivial (can ignore the matrix) or clamping
  168. // in both X and Y since clamping to width,height is just as easy as to 0xFFFF.
  169. if (fTileModeX != SkTileMode::kClamp || fTileModeY != SkTileMode::kClamp) {
  170. fInvMatrix.postIDiv(fPixmap.width(), fPixmap.height());
  171. }
  172. // Now that all possible changes to the matrix have taken place, check
  173. // to see if we're really close to a no-scale matrix. If so, explicitly
  174. // set it to be so. Subsequent code may inspect this matrix to choose
  175. // a faster path in this case.
  176. // This code will only execute if the matrix has some scale component;
  177. // if it's already pure translate then we won't do this inversion.
  178. if (matrix_only_scale_translate(fInvMatrix)) {
  179. SkMatrix forward;
  180. if (fInvMatrix.invert(&forward) && just_trans_general(forward)) {
  181. fInvMatrix.setTranslate(-forward.getTranslateX(), -forward.getTranslateY());
  182. }
  183. }
  184. // Recompute the flag after matrix adjustments.
  185. integral_translate_only = just_trans_integral(fInvMatrix);
  186. }
  187. fInvType = fInvMatrix.getType();
  188. if (kLow_SkFilterQuality == fFilterQuality &&
  189. (!valid_for_filtering(fPixmap.width() | fPixmap.height()) ||
  190. integral_translate_only)) {
  191. fFilterQuality = kNone_SkFilterQuality;
  192. }
  193. return true;
  194. }
  195. /*
  196. * Analyze filter-quality and matrix, and decide how to implement that.
  197. *
  198. * In general, we cascade down the request level [ High ... None ]
  199. * - for a given level, if we can fulfill it, fine, else
  200. * - else we downgrade to the next lower level and try again.
  201. * We can always fulfill requests for Low and None
  202. * - sometimes we will "ignore" Low and give None, but this is likely a legacy perf hack
  203. * and may be removed.
  204. */
  205. bool SkBitmapProcState::chooseProcs() {
  206. SkASSERT(fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask));
  207. SkASSERT(fPixmap.colorType() == kN32_SkColorType);
  208. SkASSERT(fPixmap.alphaType() == kPremul_SkAlphaType ||
  209. fPixmap.alphaType() == kOpaque_SkAlphaType);
  210. SkASSERT(fTileModeX == fTileModeY);
  211. SkASSERT(fTileModeX != SkTileMode::kDecal);
  212. SkASSERT(fFilterQuality < kHigh_SkFilterQuality);
  213. fInvProc = SkMatrixPriv::GetMapXYProc(fInvMatrix);
  214. fInvSx = SkScalarToFixed (fInvMatrix.getScaleX());
  215. fInvSxFractionalInt = SkScalarToFractionalInt(fInvMatrix.getScaleX());
  216. fInvKy = SkScalarToFixed (fInvMatrix.getSkewY());
  217. fInvKyFractionalInt = SkScalarToFractionalInt(fInvMatrix.getSkewY());
  218. fAlphaScale = SkAlpha255To256(SkColorGetA(fPaintColor));
  219. bool translate_only = (fInvMatrix.getType() & ~SkMatrix::kTranslate_Mask) == 0;
  220. fMatrixProc = this->chooseMatrixProc(translate_only);
  221. SkASSERT(fMatrixProc);
  222. if (fFilterQuality > kNone_SkFilterQuality) {
  223. fSampleProc32 = SkOpts::S32_alpha_D32_filter_DX;
  224. } else {
  225. fSampleProc32 = S32_alpha_D32_nofilter_DX;
  226. }
  227. // our special-case shaderprocs
  228. // TODO: move this one into chooseShaderProc32() or pull all that in here.
  229. if (fAlphaScale == 256
  230. && fFilterQuality == kNone_SkFilterQuality
  231. && SkTileMode::kClamp == fTileModeX) {
  232. fShaderProc32 = Clamp_S32_opaque_D32_nofilter_DX_shaderproc;
  233. } else {
  234. fShaderProc32 = this->chooseShaderProc32();
  235. }
  236. return true;
  237. }
  238. static void Clamp_S32_D32_nofilter_trans_shaderproc(const void* sIn,
  239. int x, int y,
  240. SkPMColor* colors,
  241. int count) {
  242. const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
  243. SkASSERT(((s.fInvType & ~SkMatrix::kTranslate_Mask)) == 0);
  244. SkASSERT(s.fInvKy == 0);
  245. SkASSERT(count > 0 && colors != nullptr);
  246. SkASSERT(kNone_SkFilterQuality == s.fFilterQuality);
  247. const int maxX = s.fPixmap.width() - 1;
  248. const int maxY = s.fPixmap.height() - 1;
  249. int ix = s.fFilterOneX + x;
  250. int iy = SkClampMax(s.fFilterOneY + y, maxY);
  251. const SkPMColor* row = s.fPixmap.addr32(0, iy);
  252. // clamp to the left
  253. if (ix < 0) {
  254. int n = SkMin32(-ix, count);
  255. sk_memset32(colors, row[0], n);
  256. count -= n;
  257. if (0 == count) {
  258. return;
  259. }
  260. colors += n;
  261. SkASSERT(-ix == n);
  262. ix = 0;
  263. }
  264. // copy the middle
  265. if (ix <= maxX) {
  266. int n = SkMin32(maxX - ix + 1, count);
  267. memcpy(colors, row + ix, n * sizeof(SkPMColor));
  268. count -= n;
  269. if (0 == count) {
  270. return;
  271. }
  272. colors += n;
  273. }
  274. SkASSERT(count > 0);
  275. // clamp to the right
  276. sk_memset32(colors, row[maxX], count);
  277. }
  278. static inline int sk_int_mod(int x, int n) {
  279. SkASSERT(n > 0);
  280. if ((unsigned)x >= (unsigned)n) {
  281. if (x < 0) {
  282. x = n + ~(~x % n);
  283. } else {
  284. x = x % n;
  285. }
  286. }
  287. return x;
  288. }
  289. static inline int sk_int_mirror(int x, int n) {
  290. x = sk_int_mod(x, 2 * n);
  291. if (x >= n) {
  292. x = n + ~(x - n);
  293. }
  294. return x;
  295. }
  296. static void Repeat_S32_D32_nofilter_trans_shaderproc(const void* sIn,
  297. int x, int y,
  298. SkPMColor* colors,
  299. int count) {
  300. const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
  301. SkASSERT(((s.fInvType & ~SkMatrix::kTranslate_Mask)) == 0);
  302. SkASSERT(s.fInvKy == 0);
  303. SkASSERT(count > 0 && colors != nullptr);
  304. SkASSERT(kNone_SkFilterQuality == s.fFilterQuality);
  305. const int stopX = s.fPixmap.width();
  306. const int stopY = s.fPixmap.height();
  307. int ix = s.fFilterOneX + x;
  308. int iy = sk_int_mod(s.fFilterOneY + y, stopY);
  309. const SkPMColor* row = s.fPixmap.addr32(0, iy);
  310. ix = sk_int_mod(ix, stopX);
  311. for (;;) {
  312. int n = SkMin32(stopX - ix, count);
  313. memcpy(colors, row + ix, n * sizeof(SkPMColor));
  314. count -= n;
  315. if (0 == count) {
  316. return;
  317. }
  318. colors += n;
  319. ix = 0;
  320. }
  321. }
  322. static inline void filter_32_alpha(unsigned t,
  323. SkPMColor color0,
  324. SkPMColor color1,
  325. SkPMColor* dstColor,
  326. unsigned alphaScale) {
  327. SkASSERT((unsigned)t <= 0xF);
  328. SkASSERT(alphaScale <= 256);
  329. const uint32_t mask = 0xFF00FF;
  330. int scale = 256 - 16*t;
  331. uint32_t lo = (color0 & mask) * scale;
  332. uint32_t hi = ((color0 >> 8) & mask) * scale;
  333. scale = 16*t;
  334. lo += (color1 & mask) * scale;
  335. hi += ((color1 >> 8) & mask) * scale;
  336. // TODO: if (alphaScale < 256) ...
  337. lo = ((lo >> 8) & mask) * alphaScale;
  338. hi = ((hi >> 8) & mask) * alphaScale;
  339. *dstColor = ((lo >> 8) & mask) | (hi & ~mask);
  340. }
  341. static void S32_D32_constX_shaderproc(const void* sIn,
  342. int x, int y,
  343. SkPMColor* colors,
  344. int count) {
  345. const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
  346. SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) == 0);
  347. SkASSERT(s.fInvKy == 0);
  348. SkASSERT(count > 0 && colors != nullptr);
  349. SkASSERT(1 == s.fPixmap.width());
  350. int iY0;
  351. int iY1 SK_INIT_TO_AVOID_WARNING;
  352. int iSubY SK_INIT_TO_AVOID_WARNING;
  353. if (kNone_SkFilterQuality != s.fFilterQuality) {
  354. SkBitmapProcState::MatrixProc mproc = s.getMatrixProc();
  355. uint32_t xy[2];
  356. mproc(s, xy, 1, x, y);
  357. iY0 = xy[0] >> 18;
  358. iY1 = xy[0] & 0x3FFF;
  359. iSubY = (xy[0] >> 14) & 0xF;
  360. } else {
  361. int yTemp;
  362. if (s.fInvType > SkMatrix::kTranslate_Mask) {
  363. const SkBitmapProcStateAutoMapper mapper(s, x, y);
  364. // When the matrix has a scale component the setup code in
  365. // chooseProcs multiples the inverse matrix by the inverse of the
  366. // bitmap's width and height. Since this method is going to do
  367. // its own tiling and sampling we need to undo that here.
  368. if (SkTileMode::kClamp != s.fTileModeX || SkTileMode::kClamp != s.fTileModeY) {
  369. yTemp = SkFractionalIntToInt(mapper.fractionalIntY() * s.fPixmap.height());
  370. } else {
  371. yTemp = mapper.intY();
  372. }
  373. } else {
  374. yTemp = s.fFilterOneY + y;
  375. }
  376. const int stopY = s.fPixmap.height();
  377. switch (s.fTileModeY) {
  378. case SkTileMode::kClamp:
  379. iY0 = SkClampMax(yTemp, stopY-1);
  380. break;
  381. case SkTileMode::kRepeat:
  382. iY0 = sk_int_mod(yTemp, stopY);
  383. break;
  384. case SkTileMode::kMirror:
  385. default:
  386. iY0 = sk_int_mirror(yTemp, stopY);
  387. break;
  388. }
  389. #ifdef SK_DEBUG
  390. {
  391. const SkBitmapProcStateAutoMapper mapper(s, x, y);
  392. int iY2;
  393. if (s.fInvType > SkMatrix::kTranslate_Mask &&
  394. (SkTileMode::kClamp != s.fTileModeX || SkTileMode::kClamp != s.fTileModeY)) {
  395. iY2 = SkFractionalIntToInt(mapper.fractionalIntY() * s.fPixmap.height());
  396. } else {
  397. iY2 = mapper.intY();
  398. }
  399. switch (s.fTileModeY) {
  400. case SkTileMode::kClamp:
  401. iY2 = SkClampMax(iY2, stopY-1);
  402. break;
  403. case SkTileMode::kRepeat:
  404. iY2 = sk_int_mod(iY2, stopY);
  405. break;
  406. case SkTileMode::kMirror:
  407. default:
  408. iY2 = sk_int_mirror(iY2, stopY);
  409. break;
  410. }
  411. SkASSERT(iY0 == iY2);
  412. }
  413. #endif
  414. }
  415. const SkPMColor* row0 = s.fPixmap.addr32(0, iY0);
  416. SkPMColor color;
  417. if (kNone_SkFilterQuality != s.fFilterQuality) {
  418. const SkPMColor* row1 = s.fPixmap.addr32(0, iY1);
  419. filter_32_alpha(iSubY, *row0, *row1, &color, s.fAlphaScale);
  420. } else {
  421. if (s.fAlphaScale < 256) {
  422. color = SkAlphaMulQ(*row0, s.fAlphaScale);
  423. } else {
  424. color = *row0;
  425. }
  426. }
  427. sk_memset32(colors, color, count);
  428. }
  429. static void DoNothing_shaderproc(const void*, int x, int y,
  430. SkPMColor* colors, int count) {
  431. // if we get called, the matrix is too tricky, so we just draw nothing
  432. sk_memset32(colors, 0, count);
  433. }
  434. bool SkBitmapProcState::setupForTranslate() {
  435. SkPoint pt;
  436. const SkBitmapProcStateAutoMapper mapper(*this, 0, 0, &pt);
  437. /*
  438. * if the translate is larger than our ints, we can get random results, or
  439. * worse, we might get 0x80000000, which wreaks havoc on us, since we can't
  440. * negate it.
  441. */
  442. const SkScalar too_big = SkIntToScalar(1 << 30);
  443. if (SkScalarAbs(pt.fX) > too_big || SkScalarAbs(pt.fY) > too_big) {
  444. return false;
  445. }
  446. // Since we know we're not filtered, we re-purpose these fields allow
  447. // us to go from device -> src coordinates w/ just an integer add,
  448. // rather than running through the inverse-matrix
  449. fFilterOneX = mapper.intX();
  450. fFilterOneY = mapper.intY();
  451. return true;
  452. }
  453. SkBitmapProcState::ShaderProc32 SkBitmapProcState::chooseShaderProc32() {
  454. if (kN32_SkColorType != fPixmap.colorType()) {
  455. return nullptr;
  456. }
  457. static const unsigned kMask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask;
  458. if (1 == fPixmap.width() && 0 == (fInvType & ~kMask)) {
  459. if (kNone_SkFilterQuality == fFilterQuality &&
  460. fInvType <= SkMatrix::kTranslate_Mask &&
  461. !this->setupForTranslate()) {
  462. return DoNothing_shaderproc;
  463. }
  464. return S32_D32_constX_shaderproc;
  465. }
  466. if (fAlphaScale < 256) {
  467. return nullptr;
  468. }
  469. if (fInvType > SkMatrix::kTranslate_Mask) {
  470. return nullptr;
  471. }
  472. if (kNone_SkFilterQuality != fFilterQuality) {
  473. return nullptr;
  474. }
  475. SkTileMode tx = fTileModeX;
  476. SkTileMode ty = fTileModeY;
  477. if (SkTileMode::kClamp == tx && SkTileMode::kClamp == ty) {
  478. if (this->setupForTranslate()) {
  479. return Clamp_S32_D32_nofilter_trans_shaderproc;
  480. }
  481. return DoNothing_shaderproc;
  482. }
  483. if (SkTileMode::kRepeat == tx && SkTileMode::kRepeat == ty) {
  484. if (this->setupForTranslate()) {
  485. return Repeat_S32_D32_nofilter_trans_shaderproc;
  486. }
  487. return DoNothing_shaderproc;
  488. }
  489. return nullptr;
  490. }
  491. #ifdef SK_DEBUG
  492. static void check_scale_nofilter(uint32_t bitmapXY[], int count,
  493. unsigned mx, unsigned my) {
  494. unsigned y = *bitmapXY++;
  495. SkASSERT(y < my);
  496. const uint16_t* xptr = reinterpret_cast<const uint16_t*>(bitmapXY);
  497. for (int i = 0; i < count; ++i) {
  498. SkASSERT(xptr[i] < mx);
  499. }
  500. }
  501. static void check_scale_filter(uint32_t bitmapXY[], int count,
  502. unsigned mx, unsigned my) {
  503. uint32_t YY = *bitmapXY++;
  504. unsigned y0 = YY >> 18;
  505. unsigned y1 = YY & 0x3FFF;
  506. SkASSERT(y0 < my);
  507. SkASSERT(y1 < my);
  508. for (int i = 0; i < count; ++i) {
  509. uint32_t XX = bitmapXY[i];
  510. unsigned x0 = XX >> 18;
  511. unsigned x1 = XX & 0x3FFF;
  512. SkASSERT(x0 < mx);
  513. SkASSERT(x1 < mx);
  514. }
  515. }
  516. void SkBitmapProcState::DebugMatrixProc(const SkBitmapProcState& state,
  517. uint32_t bitmapXY[], int count,
  518. int x, int y) {
  519. SkASSERT(bitmapXY);
  520. SkASSERT(count > 0);
  521. state.fMatrixProc(state, bitmapXY, count, x, y);
  522. void (*proc)(uint32_t bitmapXY[], int count, unsigned mx, unsigned my);
  523. // There are two formats possible:
  524. // filter -vs- nofilter
  525. SkASSERT(state.fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask));
  526. proc = state.fFilterQuality != kNone_SkFilterQuality ?
  527. check_scale_filter : check_scale_nofilter;
  528. proc(bitmapXY, count, state.fPixmap.width(), state.fPixmap.height());
  529. }
  530. SkBitmapProcState::MatrixProc SkBitmapProcState::getMatrixProc() const {
  531. return DebugMatrixProc;
  532. }
  533. #endif
  534. /*
  535. The storage requirements for the different matrix procs are as follows,
  536. where each X or Y is 2 bytes, and N is the number of pixels/elements:
  537. scale/translate nofilter Y(4bytes) + N * X
  538. affine/perspective nofilter N * (X Y)
  539. scale/translate filter Y Y + N * (X X)
  540. affine filter N * (Y Y X X)
  541. */
  542. int SkBitmapProcState::maxCountForBufferSize(size_t bufferSize) const {
  543. int32_t size = static_cast<int32_t>(bufferSize);
  544. size &= ~3; // only care about 4-byte aligned chunks
  545. if (fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) {
  546. size -= 4; // the shared Y (or YY) coordinate
  547. if (size < 0) {
  548. size = 0;
  549. }
  550. size >>= 1;
  551. } else {
  552. size >>= 2;
  553. }
  554. if (fFilterQuality != kNone_SkFilterQuality) {
  555. size >>= 1;
  556. }
  557. return size;
  558. }