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