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