blob: 8171b855a40905f50a40da4751ad9f2c1c94ad45 [file] [log] [blame]
herb6eff52a2016-03-23 09:00:33 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkLinearBitmapPipeline_sampler_DEFINED
9#define SkLinearBitmapPipeline_sampler_DEFINED
10
herbcf05dcd2016-05-11 11:53:36 -070011#include <tuple>
12
benjaminwagner6c71e0a2016-04-07 08:49:31 -070013#include "SkFixed.h"
herbd5f2e2e2016-04-14 11:16:44 -070014#include "SkHalf.h"
herb6eff52a2016-03-23 09:00:33 -070015#include "SkLinearBitmapPipeline_core.h"
herbcf05dcd2016-05-11 11:53:36 -070016#include "SkPM4fPriv.h"
herb6eff52a2016-03-23 09:00:33 -070017
18namespace {
19// Explaination of the math:
20// 1 - x x
21// +--------+--------+
22// | | |
23// 1 - y | px00 | px10 |
24// | | |
25// +--------+--------+
26// | | |
27// y | px01 | px11 |
28// | | |
29// +--------+--------+
30//
31//
32// Given a pixelxy each is multiplied by a different factor derived from the fractional part of x
33// and y:
34// * px00 -> (1 - x)(1 - y) = 1 - x - y + xy
35// * px10 -> x(1 - y) = x - xy
36// * px01 -> (1 - x)y = y - xy
37// * px11 -> xy
38// So x * y is calculated first and then used to calculate all the other factors.
39static Sk4s VECTORCALL bilerp4(Sk4s xs, Sk4s ys, Sk4f px00, Sk4f px10,
40 Sk4f px01, Sk4f px11) {
41 // Calculate fractional xs and ys.
42 Sk4s fxs = xs - xs.floor();
43 Sk4s fys = ys - ys.floor();
44 Sk4s fxys{fxs * fys};
45 Sk4f sum = px11 * fxys;
46 sum = sum + px01 * (fys - fxys);
47 sum = sum + px10 * (fxs - fxys);
48 sum = sum + px00 * (Sk4f{1.0f} - fxs - fys + fxys);
49 return sum;
50}
51
herb6eff52a2016-03-23 09:00:33 -070052template<typename SourceStrategy, typename Next>
53class GeneralSampler {
54public:
55 template<typename... Args>
herb9e0efe52016-04-08 13:25:28 -070056 GeneralSampler(SkLinearBitmapPipeline::BlendProcessorInterface* next, Args&& ... args)
herb6eff52a2016-03-23 09:00:33 -070057 : fNext{next}, fStrategy{std::forward<Args>(args)...} { }
58
herb9e0efe52016-04-08 13:25:28 -070059 GeneralSampler(SkLinearBitmapPipeline::BlendProcessorInterface* next,
60 const GeneralSampler& sampler)
61 : fNext{next}, fStrategy{sampler.fStrategy} { }
62
herb6eff52a2016-03-23 09:00:33 -070063 void VECTORCALL nearestListFew(int n, Sk4s xs, Sk4s ys) {
64 SkASSERT(0 < n && n < 4);
65 Sk4f px0, px1, px2;
66 fStrategy.getFewPixels(n, xs, ys, &px0, &px1, &px2);
herb9e0efe52016-04-08 13:25:28 -070067 if (n >= 1) fNext->blendPixel(px0);
68 if (n >= 2) fNext->blendPixel(px1);
69 if (n >= 3) fNext->blendPixel(px2);
herb6eff52a2016-03-23 09:00:33 -070070 }
71
72 void VECTORCALL nearestList4(Sk4s xs, Sk4s ys) {
73 Sk4f px0, px1, px2, px3;
74 fStrategy.get4Pixels(xs, ys, &px0, &px1, &px2, &px3);
herb9e0efe52016-04-08 13:25:28 -070075 fNext->blend4Pixels(px0, px1, px2, px3);
herb6eff52a2016-03-23 09:00:33 -070076 }
77
78 void nearestSpan(Span span) {
79 SkASSERT(!span.isEmpty());
80 SkPoint start;
81 SkScalar length;
82 int count;
83 std::tie(start, length, count) = span;
84 SkScalar absLength = SkScalarAbs(length);
85 if (absLength < (count - 1)) {
86 this->nearestSpanSlowRate(span);
87 } else if (absLength == (count - 1)) {
88 this->nearestSpanUnitRate(span);
89 } else {
90 this->nearestSpanFastRate(span);
91 }
92 }
93
94 Sk4f bilerNonEdgePixel(SkScalar x, SkScalar y) {
95 Sk4f px00, px10, px01, px11;
96 Sk4f xs = Sk4f{x};
97 Sk4f ys = Sk4f{y};
98 Sk4f sampleXs = xs + Sk4f{-0.5f, 0.5f, -0.5f, 0.5f};
99 Sk4f sampleYs = ys + Sk4f{-0.5f, -0.5f, 0.5f, 0.5f};
100 fStrategy.get4Pixels(sampleXs, sampleYs, &px00, &px10, &px01, &px11);
101 return bilerp4(xs, ys, px00, px10, px01, px11);
102 }
103
104 void VECTORCALL bilerpListFew(int n, Sk4s xs, Sk4s ys) {
105 SkASSERT(0 < n && n < 4);
106 auto bilerpPixel = [&](int index) {
107 return this->bilerNonEdgePixel(xs[index], ys[index]);
108 };
109
herb9e0efe52016-04-08 13:25:28 -0700110 if (n >= 1) fNext->blendPixel(bilerpPixel(0));
111 if (n >= 2) fNext->blendPixel(bilerpPixel(1));
112 if (n >= 3) fNext->blendPixel(bilerpPixel(2));
herb6eff52a2016-03-23 09:00:33 -0700113 }
114
115 void VECTORCALL bilerpList4(Sk4s xs, Sk4s ys) {
116 auto bilerpPixel = [&](int index) {
117 return this->bilerNonEdgePixel(xs[index], ys[index]);
118 };
herb9e0efe52016-04-08 13:25:28 -0700119 fNext->blend4Pixels(bilerpPixel(0), bilerpPixel(1), bilerpPixel(2), bilerpPixel(3));
herb6eff52a2016-03-23 09:00:33 -0700120 }
121
122 void VECTORCALL bilerpEdge(Sk4s sampleXs, Sk4s sampleYs) {
123 Sk4f px00, px10, px01, px11;
124 Sk4f xs = Sk4f{sampleXs[0]};
125 Sk4f ys = Sk4f{sampleYs[0]};
126 fStrategy.get4Pixels(sampleXs, sampleYs, &px00, &px10, &px01, &px11);
127 Sk4f pixel = bilerp4(xs, ys, px00, px10, px01, px11);
herb9e0efe52016-04-08 13:25:28 -0700128 fNext->blendPixel(pixel);
herb6eff52a2016-03-23 09:00:33 -0700129 }
130
131 void bilerpSpan(Span span) {
132 this->bilerpSpanWithY(span, span.startY());
133 }
134
135 void bilerpSpanWithY(Span span, SkScalar y) {
136 SkASSERT(!span.isEmpty());
137 SkPoint start;
138 SkScalar length;
139 int count;
140 std::tie(start, length, count) = span;
141 SkScalar absLength = SkScalarAbs(length);
142 if (absLength == 0.0f) {
143 this->bilerpSpanZeroRate(span, y);
144 } else if (absLength < (count - 1)) {
145 this->bilerpSpanSlowRate(span, y);
146 } else if (absLength == (count - 1)) {
147 if (std::fmod(span.startX() - 0.5f, 1.0f) == 0.0f) {
148 if (std::fmod(span.startY() - 0.5f, 1.0f) == 0.0f) {
149 this->nearestSpanUnitRate(span);
150 } else {
151 this->bilerpSpanUnitRateAlignedX(span, y);
152 }
153 } else {
154 this->bilerpSpanUnitRate(span, y);
155 }
156 } else {
157 this->bilerpSpanFastRate(span, y);
158 }
159 }
160
161private:
162 // When moving through source space more slowly than dst space (zoomed in),
163 // we'll be sampling from the same source pixel more than once.
164 void nearestSpanSlowRate(Span span) {
165 SkPoint start;
166 SkScalar length;
167 int count;
168 std::tie(start, length, count) = span;
169 SkScalar x = X(start);
170 SkFixed fx = SkScalarToFixed(x);
171 SkScalar dx = length / (count - 1);
172 SkFixed fdx = SkScalarToFixed(dx);
173
174 const void* row = fStrategy.row((int)std::floor(Y(start)));
175 Next* next = fNext;
176
177 int ix = SkFixedFloorToInt(fx);
178 int prevIX = ix;
herbdd964892016-05-11 10:39:55 -0700179 Sk4f fpixel = fStrategy.getPixelFromRow(row, ix);
herb6eff52a2016-03-23 09:00:33 -0700180
181 // When dx is less than one, each pixel is used more than once. Using the fixed point fx
182 // allows the code to quickly check that the same pixel is being used. The code uses this
183 // same pixel check to do the sRGB and normalization only once.
184 auto getNextPixel = [&]() {
185 if (ix != prevIX) {
herbdd964892016-05-11 10:39:55 -0700186 fpixel = fStrategy.getPixelFromRow(row, ix);
herb6eff52a2016-03-23 09:00:33 -0700187 prevIX = ix;
188 }
189 fx += fdx;
190 ix = SkFixedFloorToInt(fx);
191 return fpixel;
192 };
193
194 while (count >= 4) {
195 Sk4f px0 = getNextPixel();
196 Sk4f px1 = getNextPixel();
197 Sk4f px2 = getNextPixel();
198 Sk4f px3 = getNextPixel();
herb9e0efe52016-04-08 13:25:28 -0700199 next->blend4Pixels(px0, px1, px2, px3);
herb6eff52a2016-03-23 09:00:33 -0700200 count -= 4;
201 }
202 while (count > 0) {
herb9e0efe52016-04-08 13:25:28 -0700203 next->blendPixel(getNextPixel());
herb6eff52a2016-03-23 09:00:33 -0700204 count -= 1;
205 }
206 }
207
208 // We're moving through source space at a rate of 1 source pixel per 1 dst pixel.
209 // We'll never re-use pixels, but we can at least load contiguous pixels.
210 void nearestSpanUnitRate(Span span) {
211 SkPoint start;
212 SkScalar length;
213 int count;
214 std::tie(start, length, count) = span;
215 int ix = SkScalarFloorToInt(X(start));
216 const void* row = fStrategy.row((int)std::floor(Y(start)));
217 Next* next = fNext;
218 if (length > 0) {
219 while (count >= 4) {
220 Sk4f px0, px1, px2, px3;
221 fStrategy.get4Pixels(row, ix, &px0, &px1, &px2, &px3);
herb9e0efe52016-04-08 13:25:28 -0700222 next->blend4Pixels(px0, px1, px2, px3);
herb6eff52a2016-03-23 09:00:33 -0700223 ix += 4;
224 count -= 4;
225 }
226
227 while (count > 0) {
herbdd964892016-05-11 10:39:55 -0700228 next->blendPixel(fStrategy.getPixelFromRow(row, ix));
herb6eff52a2016-03-23 09:00:33 -0700229 ix += 1;
230 count -= 1;
231 }
232 } else {
233 while (count >= 4) {
234 Sk4f px0, px1, px2, px3;
235 fStrategy.get4Pixels(row, ix - 3, &px3, &px2, &px1, &px0);
herb9e0efe52016-04-08 13:25:28 -0700236 next->blend4Pixels(px0, px1, px2, px3);
herb6eff52a2016-03-23 09:00:33 -0700237 ix -= 4;
238 count -= 4;
239 }
240
241 while (count > 0) {
herbdd964892016-05-11 10:39:55 -0700242 next->blendPixel(fStrategy.getPixelFromRow(row, ix));
herb6eff52a2016-03-23 09:00:33 -0700243 ix -= 1;
244 count -= 1;
245 }
246 }
247 }
248
249 // We're moving through source space faster than dst (zoomed out),
250 // so we'll never reuse a source pixel or be able to do contiguous loads.
251 void nearestSpanFastRate(Span span) {
252 struct NearestWrapper {
253 void VECTORCALL pointListFew(int n, Sk4s xs, Sk4s ys) {
254 fSampler.nearestListFew(n, xs, ys);
255 }
256
257 void VECTORCALL pointList4(Sk4s xs, Sk4s ys) {
258 fSampler.nearestList4(xs, ys);
259 }
260
261 GeneralSampler& fSampler;
262 };
263 NearestWrapper wrapper{*this};
264 span_fallback(span, &wrapper);
265 }
266
267 void bilerpSpanZeroRate(Span span, SkScalar y1) {
268 SkScalar y0 = span.startY() - 0.5f;
269 y1 += 0.5f;
270 int iy0 = SkScalarFloorToInt(y0);
271 SkScalar filterY1 = y0 - iy0;
272 SkScalar filterY0 = 1.0f - filterY1;
273 int iy1 = SkScalarFloorToInt(y1);
274 int ix = SkScalarFloorToInt(span.startX());
herbdd964892016-05-11 10:39:55 -0700275 Sk4f pixelY0 = fStrategy.getPixelFromRow(fStrategy.row(iy0), ix);
276 Sk4f pixelY1 = fStrategy.getPixelFromRow(fStrategy.row(iy1), ix);
herb6eff52a2016-03-23 09:00:33 -0700277 Sk4f filterPixel = pixelY0 * filterY0 + pixelY1 * filterY1;
278 int count = span.count();
279 while (count >= 4) {
herb9e0efe52016-04-08 13:25:28 -0700280 fNext->blend4Pixels(filterPixel, filterPixel, filterPixel, filterPixel);
herb6eff52a2016-03-23 09:00:33 -0700281 count -= 4;
282 }
283 while (count > 0) {
herb9e0efe52016-04-08 13:25:28 -0700284 fNext->blendPixel(filterPixel);
herb6eff52a2016-03-23 09:00:33 -0700285 count -= 1;
286 }
287 }
288
289 // When moving through source space more slowly than dst space (zoomed in),
290 // we'll be sampling from the same source pixel more than once.
291 void bilerpSpanSlowRate(Span span, SkScalar ry1) {
292 SkPoint start;
293 SkScalar length;
294 int count;
295 std::tie(start, length, count) = span;
296 SkFixed fx = SkScalarToFixed(X(start)
297 -0.5f);
298
299 SkFixed fdx = SkScalarToFixed(length / (count - 1));
300 //start = start + SkPoint{-0.5f, -0.5f};
301
302 Sk4f xAdjust;
303 if (fdx >= 0) {
304 xAdjust = Sk4f{-1.0f};
305 } else {
306 xAdjust = Sk4f{1.0f};
307 }
308 int ix = SkFixedFloorToInt(fx);
309 int ioldx = ix;
310 Sk4f x{SkFixedToScalar(fx) - ix};
311 Sk4f dx{SkFixedToScalar(fdx)};
312 SkScalar ry0 = Y(start) - 0.5f;
313 ry1 += 0.5f;
314 SkScalar yFloor = std::floor(ry0);
315 Sk4f y1 = Sk4f{ry0 - yFloor};
316 Sk4f y0 = Sk4f{1.0f} - y1;
herb222f8ff2016-03-23 15:14:23 -0700317 const void* const row0 = fStrategy.row(SkScalarFloorToInt(ry0));
318 const void* const row1 = fStrategy.row(SkScalarFloorToInt(ry1));
herbdd964892016-05-11 10:39:55 -0700319 Sk4f fpixel00 = y0 * fStrategy.getPixelFromRow(row0, ix);
320 Sk4f fpixel01 = y1 * fStrategy.getPixelFromRow(row1, ix);
321 Sk4f fpixel10 = y0 * fStrategy.getPixelFromRow(row0, ix + 1);
322 Sk4f fpixel11 = y1 * fStrategy.getPixelFromRow(row1, ix + 1);
herb6eff52a2016-03-23 09:00:33 -0700323 auto getNextPixel = [&]() {
324 if (ix != ioldx) {
325 fpixel00 = fpixel10;
326 fpixel01 = fpixel11;
herbdd964892016-05-11 10:39:55 -0700327 fpixel10 = y0 * fStrategy.getPixelFromRow(row0, ix + 1);
328 fpixel11 = y1 * fStrategy.getPixelFromRow(row1, ix + 1);
herb6eff52a2016-03-23 09:00:33 -0700329 ioldx = ix;
330 x = x + xAdjust;
331 }
332
333 Sk4f x0, x1;
334 x0 = Sk4f{1.0f} - x;
335 x1 = x;
336 Sk4f fpixel = x0 * (fpixel00 + fpixel01) + x1 * (fpixel10 + fpixel11);
337 fx += fdx;
338 ix = SkFixedFloorToInt(fx);
339 x = x + dx;
340 return fpixel;
341 };
342
343 while (count >= 4) {
344 Sk4f fpixel0 = getNextPixel();
345 Sk4f fpixel1 = getNextPixel();
346 Sk4f fpixel2 = getNextPixel();
347 Sk4f fpixel3 = getNextPixel();
348
herb9e0efe52016-04-08 13:25:28 -0700349 fNext->blend4Pixels(fpixel0, fpixel1, fpixel2, fpixel3);
herb6eff52a2016-03-23 09:00:33 -0700350 count -= 4;
351 }
352
353 while (count > 0) {
herb9e0efe52016-04-08 13:25:28 -0700354 fNext->blendPixel(getNextPixel());
herb6eff52a2016-03-23 09:00:33 -0700355
356 count -= 1;
357 }
358 }
359
360 // We're moving through source space at a rate of 1 source pixel per 1 dst pixel.
361 // We'll never re-use pixels, but we can at least load contiguous pixels.
362 void bilerpSpanUnitRate(Span span, SkScalar y1) {
363 y1 += 0.5f;
364 SkScalar y0 = span.startY() - 0.5f;
365 int iy0 = SkScalarFloorToInt(y0);
366 SkScalar filterY1 = y0 - iy0;
367 SkScalar filterY0 = 1.0f - filterY1;
368 int iy1 = SkScalarFloorToInt(y1);
369 const void* rowY0 = fStrategy.row(iy0);
370 const void* rowY1 = fStrategy.row(iy1);
371 SkScalar x0 = span.startX() - 0.5f;
372 int ix0 = SkScalarFloorToInt(x0);
373 SkScalar filterX1 = x0 - ix0;
374 SkScalar filterX0 = 1.0f - filterX1;
375
376 auto getPixelY0 = [&]() {
herbdd964892016-05-11 10:39:55 -0700377 Sk4f px = fStrategy.getPixelFromRow(rowY0, ix0);
herb6eff52a2016-03-23 09:00:33 -0700378 return px * filterY0;
379 };
380
381 auto getPixelY1 = [&]() {
herbdd964892016-05-11 10:39:55 -0700382 Sk4f px = fStrategy.getPixelFromRow(rowY1, ix0);
herb6eff52a2016-03-23 09:00:33 -0700383 return px * filterY1;
384 };
385
386 auto get4PixelsY0 = [&](int ix, Sk4f* px0, Sk4f* px1, Sk4f* px2, Sk4f* px3) {
387 fStrategy.get4Pixels(rowY0, ix, px0, px1, px2, px3);
388 *px0 = *px0 * filterY0;
389 *px1 = *px1 * filterY0;
390 *px2 = *px2 * filterY0;
391 *px3 = *px3 * filterY0;
392 };
393
394 auto get4PixelsY1 = [&](int ix, Sk4f* px0, Sk4f* px1, Sk4f* px2, Sk4f* px3) {
395 fStrategy.get4Pixels(rowY1, ix, px0, px1, px2, px3);
396 *px0 = *px0 * filterY1;
397 *px1 = *px1 * filterY1;
398 *px2 = *px2 * filterY1;
399 *px3 = *px3 * filterY1;
400 };
401
402 auto lerp = [&](Sk4f& pixelX0, Sk4f& pixelX1) {
403 return pixelX0 * filterX0 + pixelX1 * filterX1;
404 };
405
406 // Mid making 4 unit rate.
407 Sk4f pxB = getPixelY0() + getPixelY1();
408 if (span.length() > 0) {
409 int count = span.count();
410 while (count >= 4) {
411 Sk4f px00, px10, px20, px30;
412 get4PixelsY0(ix0, &px00, &px10, &px20, &px30);
413 Sk4f px01, px11, px21, px31;
414 get4PixelsY1(ix0, &px01, &px11, &px21, &px31);
415 Sk4f pxS0 = px00 + px01;
416 Sk4f px0 = lerp(pxB, pxS0);
417 Sk4f pxS1 = px10 + px11;
418 Sk4f px1 = lerp(pxS0, pxS1);
419 Sk4f pxS2 = px20 + px21;
420 Sk4f px2 = lerp(pxS1, pxS2);
421 Sk4f pxS3 = px30 + px31;
422 Sk4f px3 = lerp(pxS2, pxS3);
423 pxB = pxS3;
herb9e0efe52016-04-08 13:25:28 -0700424 fNext->blend4Pixels(px0, px1, px2, px3);
herb6eff52a2016-03-23 09:00:33 -0700425 ix0 += 4;
426 count -= 4;
427 }
428 while (count > 0) {
herbdd964892016-05-11 10:39:55 -0700429 Sk4f pixelY0 = fStrategy.getPixelFromRow(rowY0, ix0);
430 Sk4f pixelY1 = fStrategy.getPixelFromRow(rowY1, ix0);
herb6eff52a2016-03-23 09:00:33 -0700431
herb9e0efe52016-04-08 13:25:28 -0700432 fNext->blendPixel(lerp(pixelY0, pixelY1));
herb6eff52a2016-03-23 09:00:33 -0700433 ix0 += 1;
434 count -= 1;
435 }
436 } else {
437 int count = span.count();
438 while (count >= 4) {
439 Sk4f px00, px10, px20, px30;
440 get4PixelsY0(ix0 - 3, &px00, &px10, &px20, &px30);
441 Sk4f px01, px11, px21, px31;
442 get4PixelsY1(ix0 - 3, &px01, &px11, &px21, &px31);
443 Sk4f pxS3 = px30 + px31;
444 Sk4f px0 = lerp(pxS3, pxB);
445 Sk4f pxS2 = px20 + px21;
446 Sk4f px1 = lerp(pxS2, pxS3);
447 Sk4f pxS1 = px10 + px11;
448 Sk4f px2 = lerp(pxS1, pxS2);
449 Sk4f pxS0 = px00 + px01;
450 Sk4f px3 = lerp(pxS0, pxS1);
451 pxB = pxS0;
herb9e0efe52016-04-08 13:25:28 -0700452 fNext->blend4Pixels(px0, px1, px2, px3);
herb6eff52a2016-03-23 09:00:33 -0700453 ix0 -= 4;
454 count -= 4;
455 }
456 while (count > 0) {
herbdd964892016-05-11 10:39:55 -0700457 Sk4f pixelY0 = fStrategy.getPixelFromRow(rowY0, ix0);
458 Sk4f pixelY1 = fStrategy.getPixelFromRow(rowY1, ix0);
herb6eff52a2016-03-23 09:00:33 -0700459
herb9e0efe52016-04-08 13:25:28 -0700460 fNext->blendPixel(lerp(pixelY0, pixelY1));
herb6eff52a2016-03-23 09:00:33 -0700461 ix0 -= 1;
462 count -= 1;
463 }
464 }
465 }
466
467 void bilerpSpanUnitRateAlignedX(Span span, SkScalar y1) {
468 SkScalar y0 = span.startY() - 0.5f;
469 y1 += 0.5f;
470 int iy0 = SkScalarFloorToInt(y0);
471 SkScalar filterY1 = y0 - iy0;
472 SkScalar filterY0 = 1.0f - filterY1;
473 int iy1 = SkScalarFloorToInt(y1);
474 int ix = SkScalarFloorToInt(span.startX());
475 const void* rowY0 = fStrategy.row(iy0);
476 const void* rowY1 = fStrategy.row(iy1);
477 auto lerp = [&](Sk4f* pixelY0, Sk4f* pixelY1) {
478 return *pixelY0 * filterY0 + *pixelY1 * filterY1;
479 };
480
481 if (span.length() > 0) {
482 int count = span.count();
483 while (count >= 4) {
484 Sk4f px00, px10, px20, px30;
485 fStrategy.get4Pixels(rowY0, ix, &px00, &px10, &px20, &px30);
486 Sk4f px01, px11, px21, px31;
487 fStrategy.get4Pixels(rowY1, ix, &px01, &px11, &px21, &px31);
herb9e0efe52016-04-08 13:25:28 -0700488 fNext->blend4Pixels(
herb6eff52a2016-03-23 09:00:33 -0700489 lerp(&px00, &px01), lerp(&px10, &px11), lerp(&px20, &px21), lerp(&px30, &px31));
490 ix += 4;
491 count -= 4;
492 }
493 while (count > 0) {
herbdd964892016-05-11 10:39:55 -0700494 Sk4f pixelY0 = fStrategy.getPixelFromRow(rowY0, ix);
495 Sk4f pixelY1 = fStrategy.getPixelFromRow(rowY1, ix);
herb6eff52a2016-03-23 09:00:33 -0700496
herb9e0efe52016-04-08 13:25:28 -0700497 fNext->blendPixel(lerp(&pixelY0, &pixelY1));
herb6eff52a2016-03-23 09:00:33 -0700498 ix += 1;
499 count -= 1;
500 }
501 } else {
502 int count = span.count();
503 while (count >= 4) {
504 Sk4f px00, px10, px20, px30;
505 fStrategy.get4Pixels(rowY0, ix - 3, &px30, &px20, &px10, &px00);
506 Sk4f px01, px11, px21, px31;
507 fStrategy.get4Pixels(rowY1, ix - 3, &px31, &px21, &px11, &px01);
herb9e0efe52016-04-08 13:25:28 -0700508 fNext->blend4Pixels(
herb6eff52a2016-03-23 09:00:33 -0700509 lerp(&px00, &px01), lerp(&px10, &px11), lerp(&px20, &px21), lerp(&px30, &px31));
510 ix -= 4;
511 count -= 4;
512 }
513 while (count > 0) {
herbdd964892016-05-11 10:39:55 -0700514 Sk4f pixelY0 = fStrategy.getPixelFromRow(rowY0, ix);
515 Sk4f pixelY1 = fStrategy.getPixelFromRow(rowY1, ix);
herb6eff52a2016-03-23 09:00:33 -0700516
herb9e0efe52016-04-08 13:25:28 -0700517 fNext->blendPixel(lerp(&pixelY0, &pixelY1));
herb6eff52a2016-03-23 09:00:33 -0700518 ix -= 1;
519 count -= 1;
520 }
521 }
522 }
523
524 // We're moving through source space faster than dst (zoomed out),
525 // so we'll never reuse a source pixel or be able to do contiguous loads.
526 void bilerpSpanFastRate(Span span, SkScalar y1) {
527 SkPoint start;
528 SkScalar length;
529 int count;
530 std::tie(start, length, count) = span;
531 SkScalar x = X(start);
532 SkScalar y = Y(start);
533 if (false && y == y1) {
534 struct BilerpWrapper {
535 void VECTORCALL pointListFew(int n, Sk4s xs, Sk4s ys) {
536 fSampler.bilerpListFew(n, xs, ys);
537 }
538
539 void VECTORCALL pointList4(Sk4s xs, Sk4s ys) {
540 fSampler.bilerpList4(xs, ys);
541 }
542
543 GeneralSampler& fSampler;
544 };
545 BilerpWrapper wrapper{*this};
546 span_fallback(span, &wrapper);
547 } else {
548 SkScalar dx = length / (count - 1);
549 Sk4f ys = {y - 0.5f, y - 0.5f, y1 + 0.5f, y1 + 0.5f};
550 while (count > 0) {
551 Sk4f xs = Sk4f{-0.5f, 0.5f, -0.5f, 0.5f} + Sk4f{x};
552 this->bilerpEdge(xs, ys);
553 x += dx;
554 count -= 1;
555 }
556 }
557 }
558
559 Next* const fNext;
560 SourceStrategy fStrategy;
561};
562
herbdd964892016-05-11 10:39:55 -0700563template <typename PixelGetter>
564class PixelAccessor {
herb6eff52a2016-03-23 09:00:33 -0700565public:
herbdd964892016-05-11 10:39:55 -0700566 PixelAccessor(const SkPixmap& srcPixmap)
567 : fWidth{srcPixmap.rowBytesAsPixels()}
568 , fGetter{srcPixmap} { }
herb6eff52a2016-03-23 09:00:33 -0700569
570 void VECTORCALL getFewPixels(int n, Sk4s xs, Sk4s ys, Sk4f* px0, Sk4f* px1, Sk4f* px2) {
571 Sk4i XIs = SkNx_cast<int, SkScalar>(xs);
572 Sk4i YIs = SkNx_cast<int, SkScalar>(ys);
573 Sk4i bufferLoc = YIs * fWidth + XIs;
574 switch (n) {
575 case 3:
herbdd964892016-05-11 10:39:55 -0700576 *px2 = fGetter.getPixelAt(bufferLoc[2]);
herb6eff52a2016-03-23 09:00:33 -0700577 case 2:
herbdd964892016-05-11 10:39:55 -0700578 *px1 = fGetter.getPixelAt(bufferLoc[1]);
herb6eff52a2016-03-23 09:00:33 -0700579 case 1:
herbdd964892016-05-11 10:39:55 -0700580 *px0 = fGetter.getPixelAt(bufferLoc[0]);
herb6eff52a2016-03-23 09:00:33 -0700581 default:
582 break;
583 }
584 }
585
586 void VECTORCALL get4Pixels(Sk4s xs, Sk4s ys, Sk4f* px0, Sk4f* px1, Sk4f* px2, Sk4f* px3) {
587 Sk4i XIs = SkNx_cast<int, SkScalar>(xs);
588 Sk4i YIs = SkNx_cast<int, SkScalar>(ys);
589 Sk4i bufferLoc = YIs * fWidth + XIs;
herbdd964892016-05-11 10:39:55 -0700590 *px0 = fGetter.getPixelAt(bufferLoc[0]);
591 *px1 = fGetter.getPixelAt(bufferLoc[1]);
592 *px2 = fGetter.getPixelAt(bufferLoc[2]);
593 *px3 = fGetter.getPixelAt(bufferLoc[3]);
herb6eff52a2016-03-23 09:00:33 -0700594 }
595
herbdd964892016-05-11 10:39:55 -0700596 void get4Pixels(const void* src, int index, Sk4f* px0, Sk4f* px1, Sk4f* px2, Sk4f* px3) {
597 *px0 = fGetter.getPixelFromRow(src, index + 0);
598 *px1 = fGetter.getPixelFromRow(src, index + 1);
599 *px2 = fGetter.getPixelFromRow(src, index + 2);
600 *px3 = fGetter.getPixelFromRow(src, index + 3);
herb6eff52a2016-03-23 09:00:33 -0700601 }
602
herbdd964892016-05-11 10:39:55 -0700603 Sk4f getPixelFromRow(const void* row, int index) {
604 return fGetter.getPixelFromRow(row, index);
605 }
606
607 const void* row(int y) { return fGetter.row(y); }
608
609private:
610 const Sk4i fWidth;
611 PixelGetter fGetter;
612};
613
614template <SkColorType colorType, SkColorProfileType colorProfile> class PixelGetter;
615
616template <SkColorProfileType colorProfile>
617class PixelGetter<kRGBA_8888_SkColorType, colorProfile> {
618public:
619 PixelGetter(const SkPixmap& srcPixmap)
620 : fSrc{srcPixmap.addr32()}
621 , fWidth{srcPixmap.rowBytesAsPixels()} { }
622
623 Sk4f getPixelFromRow(const void* row, int index) {
624 const uint32_t* src = static_cast<const uint32_t*>(row);
herbcf05dcd2016-05-11 11:53:36 -0700625 return colorProfile == kSRGB_SkColorProfileType
626 ? Sk4f_fromS32(*src)
627 : Sk4f_fromL32(*src);
herb6eff52a2016-03-23 09:00:33 -0700628 }
629
herbdd964892016-05-11 10:39:55 -0700630 Sk4f getPixelAt(int index) {
631 return this->getPixelFromRow(fSrc, index);
632 }
633
634 const void* row(int y) { return fSrc + y * fWidth; }
herb6eff52a2016-03-23 09:00:33 -0700635
636private:
637 const uint32_t* const fSrc;
herbdd964892016-05-11 10:39:55 -0700638 const int fWidth;
herb6eff52a2016-03-23 09:00:33 -0700639};
herbdd964892016-05-11 10:39:55 -0700640
641using Pixel8888SRGB = PixelAccessor<PixelGetter<kRGBA_8888_SkColorType, kSRGB_SkColorProfileType>>;
642using Pixel8888LRGB = PixelAccessor<PixelGetter<kRGBA_8888_SkColorType, kLinear_SkColorProfileType>>;
herb222f8ff2016-03-23 15:14:23 -0700643
644template <SkColorProfileType colorProfile>
herbdd964892016-05-11 10:39:55 -0700645class PixelGetter<kBGRA_8888_SkColorType, colorProfile> {
herb222f8ff2016-03-23 15:14:23 -0700646public:
herbdd964892016-05-11 10:39:55 -0700647 PixelGetter(const SkPixmap& srcPixmap)
648 : fSrc{srcPixmap.addr32()}
649 , fWidth{srcPixmap.rowBytesAsPixels()} { }
650
651 Sk4f getPixelFromRow(const void* row, int index) {
652 const uint32_t* src = static_cast<const uint32_t*>(row);
herbcf05dcd2016-05-11 11:53:36 -0700653 Sk4f pixel = colorProfile == kSRGB_SkColorProfileType
654 ? Sk4f_fromS32(*src)
655 : Sk4f_fromL32(*src);
656 return SkNx_shuffle<2, 1, 0, 3>(pixel);
herbdd964892016-05-11 10:39:55 -0700657 }
658
659 Sk4f getPixelAt(int index) {
660 return this->getPixelFromRow(fSrc, index);
661 }
662
663 const void* row(int y) { return fSrc + y * fWidth; }
664
665private:
666 const uint32_t* const fSrc;
667 const int fWidth;
668};
669
670using Pixel8888SBGR = PixelAccessor<PixelGetter<kBGRA_8888_SkColorType, kSRGB_SkColorProfileType>>;
671using Pixel8888LBGR = PixelAccessor<PixelGetter<kBGRA_8888_SkColorType, kLinear_SkColorProfileType>>;
672
673template <SkColorProfileType colorProfile>
674class PixelGetter<kIndex_8_SkColorType, colorProfile> {
675public:
676 PixelGetter(const SkPixmap& srcPixmap)
herb222f8ff2016-03-23 15:14:23 -0700677 : fSrc{srcPixmap.addr8()}, fWidth{static_cast<int>(srcPixmap.rowBytes())} {
678 SkASSERT(srcPixmap.colorType() == kIndex_8_SkColorType);
679 SkColorTable* skColorTable = srcPixmap.ctable();
680 SkASSERT(skColorTable != nullptr);
681
682 fColorTable = (Sk4f*)SkAlign16((intptr_t)fColorTableStorage.get());
683 for (int i = 0; i < skColorTable->count(); i++) {
684 fColorTable[i] = this->convertPixel((*skColorTable)[i]);
685 }
686 }
687
herbdd964892016-05-11 10:39:55 -0700688 PixelGetter(const PixelGetter& strategy)
herb9e0efe52016-04-08 13:25:28 -0700689 : fSrc{strategy.fSrc}, fWidth{strategy.fWidth} {
690 fColorTable = (Sk4f*)SkAlign16((intptr_t)fColorTableStorage.get());
691 // TODO: figure out the count.
692 for (int i = 0; i < 256; i++) {
693 fColorTable[i] = strategy.fColorTable[i];
694 }
695 }
696
herbdd964892016-05-11 10:39:55 -0700697 Sk4f getPixelFromRow(const void* row, int index) {
698 const uint8_t* src = static_cast<const uint8_t*>(row);
herb222f8ff2016-03-23 15:14:23 -0700699 Sk4f pixel = fColorTable[*src];
700 return pixel;
701 }
702
herbdd964892016-05-11 10:39:55 -0700703 Sk4f getPixelAt(int index) {
704 return this->getPixelFromRow(fSrc, index);
705 }
706
707 const void* row(int y) { return fSrc + y * fWidth; }
herb222f8ff2016-03-23 15:14:23 -0700708
709private:
710 static const size_t kColorTableSize = sizeof(Sk4f[256]) + 12;
711 Sk4f convertPixel(SkPMColor pmColor) {
herbcf05dcd2016-05-11 11:53:36 -0700712 Sk4f pixel = to_4f(pmColor);
713 float alpha = get_alpha(pixel);
herb222f8ff2016-03-23 15:14:23 -0700714 if (alpha != 0.0f) {
herbcf05dcd2016-05-11 11:53:36 -0700715 float invAlpha = 1.0f / alpha;
herb222f8ff2016-03-23 15:14:23 -0700716 Sk4f normalize = {invAlpha, invAlpha, invAlpha, 1.0f / 255.0f};
717 pixel = pixel * normalize;
718 if (colorProfile == kSRGB_SkColorProfileType) {
herbcf05dcd2016-05-11 11:53:36 -0700719 pixel = linear_to_srgb(pixel);
herb222f8ff2016-03-23 15:14:23 -0700720 }
721 return pixel;
722 } else {
723 return Sk4f{0.0f};
724 }
725 }
726 const uint8_t* const fSrc;
herbdd964892016-05-11 10:39:55 -0700727 const int fWidth;
herb222f8ff2016-03-23 15:14:23 -0700728 SkAutoMalloc fColorTableStorage{kColorTableSize};
729 Sk4f* fColorTable;
730};
731
herbdd964892016-05-11 10:39:55 -0700732using PixelIndex8SRGB = PixelAccessor<PixelGetter<kIndex_8_SkColorType, kSRGB_SkColorProfileType>>;
733using PixelIndex8LRGB = PixelAccessor<PixelGetter<kIndex_8_SkColorType, kLinear_SkColorProfileType>>;
herb222f8ff2016-03-23 15:14:23 -0700734
herbdd964892016-05-11 10:39:55 -0700735template <>
736class PixelGetter<kRGBA_F16_SkColorType, kLinear_SkColorProfileType> {
herbd5f2e2e2016-04-14 11:16:44 -0700737public:
herbdd964892016-05-11 10:39:55 -0700738 PixelGetter(const SkPixmap& srcPixmap)
herbd5f2e2e2016-04-14 11:16:44 -0700739 : fSrc{srcPixmap.addr64()}
herbdd964892016-05-11 10:39:55 -0700740 , fWidth{static_cast<int>(srcPixmap.rowBytesAsPixels())} { }
herbd5f2e2e2016-04-14 11:16:44 -0700741
herbdd964892016-05-11 10:39:55 -0700742 Sk4f getPixelFromRow(const void* row, int index) {
743 const uint64_t* src = static_cast<const uint64_t*>(row) + index;
herbd5f2e2e2016-04-14 11:16:44 -0700744 return SkHalfToFloat_01(*src);
745 }
746
herbdd964892016-05-11 10:39:55 -0700747 Sk4f getPixelAt(int index) {
748 return this->getPixelFromRow(fSrc, index);
749 }
750
751 const void* row(int y) { return fSrc + y * fWidth; }
herbd5f2e2e2016-04-14 11:16:44 -0700752
753private:
754 const uint64_t* const fSrc;
herbdd964892016-05-11 10:39:55 -0700755 const int fWidth;
herbd5f2e2e2016-04-14 11:16:44 -0700756};
757
herbdd964892016-05-11 10:39:55 -0700758using PixelHalfLinear = PixelAccessor<PixelGetter<kRGBA_F16_SkColorType, kLinear_SkColorProfileType>>;
759
herb6eff52a2016-03-23 09:00:33 -0700760} // namespace
761
762#endif // SkLinearBitmapPipeline_sampler_DEFINED