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 | |
| 8 | #include "gm.h" |
| 9 | #include "SkColorPriv.h" |
| 10 | #include "SkGradientShader.h" |
| 11 | #include "SkImage.h" |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 12 | #include "SkMathPriv.h" |
joshualitt | 2ba7099 | 2015-08-14 06:30:50 -0700 | [diff] [blame] | 13 | #include "SkRandom.h" |
| 14 | #include "SkShader.h" |
| 15 | #include "SkSurface.h" |
| 16 | |
Brian Osman | 876c841 | 2018-01-04 15:28:39 -0500 | [diff] [blame] | 17 | static sk_sp<SkImage> makebm(int w, int h) { |
joshualitt | 2ba7099 | 2015-08-14 06:30:50 -0700 | [diff] [blame] | 18 | SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); |
Brian Osman | 876c841 | 2018-01-04 15:28:39 -0500 | [diff] [blame] | 19 | auto surface(SkSurface::MakeRaster(info)); |
joshualitt | 2ba7099 | 2015-08-14 06:30:50 -0700 | [diff] [blame] | 20 | 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 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 29 | constexpr SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW, |
joshualitt | 2ba7099 | 2015-08-14 06:30:50 -0700 | [diff] [blame] | 30 | SK_ColorGREEN, SK_ColorMAGENTA, |
| 31 | SK_ColorBLUE, SK_ColorCYAN, |
| 32 | SK_ColorRED}; |
| 33 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 34 | constexpr SkScalar pos[] = {0, |
joshualitt | 2ba7099 | 2015-08-14 06:30:50 -0700 | [diff] [blame] | 35 | 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) { |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 47 | paint.setShader(SkGradientShader::MakeRadial( |
joshualitt | 2ba7099 | 2015-08-14 06:30:50 -0700 | [diff] [blame] | 48 | pt, radius, |
| 49 | colors, pos, |
| 50 | SK_ARRAY_COUNT(colors), |
| 51 | SkShader::kRepeat_TileMode, |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 52 | 0, &mat)); |
joshualitt | 2ba7099 | 2015-08-14 06:30:50 -0700 | [diff] [blame] | 53 | canvas->drawRect(rect, paint); |
| 54 | rect.inset(wScalar / 8, hScalar / 8); |
| 55 | mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4); |
| 56 | } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 57 | return surface->makeImageSnapshot(); |
joshualitt | 2ba7099 | 2015-08-14 06:30:50 -0700 | [diff] [blame] | 58 | } |
| 59 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 60 | constexpr int gSize = 1024; |
| 61 | constexpr int gSurfaceSize = 2048; |
joshualitt | 2ba7099 | 2015-08-14 06:30:50 -0700 | [diff] [blame] | 62 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 63 | // This GM calls drawImageRect several times using the same texture. This is intended to exercise |
| 64 | // combining GrDrawOps during these calls. |
joshualitt | 2ba7099 | 2015-08-14 06:30:50 -0700 | [diff] [blame] | 65 | class DrawMiniBitmapRectGM : public skiagm::GM { |
| 66 | public: |
| 67 | DrawMiniBitmapRectGM(bool antiAlias) : fAA(antiAlias) { |
| 68 | fName.set("drawminibitmaprect"); |
| 69 | if (fAA) { |
| 70 | fName.appendf("_aa"); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | protected: |
| 75 | SkString onShortName() override { return fName; } |
| 76 | |
| 77 | SkISize onISize() override { return SkISize::Make(gSize, gSize); } |
| 78 | |
| 79 | void onDraw(SkCanvas* canvas) override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 80 | if (nullptr == fImage) { |
Brian Osman | 876c841 | 2018-01-04 15:28:39 -0500 | [diff] [blame] | 81 | fImage = makebm(gSurfaceSize, gSurfaceSize); |
joshualitt | 2ba7099 | 2015-08-14 06:30:50 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | const SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)}; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 85 | const int kMaxSrcRectSize = 1 << (SkNextLog2(gSurfaceSize) + 2); |
joshualitt | 2ba7099 | 2015-08-14 06:30:50 -0700 | [diff] [blame] | 86 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 87 | constexpr int kPadX = 30; |
| 88 | constexpr int kPadY = 40; |
joshualitt | 2ba7099 | 2015-08-14 06:30:50 -0700 | [diff] [blame] | 89 | |
| 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 | } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 114 | canvas->drawImageRect(fImage.get(), srcRect, dstRect, &paint, |
joshualitt | 2ba7099 | 2015-08-14 06:30:50 -0700 | [diff] [blame] | 115 | 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 | |
| 131 | private: |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 132 | bool fAA; |
| 133 | sk_sp<SkImage> fImage; |
| 134 | SkString fName; |
joshualitt | 2ba7099 | 2015-08-14 06:30:50 -0700 | [diff] [blame] | 135 | |
| 136 | typedef skiagm::GM INHERITED; |
| 137 | }; |
| 138 | |
| 139 | DEF_GM( return new DrawMiniBitmapRectGM(true); ) |
| 140 | DEF_GM( return new DrawMiniBitmapRectGM(false); ) |