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: |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 30 | // The info has been allocated elsewhere, but we are responsible for calling its destructor. |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 31 | BitmapProcInfoContext(const SkShader& shader, const SkShader::ContextRec& rec, |
| 32 | SkBitmapProcInfo* info) |
| 33 | : INHERITED(shader, rec) |
| 34 | , fInfo(info) |
| 35 | { |
| 36 | fFlags = 0; |
| 37 | if (fInfo->fPixmap.isOpaque() && (255 == this->getPaintAlpha())) { |
| 38 | fFlags |= SkShader::kOpaqueAlpha_Flag; |
| 39 | } |
| 40 | |
| 41 | if (1 == fInfo->fPixmap.height() && only_scale_and_translate(this->getTotalInverse())) { |
| 42 | fFlags |= SkShader::kConstInY32_Flag; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | ~BitmapProcInfoContext() override { |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 47 | fInfo->~SkBitmapProcInfo(); |
| 48 | } |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 49 | |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 50 | uint32_t getFlags() const override { return fFlags; } |
| 51 | |
| 52 | private: |
| 53 | SkBitmapProcInfo* fInfo; |
| 54 | uint32_t fFlags; |
| 55 | |
| 56 | typedef SkShader::Context INHERITED; |
| 57 | }; |
| 58 | |
| 59 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 60 | |
| 61 | class BitmapProcShaderContext : public BitmapProcInfoContext { |
| 62 | public: |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 63 | BitmapProcShaderContext(const SkShader& shader, const SkShader::ContextRec& rec, |
| 64 | SkBitmapProcState* state) |
| 65 | : INHERITED(shader, rec, state) |
| 66 | , fState(state) |
| 67 | {} |
| 68 | |
| 69 | void shadeSpan(int x, int y, SkPMColor dstC[], int count) override { |
| 70 | const SkBitmapProcState& state = *fState; |
| 71 | if (state.getShaderProc32()) { |
| 72 | state.getShaderProc32()(&state, x, y, dstC, count); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | const int BUF_MAX = 128; |
| 77 | uint32_t buffer[BUF_MAX]; |
| 78 | SkBitmapProcState::MatrixProc mproc = state.getMatrixProc(); |
| 79 | SkBitmapProcState::SampleProc32 sproc = state.getSampleProc32(); |
| 80 | const int max = state.maxCountForBufferSize(sizeof(buffer[0]) * BUF_MAX); |
| 81 | |
| 82 | SkASSERT(state.fPixmap.addr()); |
| 83 | |
| 84 | for (;;) { |
| 85 | int n = SkTMin(count, max); |
| 86 | SkASSERT(n > 0 && n < BUF_MAX*2); |
| 87 | mproc(state, buffer, n, x, y); |
| 88 | sproc(state, buffer, n, dstC); |
| 89 | |
| 90 | if ((count -= n) == 0) { |
| 91 | break; |
| 92 | } |
| 93 | SkASSERT(count > 0); |
| 94 | x += n; |
| 95 | dstC += n; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | ShadeProc asAShadeProc(void** ctx) override { |
| 100 | if (fState->getShaderProc32()) { |
| 101 | *ctx = fState; |
| 102 | return (ShadeProc)fState->getShaderProc32(); |
| 103 | } |
| 104 | return nullptr; |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | SkBitmapProcState* fState; |
| 109 | |
| 110 | typedef BitmapProcInfoContext INHERITED; |
| 111 | }; |
| 112 | |
| 113 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 114 | #include "SkLinearBitmapPipeline.h" |
| 115 | #include "SkPM4f.h" |
| 116 | #include "SkXfermode.h" |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 117 | |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 118 | class LinearPipelineContext : public BitmapProcInfoContext { |
| 119 | public: |
| 120 | LinearPipelineContext(const SkShader& shader, const SkShader::ContextRec& rec, |
| 121 | SkBitmapProcInfo* info) |
| 122 | : INHERITED(shader, rec, info) |
| 123 | { |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 124 | // Save things off in case we need to build a blitter pipeline. |
| 125 | fSrcPixmap = info->fPixmap; |
| 126 | fAlpha = SkColorGetA(info->fPaintColor) / 255.0f; |
| 127 | fXMode = info->fTileModeX; |
| 128 | fYMode = info->fTileModeY; |
| 129 | fFilterQuality = info->fFilterQuality; |
| 130 | fMatrixTypeMask = info->fRealInvMatrix.getType(); |
| 131 | |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 132 | // Need to ensure that our pipeline is created at a 16byte aligned address |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 133 | fShaderPipeline = (SkLinearBitmapPipeline*)SkAlign16((intptr_t)fShaderStorage); |
| 134 | new (fShaderPipeline) SkLinearBitmapPipeline(info->fRealInvMatrix, info->fFilterQuality, |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 135 | info->fTileModeX, info->fTileModeY, |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 136 | fAlpha, |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 137 | info->fPixmap); |
| 138 | |
| 139 | // To implement the old shadeSpan entry-point, we need to efficiently convert our native |
| 140 | // floats into SkPMColor. The SkXfermode::D32Procs do exactly that. |
| 141 | // |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 142 | sk_sp<SkXfermode> xfer(SkXfermode::Make(SkXfermode::kSrc_Mode)); |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 143 | fXferProc = SkXfermode::GetD32Proc(xfer.get(), 0); |
| 144 | } |
| 145 | |
| 146 | ~LinearPipelineContext() override { |
| 147 | // since we did a manual new, we need to manually destroy as well. |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 148 | fShaderPipeline->~SkLinearBitmapPipeline(); |
| 149 | if (fBlitterPipeline != nullptr) { |
| 150 | fBlitterPipeline->~SkLinearBitmapPipeline(); |
| 151 | } |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void shadeSpan4f(int x, int y, SkPM4f dstC[], int count) override { |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 155 | fShaderPipeline->shadeSpan4f(x, y, dstC, count); |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void shadeSpan(int x, int y, SkPMColor dstC[], int count) override { |
| 159 | const int N = 128; |
| 160 | SkPM4f tmp[N]; |
| 161 | |
| 162 | while (count > 0) { |
| 163 | const int n = SkTMin(count, N); |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 164 | fShaderPipeline->shadeSpan4f(x, y, tmp, n); |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 165 | fXferProc(nullptr, dstC, tmp, n, nullptr); |
| 166 | dstC += n; |
| 167 | x += n; |
| 168 | count -= n; |
| 169 | } |
| 170 | } |
| 171 | |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 172 | bool onChooseBlitProcs(const SkImageInfo& dstInfo, BlitState* state) override { |
| 173 | SkXfermode::Mode mode; |
| 174 | if (!SkXfermode::AsMode(state->fXfer, &mode)) { return false; } |
| 175 | |
| 176 | // Need to ensure that our pipeline is created at a 16byte aligned address |
| 177 | fBlitterPipeline = (SkLinearBitmapPipeline*)SkAlign16((intptr_t)fBlitterStorage); |
| 178 | if (SkLinearBitmapPipeline::ClonePipelineForBlitting( |
| 179 | fBlitterPipeline, *fShaderPipeline, |
| 180 | fMatrixTypeMask, |
| 181 | fXMode, fYMode, |
| 182 | fFilterQuality, fSrcPixmap, |
herb | 9f666a1 | 2016-04-25 09:05:14 -0700 | [diff] [blame^] | 183 | fAlpha, mode, dstInfo)) |
| 184 | { |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 185 | state->fStorage[0] = fBlitterPipeline; |
| 186 | state->fBlitBW = &LinearPipelineContext::ForwardToPipeline; |
| 187 | |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | // Did not successfully create a pipeline so don't destruct it. |
| 192 | fBlitterPipeline = nullptr; |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | static void ForwardToPipeline(BlitState* state, int x, int y, const SkPixmap& dst, int count) { |
| 197 | SkLinearBitmapPipeline* pipeline = static_cast<SkLinearBitmapPipeline*>(state->fStorage[0]); |
| 198 | void* addr = dst.writable_addr32(x, y); |
| 199 | pipeline->blitSpan(x, y, addr, count); |
| 200 | } |
| 201 | |
| 202 | |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 203 | private: |
| 204 | enum { |
| 205 | kActualSize = sizeof(SkLinearBitmapPipeline), |
| 206 | kPaddedSize = SkAlignPtr(kActualSize + 12), |
| 207 | }; |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 208 | void* fShaderStorage[kPaddedSize / sizeof(void*)]; |
| 209 | SkLinearBitmapPipeline* fShaderPipeline; |
| 210 | void* fBlitterStorage[kPaddedSize / sizeof(void*)]; |
| 211 | SkLinearBitmapPipeline* fBlitterPipeline{nullptr}; |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 212 | SkXfermode::D32Proc fXferProc; |
herb | 69076fe | 2016-04-12 14:07:59 -0700 | [diff] [blame] | 213 | SkPixmap fSrcPixmap; |
| 214 | float fAlpha; |
| 215 | SkShader::TileMode fXMode; |
| 216 | SkShader::TileMode fYMode; |
| 217 | SkMatrix::TypeMask fMatrixTypeMask; |
| 218 | SkFilterQuality fFilterQuality; |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 219 | |
| 220 | typedef BitmapProcInfoContext INHERITED; |
| 221 | }; |
| 222 | |
| 223 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 224 | |
| 225 | static bool choose_linear_pipeline(const SkShader::ContextRec& rec, const SkImageInfo& srcInfo) { |
| 226 | // These src attributes are not supported in the new 4f context (yet) |
| 227 | // |
herb | 222f8ff | 2016-03-23 15:14:23 -0700 | [diff] [blame] | 228 | if (srcInfo.colorType() != kRGBA_8888_SkColorType |
| 229 | && srcInfo.colorType() != kBGRA_8888_SkColorType |
herb | d5f2e2e | 2016-04-14 11:16:44 -0700 | [diff] [blame] | 230 | && srcInfo.colorType() != kIndex_8_SkColorType |
| 231 | && srcInfo.colorType() != kRGBA_F16_SkColorType) { |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 232 | return false; |
| 233 | } |
| 234 | |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 235 | // If we get here, we can reasonably use either context, respect the caller's preference |
| 236 | // |
| 237 | return SkShader::ContextRec::kPM4f_DstType == rec.fPreferredDstType; |
| 238 | } |
| 239 | |
| 240 | size_t SkBitmapProcShader::ContextSize(const ContextRec& rec, const SkImageInfo& srcInfo) { |
| 241 | size_t size0 = sizeof(BitmapProcShaderContext) + sizeof(SkBitmapProcState); |
| 242 | size_t size1 = sizeof(LinearPipelineContext) + sizeof(SkBitmapProcInfo); |
| 243 | return SkTMax(size0, size1); |
reed | 7a4d847 | 2015-09-15 13:33:58 -0700 | [diff] [blame] | 244 | } |
| 245 | |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 246 | SkShader::Context* SkBitmapProcShader::MakeContext(const SkShader& shader, |
| 247 | TileMode tmx, TileMode tmy, |
| 248 | const SkBitmapProvider& provider, |
| 249 | const ContextRec& rec, void* storage) { |
| 250 | SkMatrix totalInverse; |
| 251 | // Do this first, so we know the matrix can be inverted. |
| 252 | if (!shader.computeTotalInverse(rec, &totalInverse)) { |
| 253 | return nullptr; |
| 254 | } |
| 255 | |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 256 | // Decide if we can/want to use the new linear pipeline |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 257 | bool useLinearPipeline = choose_linear_pipeline(rec, provider.info()); |
reed | cd660e1 | 2016-03-03 09:36:50 -0800 | [diff] [blame] | 258 | |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 259 | if (useLinearPipeline) { |
| 260 | void* infoStorage = (char*)storage + sizeof(LinearPipelineContext); |
| 261 | SkBitmapProcInfo* info = new (infoStorage) SkBitmapProcInfo(provider, tmx, tmy); |
| 262 | if (!info->init(totalInverse, *rec.fPaint)) { |
| 263 | info->~SkBitmapProcInfo(); |
| 264 | return nullptr; |
| 265 | } |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 266 | if (info->fPixmap.colorType() != kRGBA_8888_SkColorType |
herb | 222f8ff | 2016-03-23 15:14:23 -0700 | [diff] [blame] | 267 | && info->fPixmap.colorType() != kBGRA_8888_SkColorType |
herb | d5f2e2e | 2016-04-14 11:16:44 -0700 | [diff] [blame] | 268 | && info->fPixmap.colorType() != kIndex_8_SkColorType |
| 269 | && info->fPixmap.colorType() != kRGBA_F16_SkColorType) { |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 270 | return nullptr; |
| 271 | } |
reed | d882901 | 2016-03-04 11:07:43 -0800 | [diff] [blame] | 272 | return new (storage) LinearPipelineContext(shader, rec, info); |
| 273 | } else { |
| 274 | void* stateStorage = (char*)storage + sizeof(BitmapProcShaderContext); |
| 275 | SkBitmapProcState* state = new (stateStorage) SkBitmapProcState(provider, tmx, tmy); |
| 276 | if (!state->setup(totalInverse, *rec.fPaint)) { |
| 277 | state->~SkBitmapProcState(); |
| 278 | return nullptr; |
| 279 | } |
| 280 | return new (storage) BitmapProcShaderContext(shader, rec, state); |
| 281 | } |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | SkShader::Context* SkBitmapProcShader::onCreateContext(const ContextRec& rec, void* storage) const { |
| 285 | return MakeContext(*this, (TileMode)fTileModeX, (TileMode)fTileModeY, |
| 286 | SkBitmapProvider(fRawBitmap), rec, storage); |
| 287 | } |
| 288 | |
| 289 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 290 | |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 291 | SkBitmapProcShader::SkBitmapProcShader(const SkBitmap& src, TileMode tmx, TileMode tmy, |
| 292 | const SkMatrix* localMatrix) |
| 293 | : INHERITED(localMatrix) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 294 | fRawBitmap = src; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 295 | fTileModeX = (uint8_t)tmx; |
| 296 | fTileModeY = (uint8_t)tmy; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 297 | } |
| 298 | |
reed | 0f0af23 | 2015-09-08 11:02:04 -0700 | [diff] [blame] | 299 | bool SkBitmapProcShader::onIsABitmap(SkBitmap* texture, SkMatrix* texM, TileMode xy[]) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 300 | if (texture) { |
| 301 | *texture = fRawBitmap; |
| 302 | } |
| 303 | if (texM) { |
| 304 | texM->reset(); |
| 305 | } |
| 306 | if (xy) { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 307 | xy[0] = (TileMode)fTileModeX; |
| 308 | xy[1] = (TileMode)fTileModeY; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 309 | } |
reed | 0f0af23 | 2015-09-08 11:02:04 -0700 | [diff] [blame] | 310 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 311 | } |
| 312 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 313 | sk_sp<SkFlattenable> SkBitmapProcShader::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 314 | SkMatrix lm; |
| 315 | buffer.readMatrix(&lm); |
| 316 | SkBitmap bm; |
| 317 | if (!buffer.readBitmap(&bm)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 318 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 319 | } |
| 320 | bm.setImmutable(); |
| 321 | TileMode mx = (TileMode)buffer.readUInt(); |
| 322 | TileMode my = (TileMode)buffer.readUInt(); |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 323 | return SkShader::MakeBitmapShader(bm, mx, my, &lm); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 324 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 325 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 326 | void SkBitmapProcShader::flatten(SkWriteBuffer& buffer) const { |
| 327 | buffer.writeMatrix(this->getLocalMatrix()); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 328 | buffer.writeBitmap(fRawBitmap); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 329 | buffer.writeUInt(fTileModeX); |
| 330 | buffer.writeUInt(fTileModeY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 331 | } |
| 332 | |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 333 | bool SkBitmapProcShader::isOpaque() const { |
| 334 | return fRawBitmap.isOpaque(); |
| 335 | } |
| 336 | |
reed | 05a5647 | 2016-03-02 09:49:02 -0800 | [diff] [blame] | 337 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 338 | |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 339 | #include "SkUnPreMultiply.h" |
| 340 | #include "SkColorShader.h" |
reed@google.com | 37a2012 | 2011-07-05 18:54:12 +0000 | [diff] [blame] | 341 | #include "SkEmptyShader.h" |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 342 | |
| 343 | // returns true and set color if the bitmap can be drawn as a single color |
| 344 | // (for efficiency) |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 345 | static bool can_use_color_shader(const SkBitmap& bm, SkColor* color) { |
djsollen | ab51cbd | 2015-07-14 10:59:28 -0700 | [diff] [blame] | 346 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 347 | // HWUI does not support color shaders (see b/22390304) |
| 348 | return false; |
| 349 | #endif |
| 350 | |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 351 | if (1 != bm.width() || 1 != bm.height()) { |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | SkAutoLockPixels alp(bm); |
| 356 | if (!bm.readyToDraw()) { |
| 357 | return false; |
| 358 | } |
| 359 | |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 360 | switch (bm.colorType()) { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 361 | case kN32_SkColorType: |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 362 | *color = SkUnPreMultiply::PMColorToColor(*bm.getAddr32(0, 0)); |
| 363 | return true; |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 364 | case kRGB_565_SkColorType: |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 365 | *color = SkPixel16ToColor(*bm.getAddr16(0, 0)); |
| 366 | return true; |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 367 | case kIndex_8_SkColorType: |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 368 | *color = SkUnPreMultiply::PMColorToColor(bm.getIndex8Color(0, 0)); |
| 369 | return true; |
| 370 | default: // just skip the other configs for now |
| 371 | break; |
| 372 | } |
| 373 | return false; |
| 374 | } |
| 375 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 376 | static bool bitmap_is_too_big(const SkBitmap& bm) { |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 377 | // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer, as it |
| 378 | // communicates between its matrix-proc and its sampler-proc. Until we can |
| 379 | // widen that, we have to reject bitmaps that are larger. |
| 380 | // |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 381 | static const int kMaxSize = 65535; |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 382 | |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 383 | return bm.width() > kMaxSize || bm.height() > kMaxSize; |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 384 | } |
| 385 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 386 | sk_sp<SkShader> SkMakeBitmapShader(const SkBitmap& src, SkShader::TileMode tmx, |
| 387 | SkShader::TileMode tmy, const SkMatrix* localMatrix, |
| 388 | SkTBlitterAllocator* allocator) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 389 | SkShader* shader; |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 390 | SkColor color; |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 391 | if (src.isNull() || bitmap_is_too_big(src)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 392 | if (nullptr == allocator) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 393 | shader = new SkEmptyShader; |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 394 | } else { |
| 395 | shader = allocator->createT<SkEmptyShader>(); |
| 396 | } |
robertphillips | 5f865b9 | 2015-07-29 12:28:04 -0700 | [diff] [blame] | 397 | } else if (can_use_color_shader(src, &color)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 398 | if (nullptr == allocator) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 399 | shader = new SkColorShader(color); |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 400 | } else { |
| 401 | shader = allocator->createT<SkColorShader>(color); |
| 402 | } |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 403 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 404 | if (nullptr == allocator) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 405 | shader = new SkBitmapProcShader(src, tmx, tmy, localMatrix); |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 406 | } else { |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 407 | shader = allocator->createT<SkBitmapProcShader>(src, tmx, tmy, localMatrix); |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 408 | } |
reed@android.com | c645996 | 2009-08-25 19:15:31 +0000 | [diff] [blame] | 409 | } |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 410 | return sk_sp<SkShader>(shader); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 411 | } |
| 412 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 413 | /////////////////////////////////////////////////////////////////////////////// |
| 414 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 415 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 416 | void SkBitmapProcShader::toString(SkString* str) const { |
| 417 | static const char* gTileModeName[SkShader::kTileModeCount] = { |
| 418 | "clamp", "repeat", "mirror" |
| 419 | }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 420 | |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 421 | str->append("BitmapShader: ("); |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 422 | |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 423 | str->appendf("(%s, %s)", |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 424 | gTileModeName[fTileModeX], |
| 425 | gTileModeName[fTileModeY]); |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 426 | |
| 427 | str->append(" "); |
| 428 | fRawBitmap.toString(str); |
| 429 | |
| 430 | this->INHERITED::toString(str); |
| 431 | |
| 432 | str->append(")"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 433 | } |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 434 | #endif |
| 435 | |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 436 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 437 | |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 438 | #if SK_SUPPORT_GPU |
| 439 | |
| 440 | #include "GrTextureAccess.h" |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 441 | #include "SkGr.h" |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 442 | #include "effects/GrSimpleTextureEffect.h" |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 443 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 444 | const GrFragmentProcessor* SkBitmapProcShader::asFragmentProcessor(GrContext* context, |
| 445 | const SkMatrix& viewM, const SkMatrix* localMatrix, |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 446 | SkFilterQuality filterQuality) const { |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 447 | SkMatrix matrix; |
| 448 | matrix.setIDiv(fRawBitmap.width(), fRawBitmap.height()); |
| 449 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 450 | SkMatrix lmInverse; |
| 451 | if (!this->getLocalMatrix().invert(&lmInverse)) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 452 | return nullptr; |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 453 | } |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 454 | if (localMatrix) { |
| 455 | SkMatrix inv; |
| 456 | if (!localMatrix->invert(&inv)) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 457 | return nullptr; |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 458 | } |
| 459 | lmInverse.postConcat(inv); |
| 460 | } |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 461 | matrix.preConcat(lmInverse); |
commit-bot@chromium.org | cea9abb | 2013-12-09 19:15:37 +0000 | [diff] [blame] | 462 | |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 463 | SkShader::TileMode tm[] = { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 464 | (TileMode)fTileModeX, |
| 465 | (TileMode)fTileModeY, |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 466 | }; |
| 467 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 468 | // Must set wrap and filter on the sampler before requesting a texture. In two places below |
| 469 | // we check the matrix scale factors to determine how to interpret the filter quality setting. |
| 470 | // This completely ignores the complexity of the drawVertices case where explicit local coords |
| 471 | // are provided by the caller. |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 472 | bool doBicubic; |
| 473 | GrTextureParams::FilterMode textureFilterMode = |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 474 | GrSkFilterQualityToGrFilterMode(filterQuality, viewM, this->getLocalMatrix(), |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 475 | &doBicubic); |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 476 | GrTextureParams params(tm, textureFilterMode); |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 477 | SkAutoTUnref<GrTexture> texture(GrRefCachedBitmapTexture(context, fRawBitmap, params)); |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 478 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 479 | if (!texture) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 480 | SkErrorInternals::SetError( kInternalError_SkError, |
| 481 | "Couldn't convert bitmap to texture."); |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 482 | return nullptr; |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 483 | } |
mtklein | 7ef849d | 2014-11-24 09:11:45 -0800 | [diff] [blame] | 484 | |
bsalomon | 8515377 | 2015-11-05 09:35:01 -0800 | [diff] [blame] | 485 | SkAutoTUnref<const GrFragmentProcessor> inner; |
joshualitt | 9bc3954 | 2015-08-12 12:57:54 -0700 | [diff] [blame] | 486 | if (doBicubic) { |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 487 | inner.reset(GrBicubicEffect::Create(texture, matrix, tm)); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 488 | } else { |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 489 | inner.reset(GrSimpleTextureEffect::Create(texture, matrix, params)); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 490 | } |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 491 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 492 | if (kAlpha_8_SkColorType == fRawBitmap.colorType()) { |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 493 | return GrFragmentProcessor::MulOutputByInputUnpremulColor(inner); |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 494 | } |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 495 | return GrFragmentProcessor::MulOutputByInputAlpha(inner); |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 496 | } |
| 497 | |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 498 | #endif |