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 return PictureShaderContext::Create(storage, *this, rec, bitmapShader.detach
()); |
| 123 } |
| 124 |
| 125 ////////////////////////////////////////////////////////////////////////////////
///////// |
| 126 |
| 127 SkShader::Context* SkPictureShader::PictureShaderContext::Create(void* storage, |
| 128 const SkPictureShader& shader, const ContextRec& rec, SkShade
r* bitmapShader) { |
| 129 PictureShaderContext* ctx = SkNEW_PLACEMENT_ARGS(storage, PictureShaderConte
xt, |
| 130 (shader, rec, bitmapShader)
); |
| 131 if (NULL == ctx->fBitmapShaderContext) { |
| 132 ctx->~PictureShaderContext(); |
| 133 ctx = NULL; |
| 134 } |
| 135 return ctx; |
| 136 } |
| 137 |
144 SkPictureShader::PictureShaderContext::PictureShaderContext( | 138 SkPictureShader::PictureShaderContext::PictureShaderContext( |
145 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapSh
ader) | 139 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapSh
ader) |
146 : INHERITED(shader, rec) | 140 : INHERITED(shader, rec) |
147 , fBitmapShader(bitmapShader) | 141 , fBitmapShader(SkRef(bitmapShader)) |
148 { | 142 { |
149 SkASSERT(fBitmapShader); | 143 fBitmapShaderContextStorage = sk_malloc_throw(bitmapShader->contextSize()); |
150 fBitmapShaderContextStorage = sk_malloc_throw(fBitmapShader->contextSize()); | 144 fBitmapShaderContext = bitmapShader->createContext(rec, fBitmapShaderContext
Storage); |
151 fBitmapShaderContext = fBitmapShader->createContext(rec, fBitmapShaderContex
tStorage); | 145 //if fBitmapShaderContext is null, we are invalid |
152 SkASSERT(fBitmapShaderContext); | |
153 } | 146 } |
154 | 147 |
155 SkPictureShader::PictureShaderContext::~PictureShaderContext() { | 148 SkPictureShader::PictureShaderContext::~PictureShaderContext() { |
156 fBitmapShaderContext->~Context(); | 149 if (fBitmapShaderContext) { |
| 150 fBitmapShaderContext->~Context(); |
| 151 } |
157 sk_free(fBitmapShaderContextStorage); | 152 sk_free(fBitmapShaderContextStorage); |
158 } | 153 } |
159 | 154 |
160 uint32_t SkPictureShader::PictureShaderContext::getFlags() const { | 155 uint32_t SkPictureShader::PictureShaderContext::getFlags() const { |
| 156 SkASSERT(fBitmapShaderContext); |
161 return fBitmapShaderContext->getFlags(); | 157 return fBitmapShaderContext->getFlags(); |
162 } | 158 } |
163 | 159 |
164 SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc
(void** ctx) { | 160 SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc
(void** ctx) { |
| 161 SkASSERT(fBitmapShaderContext); |
165 return fBitmapShaderContext->asAShadeProc(ctx); | 162 return fBitmapShaderContext->asAShadeProc(ctx); |
166 } | 163 } |
167 | 164 |
168 void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor ds
tC[], int count) { | 165 void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor ds
tC[], int count) { |
169 SkASSERT(fBitmapShaderContext); | 166 SkASSERT(fBitmapShaderContext); |
170 fBitmapShaderContext->shadeSpan(x, y, dstC, count); | 167 fBitmapShaderContext->shadeSpan(x, y, dstC, count); |
171 } | 168 } |
172 | 169 |
173 void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t d
stC[], int count) { | 170 void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t d
stC[], int count) { |
174 SkASSERT(fBitmapShaderContext); | 171 SkASSERT(fBitmapShaderContext); |
(...skipping 18 matching lines...) Expand all Loading... |
193 | 190 |
194 #if SK_SUPPORT_GPU | 191 #if SK_SUPPORT_GPU |
195 GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& pai
nt) const { | 192 GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& pai
nt) const { |
196 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix
())); | 193 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix
())); |
197 if (!bitmapShader) { | 194 if (!bitmapShader) { |
198 return NULL; | 195 return NULL; |
199 } | 196 } |
200 return bitmapShader->asNewEffect(context, paint); | 197 return bitmapShader->asNewEffect(context, paint); |
201 } | 198 } |
202 #endif | 199 #endif |
OLD | NEW |