reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkBlendMode.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkCanvas.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 12 | #include "include/core/SkColor.h" |
| 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkPoint.h" |
| 15 | #include "include/core/SkRect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/core/SkShader.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 17 | #include "include/core/SkSize.h" |
| 18 | #include "include/core/SkString.h" |
| 19 | #include "include/core/SkTileMode.h" |
| 20 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "include/effects/SkGradientShader.h" |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 22 | |
| 23 | static void make_bitmap(SkBitmap* bitmap) { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 24 | bitmap->allocN32Pixels(64, 64); |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 25 | |
mike@reedtribe.org | 3bd2173 | 2012-09-26 02:45:10 +0000 | [diff] [blame] | 26 | SkCanvas canvas(*bitmap); |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 27 | |
| 28 | canvas.drawColor(SK_ColorRED); |
| 29 | SkPaint paint; |
| 30 | paint.setAntiAlias(true); |
| 31 | const SkPoint pts[] = { { 0, 0 }, { 64, 64 } }; |
| 32 | const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE }; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 33 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp)); |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 34 | canvas.drawCircle(32, 32, 32, paint); |
| 35 | } |
| 36 | |
| 37 | class DrawBitmapRect2 : public skiagm::GM { |
| 38 | bool fUseIRect; |
| 39 | public: |
| 40 | DrawBitmapRect2(bool useIRect) : fUseIRect(useIRect) { |
| 41 | } |
| 42 | |
| 43 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 44 | SkString onShortName() override { |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 45 | SkString str; |
| 46 | str.printf("bitmaprect_%s", fUseIRect ? "i" : "s"); |
| 47 | return str; |
| 48 | } |
| 49 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 50 | SkISize onISize() override { |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 51 | return SkISize::Make(640, 480); |
| 52 | } |
| 53 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | void onDraw(SkCanvas* canvas) override { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 55 | canvas->drawColor(0xFFCCCCCC); |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 56 | |
| 57 | const SkIRect src[] = { |
| 58 | { 0, 0, 32, 32 }, |
| 59 | { 0, 0, 80, 80 }, |
| 60 | { 32, 32, 96, 96 }, |
| 61 | { -32, -32, 32, 32, } |
| 62 | }; |
| 63 | |
| 64 | SkPaint paint; |
| 65 | paint.setStyle(SkPaint::kStroke_Style); |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 66 | |
| 67 | SkBitmap bitmap; |
| 68 | make_bitmap(&bitmap); |
| 69 | |
| 70 | SkRect dstR = { 0, 200, 128, 380 }; |
| 71 | |
| 72 | canvas->translate(16, 40); |
| 73 | for (size_t i = 0; i < SK_ARRAY_COUNT(src); i++) { |
| 74 | SkRect srcR; |
| 75 | srcR.set(src[i]); |
| 76 | |
| 77 | canvas->drawBitmap(bitmap, 0, 0, &paint); |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 78 | if (!fUseIRect) { |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 79 | canvas->drawBitmapRect(bitmap, srcR, dstR, &paint, |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 80 | SkCanvas::kStrict_SrcRectConstraint); |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 81 | } else { |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 82 | canvas->drawBitmapRect(bitmap, src[i], dstR, &paint); |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | canvas->drawRect(dstR, paint); |
| 86 | canvas->drawRect(srcR, paint); |
| 87 | |
| 88 | canvas->translate(160, 0); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | typedef skiagm::GM INHERITED; |
| 94 | }; |
| 95 | |
| 96 | ////////////////////////////////////////////////////////////////////////////// |
reed | 776c0cd | 2015-01-27 07:26:51 -0800 | [diff] [blame] | 97 | |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 98 | static void make_3x3_bitmap(SkBitmap* bitmap) { |
reed | 776c0cd | 2015-01-27 07:26:51 -0800 | [diff] [blame] | 99 | const int xSize = 3; |
| 100 | const int ySize = 3; |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 101 | |
reed | 776c0cd | 2015-01-27 07:26:51 -0800 | [diff] [blame] | 102 | const SkColor textureData[xSize][ySize] = { |
robertphillips@google.com | 93f0332 | 2012-12-03 17:35:19 +0000 | [diff] [blame] | 103 | { SK_ColorRED, SK_ColorWHITE, SK_ColorBLUE }, |
| 104 | { SK_ColorGREEN, SK_ColorBLACK, SK_ColorCYAN }, |
| 105 | { SK_ColorYELLOW, SK_ColorGRAY, SK_ColorMAGENTA } |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
reed | 776c0cd | 2015-01-27 07:26:51 -0800 | [diff] [blame] | 108 | bitmap->allocN32Pixels(xSize, ySize, true); |
| 109 | SkCanvas canvas(*bitmap); |
| 110 | SkPaint paint; |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 111 | |
reed | 776c0cd | 2015-01-27 07:26:51 -0800 | [diff] [blame] | 112 | for (int y = 0; y < ySize; y++) { |
| 113 | for (int x = 0; x < xSize; x++) { |
| 114 | paint.setColor(textureData[x][y]); |
| 115 | canvas.drawIRect(SkIRect::MakeXYWH(x, y, 1, 1), paint); |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 120 | // This GM attempts to make visible any issues drawBitmapRect may have |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 121 | // with partial source rects. In this case the eight pixels on the border |
| 122 | // should be half the width/height of the central pixel, i.e.: |
| 123 | // __|____|__ |
| 124 | // | | |
| 125 | // __|____|__ |
| 126 | // | | |
| 127 | class DrawBitmapRect3 : public skiagm::GM { |
| 128 | public: |
| 129 | DrawBitmapRect3() { |
| 130 | this->setBGColor(SK_ColorBLACK); |
| 131 | } |
| 132 | |
| 133 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 134 | SkString onShortName() override { |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 135 | SkString str; |
| 136 | str.printf("3x3bitmaprect"); |
| 137 | return str; |
| 138 | } |
| 139 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 140 | SkISize onISize() override { |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 141 | return SkISize::Make(640, 480); |
| 142 | } |
| 143 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 144 | void onDraw(SkCanvas* canvas) override { |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 145 | |
| 146 | SkBitmap bitmap; |
| 147 | make_3x3_bitmap(&bitmap); |
| 148 | |
| 149 | SkRect srcR = { 0.5f, 0.5f, 2.5f, 2.5f }; |
| 150 | SkRect dstR = { 100, 100, 300, 200 }; |
| 151 | |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 152 | canvas->drawBitmapRect(bitmap, srcR, dstR, nullptr, SkCanvas::kStrict_SrcRectConstraint); |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | private: |
| 156 | typedef skiagm::GM INHERITED; |
| 157 | }; |
| 158 | |
| 159 | ////////////////////////////////////////////////////////////////////////////// |
| 160 | static void make_big_bitmap(SkBitmap* bitmap) { |
| 161 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 162 | constexpr int gXSize = 4096; |
| 163 | constexpr int gYSize = 4096; |
| 164 | constexpr int gBorderWidth = 10; |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 165 | |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 166 | bitmap->allocN32Pixels(gXSize, gYSize); |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 167 | for (int y = 0; y < gYSize; ++y) { |
| 168 | for (int x = 0; x < gXSize; ++x) { |
skia.committer@gmail.com | 44d4988 | 2012-09-27 02:01:04 +0000 | [diff] [blame] | 169 | if (x <= gBorderWidth || x >= gXSize-gBorderWidth || |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 170 | y <= gBorderWidth || y >= gYSize-gBorderWidth) { |
reed | c6e13d7 | 2015-04-02 05:43:09 -0700 | [diff] [blame] | 171 | *bitmap->getAddr32(x, y) = SkPreMultiplyColor(0x88FFFFFF); |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 172 | } else { |
reed | c6e13d7 | 2015-04-02 05:43:09 -0700 | [diff] [blame] | 173 | *bitmap->getAddr32(x, y) = SkPreMultiplyColor(0x88FF0000); |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // This GM attempts to reveal any issues we may have when the GPU has to |
| 180 | // break up a large texture in order to draw it. The XOR transfer mode will |
| 181 | // create stripes in the image if there is imprecision in the destination |
| 182 | // tile placement. |
| 183 | class DrawBitmapRect4 : public skiagm::GM { |
| 184 | bool fUseIRect; |
commit-bot@chromium.org | 9a558d4 | 2014-05-30 15:06:24 +0000 | [diff] [blame] | 185 | SkBitmap fBigBitmap; |
| 186 | |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 187 | public: |
| 188 | DrawBitmapRect4(bool useIRect) : fUseIRect(useIRect) { |
| 189 | this->setBGColor(0x88444444); |
| 190 | } |
| 191 | |
| 192 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 193 | SkString onShortName() override { |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 194 | SkString str; |
| 195 | str.printf("bigbitmaprect_%s", fUseIRect ? "i" : "s"); |
| 196 | return str; |
| 197 | } |
| 198 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 199 | SkISize onISize() override { |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 200 | return SkISize::Make(640, 480); |
| 201 | } |
| 202 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 203 | void onOnceBeforeDraw() override { |
commit-bot@chromium.org | 9a558d4 | 2014-05-30 15:06:24 +0000 | [diff] [blame] | 204 | make_big_bitmap(&fBigBitmap); |
| 205 | } |
| 206 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 207 | void onDraw(SkCanvas* canvas) override { |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 208 | SkPaint paint; |
| 209 | paint.setAlpha(128); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 210 | paint.setBlendMode(SkBlendMode::kXor); |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 211 | |
robertphillips@google.com | ffad46b | 2012-10-01 14:32:51 +0000 | [diff] [blame] | 212 | SkRect srcR1 = { 0.0f, 0.0f, 4096.0f, 2040.0f }; |
| 213 | SkRect dstR1 = { 10.1f, 10.1f, 629.9f, 400.9f }; |
| 214 | |
| 215 | SkRect srcR2 = { 4085.0f, 10.0f, 4087.0f, 12.0f }; |
| 216 | SkRect dstR2 = { 10, 410, 30, 430 }; |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 217 | |
| 218 | if (!fUseIRect) { |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 219 | canvas->drawBitmapRect(fBigBitmap, srcR1, dstR1, &paint, |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 220 | SkCanvas::kStrict_SrcRectConstraint); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 221 | canvas->drawBitmapRect(fBigBitmap, srcR2, dstR2, &paint, |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 222 | SkCanvas::kStrict_SrcRectConstraint); |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 223 | } else { |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 224 | canvas->drawBitmapRect(fBigBitmap, srcR1.roundOut(), dstR1, &paint); |
| 225 | canvas->drawBitmapRect(fBigBitmap, srcR2.roundOut(), dstR2, &paint); |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | private: |
| 230 | typedef skiagm::GM INHERITED; |
| 231 | }; |
| 232 | |
reed | f786901 | 2014-12-01 13:54:01 -0800 | [diff] [blame] | 233 | class BitmapRectRounding : public skiagm::GM { |
| 234 | SkBitmap fBM; |
| 235 | |
| 236 | public: |
| 237 | BitmapRectRounding() {} |
| 238 | |
| 239 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 240 | SkString onShortName() override { |
reed | f786901 | 2014-12-01 13:54:01 -0800 | [diff] [blame] | 241 | SkString str; |
| 242 | str.printf("bitmaprect_rounding"); |
| 243 | return str; |
| 244 | } |
| 245 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 246 | SkISize onISize() override { |
reed | f786901 | 2014-12-01 13:54:01 -0800 | [diff] [blame] | 247 | return SkISize::Make(640, 480); |
| 248 | } |
| 249 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 250 | void onOnceBeforeDraw() override { |
reed | f786901 | 2014-12-01 13:54:01 -0800 | [diff] [blame] | 251 | fBM.allocN32Pixels(10, 10); |
| 252 | fBM.eraseColor(SK_ColorBLUE); |
| 253 | } |
| 254 | |
| 255 | // This choice of coordinates and matrix land the bottom edge of the clip (and bitmap dst) |
| 256 | // at exactly 1/2 pixel boundary. However, drawBitmapRect may lose precision along the way. |
| 257 | // If it does, we may see a red-line at the bottom, instead of the bitmap exactly matching |
| 258 | // the clip (in which case we should see all blue). |
| 259 | // The correct image should be all blue. |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 260 | void onDraw(SkCanvas* canvas) override { |
reed | f786901 | 2014-12-01 13:54:01 -0800 | [diff] [blame] | 261 | SkPaint paint; |
| 262 | paint.setColor(SK_ColorRED); |
| 263 | |
| 264 | const SkRect r = SkRect::MakeXYWH(1, 1, 110, 114); |
| 265 | canvas->scale(0.9f, 0.9f); |
| 266 | |
| 267 | // the drawRect shows the same problem as clipRect(r) followed by drawcolor(red) |
| 268 | canvas->drawRect(r, paint); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 269 | canvas->drawBitmapRect(fBM, r, nullptr); |
reed | f786901 | 2014-12-01 13:54:01 -0800 | [diff] [blame] | 270 | } |
mtklein | 1c40292 | 2015-01-23 11:07:07 -0800 | [diff] [blame] | 271 | |
reed | f786901 | 2014-12-01 13:54:01 -0800 | [diff] [blame] | 272 | private: |
| 273 | typedef skiagm::GM INHERITED; |
| 274 | }; |
reed | 0393912 | 2014-12-15 13:42:51 -0800 | [diff] [blame] | 275 | DEF_GM( return new BitmapRectRounding; ) |
reed | f786901 | 2014-12-01 13:54:01 -0800 | [diff] [blame] | 276 | |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 277 | ////////////////////////////////////////////////////////////////////////////// |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 278 | |
scroggo | 96f16e8 | 2015-12-10 13:31:59 -0800 | [diff] [blame] | 279 | DEF_GM( return new DrawBitmapRect2(false); ) |
| 280 | DEF_GM( return new DrawBitmapRect2(true); ) |
| 281 | DEF_GM( return new DrawBitmapRect3(); ) |
robertphillips@google.com | 21a95f1 | 2012-09-26 13:10:19 +0000 | [diff] [blame] | 282 | |
robertphillips@google.com | 653b0d6 | 2012-09-26 15:28:04 +0000 | [diff] [blame] | 283 | #ifndef SK_BUILD_FOR_ANDROID |
scroggo | 96f16e8 | 2015-12-10 13:31:59 -0800 | [diff] [blame] | 284 | DEF_GM( return new DrawBitmapRect4(false); ) |
| 285 | DEF_GM( return new DrawBitmapRect4(true); ) |
robertphillips@google.com | 653b0d6 | 2012-09-26 15:28:04 +0000 | [diff] [blame] | 286 | #endif |