Flutter Engine
The Flutter Engine
Classes | Typedefs | Functions | Variables
image_shader.cpp File Reference
#include "gm/gm.h"
#include "include/codec/SkEncodedImageFormat.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkData.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPicture.h"
#include "include/core/SkPictureRecorder.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkShader.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTileMode.h"
#include "include/core/SkTypes.h"
#include "include/encode/SkPngEncoder.h"
#include "include/gpu/GpuTypes.h"
#include "include/gpu/ganesh/SkSurfaceGanesh.h"
#include <utility>
#include "tools/ToolUtils.h"

Go to the source code of this file.

Classes

class  ImageShaderGM
 

Typedefs

typedef sk_sp< SkImage >(* ImageMakerProc) (GrRecordingContext *, SkPicture *, const SkImageInfo &)
 

Functions

static void draw_something (SkCanvas *canvas, const SkRect &bounds)
 
static sk_sp< SkImagemake_raster (GrRecordingContext *, SkPicture *pic, const SkImageInfo &info)
 
static sk_sp< SkImagemake_texture (GrRecordingContext *ctx, SkPicture *pic, const SkImageInfo &info)
 
static sk_sp< SkImagemake_pict_gen (GrRecordingContext *, SkPicture *pic, const SkImageInfo &info)
 
static sk_sp< SkImagemake_encode_gen (GrRecordingContext *ctx, SkPicture *pic, const SkImageInfo &info)
 
static sk_sp< SkImagemake_checker_img (int w, int h, SkColor c0, SkColor c1, int size)
 
 DEF_SIMPLE_GM (drawimage_sampling, canvas, 500, 500)
 
 DEF_SIMPLE_GM (textureimage_and_shader, canvas, 100, 50)
 

Variables

const ImageMakerProc gProcs []
 

Typedef Documentation

◆ ImageMakerProc

typedef sk_sp< SkImage >(* ImageMakerProc) (GrRecordingContext *, SkPicture *, const SkImageInfo &)

Definition at line 47 of file image_shader.cpp.

Function Documentation

◆ DEF_SIMPLE_GM() [1/2]

DEF_SIMPLE_GM ( drawimage_sampling  ,
canvas  ,
500  ,
500   
)

Definition at line 170 of file image_shader.cpp.

170 {
171 constexpr int N = 256;
172 constexpr float kScale = 1.0f/6;
173 const SkRect dst = {0, 0, kScale*N, kScale*N};
174
176 const SkRect src = SkRect::MakeIWH(img->width(), img->height());
177
179
181
185
186 canvas->save();
187
188 canvas->save();
189 canvas->concat(mx);
190 canvas->drawImage(img.get(), 0, 0, sampling);
191 canvas->restore();
192
193 canvas->translate(dst.width() + 4, 0);
194
195 paint.setShader(img->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, sampling, &mx));
196 canvas->drawRect(dst, paint);
197
198 canvas->translate(dst.width() + 4, 0);
199
200 canvas->drawImageRect(img.get(), src, dst, sampling, nullptr,
202 canvas->restore();
203
204 canvas->translate(0, dst.height() + 8);
205 }
206 }
207
208}
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
#define N
Definition: beziers.cpp:19
@ kFast_SrcRectConstraint
sample outside bounds; faster
Definition: SkCanvas.h:1543
sk_sp< SkImage > withDefaultMipmaps() const
Definition: SkImage.cpp:305
static SkMatrix RectToRect(const SkRect &src, const SkRect &dst, ScaleToFit mode=kFill_ScaleToFit)
Definition: SkMatrix.h:157
const Paint & paint
Definition: color_source.cc:38
static sk_sp< SkImage > make_checker_img(int w, int h, SkColor c0, SkColor c1, int size)
SkSamplingOptions sampling
Definition: SkRecords.h:337
dst
Definition: cp.py:12
static SkRect MakeIWH(int w, int h)
Definition: SkRect.h:623

◆ DEF_SIMPLE_GM() [2/2]

DEF_SIMPLE_GM ( textureimage_and_shader  ,
canvas  ,
100  ,
50   
)

Definition at line 211 of file image_shader.cpp.

