Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: src/core/SkLinearBitmapPipeline_sample.h

Issue 2134893002: Redo Tiling (Closed) Base URL: https://skia.googlesource.com/skia.git@reduce-LBP-sample
Patch Set: Document bug. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkLinearBitmapPipeline_core.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkLinearBitmapPipeline_sampler_DEFINED 8 #ifndef SkLinearBitmapPipeline_sampler_DEFINED
9 #define SkLinearBitmapPipeline_sampler_DEFINED 9 #define SkLinearBitmapPipeline_sampler_DEFINED
10 10
(...skipping 22 matching lines...) Expand all
33 // 33 //
34 // 34 //
35 // Given a pixelxy each is multiplied by a different factor derived from the fra ctional part of x 35 // Given a pixelxy each is multiplied by a different factor derived from the fra ctional part of x
36 // and y: 36 // and y:
37 // * px00 -> (1 - x)(1 - y) = 1 - x - y + xy 37 // * px00 -> (1 - x)(1 - y) = 1 - x - y + xy
38 // * px10 -> x(1 - y) = x - xy 38 // * px10 -> x(1 - y) = x - xy
39 // * px01 -> (1 - x)y = y - xy 39 // * px01 -> (1 - x)y = y - xy
40 // * px11 -> xy 40 // * px11 -> xy
41 // So x * y is calculated first and then used to calculate all the other factors . 41 // So x * y is calculated first and then used to calculate all the other factors .
42 static Sk4s SK_VECTORCALL bilerp4(Sk4s xs, Sk4s ys, Sk4f px00, Sk4f px10, 42 static Sk4s SK_VECTORCALL bilerp4(Sk4s xs, Sk4s ys, Sk4f px00, Sk4f px10,
43 Sk4f px01, Sk4f px11) { 43 Sk4f px01, Sk4f px11) {
44 // Calculate fractional xs and ys. 44 // Calculate fractional xs and ys.
45 Sk4s fxs = xs - xs.floor(); 45 Sk4s fxs = xs - xs.floor();
46 Sk4s fys = ys - ys.floor(); 46 Sk4s fys = ys - ys.floor();
47 Sk4s fxys{fxs * fys}; 47 Sk4s fxys{fxs * fys};
48 Sk4f sum = px11 * fxys; 48 Sk4f sum = px11 * fxys;
49 sum = sum + px01 * (fys - fxys); 49 sum = sum + px01 * (fys - fxys);
50 sum = sum + px10 * (fxs - fxys); 50 sum = sum + px10 * (fxs - fxys);
51 sum = sum + px00 * (Sk4f{1.0f} - fxs - fys + fxys); 51 sum = sum + px00 * (Sk4f{1.0f} - fxs - fys + fxys);
52 return sum; 52 return sum;
53 } 53 }
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 Sk4f toSk4f(Element pixel) const { 127 Sk4f toSk4f(Element pixel) const {
128 return swizzle_rb( 128 return swizzle_rb(
129 gammaType == kSRGB_SkGammaType ? Sk4f_fromS32(pixel) : Sk4f_f romL32(pixel)); 129 gammaType == kSRGB_SkGammaType ? Sk4f_fromS32(pixel) : Sk4f_f romL32(pixel));
130 } 130 }
131 }; 131 };
132 132
133 template <SkGammaType gammaType> 133 template <SkGammaType gammaType>
134 class PixelConverter<kIndex_8_SkColorType, gammaType> { 134 class PixelConverter<kIndex_8_SkColorType, gammaType> {
135 public: 135 public:
136 using Element = uint8_t; 136 using Element = uint8_t;
137 PixelConverter(const SkPixmap& srcPixmap) { 137 PixelConverter(const SkPixmap& srcPixmap)
138 : fColorTableSize(srcPixmap.ctable()->count()){
138 SkColorTable* skColorTable = srcPixmap.ctable(); 139 SkColorTable* skColorTable = srcPixmap.ctable();
139 SkASSERT(skColorTable != nullptr); 140 SkASSERT(skColorTable != nullptr);
140 141
141 fColorTable = (Sk4f*)SkAlign16((intptr_t)fColorTableStorage.get()); 142 fColorTable = (Sk4f*)SkAlign16((intptr_t)fColorTableStorage.get());
142 for (int i = 0; i < skColorTable->count(); i++) { 143 for (int i = 0; i < fColorTableSize; i++) {
143 fColorTable[i] = pmcolor_to_rgba<gammaType>((*skColorTable)[i]); 144 fColorTable[i] = pmcolor_to_rgba<gammaType>((*skColorTable)[i]);
144 } 145 }
145 } 146 }
146 147
147 PixelConverter(const PixelConverter& strategy) { 148 PixelConverter(const PixelConverter& strategy)
149 : fColorTableSize{strategy.fColorTableSize}{
148 fColorTable = (Sk4f*)SkAlign16((intptr_t)fColorTableStorage.get()); 150 fColorTable = (Sk4f*)SkAlign16((intptr_t)fColorTableStorage.get());
149 // TODO: figure out the count. 151 for (int i = 0; i < fColorTableSize; i++) {
150 for (int i = 0; i < 256; i++) {
151 fColorTable[i] = strategy.fColorTable[i]; 152 fColorTable[i] = strategy.fColorTable[i];
152 } 153 }
153 } 154 }
154 155
155 Sk4f toSk4f(Element index) const { 156 Sk4f toSk4f(Element index) const {
156 return fColorTable[index]; 157 return fColorTable[index];
157 } 158 }
158 159
159 private: 160 private:
160 static const size_t kColorTableSize = sizeof(Sk4f[256]) + 12; 161 static const size_t kColorTableSize = sizeof(Sk4f[256]) + 12;
161 162 const int fColorTableSize;
162 SkAutoMalloc fColorTableStorage{kColorTableSize}; 163 SkAutoMalloc fColorTableStorage{kColorTableSize};
163 Sk4f* fColorTable; 164 Sk4f* fColorTable;
164 }; 165 };
165 166
166 template <SkGammaType gammaType> 167 template <SkGammaType gammaType>
167 class PixelConverter<kGray_8_SkColorType, gammaType> { 168 class PixelConverter<kGray_8_SkColorType, gammaType> {
168 public: 169 public:
169 using Element = uint8_t; 170 using Element = uint8_t;
170 PixelConverter(const SkPixmap& srcPixmap) { } 171 PixelConverter(const SkPixmap& srcPixmap) { }
171 172
172 Sk4f toSk4f(Element pixel) const { 173 Sk4f toSk4f(Element pixel) const {
173 float gray = (gammaType == kSRGB_SkGammaType) 174 float gray = (gammaType == kSRGB_SkGammaType)
(...skipping 13 matching lines...) Expand all
187 return SkHalfToFloat_finite(pixel); 188 return SkHalfToFloat_finite(pixel);
188 } 189 }
189 }; 190 };
190 191
191 class PixelAccessorShim { 192 class PixelAccessorShim {
192 public: 193 public:
193 explicit PixelAccessorShim(SkLinearBitmapPipeline::PixelAccessorInterface* a ccessor) 194 explicit PixelAccessorShim(SkLinearBitmapPipeline::PixelAccessorInterface* a ccessor)
194 : fPixelAccessor(accessor) { } 195 : fPixelAccessor(accessor) { }
195 196
196 void SK_VECTORCALL getFewPixels( 197 void SK_VECTORCALL getFewPixels(
197 int n, Sk4s xs, Sk4s ys, Sk4f* px0, Sk4f* px1, Sk4f* px2) const { 198 int n, Sk4i xs, Sk4i ys, Sk4f* px0, Sk4f* px1, Sk4f* px2) const {
198 fPixelAccessor->getFewPixels(n, xs, ys, px0, px1, px2); 199 fPixelAccessor->getFewPixels(n, xs, ys, px0, px1, px2);
199 } 200 }
200 201
201 void SK_VECTORCALL get4Pixels( 202 void SK_VECTORCALL get4Pixels(
202 Sk4s xs, Sk4s ys, Sk4f* px0, Sk4f* px1, Sk4f* px2, Sk4f* px3) const { 203 Sk4i xs, Sk4i ys, Sk4f* px0, Sk4f* px1, Sk4f* px2, Sk4f* px3) const {
203 fPixelAccessor->get4Pixels(xs, ys, px0, px1, px2, px3); 204 fPixelAccessor->get4Pixels(xs, ys, px0, px1, px2, px3);
204 } 205 }
205 206
206 void get4Pixels( 207 void get4Pixels(
207 const void* src, int index, Sk4f* px0, Sk4f* px1, Sk4f* px2, Sk4f* px3) const { 208 const void* src, int index, Sk4f* px0, Sk4f* px1, Sk4f* px2, Sk4f* px3) const {
208 fPixelAccessor->get4Pixels(src, index, px0, px1, px2, px3); 209 fPixelAccessor->get4Pixels(src, index, px0, px1, px2, px3);
209 }; 210 };
210 211
211 Sk4f getPixelFromRow(const void* row, int index) const { 212 Sk4f getPixelFromRow(const void* row, int index) const {
212 return fPixelAccessor->getPixelFromRow(row, index); 213 return fPixelAccessor->getPixelFromRow(row, index);
(...skipping 17 matching lines...) Expand all
230 class PixelAccessor final : public SkLinearBitmapPipeline::PixelAccessorInterfac e { 231 class PixelAccessor final : public SkLinearBitmapPipeline::PixelAccessorInterfac e {
231 using Element = typename PixelConverter<colorType, gammaType>::Element; 232 using Element = typename PixelConverter<colorType, gammaType>::Element;
232 public: 233 public:
233 template <typename... Args> 234 template <typename... Args>
234 PixelAccessor(const SkPixmap& srcPixmap, Args&&... args) 235 PixelAccessor(const SkPixmap& srcPixmap, Args&&... args)
235 : fSrc{static_cast<const Element*>(srcPixmap.addr())} 236 : fSrc{static_cast<const Element*>(srcPixmap.addr())}
236 , fWidth{srcPixmap.rowBytesAsPixels()} 237 , fWidth{srcPixmap.rowBytesAsPixels()}
237 , fConverter{srcPixmap, std::move<Args>(args)...} { } 238 , fConverter{srcPixmap, std::move<Args>(args)...} { }
238 239
239 void SK_VECTORCALL getFewPixels ( 240 void SK_VECTORCALL getFewPixels (
240 int n, Sk4s xs, Sk4s ys, Sk4f* px0, Sk4f* px1, Sk4f* px2) const override { 241 int n, Sk4i xs, Sk4i ys, Sk4f* px0, Sk4f* px1, Sk4f* px2) const override {
241 Sk4i XIs = SkNx_cast<int, SkScalar>(xs); 242 Sk4i bufferLoc = ys * fWidth + xs;
242 Sk4i YIs = SkNx_cast<int, SkScalar>(ys);
243 Sk4i bufferLoc = YIs * fWidth + XIs;
244 switch (n) { 243 switch (n) {
245 case 3: 244 case 3:
246 *px2 = this->getPixelAt(bufferLoc[2]); 245 *px2 = this->getPixelAt(bufferLoc[2]);
247 case 2: 246 case 2:
248 *px1 = this->getPixelAt(bufferLoc[1]); 247 *px1 = this->getPixelAt(bufferLoc[1]);
249 case 1: 248 case 1:
250 *px0 = this->getPixelAt(bufferLoc[0]); 249 *px0 = this->getPixelAt(bufferLoc[0]);
251 default: 250 default:
252 break; 251 break;
253 } 252 }
254 } 253 }
255 254
256 void SK_VECTORCALL get4Pixels( 255 void SK_VECTORCALL get4Pixels(
257 Sk4s xs, Sk4s ys, Sk4f* px0, Sk4f* px1, Sk4f* px2, Sk4f* px3) const over ride { 256 Sk4i xs, Sk4i ys, Sk4f* px0, Sk4f* px1, Sk4f* px2, Sk4f* px3) const over ride {
258 Sk4i XIs = SkNx_cast<int, SkScalar>(xs); 257 Sk4i bufferLoc = ys * fWidth + xs;
259 Sk4i YIs = SkNx_cast<int, SkScalar>(ys);
260 Sk4i bufferLoc = YIs * fWidth + XIs;
261 *px0 = this->getPixelAt(bufferLoc[0]); 258 *px0 = this->getPixelAt(bufferLoc[0]);
262 *px1 = this->getPixelAt(bufferLoc[1]); 259 *px1 = this->getPixelAt(bufferLoc[1]);
263 *px2 = this->getPixelAt(bufferLoc[2]); 260 *px2 = this->getPixelAt(bufferLoc[2]);
264 *px3 = this->getPixelAt(bufferLoc[3]); 261 *px3 = this->getPixelAt(bufferLoc[3]);
265 } 262 }
266 263
267 void get4Pixels( 264 void get4Pixels(
268 const void* src, int index, Sk4f* px0, Sk4f* px1, Sk4f* px2, Sk4f* px3) const override { 265 const void* src, int index, Sk4f* px0, Sk4f* px1, Sk4f* px2, Sk4f* px3) const override {
269 *px0 = this->getPixelFromRow(src, index + 0); 266 *px0 = this->getPixelFromRow(src, index + 0);
270 *px1 = this->getPixelFromRow(src, index + 1); 267 *px1 = this->getPixelFromRow(src, index + 1);
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 } 320 }
324 321
325 while (count > 0) { 322 while (count > 0) {
326 next->blendPixel(strategy->getPixelFromRow(row, ix)); 323 next->blendPixel(strategy->getPixelFromRow(row, ix));
327 ix -= 1; 324 ix -= 1;
328 count -= 1; 325 count -= 1;
329 } 326 }
330 } 327 }
331 } 328 }
332 329
330 // -- NearestNeighborSampler --------------------------------------------------- --------------------
333 // NearestNeighborSampler - use nearest neighbor filtering to create runs of des tination pixels. 331 // NearestNeighborSampler - use nearest neighbor filtering to create runs of des tination pixels.
334 template<typename Accessor, typename Next> 332 template<typename Accessor, typename Next>
335 class NearestNeighborSampler : public SkLinearBitmapPipeline::SampleProcessorInt erface { 333 class NearestNeighborSampler : public SkLinearBitmapPipeline::SampleProcessorInt erface {
336 public: 334 public:
337 template<typename... Args> 335 template<typename... Args>
338 NearestNeighborSampler(SkLinearBitmapPipeline::BlendProcessorInterface* next , Args&& ... args) 336 NearestNeighborSampler(SkLinearBitmapPipeline::BlendProcessorInterface* next , Args&& ... args)
339 : fNext{next}, fAccessor{std::forward<Args>(args)...} { } 337 : fNext{next}, fAccessor{std::forward<Args>(args)...} { }
340 338
341 NearestNeighborSampler(SkLinearBitmapPipeline::BlendProcessorInterface* next , 339 NearestNeighborSampler(SkLinearBitmapPipeline::BlendProcessorInterface* next ,
342 const NearestNeighborSampler& sampler) 340 const NearestNeighborSampler& sampler)
343 : fNext{next}, fAccessor{sampler.fAccessor} { } 341 : fNext{next}, fAccessor{sampler.fAccessor} { }
344 342
345 void SK_VECTORCALL pointListFew(int n, Sk4s xs, Sk4s ys) override { 343 void SK_VECTORCALL pointListFew(int n, Sk4s xs, Sk4s ys) override {
346 SkASSERT(0 < n && n < 4); 344 SkASSERT(0 < n && n < 4);
347 Sk4f px0, px1, px2; 345 Sk4f px0, px1, px2;
348 fAccessor.getFewPixels(n, xs, ys, &px0, &px1, &px2); 346 fAccessor.getFewPixels(n, SkNx_cast<int>(xs), SkNx_cast<int>(ys), &px0, &px1, &px2);
349 if (n >= 1) fNext->blendPixel(px0); 347 if (n >= 1) fNext->blendPixel(px0);
350 if (n >= 2) fNext->blendPixel(px1); 348 if (n >= 2) fNext->blendPixel(px1);
351 if (n >= 3) fNext->blendPixel(px2); 349 if (n >= 3) fNext->blendPixel(px2);
352 } 350 }
353 351
354 void SK_VECTORCALL pointList4(Sk4s xs, Sk4s ys) override { 352 void SK_VECTORCALL pointList4(Sk4s xs, Sk4s ys) override {
355 Sk4f px0, px1, px2, px3; 353 Sk4f px0, px1, px2, px3;
356 fAccessor.get4Pixels(xs, ys, &px0, &px1, &px2, &px3); 354 fAccessor.get4Pixels(SkNx_cast<int>(xs), SkNx_cast<int>(ys), &px0, &px1, &px2, &px3);
357 fNext->blend4Pixels(px0, px1, px2, px3); 355 fNext->blend4Pixels(px0, px1, px2, px3);
358 } 356 }
359 357
360 void pointSpan(Span span) override { 358 void pointSpan(Span span) override {
361 SkASSERT(!span.isEmpty()); 359 SkASSERT(!span.isEmpty());
362 SkPoint start; 360 SkPoint start;
363 SkScalar length; 361 SkScalar length;
364 int count; 362 int count;
365 std::tie(start, length, count) = span; 363 std::tie(start, length, count) = span;
366 SkScalar absLength = SkScalarAbs(length); 364 SkScalar absLength = SkScalarAbs(length);
367 if (absLength < (count - 1)) { 365 if (absLength < (count - 1)) {
368 this->spanSlowRate(span); 366 this->spanSlowRate(span);
369 } else if (absLength == (count - 1)) { 367 } else if (absLength == (count - 1)) {
370 src_strategy_blend(span, fNext, &fAccessor); 368 src_strategy_blend(span, fNext, &fAccessor);
371 } else { 369 } else {
372 this->spanFastRate(span); 370 this->spanFastRate(span);
373 } 371 }
374 } 372 }
375 373
376 void repeatSpan(Span span, int32_t repeatCount) override { 374 void repeatSpan(Span span, int32_t repeatCount) override {
377 while (repeatCount > 0) { 375 while (repeatCount > 0) {
378 this->pointSpan(span); 376 this->pointSpan(span);
379 repeatCount--; 377 repeatCount--;
380 } 378 }
381 } 379 }
382 380
383 void SK_VECTORCALL bilerpEdge(Sk4s xs, Sk4s ys) override {
384 SkFAIL("Using nearest neighbor sampler, but calling a bilerpEdge.");
385 }
386
387 void bilerpSpan(Span span, SkScalar y) override {
388 SkFAIL("Using nearest neighbor sampler, but calling a bilerpSpan.");
389 }
390
391 private: 381 private:
392 // When moving through source space more slowly than dst space (zoomed in), 382 // When moving through source space more slowly than dst space (zoomed in),
393 // we'll be sampling from the same source pixel more than once. 383 // we'll be sampling from the same source pixel more than once.
394 void spanSlowRate(Span span) { 384 void spanSlowRate(Span span) {
395 SkPoint start; 385 SkPoint start; SkScalar length; int count;
396 SkScalar length;
397 int count;
398 std::tie(start, length, count) = span; 386 std::tie(start, length, count) = span;
399 SkScalar x = X(start); 387 SkScalar x = X(start);
400 SkFixed fx = SkScalarToFixed(x); 388 SkFixed fx = SkScalarToFixed(x);
401 SkScalar dx = length / (count - 1); 389 SkScalar dx = length / (count - 1);
402 SkFixed fdx = SkScalarToFixed(dx); 390 SkFixed fdx = SkScalarToFixed(dx);
403 391
404 const void* row = fAccessor.row((int)std::floor(Y(start))); 392 const void* row = fAccessor.row((int)std::floor(Y(start)));
405 Next* next = fNext; 393 Next* next = fNext;
406 394
407 int ix = SkFixedFloorToInt(fx); 395 int ix = SkFixedFloorToInt(fx);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 // We're moving through source space faster than dst (zoomed out), 432 // We're moving through source space faster than dst (zoomed out),
445 // so we'll never reuse a source pixel or be able to do contiguous loads. 433 // so we'll never reuse a source pixel or be able to do contiguous loads.
446 void spanFastRate(Span span) { 434 void spanFastRate(Span span) {
447 span_fallback(span, this); 435 span_fallback(span, this);
448 } 436 }
449 437
450 Next* const fNext; 438 Next* const fNext;
451 Accessor fAccessor; 439 Accessor fAccessor;
452 }; 440 };
453 441
442 // From an edgeType, the integer value of a pixel vs, and the integer value of t he extreme edge
443 // vMax, take the point which might be off the tile by one pixel and either wrap it or pin it to
444 // generate the right pixel. The value vs is on the interval [-1, vMax + 1]. It produces a value
445 // on the interval [0, vMax].
446 // Note: vMax is not width or height, but width-1 or height-1 because it is the largest valid pixel.
447 static inline int adjust_edge(SkShader::TileMode edgeType, int vs, int vMax) {
448 SkASSERT(-1 <= vs && vs <= vMax + 1)
449 switch (edgeType) {
450 case SkShader::kClamp_TileMode:
451 case SkShader::kMirror_TileMode:
452 vs = std::max(vs, 0);
453 vs = std::min(vs, vMax);
454 break;
455 case SkShader::kRepeat_TileMode:
456 vs = (vs <= vMax) ? vs : 0;
457 vs = (vs >= 0) ? vs : vMax;
458 break;
459 }
460 SkASSERT(0 <= vs && vs <= vMax);
461 return vs;
462 }
463
464 // From a sample point on the tile, return the top or left filter value.
465 // The result r should be in the range (0, 1]. Since this represents the weight given to the top
466 // left element, then if x == 0.5 the filter value should be 1.0.
467 // The input sample point must be on the tile, therefore it must be >= 0.
468 static SkScalar sample_to_filter(SkScalar x) {
469 SkASSERT(x >= 0.0f);
470 // The usual form of the top or left edge is x - .5, but since we are workin g on the unit
471 // square, then x + .5 works just as well. This also guarantees that v > 0.0 allowing the use
472 // of trunc.
473 SkScalar v = x + 0.5f;
474 // Produce the top or left offset a value on the range [0, 1).
475 SkScalar f = v - SkScalarTruncToScalar(v);
476 // Produce the filter value which is on the range (0, 1].
477 SkScalar r = 1.0f - f;
478 SkASSERT(0.0f < r && r <= 1.0f);
479 return r;
480 }
481
454 // -- BilerpSampler ------------------------------------------------------------ -------------------- 482 // -- BilerpSampler ------------------------------------------------------------ --------------------
455 // BilerpSampler - use a bilerp filter to create runs of destination pixels. 483 // BilerpSampler - use a bilerp filter to create runs of destination pixels.
484 // Note: in the code below, there are two types of points
485 // * sample points - these are the points passed in by pointList* and Span s.
486 // * filter points - are created from a sample point to form the coordinat es of the points
487 // to use in the filter and to generate the filter value s.
456 template<typename Accessor, typename Next> 488 template<typename Accessor, typename Next>
457 class BilerpSampler : public SkLinearBitmapPipeline::SampleProcessorInterface { 489 class BilerpSampler : public SkLinearBitmapPipeline::SampleProcessorInterface {
458 public: 490 public:
459 template<typename... Args> 491 template<typename... Args>
460 BilerpSampler(SkLinearBitmapPipeline::BlendProcessorInterface* next, Args&& ... args) 492 BilerpSampler(
461 : fNext{next}, fAccessor{std::forward<Args>(args)...} { } 493 SkLinearBitmapPipeline::BlendProcessorInterface* next,
494 SkISize dimensions,
495 SkShader::TileMode xTile, SkShader::TileMode yTile,
496 Args&& ... args
497 )
498 : fNext{next}
499 , fXEdgeType{xTile}
500 , fXMax{dimensions.width() - 1}
501 , fYEdgeType{yTile}
502 , fYMax{dimensions.height() - 1}
503 , fAccessor{std::forward<Args>(args)...} { }
462 504
463 BilerpSampler(SkLinearBitmapPipeline::BlendProcessorInterface* next, 505 BilerpSampler(SkLinearBitmapPipeline::BlendProcessorInterface* next,
464 const BilerpSampler& sampler) 506 const BilerpSampler& sampler)
465 : fNext{next}, fAccessor{sampler.fAccessor} { } 507 : fNext{next}
466 508 , fXEdgeType{sampler.fXEdgeType}
467 Sk4f bilerpNonEdgePixel(SkScalar x, SkScalar y) { 509 , fXMax{sampler.fXMax}
468 Sk4f px00, px10, px01, px11; 510 , fYEdgeType{sampler.fYEdgeType}
469 511 , fYMax{sampler.fYMax}
470 // bilerp4() expects xs, ys are the top-lefts of the 2x2 kernel. 512 , fAccessor{sampler.fAccessor} { }
471 Sk4f xs = Sk4f{x} - 0.5f;
472 Sk4f ys = Sk4f{y} - 0.5f;
473 Sk4f sampleXs = xs + Sk4f{0.0f, 1.0f, 0.0f, 1.0f};
474 Sk4f sampleYs = ys + Sk4f{0.0f, 0.0f, 1.0f, 1.0f};
475 fAccessor.get4Pixels(sampleXs, sampleYs, &px00, &px10, &px01, &px11);
476 return bilerp4(xs, ys, px00, px10, px01, px11);
477 }
478 513
479 void SK_VECTORCALL pointListFew(int n, Sk4s xs, Sk4s ys) override { 514 void SK_VECTORCALL pointListFew(int n, Sk4s xs, Sk4s ys) override {
480 SkASSERT(0 < n && n < 4); 515 SkASSERT(0 < n && n < 4);
481 auto bilerpPixel = [&](int index) { 516 auto bilerpPixel = [&](int index) {
482 return this->bilerpNonEdgePixel(xs[index], ys[index]); 517 return this->bilerpSamplePoint(SkPoint{xs[index], ys[index]});
483 }; 518 };
484 519
485 if (n >= 1) fNext->blendPixel(bilerpPixel(0)); 520 if (n >= 1) fNext->blendPixel(bilerpPixel(0));
486 if (n >= 2) fNext->blendPixel(bilerpPixel(1)); 521 if (n >= 2) fNext->blendPixel(bilerpPixel(1));
487 if (n >= 3) fNext->blendPixel(bilerpPixel(2)); 522 if (n >= 3) fNext->blendPixel(bilerpPixel(2));
488 } 523 }
489 524
490 void SK_VECTORCALL pointList4(Sk4s xs, Sk4s ys) override { 525 void SK_VECTORCALL pointList4(Sk4s xs, Sk4s ys) override {
491 auto bilerpPixel = [&](int index) { 526 auto bilerpPixel = [&](int index) {
492 return this->bilerpNonEdgePixel(xs[index], ys[index]); 527 return this->bilerpSamplePoint(SkPoint{xs[index], ys[index]});
493 }; 528 };
494 fNext->blend4Pixels(bilerpPixel(0), bilerpPixel(1), bilerpPixel(2), bile rpPixel(3)); 529 fNext->blend4Pixels(bilerpPixel(0), bilerpPixel(1), bilerpPixel(2), bile rpPixel(3));
495 } 530 }
496 531
497 void pointSpan(Span span) override { 532 void pointSpan(Span span) override {
498 this->bilerpSpan(span, span.startY());
499 }
500
501 void repeatSpan(Span span, int32_t repeatCount) override {
502 while (repeatCount > 0) {
503 this->pointSpan(span);
504 repeatCount--;
505 }
506 }
507
508 void SK_VECTORCALL bilerpEdge(Sk4s sampleXs, Sk4s sampleYs) override {
509 Sk4f px00, px10, px01, px11;
510 Sk4f xs = Sk4f{sampleXs[0]};
511 Sk4f ys = Sk4f{sampleYs[0]};
512 fAccessor.get4Pixels(sampleXs, sampleYs, &px00, &px10, &px01, &px11);
513 Sk4f pixel = bilerp4(xs, ys, px00, px10, px01, px11);
514 fNext->blendPixel(pixel);
515 }
516
517 void bilerpSpan(Span span, SkScalar y) override {
518 SkASSERT(!span.isEmpty()); 533 SkASSERT(!span.isEmpty());
519 SkPoint start; 534 SkPoint start;
520 SkScalar length; 535 SkScalar length;
521 int count; 536 int count;
522 std::tie(start, length, count) = span; 537 std::tie(start, length, count) = span;
538
539 // Nothing to do.
540 if (count == 0) {
541 return;
542 }
543
544 // Trivial case. No sample points are generated other than start.
545 if (count == 1) {
546 fNext->blendPixel(this->bilerpSamplePoint(start));
547 return;
548 }
549
550 // Note: the following code could be done in terms of dx = length / (cou nt -1), but that
551 // would introduce a divide that is not needed for the most common dx == 1 cases.
523 SkScalar absLength = SkScalarAbs(length); 552 SkScalar absLength = SkScalarAbs(length);
524 if (absLength == 0.0f) { 553 if (absLength == 0.0f) {
525 this->spanZeroRate(span, y); 554 // |dx| == 0
555 // length is zero, so clamp an edge pixel.
556 this->spanZeroRate(span);
526 } else if (absLength < (count - 1)) { 557 } else if (absLength < (count - 1)) {
527 this->spanSlowRate(span, y); 558 // 0 < |dx| < 1.
559 this->spanSlowRate(span);
528 } else if (absLength == (count - 1)) { 560 } else if (absLength == (count - 1)) {
529 if (std::fmod(span.startX() - 0.5f, 1.0f) == 0.0f) { 561 // |dx| == 1.
530 if (std::fmod(span.startY() - 0.5f, 1.0f) == 0.0f) { 562 if (sample_to_filter(span.startX()) == 1.0f
531 src_strategy_blend(span, fNext, &fAccessor); 563 && sample_to_filter(span.startY()) == 1.0f) {
564 // All the pixels are aligned with the dest; go fast.
565 src_strategy_blend(span, fNext, &fAccessor);
566 } else {
567 // There is some sub-pixel offsets, so bilerp.
568 this->spanUnitRate(span);
569 }
570 } else if (absLength < 2.0f * (count - 1)) {
571 // 1 < |dx| < 2.
572 this->spanMediumRate(span);
573 } else {
574 // |dx| >= 2.
575 this->spanFastRate(span);
576 }
577 }
578
579 void repeatSpan(Span span, int32_t repeatCount) override {
580 while (repeatCount > 0) {
581 this->pointSpan(span);
582 repeatCount--;
583 }
584 }
585
586 private:
587
588 // Convert a sample point to the points used by the filter.
589 void filterPoints(SkPoint sample, Sk4i* filterXs, Sk4i* filterYs) {
590 // May be less than zero. Be careful to use Floor.
591 int x0 = adjust_edge(fXEdgeType, SkScalarFloorToInt(X(sample) - 0.5), fX Max);
592 // Always greater than zero. Use the faster Trunc.
593 int x1 = adjust_edge(fXEdgeType, SkScalarTruncToInt(X(sample) + 0.5), fX Max);
594 int y0 = adjust_edge(fYEdgeType, SkScalarFloorToInt(Y(sample) - 0.5), fY Max);
595 int y1 = adjust_edge(fYEdgeType, SkScalarTruncToInt(Y(sample) + 0.5), fY Max);
596
597 *filterXs = Sk4i{x0, x1, x0, x1};
598 *filterYs = Sk4i{y0, y0, y1, y1};
599 }
600
601 // Given a sample point, generate a color by bilerping the four filter point s.
602 Sk4f bilerpSamplePoint(SkPoint sample) {
603 Sk4i iXs, iYs;
604 filterPoints(sample, &iXs, &iYs);
605 Sk4f px00, px10, px01, px11;
606 fAccessor.get4Pixels(iXs, iYs, &px00, &px10, &px01, &px11);
607 return bilerp4(Sk4f{X(sample) - 0.5f}, Sk4f{Y(sample) - 0.5f}, px00, px1 0, px01, px11);
608 }
609
610 // Get two pixels at x from row0 and row1.
611 void get2PixelColumn(const void* row0, const void* row1, int x, Sk4f* px0, S k4f* px1) {
612 *px0 = fAccessor.getPixelFromRow(row0, x);
613 *px1 = fAccessor.getPixelFromRow(row1, x);
614 }
615
616 // |dx| == 0. This code assumes that length is zero.
617 void spanZeroRate(Span span) {
618 SkPoint start; SkScalar length; int count;
619 std::tie(start, length, count) = span;
620 SkASSERT(length == 0.0f);
621
622 // Filter for the blending of the top and bottom pixels.
623 SkScalar filterY = sample_to_filter(Y(start));
624
625 // Generate the four filter points from the sample point start. Generate the row* values.
626 Sk4i iXs, iYs;
627 this->filterPoints(start, &iXs, &iYs);
628 const void* const row0 = fAccessor.row(iYs[0]);
629 const void* const row1 = fAccessor.row(iYs[2]);
630
631 // Get the two pixels that make up the clamping pixel.
632 Sk4f pxTop, pxBottom;
633 this->get2PixelColumn(row0, row1, SkScalarFloorToInt(X(start)), &pxTop, &pxBottom);
634 Sk4f pixel = pxTop * filterY + (1.0f - filterY) * pxBottom;
635
636 while (count >= 4) {
637 fNext->blend4Pixels(pixel, pixel, pixel, pixel);
638 count -= 4;
639 }
640 while (count > 0) {
641 fNext->blendPixel(pixel);
642 count -= 1;
643 }
644 }
645
646 // 0 < |dx| < 1. This code reuses the calculations from previous pixels to r educe
647 // computation. In particular, several destination pixels maybe generated fr om the same four
648 // source pixels.
649 // In the following code a "part" is a combination of two pixels from the sa me column of the
650 // filter.
651 void spanSlowRate(Span span) {
652 SkPoint start; SkScalar length; int count;
653 std::tie(start, length, count) = span;
654
655 // Calculate the distance between each sample point.
656 const SkScalar dx = length / (count - 1);
657 SkASSERT(-1.0f < dx && dx < 1.0f && dx != 0.0f);
658
659 // Generate the filter values for the top-left corner.
660 // Note: these values are in filter space; this has implications about h ow to adjust
661 // these values at each step. For example, as the sample point increases , the filter
662 // value decreases, this is because the filter and position are related by
663 // (1 - (X(sample) - .5)) % 1. The (1 - stuff) causes the filter to move in the opposite
664 // direction of the sample point which is increasing by dx.
665 SkScalar filterX = sample_to_filter(X(start));
666 SkScalar filterY = sample_to_filter(Y(start));
667
668 // Generate the four filter points from the sample point start. Generate the row* values.
669 Sk4i iXs, iYs;
670 this->filterPoints(start, &iXs, &iYs);
671 const void* const row0 = fAccessor.row(iYs[0]);
672 const void* const row1 = fAccessor.row(iYs[2]);
673
674 // Generate part of the filter value at xColumn.
675 auto partAtColumn = [&](int xColumn) {
676 int adjustedColumn = adjust_edge(fXEdgeType, xColumn, fXMax);
677 Sk4f pxTop, pxBottom;
678 this->get2PixelColumn(row0, row1, adjustedColumn, &pxTop, &pxBottom) ;
679 return pxTop * filterY + (1.0f - filterY) * pxBottom;
680 };
681
682 // The leftPart is made up of two pixels from the left column of the fil ter, right part
683 // is similar. The top and bottom pixels in the *Part are created as a l inear blend of
684 // the top and bottom pixels using filterY. See the partAtColumn functio n above.
685 Sk4f leftPart = partAtColumn(iXs[0]);
686 Sk4f rightPart = partAtColumn(iXs[1]);
687
688 // Create a destination color by blending together a left and right part using filterX.
689 auto bilerp = [&](const Sk4f& leftPart, const Sk4f& rightPart) {
690 Sk4f pixel = leftPart * filterX + rightPart * (1.0f - filterX);
691 return check_pixel(pixel);
692 };
693
694 // Send the first pixel to the destination. This simplifies the loop str ucture so that no
695 // extra pixels are fetched for the last iteration of the loop.
696 fNext->blendPixel(bilerp(leftPart, rightPart));
697 count -= 1;
698
699 if (dx > 0.0f) {
700 // * positive direction - generate destination pixels by sliding the filter from left
701 // to right.
702 int rightPartCursor = iXs[1];
703
704 // Advance the filter from left to right. Remember that moving the t op-left corner of
705 // the filter to the right actually makes the filter value smaller.
706 auto advanceFilter = [&]() {
707 filterX -= dx;
708 if (filterX <= 0.0f) {
709 filterX += 1.0f;
710 leftPart = rightPart;
711 rightPartCursor += 1;
712 rightPart = partAtColumn(rightPartCursor);
713 }
714 SkASSERT(0.0f < filterX && filterX <= 1.0f);
715
716 return bilerp(leftPart, rightPart);
717 };
718
719 while (count >= 4) {
720 Sk4f px0 = advanceFilter(),
721 px1 = advanceFilter(),
722 px2 = advanceFilter(),
723 px3 = advanceFilter();
724 fNext->blend4Pixels(px0, px1, px2, px3);
725 count -= 4;
726 }
727
728 while (count > 0) {
729 fNext->blendPixel(advanceFilter());
730 count -= 1;
731 }
732 } else {
733 // * negative direction - generate destination pixels by sliding the filter from
734 // right to left.
735 int leftPartCursor = iXs[0];
736
737 // Advance the filter from right to left. Remember that moving the t op-left corner of
738 // the filter to the left actually makes the filter value larger.
739 auto advanceFilter = [&]() {
740 // Remember, dx < 0 therefore this adds |dx| to filterX.
741 filterX -= dx;
742 // At this point filterX may be > 1, and needs to be wrapped bac k on to the filter
743 // interval, and the next column in the filter is calculated.
744 if (filterX > 1.0f) {
745 filterX -= 1.0f;
746 rightPart = leftPart;
747 leftPartCursor -= 1;
748 leftPart = partAtColumn(leftPartCursor);
749 }
750 SkASSERT(0.0f < filterX && filterX <= 1.0f);
751
752 return bilerp(leftPart, rightPart);
753 };
754
755 while (count >= 4) {
756 Sk4f px0 = advanceFilter(),
757 px1 = advanceFilter(),
758 px2 = advanceFilter(),
759 px3 = advanceFilter();
760 fNext->blend4Pixels(px0, px1, px2, px3);
761 count -= 4;
762 }
763
764 while (count > 0) {
765 fNext->blendPixel(advanceFilter());
766 count -= 1;
767 }
768 }
769 }
770
771 // |dx| == 1. Moving through source space at a rate of 1 source pixel per 1 dst pixel.
772 // Every filter part is used for two destination pixels, and the code can bu lk load four
773 // pixels at a time.
774 void spanUnitRate(Span span) {
775 SkPoint start; SkScalar length; int count;
776 std::tie(start, length, count) = span;
777 SkASSERT(SkScalarAbs(length) == (count - 1));
778
779 // Calculate the four filter points of start, and use the two different Y values to
780 // generate the row pointers.
781 Sk4i iXs, iYs;
782 filterPoints(start, &iXs, &iYs);
783 const void* row0 = fAccessor.row(iYs[0]);
784 const void* row1 = fAccessor.row(iYs[2]);
785
786 // Calculate the filter values for the top-left filter element.
787 const SkScalar filterX = sample_to_filter(X(start));
788 const SkScalar filterY = sample_to_filter(Y(start));
789
790 // Generate part of the filter value at xColumn.
791 auto partAtColumn = [&](int xColumn) {
792 int adjustedColumn = adjust_edge(fXEdgeType, xColumn, fXMax);
793 Sk4f pxTop, pxBottom;
794 this->get2PixelColumn(row0, row1, adjustedColumn, &pxTop, &pxBottom) ;
795 return pxTop * filterY + (1.0f - filterY) * pxBottom;
796 };
797
798 auto get4Parts = [&](int ix, Sk4f* part0, Sk4f* part1, Sk4f* part2, Sk4f * part3) {
799 // Check if the pixels needed are near the edges. If not go fast usi ng bulk pixels,
800 // otherwise be careful.
801 if (0 <= ix && ix <= fXMax - 3) {
802 Sk4f px00, px10, px20, px30,
803 px01, px11, px21, px31;
804 fAccessor.get4Pixels(row0, ix, &px00, &px10, &px20, &px30);
805 fAccessor.get4Pixels(row1, ix, &px01, &px11, &px21, &px31);
806 *part0 = filterY * px00 + (1.0f - filterY) * px01;
807 *part1 = filterY * px10 + (1.0f - filterY) * px11;
808 *part2 = filterY * px20 + (1.0f - filterY) * px21;
809 *part3 = filterY * px30 + (1.0f - filterY) * px31;
810 } else {
811 *part0 = partAtColumn(ix + 0);
812 *part1 = partAtColumn(ix + 1);
813 *part2 = partAtColumn(ix + 2);
814 *part3 = partAtColumn(ix + 3);
815 }
816 };
817
818 auto bilerp = [&](const Sk4f& part0, const Sk4f& part1) {
819 return part0 * filterX + part1 * (1.0f - filterX);
820 };
821
822 if (length > 0) {
823 // * positive direction - generate destination pixels by sliding the filter from left
824 // to right.
825
826 // overlapPart is the filter part from the end of the previous four pixels used at
827 // the start of the next four pixels.
828 Sk4f overlapPart = partAtColumn(iXs[0]);
829 int rightColumnCursor = iXs[1];
830 while (count >= 4) {
831 Sk4f part0, part1, part2, part3;
832 get4Parts(rightColumnCursor, &part0, &part1, &part2, &part3);
833 Sk4f px0 = bilerp(overlapPart, part0);
834 Sk4f px1 = bilerp(part0, part1);
835 Sk4f px2 = bilerp(part1, part2);
836 Sk4f px3 = bilerp(part2, part3);
837 overlapPart = part3;
838 fNext->blend4Pixels(px0, px1, px2, px3);
839 rightColumnCursor += 4;
840 count -= 4;
841 }
842
843 while (count > 0) {
844 Sk4f rightPart = partAtColumn(rightColumnCursor);
845
846 fNext->blendPixel(bilerp(overlapPart, rightPart));
847 overlapPart = rightPart;
848 rightColumnCursor += 1;
849 count -= 1;
850 }
851 } else {
852 // * negative direction - generate destination pixels by sliding the filter from
853 // right to left.
854 Sk4f overlapPart = partAtColumn(iXs[1]);
855 int leftColumnCursor = iXs[0];
856
857 while (count >= 4) {
858 Sk4f part0, part1, part2, part3;
859 get4Parts(leftColumnCursor - 3, &part3, &part2, &part1, &part0);
860 Sk4f px0 = bilerp(part0, overlapPart);
861 Sk4f px1 = bilerp(part1, part0);
862 Sk4f px2 = bilerp(part2, part1);
863 Sk4f px3 = bilerp(part3, part2);
864 overlapPart = part3;
865 fNext->blend4Pixels(px0, px1, px2, px3);
866 leftColumnCursor -= 4;
867 count -= 4;
868 }
869
870 while (count > 0) {
871 Sk4f leftPart = partAtColumn(leftColumnCursor);
872
873 fNext->blendPixel(bilerp(leftPart, overlapPart));
874 overlapPart = leftPart;
875 leftColumnCursor -= 1;
876 count -= 1;
877 }
878 }
879 }
880
881 // 1 < |dx| < 2. Going through the source pixels at a faster rate than the d est pixels, but
882 // still slow enough to take advantage of previous calculations.
883 void spanMediumRate(Span span) {
884 SkPoint start; SkScalar length; int count;
885 std::tie(start, length, count) = span;
886
887 // Calculate the distance between each sample point.
888 const SkScalar dx = length / (count - 1);
889 SkASSERT((-2.0f < dx && dx < -1.0f) || (1.0f < dx && dx < 2.0f));
890
891 // Generate the filter values for the top-left corner.
892 // Note: these values are in filter space; this has implications about h ow to adjust
893 // these values at each step. For example, as the sample point increases , the filter
894 // value decreases, this is because the filter and position are related by
895 // (1 - (X(sample) - .5)) % 1. The (1 - stuff) causes the filter to move in the opposite
896 // direction of the sample point which is increasing by dx.
897 SkScalar filterX = sample_to_filter(X(start));
898 SkScalar filterY = sample_to_filter(Y(start));
899
900 // Generate the four filter points from the sample point start. Generate the row* values.
901 Sk4i iXs, iYs;
902 this->filterPoints(start, &iXs, &iYs);
903 const void* const row0 = fAccessor.row(iYs[0]);
904 const void* const row1 = fAccessor.row(iYs[2]);
905
906 // Generate part of the filter value at xColumn.
907 auto partAtColumn = [&](int xColumn) {
908 int adjustedColumn = adjust_edge(fXEdgeType, xColumn, fXMax);
909 Sk4f pxTop, pxBottom;
910 this->get2PixelColumn(row0, row1, adjustedColumn, &pxTop, &pxBottom) ;
911 return pxTop * filterY + (1.0f - filterY) * pxBottom;
912 };
913
914 // The leftPart is made up of two pixels from the left column of the fil ter, right part
915 // is similar. The top and bottom pixels in the *Part are created as a l inear blend of
916 // the top and bottom pixels using filterY. See the nextPart function be low.
917 Sk4f leftPart = partAtColumn(iXs[0]);
918 Sk4f rightPart = partAtColumn(iXs[1]);
919
920 // Create a destination color by blending together a left and right part using filterX.
921 auto bilerp = [&](const Sk4f& leftPart, const Sk4f& rightPart) {
922 Sk4f pixel = leftPart * filterX + rightPart * (1.0f - filterX);
923 return check_pixel(pixel);
924 };
925
926 // Send the first pixel to the destination. This simplifies the loop str ucture so that no
927 // extra pixels are fetched for the last iteration of the loop.
928 fNext->blendPixel(bilerp(leftPart, rightPart));
929 count -= 1;
930
931 if (dx > 0.0f) {
932 // * positive direction - generate destination pixels by sliding the filter from left
933 // to right.
934 int rightPartCursor = iXs[1];
935
936 // Advance the filter from left to right. Remember that moving the t op-left corner of
937 // the filter to the right actually makes the filter value smaller.
938 auto advanceFilter = [&]() {
939 filterX -= dx;
940 // At this point filterX is less than zero, but might actually b e less than -1.
941 if (filterX > -1.0f) {
942 filterX += 1.0f;
943 leftPart = rightPart;
944 rightPartCursor += 1;
945 rightPart = partAtColumn(rightPartCursor);
532 } else { 946 } else {
533 this->spanUnitRateAlignedX(span, y); 947 filterX += 2.0f;
948 rightPartCursor += 2;
949 leftPart = partAtColumn(rightPartCursor - 1);
950 rightPart = partAtColumn(rightPartCursor);
534 } 951 }
535 } else { 952 SkASSERT(0.0f < filterX && filterX <= 1.0f);
536 this->spanUnitRate(span, y); 953
954 return bilerp(leftPart, rightPart);
955 };
956
957 while (count >= 4) {
958 Sk4f px0 = advanceFilter(),
959 px1 = advanceFilter(),
960 px2 = advanceFilter(),
961 px3 = advanceFilter();
962 fNext->blend4Pixels(px0, px1, px2, px3);
963 count -= 4;
964 }
965
966 while (count > 0) {
967 fNext->blendPixel(advanceFilter());
968 count -= 1;
537 } 969 }
538 } else { 970 } else {
539 this->spanFastRate(span, y); 971 // * negative direction - generate destination pixels by sliding the filter from
540 } 972 // right to left.
541 } 973 int leftPartCursor = iXs[0];
542 974
543 private: 975 auto advanceFilter = [&]() {
544 void spanZeroRate(Span span, SkScalar y1) { 976 // Remember, dx < 0 therefore this adds |dx| to filterX.
545 SkScalar y0 = span.startY() - 0.5f; 977 filterX -= dx;
546 y1 += 0.5f; 978 // At this point, filterX is greater than one, but may actually be greater than two.
547 int iy0 = SkScalarFloorToInt(y0); 979 if (filterX < 2.0f) {
548 SkScalar filterY1 = y0 - iy0; 980 filterX -= 1.0f;
549 SkScalar filterY0 = 1.0f - filterY1; 981 rightPart = leftPart;
550 int iy1 = SkScalarFloorToInt(y1); 982 leftPartCursor -= 1;
551 int ix = SkScalarFloorToInt(span.startX()); 983 leftPart = partAtColumn(leftPartCursor);
552 Sk4f pixelY0 = fAccessor.getPixelFromRow(fAccessor.row(iy0), ix); 984 } else {
553 Sk4f pixelY1 = fAccessor.getPixelFromRow(fAccessor.row(iy1), ix); 985 filterX -= 2.0f;
554 Sk4f filterPixel = pixelY0 * filterY0 + pixelY1 * filterY1; 986 leftPartCursor -= 2;
555 int count = span.count(); 987 rightPart = partAtColumn(leftPartCursor - 1);
556 while (count >= 4) { 988 leftPart = partAtColumn(leftPartCursor);
557 fNext->blend4Pixels(filterPixel, filterPixel, filterPixel, filterPix el); 989 }
558 count -= 4; 990 SkASSERT(0.0f < filterX && filterX <= 1.0f);
559 } 991 return bilerp(leftPart, rightPart);
560 while (count > 0) { 992 };
561 fNext->blendPixel(filterPixel); 993
562 count -= 1; 994 while (count >= 4) {
563 } 995 Sk4f px0 = advanceFilter(),
564 } 996 px1 = advanceFilter(),
565 997 px2 = advanceFilter(),
566 // When moving through source space more slowly than dst space (zoomed in), 998 px3 = advanceFilter();
567 // we'll be sampling from the same source pixel more than once. 999 fNext->blend4Pixels(px0, px1, px2, px3);
568 void spanSlowRate(Span span, SkScalar ry1) { 1000 count -= 4;
569 SkPoint start; 1001 }
570 SkScalar length; 1002
571 int count; 1003 while (count > 0) {
572 std::tie(start, length, count) = span; 1004 fNext->blendPixel(advanceFilter());
573 SkFixed fx = SkScalarToFixed(X(start)-0.5f); 1005 count -= 1;
574 1006 }
575 SkFixed fdx = SkScalarToFixed(length / (count - 1)); 1007 }
576
577 Sk4f xAdjust;
578 if (fdx >= 0) {
579 xAdjust = Sk4f{-1.0f};
580 } else {
581 xAdjust = Sk4f{1.0f};
582 }
583 int ix = SkFixedFloorToInt(fx);
584 int ioldx = ix;
585 Sk4f x{SkFixedToScalar(fx) - ix};
586 Sk4f dx{SkFixedToScalar(fdx)};
587 SkScalar ry0 = Y(start) - 0.5f;
588 ry1 += 0.5f;
589 SkScalar yFloor = std::floor(ry0);
590 Sk4f y1 = Sk4f{ry0 - yFloor};
591 Sk4f y0 = Sk4f{1.0f} - y1;
592 const void* const row0 = fAccessor.row(SkScalarFloorToInt(ry0));
593 const void* const row1 = fAccessor.row(SkScalarFloorToInt(ry1));
594 Sk4f fpixel00 = y0 * fAccessor.getPixelFromRow(row0, ix);
595 Sk4f fpixel01 = y1 * fAccessor.getPixelFromRow(row1, ix);
596 Sk4f fpixel10 = y0 * fAccessor.getPixelFromRow(row0, ix + 1);
597 Sk4f fpixel11 = y1 * fAccessor.getPixelFromRow(row1, ix + 1);
598 auto getNextPixel = [&]() {
599 if (ix != ioldx) {
600 fpixel00 = fpixel10;
601 fpixel01 = fpixel11;
602 fpixel10 = y0 * fAccessor.getPixelFromRow(row0, ix + 1);
603 fpixel11 = y1 * fAccessor.getPixelFromRow(row1, ix + 1);
604 ioldx = ix;
605 x = x + xAdjust;
606 }
607
608 Sk4f x0, x1;
609 x0 = Sk4f{1.0f} - x;
610 x1 = x;
611 Sk4f fpixel = x0 * (fpixel00 + fpixel01) + x1 * (fpixel10 + fpixel11 );
612 fx += fdx;
613 ix = SkFixedFloorToInt(fx);
614 x = x + dx;
615 return fpixel;
616 };
617
618 while (count >= 4) {
619 Sk4f fpixel0 = getNextPixel();
620 Sk4f fpixel1 = getNextPixel();
621 Sk4f fpixel2 = getNextPixel();
622 Sk4f fpixel3 = getNextPixel();
623
624 fNext->blend4Pixels(fpixel0, fpixel1, fpixel2, fpixel3);
625 count -= 4;
626 }
627
628 while (count > 0) {
629 fNext->blendPixel(getNextPixel());
630
631 count -= 1;
632 }
633 }
634
635 // We're moving through source space at a rate of 1 source pixel per 1 dst p ixel.
636 // We'll never re-use pixels, but we can at least load contiguous pixels.
637 void spanUnitRate(Span span, SkScalar y1) {
638 y1 += 0.5f;
639 SkScalar y0 = span.startY() - 0.5f;
640 int iy0 = SkScalarFloorToInt(y0);
641 SkScalar filterY1 = y0 - iy0;
642 SkScalar filterY0 = 1.0f - filterY1;
643 int iy1 = SkScalarFloorToInt(y1);
644 const void* rowY0 = fAccessor.row(iy0);
645 const void* rowY1 = fAccessor.row(iy1);
646 SkScalar x0 = span.startX() - 0.5f;
647 int ix0 = SkScalarFloorToInt(x0);
648 SkScalar filterX1 = x0 - ix0;
649 SkScalar filterX0 = 1.0f - filterX1;
650
651 auto getPixelY0 = [&]() {
652 Sk4f px = fAccessor.getPixelFromRow(rowY0, ix0);
653 return px * filterY0;
654 };
655
656 auto getPixelY1 = [&]() {
657 Sk4f px = fAccessor.getPixelFromRow(rowY1, ix0);
658 return px * filterY1;
659 };
660
661 auto get4PixelsY0 = [&](int ix, Sk4f* px0, Sk4f* px1, Sk4f* px2, Sk4f* p x3) {
662 fAccessor.get4Pixels(rowY0, ix, px0, px1, px2, px3);
663 *px0 = *px0 * filterY0;
664 *px1 = *px1 * filterY0;
665 *px2 = *px2 * filterY0;
666 *px3 = *px3 * filterY0;
667 };
668
669 auto get4PixelsY1 = [&](int ix, Sk4f* px0, Sk4f* px1, Sk4f* px2, Sk4f* p x3) {
670 fAccessor.get4Pixels(rowY1, ix, px0, px1, px2, px3);
671 *px0 = *px0 * filterY1;
672 *px1 = *px1 * filterY1;
673 *px2 = *px2 * filterY1;
674 *px3 = *px3 * filterY1;
675 };
676
677 auto lerp = [&](Sk4f& pixelX0, Sk4f& pixelX1) {
678 return pixelX0 * filterX0 + pixelX1 * filterX1;
679 };
680
681 // Mid making 4 unit rate.
682 Sk4f pxB = getPixelY0() + getPixelY1();
683 if (span.length() > 0) {
684 int count = span.count();
685 while (count >= 4) {
686 Sk4f px00, px10, px20, px30;
687 get4PixelsY0(ix0, &px00, &px10, &px20, &px30);
688 Sk4f px01, px11, px21, px31;
689 get4PixelsY1(ix0, &px01, &px11, &px21, &px31);
690 Sk4f pxS0 = px00 + px01;
691 Sk4f px0 = lerp(pxB, pxS0);
692 Sk4f pxS1 = px10 + px11;
693 Sk4f px1 = lerp(pxS0, pxS1);
694 Sk4f pxS2 = px20 + px21;
695 Sk4f px2 = lerp(pxS1, pxS2);
696 Sk4f pxS3 = px30 + px31;
697 Sk4f px3 = lerp(pxS2, pxS3);
698 pxB = pxS3;
699 fNext->blend4Pixels(px0, px1, px2, px3);
700 ix0 += 4;
701 count -= 4;
702 }
703 while (count > 0) {
704 Sk4f pixelY0 = fAccessor.getPixelFromRow(rowY0, ix0);
705 Sk4f pixelY1 = fAccessor.getPixelFromRow(rowY1, ix0);
706
707 fNext->blendPixel(lerp(pixelY0, pixelY1));
708 ix0 += 1;
709 count -= 1;
710 }
711 } else {
712 int count = span.count();
713 while (count >= 4) {
714 Sk4f px00, px10, px20, px30;
715 get4PixelsY0(ix0 - 3, &px00, &px10, &px20, &px30);
716 Sk4f px01, px11, px21, px31;
717 get4PixelsY1(ix0 - 3, &px01, &px11, &px21, &px31);
718 Sk4f pxS3 = px30 + px31;
719 Sk4f px0 = lerp(pxS3, pxB);
720 Sk4f pxS2 = px20 + px21;
721 Sk4f px1 = lerp(pxS2, pxS3);
722 Sk4f pxS1 = px10 + px11;
723 Sk4f px2 = lerp(pxS1, pxS2);
724 Sk4f pxS0 = px00 + px01;
725 Sk4f px3 = lerp(pxS0, pxS1);
726 pxB = pxS0;
727 fNext->blend4Pixels(px0, px1, px2, px3);
728 ix0 -= 4;
729 count -= 4;
730 }
731 while (count > 0) {
732 Sk4f pixelY0 = fAccessor.getPixelFromRow(rowY0, ix0);
733 Sk4f pixelY1 = fAccessor.getPixelFromRow(rowY1, ix0);
734
735 fNext->blendPixel(lerp(pixelY0, pixelY1));
736 ix0 -= 1;
737 count -= 1;
738 }
739 }
740 }
741
742 void spanUnitRateAlignedX(Span span, SkScalar y1) {
743 SkScalar y0 = span.startY() - 0.5f;
744 y1 += 0.5f;
745 int iy0 = SkScalarFloorToInt(y0);
746 SkScalar filterY1 = y0 - iy0;
747 SkScalar filterY0 = 1.0f - filterY1;
748 int iy1 = SkScalarFloorToInt(y1);
749 int ix = SkScalarFloorToInt(span.startX());
750 const void* rowY0 = fAccessor.row(iy0);
751 const void* rowY1 = fAccessor.row(iy1);
752 auto lerp = [&](Sk4f* pixelY0, Sk4f* pixelY1) {
753 return *pixelY0 * filterY0 + *pixelY1 * filterY1;
754 };
755
756 if (span.length() > 0) {
757 int count = span.count();
758 while (count >= 4) {
759 Sk4f px00, px10, px20, px30;
760 fAccessor.get4Pixels(rowY0, ix, &px00, &px10, &px20, &px30);
761 Sk4f px01, px11, px21, px31;
762 fAccessor.get4Pixels(rowY1, ix, &px01, &px11, &px21, &px31);
763 fNext->blend4Pixels(
764 lerp(&px00, &px01), lerp(&px10, &px11), lerp(&px20, &px21), lerp(&px30, &px31));
765 ix += 4;
766 count -= 4;
767 }
768 while (count > 0) {
769 Sk4f pixelY0 = fAccessor.getPixelFromRow(rowY0, ix);
770 Sk4f pixelY1 = fAccessor.getPixelFromRow(rowY1, ix);
771
772 fNext->blendPixel(lerp(&pixelY0, &pixelY1));
773 ix += 1;
774 count -= 1;
775 }
776 } else {
777 int count = span.count();
778 while (count >= 4) {
779 Sk4f px00, px10, px20, px30;
780 fAccessor.get4Pixels(rowY0, ix - 3, &px30, &px20, &px10, &px00);
781 Sk4f px01, px11, px21, px31;
782 fAccessor.get4Pixels(rowY1, ix - 3, &px31, &px21, &px11, &px01);
783 fNext->blend4Pixels(
784 lerp(&px00, &px01), lerp(&px10, &px11), lerp(&px20, &px21), lerp(&px30, &px31));
785 ix -= 4;
786 count -= 4;
787 }
788 while (count > 0) {
789 Sk4f pixelY0 = fAccessor.getPixelFromRow(rowY0, ix);
790 Sk4f pixelY1 = fAccessor.getPixelFromRow(rowY1, ix);
791
792 fNext->blendPixel(lerp(&pixelY0, &pixelY1));
793 ix -= 1;
794 count -= 1;
795 }
796 }
797 } 1008 }
798 1009
799 // We're moving through source space faster than dst (zoomed out), 1010 // We're moving through source space faster than dst (zoomed out),
800 // so we'll never reuse a source pixel or be able to do contiguous loads. 1011 // so we'll never reuse a source pixel or be able to do contiguous loads.
801 void spanFastRate(Span span, SkScalar y1) { 1012 void spanFastRate(Span span) {
802 SkPoint start; 1013 SkPoint start; SkScalar length; int count;
803 SkScalar length;
804 int count;
805 std::tie(start, length, count) = span; 1014 std::tie(start, length, count) = span;
806 SkScalar x = X(start); 1015 SkScalar x = X(start);
807 SkScalar y = Y(start); 1016 SkScalar y = Y(start);
808 1017
809 // In this sampler, it is assumed that if span.StartY() and y1 are the s ame then both 1018 SkScalar dx = length / (count - 1);
810 // y-lines are on the same tile. 1019 while (count > 0) {
811 if (y == y1) { 1020 fNext->blendPixel(this->bilerpSamplePoint(SkPoint{x, y}));
812 // Both y-lines are on the same tile. 1021 x += dx;
813 span_fallback(span, this); 1022 count -= 1;
814 } else {
815 // The y-lines are on different tiles.
816 SkScalar dx = length / (count - 1);
817 Sk4f ys = {y - 0.5f, y - 0.5f, y1 + 0.5f, y1 + 0.5f};
818 while (count > 0) {
819 Sk4f xs = Sk4f{-0.5f, 0.5f, -0.5f, 0.5f} + Sk4f{x};
820 this->bilerpEdge(xs, ys);
821 x += dx;
822 count -= 1;
823 }
824 } 1023 }
825 } 1024 }
826 1025
827 Next* const fNext; 1026 Next* const fNext;
828 Accessor fAccessor; 1027 const SkShader::TileMode fXEdgeType;
1028 const int fXMax;
1029 const SkShader::TileMode fYEdgeType;
1030 const int fYMax;
1031 Accessor fAccessor;
829 }; 1032 };
830 1033
831 } // namespace 1034 } // namespace
832 1035
833 #endif // SkLinearBitmapPipeline_sampler_DEFINED 1036 #endif // SkLinearBitmapPipeline_sampler_DEFINED
OLDNEW
« no previous file with comments | « src/core/SkLinearBitmapPipeline_core.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698