Flutter Engine
The Flutter Engine
aaclip.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2011 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"
16#include "include/core/SkRect.h"
18#include "include/core/SkSize.h"
21#include "tools/ToolUtils.h"
22
23/** Draw a 2px border around the target, then red behind the target;
24 set the clip to match the target, then draw >> the target in blue.
25*/
26
27static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
28 SkPaint borderPaint;
29 borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
30 borderPaint.setAntiAlias(true);
31 SkPaint backgroundPaint;
32 backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
33 backgroundPaint.setAntiAlias(true);
34 SkPaint foregroundPaint;
35 foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
36 foregroundPaint.setAntiAlias(true);
37
38 canvas->save();
40 target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
41 canvas->drawRect(target, borderPaint);
43 canvas->drawRect(target, backgroundPaint);
44 canvas->clipRect(target, true);
45 target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
46 canvas->drawRect(target, foregroundPaint);
47 canvas->restore();
48}
49
50static void draw_square(SkCanvas* canvas, int x, int y) {
52 draw(canvas, target, x, y);
53}
54
55static void draw_column(SkCanvas* canvas, int x, int y) {
57 draw(canvas, target, x, y);
58}
59
60static void draw_bar(SkCanvas* canvas, int x, int y) {
62 draw(canvas, target, x, y);
63}
64
65static void draw_rect_tests(SkCanvas* canvas) {
66 draw_square(canvas, 10, 10);
67 draw_column(canvas, 30, 10);
68 draw_bar(canvas, 10, 30);
69}
70
71/**
72 Test a set of clipping problems discovered while writing blitAntiRect,
73 and test all the code paths through the clipping blitters.
74 Each region should show as a blue center surrounded by a 2px green
75 border, with no red.
76*/
77DEF_SIMPLE_GM(aaclip, canvas, 240, 120) {
78 // Initial pixel-boundary-aligned draw
79 draw_rect_tests(canvas);
80
81 // Repeat 4x with .2, .4, .6, .8 px offsets
82 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
83 canvas->translate(SkIntToScalar(50), 0);
84 draw_rect_tests(canvas);
85
86 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
87 canvas->translate(SkIntToScalar(50), 0);
88 draw_rect_tests(canvas);
89
90 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
91 canvas->translate(SkIntToScalar(50), 0);
92 draw_rect_tests(canvas);
93
94 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
95 canvas->translate(SkIntToScalar(50), 0);
96 draw_rect_tests(canvas);
97}
98
99/////////////////////////////////////////////////////////////////////////
100
101#ifdef SK_BUILD_FOR_MAC
102
104
105static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) {
106 return SkCanvas::MakeRasterDirect(bm.info(), bm.getPixels(), bm.rowBytes());
107}
108
109static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
110 SkBitmap bm;
111 bm.allocPixels(info);
112
113 if (info.isOpaque()) {
115 } else {
116 bm.eraseColor(0);
117 }
118
120 paint.setAntiAlias(true);
121 paint.setColor(SK_ColorBLUE);
122 make_canvas(bm)->drawCircle(50, 50, 49, paint);
123 canvas->drawImage(bm.asImage(), 10, 10);
124
125 CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
126
127 SkBitmap bm2;
128 SkCreateBitmapFromCGImage(&bm2, image);
129 canvas->drawImage(bm2.asImage(), 10, 120);
130 canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
131
132 CGImageRelease(image);
133}
134
135DEF_SIMPLE_GM(cgimage, canvas, 800, 250) {
136 const struct {
137 SkColorType fCT;
138 SkAlphaType fAT;
139 } rec[] = {
141
145
149 };
150
151 for (size_t i = 0; i < std::size(rec); ++i) {
152 SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
153 test_image(canvas, info);
154 canvas->translate(info.width() + 10, 0);
155 }
156}
157
158#endif
159
160///////////////////////////////////////////////////////////////////////////////////////////////////
161
162// https://bug.skia.org/3716
163class ClipCubicGM : public skiagm::GM {
164 const SkScalar W = 100;
165 const SkScalar H = 240;
166
167 SkPath fVPath, fHPath;
168public:
170 fVPath = SkPathBuilder().moveTo(W, 0)
171 .cubicTo(W, H-10, 0, 10, 0, H)
172 .detach();
173
174 SkMatrix pivot;
175 pivot.setRotate(90, W/2, H/2);
176 fHPath = fVPath.makeTransform(pivot);
177 }
178
179protected:
180 SkString getName() const override { return SkString("clipcubic"); }
181
182 SkISize getISize() override { return SkISize::Make(400, 410); }
183
184 void doDraw(SkCanvas* canvas, const SkPath& path) {
186 paint.setAntiAlias(true);
187
188 paint.setColor(0xFFCCCCCC);
189 canvas->drawPath(path, paint);
190
191 paint.setColor(SK_ColorRED);
193 canvas->drawPath(path, paint);
194 }
195
196 void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
197 SkAutoCanvasRestore acr(canvas, true);
198
199 SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
201 paint.setColor(ToolUtils::color_to_565(0xFF8888FF));
202
203 canvas->drawRect(r, paint);
204 this->doDraw(canvas, path);
205
206 canvas->translate(dx, dy);
207
208 canvas->drawRect(r, paint);
209 canvas->clipRect(r);
210 this->doDraw(canvas, path);
211 }
212
213 void onDraw(SkCanvas* canvas) override {
214 canvas->translate(80, 10);
215 this->drawAndClip(canvas, fVPath, 200, 0);
216 canvas->translate(0, 200);
217 this->drawAndClip(canvas, fHPath, 200, 0);
218 }
219
220private:
221 using INHERITED = skiagm::GM;
222};
223DEF_GM(return new ClipCubicGM;)
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
kUnpremul_SkAlphaType
SkAlphaType
Definition: SkAlphaType.h:26
@ kOpaque_SkAlphaType
pixel is opaque
Definition: SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
SkColorType
Definition: SkColorType.h:19
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition: SkColorType.h:22
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
#define SkColorSetRGB(r, g, b)
Definition: SkColor.h:57
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
#define SK_Scalar1
Definition: SkScalar.h:18
#define SkIntToScalar(x)
Definition: SkScalar.h:57
static void test_image(const sk_sp< SkSpecialImage > &img, skiatest::Reporter *reporter, GrRecordingContext *rContext, bool isGPUBacked)
#define W
Definition: aaa.cpp:17
static void draw_rect_tests(SkCanvas *canvas)
Definition: aaclip.cpp:65
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
static void draw_column(SkCanvas *canvas, int x, int y)
Definition: aaclip.cpp:55
DEF_SIMPLE_GM(aaclip, canvas, 240, 120)
Definition: aaclip.cpp:77
static void draw_square(SkCanvas *canvas, int x, int y)
Definition: aaclip.cpp:50
static void draw_bar(SkCanvas *canvas, int x, int y)
Definition: aaclip.cpp:60
SkString getName() const override
Definition: aaclip.cpp:180
void onDraw(SkCanvas *canvas) override
Definition: aaclip.cpp:213
void doDraw(SkCanvas *canvas, const SkPath &path)
Definition: aaclip.cpp:184
void drawAndClip(SkCanvas *canvas, const SkPath &path, SkScalar dx, SkScalar dy)
Definition: aaclip.cpp:196
SkISize getISize() override
Definition: aaclip.cpp:182
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
size_t rowBytes() const
Definition: SkBitmap.h:238
void * getPixels() const
Definition: SkBitmap.h:283
const SkImageInfo & info() const
Definition: SkBitmap.h:139
int height() const
Definition: SkBitmap.h:158
void eraseColor(SkColor4f) const
Definition: SkBitmap.cpp:442
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
int save()
Definition: SkCanvas.cpp:447
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
static std::unique_ptr< SkCanvas > MakeRasterDirect(const SkImageInfo &info, void *pixels, size_t rowBytes, const SkSurfaceProps *props=nullptr)
Definition: SkCanvas.cpp:2801
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
SkMatrix & setRotate(SkScalar degrees, SkScalar px, SkScalar py)
Definition: SkMatrix.cpp:452
void setColor(SkColor color)
Definition: SkPaint.cpp:119
void setAntiAlias(bool aa)
Definition: SkPaint.h:170
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
SkPathBuilder & cubicTo(SkPoint pt1, SkPoint pt2, SkPoint pt3)
SkPathBuilder & moveTo(SkPoint pt)
Definition: SkPath.h:59
SkPath makeTransform(const SkMatrix &m, SkApplyPerspectiveClip pc=SkApplyPerspectiveClip::kYes) const
Definition: SkPath.h:1400
Definition: gm.h:110
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
uint32_t * target
#define DEF_GM(CODE)
Definition: gm.h:40
double y
double x
sk_sp< const SkImage > image
Definition: SkRecords.h:269
skia_private::AutoTArray< sk_sp< SkImageFilter > > filters TypedMatrix matrix TypedMatrix matrix SkScalar dx
Definition: SkRecords.h:208
SkColor color_to_565(SkColor color)
Definition: ToolUtils.cpp:139
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
Definition: SkMD5.cpp:130
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
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