Flutter Engine
The Flutter Engine
drawbitmaprect.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2011 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"
20#include "include/core/SkRect.h"
24#include "include/core/SkSize.h"
32#include "src/base/SkMathPriv.h"
33#include "src/core/SkBlurMask.h"
34#include "tools/GpuToolUtils.h"
35#include "tools/ToolUtils.h"
37
38static SkBitmap make_chessbm(int w, int h) {
39 SkBitmap bm;
40 bm.allocN32Pixels(w, h);
41
42 for (int y = 0; y < bm.height(); y++) {
43 uint32_t* p = bm.getAddr32(0, y);
44 for (int x = 0; x < bm.width(); x++) {
45 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
46 }
47 }
48 bm.setImmutable();
49 return bm;
50}
51
52// Creates a bitmap and a matching image.
53static sk_sp<SkImage> makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) {
55
56 auto surface(ToolUtils::makeSurface(origCanvas, info));
57 SkCanvas* canvas = surface->getCanvas();
58
60
61 SkScalar wScalar = SkIntToScalar(w);
62 SkScalar hScalar = SkIntToScalar(h);
63
64 SkPoint pt = { wScalar / 2, hScalar / 2 };
65
66 SkScalar radius = 4 * std::max(wScalar, hScalar);
67
72
73 SkScalar pos[] = {0,
74 SK_Scalar1 / 6,
75 2 * SK_Scalar1 / 6,
76 3 * SK_Scalar1 / 6,
77 4 * SK_Scalar1 / 6,
78 5 * SK_Scalar1 / 6,
80
82 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
83 SkMatrix mat = SkMatrix::I();
84 for (int i = 0; i < 4; ++i) {
86 pt, radius,
87 colors, pos,
90 0, &mat));
91 canvas->drawRect(rect, paint);
92 rect.inset(wScalar / 8, hScalar / 8);
93 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
94 }
95
96 auto image = surface->makeImageSnapshot();
97
98 SkBitmap tempBM;
99
100 image->asLegacyBitmap(&tempBM);
101
102 // Let backends know we won't change this, so they don't have to deep copy it defensively.
103 tempBM.setImmutable();
104 *resultBM = tempBM;
105
106 return image;
107}
108
110 const SkBitmap&, const SkIRect& srcR,
111 const SkRect& dstR, const SkSamplingOptions& sampling,
112 const SkPaint* paint) {
113 canvas->drawImageRect(image, SkRect::Make(srcR), dstR, sampling, paint,
115}
116
118 const SkBitmap& bm, const SkIRect& srcR,
119 const SkRect& dstR, const SkSamplingOptions& sampling,
120 const SkPaint* paint) {
121 if (!bm.bounds().contains(srcR)) {
122 bitmapproc(canvas, std::move(image), bm, srcR, dstR, sampling, paint);
123 return;
124 }
125
126 SkBitmap subset;
127 if (bm.extractSubset(&subset, srcR)) {
128 sk_sp<SkImage> subsetImg = ToolUtils::MakeTextureImage(canvas, subset.asImage());
129 canvas->drawImageRect(subsetImg, dstR, sampling, paint);
130 }
131}
132
133static void imageproc(SkCanvas* canvas, sk_sp<SkImage> image, const SkBitmap&, const SkIRect& srcR,
134 const SkRect& dstR, const SkSamplingOptions& sampling, const SkPaint* paint) {
135 sk_sp<SkImage> tmp = ToolUtils::MakeTextureImage(canvas, std::move(image));
136 canvas->drawImageRect(tmp, SkRect::Make(srcR), dstR, sampling, paint,
138}
139
140static void imagesubsetproc(SkCanvas* canvas, sk_sp<SkImage> image, const SkBitmap& bm,
141 const SkIRect& srcR, const SkRect& dstR,
142 const SkSamplingOptions& sampling, const SkPaint* paint) {
144 if (!image->bounds().contains(srcR)) {
145 imageproc(canvas, std::move(image), bm, srcR, dstR, sampling, paint);
146 return;
147 }
148
149 auto direct = GrAsDirectContext(canvas->recordingContext());
150 if (sk_sp<SkImage> subset = image->makeSubset(direct, srcR)) {
151 canvas->drawImageRect(subset, dstR, sampling, paint);
152 return;
153 }
154#if defined(SK_GRAPHITE)
155 if (sk_sp<SkImage> subset = image->makeSubset(canvas->recorder(), srcR, {})) {
156 canvas->drawImageRect(subset, dstR, sampling, paint);
157 }
158#endif
159}
160
162 const SkIRect& srcR, const SkRect& dstR,
163 const SkSamplingOptions&, const SkPaint*);
164
165constexpr int gSize = 1024;
166constexpr int gBmpSize = 2048;
167
169public:
170 DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) {
171 fName.set("drawbitmaprect");
172 if (suffix) {
174 }
175 }
176
181
182protected:
183 SkString getName() const override { return fName; }
184
185 SkISize getISize() override { return SkISize::Make(gSize, gSize); }
186
187 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
188 if (!fImage || !fImage->isValid(canvas->recordingContext())) {
191 if (!fImage) {
192 *errorMsg = "Image creation failed";
193 return DrawResult::kSkip;
194 }
195 }
196
197 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
198 const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2);
199
200 const int kPadX = 30;
201 const int kPadY = 40;
202 SkPaint alphaPaint;
203 alphaPaint.setAlphaf(0.125f);
205 &alphaPaint);
206 canvas->translate(SK_Scalar1 * kPadX / 2,
207 SK_Scalar1 * kPadY / 2);
208 SkPaint blackPaint;
209 SkScalar titleHeight = SK_Scalar1 * 24;
210 blackPaint.setColor(SK_ColorBLACK);
211 blackPaint.setAntiAlias(true);
212
214
215 SkString title;
216 title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize);
217 canvas->drawString(title, 0, titleHeight, font, blackPaint);
218
219 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight);
220 int rowCount = 0;
221 canvas->save();
222 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
223 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
224
225 SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h);
226 fProc(canvas, fImage, fLargeBitmap, srcRect, dstRect, SkSamplingOptions(),
227 nullptr);
228
229 SkString label;
230 label.appendf("%d x %d", w, h);
231 blackPaint.setAntiAlias(true);
232 blackPaint.setStyle(SkPaint::kFill_Style);
233 font.setSize(SK_Scalar1 * 10);
234 SkScalar baseline = dstRect.height() + font.getSize() + SK_Scalar1 * 3;
235 canvas->drawString(label, 0, baseline, font, blackPaint);
237 blackPaint.setStrokeWidth(SK_Scalar1);
238 blackPaint.setAntiAlias(false);
239 canvas->drawRect(dstRect, blackPaint);
240
241 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
242 ++rowCount;
243 if ((dstRect.width() + kPadX) * rowCount > gSize) {
244 canvas->restore();
245 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
246 canvas->save();
247 rowCount = 0;
248 }
249 }
250 }
251
252 {
253 // test the following code path:
254 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
255 SkIRect srcRect;
256 SkPaint maskPaint;
257 SkBitmap bm = make_chessbm(5, 5);
259
260 srcRect.setXYWH(1, 1, 3, 3);
264
265 fProc(canvas, img, bm, srcRect, dstRect,
267 }
268
269 return DrawResult::kOk;
270 }
271
272private:
273 using INHERITED = skiagm::GM;
274};
275
276DEF_GM( return new DrawBitmapRectGM(bitmapproc , nullptr); )
277DEF_GM( return new DrawBitmapRectGM(bitmapsubsetproc, "-subset"); )
278DEF_GM( return new DrawBitmapRectGM(imageproc , "-imagerect"); )
279DEF_GM( return new DrawBitmapRectGM(imagesubsetproc , "-imagerect-subset"); )
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
SkPoint pos
#define SkASSERT_RELEASE(cond)
Definition: SkAssert.h:100
@ kNormal_SkBlurStyle
fuzzy inside and outside
Definition: SkBlurTypes.h:12
constexpr SkColor SK_ColorYELLOW
Definition: SkColor.h:139
constexpr SkColor SK_ColorMAGENTA
Definition: SkColor.h:147
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorCYAN
Definition: SkColor.h:143
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
static int SkNextLog2(uint32_t value)
Definition: SkMathPriv.h:238
#define SK_Scalar1
Definition: SkScalar.h:18
#define SkIntToScalar(x)
Definition: SkScalar.h:57
DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[])
SkString getName() const override
DrawRectRectProc * fProc
SkISize getISize() override
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
sk_sp< SkImage > fImage
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
void setImmutable()
Definition: SkBitmap.cpp:400
bool extractSubset(SkBitmap *dst, const SkIRect &subset) const
Definition: SkBitmap.cpp:453
int width() const
Definition: SkBitmap.h:149
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
SkIRect bounds() const
Definition: SkBitmap.h:382
int height() const
Definition: SkBitmap.h:158
uint32_t * getAddr32(int x, int y) const
Definition: SkBitmap.h:1260
static SkScalar SK_SPI ConvertRadiusToSigma(SkScalar radius)
Definition: SkBlurMask.cpp:39
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
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
void clear(SkColor color)
Definition: SkCanvas.h:1199
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
Definition: SkCanvas.cpp:2333
int save()
Definition: SkCanvas.cpp:447
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
Definition: SkFont.h:35
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
virtual bool isValid(GrRecordingContext *context) const =0
SkIRect bounds() const
Definition: SkImage.h:303
bool asLegacyBitmap(SkBitmap *bitmap, LegacyBitmapMode legacyBitmapMode=kRO_LegacyBitmapMode) const
Definition: SkImage.cpp:233
virtual sk_sp< SkImage > makeSubset(GrDirectContext *direct, const SkIRect &subset) const =0
static sk_sp< SkMaskFilter > MakeBlur(SkBlurStyle style, SkScalar sigma, bool respectCTM=true)
SkMatrix & postScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py)
Definition: SkMatrix.cpp:360
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
void setStyle(Style style)
Definition: SkPaint.cpp:105
void setColor(SkColor color)
Definition: SkPaint.cpp:119
void setAntiAlias(bool aa)
Definition: SkPaint.h:170
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition: SkPaint.h:193
void setMaskFilter(sk_sp< SkMaskFilter > maskFilter)
void setStrokeWidth(SkScalar width)
Definition: SkPaint.cpp:159
void setAlphaf(float a)
Definition: SkPaint.cpp:130
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:534
void set(const SkString &src)
Definition: SkString.h:186
void append(const char text[])
Definition: SkString.h:203
void void void appendf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:550
Definition: gm.h:110
const Paint & paint
Definition: color_source.cc:38
void DrawRectRectProc(SkCanvas *, sk_sp< SkImage >, const SkBitmap &, const SkIRect &srcR, const SkRect &dstR, const SkSamplingOptions &, const SkPaint *)
constexpr int gSize
constexpr int gBmpSize
static void imagesubsetproc(SkCanvas *canvas, sk_sp< SkImage > image, const SkBitmap &bm, const SkIRect &srcR, const SkRect &dstR, const SkSamplingOptions &sampling, const SkPaint *paint)
static SkBitmap make_chessbm(int w, int h)
static void imageproc(SkCanvas *canvas, sk_sp< SkImage > image, const SkBitmap &, const SkIRect &srcR, const SkRect &dstR, const SkSamplingOptions &sampling, const SkPaint *paint)
static sk_sp< SkImage > makebm(SkCanvas *origCanvas, SkBitmap *resultBM, int w, int h)
static void bitmapsubsetproc(SkCanvas *canvas, sk_sp< SkImage > image, const SkBitmap &bm, const SkIRect &srcR, const SkRect &dstR, const SkSamplingOptions &sampling, const SkPaint *paint)
static void bitmapproc(SkCanvas *canvas, sk_sp< SkImage > image, const SkBitmap &, const SkIRect &srcR, const SkRect &dstR, const SkSamplingOptions &sampling, const SkPaint *paint)
VkSurfaceKHR surface
Definition: main.cc:49
float SkScalar
Definition: extension.cpp:12
#define DEF_GM(CODE)
Definition: gm.h:40
static float max(float r, float g, float b)
Definition: hsl.cpp:49
double y
double x
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
PODArray< SkColor > colors
Definition: SkRecords.h:276
SkSamplingOptions sampling
Definition: SkRecords.h:337
sk_sp< SkTypeface > DefaultPortableTypeface()
sk_sp< SkSurface > makeSurface(SkCanvas *canvas, const SkImageInfo &info, const SkSurfaceProps *props)
Definition: ToolUtils.cpp:512
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
font
Font Metadata and Metrics.
SkSamplingOptions(SkFilterMode::kLinear))
DrawResult
Definition: gm.h:104
SkScalar w
SkScalar h
Definition: SkRect.h:32
void setXYWH(int32_t x, int32_t y, int32_t width, int32_t height)
Definition: SkRect.h:268
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: SkRect.h:104
bool contains(int32_t x, int32_t y) const
Definition: SkRect.h:463
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 SkRect Make(const SkISize &size)
Definition: SkRect.h:669
static SkRect MakeIWH(int w, int h)
Definition: SkRect.h:623
constexpr float height() const
Definition: SkRect.h:769
constexpr float width() const
Definition: SkRect.h:762
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609