SkTableColorFilter.cpp revision 587e08f361ee3e775a6bbc6dca761dbba82e422c
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
10#include "SkArenaAlloc.h"
11#include "SkBitmap.h"
12#include "SkColorPriv.h"
13#include "SkRasterPipeline.h"
14#include "SkReadBuffer.h"
15#include "SkString.h"
16#include "SkUnPreMultiply.h"
17#include "SkWriteBuffer.h"
18
19static 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
54class SkTable_ColorFilter : public SkColorFilter {
55public:
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
83    virtual ~SkTable_ColorFilter() { delete fBitmap; }
84
85    bool asComponentTable(SkBitmap* table) const override;
86    sk_sp<SkColorFilter> makeComposed(sk_sp<SkColorFilter> inner) const override;
87
88#if SK_SUPPORT_GPU
89    sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, SkColorSpace*) const override;
90#endif
91
92    void filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const override;
93
94    SK_TO_STRING_OVERRIDE()
95
96    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTable_ColorFilter)
97
98    enum {
99        kA_Flag = 1 << 0,
100        kR_Flag = 1 << 1,
101        kG_Flag = 1 << 2,
102        kB_Flag = 1 << 3,
103    };
104
105    bool onAppendStages(SkRasterPipeline* p, SkColorSpace*, SkArenaAlloc* alloc,
106                        bool shaderIsOpaque) const override {
107        const uint8_t *r = gIdentityTable,
108                      *g = gIdentityTable,
109                      *b = gIdentityTable,
110                      *a = gIdentityTable;
111        const uint8_t* ptr = fStorage;
112        if (fFlags & kA_Flag) { a = ptr; ptr += 256; }
113        if (fFlags & kR_Flag) { r = ptr; ptr += 256; }
114        if (fFlags & kG_Flag) { g = ptr; ptr += 256; }
115        if (fFlags & kB_Flag) { b = ptr;             }
116
117        if (!shaderIsOpaque) {
118            p->append(SkRasterPipeline::unpremul);
119        }
120
121        struct Tables { const uint8_t *r, *g, *b, *a; };
122        p->append(SkRasterPipeline::byte_tables, alloc->make<Tables>(Tables{r,g,b,a}));
123
124        bool definitelyOpaque = shaderIsOpaque && a[0xff] == 0xff;
125        if (!definitelyOpaque) {
126            p->append(SkRasterPipeline::premul);
127        }
128        return true;
129    }
130
131protected:
132    void flatten(SkWriteBuffer&) const override;
133
134private:
135    mutable const SkBitmap* fBitmap; // lazily allocated
136
137    uint8_t fStorage[256 * 4];
138    unsigned fFlags;
139
140    friend class SkTableColorFilter;
141
142    typedef SkColorFilter INHERITED;
143};
144
145void SkTable_ColorFilter::filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const {
146    const uint8_t* table = fStorage;
147    const uint8_t* tableA = gIdentityTable;
148    const uint8_t* tableR = gIdentityTable;
149    const uint8_t* tableG = gIdentityTable;
150    const uint8_t* tableB = gIdentityTable;
151    if (fFlags & kA_Flag) {
152        tableA = table; table += 256;
153    }
154    if (fFlags & kR_Flag) {
155        tableR = table; table += 256;
156    }
157    if (fFlags & kG_Flag) {
158        tableG = table; table += 256;
159    }
160    if (fFlags & kB_Flag) {
161        tableB = table;
162    }
163
164    const SkUnPreMultiply::Scale* scaleTable = SkUnPreMultiply::GetScaleTable();
165    for (int i = 0; i < count; ++i) {
166        SkPMColor c = src[i];
167        unsigned a, r, g, b;
168        if (0 == c) {
169            a = r = g = b = 0;
170        } else {
171            a = SkGetPackedA32(c);
172            r = SkGetPackedR32(c);
173            g = SkGetPackedG32(c);
174            b = SkGetPackedB32(c);
175
176            if (a < 255) {
177                SkUnPreMultiply::Scale scale = scaleTable[a];
178                r = SkUnPreMultiply::ApplyScale(scale, r);
179                g = SkUnPreMultiply::ApplyScale(scale, g);
180                b = SkUnPreMultiply::ApplyScale(scale, b);
181            }
182        }
183        dst[i] = SkPremultiplyARGBInline(tableA[a], tableR[r],
184                                         tableG[g], tableB[b]);
185    }
186}
187
188#ifndef SK_IGNORE_TO_STRING
189void SkTable_ColorFilter::toString(SkString* str) const {
190    const uint8_t* table = fStorage;
191    const uint8_t* tableA = gIdentityTable;
192    const uint8_t* tableR = gIdentityTable;
193    const uint8_t* tableG = gIdentityTable;
194    const uint8_t* tableB = gIdentityTable;
195    if (fFlags & kA_Flag) {
196        tableA = table; table += 256;
197    }
198    if (fFlags & kR_Flag) {
199        tableR = table; table += 256;
200    }
201    if (fFlags & kG_Flag) {
202        tableG = table; table += 256;
203    }
204    if (fFlags & kB_Flag) {
205        tableB = table;
206    }
207
208    str->append("SkTable_ColorFilter (");
209
210    for (int i = 0; i < 256; ++i) {
211        str->appendf("%d: %d,%d,%d,%d\n",
212                     i, tableR[i], tableG[i], tableB[i], tableA[i]);
213    }
214
215    str->append(")");
216}
217#endif
218
219static const uint8_t gCountNibBits[] = {
220    0, 1, 1, 2,
221    1, 2, 2, 3,
222    1, 2, 2, 3,
223    2, 3, 3, 4
224};
225
226#include "SkPackBits.h"
227
228void SkTable_ColorFilter::flatten(SkWriteBuffer& buffer) const {
229    uint8_t storage[5*256];
230    int count = gCountNibBits[fFlags & 0xF];
231    size_t size = SkPackBits::Pack8(fStorage, count * 256, storage,
232                                    sizeof(storage));
233
234    buffer.write32(fFlags);
235    buffer.writeByteArray(storage, size);
236}
237
238sk_sp<SkFlattenable> SkTable_ColorFilter::CreateProc(SkReadBuffer& buffer) {
239    const int flags = buffer.read32();
240    const size_t count = gCountNibBits[flags & 0xF];
241    SkASSERT(count <= 4);
242
243    uint8_t packedStorage[5*256];
244    size_t packedSize = buffer.getArrayCount();
245    if (!buffer.validate(packedSize <= sizeof(packedStorage))) {
246        return nullptr;
247    }
248    if (!buffer.readByteArray(packedStorage, packedSize)) {
249        return nullptr;
250    }
251
252    uint8_t unpackedStorage[4*256];
253    size_t unpackedSize = SkPackBits::Unpack8(packedStorage, packedSize,
254                              unpackedStorage, sizeof(unpackedStorage));
255    // now check that we got the size we expected
256    if (!buffer.validate(unpackedSize == count*256)) {
257        return nullptr;
258    }
259
260    const uint8_t* a = nullptr;
261    const uint8_t* r = nullptr;
262    const uint8_t* g = nullptr;
263    const uint8_t* b = nullptr;
264    const uint8_t* ptr = unpackedStorage;
265
266    if (flags & kA_Flag) {
267        a = ptr;
268        ptr += 256;
269    }
270    if (flags & kR_Flag) {
271        r = ptr;
272        ptr += 256;
273    }
274    if (flags & kG_Flag) {
275        g = ptr;
276        ptr += 256;
277    }
278    if (flags & kB_Flag) {
279        b = ptr;
280        ptr += 256;
281    }
282    return SkTableColorFilter::MakeARGB(a, r, g, b);
283}
284
285bool SkTable_ColorFilter::asComponentTable(SkBitmap* table) const {
286    if (table) {
287        if (nullptr == fBitmap) {
288            SkBitmap* bmp = new SkBitmap;
289            bmp->allocPixels(SkImageInfo::MakeA8(256, 4));
290            uint8_t* bitmapPixels = bmp->getAddr8(0, 0);
291            int offset = 0;
292            static const unsigned kFlags[] = { kA_Flag, kR_Flag, kG_Flag, kB_Flag };
293
294            for (int x = 0; x < 4; ++x) {
295                if (!(fFlags & kFlags[x])) {
296                    memcpy(bitmapPixels, gIdentityTable, sizeof(gIdentityTable));
297                } else {
298                    memcpy(bitmapPixels, fStorage + offset, 256);
299                    offset += 256;
300                }
301                bitmapPixels += 256;
302            }
303            fBitmap = bmp;
304        }
305        *table = *fBitmap;
306    }
307    return true;
308}
309
310// Combines the two lookup tables so that making a lookup using res[] has
311// the same effect as making a lookup through inner[] then outer[].
312static void combine_tables(uint8_t res[256], const uint8_t outer[256], const uint8_t inner[256]) {
313    for (int i = 0; i < 256; i++) {
314        res[i] = outer[inner[i]];
315    }
316}
317
318sk_sp<SkColorFilter> SkTable_ColorFilter::makeComposed(sk_sp<SkColorFilter> innerFilter) const {
319    SkBitmap innerBM;
320    if (!innerFilter->asComponentTable(&innerBM)) {
321        return nullptr;
322    }
323
324    innerBM.lockPixels();
325    if (nullptr == innerBM.getPixels()) {
326        return nullptr;
327    }
328
329    const uint8_t* table = fStorage;
330    const uint8_t* tableA = gIdentityTable;
331    const uint8_t* tableR = gIdentityTable;
332    const uint8_t* tableG = gIdentityTable;
333    const uint8_t* tableB = gIdentityTable;
334    if (fFlags & kA_Flag) {
335        tableA = table; table += 256;
336    }
337    if (fFlags & kR_Flag) {
338        tableR = table; table += 256;
339    }
340    if (fFlags & kG_Flag) {
341        tableG = table; table += 256;
342    }
343    if (fFlags & kB_Flag) {
344        tableB = table;
345    }
346
347    uint8_t concatA[256];
348    uint8_t concatR[256];
349    uint8_t concatG[256];
350    uint8_t concatB[256];
351
352    combine_tables(concatA, tableA, innerBM.getAddr8(0, 0));
353    combine_tables(concatR, tableR, innerBM.getAddr8(0, 1));
354    combine_tables(concatG, tableG, innerBM.getAddr8(0, 2));
355    combine_tables(concatB, tableB, innerBM.getAddr8(0, 3));
356
357    return SkTableColorFilter::MakeARGB(concatA, concatR, concatG, concatB);
358}
359
360#if SK_SUPPORT_GPU
361
362#include "GrContext.h"
363#include "GrFragmentProcessor.h"
364#include "GrInvariantOutput.h"
365#include "GrTextureStripAtlas.h"
366#include "SkGr.h"
367#include "glsl/GrGLSLFragmentProcessor.h"
368#include "glsl/GrGLSLFragmentShaderBuilder.h"
369#include "glsl/GrGLSLProgramDataManager.h"
370#include "glsl/GrGLSLUniformHandler.h"
371
372class ColorTableEffect : public GrFragmentProcessor {
373public:
374    static sk_sp<GrFragmentProcessor> Make(GrContext* context, SkBitmap bitmap, unsigned flags);
375
376    virtual ~ColorTableEffect();
377
378    const char* name() const override { return "ColorTable"; }
379
380    const GrTextureStripAtlas* atlas() const { return fAtlas; }
381    int atlasRow() const { return fRow; }
382
383private:
384    GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
385
386    void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
387
388    bool onIsEqual(const GrFragmentProcessor&) const override;
389
390    void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
391
392    ColorTableEffect(GrTexture* texture, GrTextureStripAtlas* atlas, int row, unsigned flags);
393
394    GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
395
396    TextureSampler          fTextureSampler;
397
398    // currently not used in shader code, just to assist onComputeInvariantOutput().
399    unsigned                fFlags;
400
401    GrTextureStripAtlas*    fAtlas;
402    int                     fRow;
403
404    typedef GrFragmentProcessor INHERITED;
405};
406
407class GLColorTableEffect : public GrGLSLFragmentProcessor {
408public:
409    void emitCode(EmitArgs&) override;
410
411    static void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*) {}
412
413protected:
414    void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
415
416private:
417    UniformHandle fRGBAYValuesUni;
418    typedef GrGLSLFragmentProcessor INHERITED;
419};
420
421void GLColorTableEffect::onSetData(const GrGLSLProgramDataManager& pdm, const GrProcessor& proc) {
422    // The textures are organized in a strip where the rows are ordered a, r, g, b.
423    float rgbaYValues[4];
424    const ColorTableEffect& cte = proc.cast<ColorTableEffect>();
425    if (cte.atlas()) {
426        SkScalar yDelta = cte.atlas()->getNormalizedTexelHeight();
427        rgbaYValues[3] = cte.atlas()->getYOffset(cte.atlasRow()) + SK_ScalarHalf * yDelta;
428        rgbaYValues[0] = rgbaYValues[3] + yDelta;
429        rgbaYValues[1] = rgbaYValues[0] + yDelta;
430        rgbaYValues[2] = rgbaYValues[1] + yDelta;
431    } else {
432        rgbaYValues[3] = 0.125;
433        rgbaYValues[0] = 0.375;
434        rgbaYValues[1] = 0.625;
435        rgbaYValues[2] = 0.875;
436    }
437    pdm.set4fv(fRGBAYValuesUni, 1, rgbaYValues);
438}
439
440void GLColorTableEffect::emitCode(EmitArgs& args) {
441    const char* yoffsets;
442    fRGBAYValuesUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag,
443                                                       kVec4f_GrSLType, kDefault_GrSLPrecision,
444                                                       "yoffsets", &yoffsets);
445    static const float kColorScaleFactor = 255.0f / 256.0f;
446    static const float kColorOffsetFactor = 1.0f / 512.0f;
447    GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
448    if (nullptr == args.fInputColor) {
449        // the input color is solid white (all ones).
450        static const float kMaxValue = kColorScaleFactor + kColorOffsetFactor;
451        fragBuilder->codeAppendf("\t\tvec4 coord = vec4(%f, %f, %f, %f);\n",
452                                 kMaxValue, kMaxValue, kMaxValue, kMaxValue);
453
454    } else {
455        fragBuilder->codeAppendf("\t\tfloat nonZeroAlpha = max(%s.a, .0001);\n", args.fInputColor);
456        fragBuilder->codeAppendf("\t\tvec4 coord = vec4(%s.rgb / nonZeroAlpha, nonZeroAlpha);\n",
457                                 args.fInputColor);
458        fragBuilder->codeAppendf("\t\tcoord = coord * %f + vec4(%f, %f, %f, %f);\n",
459                                 kColorScaleFactor,
460                                 kColorOffsetFactor, kColorOffsetFactor,
461                                 kColorOffsetFactor, kColorOffsetFactor);
462    }
463
464    SkString coord;
465
466    fragBuilder->codeAppendf("\t\t%s.a = ", args.fOutputColor);
467    coord.printf("vec2(coord.a, %s.a)", yoffsets);
468    fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
469    fragBuilder->codeAppend(".a;\n");
470
471    fragBuilder->codeAppendf("\t\t%s.r = ", args.fOutputColor);
472    coord.printf("vec2(coord.r, %s.r)", yoffsets);
473    fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
474    fragBuilder->codeAppend(".a;\n");
475
476    fragBuilder->codeAppendf("\t\t%s.g = ", args.fOutputColor);
477    coord.printf("vec2(coord.g, %s.g)", yoffsets);
478    fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
479    fragBuilder->codeAppend(".a;\n");
480
481    fragBuilder->codeAppendf("\t\t%s.b = ", args.fOutputColor);
482    coord.printf("vec2(coord.b, %s.b)", yoffsets);
483    fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
484    fragBuilder->codeAppend(".a;\n");
485
486    fragBuilder->codeAppendf("\t\t%s.rgb *= %s.a;\n", args.fOutputColor, args.fOutputColor);
487}
488
489///////////////////////////////////////////////////////////////////////////////
490sk_sp<GrFragmentProcessor> ColorTableEffect::Make(GrContext* context, SkBitmap bitmap,
491                                                  unsigned flags) {
492
493    GrTextureStripAtlas::Desc desc;
494    desc.fWidth  = bitmap.width();
495    desc.fHeight = 128;
496    desc.fRowHeight = bitmap.height();
497    desc.fContext = context;
498    desc.fConfig = SkImageInfo2GrPixelConfig(bitmap.info(), *context->caps());
499    GrTextureStripAtlas* atlas = GrTextureStripAtlas::GetAtlas(desc);
500    int row = atlas->lockRow(bitmap);
501    sk_sp<GrTexture> texture;
502    if (-1 == row) {
503        atlas = nullptr;
504        texture.reset(
505            GrRefCachedBitmapTexture(context, bitmap, GrSamplerParams::ClampNoFilter(), nullptr));
506    } else {
507        texture.reset(SkRef(atlas->getTexture()));
508    }
509
510    return sk_sp<GrFragmentProcessor>(new ColorTableEffect(texture.get(), atlas, row, flags));
511}
512
513ColorTableEffect::ColorTableEffect(GrTexture* texture, GrTextureStripAtlas* atlas, int row,
514                                   unsigned flags)
515        : INHERITED(kNone_OptimizationFlags)  // Not bothering with table-specific optimizations.
516        , fTextureSampler(texture)
517        , fFlags(flags)
518        , fAtlas(atlas)
519        , fRow(row) {
520    this->initClassID<ColorTableEffect>();
521    this->addTextureSampler(&fTextureSampler);
522}
523
524ColorTableEffect::~ColorTableEffect() {
525    if (fAtlas) {
526        fAtlas->unlockRow(fRow);
527    }
528}
529
530void ColorTableEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
531                                             GrProcessorKeyBuilder* b) const {
532    GLColorTableEffect::GenKey(*this, caps, b);
533}
534
535GrGLSLFragmentProcessor* ColorTableEffect::onCreateGLSLInstance() const {
536    return new GLColorTableEffect;
537}
538
539bool ColorTableEffect::onIsEqual(const GrFragmentProcessor& other) const {
540    // For non-atlased instances, the texture (compared by base class) is sufficient to
541    // differentiate different tables. For atlased instances we ensure they are using the
542    // same row.
543    const ColorTableEffect& that = other.cast<ColorTableEffect>();
544    SkASSERT(SkToBool(fAtlas) == SkToBool(that.fAtlas));
545    // Ok to always do this comparison since both would be -1 if non-atlased.
546    return fRow == that.fRow;
547}
548
549void ColorTableEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
550    // If we kept the table in the effect then we could actually run known inputs through the
551    // table.
552    GrColorComponentFlags invalidateFlags = kNone_GrColorComponentFlags;
553    if (fFlags & SkTable_ColorFilter::kR_Flag) {
554        invalidateFlags |= kR_GrColorComponentFlag;
555    }
556    if (fFlags & SkTable_ColorFilter::kG_Flag) {
557        invalidateFlags |= kG_GrColorComponentFlag;
558    }
559    if (fFlags & SkTable_ColorFilter::kB_Flag) {
560        invalidateFlags |= kB_GrColorComponentFlag;
561    }
562    if (fFlags & SkTable_ColorFilter::kA_Flag) {
563        invalidateFlags |= kA_GrColorComponentFlag;
564    }
565    inout->invalidateComponents(invalidateFlags);
566}
567
568///////////////////////////////////////////////////////////////////////////////
569
570GR_DEFINE_FRAGMENT_PROCESSOR_TEST(ColorTableEffect);
571
572sk_sp<GrFragmentProcessor> ColorTableEffect::TestCreate(GrProcessorTestData* d) {
573    int flags = 0;
574    uint8_t luts[256][4];
575    do {
576        for (int i = 0; i < 4; ++i) {
577            flags |= d->fRandom->nextBool() ? (1  << i): 0;
578        }
579    } while (!flags);
580    for (int i = 0; i < 4; ++i) {
581        if (flags & (1 << i)) {
582            for (int j = 0; j < 256; ++j) {
583                luts[j][i] = SkToU8(d->fRandom->nextBits(8));
584            }
585        }
586    }
587    auto filter(SkTableColorFilter::MakeARGB(
588        (flags & (1 << 0)) ? luts[0] : nullptr,
589        (flags & (1 << 1)) ? luts[1] : nullptr,
590        (flags & (1 << 2)) ? luts[2] : nullptr,
591        (flags & (1 << 3)) ? luts[3] : nullptr
592    ));
593    sk_sp<SkColorSpace> colorSpace = GrTest::TestColorSpace(d->fRandom);
594    sk_sp<GrFragmentProcessor> fp = filter->asFragmentProcessor(d->fContext, colorSpace.get());
595    SkASSERT(fp);
596    return fp;
597}
598
599sk_sp<GrFragmentProcessor> SkTable_ColorFilter::asFragmentProcessor(GrContext* context,
600                                                                    SkColorSpace*) const {
601    SkBitmap bitmap;
602    this->asComponentTable(&bitmap);
603
604    return ColorTableEffect::Make(context, bitmap, fFlags);
605}
606
607#endif // SK_SUPPORT_GPU
608
609///////////////////////////////////////////////////////////////////////////////
610
611#ifdef SK_CPU_BENDIAN
612#else
613    #define SK_A32_INDEX    (3 - (SK_A32_SHIFT >> 3))
614    #define SK_R32_INDEX    (3 - (SK_R32_SHIFT >> 3))
615    #define SK_G32_INDEX    (3 - (SK_G32_SHIFT >> 3))
616    #define SK_B32_INDEX    (3 - (SK_B32_SHIFT >> 3))
617#endif
618
619///////////////////////////////////////////////////////////////////////////////
620
621sk_sp<SkColorFilter> SkTableColorFilter::Make(const uint8_t table[256]) {
622    return sk_make_sp<SkTable_ColorFilter>(table, table, table, table);
623}
624
625sk_sp<SkColorFilter> SkTableColorFilter::MakeARGB(const uint8_t tableA[256],
626                                                  const uint8_t tableR[256],
627                                                  const uint8_t tableG[256],
628                                                  const uint8_t tableB[256]) {
629    return sk_make_sp<SkTable_ColorFilter>(tableA, tableR, tableG, tableB);
630}
631
632SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkTableColorFilter)
633    SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTable_ColorFilter)
634SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
635