Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 "SkPictureShader.h" | 8 #include "SkPictureShader.h" |
| 9 | 9 |
| 10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatr ix)); | 103 fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatr ix)); |
| 104 } | 104 } |
| 105 | 105 |
| 106 // Increment the ref counter inside the mutex to ensure the returned pointer is still valid. | 106 // Increment the ref counter inside the mutex to ensure the returned pointer is still valid. |
| 107 // Otherwise, the pointer may have been overwritten on a different thread be fore the object's | 107 // Otherwise, the pointer may have been overwritten on a different thread be fore the object's |
| 108 // ref count was incremented. | 108 // ref count was incremented. |
| 109 fCachedBitmapShader.get()->ref(); | 109 fCachedBitmapShader.get()->ref(); |
| 110 return fCachedBitmapShader; | 110 return fCachedBitmapShader; |
| 111 } | 111 } |
| 112 | 112 |
| 113 SkShader* SkPictureShader::validInternal(const ContextRec& rec, SkMatrix* totalI nverse) const { | |
| 114 if (!this->INHERITED::validContext(rec, totalInverse)) { | |
| 115 return NULL; | |
| 116 } | |
| 117 | |
| 118 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix)); | |
| 119 if (!bitmapShader || !bitmapShader->validContext(rec)) { | |
| 120 return NULL; | |
| 121 } | |
| 122 | |
| 123 return bitmapShader.detach(); | |
| 124 } | |
| 125 | |
| 126 bool SkPictureShader::validContext(const ContextRec& rec, SkMatrix* totalInverse ) const { | |
| 127 SkAutoTUnref<SkShader> shader(this->validInternal(rec, totalInverse)); | |
| 128 return shader != NULL; | |
| 129 } | |
| 130 | |
| 131 SkShader::Context* SkPictureShader::createContext(const ContextRec& rec, void* s torage) const { | |
| 132 SkAutoTUnref<SkShader> bitmapShader(this->validInternal(rec, NULL)); | |
| 133 if (!bitmapShader) { | |
| 134 return NULL; | |
| 135 } | |
| 136 | |
| 137 return SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext, (*this, rec, bitm apShader.detach())); | |
| 138 } | |
| 139 | |
| 140 size_t SkPictureShader::contextSize() const { | 113 size_t SkPictureShader::contextSize() const { |
| 141 return sizeof(PictureShaderContext); | 114 return sizeof(PictureShaderContext); |
| 142 } | 115 } |
| 143 | 116 |
| 117 SkShader::Context* SkPictureShader::onCreateContext(const ContextRec& rec, void* storage) const { | |
| 118 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix)); | |
| 119 if (NULL == bitmapShader.get()) { | |
| 120 return NULL; | |
| 121 } | |
| 122 | |
| 123 PictureShaderContext* ctx = SkNEW_PLACEMENT_ARGS(storage, PictureShaderConte xt, | |
| 124 (*this, rec, bitmapShader.d etach())); | |
| 125 if (!ctx->isValid()) { | |
|
scroggo
2014/05/05 14:01:19
Don't we have a general rule against creating an o
reed1
2014/05/05 15:10:01
Done.
| |
| 126 ctx->~PictureShaderContext(); | |
| 127 ctx = NULL; | |
| 128 } | |
| 129 return ctx; | |
| 130 } | |
| 131 | |
| 132 //////////////////////////////////////////////////////////////////////////////// ///////// | |
| 133 | |
| 144 SkPictureShader::PictureShaderContext::PictureShaderContext( | 134 SkPictureShader::PictureShaderContext::PictureShaderContext( |
| 145 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapSh ader) | 135 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapSh ader) |
| 146 : INHERITED(shader, rec) | 136 : INHERITED(shader, rec) |
| 147 , fBitmapShader(bitmapShader) | 137 , fBitmapShader(SkRef(bitmapShader)) |
| 148 { | 138 { |
| 149 SkASSERT(fBitmapShader); | 139 fBitmapShaderContextStorage = sk_malloc_throw(bitmapShader->contextSize()); |
| 150 fBitmapShaderContextStorage = sk_malloc_throw(fBitmapShader->contextSize()); | 140 fBitmapShaderContext = bitmapShader->createContext(rec, fBitmapShaderContext Storage); |
| 151 fBitmapShaderContext = fBitmapShader->createContext(rec, fBitmapShaderContex tStorage); | 141 //if fBitmapShaderContext is null, we are invalid |
| 152 SkASSERT(fBitmapShaderContext); | |
| 153 } | 142 } |
| 154 | 143 |
| 155 SkPictureShader::PictureShaderContext::~PictureShaderContext() { | 144 SkPictureShader::PictureShaderContext::~PictureShaderContext() { |
| 156 fBitmapShaderContext->~Context(); | 145 if (fBitmapShaderContext) { |
| 146 fBitmapShaderContext->~Context(); | |
| 147 } | |
| 157 sk_free(fBitmapShaderContextStorage); | 148 sk_free(fBitmapShaderContextStorage); |
| 158 } | 149 } |
| 159 | 150 |
| 160 uint32_t SkPictureShader::PictureShaderContext::getFlags() const { | 151 uint32_t SkPictureShader::PictureShaderContext::getFlags() const { |
| 152 SkASSERT(fBitmapShaderContext); | |
| 161 return fBitmapShaderContext->getFlags(); | 153 return fBitmapShaderContext->getFlags(); |
| 162 } | 154 } |
| 163 | 155 |
| 164 SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc (void** ctx) { | 156 SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc (void** ctx) { |
| 157 SkASSERT(fBitmapShaderContext); | |
| 165 return fBitmapShaderContext->asAShadeProc(ctx); | 158 return fBitmapShaderContext->asAShadeProc(ctx); |
| 166 } | 159 } |
| 167 | 160 |
| 168 void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor ds tC[], int count) { | 161 void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor ds tC[], int count) { |
| 169 SkASSERT(fBitmapShaderContext); | 162 SkASSERT(fBitmapShaderContext); |
| 170 fBitmapShaderContext->shadeSpan(x, y, dstC, count); | 163 fBitmapShaderContext->shadeSpan(x, y, dstC, count); |
| 171 } | 164 } |
| 172 | 165 |
| 173 void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t d stC[], int count) { | 166 void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t d stC[], int count) { |
| 174 SkASSERT(fBitmapShaderContext); | 167 SkASSERT(fBitmapShaderContext); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 193 | 186 |
| 194 #if SK_SUPPORT_GPU | 187 #if SK_SUPPORT_GPU |
| 195 GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& pai nt) const { | 188 GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& pai nt) const { |
| 196 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix ())); | 189 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix ())); |
| 197 if (!bitmapShader) { | 190 if (!bitmapShader) { |
| 198 return NULL; | 191 return NULL; |
| 199 } | 192 } |
| 200 return bitmapShader->asNewEffect(context, paint); | 193 return bitmapShader->asNewEffect(context, paint); |
| 201 } | 194 } |
| 202 #endif | 195 #endif |
| OLD | NEW |