epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 7 | |
| 8 | #include "SkBitmapProcShader.h" |
| 9 | #include "SkBitmapProcState.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 11 | #include "SkErrorInternals.h" |
| 12 | #include "SkPixelRef.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 13 | #include "SkReadBuffer.h" |
| 14 | #include "SkWriteBuffer.h" |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 15 | |
humper@google.com | 30df03c | 2013-09-04 19:57:11 +0000 | [diff] [blame] | 16 | #if SK_SUPPORT_GPU |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 17 | #include "effects/GrBicubicEffect.h" |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 18 | #include "effects/GrExtractAlphaFragmentProcessor.h" |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 19 | #include "effects/GrSimpleTextureEffect.h" |
humper@google.com | 30df03c | 2013-09-04 19:57:11 +0000 | [diff] [blame] | 20 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | |
reed | 7a4d847 | 2015-09-15 13:33:58 -0700 | [diff] [blame^] | 22 | size_t SkBitmapProcShader::ContextSize() { |
| 23 | // The SkBitmapProcState is stored outside of the context object, with the context holding |
| 24 | // a pointer to it. |
| 25 | return sizeof(BitmapProcShaderContext) + sizeof(SkBitmapProcState); |
| 26 | } |
| 27 | |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 28 | SkBitmapProcShader::SkBitmapProcShader(const SkBitmap& src, TileMode tmx, TileMode tmy, |
| 29 | const SkMatrix* localMatrix) |
| 30 | : INHERITED(localMatrix) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | fRawBitmap = src; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 32 | fTileModeX = (uint8_t)tmx; |
| 33 | fTileModeY = (uint8_t)tmy; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | } |
| 35 | |
reed | 0f0af23 | 2015-09-08 11:02:04 -0700 | [diff] [blame] | 36 | bool SkBitmapProcShader::onIsABitmap(SkBitmap* texture, SkMatrix* texM, TileMode xy[]) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | if (texture) { |
| 38 | *texture = fRawBitmap; |
| 39 | } |
| 40 | if (texM) { |
| 41 | texM->reset(); |
| 42 | } |
| 43 | if (xy) { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 44 | xy[0] = (TileMode)fTileModeX; |
| 45 | xy[1] = (TileMode)fTileModeY; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | } |
reed | 0f0af23 | 2015-09-08 11:02:04 -0700 | [diff] [blame] | 47 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | } |
| 49 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 50 | SkFlattenable* SkBitmapProcShader::CreateProc(SkReadBuffer& buffer) { |
| 51 | SkMatrix lm; |
| 52 | buffer.readMatrix(&lm); |
| 53 | SkBitmap bm; |
| 54 | if (!buffer.readBitmap(&bm)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 55 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 56 | } |
| 57 | bm.setImmutable(); |
| 58 | TileMode mx = (TileMode)buffer.readUInt(); |
| 59 | TileMode my = (TileMode)buffer.readUInt(); |
| 60 | return SkShader::CreateBitmapShader(bm, mx, my, &lm); |
| 61 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 63 | void SkBitmapProcShader::flatten(SkWriteBuffer& buffer) const { |
| 64 | buffer.writeMatrix(this->getLocalMatrix()); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 65 | buffer.writeBitmap(fRawBitmap); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 66 | buffer.writeUInt(fTileModeX); |
| 67 | buffer.writeUInt(fTileModeY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | } |
| 69 | |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 70 | static bool only_scale_and_translate(const SkMatrix& matrix) { |
| 71 | unsigned mask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask; |
| 72 | return (matrix.getType() & ~mask) == 0; |
| 73 | } |
| 74 | |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 75 | bool SkBitmapProcShader::isOpaque() const { |
| 76 | return fRawBitmap.isOpaque(); |
| 77 | } |
| 78 | |
reed | 7a4d847 | 2015-09-15 13:33:58 -0700 | [diff] [blame^] | 79 | SkShader::Context* SkBitmapProcShader::MakeContext(const SkShader& shader, |
| 80 | TileMode tmx, TileMode tmy, |
| 81 | const SkBitmap& bitmap, |
| 82 | const ContextRec& rec, void* storage) { |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 83 | SkMatrix totalInverse; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 84 | // Do this first, so we know the matrix can be inverted. |
reed | 7a4d847 | 2015-09-15 13:33:58 -0700 | [diff] [blame^] | 85 | if (!shader.computeTotalInverse(rec, &totalInverse)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 86 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | } |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 88 | |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 89 | void* stateStorage = (char*)storage + sizeof(BitmapProcShaderContext); |
reed | 7a4d847 | 2015-09-15 13:33:58 -0700 | [diff] [blame^] | 90 | SkBitmapProcState* state = new (stateStorage) SkBitmapProcState(SkBitmapProvider(bitmap), |
| 91 | tmx, tmy); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 93 | SkASSERT(state); |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 94 | if (!state->chooseProcs(totalInverse, *rec.fPaint)) { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 95 | state->~SkBitmapProcState(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 96 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | } |
| 98 | |
reed | 7a4d847 | 2015-09-15 13:33:58 -0700 | [diff] [blame^] | 99 | return new (storage) BitmapProcShaderContext(shader, rec, state); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 100 | } |
| 101 | |
reed | 7a4d847 | 2015-09-15 13:33:58 -0700 | [diff] [blame^] | 102 | SkShader::Context* SkBitmapProcShader::onCreateContext(const ContextRec& rec, void* storage) const { |
| 103 | return MakeContext(*this, (TileMode)fTileModeX, (TileMode)fTileModeY, fRawBitmap, rec, storage); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 104 | } |
| 105 | |
reed | 7a4d847 | 2015-09-15 13:33:58 -0700 | [diff] [blame^] | 106 | SkBitmapProcShader::BitmapProcShaderContext::BitmapProcShaderContext(const SkShader& shader, |
| 107 | const ContextRec& rec, |
| 108 | SkBitmapProcState* state) |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 109 | : INHERITED(shader, rec) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 110 | , fState(state) |
| 111 | { |
reed | ad7ae6c | 2015-06-04 14:12:25 -0700 | [diff] [blame] | 112 | const SkPixmap& pixmap = fState->fPixmap; |
| 113 | bool isOpaque = pixmap.isOpaque(); |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 114 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | // update fFlags |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 116 | uint32_t flags = 0; |
reed | ad7ae6c | 2015-06-04 14:12:25 -0700 | [diff] [blame] | 117 | if (isOpaque && (255 == this->getPaintAlpha())) { |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 118 | flags |= kOpaqueAlpha_Flag; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | } |
| 120 | |
reed | ad7ae6c | 2015-06-04 14:12:25 -0700 | [diff] [blame] | 121 | switch (pixmap.colorType()) { |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 122 | case kRGB_565_SkColorType: |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 123 | flags |= (kHasSpan16_Flag | kIntrinsicly16_Flag); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | break; |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 125 | case kIndex_8_SkColorType: |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 126 | case kN32_SkColorType: |
reed | ad7ae6c | 2015-06-04 14:12:25 -0700 | [diff] [blame] | 127 | if (isOpaque) { |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 128 | flags |= kHasSpan16_Flag; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | } |
| 130 | break; |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 131 | case kAlpha_8_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | break; // never set kHasSpan16_Flag |
| 133 | default: |
| 134 | break; |
| 135 | } |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 136 | |
reed | ad7ae6c | 2015-06-04 14:12:25 -0700 | [diff] [blame] | 137 | if (rec.fPaint->isDither() && pixmap.colorType() != kRGB_565_SkColorType) { |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 138 | // gradients can auto-dither in their 16bit sampler, but we don't so |
reed@android.com | 7f6e1e9 | 2009-10-14 21:06:29 +0000 | [diff] [blame] | 139 | // we clear the flag here. |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 140 | flags &= ~kHasSpan16_Flag; |
| 141 | } |
| 142 | |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 143 | // if we're only 1-pixel high, and we don't rotate, then we can claim this |
reed | ad7ae6c | 2015-06-04 14:12:25 -0700 | [diff] [blame] | 144 | if (1 == pixmap.height() && |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 145 | only_scale_and_translate(this->getTotalInverse())) { |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 146 | flags |= kConstInY32_Flag; |
| 147 | if (flags & kHasSpan16_Flag) { |
| 148 | flags |= kConstInY16_Flag; |
| 149 | } |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 150 | } |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 151 | |
| 152 | fFlags = flags; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | } |
| 154 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 155 | SkBitmapProcShader::BitmapProcShaderContext::~BitmapProcShaderContext() { |
| 156 | // The bitmap proc state has been created outside of the context on memory that will be freed |
| 157 | // elsewhere. Only call the destructor but leave the freeing of the memory to the caller. |
| 158 | fState->~SkBitmapProcState(); |
reed@google.com | a641f3f | 2012-12-13 22:16:30 +0000 | [diff] [blame] | 159 | } |
| 160 | |
commit-bot@chromium.org | f21991b | 2014-05-23 19:13:56 +0000 | [diff] [blame] | 161 | #define BUF_MAX 128 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | |
reed@android.com | 258cb22 | 2010-04-14 13:36:33 +0000 | [diff] [blame] | 163 | #define TEST_BUFFER_OVERRITEx |
| 164 | |
| 165 | #ifdef TEST_BUFFER_OVERRITE |
| 166 | #define TEST_BUFFER_EXTRA 32 |
commit-bot@chromium.org | f21991b | 2014-05-23 19:13:56 +0000 | [diff] [blame] | 167 | #define TEST_PATTERN 0x88888888 |
reed@android.com | 258cb22 | 2010-04-14 13:36:33 +0000 | [diff] [blame] | 168 | #else |
| 169 | #define TEST_BUFFER_EXTRA 0 |
| 170 | #endif |
| 171 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 172 | void SkBitmapProcShader::BitmapProcShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], |
| 173 | int count) { |
| 174 | const SkBitmapProcState& state = *fState; |
reed@google.com | 9fe287b | 2012-03-27 15:54:28 +0000 | [diff] [blame] | 175 | if (state.getShaderProc32()) { |
| 176 | state.getShaderProc32()(state, x, y, dstC, count); |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 177 | return; |
| 178 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 179 | |
commit-bot@chromium.org | f21991b | 2014-05-23 19:13:56 +0000 | [diff] [blame] | 180 | uint32_t buffer[BUF_MAX + TEST_BUFFER_EXTRA]; |
reed@google.com | 9fe287b | 2012-03-27 15:54:28 +0000 | [diff] [blame] | 181 | SkBitmapProcState::MatrixProc mproc = state.getMatrixProc(); |
| 182 | SkBitmapProcState::SampleProc32 sproc = state.getSampleProc32(); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 183 | int max = state.maxCountForBufferSize(sizeof(buffer[0]) * BUF_MAX); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | |
reed | ad7ae6c | 2015-06-04 14:12:25 -0700 | [diff] [blame] | 185 | SkASSERT(state.fPixmap.addr()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 186 | |
| 187 | for (;;) { |
| 188 | int n = count; |
| 189 | if (n > max) { |
| 190 | n = max; |
| 191 | } |
reed@android.com | 258cb22 | 2010-04-14 13:36:33 +0000 | [diff] [blame] | 192 | SkASSERT(n > 0 && n < BUF_MAX*2); |
| 193 | #ifdef TEST_BUFFER_OVERRITE |
| 194 | for (int i = 0; i < TEST_BUFFER_EXTRA; i++) { |
| 195 | buffer[BUF_MAX + i] = TEST_PATTERN; |
| 196 | } |
| 197 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 198 | mproc(state, buffer, n, x, y); |
reed@android.com | 258cb22 | 2010-04-14 13:36:33 +0000 | [diff] [blame] | 199 | #ifdef TEST_BUFFER_OVERRITE |
| 200 | for (int j = 0; j < TEST_BUFFER_EXTRA; j++) { |
| 201 | SkASSERT(buffer[BUF_MAX + j] == TEST_PATTERN); |
| 202 | } |
| 203 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | sproc(state, buffer, n, dstC); |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 205 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 206 | if ((count -= n) == 0) { |
| 207 | break; |
| 208 | } |
reed@android.com | 258cb22 | 2010-04-14 13:36:33 +0000 | [diff] [blame] | 209 | SkASSERT(count > 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 210 | x += n; |
| 211 | dstC += n; |
| 212 | } |
| 213 | } |
| 214 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 215 | SkShader::Context::ShadeProc SkBitmapProcShader::BitmapProcShaderContext::asAShadeProc(void** ctx) { |
| 216 | if (fState->getShaderProc32()) { |
| 217 | *ctx = fState; |
| 218 | return (ShadeProc)fState->getShaderProc32(); |
reed@google.com | 3bafe74 | 2012-10-12 18:56:18 +0000 | [diff] [blame] | 219 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 220 | return nullptr; |
reed@google.com | 3bafe74 | 2012-10-12 18:56:18 +0000 | [diff] [blame] | 221 | } |
| 222 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 223 | void SkBitmapProcShader::BitmapProcShaderContext::shadeSpan16(int x, int y, uint16_t dstC[], |
| 224 | int count) { |
| 225 | const SkBitmapProcState& state = *fState; |
reed@google.com | 9fe287b | 2012-03-27 15:54:28 +0000 | [diff] [blame] | 226 | if (state.getShaderProc16()) { |
| 227 | state.getShaderProc16()(state, x, y, dstC, count); |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 228 | return; |
| 229 | } |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 230 | |
commit-bot@chromium.org | f21991b | 2014-05-23 19:13:56 +0000 | [diff] [blame] | 231 | uint32_t buffer[BUF_MAX]; |
reed@google.com | 9fe287b | 2012-03-27 15:54:28 +0000 | [diff] [blame] | 232 | SkBitmapProcState::MatrixProc mproc = state.getMatrixProc(); |
| 233 | SkBitmapProcState::SampleProc16 sproc = state.getSampleProc16(); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 234 | int max = state.maxCountForBufferSize(sizeof(buffer)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 235 | |
reed | ad7ae6c | 2015-06-04 14:12:25 -0700 | [diff] [blame] | 236 | SkASSERT(state.fPixmap.addr()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | |
| 238 | for (;;) { |
| 239 | int n = count; |
| 240 | if (n > max) { |
| 241 | n = max; |
| 242 | } |
| 243 | mproc(state, buffer, n, x, y); |
| 244 | sproc(state, buffer, n, dstC); |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 245 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 246 | if ((count -= n) == 0) { |
| 247 | break; |
| 248 | } |
| 249 | x += n; |
| 250 | dstC += n; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /////////////////////////////////////////////////////////////////////////////// |
| 255 | |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 256 | #include "SkUnPreMultiply.h" |
| 257 | #include "SkColorShader.h" |
reed@google.com | 37a2012 | 2011-07-05 18:54:12 +0000 | [diff] [blame] | 258 | #include "SkEmptyShader.h" |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 259 | |
| 260 | // returns true and set color if the bitmap can be drawn as a single color |
| 261 | // (for efficiency) |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 262 | static bool can_use_color_shader(const SkBitmap& bm, SkColor* color) { |
djsollen | ab51cbd | 2015-07-14 10:59:28 -0700 | [diff] [blame] | 263 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 264 | // HWUI does not support color shaders (see b/22390304) |
| 265 | return false; |
| 266 | #endif |
| 267 | |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 268 | if (1 != bm.width() || 1 != bm.height()) { |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | SkAutoLockPixels alp(bm); |
| 273 | if (!bm.readyToDraw()) { |
| 274 | return false; |
| 275 | } |
| 276 | |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 277 | switch (bm.colorType()) { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 278 | case kN32_SkColorType: |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 279 | *color = SkUnPreMultiply::PMColorToColor(*bm.getAddr32(0, 0)); |
| 280 | return true; |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 281 | case kRGB_565_SkColorType: |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 282 | *color = SkPixel16ToColor(*bm.getAddr16(0, 0)); |
| 283 | return true; |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 284 | case kIndex_8_SkColorType: |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 285 | *color = SkUnPreMultiply::PMColorToColor(bm.getIndex8Color(0, 0)); |
| 286 | return true; |
| 287 | default: // just skip the other configs for now |
| 288 | break; |
| 289 | } |
| 290 | return false; |
| 291 | } |
| 292 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 293 | static bool bitmap_is_too_big(const SkBitmap& bm) { |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 294 | // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer, as it |
| 295 | // communicates between its matrix-proc and its sampler-proc. Until we can |
| 296 | // widen that, we have to reject bitmaps that are larger. |
| 297 | // |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 298 | static const int kMaxSize = 65535; |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 299 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 300 | return bm.width() > kMaxSize || bm.height() > kMaxSize; |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 301 | } |
| 302 | |
mtklein | 7ef849d | 2014-11-24 09:11:45 -0800 | [diff] [blame] | 303 | SkShader* SkCreateBitmapShader(const SkBitmap& src, SkShader::TileMode tmx, |
| 304 | SkShader::TileMode tmy, const SkMatrix* localMatrix, |
| 305 | SkTBlitterAllocator* allocator) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 306 | SkShader* shader; |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 307 | SkColor color; |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 308 | if (src.isNull() || bitmap_is_too_big(src)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 309 | if (nullptr == allocator) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 310 | shader = new SkEmptyShader; |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 311 | } else { |
| 312 | shader = allocator->createT<SkEmptyShader>(); |
| 313 | } |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 314 | } else if (can_use_color_shader(src, &color)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 315 | if (nullptr == allocator) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 316 | shader = new SkColorShader(color); |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 317 | } else { |
| 318 | shader = allocator->createT<SkColorShader>(color); |
| 319 | } |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 320 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 321 | if (nullptr == allocator) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 322 | shader = new SkBitmapProcShader(src, tmx, tmy, localMatrix); |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 323 | } else { |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 324 | shader = allocator->createT<SkBitmapProcShader>(src, tmx, tmy, localMatrix); |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 325 | } |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 326 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 327 | return shader; |
| 328 | } |
| 329 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 330 | /////////////////////////////////////////////////////////////////////////////// |
| 331 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 332 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 333 | void SkBitmapProcShader::toString(SkString* str) const { |
| 334 | static const char* gTileModeName[SkShader::kTileModeCount] = { |
| 335 | "clamp", "repeat", "mirror" |
| 336 | }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 337 | |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 338 | str->append("BitmapShader: ("); |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 339 | |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 340 | str->appendf("(%s, %s)", |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 341 | gTileModeName[fTileModeX], |
| 342 | gTileModeName[fTileModeY]); |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 343 | |
| 344 | str->append(" "); |
| 345 | fRawBitmap.toString(str); |
| 346 | |
| 347 | this->INHERITED::toString(str); |
| 348 | |
| 349 | str->append(")"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 350 | } |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 351 | #endif |
| 352 | |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 353 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 354 | |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 355 | #if SK_SUPPORT_GPU |
| 356 | |
| 357 | #include "GrTextureAccess.h" |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 358 | #include "SkGr.h" |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 359 | #include "effects/GrSimpleTextureEffect.h" |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 360 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 361 | const GrFragmentProcessor* SkBitmapProcShader::asFragmentProcessor(GrContext* context, |
| 362 | const SkMatrix& viewM, const SkMatrix* localMatrix, |
| 363 | SkFilterQuality filterQuality, |
| 364 | GrProcessorDataManager* procDataManager) const { |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 365 | SkMatrix matrix; |
| 366 | matrix.setIDiv(fRawBitmap.width(), fRawBitmap.height()); |
| 367 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 368 | SkMatrix lmInverse; |
| 369 | if (!this->getLocalMatrix().invert(&lmInverse)) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 370 | return nullptr; |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 371 | } |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 372 | if (localMatrix) { |
| 373 | SkMatrix inv; |
| 374 | if (!localMatrix->invert(&inv)) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 375 | return nullptr; |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 376 | } |
| 377 | lmInverse.postConcat(inv); |
| 378 | } |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 379 | matrix.preConcat(lmInverse); |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 380 | |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 381 | SkShader::TileMode tm[] = { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 382 | (TileMode)fTileModeX, |
| 383 | (TileMode)fTileModeY, |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 384 | }; |
| 385 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 386 | // Must set wrap and filter on the sampler before requesting a texture. In two places below |
| 387 | // we check the matrix scale factors to determine how to interpret the filter quality setting. |
| 388 | // This completely ignores the complexity of the drawVertices case where explicit local coords |
| 389 | // are provided by the caller. |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 390 | bool doBicubic; |
| 391 | GrTextureParams::FilterMode textureFilterMode = |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 392 | GrSkFilterQualityToGrFilterMode(filterQuality, viewM, this->getLocalMatrix(), |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 393 | &doBicubic); |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 394 | GrTextureParams params(tm, textureFilterMode); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 395 | SkAutoTUnref<GrTexture> texture(GrRefCachedBitmapTexture(context, fRawBitmap, ¶ms)); |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 396 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 397 | if (!texture) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 398 | SkErrorInternals::SetError( kInternalError_SkError, |
| 399 | "Couldn't convert bitmap to texture."); |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 400 | return nullptr; |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 401 | } |
mtklein | 7ef849d | 2014-11-24 09:11:45 -0800 | [diff] [blame] | 402 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 403 | SkAutoTUnref<GrFragmentProcessor> inner; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 404 | if (doBicubic) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 405 | inner.reset(GrBicubicEffect::Create(procDataManager, texture, matrix, tm)); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 406 | } else { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 407 | inner.reset(GrSimpleTextureEffect::Create(procDataManager, texture, matrix, params)); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 408 | } |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 409 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 410 | if (kAlpha_8_SkColorType == fRawBitmap.colorType()) { |
| 411 | return SkRef(inner.get()); |
| 412 | } |
| 413 | return GrExtractAlphaFragmentProcessor::Create(inner); |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 414 | } |
| 415 | |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 416 | #endif |