• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 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/gm.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkPath.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkString.h"
19 #include "src/core/SkCanvasPriv.h"
20 #include "tools/ToolUtils.h"
21 
do_draw(SkCanvas * canvas,const SkRect & r)22 static void do_draw(SkCanvas* canvas, const SkRect& r) {
23     SkPaint paint;
24     paint.setBlendMode(SkBlendMode::kSrc);
25     paint.setColor(0x800000FF);
26     canvas->drawRect(r, paint);
27 }
28 
29 /**
30  *  Exercise SkCanvasPriv::kDontClipToLayer_SaveLayerFlag flag, which does not limit the clip to the
31  *  layer's bounds. Thus when a draw occurs, it can (depending on "where" it is) draw into the layer
32  *  and/or draw onto the surrounding portions of the canvas, or both.
33  *
34  *  This GM has a 100x100 rectangle (r), which its going to draw. However first it creates a layer
35  *  with this flag covering 1/2 of the rectangle (upper half). Then it draws the rect in SRC mode.
36  *
37  *  The portion of the draw that intersects the layer should see the SRC draw, apply it to the layer
38  *  and then during restore, it will SRC_OVER that layer onto the canvas (SRC_OVER since the layer
39  *  has no paint, so it gets the default xfermode during restore).
40  *
41  *  Note: when we talk about drawing directly into the "canvas", in fact we are drawing into an
42  *        "outer" layer we created (filled with red). This is a testing detail, so that our final
43  *        output image is itself opaque, otherwise we make it harder to view the GM as a PNG.
44  *
45  *  The portion of the draw below the layer draws directly into the canvas. Since it is in SRC mode,
46  *  it will write 0x80 to the canvas' alpha, overwriting the "red", which then gets blended with
47  *  the GM's white background.
48  *
49  *  The portion in the layer, will end up SRC_OVERing the 0x80 layer pixels onto the canvas' red
50  *  pixels, making magenta.
51  *
52  *  Thus the expected result is the upper half to be magenta 0xFF7F0080, and the lower half to be
53  *  light blue 0xFF7F7FFF.
54  */
55 DEF_SIMPLE_GM(dont_clip_to_layer, canvas, 120, 120) {
56     const SkRect r { 10, 10, 110, 110 };
57 
58     // Wrap the entire test inside a red layer, so we don't punch the actual gm's alpha with
59     // kSrc_Mode, which makes it hard to view (we like our GMs to have opaque pixels).
60     canvas->saveLayer(&r, nullptr);
61     canvas->drawColor(SK_ColorRED);
62 
63     SkRect r0 = { 20, 20, 100, 55 };
64     SkRect r1 = { 20, 65, 100, 100 };
65 
66     SkCanvas::SaveLayerRec rec;
67     rec.fPaint = nullptr;
68     rec.fBounds = &r0;
69     rec.fBackdrop = nullptr;
70     rec.fSaveLayerFlags = SkCanvasPriv::kDontClipToLayer_SaveLayerFlag;
71     canvas->saveLayer(rec);
72     rec.fBounds = &r1;
73     canvas->saveLayer(rec);
74     do_draw(canvas, r);
75     canvas->restore();
76     canvas->restore();
77 
78     canvas->restore();  // red-layer
79 }
80 
81 /** Draw a 2px border around the target, then red behind the target;
82     set the clip to match the target, then draw >> the target in blue.
83 */
84 
draw(SkCanvas * canvas,SkRect & target,int x,int y)85 static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
86     SkPaint borderPaint;
87     borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
88     borderPaint.setAntiAlias(true);
89     SkPaint backgroundPaint;
90     backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
91     backgroundPaint.setAntiAlias(true);
92     SkPaint foregroundPaint;
93     foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
94     foregroundPaint.setAntiAlias(true);
95 
96     canvas->save();
97     canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
98     target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
99     canvas->drawRect(target, borderPaint);
100     target.inset(SkIntToScalar(2), SkIntToScalar(2));
101     canvas->drawRect(target, backgroundPaint);
102     canvas->clipRect(target, true);
103     target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
104     canvas->drawRect(target, foregroundPaint);
105     canvas->restore();
106 }
107 
draw_square(SkCanvas * canvas,int x,int y)108 static void draw_square(SkCanvas* canvas, int x, int y) {
109     SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
110     draw(canvas, target, x, y);
111 }
112 
draw_column(SkCanvas * canvas,int x,int y)113 static void draw_column(SkCanvas* canvas, int x, int y) {
114     SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
115     draw(canvas, target, x, y);
116 }
117 
draw_bar(SkCanvas * canvas,int x,int y)118 static void draw_bar(SkCanvas* canvas, int x, int y) {
119     SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
120     draw(canvas, target, x, y);
121 }
122 
draw_rect_tests(SkCanvas * canvas)123 static void draw_rect_tests(SkCanvas* canvas) {
124     draw_square(canvas, 10, 10);
125     draw_column(canvas, 30, 10);
126     draw_bar(canvas, 10, 30);
127 }
128 
129 /**
130    Test a set of clipping problems discovered while writing blitAntiRect,
131    and test all the code paths through the clipping blitters.
132    Each region should show as a blue center surrounded by a 2px green
133    border, with no red.
134 */
135 DEF_SIMPLE_GM(aaclip, canvas, 240, 120) {
136         // Initial pixel-boundary-aligned draw
137         draw_rect_tests(canvas);
138 
139         // Repeat 4x with .2, .4, .6, .8 px offsets
140         canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
141         canvas->translate(SkIntToScalar(50), 0);
142         draw_rect_tests(canvas);
143 
144         canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
145         canvas->translate(SkIntToScalar(50), 0);
146         draw_rect_tests(canvas);
147 
148         canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
149         canvas->translate(SkIntToScalar(50), 0);
150         draw_rect_tests(canvas);
151 
152         canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
153         canvas->translate(SkIntToScalar(50), 0);
154         draw_rect_tests(canvas);
155 }
156 
157 /////////////////////////////////////////////////////////////////////////
158 
159 #ifdef SK_BUILD_FOR_MAC
160 
161 #include "include/utils/mac/SkCGUtils.h"
162 #include "src/core/SkMakeUnique.h"
163 
make_canvas(const SkBitmap & bm)164 static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) {
165     const SkImageInfo& info = bm.info();
166     if (info.bytesPerPixel() == 4) {
167         return SkCanvas::MakeRasterDirectN32(info.width(), info.height(),
168                                              (SkPMColor*)bm.getPixels(),
169                                              bm.rowBytes());
170     } else {
171         return skstd::make_unique<SkCanvas>(bm);
172     }
173 }
174 
test_image(SkCanvas * canvas,const SkImageInfo & info)175 static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
176     SkBitmap bm;
177     bm.allocPixels(info);
178 
179     if (info.isOpaque()) {
180         bm.eraseColor(SK_ColorGREEN);
181     } else {
182         bm.eraseColor(0);
183     }
184 
185     SkPaint paint;
186     paint.setAntiAlias(true);
187     paint.setColor(SK_ColorBLUE);
188     make_canvas(bm)->drawCircle(50, 50, 49, paint);
189     canvas->drawBitmap(bm, 10, 10);
190 
191     CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
192 
193     SkBitmap bm2;
194     SkCreateBitmapFromCGImage(&bm2, image);
195     canvas->drawBitmap(bm2, 10, 120);
196     canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
197 
198     CGImageRelease(image);
199 }
200 
201 DEF_SIMPLE_GM(cgimage, canvas, 800, 250) {
202         const struct {
203             SkColorType fCT;
204             SkAlphaType fAT;
205         } rec[] = {
206             { kRGB_565_SkColorType, kOpaque_SkAlphaType },
207 
208             { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
209             { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
210             { kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
211 
212             { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
213             { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
214             { kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
215         };
216 
217         for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
218             SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
219             test_image(canvas, info);
220             canvas->translate(info.width() + 10, 0);
221         }
222 }
223 
224 #endif
225 
226 ///////////////////////////////////////////////////////////////////////////////////////////////////
227 
228 // https://bug.skia.org/3716
229 class ClipCubicGM : public skiagm::GM {
230     const SkScalar W = 100;
231     const SkScalar H = 240;
232 
233     SkPath fVPath, fHPath;
234 public:
ClipCubicGM()235     ClipCubicGM() {
236         fVPath.moveTo(W, 0);
237         fVPath.cubicTo(W, H-10, 0, 10, 0, H);
238 
239         SkMatrix pivot;
240         pivot.setRotate(90, W/2, H/2);
241         fVPath.transform(pivot, &fHPath);
242     }
243 
244 protected:
onShortName()245     SkString onShortName() override {
246         return SkString("clipcubic");
247     }
248 
onISize()249     SkISize onISize() override {
250         return SkISize::Make(400, 410);
251     }
252 
doDraw(SkCanvas * canvas,const SkPath & path)253     void doDraw(SkCanvas* canvas, const SkPath& path) {
254         SkPaint paint;
255         paint.setAntiAlias(true);
256 
257         paint.setColor(0xFFCCCCCC);
258         canvas->drawPath(path, paint);
259 
260         paint.setColor(SK_ColorRED);
261         paint.setStyle(SkPaint::kStroke_Style);
262         canvas->drawPath(path, paint);
263     }
264 
drawAndClip(SkCanvas * canvas,const SkPath & path,SkScalar dx,SkScalar dy)265     void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
266         SkAutoCanvasRestore acr(canvas, true);
267 
268         SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
269         SkPaint paint;
270         paint.setColor(ToolUtils::color_to_565(0xFF8888FF));
271 
272         canvas->drawRect(r, paint);
273         this->doDraw(canvas, path);
274 
275         canvas->translate(dx, dy);
276 
277         canvas->drawRect(r, paint);
278         canvas->clipRect(r);
279         this->doDraw(canvas, path);
280     }
281 
onDraw(SkCanvas * canvas)282     void onDraw(SkCanvas* canvas) override {
283         canvas->translate(80, 10);
284         this->drawAndClip(canvas, fVPath, 200, 0);
285         canvas->translate(0, 200);
286         this->drawAndClip(canvas, fHPath, 200, 0);
287     }
288 
289 private:
290     typedef skiagm::GM INHERITED;
291 };
292 DEF_GM(return new ClipCubicGM;)
293