Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
| 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 "SkEmptyShader.h" | 9 #include "SkEmptyShader.h" |
| 10 #include "SkReadBuffer.h" | 10 #include "SkReadBuffer.h" |
| 11 #include "SkMallocPixelRef.h" | 11 #include "SkMallocPixelRef.h" |
| 12 #include "SkPaint.h" | 12 #include "SkPaint.h" |
| 13 #include "SkPicture.h" | 13 #include "SkPicture.h" |
| 14 #include "SkPictureShader.h" | 14 #include "SkPictureShader.h" |
| 15 #include "SkScalar.h" | 15 #include "SkScalar.h" |
| 16 #include "SkShader.h" | 16 #include "SkShader.h" |
| 17 #include "SkThread.h" | |
| 17 #include "SkWriteBuffer.h" | 18 #include "SkWriteBuffer.h" |
| 18 | 19 |
| 20 //#define SK_TRACK_SHADER_LIFETIME | |
| 21 | |
| 22 #ifdef SK_TRACK_SHADER_LIFETIME | |
|
scroggo
2014/05/07 22:01:15
It might be nice if this was part of SkInstCount.
| |
| 23 static int32_t gShaderCounter; | |
|
scroggo
2014/05/07 22:01:15
Does this need to be indented?
reed1
2014/05/07 22:05:05
We seem to do both practices. in SkTypes.h we inde
| |
| 24 #endif | |
| 25 | |
| 26 static inline void inc_shader_counter() { | |
| 27 #ifdef SK_TRACK_SHADER_LIFETIME | |
| 28 int32_t prev = sk_atomic_inc(&gShaderCounter); | |
| 29 SkDebugf("+++ shader counter %d\n", prev + 1); | |
| 30 #endif | |
| 31 } | |
| 32 static inline void dec_shader_counter() { | |
| 33 #ifdef SK_TRACK_SHADER_LIFETIME | |
| 34 int32_t prev = sk_atomic_dec(&gShaderCounter); | |
| 35 SkDebugf("--- shader counter %d\n", prev - 1); | |
| 36 #endif | |
| 37 } | |
| 38 | |
| 19 SkShader::SkShader(const SkMatrix* localMatrix) { | 39 SkShader::SkShader(const SkMatrix* localMatrix) { |
| 40 inc_shader_counter(); | |
| 20 if (localMatrix) { | 41 if (localMatrix) { |
| 21 fLocalMatrix = *localMatrix; | 42 fLocalMatrix = *localMatrix; |
| 22 } else { | 43 } else { |
| 23 fLocalMatrix.reset(); | 44 fLocalMatrix.reset(); |
| 24 } | 45 } |
| 25 } | 46 } |
| 26 | 47 |
| 27 SkShader::SkShader(SkReadBuffer& buffer) | 48 SkShader::SkShader(SkReadBuffer& buffer) : INHERITED(buffer) { |
| 28 : INHERITED(buffer) { | 49 inc_shader_counter(); |
| 29 if (buffer.readBool()) { | 50 if (buffer.readBool()) { |
| 30 buffer.readMatrix(&fLocalMatrix); | 51 buffer.readMatrix(&fLocalMatrix); |
| 31 } else { | 52 } else { |
| 32 fLocalMatrix.reset(); | 53 fLocalMatrix.reset(); |
| 33 } | 54 } |
| 34 } | 55 } |
| 35 | 56 |
| 36 SkShader::~SkShader() { | 57 SkShader::~SkShader() { |
| 58 dec_shader_counter(); | |
| 37 } | 59 } |
| 38 | 60 |
| 39 void SkShader::flatten(SkWriteBuffer& buffer) const { | 61 void SkShader::flatten(SkWriteBuffer& buffer) const { |
| 40 this->INHERITED::flatten(buffer); | 62 this->INHERITED::flatten(buffer); |
| 41 bool hasLocalM = this->hasLocalMatrix(); | 63 bool hasLocalM = this->hasLocalMatrix(); |
| 42 buffer.writeBool(hasLocalM); | 64 buffer.writeBool(hasLocalM); |
| 43 if (hasLocalM) { | 65 if (hasLocalM) { |
| 44 buffer.writeMatrix(fLocalMatrix); | 66 buffer.writeMatrix(fLocalMatrix); |
| 45 } | 67 } |
| 46 } | 68 } |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 68 } | 90 } |
| 69 | 91 |
| 70 SkShader::Context* SkShader::onCreateContext(const ContextRec& rec, void*) const { | 92 SkShader::Context* SkShader::onCreateContext(const ContextRec& rec, void*) const { |
| 71 return NULL; | 93 return NULL; |
| 72 } | 94 } |
| 73 | 95 |
| 74 size_t SkShader::contextSize() const { | 96 size_t SkShader::contextSize() const { |
| 75 return 0; | 97 return 0; |
| 76 } | 98 } |
| 77 | 99 |
| 100 SkShader* SkShader::refAsALocalMatrixShader(SkMatrix*) const { | |
| 101 return false; | |
| 102 } | |
| 103 | |
| 78 SkShader::Context::Context(const SkShader& shader, const ContextRec& rec) | 104 SkShader::Context::Context(const SkShader& shader, const ContextRec& rec) |
| 79 : fShader(shader), fCTM(*rec.fMatrix) | 105 : fShader(shader), fCTM(*rec.fMatrix) |
| 80 { | 106 { |
| 81 // Because the context parameters must be valid at this point, we know that the matrix is | 107 // Because the context parameters must be valid at this point, we know that the matrix is |
| 82 // invertible. | 108 // invertible. |
| 83 SkAssertResult(fShader.computeTotalInverse(rec, &fTotalInverse)); | 109 SkAssertResult(fShader.computeTotalInverse(rec, &fTotalInverse)); |
| 84 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse); | 110 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse); |
| 85 | 111 |
| 86 fPaintAlpha = rec.fPaint->getAlpha(); | 112 fPaintAlpha = rec.fPaint->getAlpha(); |
| 87 } | 113 } |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 337 #include "SkEmptyShader.h" | 363 #include "SkEmptyShader.h" |
| 338 | 364 |
| 339 void SkEmptyShader::toString(SkString* str) const { | 365 void SkEmptyShader::toString(SkString* str) const { |
| 340 str->append("SkEmptyShader: ("); | 366 str->append("SkEmptyShader: ("); |
| 341 | 367 |
| 342 this->INHERITED::toString(str); | 368 this->INHERITED::toString(str); |
| 343 | 369 |
| 344 str->append(")"); | 370 str->append(")"); |
| 345 } | 371 } |
| 346 #endif | 372 #endif |
| 373 | |
| 374 //////////////////////////////////////////////////////////////////////////////// // | |
| 375 | |
| 376 class SkLocalMatrixShader : public SkShader { | |
| 377 public: | |
| 378 SkLocalMatrixShader(SkShader* proxy, const SkMatrix& localMatrix) | |
| 379 : fProxyShader(SkRef(proxy)) | |
| 380 , fProxyLocalMatrix(localMatrix) | |
| 381 {} | |
| 382 | |
| 383 virtual size_t contextSize() const SK_OVERRIDE { | |
| 384 return fProxyShader->contextSize(); | |
| 385 } | |
| 386 | |
| 387 virtual SkShader* refAsALocalMatrixShader(SkMatrix* localMatrix) const SK_OV ERRIDE { | |
| 388 if (localMatrix) { | |
| 389 *localMatrix = fProxyLocalMatrix; | |
| 390 } | |
| 391 return SkRef(fProxyShader.get()); | |
| 392 } | |
| 393 | |
| 394 SK_TO_STRING_OVERRIDE() | |
| 395 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLocalMatrixShader) | |
| 396 | |
| 397 protected: | |
| 398 SkLocalMatrixShader(SkReadBuffer&); | |
| 399 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; | |
| 400 virtual Context* onCreateContext(const ContextRec&, void*) const SK_OVERRIDE ; | |
| 401 | |
| 402 private: | |
| 403 SkAutoTUnref<SkShader> fProxyShader; | |
| 404 SkMatrix fProxyLocalMatrix; | |
| 405 | |
| 406 typedef SkShader INHERITED; | |
| 407 }; | |
| 408 | |
| 409 SkLocalMatrixShader::SkLocalMatrixShader(SkReadBuffer& buffer) : INHERITED(buffe r) { | |
| 410 buffer.readMatrix(&fProxyLocalMatrix); | |
| 411 fProxyShader.reset(buffer.readFlattenable<SkShader>()); | |
| 412 if (NULL == fProxyShader.get()) { | |
| 413 sk_throw(); | |
| 414 } | |
| 415 } | |
| 416 | |
| 417 void SkLocalMatrixShader::flatten(SkWriteBuffer& buffer) const { | |
| 418 this->INHERITED::flatten(buffer); | |
| 419 buffer.writeMatrix(fProxyLocalMatrix); | |
| 420 buffer.writeFlattenable(fProxyShader.get()); | |
| 421 } | |
| 422 | |
| 423 SkShader::Context* SkLocalMatrixShader::onCreateContext(const ContextRec& rec, | |
| 424 void* storage) const { | |
| 425 ContextRec newRec(rec); | |
| 426 SkMatrix tmp; | |
| 427 if (rec.fLocalMatrix) { | |
| 428 tmp.setConcat(*rec.fLocalMatrix, fProxyLocalMatrix); | |
| 429 newRec.fLocalMatrix = &tmp; | |
| 430 } else { | |
| 431 newRec.fLocalMatrix = &fProxyLocalMatrix; | |
| 432 } | |
| 433 return fProxyShader->createContext(newRec, storage); | |
| 434 } | |
| 435 | |
| 436 #ifndef SK_IGNORE_TO_STRING | |
| 437 void SkLocalMatrixShader::toString(SkString* str) const { | |
| 438 str->append("SkLocalMatrixShader: ("); | |
| 439 | |
| 440 fProxyShader->toString(str); | |
| 441 | |
| 442 this->INHERITED::toString(str); | |
| 443 | |
| 444 str->append(")"); | |
| 445 } | |
| 446 #endif | |
| 447 | |
| 448 SkShader* SkShader::CreateLocalMatrixShader(SkShader* proxy, const SkMatrix& loc alMatrix) { | |
| 449 if (localMatrix.isIdentity()) { | |
| 450 return SkRef(proxy); | |
| 451 } | |
| 452 | |
| 453 const SkMatrix* lm = &localMatrix; | |
| 454 | |
| 455 SkMatrix otherLocalMatrix; | |
| 456 SkAutoTUnref<SkShader> otherProxy(proxy->refAsALocalMatrixShader(&otherLocal Matrix)); | |
| 457 if (otherProxy.get()) { | |
| 458 otherLocalMatrix.preConcat(localMatrix); | |
| 459 lm = &otherLocalMatrix; | |
| 460 proxy = otherProxy.get(); | |
| 461 } | |
| 462 | |
| 463 return SkNEW_ARGS(SkLocalMatrixShader, (proxy, *lm)); | |
| 464 } | |
| 465 | |
| OLD | NEW |