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