Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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, const SkImageInfo &info, 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)
bool abandoned() override
const char * name
Definition fuchsia.cc:50
#define DEF_SIMPLE_GM_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H)
Definition gm.h:62
static sk_sp< SkImage > make(sk_sp< SkColorSpace > cs)
Definition mipmap.cpp:65
Definition SkMD5.cpp:130
std::function< sk_sp< SkSurface >(const SkImageInfo &)> MakeSurfaceFn
Definition surface.cpp:222
#define W
Definition surface.cpp:38
static sk_sp< SkSurface > make_surface(GrRecordingContext *ctx, const SkImageInfo &info, SkPixelGeometry geo, SkScalar contrast, SkScalar gamma)
Definition surface.cpp:49

Definition at line 232 of file surface.cpp.

233 { \
234 GrDirectContext* direct = GrAsDirectContext(canvas->recordingContext()); \
235 if (!direct || direct->abandoned()) { \
236 *err_msg = "Requires non-abandoned GrDirectContext"; \
238 } \
239 auto make = [canvas](const SkImageInfo& ii) { return make_surface(ii, canvas, type); }; \
240 main(canvas, MakeSurfaceFn(make)); \
242 }

◆ 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)); \
}
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50

Definition at line 224 of file surface.cpp.

225 { \
226 auto make = [canvas](const SkImageInfo& ii) { \
227 return make_surface(ii, canvas, SurfaceType::kManaged); \
228 }; \
229 main(canvas, MakeSurfaceFn(make)); \
230 }

◆ 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:232

Definition at line 248 of file surface.cpp.

◆ DEF_BET_SURFACE_TEST

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

Definition at line 244 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 254 of file surface.cpp.

◆ H

#define H   100

Definition at line 39 of file surface.cpp.

◆ W

#define W   800

Definition at line 38 of file surface.cpp.

Typedef Documentation

◆ MakeSurfaceFn

Definition at line 222 of file surface.cpp.

Function Documentation

◆ DEF_SIMPLE_GM()

DEF_SIMPLE_GM ( snap_with_mips  ,
canvas  ,
80  ,
75   
)

Definition at line 321 of file surface.cpp.

321 {
322 auto ct = canvas->imageInfo().colorType() == kUnknown_SkColorType
324 : canvas->imageInfo().colorType();
325 auto ii = SkImageInfo::Make({32, 32},
326 ct,
328 canvas->imageInfo().refColorSpace());
329 auto surface = SkSurfaces::Raster(ii);
330
331 auto nextImage = [&](SkColor color) {
332 surface->getCanvas()->clear(color);
334 paint.setColor(~color | 0xFF000000);
335 surface->getCanvas()->drawRect(SkRect::MakeLTRB(surface->width() *2/5.f,
336 surface->height()*2/5.f,
337 surface->width() *3/5.f,
338 surface->height()*3/5.f),
339 paint);
340 return surface->makeImageSnapshot()->withDefaultMipmaps();
341 };
342
343 static constexpr int kPad = 8;
345
346 canvas->save();
347 for (int y = 0; y < 3; ++y) {
348 canvas->save();
349 SkColor kColors[] = {0xFFF0F0F0, SK_ColorBLUE};
350 for (int x = 0; x < 2; ++x) {
351 auto image = nextImage(kColors[x]);
352 canvas->drawImage(image, 0, 0, kSampling);
353 canvas->translate(ii.width() + kPad, 0);
354 }
355 canvas->restore();
356 canvas->translate(0, ii.width() + kPad);
357 canvas->scale(.4f, .4f);
358 }
359 canvas->restore();
360}
SkColor4f color
@ 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
VkSurfaceKHR surface
Definition main.cc:49
sk_sp< SkImage > image
Definition examples.cpp:29
double y
double x
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 261 of file surface.cpp.

261 {
263 sk_sp<SkSurface> surf = make(info);
264
265 surf->getCanvas()->clear(SK_ColorRED);
266 // its important that image survives longer than the next draw, so the surface will see
267 // an outstanding image, and have to decide if it should retain or discard those pixels
268 sk_sp<SkImage> image = surf->makeImageSnapshot();
269
270 // normally a clear+opaque should trigger the discard optimization, but since we have a clip
271 // it should not (we need the previous red pixels).
272 surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
273 surf->getCanvas()->clear(SK_ColorBLUE);
274
275 // expect to see two rects: blue | red
276 canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
277}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
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 280 of file surface.cpp.

280 {
282 sk_sp<SkSurface> surf = make(info);
283
284 surf->getCanvas()->clear(SK_ColorBLUE);
285 // its important that image survives longer than the next draw, so the surface will see
286 // an outstanding image, and have to decide if it should retain or discard those pixels
287 sk_sp<SkImage> image = surf->makeImageSnapshot();
288
289 surf->getCanvas()->clear(SK_ColorRED);
290 // normally a clear+opaque should trigger the discard optimization, but since we have a clip
291 // it should not (we need the previous red pixels).
292 surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
293 surf->getCanvas()->drawImage(image, 0, 0);
294
295 // expect to see two rects: blue | red
296 canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
297}

◆ DEF_SURFACE_TESTS() [3/6]

DEF_SURFACE_TESTS ( copy_on_write_savelayer  ,
canvas  ,
256  ,
256   
)

Definition at line 362 of file surface.cpp.

