Flutter Engine
The Flutter Engine
Classes | Macros | Typedefs | Functions
surface.cpp File Reference
#include "gm/gm.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkFont.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkShader.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
#include "include/core/SkSurfaceProps.h"
#include "include/core/SkTileMode.h"
#include "include/core/SkTypeface.h"
#include "include/core/SkTypes.h"
#include "include/effects/SkGradientShader.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/GrRecordingContext.h"
#include "include/gpu/ganesh/SkSurfaceGanesh.h"
#include "include/utils/SkTextUtils.h"
#include "tools/ToolUtils.h"
#include "tools/fonts/FontToolUtils.h"
#include "tools/gpu/BackendSurfaceFactory.h"

Go to the source code of this file.

Classes

class  SurfacePropsGM
 
class  NewSurfaceGM
 

Macros

#define W   800
 
#define H   100
 
#define DEF_BASIC_SURFACE_TEST(name, canvas, main, W, H)
 
#define DEF_BACKEND_SURFACE_TEST(name, canvas, main, type, W, H)
 
#define DEF_BET_SURFACE_TEST(name, canvas, main, W, H)
 
#define DEF_BERT_SURFACE_TEST(name, canvas, main, W, H)
 
#define DEF_SURFACE_TESTS(name, canvas, W, H)
 

Typedefs

using MakeSurfaceFn = std::function< sk_sp< SkSurface >(const SkImageInfo &)>
 

Functions

static sk_sp< SkShadermake_shader ()
 
static sk_sp< SkSurfacemake_surface (GrRecordingContext *ctx, skgpu::graphite::Recorder *recorder, const SkImageInfo &info, uint32_t flags, SkPixelGeometry geo, SkScalar contrast, SkScalar gamma)
 
static void test_draw (SkCanvas *canvas, const char label[])
 
static sk_sp< SkSurfacemake_surface (const SkImageInfo &ii, SkCanvas *canvas, SurfaceType type)
 
 DEF_SURFACE_TESTS (copy_on_write_retain, canvas, 256, 256)
 
 DEF_SURFACE_TESTS (copy_on_write_retain2, canvas, 256, 256)
 
 DEF_SURFACE_TESTS (simple_snap_image, canvas, 256, 256)
 
 DEF_SURFACE_TESTS (simple_snap_image2, canvas, 256, 256)
 
 DEF_SIMPLE_GM (snap_with_mips, canvas, 80, 75)
 
 DEF_SURFACE_TESTS (copy_on_write_savelayer, canvas, 256, 256)
 
 DEF_SURFACE_TESTS (surface_underdraw, canvas, 256, 256)
 

Macro Definition Documentation

◆ DEF_BACKEND_SURFACE_TEST

#define DEF_BACKEND_SURFACE_TEST (   name,
  canvas,
  main,
  type,
  W,
  H 
)
Value:
DEF_SIMPLE_GM_CAN_FAIL(name, canvas, err_msg, W, H) { \
GrDirectContext* direct = GrAsDirectContext(canvas->recordingContext()); \
if (!direct || direct->abandoned()) { \
*err_msg = "Requires non-abandoned GrDirectContext"; \
} \
auto make = [canvas](const SkImageInfo& ii) { return make_surface(ii, canvas, type); }; \
main(canvas, MakeSurfaceFn(make)); \
}
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
GLenum type
#define DEF_SIMPLE_GM_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H)
Definition: gm.h:62
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
static void make(SkBitmap *bitmap, SkColorType colorType, SkAlphaType alphaType, sk_sp< SkColorSpace > colorSpace)
Definition: encode_srgb.cpp:35
Definition: SkMD5.cpp:130
std::function< sk_sp< SkSurface >(const SkImageInfo &)> MakeSurfaceFn
Definition: surface.cpp:240
#define W
Definition: surface.cpp:41
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

Definition at line 250 of file surface.cpp.

◆ DEF_BASIC_SURFACE_TEST

#define DEF_BASIC_SURFACE_TEST (   name,
  canvas,
  main,
  W,
  H 
)
Value:
DEF_SIMPLE_GM(name, canvas, W, H) { \
auto make = [canvas](const SkImageInfo& ii) { \
return make_surface(ii, canvas, SurfaceType::kManaged); \
}; \
main(canvas, MakeSurfaceFn(make)); \
}
DEF_SIMPLE_GM(snap_with_mips, canvas, 80, 75)
Definition: surface.cpp:339

Definition at line 242 of file surface.cpp.

◆ DEF_BERT_SURFACE_TEST

#define DEF_BERT_SURFACE_TEST (   name,
  canvas,
  main,
  W,
  H 
)
Value:
@ kBackendRenderTarget
#define SK_MACRO_CONCAT(X, Y)
Definition: SkMacros.h:16
Definition: main.py:1
#define DEF_BACKEND_SURFACE_TEST(name, canvas, main, type, W, H)
Definition: surface.cpp:250

Definition at line 266 of file surface.cpp.

◆ DEF_BET_SURFACE_TEST

#define DEF_BET_SURFACE_TEST (   name,
  canvas,
  main,
  W,
  H 
)
Value:

Definition at line 262 of file surface.cpp.

◆ DEF_SURFACE_TESTS

