Debian Codegraph @

gm/drawbitmaprect.cpp

raw
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"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlurTypes.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkMaskFilter.h"
17 #include "include/core/SkMatrix.h"
18 #include "include/core/SkPaint.h"
19 #include "include/core/SkPoint.h"
20 #include "include/core/SkRect.h"
21 #include "include/core/SkRefCnt.h"
22 #include "include/core/SkScalar.h"
23 #include "include/core/SkShader.h"
24 #include "include/core/SkSize.h"
25 #include "include/core/SkString.h"
26 #include "include/core/SkSurface.h"
27 #include "include/core/SkTileMode.h"
28 #include "include/core/SkTypeface.h"
29 #include "include/core/SkTypes.h"
30 #include "include/effects/SkGradient.h"
31 #include "src/base/SkMathPriv.h"
32 #include "src/core/SkBlurMask.h"
33 #include "tools/GpuToolUtils.h"
34 #include "tools/ToolUtils.h"
35 #include "tools/fonts/FontToolUtils.h"
36
37 static SkBitmap make_chessbm(int w, int h) {
38 SkBitmap bm;
39 bm.allocN32Pixels(w, h);
40
41 for (int y = 0; y < bm.height(); y++) {
42 uint32_t* p = bm.getAddr32(0, y);
43 for (int x = 0; x < bm.width(); x++) {
44 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
45 }
46 }
47 bm.setImmutable();
48 return bm;
49 }
50
51 // Creates a bitmap and a matching image.
52 static sk_sp<SkImage> makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) {
53 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
54
55 auto surface(ToolUtils::makeSurface(origCanvas, info));
56 SkCanvas* canvas = surface->getCanvas();
57
58 canvas->clear(SK_ColorTRANSPARENT);
59
60 SkScalar wScalar = SkIntToScalar(w);
61 SkScalar hScalar = SkIntToScalar(h);
62
63 SkPoint pt = { wScalar / 2, hScalar / 2 };
64
65 SkScalar radius = 4 * std::max(wScalar, hScalar);
66
67 const SkColor4f colors[] = { SkColors::kRed, SkColors::kYellow,
68 SkColors::kGreen, SkColors::kMagenta,
69 SkColors::kBlue, SkColors::kCyan,
70 SkColors::kRed};
71
72 SkScalar pos[] = {0,
73 SK_Scalar1 / 6,
74 2 * SK_Scalar1 / 6,
75 3 * SK_Scalar1 / 6,
76 4 * SK_Scalar1 / 6,
77 5 * SK_Scalar1 / 6,
78 SK_Scalar1};
79
80 SkPaint paint;
81 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
82 SkMatrix mat = SkMatrix::I();
83 for (int i = 0; i < 4; ++i) {
84 paint.setShader(SkShaders::RadialGradient(
85 pt, radius,
86 {{colors, pos, SkTileMode::kRepeat}, {}},
87 &mat));
88 canvas->drawRect(rect, paint);
89 rect.inset(wScalar / 8, hScalar / 8);
90 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
91 }
92
93 auto image = surface->makeImageSnapshot();
94
95 SkBitmap tempBM;
96
97 image->asLegacyBitmap(&tempBM);
98
99 // Let backends know we won't change this, so they don't have to deep copy it defensively.
100 tempBM.setImmutable();
101 *resultBM = tempBM;
102
103 return image;
104 }
105
106 static void bitmapproc(SkCanvas* canvas, sk_sp<SkImage> image,
107 const SkBitmap&, const SkIRect& srcR,
108 const SkRect& dstR, const SkSamplingOptions& sampling,
109 const SkPaint* paint) {
110 canvas->drawImageRect(image, SkRect::Make(srcR), dstR, sampling, paint,
111 SkCanvas::kStrict_SrcRectConstraint);
112 }
113
114 static void bitmapsubsetproc(SkCanvas* canvas, sk_sp<SkImage> image,
115 const SkBitmap& bm, const SkIRect& srcR,
116 const SkRect& dstR, const SkSamplingOptions& sampling,
117 const SkPaint* paint) {
118 if (!bm.bounds().contains(srcR)) {
119 bitmapproc(canvas, std::move(image), bm, srcR, dstR, sampling, paint);
120 return;
121 }
122
123 SkBitmap subset;
124 if (bm.extractSubset(&subset, srcR)) {
125 sk_sp<SkImage> subsetImg = ToolUtils::MakeTextureImage(canvas, subset.asImage());
126 canvas->drawImageRect(subsetImg, dstR, sampling, paint);
127 }
128 }
129
130 static void imageproc(SkCanvas* canvas, sk_sp<SkImage> image, const SkBitmap&, const SkIRect& srcR,
131 const SkRect& dstR, const SkSamplingOptions& sampling, const SkPaint* paint) {
132 sk_sp<SkImage> tmp = ToolUtils::MakeTextureImage(canvas, std::move(image));
133 canvas->drawImageRect(tmp, SkRect::Make(srcR), dstR, sampling, paint,
134 SkCanvas::kStrict_SrcRectConstraint);
135 }
136
137 static void imagesubsetproc(SkCanvas* canvas, sk_sp<SkImage> image, const SkBitmap& bm,
138 const SkIRect& srcR, const SkRect& dstR,
139 const SkSamplingOptions& sampling, const SkPaint* paint) {
140 SkASSERT_RELEASE(image);
141 if (!image->bounds().contains(srcR)) {
142 imageproc(canvas, std::move(image), bm, srcR, dstR, sampling, paint);
143 return;
144 }
145
146 if (sk_sp<SkImage> subset = image->makeSubset(canvas->baseRecorder(), srcR, {})) {
147 canvas->drawImageRect(subset, dstR, sampling, paint);
148 }
149 }
150
151 typedef void DrawRectRectProc(SkCanvas*, sk_sp<SkImage>, const SkBitmap&,
152 const SkIRect& srcR, const SkRect& dstR,
153 const SkSamplingOptions&, const SkPaint*);
154
155 constexpr int gSize = 1024;
156 constexpr int gBmpSize = 2048;
157
158 class DrawBitmapRectGM : public skiagm::GM {
159 public:
160 DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) {
161 fName.set("drawbitmaprect");
162 if (suffix) {
163 fName.append(suffix);
164 }
165 }
166
167 DrawRectRectProc* fProc;
168 SkBitmap fLargeBitmap;
169 sk_sp<SkImage> fImage;
170 SkString fName;
171
172 protected:
173 SkString getName() const override { return fName; }
174
175 SkISize getISize() override { return SkISize::Make(gSize, gSize); }
176
177 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
178 if (!fImage || !fImage->isValid(canvas->baseRecorder())) {
179 fImage = ToolUtils::MakeTextureImage(canvas,
180 makebm(canvas, &fLargeBitmap, gBmpSize, gBmpSize));
181 if (!fImage) {
182 *errorMsg = "Image creation failed";
183 return DrawResult::kSkip;
184 }
185 }
186
187 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
188 const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2);
189
190 const int kPadX = 30;
191 const int kPadY = 40;
192 SkPaint alphaPaint;
193 alphaPaint.setAlphaf(0.125f);
194 canvas->drawImageRect(fImage, SkRect::MakeIWH(gSize, gSize), SkSamplingOptions(),
195 &alphaPaint);
196 canvas->translate(SK_Scalar1 * kPadX / 2,
197 SK_Scalar1 * kPadY / 2);
198 SkPaint blackPaint;
199 SkScalar titleHeight = SK_Scalar1 * 24;
200 blackPaint.setColor(SK_ColorBLACK);
201 blackPaint.setAntiAlias(true);
202
203 SkFont font(ToolUtils::DefaultPortableTypeface(), titleHeight);
204
205 SkString title;
206 title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize);
207 canvas->drawString(title, 0, titleHeight, font, blackPaint);
208
209 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight);
210 int rowCount = 0;
211 canvas->save();
212 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
213 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
214
215 SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h);
216 fProc(canvas, fImage, fLargeBitmap, srcRect, dstRect, SkSamplingOptions(),
217 nullptr);
218
219 SkString label;
220 label.appendf("%d x %d", w, h);
221 blackPaint.setAntiAlias(true);
222 blackPaint.setStyle(SkPaint::kFill_Style);
223 font.setSize(SK_Scalar1 * 10);
224 SkScalar baseline = dstRect.height() + font.getSize() + SK_Scalar1 * 3;
225 canvas->drawString(label, 0, baseline, font, blackPaint);
226 blackPaint.setStyle(SkPaint::kStroke_Style);
227 blackPaint.setStrokeWidth(SK_Scalar1);
228 blackPaint.setAntiAlias(false);
229 canvas->drawRect(dstRect, blackPaint);
230
231 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
232 ++rowCount;
233 if ((dstRect.width() + kPadX) * rowCount > gSize) {
234 canvas->restore();
235 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
236 canvas->save();
237 rowCount = 0;
238 }
239 }
240 }
241
242 {
243 // test the following code path:
244 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
245 SkIRect srcRect;
246 SkPaint maskPaint;
247 SkBitmap bm = make_chessbm(5, 5);
248 sk_sp<SkImage> img = ToolUtils::MakeTextureImage(canvas, bm.asImage());
249
250 srcRect.setXYWH(1, 1, 3, 3);
251 maskPaint.setMaskFilter(SkMaskFilter::MakeBlur(
252 kNormal_SkBlurStyle,
253 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5))));
254
255 fProc(canvas, img, bm, srcRect, dstRect,
256 SkSamplingOptions(SkFilterMode::kLinear), &maskPaint);
257 }
258
259 return DrawResult::kOk;
260 }
261
262 private:
263 using INHERITED = skiagm::GM;
264 };
265
266 DEF_GM( return new DrawBitmapRectGM(bitmapproc , nullptr); )
267 DEF_GM( return new DrawBitmapRectGM(bitmapsubsetproc, "-subset"); )
268 DEF_GM( return new DrawBitmapRectGM(imageproc , "-imagerect"); )
269 DEF_GM( return new DrawBitmapRectGM(imagesubsetproc , "-imagerect-subset"); )