| 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 context takes ownership of the info. It will call its destructor | 30 // The info has been allocated elsewhere, but we are responsible for calling
its destructor. |
| 31 // but will NOT free the memory. | |
| 32 BitmapProcInfoContext(const SkShader& shader, const SkShader::ContextRec& re
c, | 31 BitmapProcInfoContext(const SkShader& shader, const SkShader::ContextRec& re
c, |
| 33 SkBitmapProcInfo* info) | 32 SkBitmapProcInfo* info) |
| 34 : INHERITED(shader, rec) | 33 : INHERITED(shader, rec) |
| 35 , fInfo(info) | 34 , fInfo(info) |
| 36 { | 35 { |
| 37 fFlags = 0; | 36 fFlags = 0; |
| 38 if (fInfo->fPixmap.isOpaque() && (255 == this->getPaintAlpha())) { | 37 if (fInfo->fPixmap.isOpaque() && (255 == this->getPaintAlpha())) { |
| 39 fFlags |= SkShader::kOpaqueAlpha_Flag; | 38 fFlags |= SkShader::kOpaqueAlpha_Flag; |
| 40 } | 39 } |
| 41 | 40 |
| 42 if (1 == fInfo->fPixmap.height() && only_scale_and_translate(this->getTo
talInverse())) { | 41 if (1 == fInfo->fPixmap.height() && only_scale_and_translate(this->getTo
talInverse())) { |
| 43 fFlags |= SkShader::kConstInY32_Flag; | 42 fFlags |= SkShader::kConstInY32_Flag; |
| 44 } | 43 } |
| 45 } | 44 } |
| 46 | 45 |
| 47 ~BitmapProcInfoContext() override { | 46 ~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. | |
| 50 fInfo->~SkBitmapProcInfo(); | 47 fInfo->~SkBitmapProcInfo(); |
| 51 } | 48 } |
| 52 | 49 |
| 53 uint32_t getFlags() const override { return fFlags; } | 50 uint32_t getFlags() const override { return fFlags; } |
| 54 | 51 |
| 55 private: | 52 private: |
| 56 SkBitmapProcInfo* fInfo; | 53 SkBitmapProcInfo* fInfo; |
| 57 uint32_t fFlags; | 54 uint32_t fFlags; |
| 58 | 55 |
| 59 typedef SkShader::Context INHERITED; | 56 typedef SkShader::Context INHERITED; |
| 60 }; | 57 }; |
| 61 | 58 |
| 62 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 59 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 63 | 60 |
| 64 class BitmapProcShaderContext : public BitmapProcInfoContext { | 61 class BitmapProcShaderContext : public BitmapProcInfoContext { |
| 65 public: | 62 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, | 63 BitmapProcShaderContext(const SkShader& shader, const SkShader::ContextRec&
rec, |
| 69 SkBitmapProcState* state) | 64 SkBitmapProcState* state) |
| 70 : INHERITED(shader, rec, state) | 65 : INHERITED(shader, rec, state) |
| 71 , fState(state) | 66 , fState(state) |
| 72 {} | 67 {} |
| 73 | 68 |
| 74 void shadeSpan(int x, int y, SkPMColor dstC[], int count) override { | 69 void shadeSpan(int x, int y, SkPMColor dstC[], int count) override { |
| 75 const SkBitmapProcState& state = *fState; | 70 const SkBitmapProcState& state = *fState; |
| 76 if (state.getShaderProc32()) { | 71 if (state.getShaderProc32()) { |
| 77 state.getShaderProc32()(&state, x, y, dstC, count); | 72 state.getShaderProc32()(&state, x, y, dstC, count); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 return nullptr; | 104 return nullptr; |
| 110 } | 105 } |
| 111 | 106 |
| 112 private: | 107 private: |
| 113 SkBitmapProcState* fState; | 108 SkBitmapProcState* fState; |
| 114 | 109 |
| 115 typedef BitmapProcInfoContext INHERITED; | 110 typedef BitmapProcInfoContext INHERITED; |
| 116 }; | 111 }; |
| 117 | 112 |
| 118 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 113 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 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 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 119 | 159 |
| 120 size_t SkBitmapProcShader::ContextSize(const ContextRec& rec) { | 160 static bool choose_linear_pipeline(const SkShader::ContextRec& rec, const SkImag
eInfo& srcInfo) { |
| 121 // The SkBitmapProcState is stored outside of the context object, with the c
ontext holding | 161 // These src attributes are not supported in the new 4f context (yet) |
| 122 // a pointer to it. | 162 // |
| 123 return sizeof(BitmapProcShaderContext) + sizeof(SkBitmapProcState); | 163 if (srcInfo.bytesPerPixel() < 4 || |
| 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); |
| 124 } | 188 } |
| 125 | 189 |
| 126 SkShader::Context* SkBitmapProcShader::MakeContext(const SkShader& shader, | 190 SkShader::Context* SkBitmapProcShader::MakeContext(const SkShader& shader, |
| 127 TileMode tmx, TileMode tmy, | 191 TileMode tmx, TileMode tmy, |
| 128 const SkBitmapProvider& provi
der, | 192 const SkBitmapProvider& provi
der, |
| 129 const ContextRec& rec, void*
storage) { | 193 const ContextRec& rec, void*
storage) { |
| 130 SkMatrix totalInverse; | 194 SkMatrix totalInverse; |
| 131 // Do this first, so we know the matrix can be inverted. | 195 // Do this first, so we know the matrix can be inverted. |
| 132 if (!shader.computeTotalInverse(rec, &totalInverse)) { | 196 if (!shader.computeTotalInverse(rec, &totalInverse)) { |
| 133 return nullptr; | 197 return nullptr; |
| 134 } | 198 } |
| 135 | 199 |
| 136 void* stateStorage = (char*)storage + sizeof(BitmapProcShaderContext); | 200 // Decide if we can/want to use the new linear pipeine |
| 137 SkBitmapProcState* state = new (stateStorage) SkBitmapProcState(provider, tm
x, tmy); | 201 bool useLinearPipeline = choose_linear_pipeline(rec, provider.info()); |
| 138 | 202 if (SkShader::kMirror_TileMode == tmx || SkShader::kMirror_TileMode == tmy)
{ |
| 139 SkASSERT(state); | 203 useLinearPipeline = false; |
| 140 if (!state->setup(totalInverse, *rec.fPaint)) { | 204 } |
| 141 state->~SkBitmapProcState(); | 205 if (totalInverse.hasPerspective()) { |
| 142 return nullptr; | 206 useLinearPipeline = false; |
| 143 } | 207 } |
| 144 | 208 |
| 145 return new (storage) BitmapProcShaderContext(shader, rec, state); | 209 if (useLinearPipeline) { |
| 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 } |
| 146 } | 226 } |
| 147 | 227 |
| 148 SkShader::Context* SkBitmapProcShader::onCreateContext(const ContextRec& rec, vo
id* storage) const { | 228 SkShader::Context* SkBitmapProcShader::onCreateContext(const ContextRec& rec, vo
id* storage) const { |
| 149 return MakeContext(*this, (TileMode)fTileModeX, (TileMode)fTileModeY, | 229 return MakeContext(*this, (TileMode)fTileModeX, (TileMode)fTileModeY, |
| 150 SkBitmapProvider(fRawBitmap), rec, storage); | 230 SkBitmapProvider(fRawBitmap), rec, storage); |
| 151 } | 231 } |
| 152 | 232 |
| 153 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 233 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 154 | 234 |
| 155 SkBitmapProcShader::SkBitmapProcShader(const SkBitmap& src, TileMode tmx, TileMo
de tmy, | 235 SkBitmapProcShader::SkBitmapProcShader(const SkBitmap& src, TileMode tmx, TileMo
de tmy, |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 inner.reset(GrSimpleTextureEffect::Create(texture, matrix, params)); | 433 inner.reset(GrSimpleTextureEffect::Create(texture, matrix, params)); |
| 354 } | 434 } |
| 355 | 435 |
| 356 if (kAlpha_8_SkColorType == fRawBitmap.colorType()) { | 436 if (kAlpha_8_SkColorType == fRawBitmap.colorType()) { |
| 357 return GrFragmentProcessor::MulOutputByInputUnpremulColor(inner); | 437 return GrFragmentProcessor::MulOutputByInputUnpremulColor(inner); |
| 358 } | 438 } |
| 359 return GrFragmentProcessor::MulOutputByInputAlpha(inner); | 439 return GrFragmentProcessor::MulOutputByInputAlpha(inner); |
| 360 } | 440 } |
| 361 | 441 |
| 362 #endif | 442 #endif |
| OLD | NEW |