blob: cf28c57f67ede70f6636e328971fc10a4756d836 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "include/core/SkSize.h"
21#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#include "include/core/SkTileMode.h"
24#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/effects/SkGradientShader.h"
Kevin Lubick1b3aa8b2023-01-19 14:03:31 -050026#include "src/base/SkMathPriv.h"
Kevin Lubick0d4d1142023-02-13 09:13:10 -050027#include "src/base/SkRandom.h"
Kevin Lubick9b028372023-10-05 15:04:54 -040028#include "tools/GpuToolUtils.h"
Robert Phillips0fb10ab2022-04-20 14:57:03 -040029#include "tools/ToolUtils.h"
joshualitt2ba70992015-08-14 06:30:50 -070030
Brian Osman876c8412018-01-04 15:28:39 -050031static sk_sp<SkImage> makebm(int w, int h) {
joshualitt2ba70992015-08-14 06:30:50 -070032 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
Kevin Lubick5c93acf2023-05-09 12:11:43 -040033 auto surface(SkSurfaces::Raster(info));
joshualitt2ba70992015-08-14 06:30:50 -070034 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
Brian Osman116b33e2020-02-05 13:34:09 -050041 const SkScalar radius = 4 * std::max(wScalar, hScalar);
joshualitt2ba70992015-08-14 06:30:50 -070042
mtkleindbfd7ab2016-09-01 11:24:54 -070043 constexpr SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW,
joshualitt2ba70992015-08-14 06:30:50 -070044 SK_ColorGREEN, SK_ColorMAGENTA,
45 SK_ColorBLUE, SK_ColorCYAN,
46 SK_ColorRED};
47
mtkleindbfd7ab2016-09-01 11:24:54 -070048 constexpr SkScalar pos[] = {0,
joshualitt2ba70992015-08-14 06:30:50 -070049 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
Herb Derbyc37b3862022-06-21 09:49:17 -040056 SkASSERT(std::size(colors) == std::size(pos));
joshualitt2ba70992015-08-14 06:30:50 -070057 SkPaint paint;
58 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
59 SkMatrix mat = SkMatrix::I();
60 for (int i = 0; i < 4; ++i) {
reed2ad1aa62016-03-09 09:50:50 -080061 paint.setShader(SkGradientShader::MakeRadial(
joshualitt2ba70992015-08-14 06:30:50 -070062 pt, radius,
63 colors, pos,
Herb Derbyc37b3862022-06-21 09:49:17 -040064 std::size(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040065 SkTileMode::kRepeat,
reed2ad1aa62016-03-09 09:50:50 -080066 0, &mat));
joshualitt2ba70992015-08-14 06:30:50 -070067 canvas->drawRect(rect, paint);
68 rect.inset(wScalar / 8, hScalar / 8);
69 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
70 }
reed9ce9d672016-03-17 10:51:11 -070071 return surface->makeImageSnapshot();
joshualitt2ba70992015-08-14 06:30:50 -070072}
73
mtkleindbfd7ab2016-09-01 11:24:54 -070074constexpr int gSize = 1024;
75constexpr int gSurfaceSize = 2048;
joshualitt2ba70992015-08-14 06:30:50 -070076
Brian Salomon09d994e2016-12-21 11:14:46 -050077// This GM calls drawImageRect several times using the same texture. This is intended to exercise
78// combining GrDrawOps during these calls.
joshualitt2ba70992015-08-14 06:30:50 -070079class DrawMiniBitmapRectGM : public skiagm::GM {
80public:
81 DrawMiniBitmapRectGM(bool antiAlias) : fAA(antiAlias) {
82 fName.set("drawminibitmaprect");
83 if (fAA) {
84 fName.appendf("_aa");
85 }
86 }
87
88protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000089 SkString getName() const override { return fName; }
joshualitt2ba70992015-08-14 06:30:50 -070090
Leandro Lovisolo8f023882023-08-15 21:13:52 +000091 SkISize getISize() override { return SkISize::Make(gSize, gSize); }
joshualitt2ba70992015-08-14 06:30:50 -070092
93 void onDraw(SkCanvas* canvas) override {
halcanary96fcdcc2015-08-27 07:41:13 -070094 if (nullptr == fImage) {
Robert Phillips0fb10ab2022-04-20 14:57:03 -040095 fImage = ToolUtils::MakeTextureImage(canvas, makebm(gSurfaceSize, gSurfaceSize));
joshualitt2ba70992015-08-14 06:30:50 -070096 }
97
98 const SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
mtkleindbfd7ab2016-09-01 11:24:54 -070099 const int kMaxSrcRectSize = 1 << (SkNextLog2(gSurfaceSize) + 2);
joshualitt2ba70992015-08-14 06:30:50 -0700100
mtkleindbfd7ab2016-09-01 11:24:54 -0700101 constexpr int kPadX = 30;
102 constexpr int kPadY = 40;
joshualitt2ba70992015-08-14 06:30:50 -0700103
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 }
Mike Reed07c5f522021-01-23 12:23:23 -0500128 canvas->drawImageRect(fImage.get(), SkRect::Make(srcRect), dstRect,
129 SkSamplingOptions(), &paint,
joshualitt2ba70992015-08-14 06:30:50 -0700130 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
146private:
reed9ce9d672016-03-17 10:51:11 -0700147 bool fAA;
148 sk_sp<SkImage> fImage;
149 SkString fName;
joshualitt2ba70992015-08-14 06:30:50 -0700150
John Stiles7571f9e2020-09-02 22:42:33 -0400151 using INHERITED = skiagm::GM;
joshualitt2ba70992015-08-14 06:30:50 -0700152};
153
154DEF_GM( return new DrawMiniBitmapRectGM(true); )
155DEF_GM( return new DrawMiniBitmapRectGM(false); )