Flutter Engine
The Flutter Engine
surface.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"
13#include "include/core/SkFont.h"
18#include "include/core/SkRect.h"
22#include "include/core/SkSize.h"
33#if defined(SK_GRAPHITE)
35#endif
37#include "tools/ToolUtils.h"
40
41#define W 800
42#define H 100
43
45 int a = 0x99;
46 int b = 0xBB;
47 SkPoint pts[] = { { 0, 0 }, { W, H } };
50}
51
54 const SkImageInfo& info,
55 uint32_t flags,
57 SkScalar contrast,
58 SkScalar gamma) {
59 SkSurfaceProps props(flags, geo, contrast, gamma);
60#if defined(SK_GRAPHITE)
61 if (recorder) {
62 return SkSurfaces::RenderTarget(recorder, info, skgpu::Mipmapped::kNo, &props);
63 } else
64#endif
65 if (ctx) {
66 return SkSurfaces::RenderTarget(ctx, skgpu::Budgeted::kNo, info, 0, &props);
67 } else {
68 return SkSurfaces::Raster(info, &props);
69 }
70}
71
72static void test_draw(SkCanvas* canvas, const char label[]) {
74
75 paint.setAntiAlias(true);
76 paint.setDither(true);
77
78 paint.setShader(make_shader());
79 canvas->drawRect(SkRect::MakeWH(W, H), paint);
80 paint.setShader(nullptr);
81
82 paint.setColor(SK_ColorWHITE);
85 SkTextUtils::DrawString(canvas, label, W / 2, H * 3 / 4, font, paint,
87}
88
89class SurfacePropsGM : public skiagm::GM {
90public:
91 SurfacePropsGM(uint32_t flags) : fFlags(flags) {
92 recs = {
94 "Unknown geometry, default contrast/gamma",
98 "RGB_H, default contrast/gamma",
102 "BGR_H, default contrast/gamma",
106 "RGB_V, default contrast/gamma",
110 "BGR_V, default contrast/gamma",
113 {kRGB_H_SkPixelGeometry, "RGB_H contrast : 0 gamma: 0", 0, 0},
114 {kRGB_H_SkPixelGeometry, "RGB_H contrast : 1 gamma: 0", 1, 0},
115 {kRGB_H_SkPixelGeometry, "RGB_H contrast : 0 gamma: 3.9", 0, 3.9f},
116 {kRGB_H_SkPixelGeometry, "RGB_H contrast : 1 gamma: 3.9", 1, 3.9f},
117 };
118 }
119
120protected:
121 SkString getName() const override {
122 return SkStringPrintf("surfaceprops%s",
123 fFlags != 0 ? "_df" : "");
124 }
125
126 SkISize getISize() override { return SkISize::Make(W, H * recs.size()); }
127
128 void onDraw(SkCanvas* canvas) override {
129 auto ctx = canvas->recordingContext();
130 auto recorder = canvas->recorder();
131
132 // must be opaque to have a hope of testing LCD text
134
135 SkScalar x = 0;
136 SkScalar y = 0;
137 for (const auto& rec : recs) {
138 auto surface(make_surface(ctx, recorder, info, fFlags, rec.fGeo, rec.fContrast,
139 rec.fGamma));
140 if (!surface) {
141 SkDebugf("failed to create surface! label: %s", rec.fLabel);
142 continue;
143 }
144 test_draw(surface->getCanvas(), rec.fLabel);
145 surface->draw(canvas, x, y);
146 y += H;
147 }
148 }
149
150private:
151 struct SurfacePropsInput {
152 SkPixelGeometry fGeo;
153 const char* fLabel;
154 SkScalar fContrast;
156 };
157 std::vector<SurfacePropsInput> recs;
158
159 uint32_t fFlags;
160
161 using INHERITED = GM;
162};
163DEF_GM( return new SurfacePropsGM(0); )
165
166#ifdef SK_DEBUG
167static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) {
168 return a == b;
169}
170#endif
171
172class NewSurfaceGM : public skiagm::GM {
173public:
175
176protected:
177 SkString getName() const override { return SkString("surfacenew"); }
178
179 SkISize getISize() override { return SkISize::Make(300, 140); }
180
181 static void drawInto(SkCanvas* canvas) {
182 canvas->drawColor(SK_ColorRED);
183 }
184
185 void onDraw(SkCanvas* canvas) override {
187
188 auto surf(ToolUtils::makeSurface(canvas, info, nullptr));
189 drawInto(surf->getCanvas());
190
191 sk_sp<SkImage> image(surf->makeImageSnapshot());
192 canvas->drawImage(image, 10, 10);
193
194 auto surf2(surf->makeSurface(info));
195 drawInto(surf2->getCanvas());
196
197 // Assert that the props were communicated transitively through the first image
198 SkASSERT(equal(surf->props(), surf2->props()));
199
200 sk_sp<SkImage> image2(surf2->makeImageSnapshot());
201 canvas->drawImage(image2.get(), 10 + SkIntToScalar(image->width()) + 10, 10);
202 }
203
204private:
205 using INHERITED = GM;
206};
207DEF_GM( return new NewSurfaceGM )
208
209///////////////////////////////////////////////////////////////////////////////////////////////////
210
211// The GPU backend may behave differently when images are snapped from wrapped textures and
212// render targets compared.
213namespace {
214enum SurfaceType {
215 kManaged,
218};
219}
220
223 switch (type) {
224 case kManaged:
225 return ToolUtils::makeSurface(canvas, ii);
226 case kBackendTexture:
227 if (!direct) {
228 return nullptr;
229 }
233 ii,
235 1);
236 }
237 return nullptr;
238}
239
240using MakeSurfaceFn = std::function<sk_sp<SkSurface>(const SkImageInfo&)>;
241
242#define DEF_BASIC_SURFACE_TEST(name, canvas, main, W, H) \
243 DEF_SIMPLE_GM(name, canvas, W, H) { \
244 auto make = [canvas](const SkImageInfo& ii) { \
245 return make_surface(ii, canvas, SurfaceType::kManaged); \
246 }; \
247 main(canvas, MakeSurfaceFn(make)); \
248 }
249
250#define DEF_BACKEND_SURFACE_TEST(name, canvas, main, type, W, H) \
251 DEF_SIMPLE_GM_CAN_FAIL(name, canvas, err_msg, W, H) { \
252 GrDirectContext* direct = GrAsDirectContext(canvas->recordingContext()); \
253 if (!direct || direct->abandoned()) { \
254 *err_msg = "Requires non-abandoned GrDirectContext"; \
255 return skiagm::DrawResult::kSkip; \
256 } \
257 auto make = [canvas](const SkImageInfo& ii) { return make_surface(ii, canvas, type); }; \
258 main(canvas, MakeSurfaceFn(make)); \
259 return skiagm::DrawResult::kOk; \
260 }
261
262#define DEF_BET_SURFACE_TEST(name, canvas, main, W, H) \
263 DEF_BACKEND_SURFACE_TEST(SK_MACRO_CONCAT(name, _bet), canvas, main, \
264 SurfaceType::kBackendTexture, W, H)
265
266#define DEF_BERT_SURFACE_TEST(name, canvas, main, W, H) \
267 DEF_BACKEND_SURFACE_TEST(SK_MACRO_CONCAT(name, _bert), canvas, main, \
268 SurfaceType::kBackendRenderTarget, W, H)
269
270// This makes 3 GMs from the same code, normal, wrapped backend texture, and wrapped backend
271// render target.
272#define DEF_SURFACE_TESTS(name, canvas, W, H) \
273 static void SK_MACRO_CONCAT(name, _main)(SkCanvas*, const MakeSurfaceFn&); \
274 DEF_BASIC_SURFACE_TEST(name, canvas, SK_MACRO_CONCAT(name, _main), W, H) \
275 DEF_BET_SURFACE_TEST (name, canvas, SK_MACRO_CONCAT(name, _main), W, H) \
276 DEF_BERT_SURFACE_TEST (name, canvas, SK_MACRO_CONCAT(name, _main), W, H) \
277 static void SK_MACRO_CONCAT(name, _main)(SkCanvas * canvas, const MakeSurfaceFn& make)
278
279DEF_SURFACE_TESTS(copy_on_write_retain, canvas, 256, 256) {
281 sk_sp<SkSurface> surf = make(info);
282
283 surf->getCanvas()->clear(SK_ColorRED);
284 // its important that image survives longer than the next draw, so the surface will see
285 // an outstanding image, and have to decide if it should retain or discard those pixels
287
288 // normally a clear+opaque should trigger the discard optimization, but since we have a clip
289 // it should not (we need the previous red pixels).
290 surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
291 surf->getCanvas()->clear(SK_ColorBLUE);
292
293 // expect to see two rects: blue | red
294 canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
295}
296
297// Like copy_on_write_retain but draws the snapped image back to the surface it was snapped from.
298DEF_SURFACE_TESTS(copy_on_write_retain2, canvas, 256, 256) {
300 sk_sp<SkSurface> surf = make(info);
301
302 surf->getCanvas()->clear(SK_ColorBLUE);
303 // its important that image survives longer than the next draw, so the surface will see
304 // an outstanding image, and have to decide if it should retain or discard those pixels
306
307 surf->getCanvas()->clear(SK_ColorRED);
308 // normally a clear+opaque should trigger the discard optimization, but since we have a clip
309 // it should not (we need the previous red pixels).
310 surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
311 surf->getCanvas()->drawImage(image, 0, 0);
312
313 // expect to see two rects: blue | red
314 canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
315}
316
317DEF_SURFACE_TESTS(simple_snap_image, canvas, 256, 256) {
319 sk_sp<SkSurface> surf = make(info);
320
321 surf->getCanvas()->clear(SK_ColorRED);
323 // expect to see just red
324 canvas->drawImage(std::move(image), 0, 0);
325}
326
327// Like simple_snap_image but the surface dies before the image.
328DEF_SURFACE_TESTS(simple_snap_image2, canvas, 256, 256) {
330 sk_sp<SkSurface> surf = make(info);
331
332 surf->getCanvas()->clear(SK_ColorRED);
334 surf.reset();
335 // expect to see just red
336 canvas->drawImage(std::move(image), 0, 0);
337}
338
339DEF_SIMPLE_GM(snap_with_mips, canvas, 80, 75) {
340 auto ct = canvas->imageInfo().colorType() == kUnknown_SkColorType
342 : canvas->imageInfo().colorType();
343 auto ii = SkImageInfo::Make({32, 32},
344 ct,
346 canvas->imageInfo().refColorSpace());
347 auto surface = SkSurfaces::Raster(ii);
348
349 auto nextImage = [&](SkColor color) {
350 surface->getCanvas()->clear(color);
352 paint.setColor(~color | 0xFF000000);
353 surface->getCanvas()->drawRect(SkRect::MakeLTRB(surface->width() *2/5.f,
354 surface->height()*2/5.f,
355 surface->width() *3/5.f,
356 surface->height()*3/5.f),
357 paint);
358 return surface->makeImageSnapshot()->withDefaultMipmaps();
359 };
360
361 static constexpr int kPad = 8;
363
364 canvas->save();
365 for (int y = 0; y < 3; ++y) {
366 canvas->save();
367 SkColor kColors[] = {0xFFF0F0F0, SK_ColorBLUE};
368 for (int x = 0; x < 2; ++x) {
369 auto image = nextImage(kColors[x]);
370 canvas->drawImage(image, 0, 0, kSampling);
371 canvas->translate(ii.width() + kPad, 0);
372 }
373 canvas->restore();
374 canvas->translate(0, ii.width() + kPad);
375 canvas->scale(.4f, .4f);
376 }
377 canvas->restore();
378}
379
380DEF_SURFACE_TESTS(copy_on_write_savelayer, canvas, 256, 256) {
382 sk_sp<SkSurface> surf = make(info);
383 surf->getCanvas()->clear(SK_ColorRED);
384 // its important that image survives longer than the next draw, so the surface will see
385 // an outstanding image, and have to decide if it should retain or discard those pixels
387
388 // now draw into a full-screen layer. This should (a) trigger a copy-on-write, but it should
389 // not trigger discard, even tho its alpha (SK_ColorBLUE) is opaque, since it is in a layer
390 // with a non-opaque paint.
392 paint.setAlphaf(0.25f);
393 surf->getCanvas()->saveLayer({0, 0, 256, 256}, &paint);
394 surf->getCanvas()->clear(SK_ColorBLUE);
395 surf->getCanvas()->restore();
396
397 // expect to see two rects: blue blended on red
398 canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
399}
400
401DEF_SURFACE_TESTS(surface_underdraw, canvas, 256, 256) {
402 SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256, nullptr);
403 auto surf = make(info);
404
405 const SkIRect subset = SkIRect::MakeLTRB(180, 0, 256, 256);
406
407 // noisy background
408 {
409 SkPoint pts[] = {{0, 0}, {40, 50}};
413 paint.setShader(sh);
414 surf->getCanvas()->drawPaint(paint);
415 }
416
417 // save away the right-hand strip, then clear it
418 sk_sp<SkImage> saveImg = surf->makeImageSnapshot(subset);
419 {
421 paint.setBlendMode(SkBlendMode::kClear);
422 surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
423 }
424
425 // draw the "foreground"
426 {
428 paint.setColor(SK_ColorGREEN);
429 SkRect r = { 0, 10, 256, 35 };
430 while (r.fBottom < 256) {
431 surf->getCanvas()->drawRect(r, paint);
432 r.offset(0, r.height() * 2);
433 }
434 }
435
436 // apply the "fade"
437 {
438 SkPoint pts[] = {{SkIntToScalar(subset.left()), 0}, {SkIntToScalar(subset.right()), 0}};
439 SkColor colors[] = {0xFF000000, 0};
442 paint.setShader(sh);
443 paint.setBlendMode(SkBlendMode::kDstIn);
444 surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
445 }
446
447 // restore the original strip, drawing it "under" the current foreground
448 {
450 paint.setBlendMode(SkBlendMode::kDstOver);
451 surf->getCanvas()->drawImage(saveImg,
452 SkIntToScalar(subset.left()), SkIntToScalar(subset.top()),
454 }
455
456 // show it on screen
457 surf->draw(canvas, 0, 0);
458}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
@ kBackendRenderTarget
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
@ kTopLeft_GrSurfaceOrigin
Definition: GrTypes.h:148
static bool equal(const SkBitmap &a, const SkBitmap &b)
Definition: ImageTest.cpp:1395
ScalarValue fGamma
@ kOpaque_SkAlphaType
pixel is opaque
Definition: SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
#define SkASSERT(cond)
Definition: SkAssert.h:116
@ kDstIn
r = d * sa
@ kDstOver
r = d + (1-da)*s
@ kClear
r = 0
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
@ kUnknown_SkColorType
uninitialized
Definition: SkColorType.h:20
uint32_t SkColor
Definition: SkColor.h:37
#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
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
#define SK_DEBUG
#define SkIntToScalar(x)
Definition: SkScalar.h:57
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
SkPixelGeometry
@ kUnknown_SkPixelGeometry
@ kRGB_V_SkPixelGeometry
@ kBGR_H_SkPixelGeometry
@ kRGB_H_SkPixelGeometry
@ kBGR_V_SkPixelGeometry
#define SK_GAMMA_CONTRAST
Definition: SkTypes.h:94
#define SK_GAMMA_EXPONENT
Definition: SkTypes.h:86
constexpr int kPad
GLenum type
SkISize getISize() override
Definition: surface.cpp:179
SkString getName() const override
Definition: surface.cpp:177
void onDraw(SkCanvas *canvas) override
Definition: surface.cpp:185
static void drawInto(SkCanvas *canvas)
Definition: surface.cpp:181
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
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 drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition: SkCanvas.h:1182
virtual GrRecordingContext * recordingContext() const
Definition: SkCanvas.cpp:1637
virtual skgpu::graphite::Recorder * recorder() const
Definition: SkCanvas.cpp:1641
void clear(SkColor color)
Definition: SkCanvas.h:1199
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
Definition: SkFont.h:35
@ kSubpixelAntiAlias
glyph positioned in pixel using transparency
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
int width() const
Definition: SkImage.h:285
@ kUseDeviceIndependentFonts_Flag
SkCanvas * getCanvas()
Definition: SkSurface.cpp:82
sk_sp< SkImage > makeImageSnapshot()
Definition: SkSurface.cpp:90
static void DrawString(SkCanvas *canvas, const char text[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint, Align align=kLeft_Align)
Definition: SkTextUtils.h:34
SurfacePropsGM(uint32_t flags)
Definition: surface.cpp:91
SkISize getISize() override
Definition: surface.cpp:126
SkString getName() const override
Definition: surface.cpp:121
void onDraw(SkCanvas *canvas) override
Definition: surface.cpp:128
T * get() const
Definition: SkRefCnt.h:303
void reset(T *ptr=nullptr)
Definition: SkRefCnt.h:310
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
const Paint & paint
Definition: color_source.cc:38
DlColor color
VkSurfaceKHR surface
Definition: main.cc:49
float SkScalar
Definition: extension.cpp:12
static bool b
struct MyStruct a[10]
FlutterSemanticsFlag flags
#define DEF_GM(CODE)
Definition: gm.h:40
double y
double x
sk_sp< const SkImage > image
Definition: SkRecords.h:269
PODArray< SkColor > colors
Definition: SkRecords.h:276
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
sk_sp< SkTypeface > DefaultPortableTypeface()
sk_sp< SkSurface > makeSurface(SkCanvas *canvas, const SkImageInfo &info, const SkSurfaceProps *props)
Definition: ToolUtils.cpp:512
const DlColor kColors[]
font
Font Metadata and Metrics.
sh
Definition: run_sh.py:10
sk_sp< SkSurface > MakeBackendRenderTargetSurface(GrDirectContext *dContext, const SkImageInfo &ii, GrSurfaceOrigin origin, int sampleCnt, GrProtected isProtected, const SkSurfaceProps *props)
sk_sp< SkSurface > MakeBackendTextureSurface(GrDirectContext *dContext, const SkImageInfo &ii, GrSurfaceOrigin origin, int sampleCnt, skgpu::Mipmapped mipmapped, GrProtected isProtected, const SkSurfaceProps *props)
static void make(SkBitmap *bitmap, SkColorType colorType, SkAlphaType alphaType, sk_sp< SkColorSpace > colorSpace)
Definition: encode_srgb.cpp:35
Definition: SkMD5.cpp:130
Definition: SkRect.h:32
constexpr int32_t top() const
Definition: SkRect.h:120
static constexpr SkIRect MakeLTRB(int32_t l, int32_t t, int32_t r, int32_t b)
Definition: SkRect.h:91
constexpr int32_t right() const
Definition: SkRect.h:127
constexpr int32_t left() const
Definition: SkRect.h:113
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkImageInfo MakeN32Premul(int width, int height)
static SkImageInfo MakeN32(int width, int height, SkAlphaType at)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669
SkScalar fBottom
larger y-axis bounds
Definition: extension.cpp:17
void offset(float dx, float dy)
Definition: SkRect.h:1016
constexpr float height() const
Definition: SkRect.h:769
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
DEF_SIMPLE_GM(snap_with_mips, canvas, 80, 75)
Definition: surface.cpp:339
std::function< sk_sp< SkSurface >(const SkImageInfo &)> MakeSurfaceFn
Definition: surface.cpp:240
#define W
Definition: surface.cpp:41
#define DEF_SURFACE_TESTS(name, canvas, W, H)
Definition: surface.cpp:272
static void test_draw(SkCanvas *canvas, const char label[])
Definition: surface.cpp:72
static sk_sp< SkShader > make_shader()
Definition: surface.cpp:44
#define H
Definition: surface.cpp:42
static sk_sp< SkSurface > make_surface(GrRecordingContext *ctx, skgpu::graphite::Recorder *recorder, const SkImageInfo &info, uint32_t flags, SkPixelGeometry geo, SkScalar contrast, SkScalar gamma)
Definition: surface.cpp:52