jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 1 | /* |
Mike Reed | b9641bd | 2017-05-04 10:57:40 -0400 | [diff] [blame] | 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 | */ |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 7 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 8 | #include "SkTableColorFilter.h" |
Mike Reed | b9641bd | 2017-05-04 10:57:40 -0400 | [diff] [blame] | 9 | #include "SkPM4f.h" |
Mike Klein | a9e8ef0 | 2017-01-23 14:06:55 -0500 | [diff] [blame] | 10 | #include "SkArenaAlloc.h" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 11 | #include "SkBitmap.h" |
Cary Clark | a4083c9 | 2017-09-15 11:59:23 -0400 | [diff] [blame] | 12 | #include "SkColorData.h" |
Mike Klein | a9e8ef0 | 2017-01-23 14:06:55 -0500 | [diff] [blame] | 13 | #include "SkRasterPipeline.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 14 | #include "SkReadBuffer.h" |
robertphillips@google.com | 1202c2a | 2013-05-23 14:00:17 +0000 | [diff] [blame] | 15 | #include "SkString.h" |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 16 | #include "SkUnPreMultiply.h" |
| 17 | #include "SkWriteBuffer.h" |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 18 | |
Mike Klein | a9e8ef0 | 2017-01-23 14:06:55 -0500 | [diff] [blame] | 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 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 54 | class SkTable_ColorFilter : public SkColorFilter { |
| 55 | public: |
| 56 | SkTable_ColorFilter(const uint8_t tableA[], const uint8_t tableR[], |
| 57 | const uint8_t tableG[], const uint8_t tableB[]) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 58 | fBitmap = nullptr; |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 59 | fFlags = 0; |
mike@reedtribe.org | f23f5f7 | 2012-01-07 03:48:45 +0000 | [diff] [blame] | 60 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 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 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 83 | ~SkTable_ColorFilter() override { delete fBitmap; } |
tomhudson@google.com | 1bb4be2 | 2012-07-24 17:24:21 +0000 | [diff] [blame] | 84 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 85 | bool asComponentTable(SkBitmap* table) const override; |
Mike Reed | 19d7bd6 | 2018-02-19 14:10:57 -0500 | [diff] [blame] | 86 | sk_sp<SkColorFilter> onMakeComposed(sk_sp<SkColorFilter> inner) const override; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 87 | |
| 88 | #if SK_SUPPORT_GPU |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 89 | std::unique_ptr<GrFragmentProcessor> asFragmentProcessor( |
| 90 | GrContext*, const GrColorSpaceInfo&) const override; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 91 | #endif |
mike@reedtribe.org | f23f5f7 | 2012-01-07 03:48:45 +0000 | [diff] [blame] | 92 | |
Cary Clark | 89ad31e | 2018-05-31 12:27:33 +0000 | [diff] [blame] | 93 | void toString(SkString* str) const override; |
| 94 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 95 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTable_ColorFilter) |
mike@reedtribe.org | f23f5f7 | 2012-01-07 03:48:45 +0000 | [diff] [blame] | 96 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 97 | enum { |
| 98 | kA_Flag = 1 << 0, |
| 99 | kR_Flag = 1 << 1, |
| 100 | kG_Flag = 1 << 2, |
| 101 | kB_Flag = 1 << 3, |
| 102 | }; |
| 103 | |
Mike Klein | fdf3103 | 2017-05-09 14:57:58 -0400 | [diff] [blame] | 104 | void onAppendStages(SkRasterPipeline* p, SkColorSpace*, SkArenaAlloc* alloc, |
Mike Klein | a9e8ef0 | 2017-01-23 14:06:55 -0500 | [diff] [blame] | 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 | } |
Mike Klein | a9e8ef0 | 2017-01-23 14:06:55 -0500 | [diff] [blame] | 127 | } |
| 128 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 129 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 130 | void flatten(SkWriteBuffer&) const override; |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 131 | |
| 132 | private: |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 133 | mutable const SkBitmap* fBitmap; // lazily allocated |
mike@reedtribe.org | f23f5f7 | 2012-01-07 03:48:45 +0000 | [diff] [blame] | 134 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 135 | uint8_t fStorage[256 * 4]; |
| 136 | unsigned fFlags; |
| 137 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 138 | friend class SkTableColorFilter; |
| 139 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 140 | typedef SkColorFilter INHERITED; |
| 141 | }; |
| 142 | |
Cary Clark | 89ad31e | 2018-05-31 12:27:33 +0000 | [diff] [blame] | 143 | void SkTable_ColorFilter::toString(SkString* str) const { |
| 144 | const uint8_t* table = fStorage; |
| 145 | const uint8_t* tableA = gIdentityTable; |
| 146 | const uint8_t* tableR = gIdentityTable; |
| 147 | const uint8_t* tableG = gIdentityTable; |
| 148 | const uint8_t* tableB = gIdentityTable; |
| 149 | if (fFlags & kA_Flag) { |
| 150 | tableA = table; table += 256; |
| 151 | } |
| 152 | if (fFlags & kR_Flag) { |
| 153 | tableR = table; table += 256; |
| 154 | } |
| 155 | if (fFlags & kG_Flag) { |
| 156 | tableG = table; table += 256; |
| 157 | } |
| 158 | if (fFlags & kB_Flag) { |
| 159 | tableB = table; |
| 160 | } |
| 161 | |
| 162 | str->append("SkTable_ColorFilter ("); |
| 163 | |
| 164 | for (int i = 0; i < 256; ++i) { |
| 165 | str->appendf("%d: %d,%d,%d,%d\n", |
| 166 | i, tableR[i], tableG[i], tableB[i], tableA[i]); |
| 167 | } |
| 168 | |
| 169 | str->append(")"); |
| 170 | } |
| 171 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 172 | static const uint8_t gCountNibBits[] = { |
| 173 | 0, 1, 1, 2, |
| 174 | 1, 2, 2, 3, |
| 175 | 1, 2, 2, 3, |
| 176 | 2, 3, 3, 4 |
| 177 | }; |
| 178 | |
| 179 | #include "SkPackBits.h" |
| 180 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 181 | void SkTable_ColorFilter::flatten(SkWriteBuffer& buffer) const { |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 182 | uint8_t storage[5*256]; |
| 183 | int count = gCountNibBits[fFlags & 0xF]; |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 184 | size_t size = SkPackBits::Pack8(fStorage, count * 256, storage, |
| 185 | sizeof(storage)); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 186 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 187 | buffer.write32(fFlags); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 188 | buffer.writeByteArray(storage, size); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 189 | } |
| 190 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 191 | sk_sp<SkFlattenable> SkTable_ColorFilter::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 192 | const int flags = buffer.read32(); |
| 193 | const size_t count = gCountNibBits[flags & 0xF]; |
| 194 | SkASSERT(count <= 4); |
| 195 | |
| 196 | uint8_t packedStorage[5*256]; |
| 197 | size_t packedSize = buffer.getArrayCount(); |
| 198 | if (!buffer.validate(packedSize <= sizeof(packedStorage))) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 199 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 200 | } |
| 201 | if (!buffer.readByteArray(packedStorage, packedSize)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 202 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | uint8_t unpackedStorage[4*256]; |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 206 | size_t unpackedSize = SkPackBits::Unpack8(packedStorage, packedSize, |
| 207 | unpackedStorage, sizeof(unpackedStorage)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 208 | // now check that we got the size we expected |
senorblanco | 91c395a | 2014-09-25 15:51:35 -0700 | [diff] [blame] | 209 | if (!buffer.validate(unpackedSize == count*256)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 210 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 211 | } |
| 212 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 213 | const uint8_t* a = nullptr; |
| 214 | const uint8_t* r = nullptr; |
| 215 | const uint8_t* g = nullptr; |
| 216 | const uint8_t* b = nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 217 | const uint8_t* ptr = unpackedStorage; |
| 218 | |
| 219 | if (flags & kA_Flag) { |
| 220 | a = ptr; |
| 221 | ptr += 256; |
| 222 | } |
| 223 | if (flags & kR_Flag) { |
| 224 | r = ptr; |
| 225 | ptr += 256; |
| 226 | } |
| 227 | if (flags & kG_Flag) { |
| 228 | g = ptr; |
| 229 | ptr += 256; |
| 230 | } |
| 231 | if (flags & kB_Flag) { |
| 232 | b = ptr; |
| 233 | ptr += 256; |
| 234 | } |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 235 | return SkTableColorFilter::MakeARGB(a, r, g, b); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 236 | } |
| 237 | |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 238 | bool SkTable_ColorFilter::asComponentTable(SkBitmap* table) const { |
mike@reedtribe.org | f23f5f7 | 2012-01-07 03:48:45 +0000 | [diff] [blame] | 239 | if (table) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 240 | if (nullptr == fBitmap) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 241 | SkBitmap* bmp = new SkBitmap; |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 242 | bmp->allocPixels(SkImageInfo::MakeA8(256, 4)); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 243 | uint8_t* bitmapPixels = bmp->getAddr8(0, 0); |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 244 | int offset = 0; |
| 245 | static const unsigned kFlags[] = { kA_Flag, kR_Flag, kG_Flag, kB_Flag }; |
| 246 | |
| 247 | for (int x = 0; x < 4; ++x) { |
| 248 | if (!(fFlags & kFlags[x])) { |
| 249 | memcpy(bitmapPixels, gIdentityTable, sizeof(gIdentityTable)); |
| 250 | } else { |
| 251 | memcpy(bitmapPixels, fStorage + offset, 256); |
| 252 | offset += 256; |
| 253 | } |
| 254 | bitmapPixels += 256; |
| 255 | } |
Robert Phillips | 19d311b | 2018-03-26 14:32:10 -0400 | [diff] [blame] | 256 | bmp->setImmutable(); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 257 | fBitmap = bmp; |
mike@reedtribe.org | f23f5f7 | 2012-01-07 03:48:45 +0000 | [diff] [blame] | 258 | } |
| 259 | *table = *fBitmap; |
| 260 | } |
| 261 | return true; |
| 262 | } |
| 263 | |
reed | 8a8d841 | 2015-03-02 13:46:03 -0800 | [diff] [blame] | 264 | // Combines the two lookup tables so that making a lookup using res[] has |
| 265 | // the same effect as making a lookup through inner[] then outer[]. |
| 266 | static void combine_tables(uint8_t res[256], const uint8_t outer[256], const uint8_t inner[256]) { |
| 267 | for (int i = 0; i < 256; i++) { |
| 268 | res[i] = outer[inner[i]]; |
| 269 | } |
| 270 | } |
| 271 | |
Mike Reed | 19d7bd6 | 2018-02-19 14:10:57 -0500 | [diff] [blame] | 272 | sk_sp<SkColorFilter> SkTable_ColorFilter::onMakeComposed(sk_sp<SkColorFilter> innerFilter) const { |
reed | 8a8d841 | 2015-03-02 13:46:03 -0800 | [diff] [blame] | 273 | SkBitmap innerBM; |
| 274 | if (!innerFilter->asComponentTable(&innerBM)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 275 | return nullptr; |
reed | 8a8d841 | 2015-03-02 13:46:03 -0800 | [diff] [blame] | 276 | } |
| 277 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 278 | if (nullptr == innerBM.getPixels()) { |
| 279 | return nullptr; |
reed | 8a8d841 | 2015-03-02 13:46:03 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | const uint8_t* table = fStorage; |
| 283 | const uint8_t* tableA = gIdentityTable; |
| 284 | const uint8_t* tableR = gIdentityTable; |
| 285 | const uint8_t* tableG = gIdentityTable; |
| 286 | const uint8_t* tableB = gIdentityTable; |
| 287 | if (fFlags & kA_Flag) { |
| 288 | tableA = table; table += 256; |
| 289 | } |
| 290 | if (fFlags & kR_Flag) { |
| 291 | tableR = table; table += 256; |
| 292 | } |
| 293 | if (fFlags & kG_Flag) { |
| 294 | tableG = table; table += 256; |
| 295 | } |
| 296 | if (fFlags & kB_Flag) { |
| 297 | tableB = table; |
| 298 | } |
| 299 | |
| 300 | uint8_t concatA[256]; |
| 301 | uint8_t concatR[256]; |
| 302 | uint8_t concatG[256]; |
| 303 | uint8_t concatB[256]; |
| 304 | |
| 305 | combine_tables(concatA, tableA, innerBM.getAddr8(0, 0)); |
| 306 | combine_tables(concatR, tableR, innerBM.getAddr8(0, 1)); |
| 307 | combine_tables(concatG, tableG, innerBM.getAddr8(0, 2)); |
| 308 | combine_tables(concatB, tableB, innerBM.getAddr8(0, 3)); |
| 309 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 310 | return SkTableColorFilter::MakeARGB(concatA, concatR, concatG, concatB); |
reed | 8a8d841 | 2015-03-02 13:46:03 -0800 | [diff] [blame] | 311 | } |
| 312 | |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 313 | #if SK_SUPPORT_GPU |
| 314 | |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 315 | #include "GrColorSpaceInfo.h" |
brianosman | a635936 | 2016-03-21 06:55:37 -0700 | [diff] [blame] | 316 | #include "GrContext.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 317 | #include "GrContextPriv.h" |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 318 | #include "GrFragmentProcessor.h" |
ajuma | 95243eb | 2016-08-24 08:19:02 -0700 | [diff] [blame] | 319 | #include "GrTextureStripAtlas.h" |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 320 | #include "SkGr.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 321 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 322 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 323 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 324 | #include "glsl/GrGLSLUniformHandler.h" |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 325 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 326 | class ColorTableEffect : public GrFragmentProcessor { |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 327 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 328 | static std::unique_ptr<GrFragmentProcessor> Make(GrContext* context, const SkBitmap& bitmap); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 329 | |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 330 | ~ColorTableEffect() override; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 331 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 332 | const char* name() const override { return "ColorTable"; } |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 333 | |
Robert Phillips | 96b6d53 | 2018-03-19 10:57:42 -0400 | [diff] [blame] | 334 | const GrTextureStripAtlas* atlas() const { return fAtlas.get(); } |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 335 | int atlasRow() const { return fRow; } |
| 336 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 337 | std::unique_ptr<GrFragmentProcessor> clone() const override; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 338 | |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 339 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 340 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 341 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 342 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 343 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 344 | bool onIsEqual(const GrFragmentProcessor&) const override; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 345 | |
Robert Phillips | 96b6d53 | 2018-03-19 10:57:42 -0400 | [diff] [blame] | 346 | ColorTableEffect(sk_sp<GrTextureProxy> proxy, sk_sp<GrTextureStripAtlas> atlas, int row); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 347 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 348 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 349 | |
Brian Salomon | cb30bb2 | 2017-02-12 09:28:54 -0500 | [diff] [blame] | 350 | TextureSampler fTextureSampler; |
Robert Phillips | 96b6d53 | 2018-03-19 10:57:42 -0400 | [diff] [blame] | 351 | sk_sp<GrTextureStripAtlas> fAtlas; |
Brian Salomon | cb30bb2 | 2017-02-12 09:28:54 -0500 | [diff] [blame] | 352 | int fRow; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 353 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 354 | typedef GrFragmentProcessor INHERITED; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 355 | }; |
| 356 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 357 | class GLColorTableEffect : public GrGLSLFragmentProcessor { |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 358 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 359 | void emitCode(EmitArgs&) override; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 360 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 361 | static void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*) {} |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 362 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 363 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 364 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 365 | |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 366 | private: |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 367 | UniformHandle fRGBAYValuesUni; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 368 | typedef GrGLSLFragmentProcessor INHERITED; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 369 | }; |
| 370 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 371 | void GLColorTableEffect::onSetData(const GrGLSLProgramDataManager& pdm, |
| 372 | const GrFragmentProcessor& proc) { |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 373 | // The textures are organized in a strip where the rows are ordered a, r, g, b. |
| 374 | float rgbaYValues[4]; |
| 375 | const ColorTableEffect& cte = proc.cast<ColorTableEffect>(); |
| 376 | if (cte.atlas()) { |
| 377 | SkScalar yDelta = cte.atlas()->getNormalizedTexelHeight(); |
| 378 | rgbaYValues[3] = cte.atlas()->getYOffset(cte.atlasRow()) + SK_ScalarHalf * yDelta; |
| 379 | rgbaYValues[0] = rgbaYValues[3] + yDelta; |
| 380 | rgbaYValues[1] = rgbaYValues[0] + yDelta; |
| 381 | rgbaYValues[2] = rgbaYValues[1] + yDelta; |
| 382 | } else { |
| 383 | rgbaYValues[3] = 0.125; |
| 384 | rgbaYValues[0] = 0.375; |
| 385 | rgbaYValues[1] = 0.625; |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 386 | rgbaYValues[2] = 0.875; |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 387 | } |
| 388 | pdm.set4fv(fRGBAYValuesUni, 1, rgbaYValues); |
| 389 | } |
| 390 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 391 | void GLColorTableEffect::emitCode(EmitArgs& args) { |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 392 | const char* yoffsets; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 393 | fRGBAYValuesUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 394 | "yoffsets", &yoffsets); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 395 | static const float kColorScaleFactor = 255.0f / 256.0f; |
| 396 | static const float kColorOffsetFactor = 1.0f / 512.0f; |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 397 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 398 | if (nullptr == args.fInputColor) { |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 399 | // the input color is solid white (all ones). |
| 400 | static const float kMaxValue = kColorScaleFactor + kColorOffsetFactor; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 401 | fragBuilder->codeAppendf("\t\thalf4 coord = half4(%f, %f, %f, %f);\n", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 402 | kMaxValue, kMaxValue, kMaxValue, kMaxValue); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 403 | |
| 404 | } else { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 405 | fragBuilder->codeAppendf("\t\thalf nonZeroAlpha = max(%s.a, .0001);\n", args.fInputColor); |
| 406 | fragBuilder->codeAppendf("\t\thalf4 coord = half4(%s.rgb / nonZeroAlpha, nonZeroAlpha);\n", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 407 | args.fInputColor); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 408 | fragBuilder->codeAppendf("\t\tcoord = coord * %f + half4(%f, %f, %f, %f);\n", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 409 | kColorScaleFactor, |
| 410 | kColorOffsetFactor, kColorOffsetFactor, |
| 411 | kColorOffsetFactor, kColorOffsetFactor); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 412 | } |
| 413 | |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 414 | SkString coord; |
| 415 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 416 | fragBuilder->codeAppendf("\t\t%s.a = ", args.fOutputColor); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 417 | coord.printf("half2(coord.a, %s.a)", yoffsets); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 418 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str()); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 419 | fragBuilder->codeAppend(".a;\n"); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 420 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 421 | fragBuilder->codeAppendf("\t\t%s.r = ", args.fOutputColor); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 422 | coord.printf("half2(coord.r, %s.r)", yoffsets); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 423 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str()); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 424 | fragBuilder->codeAppend(".a;\n"); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 425 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 426 | fragBuilder->codeAppendf("\t\t%s.g = ", args.fOutputColor); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 427 | coord.printf("half2(coord.g, %s.g)", yoffsets); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 428 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str()); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 429 | fragBuilder->codeAppend(".a;\n"); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 430 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 431 | fragBuilder->codeAppendf("\t\t%s.b = ", args.fOutputColor); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 432 | coord.printf("half2(coord.b, %s.b)", yoffsets); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 433 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], coord.c_str()); |
bsalomon | cdee009 | 2016-01-08 13:20:12 -0800 | [diff] [blame] | 434 | fragBuilder->codeAppend(".a;\n"); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 435 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 436 | fragBuilder->codeAppendf("\t\t%s.rgb *= %s.a;\n", args.fOutputColor, args.fOutputColor); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 437 | } |
| 438 | |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 439 | /////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 440 | std::unique_ptr<GrFragmentProcessor> ColorTableEffect::Make(GrContext* context, |
| 441 | const SkBitmap& bitmap) { |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 442 | GrTextureStripAtlas::Desc desc; |
| 443 | desc.fWidth = bitmap.width(); |
| 444 | desc.fHeight = 128; |
| 445 | desc.fRowHeight = bitmap.height(); |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 446 | desc.fConfig = SkColorType2GrPixelConfig(bitmap.colorType()); |
Robert Phillips | 7a92639 | 2018-02-01 15:49:54 -0500 | [diff] [blame] | 447 | |
Greg Daniel | 0a7aa14 | 2018-02-21 13:02:32 -0500 | [diff] [blame] | 448 | if (kUnknown_GrPixelConfig == desc.fConfig) { |
| 449 | return nullptr; |
| 450 | } |
| 451 | |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 452 | auto atlasManager = context->contextPriv().textureStripAtlasManager(); |
| 453 | |
Robert Phillips | 96b6d53 | 2018-03-19 10:57:42 -0400 | [diff] [blame] | 454 | sk_sp<GrTextureStripAtlas> atlas = atlasManager->refAtlas(desc); |
Robert Phillips | 41a3b87 | 2018-03-09 12:00:34 -0500 | [diff] [blame] | 455 | int row = atlas->lockRow(context, bitmap); |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 456 | sk_sp<GrTextureProxy> proxy; |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 457 | if (-1 == row) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 458 | atlas = nullptr; |
Robert Phillips | 6f9f7eb | 2017-02-18 15:15:51 -0500 | [diff] [blame] | 459 | |
Robert Phillips | 7a92639 | 2018-02-01 15:49:54 -0500 | [diff] [blame] | 460 | SkASSERT(bitmap.isImmutable()); |
| 461 | |
| 462 | sk_sp<SkImage> srcImage = SkImage::MakeFromBitmap(bitmap); |
| 463 | if (!srcImage) { |
| 464 | return nullptr; |
| 465 | } |
| 466 | |
| 467 | proxy = GrMakeCachedImageProxy(context->contextPriv().proxyProvider(), std::move(srcImage)); |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 468 | } else { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 469 | proxy = atlas->asTextureProxyRef(); |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 472 | if (!proxy) { |
Robert Phillips | 6f9f7eb | 2017-02-18 15:15:51 -0500 | [diff] [blame] | 473 | return nullptr; |
| 474 | } |
| 475 | |
Robert Phillips | 96b6d53 | 2018-03-19 10:57:42 -0400 | [diff] [blame] | 476 | return std::unique_ptr<GrFragmentProcessor>(new ColorTableEffect(std::move(proxy), |
| 477 | std::move(atlas), row)); |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Robert Phillips | 96b6d53 | 2018-03-19 10:57:42 -0400 | [diff] [blame] | 480 | ColorTableEffect::ColorTableEffect(sk_sp<GrTextureProxy> proxy, |
| 481 | sk_sp<GrTextureStripAtlas> atlas, int row) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 482 | : INHERITED(kColorTableEffect_ClassID, |
| 483 | kNone_OptimizationFlags) // Not bothering with table-specific optimizations. |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 484 | , fTextureSampler(std::move(proxy)) |
Robert Phillips | 96b6d53 | 2018-03-19 10:57:42 -0400 | [diff] [blame] | 485 | , fAtlas(std::move(atlas)) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 486 | , fRow(row) { |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 487 | this->addTextureSampler(&fTextureSampler); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | ColorTableEffect::~ColorTableEffect() { |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 491 | if (fAtlas) { |
| 492 | fAtlas->unlockRow(fRow); |
| 493 | } |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 496 | std::unique_ptr<GrFragmentProcessor> ColorTableEffect::clone() const { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 497 | fAtlas->lockRow(fRow); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 498 | return std::unique_ptr<GrFragmentProcessor>( |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 499 | new ColorTableEffect(sk_ref_sp(fTextureSampler.proxy()), fAtlas, fRow)); |
| 500 | } |
| 501 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 502 | void ColorTableEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 503 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 504 | GLColorTableEffect::GenKey(*this, caps, b); |
| 505 | } |
| 506 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 507 | GrGLSLFragmentProcessor* ColorTableEffect::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 508 | return new GLColorTableEffect; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 509 | } |
| 510 | |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 511 | bool ColorTableEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 512 | // For non-atlased instances, the texture (compared by base class) is sufficient to |
| 513 | // differentiate different tables. For atlased instances we ensure they are using the |
| 514 | // same row. |
| 515 | const ColorTableEffect& that = other.cast<ColorTableEffect>(); |
| 516 | SkASSERT(SkToBool(fAtlas) == SkToBool(that.fAtlas)); |
| 517 | // Ok to always do this comparison since both would be -1 if non-atlased. |
| 518 | return fRow == that.fRow; |
| 519 | } |
| 520 | |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 521 | /////////////////////////////////////////////////////////////////////////////// |
| 522 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 523 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(ColorTableEffect); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 524 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 525 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 526 | std::unique_ptr<GrFragmentProcessor> ColorTableEffect::TestCreate(GrProcessorTestData* d) { |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 527 | int flags = 0; |
| 528 | uint8_t luts[256][4]; |
| 529 | do { |
| 530 | for (int i = 0; i < 4; ++i) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 531 | flags |= d->fRandom->nextBool() ? (1 << i): 0; |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 532 | } |
| 533 | } while (!flags); |
| 534 | for (int i = 0; i < 4; ++i) { |
| 535 | if (flags & (1 << i)) { |
| 536 | for (int j = 0; j < 256; ++j) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 537 | luts[j][i] = SkToU8(d->fRandom->nextBits(8)); |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | } |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 541 | auto filter(SkTableColorFilter::MakeARGB( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 542 | (flags & (1 << 0)) ? luts[0] : nullptr, |
| 543 | (flags & (1 << 1)) ? luts[1] : nullptr, |
| 544 | (flags & (1 << 2)) ? luts[2] : nullptr, |
| 545 | (flags & (1 << 3)) ? luts[3] : nullptr |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 546 | )); |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 547 | sk_sp<SkColorSpace> colorSpace = GrTest::TestColorSpace(d->fRandom); |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 548 | auto fp = filter->asFragmentProcessor( |
| 549 | d->context(), GrColorSpaceInfo(std::move(colorSpace), kRGBA_8888_GrPixelConfig)); |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 550 | SkASSERT(fp); |
| 551 | return fp; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 552 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 553 | #endif |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 554 | |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 555 | std::unique_ptr<GrFragmentProcessor> SkTable_ColorFilter::asFragmentProcessor( |
| 556 | GrContext* context, const GrColorSpaceInfo&) const { |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 557 | SkBitmap bitmap; |
| 558 | this->asComponentTable(&bitmap); |
bsalomon | c6327a8 | 2014-10-27 12:53:08 -0700 | [diff] [blame] | 559 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 560 | return ColorTableEffect::Make(context, bitmap); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | #endif // SK_SUPPORT_GPU |
| 564 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 565 | /////////////////////////////////////////////////////////////////////////////// |
| 566 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 567 | sk_sp<SkColorFilter> SkTableColorFilter::Make(const uint8_t table[256]) { |
| 568 | return sk_make_sp<SkTable_ColorFilter>(table, table, table, table); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 569 | } |
| 570 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 571 | sk_sp<SkColorFilter> SkTableColorFilter::MakeARGB(const uint8_t tableA[256], |
| 572 | const uint8_t tableR[256], |
| 573 | const uint8_t tableG[256], |
| 574 | const uint8_t tableB[256]) { |
| 575 | return sk_make_sp<SkTable_ColorFilter>(tableA, tableR, tableG, tableB); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 576 | } |
djsollen@google.com | a2ca41e | 2012-03-23 19:00:34 +0000 | [diff] [blame] | 577 | |
| 578 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkTableColorFilter) |
| 579 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTable_ColorFilter) |
| 580 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |