• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 
4 #ifndef Cmds_DEFINED
5 #define Cmds_DEFINED
6 
7 class SkBitmap;
8 class SkCanvas;
9 class FakeCanvas;
10 class FakeMCBlob;
11 class SortKey;
12 
13 #include "include/core/SkColor.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 
17 #include "experimental/sorttoy/Fake.h"
18 #include "experimental/sorttoy/sorttypes.h"
19 
20 //------------------------------------------------------------------------------------------------
21 class Cmd : public SkRefCnt {
22 public:
Cmd()23     Cmd() : fID(ID::Invalid()) {}
Cmd(ID id)24     Cmd(ID id) : fID(id) {}
~Cmd()25     ~Cmd() override {}
26 
id()27     ID id() const { return fID; }
28 
29     virtual SortKey getKey() = 0;
30 
31     // To generate the actual image
32     virtual void execute(FakeCanvas*) const = 0;
33     virtual void rasterize(uint32_t zBuffer[256][256], SkBitmap* dstBM) const = 0;
34 
35     // To generate the expected image
36     virtual void execute(SkCanvas*) const = 0;
37     virtual void dump() const = 0;
38 
39 protected:
40     const ID fID;
41 
42 private:
43 };
44 
45 //------------------------------------------------------------------------------------------------
46 // This Cmd only appears in the initial list defining a test case. It never makes it into the
47 // sorted Cmds.
48 class SaveCmd : public Cmd {
49 public:
SaveCmd()50     SaveCmd() : Cmd() {}
51 
52     SortKey getKey() override;
53 
54     void execute(FakeCanvas*) const override;
55     void execute(SkCanvas*) const override;
rasterize(uint32_t zBuffer[256][256],SkBitmap * dstBM)56     void rasterize(uint32_t zBuffer[256][256], SkBitmap* dstBM) const override {
57         SkASSERT(0);
58     }
59 
dump()60     void dump() const override {
61         SkDebugf("%d: save", fID.toInt());
62     }
63 
64 protected:
65 private:
66 };
67 
68 //------------------------------------------------------------------------------------------------
69 // This Cmd only appears in the initial list defining a test case. It never makes it into the
70 // sorted Cmds.
71 class RestoreCmd : public Cmd {
72 public:
RestoreCmd()73     RestoreCmd() : Cmd() {}
74 
75     SortKey getKey() override;
76 
77     void execute(FakeCanvas*) const override;
78     void execute(SkCanvas*) const override;
rasterize(uint32_t zBuffer[256][256],SkBitmap * dstBM)79     void rasterize(uint32_t zBuffer[256][256], SkBitmap* dstBM) const override {
80         SkASSERT(0);
81     }
82 
dump()83     void dump() const override {
84         SkDebugf("%d: restore", fID.toInt());
85     }
86 
87 protected:
88 private:
89 };
90 
91 //------------------------------------------------------------------------------------------------
92 class DrawCmd : public Cmd {
93 public:
94     DrawCmd(ID, Shape, SkIRect, const FakePaint&);  // for creating the test cases
95     DrawCmd(ID, PaintersOrder, Shape, SkIRect, const FakePaint&, sk_sp<FakeMCBlob> state);
96 
97     bool contains(int x, int y) const;
98 
99     uint32_t getSortZ() const;
100     uint32_t getDrawZ() const;
101 
102     SortKey getKey() override;
state()103     const FakeMCBlob* state() const { return fMCState.get(); }
104 
105     void execute(FakeCanvas*) const override;
106     void execute(SkCanvas*) const override;
107     void rasterize(uint32_t zBuffer[256][256], SkBitmap* dstBM) const override;
108 
dump()109     void dump() const override {
110         SkDebugf("%d: draw%s %d %d %d %d -- %d",
111                  fID.toInt(),
112                  fShape == Shape::kRect ? "Rect" : "Oval",
113                  fRect.fLeft, fRect.fTop, fRect.fRight, fRect.fBottom,
114                  fPaintersOrder.toUInt());
115     }
116 
117 protected:
118 
119 private:
120     PaintersOrder     fPaintersOrder;
121     Shape             fShape;
122     SkIRect           fRect;
123     FakePaint         fPaint;
124     sk_sp<FakeMCBlob> fMCState;
125 };
126 
127 //------------------------------------------------------------------------------------------------
128 class ClipCmd : public Cmd {
129 public:
130     ClipCmd(ID, Shape, SkIRect);  // for creating the test cases
131     ClipCmd(ID, PaintersOrder paintersOrderWhenAdded, Shape, SkIRect);
132     ~ClipCmd() override;
133 
134     bool contains(int x, int y) const;
135 
136     uint32_t getSortZ() const;
137     uint32_t getDrawZ() const;
138 
139     SortKey getKey() override;
140 
141     void onAboutToBePopped(PaintersOrder paintersOrderWhenPopped);
142 
143     void execute(FakeCanvas*) const override;
144     void execute(SkCanvas*) const override;
145     void rasterize(uint32_t zBuffer[256][256], SkBitmap* dstBM) const override;
146 
dump()147     void dump() const override {
148         SkDebugf("%d: clip%s %d %d %d %d",
149                  fID.toInt(),
150                  fShape == Shape::kRect ? "Rect" : "Oval",
151                  fRect.fLeft, fRect.fTop, fRect.fRight, fRect.fBottom);
152     }
153 
mutate(SkIPoint trans)154     void mutate(SkIPoint trans) {
155         SkASSERT(!fHasBeenMutated);
156 
157         fRect.offset(trans.fX, trans.fY);
158         fHasBeenMutated = true;
159     }
160 
hasBeenMutated()161     bool hasBeenMutated() const { return fHasBeenMutated; }
rect()162     SkIRect rect() const { return fRect; }
163 
164 protected:
165 
166 private:
167     bool          fHasBeenMutated = false;
168     Shape         fShape;
169     SkIRect       fRect;
170     PaintersOrder fPaintersOrderWhenAdded;
171     PaintersOrder fPaintersOrderWhenPopped;
172 };
173 
174 //------------------------------------------------------------------------------------------------
175 
176 #endif // Cmds_DEFINED
177