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 "gm/gm.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkPicture.h"
15 #include "include/core/SkPictureRecorder.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkSize.h"
19 #include "include/core/SkString.h"
20
make_picture()21 static sk_sp<SkPicture> make_picture() {
22 SkPictureRecorder rec;
23 SkCanvas* canvas = rec.beginRecording(100, 100);
24
25 SkPaint paint;
26 paint.setAntiAlias(true);
27 SkPath path;
28
29 paint.setColor(0x800000FF);
30 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
31
32 paint.setColor(0x80FF0000);
33 path.moveTo(0, 0); path.lineTo(100, 0); path.lineTo(100, 100);
34 canvas->drawPath(path, paint);
35
36 paint.setColor(0x8000FF00);
37 path.reset(); path.moveTo(0, 0); path.lineTo(100, 0); path.lineTo(0, 100);
38 canvas->drawPath(path, paint);
39
40 paint.setColor(0x80FFFFFF);
41 paint.setBlendMode(SkBlendMode::kPlus);
42 canvas->drawRect(SkRect::MakeXYWH(25, 25, 50, 50), paint);
43
44 return rec.finishRecordingAsPicture();
45 }
46
47 // Exercise the optional arguments to drawPicture
48 //
49 class PictureGM : public skiagm::GM {
50 public:
PictureGM()51 PictureGM()
52 : fPicture(nullptr)
53 {}
54
55 protected:
onOnceBeforeDraw()56 void onOnceBeforeDraw() override {
57 fPicture = make_picture();
58 }
59
onShortName()60 SkString onShortName() override {
61 return SkString("pictures");
62 }
63
onISize()64 SkISize onISize() override {
65 return SkISize::Make(450, 120);
66 }
67
onDraw(SkCanvas * canvas)68 void onDraw(SkCanvas* canvas) override {
69 canvas->translate(10, 10);
70
71 SkMatrix matrix;
72 SkPaint paint;
73
74 canvas->drawPicture(fPicture);
75
76 matrix.setTranslate(110, 0);
77 canvas->drawPicture(fPicture, &matrix, nullptr);
78
79 matrix.postTranslate(110, 0);
80 canvas->drawPicture(fPicture, &matrix, &paint);
81
82 paint.setAlphaf(0.5f);
83 matrix.postTranslate(110, 0);
84 canvas->drawPicture(fPicture, &matrix, &paint);
85 }
86
87 private:
88 sk_sp<SkPicture> fPicture;
89
90 typedef skiagm::GM INHERITED;
91 };
92
93 // Exercise drawing a picture with a cull rect of non-zero top-left corner.
94 //
95 // See skbug.com/9334, which would fail
96 // ```
97 // dm -m picture_cull_rect --config serialize-8888
98 // ```
99 // until that bug is fixed.
100 class PictureCullRectGM : public skiagm::GM {
101 public:
PictureCullRectGM()102 PictureCullRectGM()
103 : fPicture(nullptr)
104 {}
105
106 protected:
onOnceBeforeDraw()107 void onOnceBeforeDraw() override {
108 SkPictureRecorder rec;
109 SkRTreeFactory rtreeFactory;
110 SkCanvas* canvas = rec.beginRecording(100, 100, &rtreeFactory);
111
112 SkPaint paint;
113 paint.setAntiAlias(false);
114
115 SkRect rect = SkRect::MakeLTRB(0, 80, 100, 100);
116
117 // Make picture complex enough to trigger the cull rect and bbh (RTree) computations.
118 // (A single drawRect won't trigger it.)
119 paint.setColor(0x800000FF);
120 canvas->drawRect(rect, paint);
121 canvas->drawOval(rect, paint);
122
123 fPicture = rec.finishRecordingAsPicture();
124
125 SkASSERT(fPicture->cullRect().top() == 80);
126 }
127
onShortName()128 SkString onShortName() override {
129 return SkString("picture_cull_rect");
130 }
131
onISize()132 SkISize onISize() override {
133 return SkISize::Make(120, 120);
134 }
135
onDraw(SkCanvas * canvas)136 void onDraw(SkCanvas* canvas) override {
137 canvas->clipRect(SkRect::MakeLTRB(0, 60, 120, 120));
138 canvas->translate(10, 10);
139 canvas->drawPicture(fPicture);
140 }
141
142 private:
143 sk_sp<SkPicture> fPicture;
144
145 typedef skiagm::GM INHERITED;
146 };
147
148 DEF_GM(return new PictureGM;)
149 DEF_GM(return new PictureCullRectGM;)
150