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 "SkColorCubeFilter.h" | 8 #include "SkColorCubeFilter.h" |
9 #include "SkColorPriv.h" | 9 #include "SkColorPriv.h" |
10 #include "SkOnce.h" | 10 #include "SkOnce.h" |
11 #include "SkReadBuffer.h" | 11 #include "SkReadBuffer.h" |
12 #include "SkUnPreMultiply.h" | 12 #include "SkUnPreMultiply.h" |
13 #include "SkWriteBuffer.h" | 13 #include "SkWriteBuffer.h" |
14 #if SK_SUPPORT_GPU | 14 #if SK_SUPPORT_GPU |
15 #include "GrContext.h" | 15 #include "GrContext.h" |
16 #include "GrCoordTransform.h" | 16 #include "GrCoordTransform.h" |
17 #include "gl/GrGLProcessor.h" | 17 #include "gl/GrGLProcessor.h" |
18 #include "gl/builders/GrGLProgramBuilder.h" | 18 #include "gl/builders/GrGLProgramBuilder.h" |
19 #include "GrTBackendProcessorFactory.h" | 19 #include "GrTBackendProcessorFactory.h" |
20 #include "GrTexturePriv.h" | 20 #include "GrTexturePriv.h" |
21 #include "SkGr.h" | 21 #include "SkGr.h" |
22 #endif | 22 #endif |
23 | 23 |
24 /////////////////////////////////////////////////////////////////////////////// | 24 /////////////////////////////////////////////////////////////////////////////// |
25 namespace { | 25 namespace { |
26 | 26 |
27 int32_t SkNextColorProfileUniqueID() { | 27 int32_t SkNextColorCubeUniqueID() { |
28 static int32_t gColorProfileUniqueID; | 28 static int32_t gColorCubeUniqueID; |
29 // do a loop in case our global wraps around, as we never want to return a 0 | 29 // do a loop in case our global wraps around, as we never want to return a 0 |
30 int32_t genID; | 30 int32_t genID; |
31 do { | 31 do { |
32 genID = sk_atomic_inc(&gColorProfileUniqueID) + 1; | 32 genID = sk_atomic_inc(&gColorCubeUniqueID) + 1; |
33 } while (0 == genID); | 33 } while (0 == genID); |
34 return genID; | 34 return genID; |
35 } | 35 } |
36 | 36 |
37 } // end namespace | 37 } // end namespace |
38 | 38 |
39 static const int MIN_CUBE_SIZE = 4; | 39 static const int MIN_CUBE_SIZE = 4; |
40 static const int MAX_CUBE_SIZE = 64; | 40 static const int MAX_CUBE_SIZE = 64; |
41 | 41 |
42 static bool is_valid_3D_lut(SkData* cubeData, int cubeDimension) { | 42 static bool is_valid_3D_lut(SkData* cubeData, int cubeDimension) { |
43 size_t minMemorySize = sizeof(uint8_t) * 4 * cubeDimension * cubeDimension *
cubeDimension; | 43 size_t minMemorySize = sizeof(uint8_t) * 4 * cubeDimension * cubeDimension *
cubeDimension; |
44 return (cubeDimension >= MIN_CUBE_SIZE) && (cubeDimension <= MAX_CUBE_SIZE)
&& | 44 return (cubeDimension >= MIN_CUBE_SIZE) && (cubeDimension <= MAX_CUBE_SIZE)
&& |
45 (NULL != cubeData) && (cubeData->size() >= minMemorySize); | 45 (NULL != cubeData) && (cubeData->size() >= minMemorySize); |
46 } | 46 } |
47 | 47 |
48 SkColorFilter* SkColorCubeFilter::Create(SkData* cubeData, int cubeDimension) { | 48 SkColorFilter* SkColorCubeFilter::Create(SkData* cubeData, int cubeDimension) { |
49 if (!is_valid_3D_lut(cubeData, cubeDimension)) { | 49 if (!is_valid_3D_lut(cubeData, cubeDimension)) { |
50 return NULL; | 50 return NULL; |
51 } | 51 } |
52 | 52 |
53 return SkNEW_ARGS(SkColorCubeFilter, (cubeData, cubeDimension)); | 53 return SkNEW_ARGS(SkColorCubeFilter, (cubeData, cubeDimension)); |
54 } | 54 } |
55 | 55 |
56 SkColorCubeFilter::SkColorCubeFilter(SkData* cubeData, int cubeDimension) | 56 SkColorCubeFilter::SkColorCubeFilter(SkData* cubeData, int cubeDimension) |
57 : fCubeData(SkRef(cubeData)) | 57 : fCubeData(SkRef(cubeData)) |
58 , fUniqueID(SkNextColorProfileUniqueID()) | 58 , fUniqueID(SkNextColorCubeUniqueID()) |
59 , fCache(cubeDimension) { | 59 , fCache(cubeDimension) { |
60 } | 60 } |
61 | 61 |
62 uint32_t SkColorCubeFilter::getFlags() const { | 62 uint32_t SkColorCubeFilter::getFlags() const { |
63 return this->INHERITED::getFlags() | kAlphaUnchanged_Flag; | 63 return this->INHERITED::getFlags() | kAlphaUnchanged_Flag; |
64 } | 64 } |
65 | 65 |
66 SkColorCubeFilter::ColorCubeProcesingCache::ColorCubeProcesingCache(int cubeDime
nsion) | 66 SkColorCubeFilter::ColorCubeProcesingCache::ColorCubeProcesingCache(int cubeDime
nsion) |
67 : fCubeDimension(cubeDimension) | 67 : fCubeDimension(cubeDimension) |
68 , fLutsInited(false) { | 68 , fLutsInited(false) { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 SkScalarRoundToInt(gOut * aOut), | 158 SkScalarRoundToInt(gOut * aOut), |
159 SkScalarRoundToInt(bOut * aOut)); | 159 SkScalarRoundToInt(bOut * aOut)); |
160 } | 160 } |
161 } | 161 } |
162 | 162 |
163 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING | 163 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
164 SkColorCubeFilter::SkColorCubeFilter(SkReadBuffer& buffer) | 164 SkColorCubeFilter::SkColorCubeFilter(SkReadBuffer& buffer) |
165 : fCache(buffer.readInt()) { | 165 : fCache(buffer.readInt()) { |
166 fCubeData.reset(buffer.readByteArrayAsData()); | 166 fCubeData.reset(buffer.readByteArrayAsData()); |
167 buffer.validate(is_valid_3D_lut(fCubeData, fCache.cubeDimension())); | 167 buffer.validate(is_valid_3D_lut(fCubeData, fCache.cubeDimension())); |
168 fUniqueID = SkNextColorProfileUniqueID(); | 168 fUniqueID = SkNextColorCubeUniqueID(); |
169 } | 169 } |
170 #endif | 170 #endif |
171 | 171 |
172 SkFlattenable* SkColorCubeFilter::CreateProc(SkReadBuffer& buffer) { | 172 SkFlattenable* SkColorCubeFilter::CreateProc(SkReadBuffer& buffer) { |
173 int cubeDimension = buffer.readInt(); | 173 int cubeDimension = buffer.readInt(); |
174 SkAutoDataUnref cubeData(buffer.readByteArrayAsData()); | 174 SkAutoDataUnref cubeData(buffer.readByteArrayAsData()); |
175 if (!buffer.validate(is_valid_3D_lut(cubeData, cubeDimension))) { | 175 if (!buffer.validate(is_valid_3D_lut(cubeData, cubeDimension))) { |
176 return NULL; | 176 return NULL; |
177 } | 177 } |
178 return Create(cubeData, cubeDimension); | 178 return Create(cubeData, cubeDimension); |
179 } | 179 } |
180 | 180 |
181 void SkColorCubeFilter::flatten(SkWriteBuffer& buffer) const { | 181 void SkColorCubeFilter::flatten(SkWriteBuffer& buffer) const { |
182 this->INHERITED::flatten(buffer); | 182 this->INHERITED::flatten(buffer); |
183 buffer.writeInt(fCache.cubeDimension()); | 183 buffer.writeInt(fCache.cubeDimension()); |
184 buffer.writeDataAsByteArray(fCubeData); | 184 buffer.writeDataAsByteArray(fCubeData); |
185 } | 185 } |
186 | 186 |
187 #ifndef SK_IGNORE_TO_STRING | 187 #ifndef SK_IGNORE_TO_STRING |
188 void SkColorCubeFilter::toString(SkString* str) const { | 188 void SkColorCubeFilter::toString(SkString* str) const { |
189 str->append("SkColorCubeFilter "); | 189 str->append("SkColorCubeFilter "); |
190 } | 190 } |
191 #endif | 191 #endif |
192 | 192 |
193 /////////////////////////////////////////////////////////////////////////////// | 193 /////////////////////////////////////////////////////////////////////////////// |
194 #if SK_SUPPORT_GPU | 194 #if SK_SUPPORT_GPU |
195 class GrColorProfileEffect : public GrFragmentProcessor { | 195 class GrColorCubeEffect : public GrFragmentProcessor { |
196 public: | 196 public: |
197 static GrFragmentProcessor* Create(GrTexture* colorCube) { | 197 static GrFragmentProcessor* Create(GrTexture* colorCube) { |
198 return (NULL != colorCube) ? SkNEW_ARGS(GrColorProfileEffect, (colorCube
)) : NULL; | 198 return (NULL != colorCube) ? SkNEW_ARGS(GrColorCubeEffect, (colorCube))
: NULL; |
199 } | 199 } |
200 | 200 |
201 virtual ~GrColorProfileEffect(); | 201 virtual ~GrColorCubeEffect(); |
202 | 202 |
203 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERR
IDE; | 203 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERR
IDE; |
204 int colorCubeSize() const { return fColorCubeAccess.getTexture()->width(); } | 204 int colorCubeSize() const { return fColorCubeAccess.getTexture()->width(); } |
205 | 205 |
206 static const char* Name() { return "ColorProfile"; } | 206 static const char* Name() { return "ColorCube"; } |
207 | 207 |
208 virtual void onComputeInvariantOutput(GrProcessor::InvariantOutput*) const S
K_OVERRIDE; | 208 virtual void onComputeInvariantOutput(GrProcessor::InvariantOutput*) const S
K_OVERRIDE; |
209 | 209 |
210 class GLProcessor : public GrGLFragmentProcessor { | 210 class GLProcessor : public GrGLFragmentProcessor { |
211 public: | 211 public: |
212 GLProcessor(const GrBackendProcessorFactory& factory, const GrProcessor&
); | 212 GLProcessor(const GrBackendProcessorFactory& factory, const GrProcessor&
); |
213 virtual ~GLProcessor(); | 213 virtual ~GLProcessor(); |
214 | 214 |
215 virtual void emitCode(GrGLFPBuilder*, | 215 virtual void emitCode(GrGLFPBuilder*, |
216 const GrFragmentProcessor&, | 216 const GrFragmentProcessor&, |
(...skipping 10 matching lines...) Expand all Loading... |
227 private: | 227 private: |
228 GrGLProgramDataManager::UniformHandle fColorCubeSizeUni; | 228 GrGLProgramDataManager::UniformHandle fColorCubeSizeUni; |
229 GrGLProgramDataManager::UniformHandle fColorCubeInvSizeUni; | 229 GrGLProgramDataManager::UniformHandle fColorCubeInvSizeUni; |
230 | 230 |
231 typedef GrGLFragmentProcessor INHERITED; | 231 typedef GrGLFragmentProcessor INHERITED; |
232 }; | 232 }; |
233 | 233 |
234 private: | 234 private: |
235 virtual bool onIsEqual(const GrProcessor&) const SK_OVERRIDE; | 235 virtual bool onIsEqual(const GrProcessor&) const SK_OVERRIDE; |
236 | 236 |
237 GrColorProfileEffect(GrTexture* colorCube); | 237 GrColorCubeEffect(GrTexture* colorCube); |
238 | 238 |
239 GrCoordTransform fColorCubeTransform; | 239 GrCoordTransform fColorCubeTransform; |
240 GrTextureAccess fColorCubeAccess; | 240 GrTextureAccess fColorCubeAccess; |
241 | 241 |
242 typedef GrFragmentProcessor INHERITED; | 242 typedef GrFragmentProcessor INHERITED; |
243 }; | 243 }; |
244 | 244 |
245 /////////////////////////////////////////////////////////////////////////////// | 245 /////////////////////////////////////////////////////////////////////////////// |
246 | 246 |
247 GrColorProfileEffect::GrColorProfileEffect(GrTexture* colorCube) | 247 GrColorCubeEffect::GrColorCubeEffect(GrTexture* colorCube) |
248 : fColorCubeTransform(kLocal_GrCoordSet, colorCube) | 248 : fColorCubeTransform(kLocal_GrCoordSet, colorCube) |
249 , fColorCubeAccess(colorCube, "bgra", GrTextureParams::kBilerp_FilterMode) { | 249 , fColorCubeAccess(colorCube, "bgra", GrTextureParams::kBilerp_FilterMode) { |
250 this->addCoordTransform(&fColorCubeTransform); | 250 this->addCoordTransform(&fColorCubeTransform); |
251 this->addTextureAccess(&fColorCubeAccess); | 251 this->addTextureAccess(&fColorCubeAccess); |
252 } | 252 } |
253 | 253 |
254 GrColorProfileEffect::~GrColorProfileEffect() { | 254 GrColorCubeEffect::~GrColorCubeEffect() { |
255 } | 255 } |
256 | 256 |
257 bool GrColorProfileEffect::onIsEqual(const GrProcessor& sBase) const { | 257 bool GrColorCubeEffect::onIsEqual(const GrProcessor& sBase) const { |
258 const GrColorProfileEffect& s = sBase.cast<GrColorProfileEffect>(); | 258 const GrColorCubeEffect& s = sBase.cast<GrColorCubeEffect>(); |
259 return fColorCubeAccess.getTexture() == s.fColorCubeAccess.getTexture(); | 259 return fColorCubeAccess.getTexture() == s.fColorCubeAccess.getTexture(); |
260 } | 260 } |
261 | 261 |
262 const GrBackendFragmentProcessorFactory& GrColorProfileEffect::getFactory() cons
t { | 262 const GrBackendFragmentProcessorFactory& GrColorCubeEffect::getFactory() const { |
263 return GrTBackendFragmentProcessorFactory<GrColorProfileEffect>::getInstance
(); | 263 return GrTBackendFragmentProcessorFactory<GrColorCubeEffect>::getInstance(); |
264 } | 264 } |
265 | 265 |
266 void GrColorProfileEffect::onComputeInvariantOutput(InvariantOutput* inout) cons
t { | 266 void GrColorCubeEffect::onComputeInvariantOutput(InvariantOutput* inout) const { |
267 inout->setToUnknown(); | 267 inout->setToUnknown(); |
268 } | 268 } |
269 | 269 |
270 /////////////////////////////////////////////////////////////////////////////// | 270 /////////////////////////////////////////////////////////////////////////////// |
271 | 271 |
272 GrColorProfileEffect::GLProcessor::GLProcessor(const GrBackendProcessorFactory&
factory, | 272 GrColorCubeEffect::GLProcessor::GLProcessor(const GrBackendProcessorFactory& fac
tory, |
273 const GrProcessor&) | 273 const GrProcessor&) |
274 : INHERITED(factory) { | 274 : INHERITED(factory) { |
275 } | 275 } |
276 | 276 |
277 GrColorProfileEffect::GLProcessor::~GLProcessor() { | 277 GrColorCubeEffect::GLProcessor::~GLProcessor() { |
278 } | 278 } |
279 | 279 |
280 void GrColorProfileEffect::GLProcessor::emitCode(GrGLFPBuilder* builder, | 280 void GrColorCubeEffect::GLProcessor::emitCode(GrGLFPBuilder* builder, |
281 const GrFragmentProcessor&, | 281 const GrFragmentProcessor&, |
282 const GrProcessorKey&, | 282 const GrProcessorKey&, |
283 const char* outputColor, | 283 const char* outputColor, |
284 const char* inputColor, | 284 const char* inputColor, |
285 const TransformedCoordsArray& c
oords, | 285 const TransformedCoordsArray& coor
ds, |
286 const TextureSamplerArray& samp
lers) { | 286 const TextureSamplerArray& sampler
s) { |
287 if (NULL == inputColor) { | 287 if (NULL == inputColor) { |
288 inputColor = "vec4(1)"; | 288 inputColor = "vec4(1)"; |
289 } | 289 } |
290 | 290 |
291 fColorCubeSizeUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibi
lity, | 291 fColorCubeSizeUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibi
lity, |
292 kFloat_GrSLType, "Size"); | 292 kFloat_GrSLType, "Size"); |
293 const char* colorCubeSizeUni = builder->getUniformCStr(fColorCubeSizeUni); | 293 const char* colorCubeSizeUni = builder->getUniformCStr(fColorCubeSizeUni); |
294 fColorCubeInvSizeUni = builder->addUniform(GrGLProgramBuilder::kFragment_Vis
ibility, | 294 fColorCubeInvSizeUni = builder->addUniform(GrGLProgramBuilder::kFragment_Vis
ibility, |
295 kFloat_GrSLType, "InvSize"); | 295 kFloat_GrSLType, "InvSize"); |
296 const char* colorCubeInvSizeUni = builder->getUniformCStr(fColorCubeInvSizeU
ni); | 296 const char* colorCubeInvSizeUni = builder->getUniformCStr(fColorCubeInvSizeU
ni); |
(...skipping 30 matching lines...) Expand all Loading... |
327 fsBuilder->codeAppendf("%s = vec4(mix(", outputColor); | 327 fsBuilder->codeAppendf("%s = vec4(mix(", outputColor); |
328 fsBuilder->appendTextureLookup(samplers[0], cCoords1, coords[0].getType()); | 328 fsBuilder->appendTextureLookup(samplers[0], cCoords1, coords[0].getType()); |
329 fsBuilder->codeAppend(".rgb, "); | 329 fsBuilder->codeAppend(".rgb, "); |
330 fsBuilder->appendTextureLookup(samplers[0], cCoords2, coords[0].getType()); | 330 fsBuilder->appendTextureLookup(samplers[0], cCoords2, coords[0].getType()); |
331 | 331 |
332 // Premultiply color by alpha. Note that the input alpha is not modified by
this shader. | 332 // Premultiply color by alpha. Note that the input alpha is not modified by
this shader. |
333 fsBuilder->codeAppendf(".rgb, fract(%s.b)) * vec3(%s), %s.a);\n", | 333 fsBuilder->codeAppendf(".rgb, fract(%s.b)) * vec3(%s), %s.a);\n", |
334 cubeIdx, nonZeroAlpha, inputColor); | 334 cubeIdx, nonZeroAlpha, inputColor); |
335 } | 335 } |
336 | 336 |
337 void GrColorProfileEffect::GLProcessor::setData(const GrGLProgramDataManager& pd
man, | 337 void GrColorCubeEffect::GLProcessor::setData(const GrGLProgramDataManager& pdman
, |
338 const GrProcessor& proc) { | 338 const GrProcessor& proc) { |
339 const GrColorProfileEffect& colorProfile = proc.cast<GrColorProfileEffect>()
; | 339 const GrColorCubeEffect& colorCube = proc.cast<GrColorCubeEffect>(); |
340 SkScalar size = SkIntToScalar(colorProfile.colorCubeSize()); | 340 SkScalar size = SkIntToScalar(colorCube.colorCubeSize()); |
341 pdman.set1f(fColorCubeSizeUni, SkScalarToFloat(size)); | 341 pdman.set1f(fColorCubeSizeUni, SkScalarToFloat(size)); |
342 pdman.set1f(fColorCubeInvSizeUni, SkScalarToFloat(SkScalarInvert(size))); | 342 pdman.set1f(fColorCubeInvSizeUni, SkScalarToFloat(SkScalarInvert(size))); |
343 } | 343 } |
344 | 344 |
345 void GrColorProfileEffect::GLProcessor::GenKey(const GrProcessor& proc, | 345 void GrColorCubeEffect::GLProcessor::GenKey(const GrProcessor& proc, |
346 const GrGLCaps&, GrProcessorKeyBu
ilder* b) { | 346 const GrGLCaps&, GrProcessorKeyBuild
er* b) { |
347 b->add32(1); // Always same shader for now | 347 b->add32(1); // Always same shader for now |
348 } | 348 } |
349 | 349 |
350 GrFragmentProcessor* SkColorCubeFilter::asFragmentProcessor(GrContext* context)
const { | 350 GrFragmentProcessor* SkColorCubeFilter::asFragmentProcessor(GrContext* context)
const { |
351 static const GrCacheID::Domain gCubeDomain = GrCacheID::GenerateDomain(); | 351 static const GrCacheID::Domain gCubeDomain = GrCacheID::GenerateDomain(); |
352 | 352 |
353 GrCacheID::Key key; | 353 GrCacheID::Key key; |
354 key.fData32[0] = fUniqueID; | 354 key.fData32[0] = fUniqueID; |
355 key.fData32[1] = fCache.cubeDimension(); | 355 key.fData32[1] = fCache.cubeDimension(); |
356 key.fData64[1] = 0; | 356 key.fData64[1] = 0; |
357 GrCacheID cacheID(gCubeDomain, key); | 357 GrCacheID cacheID(gCubeDomain, key); |
358 | 358 |
359 GrTextureDesc desc; | 359 GrTextureDesc desc; |
360 desc.fWidth = fCache.cubeDimension(); | 360 desc.fWidth = fCache.cubeDimension(); |
361 desc.fHeight = fCache.cubeDimension() * fCache.cubeDimension(); | 361 desc.fHeight = fCache.cubeDimension() * fCache.cubeDimension(); |
362 desc.fConfig = kRGBA_8888_GrPixelConfig; | 362 desc.fConfig = kRGBA_8888_GrPixelConfig; |
363 | 363 |
364 SkAutoTUnref<GrTexture> textureCube( | 364 SkAutoTUnref<GrTexture> textureCube( |
365 static_cast<GrTexture*>(context->findAndRefCachedResource( | 365 static_cast<GrTexture*>(context->findAndRefCachedResource( |
366 GrTexturePriv::ComputeKey(context->getGpu(), NULL, desc, cacheID))))
; | 366 GrTexturePriv::ComputeKey(context->getGpu(), NULL, desc, cacheID))))
; |
367 | 367 |
368 if (!textureCube) { | 368 if (!textureCube) { |
369 textureCube.reset(context->createTexture(NULL, desc, cacheID, fCubeData-
>data(), 0)); | 369 textureCube.reset(context->createTexture(NULL, desc, cacheID, fCubeData-
>data(), 0)); |
370 } | 370 } |
371 | 371 |
372 return textureCube ? GrColorProfileEffect::Create(textureCube) : NULL; | 372 return textureCube ? GrColorCubeEffect::Create(textureCube) : NULL; |
373 } | 373 } |
374 #endif | 374 #endif |
OLD | NEW |