blob: d7f43bb87a9ea1e9b6689a5259f47e8338a842d1 [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);
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);
halcanary9d524f22016-03-29 09:03:52 -070035
reedd5fa1a42014-08-09 11:08:05 -070036 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);
reed374772b2016-10-05 17:33:02 -070041 paint.setBlendMode(SkBlendMode::kPlus);
reedd5fa1a42014-08-09 11:08:05 -070042 canvas->drawRect(SkRect::MakeXYWH(25, 25, 50, 50), paint);
43
reedca2622b2016-03-18 07:25:55 -070044 return rec.finishRecordingAsPicture();
reedd5fa1a42014-08-09 11:08:05 -070045}
46
47// Exercise the optional arguments to drawPicture
48//
49class PictureGM : public skiagm::GM {
50public:
caryclark63c684a2015-02-25 09:04:04 -080051 PictureGM()
halcanary96fcdcc2015-08-27 07:41:13 -070052 : fPicture(nullptr)
caryclark63c684a2015-02-25 09:04:04 -080053 {}
reedd5fa1a42014-08-09 11:08:05 -070054
55protected:
mtklein36352bf2015-03-25 18:17:31 -070056 void onOnceBeforeDraw() override {
reedca2622b2016-03-18 07:25:55 -070057 fPicture = make_picture();
caryclark63c684a2015-02-25 09:04:04 -080058 }
59
mtklein36352bf2015-03-25 18:17:31 -070060 SkString onShortName() override {
reedd5fa1a42014-08-09 11:08:05 -070061 return SkString("pictures");
62 }
63
mtklein36352bf2015-03-25 18:17:31 -070064 SkISize onISize() override {
reedd5fa1a42014-08-09 11:08:05 -070065 return SkISize::Make(450, 120);
66 }
67
mtklein36352bf2015-03-25 18:17:31 -070068 void onDraw(SkCanvas* canvas) override {
reedd5fa1a42014-08-09 11:08:05 -070069 canvas->translate(10, 10);
70
71 SkMatrix matrix;
72 SkPaint paint;
73
74 canvas->drawPicture(fPicture);
halcanary9d524f22016-03-29 09:03:52 -070075
reedd5fa1a42014-08-09 11:08:05 -070076 matrix.setTranslate(110, 0);
halcanary96fcdcc2015-08-27 07:41:13 -070077 canvas->drawPicture(fPicture, &matrix, nullptr);
halcanary9d524f22016-03-29 09:03:52 -070078
reedd5fa1a42014-08-09 11:08:05 -070079 matrix.postTranslate(110, 0);
80 canvas->drawPicture(fPicture, &matrix, &paint);
81
Mike Reed9407e242019-02-15 16:13:57 -050082 paint.setAlphaf(0.5f);
reedd5fa1a42014-08-09 11:08:05 -070083 matrix.postTranslate(110, 0);
84 canvas->drawPicture(fPicture, &matrix, &paint);
85 }
86
87private:
reedca2622b2016-03-18 07:25:55 -070088 sk_sp<SkPicture> fPicture;
reedd5fa1a42014-08-09 11:08:05 -070089
90 typedef skiagm::GM INHERITED;
91};
92
Yuqian Li74587d82019-08-14 12:45:17 -070093// 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.
100class PictureCullRectGM : public skiagm::GM {
101public:
102 PictureCullRectGM()
103 : fPicture(nullptr)
104 {}
105
106protected:
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
128 SkString onShortName() override {
129 return SkString("picture_cull_rect");
130 }
131
132 SkISize onISize() override {
133 return SkISize::Make(120, 120);
134 }
135
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
142private:
143 sk_sp<SkPicture> fPicture;
144
145 typedef skiagm::GM INHERITED;
146};
147
halcanary385fe4d2015-08-26 13:07:48 -0700148DEF_GM(return new PictureGM;)
Yuqian Li74587d82019-08-14 12:45:17 -0700149DEF_GM(return new PictureCullRectGM;)