OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2014 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "SkShader.h" |
| 9 #include "SkReadBuffer.h" |
| 10 #include "SkWriteBuffer.h" |
| 11 |
| 12 class SkLocalMatrixShader : public SkShader { |
| 13 public: |
| 14 SkLocalMatrixShader(SkShader* proxy, const SkMatrix& localMatrix) |
| 15 : fProxyShader(SkRef(proxy)) |
| 16 , fProxyLocalMatrix(localMatrix) |
| 17 {} |
| 18 |
| 19 virtual size_t contextSize() const SK_OVERRIDE { |
| 20 return fProxyShader->contextSize(); |
| 21 } |
| 22 |
| 23 virtual BitmapType asABitmap(SkBitmap* bitmap, SkMatrix* matrix, |
| 24 TileMode* mode) const SK_OVERRIDE { |
| 25 return fProxyShader->asABitmap(bitmap, matrix, mode); |
| 26 } |
| 27 |
| 28 virtual GradientType asAGradient(GradientInfo* info) const SK_OVERRIDE { |
| 29 return fProxyShader->asAGradient(info); |
| 30 } |
| 31 |
| 32 // TODO: need to augment this API to pass in a localmatrix (which we can aug
ment) |
| 33 virtual GrEffectRef* asNewEffect(GrContext* ctx, const SkPaint& paint) const
SK_OVERRIDE { |
| 34 return fProxyShader->asNewEffect(ctx, paint); |
| 35 } |
| 36 |
| 37 virtual SkShader* refAsALocalMatrixShader(SkMatrix* localMatrix) const SK_OV
ERRIDE { |
| 38 if (localMatrix) { |
| 39 *localMatrix = fProxyLocalMatrix; |
| 40 } |
| 41 return SkRef(fProxyShader.get()); |
| 42 } |
| 43 |
| 44 SK_TO_STRING_OVERRIDE() |
| 45 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLocalMatrixShader) |
| 46 |
| 47 protected: |
| 48 SkLocalMatrixShader(SkReadBuffer&); |
| 49 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; |
| 50 virtual Context* onCreateContext(const ContextRec&, void*) const SK_OVERRIDE
; |
| 51 |
| 52 private: |
| 53 SkAutoTUnref<SkShader> fProxyShader; |
| 54 SkMatrix fProxyLocalMatrix; |
| 55 |
| 56 typedef SkShader INHERITED; |
| 57 }; |
| 58 |
| 59 SkLocalMatrixShader::SkLocalMatrixShader(SkReadBuffer& buffer) : INHERITED(buffe
r) { |
| 60 buffer.readMatrix(&fProxyLocalMatrix); |
| 61 fProxyShader.reset(buffer.readFlattenable<SkShader>()); |
| 62 if (NULL == fProxyShader.get()) { |
| 63 sk_throw(); |
| 64 } |
| 65 } |
| 66 |
| 67 void SkLocalMatrixShader::flatten(SkWriteBuffer& buffer) const { |
| 68 this->INHERITED::flatten(buffer); |
| 69 buffer.writeMatrix(fProxyLocalMatrix); |
| 70 buffer.writeFlattenable(fProxyShader.get()); |
| 71 } |
| 72 |
| 73 SkShader::Context* SkLocalMatrixShader::onCreateContext(const ContextRec& rec, |
| 74 void* storage) const { |
| 75 ContextRec newRec(rec); |
| 76 SkMatrix tmp; |
| 77 if (rec.fLocalMatrix) { |
| 78 tmp.setConcat(fProxyLocalMatrix, *rec.fLocalMatrix); |
| 79 newRec.fLocalMatrix = &tmp; |
| 80 } else { |
| 81 newRec.fLocalMatrix = &fProxyLocalMatrix; |
| 82 } |
| 83 return fProxyShader->createContext(newRec, storage); |
| 84 } |
| 85 |
| 86 #ifndef SK_IGNORE_TO_STRING |
| 87 void SkLocalMatrixShader::toString(SkString* str) const { |
| 88 str->append("SkLocalMatrixShader: ("); |
| 89 |
| 90 fProxyShader->toString(str); |
| 91 |
| 92 this->INHERITED::toString(str); |
| 93 |
| 94 str->append(")"); |
| 95 } |
| 96 #endif |
| 97 |
| 98 SkShader* SkShader::CreateLocalMatrixShader(SkShader* proxy, const SkMatrix& loc
alMatrix) { |
| 99 if (localMatrix.isIdentity()) { |
| 100 return SkRef(proxy); |
| 101 } |
| 102 |
| 103 const SkMatrix* lm = &localMatrix; |
| 104 |
| 105 SkMatrix otherLocalMatrix; |
| 106 SkAutoTUnref<SkShader> otherProxy(proxy->refAsALocalMatrixShader(&otherLocal
Matrix)); |
| 107 if (otherProxy.get()) { |
| 108 otherLocalMatrix.preConcat(localMatrix); |
| 109 lm = &otherLocalMatrix; |
| 110 proxy = otherProxy.get(); |
| 111 } |
| 112 |
| 113 return SkNEW_ARGS(SkLocalMatrixShader, (proxy, *lm)); |
| 114 } |
| 115 |
OLD | NEW |