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