• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkFilterQuality.h"
14 #include "include/core/SkFont.h"
15 #include "include/core/SkImage.h"
16 #include "include/core/SkImageInfo.h"
17 #include "include/core/SkMaskFilter.h"
18 #include "include/core/SkMatrix.h"
19 #include "include/core/SkPaint.h"
20 #include "include/core/SkPoint.h"
21 #include "include/core/SkRect.h"
22 #include "include/core/SkRefCnt.h"
23 #include "include/core/SkScalar.h"
24 #include "include/core/SkShader.h"
25 #include "include/core/SkSize.h"
26 #include "include/core/SkString.h"
27 #include "include/core/SkSurface.h"
28 #include "include/core/SkTileMode.h"
29 #include "include/core/SkTypeface.h"
30 #include "include/core/SkTypes.h"
31 #include "include/effects/SkGradientShader.h"
32 #include "include/gpu/GrDirectContext.h"
33 #include "src/core/SkBlurMask.h"
34 #include "src/core/SkMathPriv.h"
35 #include "tools/ToolUtils.h"
36 
make_chessbm(int w,int h)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.
makebm(SkCanvas * origCanvas,SkBitmap * resultBM,int w,int h)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     SkColor     colors[] = { SK_ColorRED, SK_ColorYELLOW,
68                              SK_ColorGREEN, SK_ColorMAGENTA,
69                              SK_ColorBLUE, SK_ColorCYAN,
70                              SK_ColorRED};
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(SkGradientShader::MakeRadial(
85                         pt, radius,
86                         colors, pos,
87                         SK_ARRAY_COUNT(colors),
88                         SkTileMode::kRepeat,
89                         0, &mat));
90         canvas->drawRect(rect, paint);
91         rect.inset(wScalar / 8, hScalar / 8);
92         mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
93     }
94 
95     auto image = surface->makeImageSnapshot();
96 
97     SkBitmap tempBM;
98 
99     image->asLegacyBitmap(&tempBM);
100 
101     // Let backends know we won't change this, so they don't have to deep copy it defensively.
102     tempBM.setImmutable();
103     *resultBM = tempBM;
104 
105     return image;
106 }
107 
bitmapproc(SkCanvas * canvas,SkImage *,const SkBitmap & bm,const SkIRect & srcR,const SkRect & dstR,const SkSamplingOptions & sampling,const SkPaint * paint)108 static void bitmapproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
109                        const SkRect& dstR, const SkSamplingOptions& sampling,
110                        const SkPaint* paint) {
111     canvas->drawImageRect(bm.asImage(), SkRect::Make(srcR), dstR, sampling, paint,
112                           SkCanvas::kStrict_SrcRectConstraint);
113 }
114 
bitmapsubsetproc(SkCanvas * canvas,SkImage *,const SkBitmap & bm,const SkIRect & srcR,const SkRect & dstR,const SkSamplingOptions & sampling,const SkPaint * paint)115 static void bitmapsubsetproc(SkCanvas* canvas, SkImage*, 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, nullptr, bm, srcR, dstR, sampling, paint);
120         return;
121     }
122 
123     SkBitmap subset;
124     if (bm.extractSubset(&subset, srcR)) {
125         canvas->drawImageRect(subset.asImage(), dstR, sampling, paint);
126     }
127 }
128 
imageproc(SkCanvas * canvas,SkImage * image,const SkBitmap &,const SkIRect & srcR,const SkRect & dstR,const SkSamplingOptions & sampling,const SkPaint * paint)129 static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect& srcR,
130                       const SkRect& dstR, const SkSamplingOptions& sampling, const SkPaint* paint) {
131     canvas->drawImageRect(image, SkRect::Make(srcR), dstR, sampling, paint,
132                           SkCanvas::kStrict_SrcRectConstraint);
133 }
134 
imagesubsetproc(SkCanvas * canvas,SkImage * image,const SkBitmap & bm,const SkIRect & srcR,const SkRect & dstR,const SkSamplingOptions & sampling,const SkPaint * paint)135 static void imagesubsetproc(SkCanvas* canvas, SkImage* image, const SkBitmap& bm,
136                             const SkIRect& srcR, const SkRect& dstR,
137                             const SkSamplingOptions& sampling, const SkPaint* paint) {
138     if (!image->bounds().contains(srcR)) {
139         imageproc(canvas, image, bm, srcR, dstR, sampling, paint);
140         return;
141     }
142 
143     auto direct = GrAsDirectContext(canvas->recordingContext());
144     if (sk_sp<SkImage> subset = image->makeSubset(srcR, direct)) {
145         canvas->drawImageRect(subset, dstR, sampling, paint);
146     }
147 }
148 
149 typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&,
150                               const SkSamplingOptions&, const SkPaint*);
151 
152 constexpr int gSize = 1024;
153 constexpr int gBmpSize = 2048;
154 
155 class DrawBitmapRectGM : public skiagm::GM {
156 public:
DrawBitmapRectGM(DrawRectRectProc proc,const char suffix[])157     DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) {
158         fName.set("drawbitmaprect");
159         if (suffix) {
160             fName.append(suffix);
161         }
162     }
163 
164     DrawRectRectProc*   fProc;
165     SkBitmap            fLargeBitmap;
166     sk_sp<SkImage>      fImage;
167     SkString            fName;
168 
169 protected:
onShortName()170     SkString onShortName() override { return fName; }
171 
onISize()172     SkISize onISize() override { return SkISize::Make(gSize, gSize); }
173 
setupImage(SkCanvas * canvas)174     void setupImage(SkCanvas* canvas) {
175         fImage = makebm(canvas, &fLargeBitmap, gBmpSize, gBmpSize);
176     }
177 
onDraw(SkCanvas * canvas)178     void onDraw(SkCanvas* canvas) override {
179         if (!fImage || !fImage->isValid(canvas->recordingContext())) {
180             this->setupImage(canvas);
181         }
182 
183         SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
184         const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2);
185 
186         const int kPadX = 30;
187         const int kPadY = 40;
188         SkPaint paint;
189         paint.setAlphaf(0.125f);
190         canvas->drawImageRect(fImage, SkRect::MakeIWH(gSize, gSize), SkSamplingOptions(), &paint);
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 paint;
242             SkBitmap bm = make_chessbm(5, 5);
243 
244             srcRect.setXYWH(1, 1, 3, 3);
245             paint.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), &paint);
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