#define DEF_SURFACE_TESTS (   name,
  canvas,
  W,
  H 
)
Value:
static void SK_MACRO_CONCAT(name, _main)(SkCanvas*, const MakeSurfaceFn&); \
DEF_BASIC_SURFACE_TEST(name, canvas, SK_MACRO_CONCAT(name, _main), W, H) \
DEF_BET_SURFACE_TEST (name, canvas, SK_MACRO_CONCAT(name, _main), W, H) \
DEF_BERT_SURFACE_TEST (name, canvas, SK_MACRO_CONCAT(name, _main), W, H) \
static void SK_MACRO_CONCAT(name, _main)(SkCanvas * canvas, const MakeSurfaceFn& make)

Definition at line 272 of file surface.cpp.

◆ H

#define H   100

Definition at line 42 of file surface.cpp.

◆ W

#define W   800

Definition at line 41 of file surface.cpp.

Typedef Documentation

◆ MakeSurfaceFn

Definition at line 240 of file surface.cpp.

Function Documentation

◆ DEF_SIMPLE_GM()

DEF_SIMPLE_GM ( snap_with_mips  ,
canvas  ,
80  ,
75   
)

Definition at line 339 of file surface.cpp.

339 {
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}
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ 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
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr int kPad
const Paint & paint
Definition: color_source.cc:38
DlColor color
VkSurfaceKHR surface
Definition: main.cc:49
double y
double x
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
const DlColor kColors[]
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646

◆ DEF_SURFACE_TESTS() [1/6]

DEF_SURFACE_TESTS ( copy_on_write_retain  ,
canvas  ,
256  ,
256   
)

Definition at line 279 of file surface.cpp.

279 {
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}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void clear(SkColor color)
Definition: SkCanvas.h:1199
SkCanvas * getCanvas()
Definition: SkSurface.cpp:82
sk_sp< SkImage > makeImageSnapshot()
Definition: SkSurface.cpp:90
static SkImageInfo MakeN32Premul(int width, int height)
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609

◆ DEF_SURFACE_TESTS() [2/6]

DEF_SURFACE_TESTS ( copy_on_write_retain2  ,
canvas  ,
256  ,
256   
)

Definition at line 298 of file surface.cpp.

298 {
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}
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528

◆ DEF_SURFACE_TESTS() [3/6]

DEF_SURFACE_TESTS ( copy_on_write_savelayer  ,
canvas  ,
256  ,
256   
)

Definition at line 380 of file surface.cpp.

380 {
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}
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
void restore()
Definition: SkCanvas.cpp:461

◆ DEF_SURFACE_TESTS() [4/6]

DEF_SURFACE_TESTS ( simple_snap_image  ,
canvas  ,
256  ,
256   
)

Definition at line 317 of file surface.cpp.

317 {
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}

◆ DEF_SURFACE_TESTS() [5/6]

DEF_SURFACE_TESTS ( simple_snap_image2  ,
canvas  ,
256  ,
256   
)

Definition at line 328 of file surface.cpp.

328 {
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}
void reset(T *ptr=nullptr)
Definition: SkRefCnt.h:310

◆ DEF_SURFACE_TESTS() [6/6]

DEF_SURFACE_TESTS ( surface_underdraw  ,
canvas  ,
256  ,
256   
)

Definition at line 401 of file surface.cpp.

401 {
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}
@ kDstIn
r = d * sa
@ kDstOver
r = d + (1-da)*s
@ kClear
r = 0
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
#define SkIntToScalar(x)
Definition: SkScalar.h:57
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)
PODArray< SkColor > colors
Definition: SkRecords.h:276
sh
Definition: run_sh.py:10
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
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

◆ make_shader()

static sk_sp< SkShader > make_shader ( )
static

Definition at line 44 of file surface.cpp.

44 {
45 int a = 0x99;
46 int b = 0xBB;
47 SkPoint pts[] = { { 0, 0 }, { W, H } };
50}
#define SkColorSetRGB(r, g, b)
Definition: SkColor.h:57
static bool b
struct MyStruct a[10]

◆ make_surface() [1/2]

static sk_sp< SkSurface > make_surface ( const SkImageInfo ii,
SkCanvas canvas,
SurfaceType  type 
)
static

Definition at line 221 of file surface.cpp.

221 {
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}
@ kTopLeft_GrSurfaceOrigin
Definition: GrTypes.h:148
virtual GrRecordingContext * recordingContext() const
Definition: SkCanvas.cpp:1637
sk_sp< SkSurface > makeSurface(SkCanvas *canvas, const SkImageInfo &info, const SkSurfaceProps *props)
Definition: ToolUtils.cpp:512
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)

◆ make_surface() [2/2]

static sk_sp< SkSurface > make_surface ( GrRecordingContext ctx,
skgpu::graphite::Recorder recorder,
const SkImageInfo info,
uint32_t  flags,
SkPixelGeometry  geo,
SkScalar  contrast,
SkScalar  gamma 
)
static

Definition at line 52 of file surface.cpp.

58 {
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}
FlutterSemanticsFlag flags
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)

◆ test_draw()

static void test_draw ( SkCanvas canvas,
const char  label[] 
)
static

Definition at line 72 of file surface.cpp.

72 {
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}
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
Definition: SkFont.h:35
@ kSubpixelAntiAlias
glyph positioned in pixel using transparency
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
sk_sp< SkTypeface > DefaultPortableTypeface()
font
Font Metadata and Metrics.
static sk_sp< SkShader > make_shader()
Definition: surface.cpp:44