blob: d9ce98a7d8e99984d4abbc4616ea2e2eef5d71bd [file] [log] [blame]
jvanverthcfc18862015-04-28 08:48:20 -07001/*
Mike Reedb9641bd2017-05-04 10:57:40 -04002 * 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 */
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00007
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +00008#include "SkTableColorFilter.h"
Mike Reedb9641bd2017-05-04 10:57:40 -04009#include "SkPM4f.h"
Mike Kleina9e8ef02017-01-23 14:06:55 -050010#include "SkArenaAlloc.h"
bsalomonf276ac52015-10-09 13:36:42 -070011#include "SkBitmap.h"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000012#include "SkColorPriv.h"
Mike Kleina9e8ef02017-01-23 14:06:55 -050013#include "SkRasterPipeline.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000014#include "SkReadBuffer.h"
robertphillips@google.com1202c2a2013-05-23 14:00:17 +000015#include "SkString.h"
bsalomonf276ac52015-10-09 13:36:42 -070016#include "SkUnPreMultiply.h"
17#include "SkWriteBuffer.h"
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000018
Mike Kleina9e8ef02017-01-23 14:06:55 -050019static 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
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000054class 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[]) {
halcanary96fcdcc2015-08-27 07:41:13 -070058 fBitmap = nullptr;
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000059 fFlags = 0;
mike@reedtribe.orgf23f5f72012-01-07 03:48:45 +000060
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000061 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
Brian Salomond3b65972017-03-22 12:05:03 -040083 ~SkTable_ColorFilter() override { delete fBitmap; }
tomhudson@google.com1bb4be22012-07-24 17:24:21 +000084
mtklein36352bf2015-03-25 18:17:31 -070085 bool asComponentTable(SkBitmap* table) const override;
reedd053ce92016-03-22 10:17:23 -070086 sk_sp<SkColorFilter> makeComposed(sk_sp<SkColorFilter> inner) const override;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +000087
88#if SK_SUPPORT_GPU
Brian Osman618d3042016-10-25 10:51:28 -040089 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, SkColorSpace*) const override;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +000090#endif
mike@reedtribe.orgf23f5f72012-01-07 03:48:45 +000091
mtklein36352bf2015-03-25 18:17:31 -070092 void filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const override;
Mike Reedb9641bd2017-05-04 10:57:40 -040093 void filterSpan4f(const SkPM4f src[], int count, SkPM4f result[]) const override;
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000094
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +000095 SK_TO_STRING_OVERRIDE()
robertphillips@google.com1202c2a2013-05-23 14:00:17 +000096
djsollen@google.comba28d032012-03-26 17:57:35 +000097 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTable_ColorFilter)
mike@reedtribe.orgf23f5f72012-01-07 03:48:45 +000098
bsalomon@google.com371e1052013-01-11 21:08:55 +000099 enum {
100 kA_Flag = 1 << 0,
101 kR_Flag = 1 << 1,
102 kG_Flag = 1 << 2,
103 kB_Flag = 1 << 3,
104 };
105
Mike Kleina9e8ef02017-01-23 14:06:55 -0500106 bool onAppendStages(SkRasterPipeline* p, SkColorSpace*, SkArenaAlloc* alloc,
107 bool shaderIsOpaque) const override {
108 const uint8_t *r = gIdentityTable,
109 *g = gIdentityTable,
110 *b = gIdentityTable,
111 *a = gIdentityTable;
112 const uint8_t* ptr = fStorage;
113 if (fFlags & kA_Flag) { a = ptr; ptr += 256; }
114 if (fFlags & kR_Flag) { r = ptr; ptr += 256; }
115 if (fFlags & kG_Flag) { g = ptr; ptr += 256; }
116 if (fFlags & kB_Flag) { b = ptr; }
117
118 if (!shaderIsOpaque) {
119 p->append(SkRasterPipeline::unpremul);
120 }
121
122 struct Tables { const uint8_t *r, *g, *b, *a; };
123 p->append(SkRasterPipeline::byte_tables, alloc->make<Tables>(Tables{r,g,b,a}));
124
125 bool definitelyOpaque = shaderIsOpaque && a[0xff] == 0xff;
126 if (!definitelyOpaque) {
127 p->append(SkRasterPipeline::premul);
128 }
129 return true;
130 }
131
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000132protected:
mtklein36352bf2015-03-25 18:17:31 -0700133 void flatten(SkWriteBuffer&) const override;
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000134
135private:
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000136 mutable const SkBitmap* fBitmap; // lazily allocated
mike@reedtribe.orgf23f5f72012-01-07 03:48:45 +0000137
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000138 uint8_t fStorage[256 * 4];
139 unsigned fFlags;
140
reed9fa60da2014-08-21 07:59:51 -0700141 friend class SkTableColorFilter;
142
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000143 typedef SkColorFilter INHERITED;
144};
145
reed8a8d8412015-03-02 13:46:03 -0800146void SkTable_ColorFilter::filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const {
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000147 const uint8_t* table = fStorage;
148 const uint8_t* tableA = gIdentityTable;
149 const uint8_t* tableR = gIdentityTable;
150 const uint8_t* tableG = gIdentityTable;
151 const uint8_t* tableB = gIdentityTable;
152 if (fFlags & kA_Flag) {
153 tableA = table; table += 256;
154 }
155 if (fFlags & kR_Flag) {
156 tableR = table; table += 256;
157 }
158 if (fFlags & kG_Flag) {
159 tableG = table; table += 256;
160 }
161 if (fFlags & kB_Flag) {
162 tableB = table;
163 }
164
165 const SkUnPreMultiply::Scale* scaleTable = SkUnPreMultiply::GetScaleTable();
166 for (int i = 0; i < count; ++i) {
167 SkPMColor c = src[i];
168 unsigned a, r, g, b;
169 if (0 == c) {
170 a = r = g = b = 0;
171 } else {
172 a = SkGetPackedA32(c);
173 r = SkGetPackedR32(c);
174 g = SkGetPackedG32(c);
175 b = SkGetPackedB32(c);
176
177 if (a < 255) {
178 SkUnPreMultiply::Scale scale = scaleTable[a];
179 r = SkUnPreMultiply::ApplyScale(scale, r);
180 g = SkUnPreMultiply::ApplyScale(scale, g);
181 b = SkUnPreMultiply::ApplyScale(scale, b);
182 }
183 }
184 dst[i] = SkPremultiplyARGBInline(tableA[a], tableR[r],
185 tableG[g], tableB[b]);
186 }
187}
188
Mike Reedb9641bd2017-05-04 10:57:40 -0400189void SkTable_ColorFilter::filterSpan4f(const SkPM4f src[], int count, SkPM4f dst[]) 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 const float oneOver255 = 1.0f / 255;
209 for (int i = 0; i < count; ++i) {
210 SkColor4f c = src[i].unpremul();
211 int r = (int)(c.fR * 255.999) & 0xFF;
212 int g = (int)(c.fG * 255.999) & 0xFF;
213 int b = (int)(c.fB * 255.999) & 0xFF;
214 int a = (int)(c.fA * 255.999) & 0xFF;
215
216 SkColor4f d {
217 tableR[r] * oneOver255, tableG[g] * oneOver255,
218 tableB[b] * oneOver255, tableA[a] * oneOver255,
219 };
220 dst[i] = d.premul();
221 }
222}
223
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000224#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com1202c2a2013-05-23 14:00:17 +0000225void SkTable_ColorFilter::toString(SkString* str) const {
robertphillipsf3f5bad2014-12-19 13:49:15 -0800226 const uint8_t* table = fStorage;
227 const uint8_t* tableA = gIdentityTable;
228 const uint8_t* tableR = gIdentityTable;
229 const uint8_t* tableG = gIdentityTable;
230 const uint8_t* tableB = gIdentityTable;
231 if (fFlags & kA_Flag) {
232 tableA = table; table += 256;
233 }
234 if (fFlags & kR_Flag) {
235 tableR = table; table += 256;
236 }
237 if (fFlags & kG_Flag) {
238 tableG = table; table += 256;
239 }
240 if (fFlags & kB_Flag) {
241 tableB = table;
242 }
243
244 str->append("SkTable_ColorFilter (");
245
246 for (int i = 0; i < 256; ++i) {
247 str->appendf("%d: %d,%d,%d,%d\n",
248 i, tableR[i], tableG[i], tableB[i], tableA[i]);
249 }
250
251 str->append(")");
robertphillips@google.com1202c2a2013-05-23 14:00:17 +0000252}
253#endif
254
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000255static const uint8_t gCountNibBits[] = {
256 0, 1, 1, 2,
257 1, 2, 2, 3,
258 1, 2, 2, 3,
259 2, 3, 3, 4
260};
261
262#include "SkPackBits.h"
263
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000264void SkTable_ColorFilter::flatten(SkWriteBuffer& buffer) const {
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000265 uint8_t storage[5*256];
266 int count = gCountNibBits[fFlags & 0xF];
jschuh699b8522015-06-04 15:10:37 -0700267 size_t size = SkPackBits::Pack8(fStorage, count * 256, storage,
268 sizeof(storage));
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000269
reed9fa60da2014-08-21 07:59:51 -0700270 buffer.write32(fFlags);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000271 buffer.writeByteArray(storage, size);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000272}
273
reed60c9b582016-04-03 09:11:13 -0700274sk_sp<SkFlattenable> SkTable_ColorFilter::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700275 const int flags = buffer.read32();
276 const size_t count = gCountNibBits[flags & 0xF];
277 SkASSERT(count <= 4);
278
279 uint8_t packedStorage[5*256];
280 size_t packedSize = buffer.getArrayCount();
281 if (!buffer.validate(packedSize <= sizeof(packedStorage))) {
halcanary96fcdcc2015-08-27 07:41:13 -0700282 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700283 }
284 if (!buffer.readByteArray(packedStorage, packedSize)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700285 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700286 }
287
288 uint8_t unpackedStorage[4*256];
jschuh699b8522015-06-04 15:10:37 -0700289 size_t unpackedSize = SkPackBits::Unpack8(packedStorage, packedSize,
290 unpackedStorage, sizeof(unpackedStorage));
reed9fa60da2014-08-21 07:59:51 -0700291 // now check that we got the size we expected
senorblanco91c395a2014-09-25 15:51:35 -0700292 if (!buffer.validate(unpackedSize == count*256)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700293 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700294 }
295
halcanary96fcdcc2015-08-27 07:41:13 -0700296 const uint8_t* a = nullptr;
297 const uint8_t* r = nullptr;
298 const uint8_t* g = nullptr;
299 const uint8_t* b = nullptr;
reed9fa60da2014-08-21 07:59:51 -0700300 const uint8_t* ptr = unpackedStorage;
301
302 if (flags & kA_Flag) {
303 a = ptr;
304 ptr += 256;
305 }
306 if (flags & kR_Flag) {
307 r = ptr;
308 ptr += 256;
309 }
310 if (flags & kG_Flag) {
311 g = ptr;
312 ptr += 256;
313 }
314 if (flags & kB_Flag) {
315 b = ptr;
316 ptr += 256;
317 }
reed60c9b582016-04-03 09:11:13 -0700318 return SkTableColorFilter::MakeARGB(a, r, g, b);
reed9fa60da2014-08-21 07:59:51 -0700319}
320
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000321bool SkTable_ColorFilter::asComponentTable(SkBitmap* table) const {
mike@reedtribe.orgf23f5f72012-01-07 03:48:45 +0000322 if (table) {
halcanary96fcdcc2015-08-27 07:41:13 -0700323 if (nullptr == fBitmap) {
halcanary385fe4d2015-08-26 13:07:48 -0700324 SkBitmap* bmp = new SkBitmap;
reed@google.com9ebcac52014-01-24 18:53:42 +0000325 bmp->allocPixels(SkImageInfo::MakeA8(256, 4));
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000326 uint8_t* bitmapPixels = bmp->getAddr8(0, 0);
twiz@google.com58071162012-07-18 21:41:50 +0000327 int offset = 0;
328 static const unsigned kFlags[] = { kA_Flag, kR_Flag, kG_Flag, kB_Flag };
329
330 for (int x = 0; x < 4; ++x) {
331 if (!(fFlags & kFlags[x])) {
332 memcpy(bitmapPixels, gIdentityTable, sizeof(gIdentityTable));
333 } else {
334 memcpy(bitmapPixels, fStorage + offset, 256);
335 offset += 256;
336 }
337 bitmapPixels += 256;
338 }
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000339 fBitmap = bmp;
mike@reedtribe.orgf23f5f72012-01-07 03:48:45 +0000340 }
341 *table = *fBitmap;
342 }
343 return true;
344}
345
reed8a8d8412015-03-02 13:46:03 -0800346// Combines the two lookup tables so that making a lookup using res[] has
347// the same effect as making a lookup through inner[] then outer[].
348static void combine_tables(uint8_t res[256], const uint8_t outer[256], const uint8_t inner[256]) {
349 for (int i = 0; i < 256; i++) {
350 res[i] = outer[inner[i]];
351 }
352}
353
reedd053ce92016-03-22 10:17:23 -0700354sk_sp<SkColorFilter> SkTable_ColorFilter::makeComposed(sk_sp<SkColorFilter> innerFilter) const {
reed8a8d8412015-03-02 13:46:03 -0800355 SkBitmap innerBM;
356 if (!innerFilter->asComponentTable(&innerBM)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700357 return nullptr;
reed8a8d8412015-03-02 13:46:03 -0800358 }
359
halcanary96fcdcc2015-08-27 07:41:13 -0700360 if (nullptr == innerBM.getPixels()) {
361 return nullptr;
reed8a8d8412015-03-02 13:46:03 -0800362 }
363
364 const uint8_t* table = fStorage;
365 const uint8_t* tableA = gIdentityTable;
366 const uint8_t* tableR = gIdentityTable;
367 const uint8_t* tableG = gIdentityTable;
368 const uint8_t* tableB = gIdentityTable;
369 if (fFlags & kA_Flag) {
370 tableA = table; table += 256;
371 }
372 if (fFlags & kR_Flag) {
373 tableR = table; table += 256;
374 }
375 if (fFlags & kG_Flag) {
376 tableG = table; table += 256;
377 }
378 if (fFlags & kB_Flag) {
379 tableB = table;
380 }
381
382 uint8_t concatA[256];
383 uint8_t concatR[256];
384 uint8_t concatG[256];
385 uint8_t concatB[256];
386
387 combine_tables(concatA, tableA, innerBM.getAddr8(0, 0));
388 combine_tables(concatR, tableR, innerBM.getAddr8(0, 1));
389 combine_tables(concatG, tableG, innerBM.getAddr8(0, 2));
390 combine_tables(concatB, tableB, innerBM.getAddr8(0, 3));
391
reedd053ce92016-03-22 10:17:23 -0700392 return SkTableColorFilter::MakeARGB(concatA, concatR, concatG, concatB);
reed8a8d8412015-03-02 13:46:03 -0800393}
394
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000395#if SK_SUPPORT_GPU
396
brianosmana6359362016-03-21 06:55:37 -0700397#include "GrContext.h"
bsalomon6251d172014-10-15 10:50:36 -0700398#include "GrFragmentProcessor.h"
ajuma95243eb2016-08-24 08:19:02 -0700399#include "GrTextureStripAtlas.h"
bsalomonc6327a82014-10-27 12:53:08 -0700400#include "SkGr.h"
egdaniel64c47282015-11-13 06:54:19 -0800401#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -0800402#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -0700403#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -0800404#include "glsl/GrGLSLUniformHandler.h"
bsalomonc6327a82014-10-27 12:53:08 -0700405
joshualittb0a8a372014-09-23 09:50:21 -0700406class ColorTableEffect : public GrFragmentProcessor {
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000407public:
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500408 static sk_sp<GrFragmentProcessor> Make(GrContext* context,
409 const SkBitmap& bitmap,
410 unsigned flags);
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000411
Robert Phillips30f9bc62017-02-22 15:28:38 -0500412 ~ColorTableEffect() override;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000413
mtklein36352bf2015-03-25 18:17:31 -0700414 const char* name() const override { return "ColorTable"; }
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000415
bsalomonc6327a82014-10-27 12:53:08 -0700416 const GrTextureStripAtlas* atlas() const { return fAtlas; }
417 int atlasRow() const { return fRow; }
418
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000419private:
egdaniel57d3b032015-11-13 11:57:27 -0800420 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -0700421
Brian Salomon94efbf52016-11-29 13:43:05 -0500422 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -0700423
mtklein36352bf2015-03-25 18:17:31 -0700424 bool onIsEqual(const GrFragmentProcessor&) const override;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000425
Robert Phillips26c90e02017-03-14 14:39:29 -0400426 ColorTableEffect(GrResourceProvider* , sk_sp<GrTextureProxy> proxy,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500427 GrTextureStripAtlas* atlas, int row, unsigned flags);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000428
joshualittb0a8a372014-09-23 09:50:21 -0700429 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000430
Brian Salomoncb30bb22017-02-12 09:28:54 -0500431 TextureSampler fTextureSampler;
432 GrTextureStripAtlas* fAtlas;
433 int fRow;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000434
joshualittb0a8a372014-09-23 09:50:21 -0700435 typedef GrFragmentProcessor INHERITED;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000436};
437
egdaniel64c47282015-11-13 06:54:19 -0800438class GLColorTableEffect : public GrGLSLFragmentProcessor {
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000439public:
robertphillips9cdb9922016-02-03 12:25:40 -0800440 void emitCode(EmitArgs&) override;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000441
Brian Salomon94efbf52016-11-29 13:43:05 -0500442 static void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*) {}
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000443
wangyixb1daa862015-08-18 11:29:31 -0700444protected:
Brian Salomonab015ef2017-04-04 10:15:51 -0400445 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -0700446
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000447private:
bsalomonc6327a82014-10-27 12:53:08 -0700448 UniformHandle fRGBAYValuesUni;
egdaniel64c47282015-11-13 06:54:19 -0800449 typedef GrGLSLFragmentProcessor INHERITED;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000450};
451
Brian Salomonab015ef2017-04-04 10:15:51 -0400452void GLColorTableEffect::onSetData(const GrGLSLProgramDataManager& pdm,
453 const GrFragmentProcessor& proc) {
bsalomonc6327a82014-10-27 12:53:08 -0700454 // The textures are organized in a strip where the rows are ordered a, r, g, b.
455 float rgbaYValues[4];
456 const ColorTableEffect& cte = proc.cast<ColorTableEffect>();
457 if (cte.atlas()) {
458 SkScalar yDelta = cte.atlas()->getNormalizedTexelHeight();
459 rgbaYValues[3] = cte.atlas()->getYOffset(cte.atlasRow()) + SK_ScalarHalf * yDelta;
460 rgbaYValues[0] = rgbaYValues[3] + yDelta;
461 rgbaYValues[1] = rgbaYValues[0] + yDelta;
462 rgbaYValues[2] = rgbaYValues[1] + yDelta;
463 } else {
464 rgbaYValues[3] = 0.125;
465 rgbaYValues[0] = 0.375;
466 rgbaYValues[1] = 0.625;
mtklein3f3b3d02014-12-01 11:47:08 -0800467 rgbaYValues[2] = 0.875;
bsalomonc6327a82014-10-27 12:53:08 -0700468 }
469 pdm.set4fv(fRGBAYValuesUni, 1, rgbaYValues);
470}
471
wangyix7c157a92015-07-22 15:08:53 -0700472void GLColorTableEffect::emitCode(EmitArgs& args) {
bsalomonc6327a82014-10-27 12:53:08 -0700473 const char* yoffsets;
cdalton5e58cee2016-02-11 12:49:47 -0800474 fRGBAYValuesUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800475 kVec4f_GrSLType, kDefault_GrSLPrecision,
476 "yoffsets", &yoffsets);
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000477 static const float kColorScaleFactor = 255.0f / 256.0f;
478 static const float kColorOffsetFactor = 1.0f / 512.0f;
cdalton85285412016-02-18 12:37:07 -0800479 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
halcanary96fcdcc2015-08-27 07:41:13 -0700480 if (nullptr == args.fInputColor) {
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000481 // the input color is solid white (all ones).
482 static const float kMaxValue = kColorScaleFactor + kColorOffsetFactor;
egdaniel4ca2e602015-11-18 08:01:26 -0800483 fragBuilder->codeAppendf("\t\tvec4 coord = vec4(%f, %f, %f, %f);\n",
484 kMaxValue, kMaxValue, kMaxValue, kMaxValue);
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000485
486 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800487 fragBuilder->codeAppendf("\t\tfloat nonZeroAlpha = max(%s.a, .0001);\n", args.fInputColor);
488 fragBuilder->codeAppendf("\t\tvec4 coord = vec4(%s.rgb / nonZeroAlpha, nonZeroAlpha);\n",
489 args.fInputColor);
490 fragBuilder->codeAppendf("\t\tcoord = coord * %f + vec4(%f, %f, %f, %f);\n",
491 kColorScaleFactor,
492 kColorOffsetFactor, kColorOffsetFactor,
493 kColorOffsetFactor, kColorOffsetFactor);
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000494 }
495
bsalomonc6327a82014-10-27 12:53:08 -0700496 SkString coord;
497
egdaniel4ca2e602015-11-18 08:01:26 -0800498 fragBuilder->codeAppendf("\t\t%s.a = ", args.fOutputColor);
bsalomonc6327a82014-10-27 12:53:08 -0700499 coord.printf("vec2(coord.a, %s.a)", yoffsets);
cdalton3f6f76f2016-04-11 12:18:09 -0700500 fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
bsalomoncdee0092016-01-08 13:20:12 -0800501 fragBuilder->codeAppend(".a;\n");
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000502
egdaniel4ca2e602015-11-18 08:01:26 -0800503 fragBuilder->codeAppendf("\t\t%s.r = ", args.fOutputColor);
bsalomonc6327a82014-10-27 12:53:08 -0700504 coord.printf("vec2(coord.r, %s.r)", yoffsets);
cdalton3f6f76f2016-04-11 12:18:09 -0700505 fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
bsalomoncdee0092016-01-08 13:20:12 -0800506 fragBuilder->codeAppend(".a;\n");
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000507
egdaniel4ca2e602015-11-18 08:01:26 -0800508 fragBuilder->codeAppendf("\t\t%s.g = ", args.fOutputColor);
bsalomonc6327a82014-10-27 12:53:08 -0700509 coord.printf("vec2(coord.g, %s.g)", yoffsets);
cdalton3f6f76f2016-04-11 12:18:09 -0700510 fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
bsalomoncdee0092016-01-08 13:20:12 -0800511 fragBuilder->codeAppend(".a;\n");
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000512
egdaniel4ca2e602015-11-18 08:01:26 -0800513 fragBuilder->codeAppendf("\t\t%s.b = ", args.fOutputColor);
bsalomonc6327a82014-10-27 12:53:08 -0700514 coord.printf("vec2(coord.b, %s.b)", yoffsets);
cdalton3f6f76f2016-04-11 12:18:09 -0700515 fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
bsalomoncdee0092016-01-08 13:20:12 -0800516 fragBuilder->codeAppend(".a;\n");
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000517
egdaniel4ca2e602015-11-18 08:01:26 -0800518 fragBuilder->codeAppendf("\t\t%s.rgb *= %s.a;\n", args.fOutputColor, args.fOutputColor);
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000519}
520
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000521///////////////////////////////////////////////////////////////////////////////
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500522sk_sp<GrFragmentProcessor> ColorTableEffect::Make(GrContext* context, const SkBitmap& bitmap,
bungeman06ca8ec2016-06-09 08:01:03 -0700523 unsigned flags) {
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000524
bsalomonc6327a82014-10-27 12:53:08 -0700525 GrTextureStripAtlas::Desc desc;
526 desc.fWidth = bitmap.width();
527 desc.fHeight = 128;
528 desc.fRowHeight = bitmap.height();
Robert Phillips26c90e02017-03-14 14:39:29 -0400529
530 // TODO: this seems a bit heavy handed (passing a GrContext as part of the desc)
bsalomonc6327a82014-10-27 12:53:08 -0700531 desc.fContext = context;
brianosmana6359362016-03-21 06:55:37 -0700532 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap.info(), *context->caps());
bsalomonc6327a82014-10-27 12:53:08 -0700533 GrTextureStripAtlas* atlas = GrTextureStripAtlas::GetAtlas(desc);
534 int row = atlas->lockRow(bitmap);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500535 sk_sp<GrTextureProxy> proxy;
bsalomonc6327a82014-10-27 12:53:08 -0700536 if (-1 == row) {
halcanary96fcdcc2015-08-27 07:41:13 -0700537 atlas = nullptr;
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500538
Robert Phillips26c90e02017-03-14 14:39:29 -0400539 proxy = GrMakeCachedBitmapProxy(context->resourceProvider(), bitmap);
bsalomonc6327a82014-10-27 12:53:08 -0700540 } else {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500541 proxy = atlas->asTextureProxyRef();
bsalomonc6327a82014-10-27 12:53:08 -0700542 }
543
Robert Phillips30f9bc62017-02-22 15:28:38 -0500544 if (!proxy) {
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500545 return nullptr;
546 }
547
Robert Phillips26c90e02017-03-14 14:39:29 -0400548 return sk_sp<GrFragmentProcessor>(new ColorTableEffect(context->resourceProvider(),
549 std::move(proxy),
Robert Phillips30f9bc62017-02-22 15:28:38 -0500550 atlas, row, flags));
bsalomonc6327a82014-10-27 12:53:08 -0700551}
552
Robert Phillips26c90e02017-03-14 14:39:29 -0400553ColorTableEffect::ColorTableEffect(GrResourceProvider* resourceProvider,
554 sk_sp<GrTextureProxy> proxy,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500555 GrTextureStripAtlas* atlas, int row, unsigned flags)
Brian Salomon587e08f2017-01-27 10:59:27 -0500556 : INHERITED(kNone_OptimizationFlags) // Not bothering with table-specific optimizations.
Robert Phillips26c90e02017-03-14 14:39:29 -0400557 , fTextureSampler(resourceProvider, std::move(proxy))
Brian Salomon587e08f2017-01-27 10:59:27 -0500558 , fAtlas(atlas)
559 , fRow(row) {
joshualitteb2a6762014-12-04 11:35:33 -0800560 this->initClassID<ColorTableEffect>();
Brian Salomon0bbecb22016-11-17 11:38:22 -0500561 this->addTextureSampler(&fTextureSampler);
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000562}
563
564ColorTableEffect::~ColorTableEffect() {
bsalomonc6327a82014-10-27 12:53:08 -0700565 if (fAtlas) {
566 fAtlas->unlockRow(fRow);
567 }
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000568}
569
Brian Salomon94efbf52016-11-29 13:43:05 -0500570void ColorTableEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800571 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800572 GLColorTableEffect::GenKey(*this, caps, b);
573}
574
egdaniel57d3b032015-11-13 11:57:27 -0800575GrGLSLFragmentProcessor* ColorTableEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800576 return new GLColorTableEffect;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000577}
578
bsalomonc6327a82014-10-27 12:53:08 -0700579bool ColorTableEffect::onIsEqual(const GrFragmentProcessor& other) const {
580 // For non-atlased instances, the texture (compared by base class) is sufficient to
581 // differentiate different tables. For atlased instances we ensure they are using the
582 // same row.
583 const ColorTableEffect& that = other.cast<ColorTableEffect>();
584 SkASSERT(SkToBool(fAtlas) == SkToBool(that.fAtlas));
585 // Ok to always do this comparison since both would be -1 if non-atlased.
586 return fRow == that.fRow;
587}
588
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000589///////////////////////////////////////////////////////////////////////////////
590
joshualittb0a8a372014-09-23 09:50:21 -0700591GR_DEFINE_FRAGMENT_PROCESSOR_TEST(ColorTableEffect);
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000592
Hal Canary6f6961e2017-01-31 13:50:44 -0500593#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700594sk_sp<GrFragmentProcessor> ColorTableEffect::TestCreate(GrProcessorTestData* d) {
bsalomonc6327a82014-10-27 12:53:08 -0700595 int flags = 0;
596 uint8_t luts[256][4];
597 do {
598 for (int i = 0; i < 4; ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700599 flags |= d->fRandom->nextBool() ? (1 << i): 0;
bsalomonc6327a82014-10-27 12:53:08 -0700600 }
601 } while (!flags);
602 for (int i = 0; i < 4; ++i) {
603 if (flags & (1 << i)) {
604 for (int j = 0; j < 256; ++j) {
joshualitt0067ff52015-07-08 14:26:19 -0700605 luts[j][i] = SkToU8(d->fRandom->nextBits(8));
bsalomonc6327a82014-10-27 12:53:08 -0700606 }
607 }
608 }
reedd053ce92016-03-22 10:17:23 -0700609 auto filter(SkTableColorFilter::MakeARGB(
halcanary96fcdcc2015-08-27 07:41:13 -0700610 (flags & (1 << 0)) ? luts[0] : nullptr,
611 (flags & (1 << 1)) ? luts[1] : nullptr,
612 (flags & (1 << 2)) ? luts[2] : nullptr,
613 (flags & (1 << 3)) ? luts[3] : nullptr
bsalomonc6327a82014-10-27 12:53:08 -0700614 ));
Brian Osman618d3042016-10-25 10:51:28 -0400615 sk_sp<SkColorSpace> colorSpace = GrTest::TestColorSpace(d->fRandom);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500616 sk_sp<GrFragmentProcessor> fp = filter->asFragmentProcessor(d->context(), colorSpace.get());
bsalomone25eea42015-09-29 06:38:55 -0700617 SkASSERT(fp);
618 return fp;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000619}
Hal Canary6f6961e2017-01-31 13:50:44 -0500620#endif
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000621
Brian Osman618d3042016-10-25 10:51:28 -0400622sk_sp<GrFragmentProcessor> SkTable_ColorFilter::asFragmentProcessor(GrContext* context,
623 SkColorSpace*) const {
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000624 SkBitmap bitmap;
625 this->asComponentTable(&bitmap);
bsalomonc6327a82014-10-27 12:53:08 -0700626
bungeman06ca8ec2016-06-09 08:01:03 -0700627 return ColorTableEffect::Make(context, bitmap, fFlags);
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000628}
629
630#endif // SK_SUPPORT_GPU
631
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000632///////////////////////////////////////////////////////////////////////////////
633
634#ifdef SK_CPU_BENDIAN
635#else
636 #define SK_A32_INDEX (3 - (SK_A32_SHIFT >> 3))
637 #define SK_R32_INDEX (3 - (SK_R32_SHIFT >> 3))
638 #define SK_G32_INDEX (3 - (SK_G32_SHIFT >> 3))
639 #define SK_B32_INDEX (3 - (SK_B32_SHIFT >> 3))
640#endif
641
642///////////////////////////////////////////////////////////////////////////////
643
reedd053ce92016-03-22 10:17:23 -0700644sk_sp<SkColorFilter> SkTableColorFilter::Make(const uint8_t table[256]) {
645 return sk_make_sp<SkTable_ColorFilter>(table, table, table, table);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000646}
647
reedd053ce92016-03-22 10:17:23 -0700648sk_sp<SkColorFilter> SkTableColorFilter::MakeARGB(const uint8_t tableA[256],
649 const uint8_t tableR[256],
650 const uint8_t tableG[256],
651 const uint8_t tableB[256]) {
652 return sk_make_sp<SkTable_ColorFilter>(tableA, tableR, tableG, tableB);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000653}
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000654
655SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkTableColorFilter)
656 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTable_ColorFilter)
657SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END