Index: src/core/SkShader.cpp |
diff --git a/src/core/SkShader.cpp b/src/core/SkShader.cpp |
index 0c954f869b359522263c77804b9fe25e74159da9..2dfbe615dcb078d6f1edf2f7bef1449425bafd75 100644 |
--- a/src/core/SkShader.cpp |
+++ b/src/core/SkShader.cpp |
@@ -14,9 +14,30 @@ |
#include "SkPictureShader.h" |
#include "SkScalar.h" |
#include "SkShader.h" |
+#include "SkThread.h" |
#include "SkWriteBuffer.h" |
+//#define SK_TRACK_SHADER_LIFETIME |
+ |
+#ifdef SK_TRACK_SHADER_LIFETIME |
scroggo
2014/05/07 22:01:15
It might be nice if this was part of SkInstCount.
|
+ 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
|
+#endif |
+ |
+static inline void inc_shader_counter() { |
+#ifdef SK_TRACK_SHADER_LIFETIME |
+ int32_t prev = sk_atomic_inc(&gShaderCounter); |
+ SkDebugf("+++ shader counter %d\n", prev + 1); |
+#endif |
+} |
+static inline void dec_shader_counter() { |
+#ifdef SK_TRACK_SHADER_LIFETIME |
+ int32_t prev = sk_atomic_dec(&gShaderCounter); |
+ SkDebugf("--- shader counter %d\n", prev - 1); |
+#endif |
+} |
+ |
SkShader::SkShader(const SkMatrix* localMatrix) { |
+ inc_shader_counter(); |
if (localMatrix) { |
fLocalMatrix = *localMatrix; |
} else { |
@@ -24,8 +45,8 @@ SkShader::SkShader(const SkMatrix* localMatrix) { |
} |
} |
-SkShader::SkShader(SkReadBuffer& buffer) |
- : INHERITED(buffer) { |
+SkShader::SkShader(SkReadBuffer& buffer) : INHERITED(buffer) { |
+ inc_shader_counter(); |
if (buffer.readBool()) { |
buffer.readMatrix(&fLocalMatrix); |
} else { |
@@ -34,6 +55,7 @@ SkShader::SkShader(SkReadBuffer& buffer) |
} |
SkShader::~SkShader() { |
+ dec_shader_counter(); |
} |
void SkShader::flatten(SkWriteBuffer& buffer) const { |
@@ -75,6 +97,10 @@ size_t SkShader::contextSize() const { |
return 0; |
} |
+SkShader* SkShader::refAsALocalMatrixShader(SkMatrix*) const { |
+ return false; |
+} |
+ |
SkShader::Context::Context(const SkShader& shader, const ContextRec& rec) |
: fShader(shader), fCTM(*rec.fMatrix) |
{ |
@@ -344,3 +370,96 @@ void SkEmptyShader::toString(SkString* str) const { |
str->append(")"); |
} |
#endif |
+ |
+////////////////////////////////////////////////////////////////////////////////// |
+ |
+class SkLocalMatrixShader : public SkShader { |
+public: |
+ SkLocalMatrixShader(SkShader* proxy, const SkMatrix& localMatrix) |
+ : fProxyShader(SkRef(proxy)) |
+ , fProxyLocalMatrix(localMatrix) |
+ {} |
+ |
+ virtual size_t contextSize() const SK_OVERRIDE { |
+ return fProxyShader->contextSize(); |
+ } |
+ |
+ virtual SkShader* refAsALocalMatrixShader(SkMatrix* localMatrix) const SK_OVERRIDE { |
+ if (localMatrix) { |
+ *localMatrix = fProxyLocalMatrix; |
+ } |
+ return SkRef(fProxyShader.get()); |
+ } |
+ |
+ SK_TO_STRING_OVERRIDE() |
+ SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLocalMatrixShader) |
+ |
+protected: |
+ SkLocalMatrixShader(SkReadBuffer&); |
+ virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; |
+ virtual Context* onCreateContext(const ContextRec&, void*) const SK_OVERRIDE; |
+ |
+private: |
+ SkAutoTUnref<SkShader> fProxyShader; |
+ SkMatrix fProxyLocalMatrix; |
+ |
+ typedef SkShader INHERITED; |
+}; |
+ |
+SkLocalMatrixShader::SkLocalMatrixShader(SkReadBuffer& buffer) : INHERITED(buffer) { |
+ buffer.readMatrix(&fProxyLocalMatrix); |
+ fProxyShader.reset(buffer.readFlattenable<SkShader>()); |
+ if (NULL == fProxyShader.get()) { |
+ sk_throw(); |
+ } |
+} |
+ |
+void SkLocalMatrixShader::flatten(SkWriteBuffer& buffer) const { |
+ this->INHERITED::flatten(buffer); |
+ buffer.writeMatrix(fProxyLocalMatrix); |
+ buffer.writeFlattenable(fProxyShader.get()); |
+} |
+ |
+SkShader::Context* SkLocalMatrixShader::onCreateContext(const ContextRec& rec, |
+ void* storage) const { |
+ ContextRec newRec(rec); |
+ SkMatrix tmp; |
+ if (rec.fLocalMatrix) { |
+ tmp.setConcat(*rec.fLocalMatrix, fProxyLocalMatrix); |
+ newRec.fLocalMatrix = &tmp; |
+ } else { |
+ newRec.fLocalMatrix = &fProxyLocalMatrix; |
+ } |
+ return fProxyShader->createContext(newRec, storage); |
+} |
+ |
+#ifndef SK_IGNORE_TO_STRING |
+void SkLocalMatrixShader::toString(SkString* str) const { |
+ str->append("SkLocalMatrixShader: ("); |
+ |
+ fProxyShader->toString(str); |
+ |
+ this->INHERITED::toString(str); |
+ |
+ str->append(")"); |
+} |
+#endif |
+ |
+SkShader* SkShader::CreateLocalMatrixShader(SkShader* proxy, const SkMatrix& localMatrix) { |
+ if (localMatrix.isIdentity()) { |
+ return SkRef(proxy); |
+ } |
+ |
+ const SkMatrix* lm = &localMatrix; |
+ |
+ SkMatrix otherLocalMatrix; |
+ SkAutoTUnref<SkShader> otherProxy(proxy->refAsALocalMatrixShader(&otherLocalMatrix)); |
+ if (otherProxy.get()) { |
+ otherLocalMatrix.preConcat(localMatrix); |
+ lm = &otherLocalMatrix; |
+ proxy = otherProxy.get(); |
+ } |
+ |
+ return SkNEW_ARGS(SkLocalMatrixShader, (proxy, *lm)); |
+} |
+ |