blob: be842dc912a92dd1f2c4a6d0b07e1442880fc3ff [file] [log] [blame]
joshualitt2ba70992015-08-14 06:30:50 -07001/*
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.h"
9#include "SkColorPriv.h"
10#include "SkGradientShader.h"
11#include "SkImage.h"
halcanary4dbbd042016-06-07 17:21:10 -070012#include "SkMathPriv.h"
joshualitt2ba70992015-08-14 06:30:50 -070013#include "SkRandom.h"
14#include "SkShader.h"
15#include "SkSurface.h"
16
Brian Osman876c8412018-01-04 15:28:39 -050017static sk_sp<SkImage> makebm(int w, int h) {
joshualitt2ba70992015-08-14 06:30:50 -070018 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
Brian Osman876c8412018-01-04 15:28:39 -050019 auto surface(SkSurface::MakeRaster(info));
joshualitt2ba70992015-08-14 06:30:50 -070020 SkCanvas* canvas = surface->getCanvas();
21
22 const SkScalar wScalar = SkIntToScalar(w);
23 const SkScalar hScalar = SkIntToScalar(h);
24
25 const SkPoint pt = { wScalar / 2, hScalar / 2 };
26
27 const SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar);
28
mtkleindbfd7ab2016-09-01 11:24:54 -070029 constexpr SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW,
joshualitt2ba70992015-08-14 06:30:50 -070030 SK_ColorGREEN, SK_ColorMAGENTA,
31 SK_ColorBLUE, SK_ColorCYAN,
32 SK_ColorRED};
33
mtkleindbfd7ab2016-09-01 11:24:54 -070034 constexpr SkScalar pos[] = {0,
joshualitt2ba70992015-08-14 06:30:50 -070035 SK_Scalar1 / 6,
36 2 * SK_Scalar1 / 6,
37 3 * SK_Scalar1 / 6,
38 4 * SK_Scalar1 / 6,
39 5 * SK_Scalar1 / 6,
40 SK_Scalar1};
41
42 SkASSERT(SK_ARRAY_COUNT(colors) == SK_ARRAY_COUNT(pos));
43 SkPaint paint;
44 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
45 SkMatrix mat = SkMatrix::I();
46 for (int i = 0; i < 4; ++i) {
reed2ad1aa62016-03-09 09:50:50 -080047 paint.setShader(SkGradientShader::MakeRadial(
joshualitt2ba70992015-08-14 06:30:50 -070048 pt, radius,
49 colors, pos,
50 SK_ARRAY_COUNT(colors),
51 SkShader::kRepeat_TileMode,
reed2ad1aa62016-03-09 09:50:50 -080052 0, &mat));
joshualitt2ba70992015-08-14 06:30:50 -070053 canvas->drawRect(rect, paint);
54 rect.inset(wScalar / 8, hScalar / 8);
55 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
56 }
reed9ce9d672016-03-17 10:51:11 -070057 return surface->makeImageSnapshot();
joshualitt2ba70992015-08-14 06:30:50 -070058}
59
mtkleindbfd7ab2016-09-01 11:24:54 -070060constexpr int gSize = 1024;
61constexpr int gSurfaceSize = 2048;
joshualitt2ba70992015-08-14 06:30:50 -070062
Brian Salomon09d994e2016-12-21 11:14:46 -050063// This GM calls drawImageRect several times using the same texture. This is intended to exercise
64// combining GrDrawOps during these calls.
joshualitt2ba70992015-08-14 06:30:50 -070065class DrawMiniBitmapRectGM : public skiagm::GM {
66public:
67 DrawMiniBitmapRectGM(bool antiAlias) : fAA(antiAlias) {
68 fName.set("drawminibitmaprect");
69 if (fAA) {
70 fName.appendf("_aa");
71 }
72 }
73
74protected:
75 SkString onShortName() override { return fName; }
76
77 SkISize onISize() override { return SkISize::Make(gSize, gSize); }
78
79 void onDraw(SkCanvas* canvas) override {
halcanary96fcdcc2015-08-27 07:41:13 -070080 if (nullptr == fImage) {
Brian Osman876c8412018-01-04 15:28:39 -050081 fImage = makebm(gSurfaceSize, gSurfaceSize);
joshualitt2ba70992015-08-14 06:30:50 -070082 }
83
84 const SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
mtkleindbfd7ab2016-09-01 11:24:54 -070085 const int kMaxSrcRectSize = 1 << (SkNextLog2(gSurfaceSize) + 2);
joshualitt2ba70992015-08-14 06:30:50 -070086
mtkleindbfd7ab2016-09-01 11:24:54 -070087 constexpr int kPadX = 30;
88 constexpr int kPadY = 40;
joshualitt2ba70992015-08-14 06:30:50 -070089
90 int rowCount = 0;
91 canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY));
92 canvas->save();
93 SkRandom random;
94
95 SkPaint paint;
96 paint.setAntiAlias(fAA);
97 for (int w = 1; w <= kMaxSrcRectSize; w *= 3) {
98 for (int h = 1; h <= kMaxSrcRectSize; h *= 3) {
99
100 const SkIRect srcRect =
101 SkIRect::MakeXYWH((gSurfaceSize - w) / 2, (gSurfaceSize - h) / 2, w, h);
102 canvas->save();
103 switch (random.nextU() % 3) {
104 case 0:
105 canvas->rotate(random.nextF() * 10.f);
106 break;
107 case 1:
108 canvas->rotate(-random.nextF() * 10.f);
109 break;
110 case 2:
111 // rect stays rect
112 break;
113 }
reed9ce9d672016-03-17 10:51:11 -0700114 canvas->drawImageRect(fImage.get(), srcRect, dstRect, &paint,
joshualitt2ba70992015-08-14 06:30:50 -0700115 SkCanvas::kFast_SrcRectConstraint);
116 canvas->restore();
117
118 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
119 ++rowCount;
120 if ((dstRect.width() + 2 * kPadX) * rowCount > gSize) {
121 canvas->restore();
122 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
123 canvas->save();
124 rowCount = 0;
125 }
126 }
127 }
128 canvas->restore();
129 }
130
131private:
reed9ce9d672016-03-17 10:51:11 -0700132 bool fAA;
133 sk_sp<SkImage> fImage;
134 SkString fName;
joshualitt2ba70992015-08-14 06:30:50 -0700135
136 typedef skiagm::GM INHERITED;
137};
138
139DEF_GM( return new DrawMiniBitmapRectGM(true); )
140DEF_GM( return new DrawMiniBitmapRectGM(false); )