blob: b5156ae5bf718a2644465d1c19bcd5065adfd0ac [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 Kleinfdf31032017-05-09 14:57:58 -0400106 void onAppendStages(SkRasterPipeline* p, SkColorSpace*, SkArenaAlloc* alloc,
Mike Kleina9e8ef02017-01-23 14:06:55 -0500107 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 }
Mike Kleina9e8ef02017-01-23 14:06:55 -0500129 }
130
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000131protected:
mtklein36352bf2015-03-25 18:17:31 -0700132 void flatten(SkWriteBuffer&) const override;
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000133
134private:
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000135 mutable const SkBitmap* fBitmap; // lazily allocated
mike@reedtribe.orgf23f5f72012-01-07 03:48:45 +0000136
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000137 uint8_t fStorage[256 * 4];
138 unsigned fFlags;
139
reed9fa60da2014-08-21 07:59:51 -0700140 friend class SkTableColorFilter;
141
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000142 typedef SkColorFilter INHERITED;
143};
144
reed8a8d8412015-03-02 13:46:03 -0800145void SkTable_ColorFilter::filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const {
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000146 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
Mike Reedb9641bd2017-05-04 10:57:40 -0400188void SkTable_ColorFilter::filterSpan4f(const SkPM4f src[], int count, SkPM4f dst[]) const {
189 const uint8_t* table = fStorage;
190 const uint8_t* tableA = gIdentityTable;
191 const uint8_t* tableR = gIdentityTable;
192 const uint8_t* tableG = gIdentityTable;
193 const uint8_t* tableB = gIdentityTable;
194 if (fFlags & kA_Flag) {
195 tableA = table; table += 256;
196 }
197 if (fFlags & kR_Flag) {
198 tableR = table; table += 256;
199 }
200 if (fFlags & kG_Flag) {
201 tableG = table; table += 256;
202 }
203 if (fFlags & kB_Flag) {
204 tableB = table;
205 }
206
207 const float oneOver255 = 1.0f / 255;
208 for (int i = 0; i < count; ++i) {
209 SkColor4f c = src[i].unpremul();
210 int r = (int)(c.fR * 255.999) & 0xFF;
211 int g = (int)(c.fG * 255.999) & 0xFF;
212 int b = (int)(c.fB * 255.999) & 0xFF;
213 int a = (int)(c.fA * 255.999) & 0xFF;
214
215 SkColor4f d {
216 tableR[r] * oneOver255, tableG[g] * oneOver255,
217 tableB[b] * oneOver255, tableA[a] * oneOver255,
218 };
219 dst[i] = d.premul();
220 }
221}
222
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000223#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com1202c2a2013-05-23 14:00:17 +0000224void SkTable_ColorFilter::toString(SkString* str) const {
robertphillipsf3f5bad2014-12-19 13:49:15 -0800225 const uint8_t* table = fStorage;
226 const uint8_t* tableA = gIdentityTable;
227 const uint8_t* tableR = gIdentityTable;
228 const uint8_t* tableG = gIdentityTable;
229 const uint8_t* tableB = gIdentityTable;
230 if (fFlags & kA_Flag) {
231 tableA = table; table += 256;
232 }
233 if (fFlags & kR_Flag) {
234 tableR = table; table += 256;
235 }
236 if (fFlags & kG_Flag) {
237 tableG = table; table += 256;
238 }
239 if (fFlags & kB_Flag) {
240 tableB = table;
241 }
242
243 str->append("SkTable_ColorFilter (");
244
245 for (int i = 0; i < 256; ++i) {
246 str->appendf("%d: %d,%d,%d,%d\n",
247 i, tableR[i], tableG[i], tableB[i], tableA[i]);
248 }
249
250 str->append(")");
robertphillips@google.com1202c2a2013-05-23 14:00:17 +0000251}
252#endif
253
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000254static const uint8_t gCountNibBits[] = {
255 0, 1, 1, 2,
256 1, 2, 2, 3,
257 1, 2, 2, 3,
258 2, 3, 3, 4
259};
260
261#include "SkPackBits.h"
262
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000263void SkTable_ColorFilter::flatten(SkWriteBuffer& buffer) const {
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000264 uint8_t storage[5*256];
265 int count = gCountNibBits[fFlags & 0xF];
jschuh699b8522015-06-04 15:10:37 -0700266 size_t size = SkPackBits::Pack8(fStorage, count * 256, storage,
267 sizeof(storage));
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000268
reed9fa60da2014-08-21 07:59:51 -0700269 buffer.write32(fFlags);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000270 buffer.writeByteArray(storage, size);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000271}
272
reed60c9b582016-04-03 09:11:13 -0700273sk_sp<SkFlattenable> SkTable_ColorFilter::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700274 const int flags = buffer.read32();
275 const size_t count = gCountNibBits[flags & 0xF];
276 SkASSERT(count <= 4);
277
278 uint8_t packedStorage[5*256];
279 size_t packedSize = buffer.getArrayCount();
280 if (!buffer.validate(packedSize <= sizeof(packedStorage))) {
halcanary96fcdcc2015-08-27 07:41:13 -0700281 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700282 }
283 if (!buffer.readByteArray(packedStorage, packedSize)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700284 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700285 }
286
287 uint8_t unpackedStorage[4*256];
jschuh699b8522015-06-04 15:10:37 -0700288 size_t unpackedSize = SkPackBits::Unpack8(packedStorage, packedSize,
289 unpackedStorage, sizeof(unpackedStorage));
reed9fa60da2014-08-21 07:59:51 -0700290 // now check that we got the size we expected
senorblanco91c395a2014-09-25 15:51:35 -0700291 if (!buffer.validate(unpackedSize == count*256)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700292 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700293 }
294
halcanary96fcdcc2015-08-27 07:41:13 -0700295 const uint8_t* a = nullptr;
296 const uint8_t* r = nullptr;
297 const uint8_t* g = nullptr;
298 const uint8_t* b = nullptr;
reed9fa60da2014-08-21 07:59:51 -0700299 const uint8_t* ptr = unpackedStorage;
300
301 if (flags & kA_Flag) {
302 a = ptr;
303 ptr += 256;
304 }
305 if (flags & kR_Flag) {
306 r = ptr;
307 ptr += 256;
308 }
309 if (flags & kG_Flag) {
310 g = ptr;
311 ptr += 256;
312 }
313 if (flags & kB_Flag) {
314 b = ptr;
315 ptr += 256;
316 }
reed60c9b582016-04-03 09:11:13 -0700317 return SkTableColorFilter::MakeARGB(a, r, g, b);
reed9fa60da2014-08-21 07:59:51 -0700318}
319
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000320bool SkTable_ColorFilter::asComponentTable(SkBitmap* table) const {
mike@reedtribe.orgf23f5f72012-01-07 03:48:45 +0000321 if (table) {
halcanary96fcdcc2015-08-27 07:41:13 -0700322 if (nullptr == fBitmap) {
halcanary385fe4d2015-08-26 13:07:48 -0700323 SkBitmap* bmp = new SkBitmap;
reed@google.com9ebcac52014-01-24 18:53:42 +0000324 bmp->allocPixels(SkImageInfo::MakeA8(256, 4));
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000325 uint8_t* bitmapPixels = bmp->getAddr8(0, 0);
twiz@google.com58071162012-07-18 21:41:50 +0000326 int offset = 0;
327 static const unsigned kFlags[] = { kA_Flag, kR_Flag, kG_Flag, kB_Flag };
328
329 for (int x = 0; x < 4; ++x) {
330 if (!(fFlags & kFlags[x])) {
331 memcpy(bitmapPixels, gIdentityTable, sizeof(gIdentityTable));
332 } else {
333 memcpy(bitmapPixels, fStorage + offset, 256);
334 offset += 256;
335 }
336 bitmapPixels += 256;
337 }
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000338 fBitmap = bmp;
mike@reedtribe.orgf23f5f72012-01-07 03:48:45 +0000339 }
340 *table = *fBitmap;
341 }
342 return true;
343}
344
reed8a8d8412015-03-02 13:46:03 -0800345// Combines the two lookup tables so that making a lookup using res[] has
346// the same effect as making a lookup through inner[] then outer[].
347static void combine_tables(uint8_t res[256], const uint8_t outer[256], const uint8_t inner[256]) {
348 for (int i = 0; i < 256; i++) {
349 res[i] = outer[inner[i]];
350 }
351}
352
reedd053ce92016-03-22 10:17:23 -0700353sk_sp<SkColorFilter> SkTable_ColorFilter::makeComposed(sk_sp<SkColorFilter> innerFilter) const {
reed8a8d8412015-03-02 13:46:03 -0800354 SkBitmap innerBM;
355 if (!innerFilter->asComponentTable(&innerBM)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700356 return nullptr;
reed8a8d8412015-03-02 13:46:03 -0800357 }
358
halcanary96fcdcc2015-08-27 07:41:13 -0700359 if (nullptr == innerBM.getPixels()) {
360 return nullptr;
reed8a8d8412015-03-02 13:46:03 -0800361 }
362
363 const uint8_t* table = fStorage;
364 const uint8_t* tableA = gIdentityTable;
365 const uint8_t* tableR = gIdentityTable;
366 const uint8_t* tableG = gIdentityTable;
367 const uint8_t* tableB = gIdentityTable;
368 if (fFlags & kA_Flag) {
369 tableA = table; table += 256;
370 }
371 if (fFlags & kR_Flag) {
372 tableR = table; table += 256;
373 }
374 if (fFlags & kG_Flag) {
375 tableG = table; table += 256;
376 }
377 if (fFlags & kB_Flag) {
378 tableB = table;
379 }
380
381 uint8_t concatA[256];
382 uint8_t concatR[256];
383 uint8_t concatG[256];
384 uint8_t concatB[256];
385
386 combine_tables(concatA, tableA, innerBM.getAddr8(0, 0));
387 combine_tables(concatR, tableR, innerBM.getAddr8(0, 1));
388 combine_tables(concatG, tableG, innerBM.getAddr8(0, 2));
389 combine_tables(concatB, tableB, innerBM.getAddr8(0, 3));
390
reedd053ce92016-03-22 10:17:23 -0700391 return SkTableColorFilter::MakeARGB(concatA, concatR, concatG, concatB);
reed8a8d8412015-03-02 13:46:03 -0800392}
393
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000394#if SK_SUPPORT_GPU
395
brianosmana6359362016-03-21 06:55:37 -0700396#include "GrContext.h"
bsalomon6251d172014-10-15 10:50:36 -0700397#include "GrFragmentProcessor.h"
ajuma95243eb2016-08-24 08:19:02 -0700398#include "GrTextureStripAtlas.h"
bsalomonc6327a82014-10-27 12:53:08 -0700399#include "SkGr.h"
egdaniel64c47282015-11-13 06:54:19 -0800400#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -0800401#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -0700402#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -0800403#include "glsl/GrGLSLUniformHandler.h"
bsalomonc6327a82014-10-27 12:53:08 -0700404
joshualittb0a8a372014-09-23 09:50:21 -0700405class ColorTableEffect : public GrFragmentProcessor {
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000406public:
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500407 static sk_sp<GrFragmentProcessor> Make(GrContext* context,
408 const SkBitmap& bitmap,
409 unsigned flags);
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000410
Robert Phillips30f9bc62017-02-22 15:28:38 -0500411 ~ColorTableEffect() override;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000412
mtklein36352bf2015-03-25 18:17:31 -0700413 const char* name() const override { return "ColorTable"; }
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000414
bsalomonc6327a82014-10-27 12:53:08 -0700415 const GrTextureStripAtlas* atlas() const { return fAtlas; }
416 int atlasRow() const { return fRow; }
417
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000418private:
egdaniel57d3b032015-11-13 11:57:27 -0800419 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -0700420
Brian Salomon94efbf52016-11-29 13:43:05 -0500421 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -0700422
mtklein36352bf2015-03-25 18:17:31 -0700423 bool onIsEqual(const GrFragmentProcessor&) const override;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000424
Robert Phillips26c90e02017-03-14 14:39:29 -0400425 ColorTableEffect(GrResourceProvider* , sk_sp<GrTextureProxy> proxy,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500426 GrTextureStripAtlas* atlas, int row, unsigned flags);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000427
joshualittb0a8a372014-09-23 09:50:21 -0700428 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000429
Brian Salomoncb30bb22017-02-12 09:28:54 -0500430 TextureSampler fTextureSampler;
431 GrTextureStripAtlas* fAtlas;
432 int fRow;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000433
joshualittb0a8a372014-09-23 09:50:21 -0700434 typedef GrFragmentProcessor INHERITED;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000435};
436
egdaniel64c47282015-11-13 06:54:19 -0800437class GLColorTableEffect : public GrGLSLFragmentProcessor {
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000438public:
robertphillips9cdb9922016-02-03 12:25:40 -0800439 void emitCode(EmitArgs&) override;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000440
Brian Salomon94efbf52016-11-29 13:43:05 -0500441 static void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*) {}
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000442
wangyixb1daa862015-08-18 11:29:31 -0700443protected:
Brian Salomonab015ef2017-04-04 10:15:51 -0400444 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -0700445
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000446private:
bsalomonc6327a82014-10-27 12:53:08 -0700447 UniformHandle fRGBAYValuesUni;
egdaniel64c47282015-11-13 06:54:19 -0800448 typedef GrGLSLFragmentProcessor INHERITED;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000449};
450
Brian Salomonab015ef2017-04-04 10:15:51 -0400451void GLColorTableEffect::onSetData(const GrGLSLProgramDataManager& pdm,
452 const GrFragmentProcessor& proc) {
bsalomonc6327a82014-10-27 12:53:08 -0700453 // The textures are organized in a strip where the rows are ordered a, r, g, b.
454 float rgbaYValues[4];
455 const ColorTableEffect& cte = proc.cast<ColorTableEffect>();
456 if (cte.atlas()) {
457 SkScalar yDelta = cte.atlas()->getNormalizedTexelHeight();
458 rgbaYValues[3] = cte.atlas()->getYOffset(cte.atlasRow()) + SK_ScalarHalf * yDelta;
459 rgbaYValues[0] = rgbaYValues[3] + yDelta;
460 rgbaYValues[1] = rgbaYValues[0] + yDelta;
461 rgbaYValues[2] = rgbaYValues[1] + yDelta;
462 } else {
463 rgbaYValues[3] = 0.125;
464 rgbaYValues[0] = 0.375;
465 rgbaYValues[1] = 0.625;
mtklein3f3b3d02014-12-01 11:47:08 -0800466 rgbaYValues[2] = 0.875;
bsalomonc6327a82014-10-27 12:53:08 -0700467 }
468 pdm.set4fv(fRGBAYValuesUni, 1, rgbaYValues);
469}
470
wangyix7c157a92015-07-22 15:08:53 -0700471void GLColorTableEffect::emitCode(EmitArgs& args) {
bsalomonc6327a82014-10-27 12:53:08 -0700472 const char* yoffsets;
cdalton5e58cee2016-02-11 12:49:47 -0800473 fRGBAYValuesUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800474 kVec4f_GrSLType, kDefault_GrSLPrecision,
475 "yoffsets", &yoffsets);
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000476 static const float kColorScaleFactor = 255.0f / 256.0f;
477 static const float kColorOffsetFactor = 1.0f / 512.0f;
cdalton85285412016-02-18 12:37:07 -0800478 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
halcanary96fcdcc2015-08-27 07:41:13 -0700479 if (nullptr == args.fInputColor) {
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000480 // the input color is solid white (all ones).
481 static const float kMaxValue = kColorScaleFactor + kColorOffsetFactor;
egdaniel4ca2e602015-11-18 08:01:26 -0800482 fragBuilder->codeAppendf("\t\tvec4 coord = vec4(%f, %f, %f, %f);\n",
483 kMaxValue, kMaxValue, kMaxValue, kMaxValue);
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000484
485 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800486 fragBuilder->codeAppendf("\t\tfloat nonZeroAlpha = max(%s.a, .0001);\n", args.fInputColor);
487 fragBuilder->codeAppendf("\t\tvec4 coord = vec4(%s.rgb / nonZeroAlpha, nonZeroAlpha);\n",
488 args.fInputColor);
489 fragBuilder->codeAppendf("\t\tcoord = coord * %f + vec4(%f, %f, %f, %f);\n",
490 kColorScaleFactor,
491 kColorOffsetFactor, kColorOffsetFactor,
492 kColorOffsetFactor, kColorOffsetFactor);
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000493 }
494
bsalomonc6327a82014-10-27 12:53:08 -0700495 SkString coord;
496
egdaniel4ca2e602015-11-18 08:01:26 -0800497 fragBuilder->codeAppendf("\t\t%s.a = ", args.fOutputColor);
bsalomonc6327a82014-10-27 12:53:08 -0700498 coord.printf("vec2(coord.a, %s.a)", yoffsets);
cdalton3f6f76f2016-04-11 12:18:09 -0700499 fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
bsalomoncdee0092016-01-08 13:20:12 -0800500 fragBuilder->codeAppend(".a;\n");
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000501
egdaniel4ca2e602015-11-18 08:01:26 -0800502 fragBuilder->codeAppendf("\t\t%s.r = ", args.fOutputColor);
bsalomonc6327a82014-10-27 12:53:08 -0700503 coord.printf("vec2(coord.r, %s.r)", yoffsets);
cdalton3f6f76f2016-04-11 12:18:09 -0700504 fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
bsalomoncdee0092016-01-08 13:20:12 -0800505 fragBuilder->codeAppend(".a;\n");
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000506
egdaniel4ca2e602015-11-18 08:01:26 -0800507 fragBuilder->codeAppendf("\t\t%s.g = ", args.fOutputColor);
bsalomonc6327a82014-10-27 12:53:08 -0700508 coord.printf("vec2(coord.g, %s.g)", yoffsets);
cdalton3f6f76f2016-04-11 12:18:09 -0700509 fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
bsalomoncdee0092016-01-08 13:20:12 -0800510 fragBuilder->codeAppend(".a;\n");
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000511
egdaniel4ca2e602015-11-18 08:01:26 -0800512 fragBuilder->codeAppendf("\t\t%s.b = ", args.fOutputColor);
bsalomonc6327a82014-10-27 12:53:08 -0700513 coord.printf("vec2(coord.b, %s.b)", yoffsets);
cdalton3f6f76f2016-04-11 12:18:09 -0700514 fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str());
bsalomoncdee0092016-01-08 13:20:12 -0800515 fragBuilder->codeAppend(".a;\n");
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000516
egdaniel4ca2e602015-11-18 08:01:26 -0800517 fragBuilder->codeAppendf("\t\t%s.rgb *= %s.a;\n", args.fOutputColor, args.fOutputColor);
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000518}
519
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000520///////////////////////////////////////////////////////////////////////////////
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500521sk_sp<GrFragmentProcessor> ColorTableEffect::Make(GrContext* context, const SkBitmap& bitmap,
bungeman06ca8ec2016-06-09 08:01:03 -0700522 unsigned flags) {
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000523
bsalomonc6327a82014-10-27 12:53:08 -0700524 GrTextureStripAtlas::Desc desc;
525 desc.fWidth = bitmap.width();
526 desc.fHeight = 128;
527 desc.fRowHeight = bitmap.height();
Robert Phillips26c90e02017-03-14 14:39:29 -0400528
529 // TODO: this seems a bit heavy handed (passing a GrContext as part of the desc)
bsalomonc6327a82014-10-27 12:53:08 -0700530 desc.fContext = context;
brianosmana6359362016-03-21 06:55:37 -0700531 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap.info(), *context->caps());
bsalomonc6327a82014-10-27 12:53:08 -0700532 GrTextureStripAtlas* atlas = GrTextureStripAtlas::GetAtlas(desc);
533 int row = atlas->lockRow(bitmap);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500534 sk_sp<GrTextureProxy> proxy;
bsalomonc6327a82014-10-27 12:53:08 -0700535 if (-1 == row) {
halcanary96fcdcc2015-08-27 07:41:13 -0700536 atlas = nullptr;
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500537
Robert Phillips26c90e02017-03-14 14:39:29 -0400538 proxy = GrMakeCachedBitmapProxy(context->resourceProvider(), bitmap);
bsalomonc6327a82014-10-27 12:53:08 -0700539 } else {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500540 proxy = atlas->asTextureProxyRef();
bsalomonc6327a82014-10-27 12:53:08 -0700541 }
542
Robert Phillips30f9bc62017-02-22 15:28:38 -0500543 if (!proxy) {
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500544 return nullptr;
545 }
546
Robert Phillips26c90e02017-03-14 14:39:29 -0400547 return sk_sp<GrFragmentProcessor>(new ColorTableEffect(context->resourceProvider(),
548 std::move(proxy),
Robert Phillips30f9bc62017-02-22 15:28:38 -0500549 atlas, row, flags));
bsalomonc6327a82014-10-27 12:53:08 -0700550}
551
Robert Phillips26c90e02017-03-14 14:39:29 -0400552ColorTableEffect::ColorTableEffect(GrResourceProvider* resourceProvider,
553 sk_sp<GrTextureProxy> proxy,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500554 GrTextureStripAtlas* atlas, int row, unsigned flags)
Brian Salomon587e08f2017-01-27 10:59:27 -0500555 : INHERITED(kNone_OptimizationFlags) // Not bothering with table-specific optimizations.
Robert Phillips26c90e02017-03-14 14:39:29 -0400556 , fTextureSampler(resourceProvider, std::move(proxy))
Brian Salomon587e08f2017-01-27 10:59:27 -0500557 , fAtlas(atlas)
558 , fRow(row) {
joshualitteb2a6762014-12-04 11:35:33 -0800559 this->initClassID<ColorTableEffect>();
Brian Salomon0bbecb22016-11-17 11:38:22 -0500560 this->addTextureSampler(&fTextureSampler);
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000561}
562
563ColorTableEffect::~ColorTableEffect() {
bsalomonc6327a82014-10-27 12:53:08 -0700564 if (fAtlas) {
565 fAtlas->unlockRow(fRow);
566 }
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000567}
568
Brian Salomon94efbf52016-11-29 13:43:05 -0500569void ColorTableEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800570 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800571 GLColorTableEffect::GenKey(*this, caps, b);
572}
573
egdaniel57d3b032015-11-13 11:57:27 -0800574GrGLSLFragmentProcessor* ColorTableEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800575 return new GLColorTableEffect;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000576}
577
bsalomonc6327a82014-10-27 12:53:08 -0700578bool ColorTableEffect::onIsEqual(const GrFragmentProcessor& other) const {
579 // For non-atlased instances, the texture (compared by base class) is sufficient to
580 // differentiate different tables. For atlased instances we ensure they are using the
581 // same row.
582 const ColorTableEffect& that = other.cast<ColorTableEffect>();
583 SkASSERT(SkToBool(fAtlas) == SkToBool(that.fAtlas));
584 // Ok to always do this comparison since both would be -1 if non-atlased.
585 return fRow == that.fRow;
586}
587
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000588///////////////////////////////////////////////////////////////////////////////
589
joshualittb0a8a372014-09-23 09:50:21 -0700590GR_DEFINE_FRAGMENT_PROCESSOR_TEST(ColorTableEffect);
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000591
Hal Canary6f6961e2017-01-31 13:50:44 -0500592#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700593sk_sp<GrFragmentProcessor> ColorTableEffect::TestCreate(GrProcessorTestData* d) {
bsalomonc6327a82014-10-27 12:53:08 -0700594 int flags = 0;
595 uint8_t luts[256][4];
596 do {
597 for (int i = 0; i < 4; ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700598 flags |= d->fRandom->nextBool() ? (1 << i): 0;
bsalomonc6327a82014-10-27 12:53:08 -0700599 }
600 } while (!flags);
601 for (int i = 0; i < 4; ++i) {
602 if (flags & (1 << i)) {
603 for (int j = 0; j < 256; ++j) {
joshualitt0067ff52015-07-08 14:26:19 -0700604 luts[j][i] = SkToU8(d->fRandom->nextBits(8));
bsalomonc6327a82014-10-27 12:53:08 -0700605 }
606 }
607 }
reedd053ce92016-03-22 10:17:23 -0700608 auto filter(SkTableColorFilter::MakeARGB(
halcanary96fcdcc2015-08-27 07:41:13 -0700609 (flags & (1 << 0)) ? luts[0] : nullptr,
610 (flags & (1 << 1)) ? luts[1] : nullptr,
611 (flags & (1 << 2)) ? luts[2] : nullptr,
612 (flags & (1 << 3)) ? luts[3] : nullptr
bsalomonc6327a82014-10-27 12:53:08 -0700613 ));
Brian Osman618d3042016-10-25 10:51:28 -0400614 sk_sp<SkColorSpace> colorSpace = GrTest::TestColorSpace(d->fRandom);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500615 sk_sp<GrFragmentProcessor> fp = filter->asFragmentProcessor(d->context(), colorSpace.get());
bsalomone25eea42015-09-29 06:38:55 -0700616 SkASSERT(fp);
617 return fp;
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000618}
Hal Canary6f6961e2017-01-31 13:50:44 -0500619#endif
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000620
Brian Osman618d3042016-10-25 10:51:28 -0400621sk_sp<GrFragmentProcessor> SkTable_ColorFilter::asFragmentProcessor(GrContext* context,
622 SkColorSpace*) const {
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000623 SkBitmap bitmap;
624 this->asComponentTable(&bitmap);
bsalomonc6327a82014-10-27 12:53:08 -0700625
bungeman06ca8ec2016-06-09 08:01:03 -0700626 return ColorTableEffect::Make(context, bitmap, fFlags);
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000627}
628
629#endif // SK_SUPPORT_GPU
630
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000631///////////////////////////////////////////////////////////////////////////////
632
633#ifdef SK_CPU_BENDIAN
634#else
635 #define SK_A32_INDEX (3 - (SK_A32_SHIFT >> 3))
636 #define SK_R32_INDEX (3 - (SK_R32_SHIFT >> 3))
637 #define SK_G32_INDEX (3 - (SK_G32_SHIFT >> 3))
638 #define SK_B32_INDEX (3 - (SK_B32_SHIFT >> 3))
639#endif
640
641///////////////////////////////////////////////////////////////////////////////
642
reedd053ce92016-03-22 10:17:23 -0700643sk_sp<SkColorFilter> SkTableColorFilter::Make(const uint8_t table[256]) {
644 return sk_make_sp<SkTable_ColorFilter>(table, table, table, table);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000645}
646
reedd053ce92016-03-22 10:17:23 -0700647sk_sp<SkColorFilter> SkTableColorFilter::MakeARGB(const uint8_t tableA[256],
648 const uint8_t tableR[256],
649 const uint8_t tableG[256],
650 const uint8_t tableB[256]) {
651 return sk_make_sp<SkTable_ColorFilter>(tableA, tableR, tableG, tableB);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000652}
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000653
654SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkTableColorFilter)
655 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTable_ColorFilter)
656SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END