| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmapProcShader.h" | 8 #include "SkBitmapProcShader.h" |
| 9 #include "SkBitmapProcState.h" | 9 #include "SkBitmapProcState.h" |
| 10 #include "SkBitmapProvider.h" | 10 #include "SkBitmapProvider.h" |
| 11 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" |
| 12 #include "SkErrorInternals.h" | 12 #include "SkErrorInternals.h" |
| 13 #include "SkPixelRef.h" | 13 #include "SkPixelRef.h" |
| 14 #include "SkReadBuffer.h" | 14 #include "SkReadBuffer.h" |
| 15 #include "SkWriteBuffer.h" | 15 #include "SkWriteBuffer.h" |
| 16 | 16 |
| 17 #if SK_SUPPORT_GPU | 17 #if SK_SUPPORT_GPU |
| 18 #include "SkGrPriv.h" | 18 #include "SkGrPriv.h" |
| 19 #include "effects/GrBicubicEffect.h" | 19 #include "effects/GrBicubicEffect.h" |
| 20 #include "effects/GrSimpleTextureEffect.h" | 20 #include "effects/GrSimpleTextureEffect.h" |
| 21 #endif | 21 #endif |
| 22 | 22 |
| 23 static bool only_scale_and_translate(const SkMatrix& matrix) { | 23 static bool only_scale_and_translate(const SkMatrix& matrix) { |
| 24 unsigned mask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask; | 24 unsigned mask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask; |
| 25 return (matrix.getType() & ~mask) == 0; | 25 return (matrix.getType() & ~mask) == 0; |
| 26 } | 26 } |
| 27 | 27 |
| 28 class BitmapProcInfoContext : public SkShader::Context { | 28 class BitmapProcInfoContext : public SkShader::Context { |
| 29 public: | 29 public: |
| 30 // The info has been allocated elsewhere, but we are responsible for calling
its destructor. | 30 // The context takes ownership of the info. It will call its destructor |
| 31 // but will NOT free the memory. |
| 31 BitmapProcInfoContext(const SkShader& shader, const SkShader::ContextRec& re
c, | 32 BitmapProcInfoContext(const SkShader& shader, const SkShader::ContextRec& re
c, |
| 32 SkBitmapProcInfo* info) | 33 SkBitmapProcInfo* info) |
| 33 : INHERITED(shader, rec) | 34 : INHERITED(shader, rec) |
| 34 , fInfo(info) | 35 , fInfo(info) |
| 35 { | 36 { |
| 36 fFlags = 0; | 37 fFlags = 0; |
| 37 if (fInfo->fPixmap.isOpaque() && (255 == this->getPaintAlpha())) { | 38 if (fInfo->fPixmap.isOpaque() && (255 == this->getPaintAlpha())) { |
| 38 fFlags |= SkShader::kOpaqueAlpha_Flag; | 39 fFlags |= SkShader::kOpaqueAlpha_Flag; |
| 39 } | 40 } |
| 40 | 41 |
| 41 if (1 == fInfo->fPixmap.height() && only_scale_and_translate(this->getTo
talInverse())) { | 42 if (1 == fInfo->fPixmap.height() && only_scale_and_translate(this->getTo
talInverse())) { |
| 42 fFlags |= SkShader::kConstInY32_Flag; | 43 fFlags |= SkShader::kConstInY32_Flag; |
| 43 } | 44 } |
| 44 } | 45 } |
| 45 | 46 |
| 46 ~BitmapProcInfoContext() override { | 47 ~BitmapProcInfoContext() override { |
| 48 // The bitmap proc state has been created outside of the context on memo
ry that will be freed |
| 49 // elsewhere. Only call the destructor but leave the freeing of the memo
ry to the caller. |
| 47 fInfo->~SkBitmapProcInfo(); | 50 fInfo->~SkBitmapProcInfo(); |
| 48 } | 51 } |
| 49 | 52 |
| 50 uint32_t getFlags() const override { return fFlags; } | 53 uint32_t getFlags() const override { return fFlags; } |
| 51 | 54 |
| 52 private: | 55 private: |
| 53 SkBitmapProcInfo* fInfo; | 56 SkBitmapProcInfo* fInfo; |
| 54 uint32_t fFlags; | 57 uint32_t fFlags; |
| 55 | 58 |
| 56 typedef SkShader::Context INHERITED; | 59 typedef SkShader::Context INHERITED; |
| 57 }; | 60 }; |
| 58 | 61 |
| 59 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 62 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 60 | 63 |
| 61 class BitmapProcShaderContext : public BitmapProcInfoContext { | 64 class BitmapProcShaderContext : public BitmapProcInfoContext { |
| 62 public: | 65 public: |
| 66 // The context takes ownership of the state. It will call its destructor |
| 67 // but will NOT free the memory. |
| 63 BitmapProcShaderContext(const SkShader& shader, const SkShader::ContextRec&
rec, | 68 BitmapProcShaderContext(const SkShader& shader, const SkShader::ContextRec&
rec, |
| 64 SkBitmapProcState* state) | 69 SkBitmapProcState* state) |
| 65 : INHERITED(shader, rec, state) | 70 : INHERITED(shader, rec, state) |
| 66 , fState(state) | 71 , fState(state) |
| 67 {} | 72 {} |
| 68 | 73 |
| 69 void shadeSpan(int x, int y, SkPMColor dstC[], int count) override { | 74 void shadeSpan(int x, int y, SkPMColor dstC[], int count) override { |
| 70 const SkBitmapProcState& state = *fState; | 75 const SkBitmapProcState& state = *fState; |
| 71 if (state.getShaderProc32()) { | 76 if (state.getShaderProc32()) { |
| 72 state.getShaderProc32()(&state, x, y, dstC, count); | 77 state.getShaderProc32()(&state, x, y, dstC, count); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 return nullptr; | 109 return nullptr; |
| 105 } | 110 } |
| 106 | 111 |
| 107 private: | 112 private: |
| 108 SkBitmapProcState* fState; | 113 SkBitmapProcState* fState; |
| 109 | 114 |
| 110 typedef BitmapProcInfoContext INHERITED; | 115 typedef BitmapProcInfoContext INHERITED; |
| 111 }; | 116 }; |
| 112 | 117 |
| 113 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 118 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 114 #include "SkLinearBitmapPipeline.h" | |
| 115 #include "SkPM4f.h" | |
| 116 #include "SkXfermode.h" | |
| 117 | |
| 118 class LinearPipelineContext : public BitmapProcInfoContext { | |
| 119 public: | |
| 120 LinearPipelineContext(const SkShader& shader, const SkShader::ContextRec& re
c, | |
| 121 SkBitmapProcInfo* info) | |
| 122 : INHERITED(shader, rec, info) | |
| 123 , fPipeline(info->fInvMatrix, info->fFilterQuality, info->fTileModeX, in
fo->fTileModeY, | |
| 124 info->fPixmap) | |
| 125 { | |
| 126 // To implement the old shadeSpan entry-point, we need to efficiently co
nvert our native | |
| 127 // floats into SkPMColor. The SkXfermode::D32Procs do exactly that. | |
| 128 // | |
| 129 sk_sp<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kSrc_Mode)); | |
| 130 fXferProc = SkXfermode::GetD32Proc(xfer.get(), 0); | |
| 131 } | |
| 132 | |
| 133 void shadeSpan4f(int x, int y, SkPM4f dstC[], int count) override { | |
| 134 fPipeline.shadeSpan4f(x, y, dstC, count); | |
| 135 } | |
| 136 | |
| 137 void shadeSpan(int x, int y, SkPMColor dstC[], int count) override { | |
| 138 const int N = 128; | |
| 139 SkPM4f tmp[N]; | |
| 140 | |
| 141 while (count > 0) { | |
| 142 const int n = SkTMin(count, N); | |
| 143 fPipeline.shadeSpan4f(x, y, tmp, n); | |
| 144 fXferProc(nullptr, dstC, tmp, n, nullptr); | |
| 145 dstC += n; | |
| 146 x += n; | |
| 147 count -= n; | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 private: | |
| 152 SkLinearBitmapPipeline fPipeline; | |
| 153 SkXfermode::D32Proc fXferProc; | |
| 154 | |
| 155 typedef BitmapProcInfoContext INHERITED; | |
| 156 }; | |
| 157 | |
| 158 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | |
| 159 | 119 |
| 160 static bool choose_linear_pipeline(const SkShader::ContextRec& rec, const SkImag
eInfo& srcInfo) { | 120 size_t SkBitmapProcShader::ContextSize(const ContextRec& rec) { |
| 161 // These src attributes are not supported in the new 4f context (yet) | 121 // The SkBitmapProcState is stored outside of the context object, with the c
ontext holding |
| 162 // | 122 // a pointer to it. |
| 163 if (srcInfo.bytesPerPixel() < 4 || | 123 return sizeof(BitmapProcShaderContext) + sizeof(SkBitmapProcState); |
| 164 kRGBA_F16_SkColorType == srcInfo.colorType()) { | |
| 165 return false; | |
| 166 } | |
| 167 | |
| 168 #if 0 // later we may opt-in to the new code even if the client hasn't request
ed it... | |
| 169 // These src attributes are only supported in the new 4f context | |
| 170 // | |
| 171 if (srcInfo.isSRGB() || | |
| 172 kUnpremul_SkAlphaType == srcInfo.alphaType() || | |
| 173 (4 == srcInfo.bytesPerPixel() && kN32_SkColorType != srcInfo.colorType()
)) | |
| 174 { | |
| 175 return true; | |
| 176 } | |
| 177 #endif | |
| 178 | |
| 179 // If we get here, we can reasonably use either context, respect the caller'
s preference | |
| 180 // | |
| 181 return SkShader::ContextRec::kPM4f_DstType == rec.fPreferredDstType; | |
| 182 } | |
| 183 | |
| 184 size_t SkBitmapProcShader::ContextSize(const ContextRec& rec, const SkImageInfo&
srcInfo) { | |
| 185 size_t size0 = sizeof(BitmapProcShaderContext) + sizeof(SkBitmapProcState); | |
| 186 size_t size1 = sizeof(LinearPipelineContext) + sizeof(SkBitmapProcInfo); | |
| 187 return SkTMax(size0, size1); | |
| 188 } | 124 } |
| 189 | 125 |
| 190 SkShader::Context* SkBitmapProcShader::MakeContext(const SkShader& shader, | 126 SkShader::Context* SkBitmapProcShader::MakeContext(const SkShader& shader, |
| 191 TileMode tmx, TileMode tmy, | 127 TileMode tmx, TileMode tmy, |
| 192 const SkBitmapProvider& provi
der, | 128 const SkBitmapProvider& provi
der, |
| 193 const ContextRec& rec, void*
storage) { | 129 const ContextRec& rec, void*
storage) { |
| 194 SkMatrix totalInverse; | 130 SkMatrix totalInverse; |
| 195 // Do this first, so we know the matrix can be inverted. | 131 // Do this first, so we know the matrix can be inverted. |
| 196 if (!shader.computeTotalInverse(rec, &totalInverse)) { | 132 if (!shader.computeTotalInverse(rec, &totalInverse)) { |
| 197 return nullptr; | 133 return nullptr; |
| 198 } | 134 } |
| 199 | 135 |
| 200 // Decide if we can/want to use the new linear pipeine | 136 void* stateStorage = (char*)storage + sizeof(BitmapProcShaderContext); |
| 201 bool useLinearPipeline = choose_linear_pipeline(rec, provider.info()); | 137 SkBitmapProcState* state = new (stateStorage) SkBitmapProcState(provider, tm
x, tmy); |
| 202 if (SkShader::kMirror_TileMode == tmx || SkShader::kMirror_TileMode == tmy)
{ | 138 |
| 203 useLinearPipeline = false; | 139 SkASSERT(state); |
| 204 } | 140 if (!state->setup(totalInverse, *rec.fPaint)) { |
| 205 if (totalInverse.hasPerspective()) { | 141 state->~SkBitmapProcState(); |
| 206 useLinearPipeline = false; | 142 return nullptr; |
| 207 } | 143 } |
| 208 | 144 |
| 209 if (useLinearPipeline) { | 145 return new (storage) BitmapProcShaderContext(shader, rec, state); |
| 210 void* infoStorage = (char*)storage + sizeof(LinearPipelineContext); | |
| 211 SkBitmapProcInfo* info = new (infoStorage) SkBitmapProcInfo(provider, tm
x, tmy); | |
| 212 if (!info->init(totalInverse, *rec.fPaint)) { | |
| 213 info->~SkBitmapProcInfo(); | |
| 214 return nullptr; | |
| 215 } | |
| 216 return new (storage) LinearPipelineContext(shader, rec, info); | |
| 217 } else { | |
| 218 void* stateStorage = (char*)storage + sizeof(BitmapProcShaderContext); | |
| 219 SkBitmapProcState* state = new (stateStorage) SkBitmapProcState(provider
, tmx, tmy); | |
| 220 if (!state->setup(totalInverse, *rec.fPaint)) { | |
| 221 state->~SkBitmapProcState(); | |
| 222 return nullptr; | |
| 223 } | |
| 224 return new (storage) BitmapProcShaderContext(shader, rec, state); | |
| 225 } | |
| 226 } | 146 } |
| 227 | 147 |
| 228 SkShader::Context* SkBitmapProcShader::onCreateContext(const ContextRec& rec, vo
id* storage) const { | 148 SkShader::Context* SkBitmapProcShader::onCreateContext(const ContextRec& rec, vo
id* storage) const { |
| 229 return MakeContext(*this, (TileMode)fTileModeX, (TileMode)fTileModeY, | 149 return MakeContext(*this, (TileMode)fTileModeX, (TileMode)fTileModeY, |
| 230 SkBitmapProvider(fRawBitmap), rec, storage); | 150 SkBitmapProvider(fRawBitmap), rec, storage); |
| 231 } | 151 } |
| 232 | 152 |
| 233 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 153 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 234 | 154 |
| 235 SkBitmapProcShader::SkBitmapProcShader(const SkBitmap& src, TileMode tmx, TileMo
de tmy, | 155 SkBitmapProcShader::SkBitmapProcShader(const SkBitmap& src, TileMode tmx, TileMo
de tmy, |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 inner.reset(GrSimpleTextureEffect::Create(texture, matrix, params)); | 353 inner.reset(GrSimpleTextureEffect::Create(texture, matrix, params)); |
| 434 } | 354 } |
| 435 | 355 |
| 436 if (kAlpha_8_SkColorType == fRawBitmap.colorType()) { | 356 if (kAlpha_8_SkColorType == fRawBitmap.colorType()) { |
| 437 return GrFragmentProcessor::MulOutputByInputUnpremulColor(inner); | 357 return GrFragmentProcessor::MulOutputByInputUnpremulColor(inner); |
| 438 } | 358 } |
| 439 return GrFragmentProcessor::MulOutputByInputAlpha(inner); | 359 return GrFragmentProcessor::MulOutputByInputAlpha(inner); |
| 440 } | 360 } |
| 441 | 361 |
| 442 #endif | 362 #endif |
| OLD | NEW |