362 {
364 sk_sp<SkSurface> surf = make(info);
365 surf->getCanvas()->clear(SK_ColorRED);
366 // its important that image survives longer than the next draw, so the surface will see
367 // an outstanding image, and have to decide if it should retain or discard those pixels
368 sk_sp<SkImage> image = surf->makeImageSnapshot();
369
370 // now draw into a full-screen layer. This should (a) trigger a copy-on-write, but it should
371 // not trigger discard, even tho its alpha (SK_ColorBLUE) is opaque, since it is in a layer
372 // with a non-opaque paint.
374 paint.setAlphaf(0.25f);
375 surf->getCanvas()->saveLayer({0, 0, 256, 256}, &paint);
376 surf->getCanvas()->clear(SK_ColorBLUE);
377 surf->getCanvas()->restore();
378
379 // expect to see two rects: blue blended on red
380 canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
381}

◆ DEF_SURFACE_TESTS() [4/6]

DEF_SURFACE_TESTS ( simple_snap_image  ,
canvas  ,
256  ,
256   
)

Definition at line 299 of file surface.cpp.

299 {
301 sk_sp<SkSurface> surf = make(info);
302
303 surf->getCanvas()->clear(SK_ColorRED);
304 sk_sp<SkImage> image = surf->makeImageSnapshot();
305 // expect to see just red
306 canvas->drawImage(std::move(image), 0, 0);
307}

◆ DEF_SURFACE_TESTS() [5/6]

DEF_SURFACE_TESTS ( simple_snap_image2  ,
canvas  ,
256  ,
256   
)

Definition at line 310 of file surface.cpp.

310 {
312 sk_sp<SkSurface> surf = make(info);
313
314 surf->getCanvas()->clear(SK_ColorRED);
315 sk_sp<SkImage> image = surf->makeImageSnapshot();
316 surf.reset();
317 // expect to see just red
318 canvas->drawImage(std::move(image), 0, 0);
319}
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 383 of file surface.cpp.

383 {
384 SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256, nullptr);
385 auto surf = make(info);
386
387 const SkIRect subset = SkIRect::MakeLTRB(180, 0, 256, 256);
388
389 // noisy background
390 {
391 SkPoint pts[] = {{0, 0}, {40, 50}};
393 auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kRepeat);
395 paint.setShader(sh);
396 surf->getCanvas()->drawPaint(paint);
397 }
398
399 // save away the right-hand strip, then clear it
400 sk_sp<SkImage> saveImg = surf->makeImageSnapshot(subset);
401 {
403 paint.setBlendMode(SkBlendMode::kClear);
404 surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
405 }
406
407 // draw the "foreground"
408 {
410 paint.setColor(SK_ColorGREEN);
411 SkRect r = { 0, 10, 256, 35 };
412 while (r.fBottom < 256) {
413 surf->getCanvas()->drawRect(r, paint);
414 r.offset(0, r.height() * 2);
415 }
416 }
417
418 // apply the "fade"
419 {
420 SkPoint pts[] = {{SkIntToScalar(subset.left()), 0}, {SkIntToScalar(subset.right()), 0}};
421 SkColor colors[] = {0xFF000000, 0};
422 auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
424 paint.setShader(sh);
425 paint.setBlendMode(SkBlendMode::kDstIn);
426 surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
427 }
428
429 // restore the original strip, drawing it "under" the current foreground
430 {
432 paint.setBlendMode(SkBlendMode::kDstOver);
433 surf->getCanvas()->drawImage(saveImg,
434 SkIntToScalar(subset.left()), SkIntToScalar(subset.top()),
436 }
437
438 // show it on screen
439 surf->draw(canvas, 0, 0);
440}
@ 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
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 41 of file surface.cpp.

41 {
42 int a = 0x99;
43 int b = 0xBB;
44 SkPoint pts[] = { { 0, 0 }, { W, H } };
46 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
47}
#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 203 of file surface.cpp.

203 {
205 switch (type) {
206 case kManaged:
207 return ToolUtils::makeSurface(canvas, ii);
208 case kBackendTexture:
209 if (!direct) {
210 return nullptr;
211 }
215 ii,
217 1);
218 }
219 return nullptr;
220}
@ kTopLeft_GrSurfaceOrigin
Definition GrTypes.h:148
virtual GrRecordingContext * recordingContext() const
sk_sp< SkSurface > makeSurface(SkCanvas *canvas, const SkImageInfo &info, const SkSurfaceProps *props)
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,
const SkImageInfo info,
SkPixelGeometry  geo,
SkScalar  contrast,
SkScalar  gamma 
)
static

Definition at line 49 of file surface.cpp.

53 {
54 SkSurfaceProps props(0, geo, contrast, gamma);
55 if (ctx) {
56 return SkSurfaces::RenderTarget(ctx, skgpu::Budgeted::kNo, info, 0, &props);
57 } else {
58 return SkSurfaces::Raster(info, &props);
59 }
60}
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 62 of file surface.cpp.

62 {
64
65 paint.setAntiAlias(true);
66 paint.setDither(true);
67
68 paint.setShader(make_shader());
69 canvas->drawRect(SkRect::MakeWH(W, H), paint);
70 paint.setShader(nullptr);
71
72 paint.setColor(SK_ColorWHITE);
75 SkTextUtils::DrawString(canvas, label, W / 2, H * 3 / 4, font, paint,
77}
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
void drawRect(const SkRect &rect, const SkPaint &paint)
@ 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:41