blob: f2761da1cb37dfbe6490b9e3e4c6fb981cf6b6da [file] [log] [blame]
djsollen@google.comd4236572013-08-13 14:29:06 +00001/*
2 * Copyright 2013 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 "gm.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkPath.h"
12#include "SkRect.h"
13
14namespace skiagm {
15
16/*
17 * This GM exercises the flags to SkCanvas::save(). The canvas' save() and
18 * restore actions can be limited to only a portion of the canvas' state through
19 * the use of flags when calling save.
20 */
21class CanvasStateGM : public GM {
22 SkSize fSize;
23 enum {
24 WIDTH = 150,
25 HEIGHT = 150,
26 };
27
28 SkPaint fFillPaint;
29 SkPaint fStrokePaint;
30
31 SkPath fPath;
32
33 SkRect fOutlineRect;
34 SkRect fFillRect;
35
36
37public:
38 CanvasStateGM() {
39 fSize.set(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT));
40
41 fFillPaint.setColor(SK_ColorRED);
42 fFillPaint.setStyle(SkPaint::kFill_Style);
43
44 fStrokePaint.setColor(SK_ColorBLUE);
45 fStrokePaint.setStyle(SkPaint::kStroke_Style);
46 fStrokePaint.setStrokeWidth(1);
47
48 fPath.moveTo(25, 25);
49 fPath.lineTo(125, 25);
50 fPath.lineTo(75, 125);
51 fPath.close();
52
53 fOutlineRect = SkRect::MakeXYWH(1, 1, WIDTH-2, HEIGHT-2);
54 fFillRect = SkRect::MakeXYWH(10, 10, WIDTH-20, HEIGHT-20);
55 }
56
57protected:
58 virtual SkString onShortName() SK_OVERRIDE {
59 return SkString("canvas-state");
60 }
61
62 virtual SkISize onISize() SK_OVERRIDE {
63 return SkISize::Make(WIDTH*3, HEIGHT*4);
64 }
65
66 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
67
68 SkCanvas::SaveFlags flags[] = { SkCanvas::kMatrix_SaveFlag,
69 SkCanvas::kClip_SaveFlag,
70 SkCanvas::kMatrixClip_SaveFlag };
71
72 // columns -- flags
73 // rows -- permutations of setting the clip and matrix
robertphillips@google.come9cd27d2013-10-16 17:48:11 +000074 for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(flags)); ++i) {
djsollen@google.comd4236572013-08-13 14:29:06 +000075 for (int j = 0; j < 2; ++j) {
76 for (int k = 0; k < 2; ++k) {
reed@google.com75e9dfe2013-08-13 15:33:17 +000077 this->drawTestPattern(i, (2*j)+k, canvas, flags[i],
78 SkToBool(j), SkToBool(k));
djsollen@google.comd4236572013-08-13 14:29:06 +000079 }
80 }
81 }
82 }
83
84
djsollen@google.com2b1155a2013-08-13 14:48:09 +000085 virtual uint32_t onGetFlags() const SK_OVERRIDE { return kSkipPicture_Flag; }
djsollen@google.comd4236572013-08-13 14:29:06 +000086
87private:
88 void drawTestPattern(int x, int y, SkCanvas* canvas,
89 SkCanvas::SaveFlags flags, bool doClip, bool doScale) {
90 canvas->save();
reed@google.com75e9dfe2013-08-13 15:33:17 +000091 canvas->translate(SkIntToScalar(x*WIDTH), SkIntToScalar(y*HEIGHT));
djsollen@google.comd4236572013-08-13 14:29:06 +000092
93 canvas->drawRect(fOutlineRect, fStrokePaint);
94 canvas->save(flags);
95 if(doClip) {
96 canvas->clipPath(fPath);
97 }
98 if (doScale) {
djsollen@google.com2b1155a2013-08-13 14:48:09 +000099 canvas->scale(SkDoubleToScalar(0.5), SkDoubleToScalar(0.5));
djsollen@google.comd4236572013-08-13 14:29:06 +0000100 }
101 canvas->restore();
102 canvas->drawRect(fFillRect, fFillPaint);
103
104 canvas->restore();
105 }
106
107 typedef GM INHERITED;
108};
109
110//////////////////////////////////////////////////////////////////////////////
111
djsollen@google.com9615fed2013-08-20 17:54:33 +0000112class CanvasLayerStateGM : public GM {
113public:
114 CanvasLayerStateGM() {
djsollen@google.com9615fed2013-08-20 17:54:33 +0000115 fBluePaint.setColor(SK_ColorBLUE);
116 fBluePaint.setStyle(SkPaint::kFill_Style);
117
118 fRect = SkRect::MakeXYWH(SPACER, SPACER, WIDTH-(2*SPACER), (HEIGHT-(2*SPACER)) / 7);
119 }
120
121protected:
122 virtual SkString onShortName() SK_OVERRIDE {
123 return SkString("canvas-layer-state");
124 }
125
126 virtual SkISize onISize() SK_OVERRIDE {
127 return SkISize::Make(WIDTH, HEIGHT);
128 }
129
130 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
131
132 // clear the canvas to red
133 canvas->drawColor(SK_ColorRED);
134
135 // both rects should appear
136 drawTestPattern(canvas, 255, SkCanvas::kARGB_NoClipLayer_SaveFlag);
137
138 canvas->translate(0, 2*(fRect.height() + 10));
139
140 // only the top rect should appear
141 drawTestPattern(canvas, 255, SkCanvas::kARGB_ClipLayer_SaveFlag);
142
143 canvas->translate(0, 2*(fRect.height() + 10));
144
145 // only the bottom rect should appear
146 drawTestPattern(canvas, 0, SkCanvas::kARGB_NoClipLayer_SaveFlag);
147 }
148
149 virtual uint32_t onGetFlags() const SK_OVERRIDE { return kSkipGPU_Flag; }
150
151private:
152 // draw a rect within the layer's bounds and again outside the layer's bounds
153 void drawTestPattern(SkCanvas* canvas, U8CPU layerAlpha, SkCanvas::SaveFlags flags) {
154 canvas->saveLayerAlpha(&fRect, layerAlpha, flags);
155 canvas->drawRect(fRect, fBluePaint);
156 canvas->translate(0, fRect.height() + 10);
157 canvas->drawRect(fRect, fBluePaint);
158 canvas->restore();
159 }
160
161 enum {
162 WIDTH = 400,
163 HEIGHT = 400,
164 SPACER = 10,
165 };
166
djsollen@google.com9615fed2013-08-20 17:54:33 +0000167 SkPaint fBluePaint;
168 SkRect fRect;
169
170 typedef GM INHERITED;
171};
172
173//////////////////////////////////////////////////////////////////////////////
174
djsollen@google.comd4236572013-08-13 14:29:06 +0000175DEF_GM( return SkNEW(CanvasStateGM); )
djsollen@google.com9615fed2013-08-20 17:54:33 +0000176DEF_GM( return SkNEW(CanvasLayerStateGM); )
djsollen@google.comd4236572013-08-13 14:29:06 +0000177
178} // end namespace