blob: 907b23441fe5d73603dde2d90cee6e54e3bebe93 [file] [log] [blame]
reedd5fa1a42014-08-09 11:08:05 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlendMode.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkMatrix.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPaint.h"
13#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkPicture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkPictureRecorder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040016#include "include/core/SkRect.h"
17#include "include/core/SkRefCnt.h"
18#include "include/core/SkSize.h"
19#include "include/core/SkString.h"
reedd5fa1a42014-08-09 11:08:05 -070020
reedca2622b2016-03-18 07:25:55 -070021static sk_sp<SkPicture> make_picture() {
reedd5fa1a42014-08-09 11:08:05 -070022 SkPictureRecorder rec;
23 SkCanvas* canvas = rec.beginRecording(100, 100);
24
25 SkPaint paint;
26 paint.setAntiAlias(true);
reedd5fa1a42014-08-09 11:08:05 -070027
28 paint.setColor(0x800000FF);
29 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
30
31 paint.setColor(0x80FF0000);
Mike Reed06d7c9d2020-08-26 12:56:51 -040032 canvas->drawPath(SkPath::Polygon({{0, 0}, {100, 0}, {100, 100}}, false), paint);
halcanary9d524f22016-03-29 09:03:52 -070033
reedd5fa1a42014-08-09 11:08:05 -070034 paint.setColor(0x8000FF00);
Mike Reed06d7c9d2020-08-26 12:56:51 -040035 canvas->drawPath(SkPath::Polygon({{0, 0}, {100, 0}, {0, 100}}, false), paint);
reedd5fa1a42014-08-09 11:08:05 -070036
37 paint.setColor(0x80FFFFFF);
reed374772b2016-10-05 17:33:02 -070038 paint.setBlendMode(SkBlendMode::kPlus);
reedd5fa1a42014-08-09 11:08:05 -070039 canvas->drawRect(SkRect::MakeXYWH(25, 25, 50, 50), paint);
40
reedca2622b2016-03-18 07:25:55 -070041 return rec.finishRecordingAsPicture();
reedd5fa1a42014-08-09 11:08:05 -070042}
43
44// Exercise the optional arguments to drawPicture
45//
46class PictureGM : public skiagm::GM {
47public:
caryclark63c684a2015-02-25 09:04:04 -080048 PictureGM()
halcanary96fcdcc2015-08-27 07:41:13 -070049 : fPicture(nullptr)
caryclark63c684a2015-02-25 09:04:04 -080050 {}
reedd5fa1a42014-08-09 11:08:05 -070051
52protected:
mtklein36352bf2015-03-25 18:17:31 -070053 void onOnceBeforeDraw() override {
reedca2622b2016-03-18 07:25:55 -070054 fPicture = make_picture();
caryclark63c684a2015-02-25 09:04:04 -080055 }
56
mtklein36352bf2015-03-25 18:17:31 -070057 SkString onShortName() override {
reedd5fa1a42014-08-09 11:08:05 -070058 return SkString("pictures");
59 }
60
mtklein36352bf2015-03-25 18:17:31 -070061 SkISize onISize() override {
reedd5fa1a42014-08-09 11:08:05 -070062 return SkISize::Make(450, 120);
63 }
64
mtklein36352bf2015-03-25 18:17:31 -070065 void onDraw(SkCanvas* canvas) override {
reedd5fa1a42014-08-09 11:08:05 -070066 canvas->translate(10, 10);
67
68 SkMatrix matrix;
69 SkPaint paint;
70
71 canvas->drawPicture(fPicture);
halcanary9d524f22016-03-29 09:03:52 -070072
reedd5fa1a42014-08-09 11:08:05 -070073 matrix.setTranslate(110, 0);
halcanary96fcdcc2015-08-27 07:41:13 -070074 canvas->drawPicture(fPicture, &matrix, nullptr);
halcanary9d524f22016-03-29 09:03:52 -070075
reedd5fa1a42014-08-09 11:08:05 -070076 matrix.postTranslate(110, 0);
77 canvas->drawPicture(fPicture, &matrix, &paint);
78
Mike Reed9407e242019-02-15 16:13:57 -050079 paint.setAlphaf(0.5f);
reedd5fa1a42014-08-09 11:08:05 -070080 matrix.postTranslate(110, 0);
81 canvas->drawPicture(fPicture, &matrix, &paint);
82 }
83
84private:
reedca2622b2016-03-18 07:25:55 -070085 sk_sp<SkPicture> fPicture;
reedd5fa1a42014-08-09 11:08:05 -070086
John Stiles7571f9e2020-09-02 22:42:33 -040087 using INHERITED = skiagm::GM;
reedd5fa1a42014-08-09 11:08:05 -070088};
89
Yuqian Li74587d82019-08-14 12:45:17 -070090// Exercise drawing a picture with a cull rect of non-zero top-left corner.
91//
92// See skbug.com/9334, which would fail
93// ```
94// dm -m picture_cull_rect --config serialize-8888
95// ```
96// until that bug is fixed.
97class PictureCullRectGM : public skiagm::GM {
98public:
99 PictureCullRectGM()
100 : fPicture(nullptr)
101 {}
102
103protected:
104 void onOnceBeforeDraw() override {
105 SkPictureRecorder rec;
106 SkRTreeFactory rtreeFactory;
107 SkCanvas* canvas = rec.beginRecording(100, 100, &rtreeFactory);
108
109 SkPaint paint;
110 paint.setAntiAlias(false);
111
112 SkRect rect = SkRect::MakeLTRB(0, 80, 100, 100);
113
114 // Make picture complex enough to trigger the cull rect and bbh (RTree) computations.
115 // (A single drawRect won't trigger it.)
116 paint.setColor(0x800000FF);
117 canvas->drawRect(rect, paint);
118 canvas->drawOval(rect, paint);
119
120 fPicture = rec.finishRecordingAsPicture();
121
122 SkASSERT(fPicture->cullRect().top() == 80);
123 }
124
125 SkString onShortName() override {
126 return SkString("picture_cull_rect");
127 }
128
129 SkISize onISize() override {
130 return SkISize::Make(120, 120);
131 }
132
133 void onDraw(SkCanvas* canvas) override {
134 canvas->clipRect(SkRect::MakeLTRB(0, 60, 120, 120));
135 canvas->translate(10, 10);
136 canvas->drawPicture(fPicture);
137 }
138
139private:
140 sk_sp<SkPicture> fPicture;
141
John Stiles7571f9e2020-09-02 22:42:33 -0400142 using INHERITED = skiagm::GM;
Yuqian Li74587d82019-08-14 12:45:17 -0700143};
144
halcanary385fe4d2015-08-26 13:07:48 -0700145DEF_GM(return new PictureGM;)
Yuqian Li74587d82019-08-14 12:45:17 -0700146DEF_GM(return new PictureCullRectGM;)