blob: 4a1417ce2979be5be538e4e31136a3d7d8e805a6 [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
reed9ce9d672016-03-17 10:51:11 -070017static sk_sp<SkImage> makebm(SkCanvas* caller, int w, int h) {
joshualitt2ba70992015-08-14 06:30:50 -070018 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
reede8f30622016-03-23 18:59:25 -070019 auto surface(caller->makeSurface(info));
halcanary96fcdcc2015-08-27 07:41:13 -070020 if (nullptr == surface) {
reede8f30622016-03-23 18:59:25 -070021 surface = SkSurface::MakeRaster(info);
joshualitt2ba70992015-08-14 06:30:50 -070022 }
23 SkCanvas* canvas = surface->getCanvas();
24
25 const SkScalar wScalar = SkIntToScalar(w);
26 const SkScalar hScalar = SkIntToScalar(h);
27
28 const SkPoint pt = { wScalar / 2, hScalar / 2 };
29
30 const SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar);
31
mtkleindbfd7ab2016-09-01 11:24:54 -070032 constexpr SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW,
joshualitt2ba70992015-08-14 06:30:50 -070033 SK_ColorGREEN, SK_ColorMAGENTA,
34 SK_ColorBLUE, SK_ColorCYAN,
35 SK_ColorRED};
36
mtkleindbfd7ab2016-09-01 11:24:54 -070037 constexpr SkScalar pos[] = {0,
joshualitt2ba70992015-08-14 06:30:50 -070038 SK_Scalar1 / 6,
39 2 * SK_Scalar1 / 6,
40 3 * SK_Scalar1 / 6,
41 4 * SK_Scalar1 / 6,
42 5 * SK_Scalar1 / 6,
43 SK_Scalar1};
44
45 SkASSERT(SK_ARRAY_COUNT(colors) == SK_ARRAY_COUNT(pos));
46 SkPaint paint;
47 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
48 SkMatrix mat = SkMatrix::I();
49 for (int i = 0; i < 4; ++i) {
reed2ad1aa62016-03-09 09:50:50 -080050 paint.setShader(SkGradientShader::MakeRadial(
joshualitt2ba70992015-08-14 06:30:50 -070051 pt, radius,
52 colors, pos,
53 SK_ARRAY_COUNT(colors),
54 SkShader::kRepeat_TileMode,
reed2ad1aa62016-03-09 09:50:50 -080055 0, &mat));
joshualitt2ba70992015-08-14 06:30:50 -070056 canvas->drawRect(rect, paint);
57 rect.inset(wScalar / 8, hScalar / 8);
58 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
59 }
reed9ce9d672016-03-17 10:51:11 -070060 return surface->makeImageSnapshot();
joshualitt2ba70992015-08-14 06:30:50 -070061}
62
mtkleindbfd7ab2016-09-01 11:24:54 -070063constexpr int gSize = 1024;
64constexpr int gSurfaceSize = 2048;
joshualitt2ba70992015-08-14 06:30:50 -070065
Brian Salomon09d994e2016-12-21 11:14:46 -050066// This GM calls drawImageRect several times using the same texture. This is intended to exercise
67// combining GrDrawOps during these calls.
joshualitt2ba70992015-08-14 06:30:50 -070068class DrawMiniBitmapRectGM : public skiagm::GM {
69public:
70 DrawMiniBitmapRectGM(bool antiAlias) : fAA(antiAlias) {
71 fName.set("drawminibitmaprect");
72 if (fAA) {
73 fName.appendf("_aa");
74 }
75 }
76
77protected:
78 SkString onShortName() override { return fName; }
79
80 SkISize onISize() override { return SkISize::Make(gSize, gSize); }
81
82 void onDraw(SkCanvas* canvas) override {
halcanary96fcdcc2015-08-27 07:41:13 -070083 if (nullptr == fImage) {
reed9ce9d672016-03-17 10:51:11 -070084 fImage = makebm(canvas, gSurfaceSize, gSurfaceSize);
joshualitt2ba70992015-08-14 06:30:50 -070085 }
86
87 const SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
mtkleindbfd7ab2016-09-01 11:24:54 -070088 const int kMaxSrcRectSize = 1 << (SkNextLog2(gSurfaceSize) + 2);
joshualitt2ba70992015-08-14 06:30:50 -070089
mtkleindbfd7ab2016-09-01 11:24:54 -070090 constexpr int kPadX = 30;
91 constexpr int kPadY = 40;
joshualitt2ba70992015-08-14 06:30:50 -070092
93 int rowCount = 0;
94 canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY));
95 canvas->save();
96 SkRandom random;
97
98 SkPaint paint;
99 paint.setAntiAlias(fAA);
100 for (int w = 1; w <= kMaxSrcRectSize; w *= 3) {
101 for (int h = 1; h <= kMaxSrcRectSize; h *= 3) {
102
103 const SkIRect srcRect =
104 SkIRect::MakeXYWH((gSurfaceSize - w) / 2, (gSurfaceSize - h) / 2, w, h);
105 canvas->save();
106 switch (random.nextU() % 3) {
107 case 0:
108 canvas->rotate(random.nextF() * 10.f);
109 break;
110 case 1:
111 canvas->rotate(-random.nextF() * 10.f);
112 break;
113 case 2:
114 // rect stays rect
115 break;
116 }
reed9ce9d672016-03-17 10:51:11 -0700117 canvas->drawImageRect(fImage.get(), srcRect, dstRect, &paint,
joshualitt2ba70992015-08-14 06:30:50 -0700118 SkCanvas::kFast_SrcRectConstraint);
119 canvas->restore();
120
121 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
122 ++rowCount;
123 if ((dstRect.width() + 2 * kPadX) * rowCount > gSize) {
124 canvas->restore();
125 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
126 canvas->save();
127 rowCount = 0;
128 }
129 }
130 }
131 canvas->restore();
132 }
133
134private:
reed9ce9d672016-03-17 10:51:11 -0700135 bool fAA;
136 sk_sp<SkImage> fImage;
137 SkString fName;
joshualitt2ba70992015-08-14 06:30:50 -0700138
139 typedef skiagm::GM INHERITED;
140};
141
142DEF_GM( return new DrawMiniBitmapRectGM(true); )
143DEF_GM( return new DrawMiniBitmapRectGM(false); )