211 {
212 canvas->clear(SK_ColorGREEN);
213
215 if (canvas->getSurface()) {
216 image = canvas->getSurface()->makeImageSnapshot();
217 canvas->clear(SK_ColorRED);
218 } else {
219 auto greenSurface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(50, 50));
220 greenSurface->getCanvas()->clear(SK_ColorGREEN);
221 image = greenSurface->makeImageSnapshot();
222 }
223
224 // At this point, 'image' contains a green image. If our original canvas is GPU-backed, then
225 // the snapped image will be a (GPU) texture. We will try to draw that image to a non-GPU
226 // surface, to ensure that we get automatic read-back. If all goes well, we will get a pure
227 // green result. If either draw fails, we'll get red (most likely).
228
230
231 // First, use drawImage:
232 surface->getCanvas()->clear(SK_ColorRED);
233 surface->getCanvas()->drawImage(image, 0, 0);
234 canvas->drawImage(surface->makeImageSnapshot(), 0, 0);
235
236 // Now, use an image shader:
239 surface->getCanvas()->clear(SK_ColorRED);
240 surface->getCanvas()->drawPaint(paint);
241 canvas->drawImage(surface->makeImageSnapshot(), 50, 0);
242}
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkImage.cpp:179
VkSurfaceKHR surface
Definition: main.cc:49
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
static SkImageInfo MakeN32Premul(int width, int height)

◆ draw_something()

static void draw_something ( SkCanvas canvas,
const SkRect bounds 
)
static

Definition at line 35 of file image_shader.cpp.

35 {
37 paint.setAntiAlias(true);
38 paint.setColor(SK_ColorRED);
40 paint.setStrokeWidth(10);
41 canvas->drawRect(bounds, paint);
43 paint.setColor(SK_ColorBLUE);
44 canvas->drawOval(bounds, paint);
45}
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void drawOval(const SkRect &oval, const SkPaint &paint)
Definition: SkCanvas.cpp:1698
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition: SkPaint.h:193
Optional< SkRect > bounds
Definition: SkRecords.h:189

◆ make_checker_img()

static sk_sp< SkImage > make_checker_img ( int  w,
int  h,
SkColor  c0,
SkColor  c1,
int  size 
)
static

Definition at line 164 of file image_shader.cpp.

164 {
166 bm.setImmutable();
167 return bm.asImage();
168}
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
void setImmutable()
Definition: SkBitmap.cpp:400
SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize)
Definition: ToolUtils.cpp:159
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
SkScalar w
SkScalar h

◆ make_encode_gen()

static sk_sp< SkImage > make_encode_gen ( GrRecordingContext ctx,
SkPicture pic,
const SkImageInfo info 
)
static

Definition at line 84 of file image_shader.cpp.

86 {
88 if (!src) {
89 return nullptr;
90 }
91 sk_sp<SkData> encoded = SkPngEncoder::Encode(nullptr, src.get(), {});
92 if (!encoded) {
93 return nullptr;
94 }
95 return SkImages::DeferredFromEncodedData(std::move(encoded));
96}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
static sk_sp< SkImage > make_raster(GrRecordingContext *, SkPicture *pic, const SkImageInfo &info)
SK_API sk_sp< SkImage > DeferredFromEncodedData(sk_sp< SkData > encoded, std::optional< SkAlphaType > alphaType=std::nullopt)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)

◆ make_pict_gen()

static sk_sp< SkImage > make_pict_gen ( GrRecordingContext ,
SkPicture pic,
const SkImageInfo info 
)
static

Definition at line 73 of file image_shader.cpp.

75 {
77 info.dimensions(),
78 nullptr,
79 nullptr,
82}
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
static sk_sp< SkColorSpace > MakeSRGB()
SK_API sk_sp< SkImage > DeferredFromPicture(sk_sp< SkPicture > picture, const SkISize &dimensions, const SkMatrix *matrix, const SkPaint *paint, BitDepth bitDepth, sk_sp< SkColorSpace > colorSpace, SkSurfaceProps props)
@ kU8
uses 8-bit unsigned int per color component

◆ make_raster()

static sk_sp< SkImage > make_raster ( GrRecordingContext ,
SkPicture pic,
const SkImageInfo info 
)
static

Definition at line 49 of file image_shader.cpp.

51 {
53 surface->getCanvas()->clear(0);
54 surface->getCanvas()->drawPicture(pic);
55 return surface->makeImageSnapshot();
56}

◆ make_texture()

static sk_sp< SkImage > make_texture ( GrRecordingContext ctx,
SkPicture pic,
const SkImageInfo info 
)
static

Definition at line 58 of file image_shader.cpp.

60 {
61 if (!ctx) {
62 return nullptr;
63 }
65 if (!surface) {
66 return nullptr;
67 }
68 surface->getCanvas()->clear(0);
69 surface->getCanvas()->drawPicture(pic);
70 return surface->makeImageSnapshot();
71}
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)

Variable Documentation

◆ gProcs

const ImageMakerProc gProcs[]
Initial value:
= {
}
static sk_sp< SkImage > make_texture(GrRecordingContext *ctx, SkPicture *pic, const SkImageInfo &info)
static sk_sp< SkImage > make_pict_gen(GrRecordingContext *, SkPicture *pic, const SkImageInfo &info)
static sk_sp< SkImage > make_encode_gen(GrRecordingContext *ctx, SkPicture *pic, const SkImageInfo &info)

Definition at line 98 of file image_shader.cpp.