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