blob: 7c37a154d2013ef857fb9620471b2a51c393ee81 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed04617132014-08-21 09:46:49 -07007
8#include "SkBitmapCache.h"
reed64045422015-06-04 06:31:31 -07009#include "SkBitmapController.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkBitmapProcState.h"
11#include "SkColorPriv.h"
12#include "SkFilterProc.h"
13#include "SkPaint.h"
14#include "SkShader.h" // for tilemodes
digit@google.com3ada0ef2012-08-13 14:06:34 +000015#include "SkUtilsArm.h"
humper@google.com138ebc32013-07-19 20:20:04 +000016#include "SkBitmapScaler.h"
reed@google.comd94697c2013-07-24 14:31:33 +000017#include "SkMipMap.h"
reed@google.comcee9dcb2013-09-13 16:04:49 +000018#include "SkPixelRef.h"
commit-bot@chromium.orgf4491562014-05-28 17:30:02 +000019#include "SkImageEncoder.h"
reed011f39a2014-08-28 13:35:23 -070020#include "SkResourceCache.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021
digit@google.com3ada0ef2012-08-13 14:06:34 +000022#if !SK_ARM_NEON_IS_NONE
23// These are defined in src/opts/SkBitmapProcState_arm_neon.cpp
digit@google.com3ada0ef2012-08-13 14:06:34 +000024extern const SkBitmapProcState::SampleProc32 gSkBitmapProcStateSample32_neon[];
25extern void S16_D16_filter_DX_neon(const SkBitmapProcState&, const uint32_t*, int, uint16_t*);
herbc7a784c2015-12-18 09:52:15 -080026extern void Clamp_S16_D16_filter_DX_shaderproc_neon(const void *, int, int, uint16_t*, int);
27extern void Repeat_S16_D16_filter_DX_shaderproc_neon(const void *, int, int, uint16_t*, int);
digit@google.com3ada0ef2012-08-13 14:06:34 +000028extern void SI8_opaque_D32_filter_DX_neon(const SkBitmapProcState&, const uint32_t*, int, SkPMColor*);
herbc7a784c2015-12-18 09:52:15 -080029extern void SI8_opaque_D32_filter_DX_shaderproc_neon(const void *, int, int, uint32_t*, int);
30extern void Clamp_SI8_opaque_D32_filter_DX_shaderproc_neon(const void*, int, int, uint32_t*, int);
digit@google.com3ada0ef2012-08-13 14:06:34 +000031#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000032
herbc7a784c2015-12-18 09:52:15 -080033extern void Clamp_S32_opaque_D32_nofilter_DX_shaderproc(const void*, int, int, uint32_t*, int);
mtklein14e4d392014-10-23 14:35:01 -070034
digit@google.com3ada0ef2012-08-13 14:06:34 +000035#define NAME_WRAP(x) x
36#include "SkBitmapProcState_filter.h"
37#include "SkBitmapProcState_procs.h"
reed@android.comb577b412009-10-27 17:49:32 +000038
reed7a4d8472015-09-15 13:33:58 -070039SkBitmapProcState::SkBitmapProcState(const SkBitmapProvider& provider,
40 SkShader::TileMode tmx, SkShader::TileMode tmy)
41 : fProvider(provider)
42 , fBMState(nullptr)
43{
44 fTileModeX = tmx;
45 fTileModeY = tmy;
46}
47
48SkBitmapProcState::SkBitmapProcState(const SkBitmap& bm,
49 SkShader::TileMode tmx, SkShader::TileMode tmy)
50 : fProvider(SkBitmapProvider(bm))
51 , fBMState(nullptr)
52{
53 fTileModeX = tmx;
54 fTileModeY = tmy;
55}
reed64045422015-06-04 06:31:31 -070056
57SkBitmapProcState::~SkBitmapProcState() {
58 SkInPlaceDeleteCheck(fBMState, fBMStateStorage.get());
59}
60
reed@android.coma44b4cc2009-07-16 02:03:58 +000061///////////////////////////////////////////////////////////////////////////////
62
reed@google.comee056a82013-04-18 15:33:27 +000063// true iff the matrix contains, at most, scale and translate elements
64static bool matrix_only_scale_translate(const SkMatrix& m) {
65 return m.getType() <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask);
66}
67
reed@google.comc0e88e02012-10-17 21:11:56 +000068/**
69 * For the purposes of drawing bitmaps, if a matrix is "almost" translate
70 * go ahead and treat it as if it were, so that subsequent code can go fast.
71 */
reedad7ae6c2015-06-04 14:12:25 -070072static bool just_trans_clamp(const SkMatrix& matrix, const SkPixmap& pixmap) {
reed@google.comee056a82013-04-18 15:33:27 +000073 SkASSERT(matrix_only_scale_translate(matrix));
reed@google.comc0e88e02012-10-17 21:11:56 +000074
reed@google.comee056a82013-04-18 15:33:27 +000075 if (matrix.getType() & SkMatrix::kScale_Mask) {
reedad7ae6c2015-06-04 14:12:25 -070076 SkRect dst;
77 SkRect src = SkRect::Make(pixmap.bounds());
reed@google.comf707adc2013-04-18 15:37:14 +000078
79 // Can't call mapRect(), since that will fix up inverted rectangles,
80 // e.g. when scale is negative, and we don't want to return true for
81 // those.
82 matrix.mapPoints(SkTCast<SkPoint*>(&dst),
83 SkTCast<const SkPoint*>(&src),
84 2);
reed@google.comee056a82013-04-18 15:33:27 +000085
86 // Now round all 4 edges to device space, and then compare the device
87 // width/height to the original. Note: we must map all 4 and subtract
88 // rather than map the "width" and compare, since we care about the
89 // phase (in pixel space) that any translate in the matrix might impart.
90 SkIRect idst;
91 dst.round(&idst);
reedad7ae6c2015-06-04 14:12:25 -070092 return idst.width() == pixmap.width() && idst.height() == pixmap.height();
reed@google.comc0e88e02012-10-17 21:11:56 +000093 }
94 // if we got here, we're either kTranslate_Mask or identity
95 return true;
96}
97
98static bool just_trans_general(const SkMatrix& matrix) {
reed@google.comee056a82013-04-18 15:33:27 +000099 SkASSERT(matrix_only_scale_translate(matrix));
reed@google.comc0e88e02012-10-17 21:11:56 +0000100
reed@google.comee056a82013-04-18 15:33:27 +0000101 if (matrix.getType() & SkMatrix::kScale_Mask) {
reed@google.comc0e88e02012-10-17 21:11:56 +0000102 const SkScalar tol = SK_Scalar1 / 32768;
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000103
reed@google.comc0e88e02012-10-17 21:11:56 +0000104 if (!SkScalarNearlyZero(matrix[SkMatrix::kMScaleX] - SK_Scalar1, tol)) {
105 return false;
106 }
107 if (!SkScalarNearlyZero(matrix[SkMatrix::kMScaleY] - SK_Scalar1, tol)) {
108 return false;
109 }
110 }
111 // if we got here, treat us as either kTranslate_Mask or identity
112 return true;
113}
114
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115static bool valid_for_filtering(unsigned dimension) {
116 // for filtering, width and height must fit in 14bits, since we use steal
117 // 2 bits from each to store our 4bit subpixel data
118 return (dimension & ~0x3FFF) == 0;
119}
120
reedf7094c42015-01-16 12:05:19 -0800121/*
122 * Analyze filter-quality and matrix, and decide how to implement that.
123 *
124 * In general, we cascade down the request level [ High ... None ]
125 * - for a given level, if we can fulfill it, fine, else
126 * - else we downgrade to the next lower level and try again.
127 * We can always fulfill requests for Low and None
128 * - sometimes we will "ignore" Low and give None, but this is likely a legacy perf hack
129 * and may be removed.
130 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131bool SkBitmapProcState::chooseProcs(const SkMatrix& inv, const SkPaint& paint) {
reedad7ae6c2015-06-04 14:12:25 -0700132 fPixmap.reset();
reed@google.comef0e3192013-09-09 13:42:39 +0000133 fInvMatrix = inv;
reed93a12152015-03-16 10:08:34 -0700134 fFilterLevel = paint.getFilterQuality();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135
reed2252eda2016-01-20 11:04:40 -0800136 const int origW = fProvider.info().width();
137 const int origH = fProvider.info().height();
138 bool allow_ignore_fractional_translate = true; // historical default
reed2252eda2016-01-20 11:04:40 -0800139 if (kMedium_SkFilterQuality == fFilterLevel) {
140 allow_ignore_fractional_translate = false;
141 }
reed2252eda2016-01-20 11:04:40 -0800142
reed64045422015-06-04 06:31:31 -0700143 SkDefaultBitmapController controller;
reed7a4d8472015-09-15 13:33:58 -0700144 fBMState = controller.requestBitmap(fProvider, inv, paint.getFilterQuality(),
reed64045422015-06-04 06:31:31 -0700145 fBMStateStorage.get(), fBMStateStorage.size());
reedad7ae6c2015-06-04 14:12:25 -0700146 // Note : we allow the controller to return an empty (zero-dimension) result. Should we?
halcanary96fcdcc2015-08-27 07:41:13 -0700147 if (nullptr == fBMState || fBMState->pixmap().info().isEmpty()) {
reed64045422015-06-04 06:31:31 -0700148 return false;
reedf7094c42015-01-16 12:05:19 -0800149 }
reedad7ae6c2015-06-04 14:12:25 -0700150 fPixmap = fBMState->pixmap();
reed64045422015-06-04 06:31:31 -0700151 fInvMatrix = fBMState->invMatrix();
152 fFilterLevel = fBMState->quality();
reedad7ae6c2015-06-04 14:12:25 -0700153 SkASSERT(fPixmap.addr());
reed64045422015-06-04 06:31:31 -0700154
reed@google.comef0e3192013-09-09 13:42:39 +0000155 bool trivialMatrix = (fInvMatrix.getType() & ~SkMatrix::kTranslate_Mask) == 0;
156 bool clampClamp = SkShader::kClamp_TileMode == fTileModeX &&
157 SkShader::kClamp_TileMode == fTileModeY;
skia.committer@gmail.com9a070f22013-09-10 07:01:44 +0000158
humper535e3b22014-10-27 10:32:06 -0700159 // Most of the scanline procs deal with "unit" texture coordinates, as this
160 // makes it easy to perform tiling modes (repeat = (x & 0xFFFF)). To generate
161 // those, we divide the matrix by its dimensions here.
162 //
163 // We don't do this if we're either trivial (can ignore the matrix) or clamping
164 // in both X and Y since clamping to width,height is just as easy as to 0xFFFF.
165
166 if (!(clampClamp || trivialMatrix)) {
reedad7ae6c2015-06-04 14:12:25 -0700167 fInvMatrix.postIDiv(fPixmap.width(), fPixmap.height());
reed@google.comef0e3192013-09-09 13:42:39 +0000168 }
169
humper@google.com9c96d4b2013-07-14 01:44:59 +0000170 // Now that all possible changes to the matrix have taken place, check
171 // to see if we're really close to a no-scale matrix. If so, explicitly
172 // set it to be so. Subsequent code may inspect this matrix to choose
173 // a faster path in this case.
174
175 // This code will only execute if the matrix has some scale component;
176 // if it's already pure translate then we won't do this inversion.
177
178 if (matrix_only_scale_translate(fInvMatrix)) {
reed@google.comee056a82013-04-18 15:33:27 +0000179 SkMatrix forward;
humper@google.com9c96d4b2013-07-14 01:44:59 +0000180 if (fInvMatrix.invert(&forward)) {
reed2252eda2016-01-20 11:04:40 -0800181 if ((clampClamp && allow_ignore_fractional_translate)
182 ? just_trans_clamp(forward, fPixmap)
reedf7094c42015-01-16 12:05:19 -0800183 : just_trans_general(forward)) {
reedf2e2ad02016-01-19 13:33:32 -0800184 fInvMatrix.setTranslate(-forward.getTranslateX(), -forward.getTranslateY());
reed@google.comce1f3cc2013-01-05 14:37:48 +0000185 }
reed@google.comc0e88e02012-10-17 21:11:56 +0000186 }
187 }
188
humper@google.com9c96d4b2013-07-14 01:44:59 +0000189 fInvProc = fInvMatrix.getMapXYProc();
190 fInvType = fInvMatrix.getType();
191 fInvSx = SkScalarToFixed(fInvMatrix.getScaleX());
192 fInvSxFractionalInt = SkScalarToFractionalInt(fInvMatrix.getScaleX());
193 fInvKy = SkScalarToFixed(fInvMatrix.getSkewY());
194 fInvKyFractionalInt = SkScalarToFractionalInt(fInvMatrix.getSkewY());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195
196 fAlphaScale = SkAlpha255To256(paint.getAlpha());
skia.committer@gmail.comfa1bd5f2013-07-13 07:00:56 +0000197
halcanary96fcdcc2015-08-27 07:41:13 -0700198 fShaderProc32 = nullptr;
199 fShaderProc16 = nullptr;
200 fSampleProc32 = nullptr;
mtklein96d68b72015-02-20 12:40:40 -0800201
humper@google.com9c96d4b2013-07-14 01:44:59 +0000202 // recompute the triviality of the matrix here because we may have
203 // changed it!
204
205 trivialMatrix = (fInvMatrix.getType() & ~SkMatrix::kTranslate_Mask) == 0;
206
reed2252eda2016-01-20 11:04:40 -0800207 // If our target pixmap is the same as the original, then we revert back to legacy behavior
208 // and allow the code to ignore fractional translate.
209 //
210 // The width/height check allows allow_ignore_fractional_translate to stay false if we
211 // previously set it that way (e.g. we started in kMedium).
212 //
213 if (fPixmap.width() == origW && fPixmap.height() == origH) {
214 allow_ignore_fractional_translate = true;
215 }
216
217 if (kLow_SkFilterQuality == fFilterLevel && allow_ignore_fractional_translate) {
humper@google.com9c96d4b2013-07-14 01:44:59 +0000218 // Only try bilerp if the matrix is "interesting" and
219 // the image has a suitable size.
220
221 if (fInvType <= SkMatrix::kTranslate_Mask ||
reedad7ae6c2015-06-04 14:12:25 -0700222 !valid_for_filtering(fPixmap.width() | fPixmap.height()))
reedf7094c42015-01-16 12:05:19 -0800223 {
reed93a12152015-03-16 10:08:34 -0700224 fFilterLevel = kNone_SkFilterQuality;
humper@google.com9c96d4b2013-07-14 01:44:59 +0000225 }
226 }
227
reed26feeba2015-01-14 12:28:22 -0800228 return this->chooseScanlineProcs(trivialMatrix, clampClamp, paint);
229}
humper@google.com9c96d4b2013-07-14 01:44:59 +0000230
reed26feeba2015-01-14 12:28:22 -0800231bool SkBitmapProcState::chooseScanlineProcs(bool trivialMatrix, bool clampClamp,
232 const SkPaint& paint) {
humper@google.com9c96d4b2013-07-14 01:44:59 +0000233 fMatrixProc = this->chooseMatrixProc(trivialMatrix);
halcanary96fcdcc2015-08-27 07:41:13 -0700234 // TODO(dominikg): SkASSERT(fMatrixProc) instead? chooseMatrixProc never returns nullptr.
235 if (nullptr == fMatrixProc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000236 return false;
237 }
238
239 ///////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000240
reedad7ae6c2015-06-04 14:12:25 -0700241 const SkAlphaType at = fPixmap.alphaType();
reed00adc752014-07-12 21:10:52 -0700242
humper@google.com9c96d4b2013-07-14 01:44:59 +0000243 // No need to do this if we're doing HQ sampling; if filter quality is
244 // still set to HQ by the time we get here, then we must have installed
mtklein@google.com0dc546c2013-08-26 16:21:35 +0000245 // the shader procs above and can skip all this.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246
reed93a12152015-03-16 10:08:34 -0700247 if (fFilterLevel < kHigh_SkFilterQuality) {
skia.committer@gmail.comfa1bd5f2013-07-13 07:00:56 +0000248
humper@google.com9c96d4b2013-07-14 01:44:59 +0000249 int index = 0;
250 if (fAlphaScale < 256) { // note: this distinction is not used for D16
251 index |= 1;
reed@android.comaa9152a2009-07-17 21:24:56 +0000252 }
humper@google.com9c96d4b2013-07-14 01:44:59 +0000253 if (fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) {
254 index |= 2;
255 }
reed93a12152015-03-16 10:08:34 -0700256 if (fFilterLevel > kNone_SkFilterQuality) {
humper@google.com9c96d4b2013-07-14 01:44:59 +0000257 index |= 4;
258 }
259 // bits 3,4,5 encoding the source bitmap format
reedad7ae6c2015-06-04 14:12:25 -0700260 switch (fPixmap.colorType()) {
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000261 case kN32_SkColorType:
reed00adc752014-07-12 21:10:52 -0700262 if (kPremul_SkAlphaType != at && kOpaque_SkAlphaType != at) {
263 return false;
264 }
humper@google.com9c96d4b2013-07-14 01:44:59 +0000265 index |= 0;
266 break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000267 case kRGB_565_SkColorType:
humper@google.com9c96d4b2013-07-14 01:44:59 +0000268 index |= 8;
269 break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000270 case kIndex_8_SkColorType:
reed00adc752014-07-12 21:10:52 -0700271 if (kPremul_SkAlphaType != at && kOpaque_SkAlphaType != at) {
272 return false;
273 }
humper@google.com9c96d4b2013-07-14 01:44:59 +0000274 index |= 16;
275 break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000276 case kARGB_4444_SkColorType:
reed00adc752014-07-12 21:10:52 -0700277 if (kPremul_SkAlphaType != at && kOpaque_SkAlphaType != at) {
278 return false;
279 }
humper@google.com9c96d4b2013-07-14 01:44:59 +0000280 index |= 24;
281 break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000282 case kAlpha_8_SkColorType:
humper@google.com9c96d4b2013-07-14 01:44:59 +0000283 index |= 32;
284 fPaintPMColor = SkPreMultiplyColor(paint.getColor());
285 break;
reed0c9b1a82015-03-17 17:44:06 -0700286 case kGray_8_SkColorType:
287 index |= 40;
288 fPaintPMColor = SkPreMultiplyColor(paint.getColor());
289 break;
humper@google.com9c96d4b2013-07-14 01:44:59 +0000290 default:
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000291 // TODO(dominikg): Should we ever get here? SkASSERT(false) instead?
humper@google.com9c96d4b2013-07-14 01:44:59 +0000292 return false;
293 }
reed@android.comc9a1d4b2009-08-03 15:05:55 +0000294
reed26feeba2015-01-14 12:28:22 -0800295#if !SK_ARM_NEON_IS_ALWAYS
humper@google.com9c96d4b2013-07-14 01:44:59 +0000296 static const SampleProc32 gSkBitmapProcStateSample32[] = {
297 S32_opaque_D32_nofilter_DXDY,
298 S32_alpha_D32_nofilter_DXDY,
299 S32_opaque_D32_nofilter_DX,
300 S32_alpha_D32_nofilter_DX,
301 S32_opaque_D32_filter_DXDY,
302 S32_alpha_D32_filter_DXDY,
303 S32_opaque_D32_filter_DX,
304 S32_alpha_D32_filter_DX,
skia.committer@gmail.com3e2345a2013-05-24 07:01:26 +0000305
humper@google.com9c96d4b2013-07-14 01:44:59 +0000306 S16_opaque_D32_nofilter_DXDY,
307 S16_alpha_D32_nofilter_DXDY,
308 S16_opaque_D32_nofilter_DX,
309 S16_alpha_D32_nofilter_DX,
310 S16_opaque_D32_filter_DXDY,
311 S16_alpha_D32_filter_DXDY,
312 S16_opaque_D32_filter_DX,
313 S16_alpha_D32_filter_DX,
314
315 SI8_opaque_D32_nofilter_DXDY,
316 SI8_alpha_D32_nofilter_DXDY,
317 SI8_opaque_D32_nofilter_DX,
318 SI8_alpha_D32_nofilter_DX,
319 SI8_opaque_D32_filter_DXDY,
320 SI8_alpha_D32_filter_DXDY,
321 SI8_opaque_D32_filter_DX,
322 SI8_alpha_D32_filter_DX,
323
324 S4444_opaque_D32_nofilter_DXDY,
325 S4444_alpha_D32_nofilter_DXDY,
326 S4444_opaque_D32_nofilter_DX,
327 S4444_alpha_D32_nofilter_DX,
328 S4444_opaque_D32_filter_DXDY,
329 S4444_alpha_D32_filter_DXDY,
330 S4444_opaque_D32_filter_DX,
331 S4444_alpha_D32_filter_DX,
reed0c9b1a82015-03-17 17:44:06 -0700332
humper@google.com9c96d4b2013-07-14 01:44:59 +0000333 // A8 treats alpha/opaque the same (equally efficient)
334 SA8_alpha_D32_nofilter_DXDY,
335 SA8_alpha_D32_nofilter_DXDY,
336 SA8_alpha_D32_nofilter_DX,
337 SA8_alpha_D32_nofilter_DX,
338 SA8_alpha_D32_filter_DXDY,
339 SA8_alpha_D32_filter_DXDY,
340 SA8_alpha_D32_filter_DX,
reed0c9b1a82015-03-17 17:44:06 -0700341 SA8_alpha_D32_filter_DX,
342
343 // todo: possibly specialize on opaqueness
344 SG8_alpha_D32_nofilter_DXDY,
345 SG8_alpha_D32_nofilter_DXDY,
346 SG8_alpha_D32_nofilter_DX,
347 SG8_alpha_D32_nofilter_DX,
348 SG8_alpha_D32_filter_DXDY,
349 SG8_alpha_D32_filter_DXDY,
350 SG8_alpha_D32_filter_DX,
351 SG8_alpha_D32_filter_DX
humper@google.com9c96d4b2013-07-14 01:44:59 +0000352 };
reed26feeba2015-01-14 12:28:22 -0800353#endif
humper@google.com9c96d4b2013-07-14 01:44:59 +0000354
355 fSampleProc32 = SK_ARM_NEON_WRAP(gSkBitmapProcStateSample32)[index];
356 index >>= 1; // shift away any opaque/alpha distinction
humper@google.com9c96d4b2013-07-14 01:44:59 +0000357
358 // our special-case shaderprocs
reed4e5a7582016-01-05 05:10:33 -0800359 if (SK_ARM_NEON_WRAP(SI8_opaque_D32_filter_DX) == fSampleProc32 && clampClamp) {
humper@google.com9c96d4b2013-07-14 01:44:59 +0000360 fShaderProc32 = SK_ARM_NEON_WRAP(Clamp_SI8_opaque_D32_filter_DX_shaderproc);
mtklein14e4d392014-10-23 14:35:01 -0700361 } else if (S32_opaque_D32_nofilter_DX == fSampleProc32 && clampClamp) {
362 fShaderProc32 = Clamp_S32_opaque_D32_nofilter_DX_shaderproc;
humper@google.com9c96d4b2013-07-14 01:44:59 +0000363 }
364
halcanary96fcdcc2015-08-27 07:41:13 -0700365 if (nullptr == fShaderProc32) {
humper@google.com9c96d4b2013-07-14 01:44:59 +0000366 fShaderProc32 = this->chooseShaderProc32();
367 }
humper@google.comb0889472013-07-09 21:37:14 +0000368 }
mtklein96d68b72015-02-20 12:40:40 -0800369
reed@android.comc9a1d4b2009-08-03 15:05:55 +0000370 // see if our platform has any accelerated overrides
371 this->platformProcs();
mtklein96d68b72015-02-20 12:40:40 -0800372
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 return true;
374}
375
herbc7a784c2015-12-18 09:52:15 -0800376static void Clamp_S32_D32_nofilter_trans_shaderproc(const void* sIn,
reed@google.com9a4c7462012-10-12 18:21:37 +0000377 int x, int y,
378 SkPMColor* SK_RESTRICT colors,
379 int count) {
herbc7a784c2015-12-18 09:52:15 -0800380 const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
reed@google.com9a4c7462012-10-12 18:21:37 +0000381 SkASSERT(((s.fInvType & ~SkMatrix::kTranslate_Mask)) == 0);
382 SkASSERT(s.fInvKy == 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700383 SkASSERT(count > 0 && colors != nullptr);
reed93a12152015-03-16 10:08:34 -0700384 SkASSERT(kNone_SkFilterQuality == s.fFilterLevel);
skia.committer@gmail.com20c301b2012-10-17 02:01:13 +0000385
reedad7ae6c2015-06-04 14:12:25 -0700386 const int maxX = s.fPixmap.width() - 1;
387 const int maxY = s.fPixmap.height() - 1;
reed@google.comf7698de2012-10-12 20:50:24 +0000388 int ix = s.fFilterOneX + x;
389 int iy = SkClampMax(s.fFilterOneY + y, maxY);
reedad7ae6c2015-06-04 14:12:25 -0700390 const SkPMColor* row = s.fPixmap.addr32(0, iy);
skia.committer@gmail.com20c301b2012-10-17 02:01:13 +0000391
reed@google.com9a4c7462012-10-12 18:21:37 +0000392 // clamp to the left
393 if (ix < 0) {
394 int n = SkMin32(-ix, count);
395 sk_memset32(colors, row[0], n);
396 count -= n;
397 if (0 == count) {
398 return;
399 }
400 colors += n;
401 SkASSERT(-ix == n);
402 ix = 0;
403 }
404 // copy the middle
405 if (ix <= maxX) {
406 int n = SkMin32(maxX - ix + 1, count);
407 memcpy(colors, row + ix, n * sizeof(SkPMColor));
408 count -= n;
409 if (0 == count) {
410 return;
411 }
412 colors += n;
413 }
414 SkASSERT(count > 0);
415 // clamp to the right
416 sk_memset32(colors, row[maxX], count);
417}
418
reed@google.coma8d99302012-10-16 20:23:25 +0000419static inline int sk_int_mod(int x, int n) {
420 SkASSERT(n > 0);
421 if ((unsigned)x >= (unsigned)n) {
422 if (x < 0) {
423 x = n + ~(~x % n);
424 } else {
425 x = x % n;
426 }
427 }
428 return x;
429}
430
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000431static inline int sk_int_mirror(int x, int n) {
432 x = sk_int_mod(x, 2 * n);
433 if (x >= n) {
434 x = n + ~(x - n);
435 }
436 return x;
437}
438
herbc7a784c2015-12-18 09:52:15 -0800439static void Repeat_S32_D32_nofilter_trans_shaderproc(const void* sIn,
reed@google.coma8d99302012-10-16 20:23:25 +0000440 int x, int y,
441 SkPMColor* SK_RESTRICT colors,
442 int count) {
herbc7a784c2015-12-18 09:52:15 -0800443 const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
reed@google.coma8d99302012-10-16 20:23:25 +0000444 SkASSERT(((s.fInvType & ~SkMatrix::kTranslate_Mask)) == 0);
445 SkASSERT(s.fInvKy == 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700446 SkASSERT(count > 0 && colors != nullptr);
reed93a12152015-03-16 10:08:34 -0700447 SkASSERT(kNone_SkFilterQuality == s.fFilterLevel);
skia.committer@gmail.com20c301b2012-10-17 02:01:13 +0000448
reedad7ae6c2015-06-04 14:12:25 -0700449 const int stopX = s.fPixmap.width();
450 const int stopY = s.fPixmap.height();
reed@google.coma8d99302012-10-16 20:23:25 +0000451 int ix = s.fFilterOneX + x;
452 int iy = sk_int_mod(s.fFilterOneY + y, stopY);
reedad7ae6c2015-06-04 14:12:25 -0700453 const SkPMColor* row = s.fPixmap.addr32(0, iy);
reed@google.coma8d99302012-10-16 20:23:25 +0000454
455 ix = sk_int_mod(ix, stopX);
456 for (;;) {
457 int n = SkMin32(stopX - ix, count);
458 memcpy(colors, row + ix, n * sizeof(SkPMColor));
459 count -= n;
460 if (0 == count) {
461 return;
462 }
463 colors += n;
464 ix = 0;
465 }
466}
467
herbc7a784c2015-12-18 09:52:15 -0800468static void S32_D32_constX_shaderproc(const void* sIn,
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000469 int x, int y,
470 SkPMColor* SK_RESTRICT colors,
471 int count) {
herbc7a784c2015-12-18 09:52:15 -0800472 const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000473 SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) == 0);
474 SkASSERT(s.fInvKy == 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700475 SkASSERT(count > 0 && colors != nullptr);
reedad7ae6c2015-06-04 14:12:25 -0700476 SkASSERT(1 == s.fPixmap.width());
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000477
scroggo@google.comad511322013-02-22 15:50:37 +0000478 int iY0;
479 int iY1 SK_INIT_TO_AVOID_WARNING;
480 int iSubY SK_INIT_TO_AVOID_WARNING;
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000481
reed93a12152015-03-16 10:08:34 -0700482 if (kNone_SkFilterQuality != s.fFilterLevel) {
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000483 SkBitmapProcState::MatrixProc mproc = s.getMatrixProc();
484 uint32_t xy[2];
485
486 mproc(s, xy, 1, x, y);
487
488 iY0 = xy[0] >> 18;
489 iY1 = xy[0] & 0x3FFF;
490 iSubY = (xy[0] >> 14) & 0xF;
491 } else {
492 int yTemp;
493
494 if (s.fInvType > SkMatrix::kTranslate_Mask) {
fmalitaeb543072016-02-02 10:17:24 -0800495 const SkBitmapProcStateAutoMapper mapper(s, x, y);
496
robertphillips@google.com1e305232013-01-22 20:29:16 +0000497 // When the matrix has a scale component the setup code in
skia.committer@gmail.com98ded842013-01-23 07:06:17 +0000498 // chooseProcs multiples the inverse matrix by the inverse of the
robertphillips@google.com1e305232013-01-22 20:29:16 +0000499 // bitmap's width and height. Since this method is going to do
500 // its own tiling and sampling we need to undo that here.
robertphillips@google.comd5077752013-01-23 00:36:02 +0000501 if (SkShader::kClamp_TileMode != s.fTileModeX ||
502 SkShader::kClamp_TileMode != s.fTileModeY) {
fmalitabe5cfa92016-02-03 10:21:33 -0800503 yTemp = SkFractionalIntToInt(mapper.fractionalIntY() * s.fPixmap.height());
robertphillips@google.comd5077752013-01-23 00:36:02 +0000504 } else {
fmalitabe5cfa92016-02-03 10:21:33 -0800505 yTemp = mapper.intY();
robertphillips@google.comd5077752013-01-23 00:36:02 +0000506 }
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000507 } else {
508 yTemp = s.fFilterOneY + y;
509 }
510
reedad7ae6c2015-06-04 14:12:25 -0700511 const int stopY = s.fPixmap.height();
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000512 switch (s.fTileModeY) {
513 case SkShader::kClamp_TileMode:
514 iY0 = SkClampMax(yTemp, stopY-1);
515 break;
516 case SkShader::kRepeat_TileMode:
517 iY0 = sk_int_mod(yTemp, stopY);
518 break;
519 case SkShader::kMirror_TileMode:
520 default:
521 iY0 = sk_int_mirror(yTemp, stopY);
522 break;
523 }
524
fmalitad2a42712016-02-01 04:48:39 -0800525#ifdef SK_DEBUG
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000526 {
fmalitaeb543072016-02-02 10:17:24 -0800527 const SkBitmapProcStateAutoMapper mapper(s, x, y);
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000528 int iY2;
fmalitaeb543072016-02-02 10:17:24 -0800529
530 if (s.fInvType > SkMatrix::kTranslate_Mask &&
531 (SkShader::kClamp_TileMode != s.fTileModeX ||
532 SkShader::kClamp_TileMode != s.fTileModeY)) {
fmalitabe5cfa92016-02-03 10:21:33 -0800533 iY2 = SkFractionalIntToInt(mapper.fractionalIntY() * s.fPixmap.height());
fmalitad2a42712016-02-01 04:48:39 -0800534 } else {
fmalitabe5cfa92016-02-03 10:21:33 -0800535 iY2 = mapper.intY();
fmalitad2a42712016-02-01 04:48:39 -0800536 }
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000537
538 switch (s.fTileModeY) {
539 case SkShader::kClamp_TileMode:
fmalitad2a42712016-02-01 04:48:39 -0800540 iY2 = SkClampMax(iY2, stopY-1);
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000541 break;
542 case SkShader::kRepeat_TileMode:
fmalitad2a42712016-02-01 04:48:39 -0800543 iY2 = sk_int_mod(iY2, stopY);
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000544 break;
545 case SkShader::kMirror_TileMode:
546 default:
fmalitad2a42712016-02-01 04:48:39 -0800547 iY2 = sk_int_mirror(iY2, stopY);
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000548 break;
549 }
550
551 SkASSERT(iY0 == iY2);
552 }
553#endif
554 }
555
reedad7ae6c2015-06-04 14:12:25 -0700556 const SkPMColor* row0 = s.fPixmap.addr32(0, iY0);
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000557 SkPMColor color;
558
reed93a12152015-03-16 10:08:34 -0700559 if (kNone_SkFilterQuality != s.fFilterLevel) {
reedad7ae6c2015-06-04 14:12:25 -0700560 const SkPMColor* row1 = s.fPixmap.addr32(0, iY1);
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000561
562 if (s.fAlphaScale < 256) {
563 Filter_32_alpha(iSubY, *row0, *row1, &color, s.fAlphaScale);
564 } else {
565 Filter_32_opaque(iSubY, *row0, *row1, &color);
566 }
567 } else {
568 if (s.fAlphaScale < 256) {
569 color = SkAlphaMulQ(*row0, s.fAlphaScale);
570 } else {
571 color = *row0;
572 }
573 }
574
575 sk_memset32(colors, color, count);
576}
577
herbc7a784c2015-12-18 09:52:15 -0800578static void DoNothing_shaderproc(const void*, int x, int y,
reed@google.com6bb92bc2012-11-20 19:45:16 +0000579 SkPMColor* SK_RESTRICT colors, int count) {
580 // if we get called, the matrix is too tricky, so we just draw nothing
581 sk_memset32(colors, 0, count);
582}
583
584bool SkBitmapProcState::setupForTranslate() {
reed@google.coma8d99302012-10-16 20:23:25 +0000585 SkPoint pt;
fmalita8a8eb522016-01-30 18:56:34 -0800586 const SkBitmapProcStateAutoMapper mapper(*this, 0, 0, &pt);
fmalita653c12d2016-01-30 10:06:46 -0800587
reed@google.com6bb92bc2012-11-20 19:45:16 +0000588 /*
589 * if the translate is larger than our ints, we can get random results, or
590 * worse, we might get 0x80000000, which wreaks havoc on us, since we can't
591 * negate it.
592 */
fmalita8a8eb522016-01-30 18:56:34 -0800593 const SkScalar too_big = SkIntToScalar(1 << 30);
594 if (SkScalarAbs(pt.fX) > too_big || SkScalarAbs(pt.fY) > too_big) {
reed@google.com6bb92bc2012-11-20 19:45:16 +0000595 return false;
596 }
597
reed@google.coma8d99302012-10-16 20:23:25 +0000598 // Since we know we're not filtered, we re-purpose these fields allow
599 // us to go from device -> src coordinates w/ just an integer add,
600 // rather than running through the inverse-matrix
fmalitabe5cfa92016-02-03 10:21:33 -0800601 fFilterOneX = mapper.intX();
602 fFilterOneY = mapper.intY();
fmalita653c12d2016-01-30 10:06:46 -0800603
reed@google.com6bb92bc2012-11-20 19:45:16 +0000604 return true;
reed@google.coma8d99302012-10-16 20:23:25 +0000605}
606
reed@google.com9a4c7462012-10-12 18:21:37 +0000607SkBitmapProcState::ShaderProc32 SkBitmapProcState::chooseShaderProc32() {
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000608
reedad7ae6c2015-06-04 14:12:25 -0700609 if (kN32_SkColorType != fPixmap.colorType()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700610 return nullptr;
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000611 }
612
613 static const unsigned kMask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask;
614
reedad7ae6c2015-06-04 14:12:25 -0700615 if (1 == fPixmap.width() && 0 == (fInvType & ~kMask)) {
reed93a12152015-03-16 10:08:34 -0700616 if (kNone_SkFilterQuality == fFilterLevel &&
humper@google.com9c96d4b2013-07-14 01:44:59 +0000617 fInvType <= SkMatrix::kTranslate_Mask &&
618 !this->setupForTranslate()) {
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000619 return DoNothing_shaderproc;
620 }
621 return S32_D32_constX_shaderproc;
622 }
623
reed@google.com9a4c7462012-10-12 18:21:37 +0000624 if (fAlphaScale < 256) {
halcanary96fcdcc2015-08-27 07:41:13 -0700625 return nullptr;
reed@google.com9a4c7462012-10-12 18:21:37 +0000626 }
627 if (fInvType > SkMatrix::kTranslate_Mask) {
halcanary96fcdcc2015-08-27 07:41:13 -0700628 return nullptr;
reed@google.com9a4c7462012-10-12 18:21:37 +0000629 }
reed93a12152015-03-16 10:08:34 -0700630 if (kNone_SkFilterQuality != fFilterLevel) {
halcanary96fcdcc2015-08-27 07:41:13 -0700631 return nullptr;
reed@google.com9a4c7462012-10-12 18:21:37 +0000632 }
reed@google.com9a4c7462012-10-12 18:21:37 +0000633
reed@google.coma8d99302012-10-16 20:23:25 +0000634 SkShader::TileMode tx = (SkShader::TileMode)fTileModeX;
635 SkShader::TileMode ty = (SkShader::TileMode)fTileModeY;
636
637 if (SkShader::kClamp_TileMode == tx && SkShader::kClamp_TileMode == ty) {
reed@google.com6bb92bc2012-11-20 19:45:16 +0000638 if (this->setupForTranslate()) {
639 return Clamp_S32_D32_nofilter_trans_shaderproc;
640 }
641 return DoNothing_shaderproc;
reed@google.com9a4c7462012-10-12 18:21:37 +0000642 }
reed@google.coma8d99302012-10-16 20:23:25 +0000643 if (SkShader::kRepeat_TileMode == tx && SkShader::kRepeat_TileMode == ty) {
reed@google.com6bb92bc2012-11-20 19:45:16 +0000644 if (this->setupForTranslate()) {
645 return Repeat_S32_D32_nofilter_trans_shaderproc;
646 }
647 return DoNothing_shaderproc;
reed@google.coma8d99302012-10-16 20:23:25 +0000648 }
halcanary96fcdcc2015-08-27 07:41:13 -0700649 return nullptr;
reed@google.com9a4c7462012-10-12 18:21:37 +0000650}
651
reed@android.com4c128c42009-08-14 13:54:37 +0000652///////////////////////////////////////////////////////////////////////////////
reed@google.com9fe287b2012-03-27 15:54:28 +0000653
654#ifdef SK_DEBUG
655
656static void check_scale_nofilter(uint32_t bitmapXY[], int count,
657 unsigned mx, unsigned my) {
658 unsigned y = *bitmapXY++;
659 SkASSERT(y < my);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000660
reed@google.com9fe287b2012-03-27 15:54:28 +0000661 const uint16_t* xptr = reinterpret_cast<const uint16_t*>(bitmapXY);
662 for (int i = 0; i < count; ++i) {
663 SkASSERT(xptr[i] < mx);
664 }
665}
666
667static void check_scale_filter(uint32_t bitmapXY[], int count,
668 unsigned mx, unsigned my) {
669 uint32_t YY = *bitmapXY++;
670 unsigned y0 = YY >> 18;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000671 unsigned y1 = YY & 0x3FFF;
reed@google.com9fe287b2012-03-27 15:54:28 +0000672 SkASSERT(y0 < my);
673 SkASSERT(y1 < my);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000674
reed@google.com9fe287b2012-03-27 15:54:28 +0000675 for (int i = 0; i < count; ++i) {
676 uint32_t XX = bitmapXY[i];
677 unsigned x0 = XX >> 18;
678 unsigned x1 = XX & 0x3FFF;
679 SkASSERT(x0 < mx);
680 SkASSERT(x1 < mx);
681 }
682}
683
684static void check_affine_nofilter(uint32_t bitmapXY[], int count,
685 unsigned mx, unsigned my) {
686 for (int i = 0; i < count; ++i) {
687 uint32_t XY = bitmapXY[i];
688 unsigned x = XY & 0xFFFF;
689 unsigned y = XY >> 16;
690 SkASSERT(x < mx);
691 SkASSERT(y < my);
692 }
693}
694
695static void check_affine_filter(uint32_t bitmapXY[], int count,
696 unsigned mx, unsigned my) {
697 for (int i = 0; i < count; ++i) {
698 uint32_t YY = *bitmapXY++;
699 unsigned y0 = YY >> 18;
700 unsigned y1 = YY & 0x3FFF;
701 SkASSERT(y0 < my);
702 SkASSERT(y1 < my);
703
704 uint32_t XX = *bitmapXY++;
705 unsigned x0 = XX >> 18;
706 unsigned x1 = XX & 0x3FFF;
707 SkASSERT(x0 < mx);
708 SkASSERT(x1 < mx);
709 }
710}
711
712void SkBitmapProcState::DebugMatrixProc(const SkBitmapProcState& state,
713 uint32_t bitmapXY[], int count,
714 int x, int y) {
715 SkASSERT(bitmapXY);
716 SkASSERT(count > 0);
717
718 state.fMatrixProc(state, bitmapXY, count, x, y);
719
720 void (*proc)(uint32_t bitmapXY[], int count, unsigned mx, unsigned my);
721
722 // There are four formats possible:
723 // scale -vs- affine
724 // filter -vs- nofilter
725 if (state.fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) {
reed93a12152015-03-16 10:08:34 -0700726 proc = state.fFilterLevel != kNone_SkFilterQuality ? check_scale_filter : check_scale_nofilter;
reed@google.com9fe287b2012-03-27 15:54:28 +0000727 } else {
reed93a12152015-03-16 10:08:34 -0700728 proc = state.fFilterLevel != kNone_SkFilterQuality ? check_affine_filter : check_affine_nofilter;
reed@google.com9fe287b2012-03-27 15:54:28 +0000729 }
reedad7ae6c2015-06-04 14:12:25 -0700730 proc(bitmapXY, count, state.fPixmap.width(), state.fPixmap.height());
reed@google.com9fe287b2012-03-27 15:54:28 +0000731}
732
733SkBitmapProcState::MatrixProc SkBitmapProcState::getMatrixProc() const {
734 return DebugMatrixProc;
735}
736
737#endif
738
739///////////////////////////////////////////////////////////////////////////////
reed@android.com4c128c42009-08-14 13:54:37 +0000740/*
741 The storage requirements for the different matrix procs are as follows,
742 where each X or Y is 2 bytes, and N is the number of pixels/elements:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000743
reed@android.com4c128c42009-08-14 13:54:37 +0000744 scale/translate nofilter Y(4bytes) + N * X
745 affine/perspective nofilter N * (X Y)
746 scale/translate filter Y Y + N * (X X)
747 affine/perspective filter N * (Y Y X X)
748 */
749int SkBitmapProcState::maxCountForBufferSize(size_t bufferSize) const {
750 int32_t size = static_cast<int32_t>(bufferSize);
reed@android.com4c128c42009-08-14 13:54:37 +0000751
752 size &= ~3; // only care about 4-byte aligned chunks
753 if (fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) {
754 size -= 4; // the shared Y (or YY) coordinate
755 if (size < 0) {
756 size = 0;
757 }
reed@android.com258cb222010-04-14 13:36:33 +0000758 size >>= 1;
reed@android.com4c128c42009-08-14 13:54:37 +0000759 } else {
reed@android.com258cb222010-04-14 13:36:33 +0000760 size >>= 2;
reed@android.com4c128c42009-08-14 13:54:37 +0000761 }
762
reed93a12152015-03-16 10:08:34 -0700763 if (fFilterLevel != kNone_SkFilterQuality) {
reed@android.com258cb222010-04-14 13:36:33 +0000764 size >>= 1;
765 }
766
767 return size;
reed@android.com4c128c42009-08-14 13:54:37 +0000768}
mtklein14e4d392014-10-23 14:35:01 -0700769
770///////////////////////
771
herbc7a784c2015-12-18 09:52:15 -0800772void Clamp_S32_opaque_D32_nofilter_DX_shaderproc(const void* sIn, int x, int y,
mtklein14e4d392014-10-23 14:35:01 -0700773 SkPMColor* SK_RESTRICT dst, int count) {
herbc7a784c2015-12-18 09:52:15 -0800774 const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
mtklein14e4d392014-10-23 14:35:01 -0700775 SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask |
776 SkMatrix::kScale_Mask)) == 0);
777
reedad7ae6c2015-06-04 14:12:25 -0700778 const unsigned maxX = s.fPixmap.width() - 1;
mtklein14e4d392014-10-23 14:35:01 -0700779 SkFractionalInt fx;
780 int dstY;
781 {
fmalitab3a83582016-01-04 10:28:11 -0800782 const SkBitmapProcStateAutoMapper mapper(s, x, y);
reedad7ae6c2015-06-04 14:12:25 -0700783 const unsigned maxY = s.fPixmap.height() - 1;
fmalitabe5cfa92016-02-03 10:21:33 -0800784 dstY = SkClampMax(mapper.intY(), maxY);
785 fx = mapper.fractionalIntX();
mtklein14e4d392014-10-23 14:35:01 -0700786 }
787
reedad7ae6c2015-06-04 14:12:25 -0700788 const SkPMColor* SK_RESTRICT src = s.fPixmap.addr32(0, dstY);
mtklein14e4d392014-10-23 14:35:01 -0700789 const SkFractionalInt dx = s.fInvSxFractionalInt;
790
791 // Check if we're safely inside [0...maxX] so no need to clamp each computed index.
792 //
793 if ((uint64_t)SkFractionalIntToInt(fx) <= maxX &&
794 (uint64_t)SkFractionalIntToInt(fx + dx * (count - 1)) <= maxX)
795 {
796 int count4 = count >> 2;
797 for (int i = 0; i < count4; ++i) {
798 SkPMColor src0 = src[SkFractionalIntToInt(fx)]; fx += dx;
799 SkPMColor src1 = src[SkFractionalIntToInt(fx)]; fx += dx;
800 SkPMColor src2 = src[SkFractionalIntToInt(fx)]; fx += dx;
801 SkPMColor src3 = src[SkFractionalIntToInt(fx)]; fx += dx;
802 dst[0] = src0;
803 dst[1] = src1;
804 dst[2] = src2;
805 dst[3] = src3;
806 dst += 4;
807 }
808 for (int i = (count4 << 2); i < count; ++i) {
809 unsigned index = SkFractionalIntToInt(fx);
810 SkASSERT(index <= maxX);
811 *dst++ = src[index];
812 fx += dx;
813 }
814 } else {
815 for (int i = 0; i < count; ++i) {
816 dst[i] = src[SkClampMax(SkFractionalIntToInt(fx), maxX)];
817 fx += dx;
818 }
819 }
820}
fmalita3e6be162015-12-18 09:36:18 -0800821