blob: 6b5fa23ddf523caa596154d596813cc6abe30fea [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"
Kevin Lubickf437d012023-06-26 14:35:06 -04009#include "include/core/SkBBHFactory.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkBlendMode.h"
11#include "include/core/SkCanvas.h"
12#include "include/core/SkMatrix.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkPaint.h"
14#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkPicture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkPictureRecorder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040017#include "include/core/SkRect.h"
18#include "include/core/SkRefCnt.h"
19#include "include/core/SkSize.h"
20#include "include/core/SkString.h"
reedd5fa1a42014-08-09 11:08:05 -070021
reedca2622b2016-03-18 07:25:55 -070022static sk_sp<SkPicture> make_picture() {
reedd5fa1a42014-08-09 11:08:05 -070023 SkPictureRecorder rec;
24 SkCanvas* canvas = rec.beginRecording(100, 100);
25
26 SkPaint paint;
27 paint.setAntiAlias(true);
reedd5fa1a42014-08-09 11:08:05 -070028
29 paint.setColor(0x800000FF);
30 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
31
32 paint.setColor(0x80FF0000);
Mike Reed06d7c9d2020-08-26 12:56:51 -040033 canvas->drawPath(SkPath::Polygon({{0, 0}, {100, 0}, {100, 100}}, false), paint);
halcanary9d524f22016-03-29 09:03:52 -070034
reedd5fa1a42014-08-09 11:08:05 -070035 paint.setColor(0x8000FF00);
Mike Reed06d7c9d2020-08-26 12:56:51 -040036 canvas->drawPath(SkPath::Polygon({{0, 0}, {100, 0}, {0, 100}}, false), paint);
reedd5fa1a42014-08-09 11:08:05 -070037
38 paint.setColor(0x80FFFFFF);
reed374772b2016-10-05 17:33:02 -070039 paint.setBlendMode(SkBlendMode::kPlus);
reedd5fa1a42014-08-09 11:08:05 -070040 canvas->drawRect(SkRect::MakeXYWH(25, 25, 50, 50), paint);
41
reedca2622b2016-03-18 07:25:55 -070042 return rec.finishRecordingAsPicture();
reedd5fa1a42014-08-09 11:08:05 -070043}
44
45// Exercise the optional arguments to drawPicture
46//
47class PictureGM : public skiagm::GM {
48public:
caryclark63c684a2015-02-25 09:04:04 -080049 PictureGM()
halcanary96fcdcc2015-08-27 07:41:13 -070050 : fPicture(nullptr)
caryclark63c684a2015-02-25 09:04:04 -080051 {}
reedd5fa1a42014-08-09 11:08:05 -070052
53protected:
mtklein36352bf2015-03-25 18:17:31 -070054 void onOnceBeforeDraw() override {
reedca2622b2016-03-18 07:25:55 -070055 fPicture = make_picture();
caryclark63c684a2015-02-25 09:04:04 -080056 }
57
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000058 SkString getName() const override { return SkString("pictures"); }
reedd5fa1a42014-08-09 11:08:05 -070059
Leandro Lovisolo8f023882023-08-15 21:13:52 +000060 SkISize getISize() override { return SkISize::Make(450, 120); }
reedd5fa1a42014-08-09 11:08:05 -070061
mtklein36352bf2015-03-25 18:17:31 -070062 void onDraw(SkCanvas* canvas) override {
reedd5fa1a42014-08-09 11:08:05 -070063 canvas->translate(10, 10);
64
65 SkMatrix matrix;
66 SkPaint paint;
67
68 canvas->drawPicture(fPicture);
halcanary9d524f22016-03-29 09:03:52 -070069
reedd5fa1a42014-08-09 11:08:05 -070070 matrix.setTranslate(110, 0);
halcanary96fcdcc2015-08-27 07:41:13 -070071 canvas->drawPicture(fPicture, &matrix, nullptr);
halcanary9d524f22016-03-29 09:03:52 -070072
reedd5fa1a42014-08-09 11:08:05 -070073 matrix.postTranslate(110, 0);
74 canvas->drawPicture(fPicture, &matrix, &paint);
75
Mike Reed9407e242019-02-15 16:13:57 -050076 paint.setAlphaf(0.5f);
reedd5fa1a42014-08-09 11:08:05 -070077 matrix.postTranslate(110, 0);
78 canvas->drawPicture(fPicture, &matrix, &paint);
79 }
80
81private:
reedca2622b2016-03-18 07:25:55 -070082 sk_sp<SkPicture> fPicture;
reedd5fa1a42014-08-09 11:08:05 -070083
John Stiles7571f9e2020-09-02 22:42:33 -040084 using INHERITED = skiagm::GM;
reedd5fa1a42014-08-09 11:08:05 -070085};
86
Yuqian Li74587d82019-08-14 12:45:17 -070087// Exercise drawing a picture with a cull rect of non-zero top-left corner.
88//
89// See skbug.com/9334, which would fail
90// ```
91// dm -m picture_cull_rect --config serialize-8888
92// ```
93// until that bug is fixed.
94class PictureCullRectGM : public skiagm::GM {
95public:
96 PictureCullRectGM()
97 : fPicture(nullptr)
98 {}
99
100protected:
101 void onOnceBeforeDraw() override {
102 SkPictureRecorder rec;
103 SkRTreeFactory rtreeFactory;
104 SkCanvas* canvas = rec.beginRecording(100, 100, &rtreeFactory);
105
106 SkPaint paint;
107 paint.setAntiAlias(false);
108
109 SkRect rect = SkRect::MakeLTRB(0, 80, 100, 100);
110
111 // Make picture complex enough to trigger the cull rect and bbh (RTree) computations.
112 // (A single drawRect won't trigger it.)
113 paint.setColor(0x800000FF);
114 canvas->drawRect(rect, paint);
115 canvas->drawOval(rect, paint);
116
117 fPicture = rec.finishRecordingAsPicture();
118
119 SkASSERT(fPicture->cullRect().top() == 80);
120 }
121
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000122 SkString getName() const override { return SkString("picture_cull_rect"); }
Yuqian Li74587d82019-08-14 12:45:17 -0700123
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000124 SkISize getISize() override { return SkISize::Make(120, 120); }
Yuqian Li74587d82019-08-14 12:45:17 -0700125
126 void onDraw(SkCanvas* canvas) override {
127 canvas->clipRect(SkRect::MakeLTRB(0, 60, 120, 120));
128 canvas->translate(10, 10);
129 canvas->drawPicture(fPicture);
130 }
131
132private:
133 sk_sp<SkPicture> fPicture;
134
John Stiles7571f9e2020-09-02 22:42:33 -0400135 using INHERITED = skiagm::GM;
Yuqian Li74587d82019-08-14 12:45:17 -0700136};
137
halcanary385fe4d2015-08-26 13:07:48 -0700138DEF_GM(return new PictureGM;)
Yuqian Li74587d82019-08-14 12:45:17 -0700139DEF_GM(return new PictureCullRectGM;)