Line data Source code
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 "SkLocalMatrixShader.h"
9 :
10 : #if SK_SUPPORT_GPU
11 : #include "GrFragmentProcessor.h"
12 : #endif
13 :
14 : #if SK_SUPPORT_GPU
15 0 : sk_sp<GrFragmentProcessor> SkLocalMatrixShader::asFragmentProcessor(const AsFPArgs& args) const {
16 0 : SkMatrix tmp = this->getLocalMatrix();
17 0 : if (args.fLocalMatrix) {
18 0 : tmp.preConcat(*args.fLocalMatrix);
19 : }
20 0 : return fProxyShader->asFragmentProcessor(AsFPArgs(
21 0 : args.fContext, args.fViewMatrix, &tmp, args.fFilterQuality, args.fDstColorSpace));
22 : }
23 : #endif
24 :
25 0 : sk_sp<SkFlattenable> SkLocalMatrixShader::CreateProc(SkReadBuffer& buffer) {
26 : SkMatrix lm;
27 0 : buffer.readMatrix(&lm);
28 0 : auto baseShader(buffer.readShader());
29 0 : if (!baseShader) {
30 0 : return nullptr;
31 : }
32 0 : return baseShader->makeWithLocalMatrix(lm);
33 : }
34 :
35 0 : void SkLocalMatrixShader::flatten(SkWriteBuffer& buffer) const {
36 0 : buffer.writeMatrix(this->getLocalMatrix());
37 0 : buffer.writeFlattenable(fProxyShader.get());
38 0 : }
39 :
40 0 : SkShader::Context* SkLocalMatrixShader::onMakeContext(
41 : const ContextRec& rec, SkArenaAlloc* alloc) const
42 : {
43 0 : ContextRec newRec(rec);
44 : SkMatrix tmp;
45 0 : if (rec.fLocalMatrix) {
46 0 : tmp.setConcat(*rec.fLocalMatrix, this->getLocalMatrix());
47 0 : newRec.fLocalMatrix = &tmp;
48 : } else {
49 0 : newRec.fLocalMatrix = &this->getLocalMatrix();
50 : }
51 0 : return fProxyShader->makeContext(newRec, alloc);
52 : }
53 :
54 0 : SkImage* SkLocalMatrixShader::onIsAImage(SkMatrix* outMatrix, enum TileMode* mode) const {
55 : SkMatrix imageMatrix;
56 0 : SkImage* image = fProxyShader->isAImage(&imageMatrix, mode);
57 0 : if (image && outMatrix) {
58 : // Local matrix must be applied first so it is on the right side of the concat.
59 0 : *outMatrix = SkMatrix::Concat(imageMatrix, this->getLocalMatrix());
60 : }
61 :
62 0 : return image;
63 : }
64 :
65 0 : bool SkLocalMatrixShader::onAppendStages(SkRasterPipeline* p,
66 : SkColorSpace* dst,
67 : SkArenaAlloc* scratch,
68 : const SkMatrix& ctm,
69 : const SkPaint& paint,
70 : const SkMatrix* localM) const {
71 : SkMatrix tmp;
72 0 : if (localM) {
73 0 : tmp.setConcat(*localM, this->getLocalMatrix());
74 : }
75 :
76 0 : return fProxyShader->onAppendStages(p, dst, scratch, ctm, paint,
77 0 : localM ? &tmp : &this->getLocalMatrix());
78 : }
79 :
80 : #ifndef SK_IGNORE_TO_STRING
81 0 : void SkLocalMatrixShader::toString(SkString* str) const {
82 0 : str->append("SkLocalMatrixShader: (");
83 :
84 0 : fProxyShader->toString(str);
85 :
86 0 : this->INHERITED::toString(str);
87 :
88 0 : str->append(")");
89 0 : }
90 : #endif
91 :
92 0 : sk_sp<SkShader> SkShader::makeWithLocalMatrix(const SkMatrix& localMatrix) const {
93 0 : if (localMatrix.isIdentity()) {
94 0 : return sk_ref_sp(const_cast<SkShader*>(this));
95 : }
96 :
97 0 : const SkMatrix* lm = &localMatrix;
98 :
99 0 : sk_sp<SkShader> baseShader;
100 : SkMatrix otherLocalMatrix;
101 0 : sk_sp<SkShader> proxy(this->makeAsALocalMatrixShader(&otherLocalMatrix));
102 0 : if (proxy) {
103 0 : otherLocalMatrix.preConcat(localMatrix);
104 0 : lm = &otherLocalMatrix;
105 0 : baseShader = proxy;
106 : } else {
107 0 : baseShader = sk_ref_sp(const_cast<SkShader*>(this));
108 : }
109 :
110 0 : return sk_make_sp<SkLocalMatrixShader>(std::move(baseShader), *lm);
111 : }
|