Flutter Engine
The Flutter Engine
imagemasksubset.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 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"
15#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
22#include "tools/GpuToolUtils.h"
23#include "tools/ToolUtils.h"
24
25#if defined(SK_GRAPHITE)
27#endif
28
29namespace {
30
31const SkISize kSize = SkISize::Make(100, 100);
32const SkIRect kSubset = SkIRect::MakeLTRB(25, 25, 75, 75);
33const SkRect kDest = SkRect::MakeXYWH(10, 10, 100, 100);
34
35sk_sp<SkImage> make_mask(const sk_sp<SkSurface>& surface) {
36 ToolUtils::draw_checkerboard(surface->getCanvas(), 0x80808080, 0x00000000, 5);
37 return surface->makeImageSnapshot();
38}
39
40class MaskGenerator final : public SkImageGenerator {
41public:
42 MaskGenerator(const SkImageInfo& info) : INHERITED(info) {}
43
44 bool onGetPixels(const SkImageInfo& info, void* pixels,
45 size_t rowBytes, const Options&) override {
46 SkImageInfo surfaceInfo = info;
47 if (kAlpha_8_SkColorType == info.colorType()) {
48 surfaceInfo = surfaceInfo.makeColorSpace(nullptr);
49 }
50
51 make_mask(SkSurfaces::WrapPixels(surfaceInfo, pixels, rowBytes));
52 return true;
53 }
54
55private:
57};
58
59using MakerT = sk_sp<SkImage>(*)(SkCanvas*, const SkImageInfo&);
60const MakerT makers[] = {
61 // SkImage_Raster
62 [](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> {
63 return make_mask(SkSurfaces::Raster(info));
64 },
65
66 // SkImage_Ganesh
67 [](SkCanvas* c, const SkImageInfo& info) -> sk_sp<SkImage> {
69 if (c->recordingContext()) {
70 surface =
72 } else {
73#if defined(SK_GRAPHITE)
75#endif
76 }
77 return make_mask(surface ? surface : SkSurfaces::Raster(info));
78 },
79
80 // SkImage_Lazy
81 [](SkCanvas*, const SkImageInfo& info) -> sk_sp<SkImage> {
82 return SkImages::DeferredFromGenerator(std::make_unique<MaskGenerator>(info));
83 },
84};
85
86} // namespace
87
88// Checks whether subset SkImages preserve the original color type (A8 in this case).
89DEF_SIMPLE_GM(imagemasksubset, canvas, 480, 480) {
91 paint.setColor(0xff00ff00);
92
93 const SkImageInfo info = SkImageInfo::MakeA8(kSize.width(), kSize.height());
94
95 for (size_t i = 0; i < std::size(makers); ++i) {
96 sk_sp<SkImage> image = ToolUtils::MakeTextureImage(canvas, makers[i](canvas, info));
97 if (image) {
98 canvas->drawImageRect(image, SkRect::Make(kSubset), kDest, SkSamplingOptions(),
100 sk_sp<SkImage> subset;
101
102 if (auto direct = GrAsDirectContext(canvas->recordingContext())) {
103 subset = image->makeSubset(direct, kSubset);
104 } else {
105#if defined(SK_GRAPHITE)
106 subset = image->makeSubset(canvas->recorder(), kSubset, {});
107#endif
108 }
109
110 canvas->drawImageRect(subset, kDest.makeOffset(kSize.width() * 1.5f, 0),
112 }
113 canvas->translate(0, kSize.height() * 1.5f);
114 }
115}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition: SkColorType.h:21
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
virtual GrRecordingContext * recordingContext() const
Definition: SkCanvas.cpp:1637
virtual skgpu::graphite::Recorder * recorder() const
Definition: SkCanvas.cpp:1641
@ kStrict_SrcRectConstraint
sample only inside bounds; slower
Definition: SkCanvas.h:1542
SkImageGenerator(const SkImageInfo &info, uint32_t uniqueId=kNeedNewImageUniqueID)
virtual bool onGetPixels(const SkImageInfo &, void *, size_t, const Options &)
virtual sk_sp< SkImage > makeSubset(GrDirectContext *direct, const SkIRect &subset) const =0
const Paint & paint
Definition: color_source.cc:38
VkSurfaceKHR surface
Definition: main.cc:49
DEF_SIMPLE_GM(imagemasksubset, canvas, 480, 480)
constexpr int kSize
SK_API sk_sp< SkImage > DeferredFromGenerator(std::unique_ptr< SkImageGenerator > imageGenerator)
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SK_API sk_sp< SkSurface > WrapPixels(const SkImageInfo &imageInfo, void *pixels, size_t rowBytes, const SkSurfaceProps *surfaceProps=nullptr)
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)
void draw_checkerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)
Definition: ToolUtils.cpp:174
sk_sp< SkImage > MakeTextureImage(SkCanvas *canvas, sk_sp< SkImage > orig)
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
SkSamplingOptions(SkFilterMode::kLinear))
Definition: SkRect.h:32
static constexpr SkIRect MakeLTRB(int32_t l, int32_t t, int32_t r, int32_t b)
Definition: SkRect.h:91
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
SkImageInfo makeColorSpace(sk_sp< SkColorSpace > cs) const
static SkImageInfo MakeA8(int width, int height)
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669
constexpr SkRect makeOffset(float dx, float dy) const
Definition: SkRect.h:965
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659