Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
picture.cpp
Go to the documentation of this file.
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"
14#include "include/core/SkPath.h"
17#include "include/core/SkRect.h"
19#include "include/core/SkSize.h"
21
24 SkCanvas* canvas = rec.beginRecording(100, 100);
25
27 paint.setAntiAlias(true);
28
29 paint.setColor(0x800000FF);
30 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
31
32 paint.setColor(0x80FF0000);
33 canvas->drawPath(SkPath::Polygon({{0, 0}, {100, 0}, {100, 100}}, false), paint);
34
35 paint.setColor(0x8000FF00);
36 canvas->drawPath(SkPath::Polygon({{0, 0}, {100, 0}, {0, 100}}, false), paint);
37
38 paint.setColor(0x80FFFFFF);
39 paint.setBlendMode(SkBlendMode::kPlus);
40 canvas->drawRect(SkRect::MakeXYWH(25, 25, 50, 50), paint);
41
42 return rec.finishRecordingAsPicture();
43}
44
45// Exercise the optional arguments to drawPicture
46//
47class PictureGM : public skiagm::GM {
48public:
50 : fPicture(nullptr)
51 {}
52
53protected:
54 void onOnceBeforeDraw() override {
55 fPicture = make_picture();
56 }
57
58 SkString getName() const override { return SkString("pictures"); }
59
60 SkISize getISize() override { return SkISize::Make(450, 120); }
61
62 void onDraw(SkCanvas* canvas) override {
63 canvas->translate(10, 10);
64
65 SkMatrix matrix;
67
68 canvas->drawPicture(fPicture);
69
70 matrix.setTranslate(110, 0);
71 canvas->drawPicture(fPicture, &matrix, nullptr);
72
73 matrix.postTranslate(110, 0);
74 canvas->drawPicture(fPicture, &matrix, &paint);
75
76 paint.setAlphaf(0.5f);
77 matrix.postTranslate(110, 0);
78 canvas->drawPicture(fPicture, &matrix, &paint);
79 }
80
81private:
82 sk_sp<SkPicture> fPicture;
83
84 using INHERITED = skiagm::GM;
85};
86
87// 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.
95public:
97 : fPicture(nullptr)
98 {}
99
100protected:
101 void onOnceBeforeDraw() override {
103 SkRTreeFactory rtreeFactory;
104 SkCanvas* canvas = rec.beginRecording(100, 100, &rtreeFactory);
105
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
122 SkString getName() const override { return SkString("picture_cull_rect"); }
123
124 SkISize getISize() override { return SkISize::Make(120, 120); }
125
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
135 using INHERITED = skiagm::GM;
136};
137
138DEF_GM(return new PictureGM;)
139DEF_GM(return new PictureCullRectGM;)
#define SkASSERT(cond)
Definition SkAssert.h:116
@ kPlus
r = min(s + d, 1)
SkISize getISize() override
Definition picture.cpp:124
SkString getName() const override
Definition picture.cpp:122
void onDraw(SkCanvas *canvas) override
Definition picture.cpp:126
void onOnceBeforeDraw() override
Definition picture.cpp:101
void onDraw(SkCanvas *canvas) override
Definition picture.cpp:62
SkString getName() const override
Definition picture.cpp:58
void onOnceBeforeDraw() override
Definition picture.cpp:54
SkISize getISize() override
Definition picture.cpp:60
void drawRect(const SkRect &rect, const SkPaint &paint)
void drawOval(const SkRect &oval, const SkPaint &paint)
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void translate(SkScalar dx, SkScalar dy)
void drawPath(const SkPath &path, const SkPaint &paint)
void drawPicture(const SkPicture *picture)
Definition SkCanvas.h:1961
static SkPath Polygon(const SkPoint pts[], int count, bool isClosed, SkPathFillType=SkPathFillType::kWinding, bool isVolatile=false)
Definition SkPath.cpp:3546
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkPicture > finishRecordingAsPicture()
virtual SkRect cullRect() const =0
const Paint & paint
#define DEF_GM(CODE)
Definition gm.h:40
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
constexpr float top() const
Definition SkRect.h:741
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646
static sk_sp< SkPicture > make_picture()
Definition picture.cpp:22