1 /*
2  * Copyright 2015 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 #include "SkTableColorFilter.h"
9 #include "SkPM4f.h"
10 #include "SkArenaAlloc.h"
11 #include "SkBitmap.h"
12 #include "SkColorData.h"
13 #include "SkRasterPipeline.h"
14 #include "SkReadBuffer.h"
15 #include "SkString.h"
16 #include "SkUnPreMultiply.h"
17 #include "SkWriteBuffer.h"
18 
19 static const uint8_t gIdentityTable[] = {
20     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
21     0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
22     0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
23     0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
24     0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
25     0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
26     0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
27     0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
28     0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
29     0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
30     0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
31     0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
32     0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
33     0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
34     0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
35     0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
36     0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
37     0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
38     0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
39     0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
40     0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
41     0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
42     0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7,
43     0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
44     0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
45     0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
46     0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7,
47     0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
48     0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
49     0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
50     0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
51     0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
52 };
53 
54 class SkTable_ColorFilter : public SkColorFilter {
55 public:
SkTable_ColorFilter(const uint8_t tableA[],const uint8_t tableR[],const uint8_t tableG[],const uint8_t tableB[])56     SkTable_ColorFilter(const uint8_t tableA[], const uint8_t tableR[],
57                         const uint8_t tableG[], const uint8_t tableB[]) {
58         fBitmap = nullptr;
59         fFlags = 0;
60 
61         uint8_t* dst = fStorage;
62         if (tableA) {
63             memcpy(dst, tableA, 256);
64             dst += 256;
65             fFlags |= kA_Flag;
66         }
67         if (tableR) {
68             memcpy(dst, tableR, 256);
69             dst += 256;
70             fFlags |= kR_Flag;
71         }
72         if (tableG) {
73             memcpy(dst, tableG, 256);
74             dst += 256;
75             fFlags |= kG_Flag;
76         }
77         if (tableB) {
78             memcpy(dst, tableB, 256);
79             fFlags |= kB_Flag;
80         }
81     }
82 
~SkTable_ColorFilter()83     ~SkTable_ColorFilter() override { delete fBitmap; }
84 
85     bool asComponentTable(SkBitmap* table) const override;
86     sk_sp<SkColorFilter> onMakeComposed(sk_sp<SkColorFilter> inner) const override;
87 
88 #if SK_SUPPORT_GPU
89     std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(
90             GrContext*, const GrColorSpaceInfo&) const override;
91 #endif
92 
93     SK_TO_STRING_OVERRIDE()
94 
95     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTable_ColorFilter)
96 
97     enum {
98         kA_Flag = 1 << 0,
99         kR_Flag = 1 << 1,
100         kG_Flag = 1 << 2,
101         kB_Flag = 1 << 3,
102     };
103 
onAppendStages(SkRasterPipeline * p,SkColorSpace *,SkArenaAlloc * alloc,bool shaderIsOpaque) const104     void onAppendStages(SkRasterPipeline* p, SkColorSpace*, SkArenaAlloc* alloc,
105                         bool shaderIsOpaque) const override {
106         const uint8_t *r = gIdentityTable,
107                       *g = gIdentityTable,
108                       *b = gIdentityTable,
109                       *a = gIdentityTable;
110         const uint8_t* ptr = fStorage;
111         if (fFlags & kA_Flag) { a = ptr; ptr += 256; }
112         if (fFlags & kR_Flag) { r = ptr; ptr += 256; }
113         if (fFlags & kG_Flag) { g = ptr; ptr += 256; }
114         if (fFlags & kB_Flag) { b = ptr;             }
115 
116         if (!shaderIsOpaque) {
117             p->append(SkRasterPipeline::unpremul);
118         }
119 
120         struct Tables { const uint8_t *r, *g, *b, *a; };
121         p->append(SkRasterPipeline::byte_tables, alloc->make<Tables>(Tables{r,g,b,a}));
122 
123         bool definitelyOpaque = shaderIsOpaque && a[0xff] == 0xff;
124         if (!definitelyOpaque) {
125             p->append(SkRasterPipeline::premul);
126         }
127     }
128 
129 protected:
130     void flatten(SkWriteBuffer&) const override;
131 
132 private:
133     mutable const SkBitmap* fBitmap; // lazily allocated
134 
135     uint8_t fStorage[256 * 4];
136     unsigned fFlags;
137 
138     friend class SkTableColorFilter;
139 
140     typedef SkColorFilter INHERITED;
141 };
142 
143 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const144 void SkTable_ColorFilter::toString(SkString* str) const {
145     const uint8_t* table = fStorage;
146     const uint8_t* tableA = gIdentityTable;
147     const uint8_t* tableR = gIdentityTable;
148     const uint8_t* tableG = gIdentityTable;
149     const uint8_t* tableB = gIdentityTable;
150     if (fFlags & kA_Flag) {
151         tableA = table; table += 256;
152     }
153     if (fFlags & kR_Flag) {
154         tableR = table; table += 256;
155     }
156     if (fFlags & kG_Flag) {
157         tableG = table; table += 256;
158     }
159     if (fFlags & kB_Flag) {
160         tableB = table;
161     }
162 
163     str->append("SkTable_ColorFilter (");
164 
165     for (int i = 0; i < 256; ++i) {
166         str->appendf("%d: %d,%d,%d,%d\n",
167                      i, tableR[i], tableG[i], tableB[i], tableA[i]);
168     }
169 
170     str->append(")");
171 }
172 #endif
173 
174 static const uint8_t gCountNibBits[] = {
175     0, 1, 1, 2,
176     1, 2, 2, 3,
177     1, 2, 2, 3,
178     2, 3, 3, 4
179 };
180 
181 #include "SkPackBits.h"
182 
flatten(SkWriteBuffer & buffer) const183 void SkTable_ColorFilter::flatten(SkWriteBuffer& buffer) const {
184     uint8_t storage[5*256];
185     int count = gCountNibBits[fFlags & 0xF];
186     size_t size = SkPackBits::Pack8(fStorage, count * 256, storage,
187                                     sizeof(storage));
188 
189     buffer.write32(fFlags);
190     buffer.writeByteArray(storage, size);
191 }
192 
CreateProc(SkReadBuffer & buffer)193 sk_sp<SkFlattenable> SkTable_ColorFilter::CreateProc(SkReadBuffer& buffer) {
194     const int flags = buffer.read32();
195     const size_t count = gCountNibBits[flags & 0xF];
196     SkASSERT(count <= 4);
197 
198     uint8_t packedStorage[5*256];
199     size_t packedSize = buffer.getArrayCount();
200     if (!buffer.validate(packedSize <= sizeof(packedStorage))) {
201         return nullptr;
202     }
203     if (!buffer.readByteArray(packedStorage, packedSize)) {
204         return nullptr;
205     }
206 
207     uint8_t unpackedStorage[4*256];
208     size_t unpackedSize = SkPackBits::Unpack8(packedStorage, packedSize,
209                               unpackedStorage, sizeof(unpackedStorage));
210     // now check that we got the size we expected
211     if (!buffer.validate(unpackedSize == count*256)) {
212         return nullptr;
213     }
214 
215     const uint8_t* a = nullptr;
216     const uint8_t* r = nullptr;
217     const uint8_t* g = nullptr;
218     const uint8_t* b = nullptr;
219     const uint8_t* ptr = unpackedStorage;
220 
221     if (flags & kA_Flag) {
222         a = ptr;
223         ptr += 256;
224     }
225     if (flags & kR_Flag) {
226         r = ptr;
227         ptr += 256;
228     }
229     if (flags & kG_Flag) {
230         g = ptr;
231         ptr += 256;
232     }
233     if (flags & kB_Flag) {
234         b = ptr;
235         ptr += 256;
236     }
237     return SkTableColorFilter::MakeARGB(a, r, g, b);
238 }
239 
asComponentTable(SkBitmap * table) const240 bool SkTable_ColorFilter::asComponentTable(SkBitmap* table) const {
241     if (table) {
242         if (nullptr == fBitmap) {
243             SkBitmap* bmp = new SkBitmap;
244             bmp->allocPixels(SkImageInfo::MakeA8(256, 4));
245             uint8_t* bitmapPixels = bmp->getAddr8(0, 0);
246             int offset = 0;
247             static const unsigned kFlags[] = { kA_Flag, kR_Flag, kG_Flag, kB_Flag };
248 
249             for (int x = 0; x < 4; ++x) {
250                 if (!(fFlags & kFlags[x])) {
251                     memcpy(bitmapPixels, gIdentityTable, sizeof(gIdentityTable));
252                 } else {
253                     memcpy(bitmapPixels, fStorage + offset, 256);
254                     offset += 256;
255                 }
256                 bitmapPixels += 256;
257             }
258             fBitmap = bmp;
259         }
260         *table = *fBitmap;
261     }
262     return true;
263 }
264 
265 // Combines the two lookup tables so that making a lookup using res[] has
266 // the same effect as making a lookup through inner[] then outer[].
combine_tables(uint8_t res[256],const uint8_t outer[256],const uint8_t inner[256])267 static void combine_tables(uint8_t res[256], const uint8_t outer[256], const uint8_t inner[256]) {
268     for (int i = 0; i < 256; i++) {
269         res[i] = outer[inner[i]];
270     }
271 }
272 
onMakeComposed(sk_sp<SkColorFilter> innerFilter) const273 sk_sp<SkColorFilter> SkTable_ColorFilter::onMakeComposed(sk_sp<SkColorFilter> innerFilter) const {
274     SkBitmap innerBM;
275     if (!innerFilter->asComponentTable(&innerBM)) {
276         return nullptr;
277     }
278 
279     if (nullptr == innerBM.getPixels()) {
280         return nullptr;
281     }
282 
283     const uint8_t* table = fStorage;
284     const uint8_t* tableA = gIdentityTable;
285     const uint8_t* tableR = gIdentityTable;
286     const uint8_t* tableG = gIdentityTable;
287     const uint8_t* tableB = gIdentityTable;
288     if (fFlags & kA_Flag) {
289         tableA = table; table += 256;
290     }
291     if (fFlags & kR_Flag) {
292         tableR = table; table += 256;
293     }
294     if (fFlags & kG_Flag) {
295         tableG = table; table += 256;
296     }
297     if (fFlags & kB_Flag) {
298         tableB = table;
299     }
300 
301     uint8_t concatA[256];
302     uint8_t concatR[256];
303     uint8_t concatG[256];
304     uint8_t concatB[256];
305 
306     combine_tables(concatA, tableA, innerBM.getAddr8(0, 0));
307     combine_tables(concatR, tableR, innerBM.getAddr8(0, 1));
308     combine_tables(concatG, tableG, innerBM.getAddr8(0, 2));
309     combine_tables(concatB, tableB, innerBM.getAddr8(0, 3));
310 
311     return SkTableColorFilter::MakeARGB(concatA, concatR, concatG, concatB);
312 }
313 
314 #if SK_SUPPORT_GPU
315 
316 #include "GrColorSpaceInfo.h"
317 #include "GrContext.h"
318 #include "GrContextPriv.h"
319 #include "GrFragmentProcessor.h"
320 #include "GrTextureStripAtlas.h"
321 #include "SkGr.h"
322 #include "glsl/GrGLSLFragmentProcessor.h"
323 #include "glsl/GrGLSLFragmentShaderBuilder.h"
324 #include "glsl/GrGLSLProgramDataManager.h"
325 #include "glsl/GrGLSLUniformHandler.h"
326 
327 class ColorTableEffect : public GrFragmentProcessor {
328 public:
329     static std::unique_ptr<GrFragmentProcessor> Make(GrContext* context, const SkBitmap& bitmap);
330 
331     ~ColorTableEffect() override;
332 
name() const333     const char* name() const override { return "ColorTable"; }
334 
atlas() const335     const GrTextureStripAtlas* atlas() const { return fAtlas; }
atlasRow() const336     int atlasRow() const { return fRow; }
337 
338     std::unique_ptr<GrFragmentProcessor> clone() const override;
339 
340 private:
341     GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
342 
343     void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
344 
345     bool onIsEqual(const GrFragmentProcessor&) const override;
346 
347     ColorTableEffect(sk_sp<GrTextureProxy> proxy, GrTextureStripAtlas* atlas, int row);
348 
349     GR_DECLARE_FRAGMENT_PROCESSOR_TEST
350 
351     TextureSampler fTextureSampler;
352     GrTextureStripAtlas* fAtlas;
353     int fRow;
354 
355     typedef GrFragmentProcessor INHERITED;
356 };
357 
358 class GLColorTableEffect : public GrGLSLFragmentProcessor {
359 public:
360     void emitCode(EmitArgs&) override;
361 
GenKey(const GrProcessor &,const GrShaderCaps &,GrProcessorKeyBuilder *)362     static void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*) {}
363 
364 protected:
365     void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
366 
367 private:
368     UniformHandle fRGBAYValuesUni;
369     typedef GrGLSLFragmentProcessor INHERITED;
370 };
371 
onSetData(const GrGLSLProgramDataManager & pdm,const GrFragmentProcessor & proc)372 void GLColorTableEffect::onSetData(const GrGLSLProgramDataManager& pdm,
373                                    const GrFragmentProcessor& proc) {
374     // The textures are organized in a strip where the rows are ordered a, r, g, b.
375     float rgbaYValues[4];
376     const ColorTableEffect& cte = proc.cast<ColorTableEffect>();
377     if (cte.atlas()) {
378         SkScalar yDelta = cte.atlas()->getNormalizedTexelHeight();
379         rgbaYValues[3] = cte.atlas()->getYOffset(cte.atlasRow()) + SK_ScalarHalf * yDelta;
380         rgbaYValues[0] = rgbaYValues[3] + yDelta;
381         rgbaYValues[1] = rgbaYValues[0] + yDelta;
382         rgbaYValues[2] = rgbaYValues[1] + yDelta;
383     } else {
384         rgbaYValues[3] = 0.125;
385         rgbaYValues[0] = 0.375;
386         rgbaYValues[1] = 0.625;
387         rgbaYValues[2] = 0.875;
388     }
389     pdm.set4fv(fRGBAYValuesUni, 1, rgbaYValues);
390 }
391 
emitCode(EmitArgs & args)392 void GLColorTableEffect::emitCode(EmitArgs& args) {
393     const char* yoffsets;
394     fRGBAYValuesUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
395                                                        "yoffsets", &yoffsets);
396     static const float kColorScaleFactor = 255.0f / 256.0f;
397     static const float kColorOffsetFactor = 1.0f / 512.0f;
398     GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
399     if (nullptr == args.fInputColor) {
400         // the input color is solid white (all ones).
401         static const float kMaxValue = kColorScaleFactor + kColorOffsetFactor;
402         fragBuilder->codeAppendf("\t\thalf4 coord = half4(%f, %f, %f, %f);\n",
403                                  kMaxValue, kMaxValue, kMaxValue, kMaxValue);
404 
405     } else {
406         fragBuilder->codeAppendf("\t\thalf nonZeroAlpha = max(%s.a, .0001);\n", args.fInputColor);
407         fragBuilder->codeAppendf("\t\thalf4 coord = half4(%s.rgb / nonZeroAlpha, nonZeroAlpha);\n",
408                                  args.fInputColor);
409         fragBuilder->codeAppendf("\t\tcoord = coord * %f + half4(%f, %f, %f, %f);\n",
410                                  kColorScaleFactor,
411                                  kColorOffsetFactor, kColorOffsetFactor,
412                                  kColorOffsetFactor, kColorOffsetFactor);
413     }
414 
415     SkString coord;
416 
417     fragBuilder->codeAppendf("\t\t%s.a = ", args.fOutputColor);
418     coord.printf("half2(coord.a, %s.a)", yoffsets);
419     fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
420     fragBuilder->codeAppend(".a;\n");
421 
422     fragBuilder->codeAppendf("\t\t%s.r = ", args.fOutputColor);
423     coord.printf("half2(coord.r, %s.r)", yoffsets);
424     fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
425     fragBuilder->codeAppend(".a;\n");
426 
427     fragBuilder->codeAppendf("\t\t%s.g = ", args.fOutputColor);
428     coord.printf("half2(coord.g, %s.g)", yoffsets);
429     fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
430     fragBuilder->codeAppend(".a;\n");
431 
432     fragBuilder->codeAppendf("\t\t%s.b = ", args.fOutputColor);
433     coord.printf("half2(coord.b, %s.b)", yoffsets);
434     fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
435     fragBuilder->codeAppend(".a;\n");
436 
437     fragBuilder->codeAppendf("\t\t%s.rgb *= %s.a;\n", args.fOutputColor, args.fOutputColor);
438 }
439 
440 ///////////////////////////////////////////////////////////////////////////////
Make(GrContext * context,const SkBitmap & bitmap)441 std::unique_ptr<GrFragmentProcessor> ColorTableEffect::Make(GrContext* context,
442                                                             const SkBitmap& bitmap) {
443     GrTextureStripAtlas::Desc desc;
444     desc.fWidth  = bitmap.width();
445     desc.fHeight = 128;
446     desc.fRowHeight = bitmap.height();
447     // TODO: this seems a bit heavy handed (passing a GrContext as part of the desc)
448     desc.fContext = context;
449     desc.fConfig = SkImageInfo2GrPixelConfig(bitmap.info(), *context->caps());
450 
451     if (kUnknown_GrPixelConfig == desc.fConfig) {
452         return nullptr;
453     }
454 
455     GrTextureStripAtlas* atlas = GrTextureStripAtlas::GetAtlas(desc);
456     int row = atlas->lockRow(bitmap);
457     sk_sp<GrTextureProxy> proxy;
458     if (-1 == row) {
459         atlas = nullptr;
460 
461         SkASSERT(bitmap.isImmutable());
462 
463         sk_sp<SkImage> srcImage = SkImage::MakeFromBitmap(bitmap);
464         if (!srcImage) {
465             return nullptr;
466         }
467 
468         proxy = GrMakeCachedImageProxy(context->contextPriv().proxyProvider(), std::move(srcImage));
469     } else {
470         proxy = atlas->asTextureProxyRef();
471     }
472 
473     if (!proxy) {
474         return nullptr;
475     }
476 
477     return std::unique_ptr<GrFragmentProcessor>(new ColorTableEffect(std::move(proxy), atlas, row));
478 }
479 
ColorTableEffect(sk_sp<GrTextureProxy> proxy,GrTextureStripAtlas * atlas,int row)480 ColorTableEffect::ColorTableEffect(sk_sp<GrTextureProxy> proxy, GrTextureStripAtlas* atlas, int row)
481         : INHERITED(kColorTableEffect_ClassID,
482                     kNone_OptimizationFlags)  // Not bothering with table-specific optimizations.
483         , fTextureSampler(std::move(proxy))
484         , fAtlas(atlas)
485         , fRow(row) {
486     this->addTextureSampler(&fTextureSampler);
487 }
488 
~ColorTableEffect()489 ColorTableEffect::~ColorTableEffect() {
490     if (fAtlas) {
491         fAtlas->unlockRow(fRow);
492     }
493 }
494 
clone() const495 std::unique_ptr<GrFragmentProcessor> ColorTableEffect::clone() const {
496     fAtlas->lockRow(fRow);
497     return std::unique_ptr<GrFragmentProcessor>(
498             new ColorTableEffect(sk_ref_sp(fTextureSampler.proxy()), fAtlas, fRow));
499 }
500 
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const501 void ColorTableEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
502                                              GrProcessorKeyBuilder* b) const {
503     GLColorTableEffect::GenKey(*this, caps, b);
504 }
505 
onCreateGLSLInstance() const506 GrGLSLFragmentProcessor* ColorTableEffect::onCreateGLSLInstance() const {
507     return new GLColorTableEffect;
508 }
509 
onIsEqual(const GrFragmentProcessor & other) const510 bool ColorTableEffect::onIsEqual(const GrFragmentProcessor& other) const {
511     // For non-atlased instances, the texture (compared by base class) is sufficient to
512     // differentiate different tables. For atlased instances we ensure they are using the
513     // same row.
514     const ColorTableEffect& that = other.cast<ColorTableEffect>();
515     SkASSERT(SkToBool(fAtlas) == SkToBool(that.fAtlas));
516     // Ok to always do this comparison since both would be -1 if non-atlased.
517     return fRow == that.fRow;
518 }
519 
520 ///////////////////////////////////////////////////////////////////////////////
521 
522 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(ColorTableEffect);
523 
524 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * d)525 std::unique_ptr<GrFragmentProcessor> ColorTableEffect::TestCreate(GrProcessorTestData* d) {
526     int flags = 0;
527     uint8_t luts[256][4];
528     do {
529         for (int i = 0; i < 4; ++i) {
530             flags |= d->fRandom->nextBool() ? (1  << i): 0;
531         }
532     } while (!flags);
533     for (int i = 0; i < 4; ++i) {
534         if (flags & (1 << i)) {
535             for (int j = 0; j < 256; ++j) {
536                 luts[j][i] = SkToU8(d->fRandom->nextBits(8));
537             }
538         }
539     }
540     auto filter(SkTableColorFilter::MakeARGB(
541         (flags & (1 << 0)) ? luts[0] : nullptr,
542         (flags & (1 << 1)) ? luts[1] : nullptr,
543         (flags & (1 << 2)) ? luts[2] : nullptr,
544         (flags & (1 << 3)) ? luts[3] : nullptr
545     ));
546     sk_sp<SkColorSpace> colorSpace = GrTest::TestColorSpace(d->fRandom);
547     auto fp = filter->asFragmentProcessor(
548             d->context(), GrColorSpaceInfo(std::move(colorSpace), kRGBA_8888_GrPixelConfig));
549     SkASSERT(fp);
550     return fp;
551 }
552 #endif
553 
asFragmentProcessor(GrContext * context,const GrColorSpaceInfo &) const554 std::unique_ptr<GrFragmentProcessor> SkTable_ColorFilter::asFragmentProcessor(
555         GrContext* context, const GrColorSpaceInfo&) const {
556     SkBitmap bitmap;
557     this->asComponentTable(&bitmap);
558 
559     return ColorTableEffect::Make(context, bitmap);
560 }
561 
562 #endif // SK_SUPPORT_GPU
563 
564 ///////////////////////////////////////////////////////////////////////////////
565 
Make(const uint8_t table[256])566 sk_sp<SkColorFilter> SkTableColorFilter::Make(const uint8_t table[256]) {
567     return sk_make_sp<SkTable_ColorFilter>(table, table, table, table);
568 }
569 
MakeARGB(const uint8_t tableA[256],const uint8_t tableR[256],const uint8_t tableG[256],const uint8_t tableB[256])570 sk_sp<SkColorFilter> SkTableColorFilter::MakeARGB(const uint8_t tableA[256],
571                                                   const uint8_t tableR[256],
572                                                   const uint8_t tableG[256],
573                                                   const uint8_t tableB[256]) {
574     return sk_make_sp<SkTable_ColorFilter>(tableA, tableR, tableG, tableB);
575 }
576 
577 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkTableColorFilter)
578     SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTable_ColorFilter)
579 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
580