djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 1 | |
reed@google.com | 02f65f2 | 2012-08-06 21:20:05 +0000 | [diff] [blame] | 2 | #include "SkBitmap.h" |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 3 | #include "SkTableColorFilter.h" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 4 | #include "SkColorPriv.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 5 | #include "SkReadBuffer.h" |
| 6 | #include "SkWriteBuffer.h" |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 7 | #include "SkUnPreMultiply.h" |
robertphillips@google.com | 1202c2a | 2013-05-23 14:00:17 +0000 | [diff] [blame] | 8 | #include "SkString.h" |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 9 | |
| 10 | class SkTable_ColorFilter : public SkColorFilter { |
| 11 | public: |
| 12 | SkTable_ColorFilter(const uint8_t tableA[], const uint8_t tableR[], |
| 13 | const uint8_t tableG[], const uint8_t tableB[]) { |
mike@reedtribe.org | f23f5f7 | 2012-01-07 03:48:45 +0000 | [diff] [blame] | 14 | fBitmap = NULL; |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 15 | fFlags = 0; |
mike@reedtribe.org | f23f5f7 | 2012-01-07 03:48:45 +0000 | [diff] [blame] | 16 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 17 | uint8_t* dst = fStorage; |
| 18 | if (tableA) { |
| 19 | memcpy(dst, tableA, 256); |
| 20 | dst += 256; |
| 21 | fFlags |= kA_Flag; |
| 22 | } |
| 23 | if (tableR) { |
| 24 | memcpy(dst, tableR, 256); |
| 25 | dst += 256; |
| 26 | fFlags |= kR_Flag; |
| 27 | } |
| 28 | if (tableG) { |
| 29 | memcpy(dst, tableG, 256); |
| 30 | dst += 256; |
| 31 | fFlags |= kG_Flag; |
| 32 | } |
| 33 | if (tableB) { |
| 34 | memcpy(dst, tableB, 256); |
| 35 | fFlags |= kB_Flag; |
| 36 | } |
| 37 | } |
| 38 | |
tomhudson@google.com | 1bb4be2 | 2012-07-24 17:24:21 +0000 | [diff] [blame] | 39 | virtual ~SkTable_ColorFilter() { |
| 40 | SkDELETE(fBitmap); |
| 41 | } |
| 42 | |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 43 | virtual bool asComponentTable(SkBitmap* table) const SK_OVERRIDE; |
| 44 | |
| 45 | #if SK_SUPPORT_GPU |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame] | 46 | virtual GrEffect* asNewEffect(GrContext* context) const SK_OVERRIDE; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 47 | #endif |
mike@reedtribe.org | f23f5f7 | 2012-01-07 03:48:45 +0000 | [diff] [blame] | 48 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 49 | virtual void filterSpan(const SkPMColor src[], int count, |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 50 | SkPMColor dst[]) const SK_OVERRIDE; |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 51 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 52 | SK_TO_STRING_OVERRIDE() |
robertphillips@google.com | 1202c2a | 2013-05-23 14:00:17 +0000 | [diff] [blame] | 53 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 54 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTable_ColorFilter) |
mike@reedtribe.org | f23f5f7 | 2012-01-07 03:48:45 +0000 | [diff] [blame] | 55 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 56 | enum { |
| 57 | kA_Flag = 1 << 0, |
| 58 | kR_Flag = 1 << 1, |
| 59 | kG_Flag = 1 << 2, |
| 60 | kB_Flag = 1 << 3, |
| 61 | }; |
| 62 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 63 | protected: |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 64 | SkTable_ColorFilter(SkReadBuffer& buffer); |
| 65 | virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 66 | |
| 67 | private: |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 68 | mutable const SkBitmap* fBitmap; // lazily allocated |
mike@reedtribe.org | f23f5f7 | 2012-01-07 03:48:45 +0000 | [diff] [blame] | 69 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 70 | uint8_t fStorage[256 * 4]; |
| 71 | unsigned fFlags; |
| 72 | |
| 73 | typedef SkColorFilter INHERITED; |
| 74 | }; |
| 75 | |
| 76 | static const uint8_t gIdentityTable[] = { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 77 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 78 | 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 79 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 80 | 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, |
| 81 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, |
| 82 | 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, |
| 83 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 84 | 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, |
| 85 | 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
| 86 | 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, |
| 87 | 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, |
| 88 | 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, |
| 89 | 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, |
| 90 | 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, |
| 91 | 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, |
| 92 | 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, |
| 93 | 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, |
| 94 | 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, |
| 95 | 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, |
| 96 | 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, |
| 97 | 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, |
| 98 | 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, |
| 99 | 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, |
| 100 | 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, |
| 101 | 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, |
| 102 | 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, |
| 103 | 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, |
| 104 | 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, |
| 105 | 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, |
| 106 | 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, |
| 107 | 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 108 | 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF |
| 109 | }; |
| 110 | |
| 111 | void SkTable_ColorFilter::filterSpan(const SkPMColor src[], int count, |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 112 | SkPMColor dst[]) const { |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 113 | const uint8_t* table = fStorage; |
| 114 | const uint8_t* tableA = gIdentityTable; |
| 115 | const uint8_t* tableR = gIdentityTable; |
| 116 | const uint8_t* tableG = gIdentityTable; |
| 117 | const uint8_t* tableB = gIdentityTable; |
| 118 | if (fFlags & kA_Flag) { |
| 119 | tableA = table; table += 256; |
| 120 | } |
| 121 | if (fFlags & kR_Flag) { |
| 122 | tableR = table; table += 256; |
| 123 | } |
| 124 | if (fFlags & kG_Flag) { |
| 125 | tableG = table; table += 256; |
| 126 | } |
| 127 | if (fFlags & kB_Flag) { |
| 128 | tableB = table; |
| 129 | } |
| 130 | |
| 131 | const SkUnPreMultiply::Scale* scaleTable = SkUnPreMultiply::GetScaleTable(); |
| 132 | for (int i = 0; i < count; ++i) { |
| 133 | SkPMColor c = src[i]; |
| 134 | unsigned a, r, g, b; |
| 135 | if (0 == c) { |
| 136 | a = r = g = b = 0; |
| 137 | } else { |
| 138 | a = SkGetPackedA32(c); |
| 139 | r = SkGetPackedR32(c); |
| 140 | g = SkGetPackedG32(c); |
| 141 | b = SkGetPackedB32(c); |
| 142 | |
| 143 | if (a < 255) { |
| 144 | SkUnPreMultiply::Scale scale = scaleTable[a]; |
| 145 | r = SkUnPreMultiply::ApplyScale(scale, r); |
| 146 | g = SkUnPreMultiply::ApplyScale(scale, g); |
| 147 | b = SkUnPreMultiply::ApplyScale(scale, b); |
| 148 | } |
| 149 | } |
| 150 | dst[i] = SkPremultiplyARGBInline(tableA[a], tableR[r], |
| 151 | tableG[g], tableB[b]); |
| 152 | } |
| 153 | } |
| 154 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 155 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | 1202c2a | 2013-05-23 14:00:17 +0000 | [diff] [blame] | 156 | void SkTable_ColorFilter::toString(SkString* str) const { |
| 157 | str->append("SkTable_ColorFilter"); |
| 158 | } |
| 159 | #endif |
| 160 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 161 | static const uint8_t gCountNibBits[] = { |
| 162 | 0, 1, 1, 2, |
| 163 | 1, 2, 2, 3, |
| 164 | 1, 2, 2, 3, |
| 165 | 2, 3, 3, 4 |
| 166 | }; |
| 167 | |
| 168 | #include "SkPackBits.h" |
| 169 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 170 | void SkTable_ColorFilter::flatten(SkWriteBuffer& buffer) const { |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 171 | this->INHERITED::flatten(buffer); |
| 172 | |
| 173 | uint8_t storage[5*256]; |
| 174 | int count = gCountNibBits[fFlags & 0xF]; |
| 175 | size_t size = SkPackBits::Pack8(fStorage, count * 256, storage); |
| 176 | SkASSERT(size <= sizeof(storage)); |
| 177 | |
| 178 | // SkDebugf("raw %d packed %d\n", count * 256, size); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 179 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 180 | buffer.writeInt(fFlags); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 181 | buffer.writeByteArray(storage, size); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 182 | } |
| 183 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 184 | SkTable_ColorFilter::SkTable_ColorFilter(SkReadBuffer& buffer) : INHERITED(buffer) { |
mike@reedtribe.org | f23f5f7 | 2012-01-07 03:48:45 +0000 | [diff] [blame] | 185 | fBitmap = NULL; |
| 186 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 187 | uint8_t storage[5*256]; |
| 188 | |
| 189 | fFlags = buffer.readInt(); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 190 | |
| 191 | size_t size = buffer.getArrayCount(); |
| 192 | SkASSERT(size <= sizeof(storage)); |
commit-bot@chromium.org | ef74fa1 | 2013-12-17 20:49:46 +0000 | [diff] [blame] | 193 | buffer.validate(size <= sizeof(storage)); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 194 | buffer.readByteArray(storage, size); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 195 | |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 196 | SkDEBUGCODE(size_t raw = ) SkPackBits::Unpack8(storage, size, fStorage); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 197 | |
| 198 | SkASSERT(raw <= sizeof(fStorage)); |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 199 | SkDEBUGCODE(size_t count = gCountNibBits[fFlags & 0xF]); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 200 | SkASSERT(raw == count * 256); |
| 201 | } |
| 202 | |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 203 | bool SkTable_ColorFilter::asComponentTable(SkBitmap* table) const { |
mike@reedtribe.org | f23f5f7 | 2012-01-07 03:48:45 +0000 | [diff] [blame] | 204 | if (table) { |
| 205 | if (NULL == fBitmap) { |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 206 | SkBitmap* bmp = SkNEW(SkBitmap); |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 207 | bmp->allocPixels(SkImageInfo::MakeA8(256, 4)); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 208 | uint8_t* bitmapPixels = bmp->getAddr8(0, 0); |
twiz@google.com | 5807116 | 2012-07-18 21:41:50 +0000 | [diff] [blame] | 209 | int offset = 0; |
| 210 | static const unsigned kFlags[] = { kA_Flag, kR_Flag, kG_Flag, kB_Flag }; |
| 211 | |
| 212 | for (int x = 0; x < 4; ++x) { |
| 213 | if (!(fFlags & kFlags[x])) { |
| 214 | memcpy(bitmapPixels, gIdentityTable, sizeof(gIdentityTable)); |
| 215 | } else { |
| 216 | memcpy(bitmapPixels, fStorage + offset, 256); |
| 217 | offset += 256; |
| 218 | } |
| 219 | bitmapPixels += 256; |
| 220 | } |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 221 | fBitmap = bmp; |
mike@reedtribe.org | f23f5f7 | 2012-01-07 03:48:45 +0000 | [diff] [blame] | 222 | } |
| 223 | *table = *fBitmap; |
| 224 | } |
| 225 | return true; |
| 226 | } |
| 227 | |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 228 | #if SK_SUPPORT_GPU |
| 229 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 230 | #include "GrEffect.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 231 | #include "GrTBackendEffectFactory.h" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 232 | #include "gl/GrGLEffect.h" |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 233 | #include "gl/GrGLShaderBuilder.h" |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 234 | #include "SkGr.h" |
| 235 | |
| 236 | class GLColorTableEffect; |
| 237 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 238 | class ColorTableEffect : public GrEffect { |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 239 | public: |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 240 | static GrEffect* Create(GrTexture* texture, unsigned flags) { |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 241 | return SkNEW_ARGS(ColorTableEffect, (texture, flags)); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 242 | } |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 243 | |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 244 | virtual ~ColorTableEffect(); |
| 245 | |
| 246 | static const char* Name() { return "ColorTable"; } |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 247 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 248 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 249 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
| 250 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 251 | typedef GLColorTableEffect GLEffect; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 252 | |
| 253 | private: |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 254 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 255 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 256 | explicit ColorTableEffect(GrTexture* texture, unsigned flags); |
| 257 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 258 | GR_DECLARE_EFFECT_TEST; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 259 | |
| 260 | GrTextureAccess fTextureAccess; |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 261 | unsigned fFlags; // currently not used in shader code, just to assist |
| 262 | // getConstantColorComponents(). |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 263 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 264 | typedef GrEffect INHERITED; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 265 | }; |
| 266 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 267 | class GLColorTableEffect : public GrGLEffect { |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 268 | public: |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 269 | GLColorTableEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 270 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 271 | virtual void emitCode(GrGLShaderBuilder*, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 272 | const GrDrawEffect&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 273 | const GrEffectKey&, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 274 | const char* outputColor, |
| 275 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 276 | const TransformedCoordsArray&, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 277 | const TextureSamplerArray&) SK_OVERRIDE; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 278 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 279 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 280 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 281 | static void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder* b) {} |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 282 | |
| 283 | private: |
| 284 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 285 | typedef GrGLEffect INHERITED; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 286 | }; |
| 287 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 288 | GLColorTableEffect::GLColorTableEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 289 | : INHERITED(factory) { |
| 290 | } |
| 291 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 292 | void GLColorTableEffect::emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 293 | const GrDrawEffect&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame^] | 294 | const GrEffectKey&, |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 295 | const char* outputColor, |
| 296 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 297 | const TransformedCoordsArray&, |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 298 | const TextureSamplerArray& samplers) { |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 299 | |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 300 | static const float kColorScaleFactor = 255.0f / 256.0f; |
| 301 | static const float kColorOffsetFactor = 1.0f / 512.0f; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 302 | if (NULL == inputColor) { |
| 303 | // the input color is solid white (all ones). |
| 304 | static const float kMaxValue = kColorScaleFactor + kColorOffsetFactor; |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 305 | builder->fsCodeAppendf("\t\tvec4 coord = vec4(%f, %f, %f, %f);\n", |
| 306 | kMaxValue, kMaxValue, kMaxValue, kMaxValue); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 307 | |
| 308 | } else { |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 309 | builder->fsCodeAppendf("\t\tfloat nonZeroAlpha = max(%s.a, .0001);\n", inputColor); |
| 310 | builder->fsCodeAppendf("\t\tvec4 coord = vec4(%s.rgb / nonZeroAlpha, nonZeroAlpha);\n", inputColor); |
| 311 | builder->fsCodeAppendf("\t\tcoord = coord * %f + vec4(%f, %f, %f, %f);\n", |
| 312 | kColorScaleFactor, |
| 313 | kColorOffsetFactor, kColorOffsetFactor, |
| 314 | kColorOffsetFactor, kColorOffsetFactor); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 315 | } |
| 316 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 317 | builder->fsCodeAppendf("\t\t%s.a = ", outputColor); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 318 | builder->fsAppendTextureLookup(samplers[0], "vec2(coord.a, 0.125)"); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 319 | builder->fsCodeAppend(";\n"); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 320 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 321 | builder->fsCodeAppendf("\t\t%s.r = ", outputColor); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 322 | builder->fsAppendTextureLookup(samplers[0], "vec2(coord.r, 0.375)"); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 323 | builder->fsCodeAppend(";\n"); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 324 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 325 | builder->fsCodeAppendf("\t\t%s.g = ", outputColor); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 326 | builder->fsAppendTextureLookup(samplers[0], "vec2(coord.g, 0.625)"); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 327 | builder->fsCodeAppend(";\n"); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 328 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 329 | builder->fsCodeAppendf("\t\t%s.b = ", outputColor); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 330 | builder->fsAppendTextureLookup(samplers[0], "vec2(coord.b, 0.875)"); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 331 | builder->fsCodeAppend(";\n"); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 332 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 333 | builder->fsCodeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 334 | } |
| 335 | |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 336 | /////////////////////////////////////////////////////////////////////////////// |
| 337 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 338 | ColorTableEffect::ColorTableEffect(GrTexture* texture, unsigned flags) |
| 339 | : fTextureAccess(texture, "a") |
| 340 | , fFlags(flags) { |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 341 | this->addTextureAccess(&fTextureAccess); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | ColorTableEffect::~ColorTableEffect() { |
| 345 | } |
| 346 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 347 | const GrBackendEffectFactory& ColorTableEffect::getFactory() const { |
| 348 | return GrTBackendEffectFactory<ColorTableEffect>::getInstance(); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 349 | } |
| 350 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 351 | bool ColorTableEffect::onIsEqual(const GrEffect& sBase) const { |
| 352 | return this->texture(0) == sBase.texture(0); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 353 | } |
| 354 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 355 | void ColorTableEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 356 | // If we kept the table in the effect then we could actually run known inputs through the |
| 357 | // table. |
| 358 | if (fFlags & SkTable_ColorFilter::kR_Flag) { |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 359 | *validFlags &= ~kR_GrColorComponentFlag; |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 360 | } |
| 361 | if (fFlags & SkTable_ColorFilter::kG_Flag) { |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 362 | *validFlags &= ~kG_GrColorComponentFlag; |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 363 | } |
| 364 | if (fFlags & SkTable_ColorFilter::kB_Flag) { |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 365 | *validFlags &= ~kB_GrColorComponentFlag; |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 366 | } |
| 367 | if (fFlags & SkTable_ColorFilter::kA_Flag) { |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 368 | *validFlags &= ~kA_GrColorComponentFlag; |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
| 372 | |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 373 | /////////////////////////////////////////////////////////////////////////////// |
| 374 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 375 | GR_DEFINE_EFFECT_TEST(ColorTableEffect); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 376 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 377 | GrEffect* ColorTableEffect::TestCreate(SkRandom* random, |
| 378 | GrContext* context, |
| 379 | const GrDrawTargetCaps&, |
| 380 | GrTexture* textures[]) { |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 381 | static unsigned kAllFlags = SkTable_ColorFilter::kR_Flag | SkTable_ColorFilter::kG_Flag | |
| 382 | SkTable_ColorFilter::kB_Flag | SkTable_ColorFilter::kA_Flag; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 383 | return ColorTableEffect::Create(textures[GrEffectUnitTest::kAlphaTextureIdx], kAllFlags); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 384 | } |
| 385 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 386 | GrEffect* SkTable_ColorFilter::asNewEffect(GrContext* context) const { |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 387 | SkBitmap bitmap; |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 388 | GrEffect* effect = NULL; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 389 | this->asComponentTable(&bitmap); |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 390 | // passing NULL because this effect does no tiling or filtering. |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 391 | GrTexture* texture = GrLockAndRefCachedBitmapTexture(context, bitmap, NULL); |
commit-bot@chromium.org | c7d624e | 2013-05-28 13:13:56 +0000 | [diff] [blame] | 392 | if (NULL != texture) { |
| 393 | effect = ColorTableEffect::Create(texture, fFlags); |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 394 | |
commit-bot@chromium.org | c7d624e | 2013-05-28 13:13:56 +0000 | [diff] [blame] | 395 | // Unlock immediately, this is not great, but we don't have a way of |
| 396 | // knowing when else to unlock it currently. TODO: Remove this when |
| 397 | // unref becomes the unlock replacement for all types of textures. |
| 398 | GrUnlockAndUnrefCachedBitmapTexture(texture); |
| 399 | } |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 400 | return effect; |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | #endif // SK_SUPPORT_GPU |
| 404 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 405 | /////////////////////////////////////////////////////////////////////////////// |
| 406 | |
| 407 | #ifdef SK_CPU_BENDIAN |
| 408 | #else |
| 409 | #define SK_A32_INDEX (3 - (SK_A32_SHIFT >> 3)) |
| 410 | #define SK_R32_INDEX (3 - (SK_R32_SHIFT >> 3)) |
| 411 | #define SK_G32_INDEX (3 - (SK_G32_SHIFT >> 3)) |
| 412 | #define SK_B32_INDEX (3 - (SK_B32_SHIFT >> 3)) |
| 413 | #endif |
| 414 | |
| 415 | /////////////////////////////////////////////////////////////////////////////// |
| 416 | |
| 417 | SkColorFilter* SkTableColorFilter::Create(const uint8_t table[256]) { |
| 418 | return SkNEW_ARGS(SkTable_ColorFilter, (table, table, table, table)); |
| 419 | } |
| 420 | |
| 421 | SkColorFilter* SkTableColorFilter::CreateARGB(const uint8_t tableA[256], |
| 422 | const uint8_t tableR[256], |
| 423 | const uint8_t tableG[256], |
| 424 | const uint8_t tableB[256]) { |
| 425 | return SkNEW_ARGS(SkTable_ColorFilter, (tableA, tableR, tableG, tableB)); |
| 426 | } |
djsollen@google.com | a2ca41e | 2012-03-23 19:00:34 +0000 | [diff] [blame] | 427 | |
| 428 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkTableColorFilter) |
| 429 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTable_ColorFilter) |
| 430 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |