• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkImage.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkShader.h"
20 #include "include/core/SkSize.h"
21 #include "include/core/SkString.h"
22 #include "include/core/SkSurface.h"
23 #include "include/core/SkTileMode.h"
24 #include "include/core/SkTypes.h"
25 #include "include/effects/SkGradientShader.h"
26 #include "src/base/SkMathPriv.h"
27 #include "src/base/SkRandom.h"
28 #include "tools/ToolUtils.h"
29 
makebm(int w,int h)30 static sk_sp<SkImage> makebm(int w, int h) {
31     SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
32     auto surface(SkSurface::MakeRaster(info));
33     SkCanvas* canvas = surface->getCanvas();
34 
35     const SkScalar wScalar = SkIntToScalar(w);
36     const SkScalar hScalar = SkIntToScalar(h);
37 
38     const SkPoint     pt = { wScalar / 2, hScalar / 2 };
39 
40     const SkScalar    radius = 4 * std::max(wScalar, hScalar);
41 
42     constexpr SkColor     colors[] = { SK_ColorRED, SK_ColorYELLOW,
43                                           SK_ColorGREEN, SK_ColorMAGENTA,
44                                           SK_ColorBLUE, SK_ColorCYAN,
45                                           SK_ColorRED};
46 
47     constexpr SkScalar    pos[] = {0,
48                                       SK_Scalar1 / 6,
49                                       2 * SK_Scalar1 / 6,
50                                       3 * SK_Scalar1 / 6,
51                                       4 * SK_Scalar1 / 6,
52                                       5 * SK_Scalar1 / 6,
53                                       SK_Scalar1};
54 
55     SkASSERT(std::size(colors) == std::size(pos));
56     SkPaint     paint;
57     SkRect rect = SkRect::MakeWH(wScalar, hScalar);
58     SkMatrix mat = SkMatrix::I();
59     for (int i = 0; i < 4; ++i) {
60         paint.setShader(SkGradientShader::MakeRadial(
61                         pt, radius,
62                         colors, pos,
63                         std::size(colors),
64                         SkTileMode::kRepeat,
65                         0, &mat));
66         canvas->drawRect(rect, paint);
67         rect.inset(wScalar / 8, hScalar / 8);
68         mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
69     }
70     return surface->makeImageSnapshot();
71 }
72 
73 constexpr int gSize = 1024;
74 constexpr int gSurfaceSize = 2048;
75 
76 // This GM calls drawImageRect several times using the same texture. This is intended to exercise
77 // combining GrDrawOps during these calls.
78 class DrawMiniBitmapRectGM : public skiagm::GM {
79 public:
DrawMiniBitmapRectGM(bool antiAlias)80     DrawMiniBitmapRectGM(bool antiAlias) : fAA(antiAlias) {
81         fName.set("drawminibitmaprect");
82         if (fAA) {
83             fName.appendf("_aa");
84         }
85     }
86 
87 protected:
onShortName()88     SkString onShortName() override { return fName; }
89 
onISize()90     SkISize onISize() override { return SkISize::Make(gSize, gSize); }
91 
onDraw(SkCanvas * canvas)92     void onDraw(SkCanvas* canvas) override {
93         if (nullptr == fImage) {
94             fImage = ToolUtils::MakeTextureImage(canvas, makebm(gSurfaceSize, gSurfaceSize));
95         }
96 
97         const SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
98         const int kMaxSrcRectSize = 1 << (SkNextLog2(gSurfaceSize) + 2);
99 
100         constexpr int kPadX = 30;
101         constexpr int kPadY = 40;
102 
103         int rowCount = 0;
104         canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY));
105         canvas->save();
106         SkRandom random;
107 
108         SkPaint paint;
109         paint.setAntiAlias(fAA);
110         for (int w = 1; w <= kMaxSrcRectSize; w *= 3) {
111             for (int h = 1; h <= kMaxSrcRectSize; h *= 3) {
112 
113                 const SkIRect srcRect =
114                         SkIRect::MakeXYWH((gSurfaceSize - w) / 2, (gSurfaceSize - h) / 2, w, h);
115                 canvas->save();
116                 switch (random.nextU() % 3) {
117                     case 0:
118                         canvas->rotate(random.nextF() * 10.f);
119                         break;
120                     case 1:
121                         canvas->rotate(-random.nextF() * 10.f);
122                         break;
123                     case 2:
124                         // rect stays rect
125                         break;
126                 }
127                 canvas->drawImageRect(fImage.get(), SkRect::Make(srcRect), dstRect,
128                                       SkSamplingOptions(), &paint,
129                                       SkCanvas::kFast_SrcRectConstraint);
130                 canvas->restore();
131 
132                 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
133                 ++rowCount;
134                 if ((dstRect.width() + 2 * kPadX) * rowCount > gSize) {
135                     canvas->restore();
136                     canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
137                     canvas->save();
138                     rowCount = 0;
139                 }
140             }
141         }
142         canvas->restore();
143     }
144 
145 private:
146     bool            fAA;
147     sk_sp<SkImage>  fImage;
148     SkString        fName;
149 
150     using INHERITED = skiagm::GM;
151 };
152 
153 DEF_GM( return new DrawMiniBitmapRectGM(true); )
154 DEF_GM( return new DrawMiniBitmapRectGM(false); )
155