1 | /* |
2 | * Copyright 2020 Google LLC |
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 | #ifndef SkMatrixProvider_DEFINED |
9 | #define SkMatrixProvider_DEFINED |
10 | |
11 | #include "include/core/SkM44.h" |
12 | #include "include/core/SkMatrix.h" |
13 | |
14 | class SkMatrixProvider { |
15 | public: |
16 | SkMatrixProvider(const SkMatrix& localToDevice) |
17 | : fLocalToDevice(localToDevice) |
18 | , fLocalToDevice33(localToDevice) {} |
19 | |
20 | SkMatrixProvider(const SkM44& localToDevice) |
21 | : fLocalToDevice(localToDevice) |
22 | , fLocalToDevice33(localToDevice.asM33()) {} |
23 | |
24 | virtual ~SkMatrixProvider() {} |
25 | |
26 | // These should return the "same" matrix, as either a 3x3 or 4x4. Most sites in Skia still |
27 | // call localToDevice, and operate on SkMatrix. |
28 | const SkMatrix& localToDevice() const { return fLocalToDevice33; } |
29 | const SkM44& localToDevice44() const { return fLocalToDevice; } |
30 | |
31 | virtual bool getLocalToMarker(uint32_t id, SkM44* localToMarker) const = 0; |
32 | |
33 | private: |
34 | friend class SkBaseDevice; |
35 | |
36 | SkM44 fLocalToDevice; |
37 | SkMatrix fLocalToDevice33; // Cached SkMatrix version of above, for legacy usage |
38 | }; |
39 | |
40 | class SkOverrideDeviceMatrixProvider : public SkMatrixProvider { |
41 | public: |
42 | SkOverrideDeviceMatrixProvider(const SkMatrixProvider& parent, const SkMatrix& localToDevice) |
43 | : SkMatrixProvider(localToDevice) |
44 | , fParent(parent) {} |
45 | |
46 | bool getLocalToMarker(uint32_t id, SkM44* localToMarker) const override { |
47 | return fParent.getLocalToMarker(id, localToMarker); |
48 | } |
49 | |
50 | private: |
51 | const SkMatrixProvider& fParent; |
52 | }; |
53 | |
54 | class SkPostTranslateMatrixProvider : public SkMatrixProvider { |
55 | public: |
56 | SkPostTranslateMatrixProvider(const SkMatrixProvider& parent, SkScalar dx, SkScalar dy) |
57 | : SkMatrixProvider(SkM44::Translate(dx, dy) * parent.localToDevice44()) |
58 | , fParent(parent) {} |
59 | |
60 | // Assume that the post-translation doesn't apply to any marked matrices |
61 | bool getLocalToMarker(uint32_t id, SkM44* localToMarker) const override { |
62 | return fParent.getLocalToMarker(id, localToMarker); |
63 | } |
64 | |
65 | private: |
66 | const SkMatrixProvider& fParent; |
67 | }; |
68 | |
69 | class SkPreConcatMatrixProvider : public SkMatrixProvider { |
70 | public: |
71 | SkPreConcatMatrixProvider(const SkMatrixProvider& parent, const SkMatrix& preMatrix) |
72 | : SkMatrixProvider(parent.localToDevice44() * SkM44(preMatrix)) |
73 | , fParent(parent) |
74 | , fPreMatrix(preMatrix) {} |
75 | |
76 | bool getLocalToMarker(uint32_t id, SkM44* localToMarker) const override { |
77 | if (fParent.getLocalToMarker(id, localToMarker)) { |
78 | if (localToMarker) { |
79 | localToMarker->preConcat(fPreMatrix); |
80 | } |
81 | return true; |
82 | } |
83 | return false; |
84 | } |
85 | |
86 | private: |
87 | const SkMatrixProvider& fParent; |
88 | const SkMatrix fPreMatrix; |
89 | }; |
90 | |
91 | class SkSimpleMatrixProvider : public SkMatrixProvider { |
92 | public: |
93 | SkSimpleMatrixProvider(const SkMatrix& localToDevice) |
94 | : SkMatrixProvider(localToDevice) {} |
95 | |
96 | bool getLocalToMarker(uint32_t, SkM44*) const override { return false; } |
97 | }; |
98 | |
99 | #endif |
100 | |