blob: 474c8c55faeef02ebc8ff3328815bae3ff8dc416 [file] [log] [blame]
reed@google.com71121732012-09-18 15:14:33 +00001/*
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 */
reed2ad1aa62016-03-09 09:50:50 -08007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkBlendMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040012#include "include/core/SkColor.h"
13#include "include/core/SkPaint.h"
14#include "include/core/SkPoint.h"
15#include "include/core/SkRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkShader.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040017#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
Mike Reed6dbeac52021-01-13 13:14:23 -050019#include "include/core/SkSurface.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040020#include "include/core/SkTileMode.h"
21#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/effects/SkGradientShader.h"
reed@google.com71121732012-09-18 15:14:33 +000023
Mike Reed6dbeac52021-01-13 13:14:23 -050024static sk_sp<SkImage> make_image() {
25 auto surf = SkSurface::MakeRasterN32Premul(64, 64);
26 auto canvas = surf->getCanvas();
reed@google.com71121732012-09-18 15:14:33 +000027
Mike Reed6dbeac52021-01-13 13:14:23 -050028 canvas->drawColor(SK_ColorRED);
reed@google.com71121732012-09-18 15:14:33 +000029 SkPaint paint;
30 paint.setAntiAlias(true);
31 const SkPoint pts[] = { { 0, 0 }, { 64, 64 } };
32 const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
Mike Reedfae8fce2019-04-03 10:27:45 -040033 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp));
Mike Reed6dbeac52021-01-13 13:14:23 -050034 canvas->drawCircle(32, 32, 32, paint);
35
36 return surf->makeImageSnapshot();
reed@google.com71121732012-09-18 15:14:33 +000037}
38
39class DrawBitmapRect2 : public skiagm::GM {
40 bool fUseIRect;
41public:
42 DrawBitmapRect2(bool useIRect) : fUseIRect(useIRect) {
43 }
44
45protected:
mtklein36352bf2015-03-25 18:17:31 -070046 SkString onShortName() override {
reed@google.com71121732012-09-18 15:14:33 +000047 SkString str;
48 str.printf("bitmaprect_%s", fUseIRect ? "i" : "s");
49 return str;
50 }
51
mtklein36352bf2015-03-25 18:17:31 -070052 SkISize onISize() override {
reed@google.com71121732012-09-18 15:14:33 +000053 return SkISize::Make(640, 480);
54 }
55
mtklein36352bf2015-03-25 18:17:31 -070056 void onDraw(SkCanvas* canvas) override {
Mike Kleind46dce32018-08-16 10:17:03 -040057 canvas->drawColor(0xFFCCCCCC);
reed@google.com71121732012-09-18 15:14:33 +000058
59 const SkIRect src[] = {
60 { 0, 0, 32, 32 },
61 { 0, 0, 80, 80 },
62 { 32, 32, 96, 96 },
63 { -32, -32, 32, 32, }
64 };
65
66 SkPaint paint;
67 paint.setStyle(SkPaint::kStroke_Style);
reed@google.com71121732012-09-18 15:14:33 +000068
Mike Reed6dbeac52021-01-13 13:14:23 -050069 auto image = make_image();
reed@google.com71121732012-09-18 15:14:33 +000070
71 SkRect dstR = { 0, 200, 128, 380 };
72
73 canvas->translate(16, 40);
74 for (size_t i = 0; i < SK_ARRAY_COUNT(src); i++) {
75 SkRect srcR;
76 srcR.set(src[i]);
77
Mike Reed6dbeac52021-01-13 13:14:23 -050078 canvas->drawImage(image, 0, 0, &paint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +000079 if (!fUseIRect) {
Mike Reed6dbeac52021-01-13 13:14:23 -050080 canvas->drawImageRect(image, srcR, dstR, &paint,
81 SkCanvas::kStrict_SrcRectConstraint);
reed@google.com71121732012-09-18 15:14:33 +000082 } else {
Mike Reed6dbeac52021-01-13 13:14:23 -050083 canvas->drawImageRect(image, src[i], dstR, &paint);
reed@google.com71121732012-09-18 15:14:33 +000084 }
85
86 canvas->drawRect(dstR, paint);
87 canvas->drawRect(srcR, paint);
88
89 canvas->translate(160, 0);
90 }
91 }
92
93private:
John Stiles7571f9e2020-09-02 22:42:33 -040094 using INHERITED = skiagm::GM;
reed@google.com71121732012-09-18 15:14:33 +000095};
96
97//////////////////////////////////////////////////////////////////////////////
reed776c0cd2015-01-27 07:26:51 -080098
robertphillips@google.com21a95f12012-09-26 13:10:19 +000099static void make_3x3_bitmap(SkBitmap* bitmap) {
reed776c0cd2015-01-27 07:26:51 -0800100 const int xSize = 3;
101 const int ySize = 3;
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000102
reed776c0cd2015-01-27 07:26:51 -0800103 const SkColor textureData[xSize][ySize] = {
robertphillips@google.com93f03322012-12-03 17:35:19 +0000104 { SK_ColorRED, SK_ColorWHITE, SK_ColorBLUE },
105 { SK_ColorGREEN, SK_ColorBLACK, SK_ColorCYAN },
106 { SK_ColorYELLOW, SK_ColorGRAY, SK_ColorMAGENTA }
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000107 };
108
reed776c0cd2015-01-27 07:26:51 -0800109 bitmap->allocN32Pixels(xSize, ySize, true);
110 SkCanvas canvas(*bitmap);
111 SkPaint paint;
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000112
reed776c0cd2015-01-27 07:26:51 -0800113 for (int y = 0; y < ySize; y++) {
114 for (int x = 0; x < xSize; x++) {
115 paint.setColor(textureData[x][y]);
116 canvas.drawIRect(SkIRect::MakeXYWH(x, y, 1, 1), paint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000117 }
118 }
119}
120
reeda5517e22015-07-14 10:54:12 -0700121// This GM attempts to make visible any issues drawBitmapRect may have
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000122// with partial source rects. In this case the eight pixels on the border
123// should be half the width/height of the central pixel, i.e.:
124// __|____|__
125// | |
126// __|____|__
127// | |
128class DrawBitmapRect3 : public skiagm::GM {
129public:
130 DrawBitmapRect3() {
131 this->setBGColor(SK_ColorBLACK);
132 }
133
134protected:
mtklein36352bf2015-03-25 18:17:31 -0700135 SkString onShortName() override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000136 SkString str;
137 str.printf("3x3bitmaprect");
138 return str;
139 }
140
mtklein36352bf2015-03-25 18:17:31 -0700141 SkISize onISize() override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000142 return SkISize::Make(640, 480);
143 }
144
mtklein36352bf2015-03-25 18:17:31 -0700145 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000146
147 SkBitmap bitmap;
148 make_3x3_bitmap(&bitmap);
149
150 SkRect srcR = { 0.5f, 0.5f, 2.5f, 2.5f };
151 SkRect dstR = { 100, 100, 300, 200 };
152
reede47829b2015-08-06 10:02:53 -0700153 canvas->drawBitmapRect(bitmap, srcR, dstR, nullptr, SkCanvas::kStrict_SrcRectConstraint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000154 }
155
156private:
John Stiles7571f9e2020-09-02 22:42:33 -0400157 using INHERITED = skiagm::GM;
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000158};
159
160//////////////////////////////////////////////////////////////////////////////
161static void make_big_bitmap(SkBitmap* bitmap) {
162
mtkleindbfd7ab2016-09-01 11:24:54 -0700163 constexpr int gXSize = 4096;
164 constexpr int gYSize = 4096;
165 constexpr int gBorderWidth = 10;
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000166
reed@google.comeb9a46c2014-01-25 16:46:20 +0000167 bitmap->allocN32Pixels(gXSize, gYSize);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000168 for (int y = 0; y < gYSize; ++y) {
169 for (int x = 0; x < gXSize; ++x) {
skia.committer@gmail.com44d49882012-09-27 02:01:04 +0000170 if (x <= gBorderWidth || x >= gXSize-gBorderWidth ||
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000171 y <= gBorderWidth || y >= gYSize-gBorderWidth) {
reedc6e13d72015-04-02 05:43:09 -0700172 *bitmap->getAddr32(x, y) = SkPreMultiplyColor(0x88FFFFFF);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000173 } else {
reedc6e13d72015-04-02 05:43:09 -0700174 *bitmap->getAddr32(x, y) = SkPreMultiplyColor(0x88FF0000);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000175 }
176 }
177 }
178}
179
180// This GM attempts to reveal any issues we may have when the GPU has to
181// break up a large texture in order to draw it. The XOR transfer mode will
182// create stripes in the image if there is imprecision in the destination
183// tile placement.
184class DrawBitmapRect4 : public skiagm::GM {
185 bool fUseIRect;
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +0000186 SkBitmap fBigBitmap;
187
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000188public:
189 DrawBitmapRect4(bool useIRect) : fUseIRect(useIRect) {
190 this->setBGColor(0x88444444);
191 }
192
193protected:
mtklein36352bf2015-03-25 18:17:31 -0700194 SkString onShortName() override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000195 SkString str;
196 str.printf("bigbitmaprect_%s", fUseIRect ? "i" : "s");
197 return str;
198 }
199
mtklein36352bf2015-03-25 18:17:31 -0700200 SkISize onISize() override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000201 return SkISize::Make(640, 480);
202 }
203
mtklein36352bf2015-03-25 18:17:31 -0700204 void onOnceBeforeDraw() override {
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +0000205 make_big_bitmap(&fBigBitmap);
206 }
207
mtklein36352bf2015-03-25 18:17:31 -0700208 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000209 SkPaint paint;
210 paint.setAlpha(128);
reed374772b2016-10-05 17:33:02 -0700211 paint.setBlendMode(SkBlendMode::kXor);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000212
robertphillips@google.comffad46b2012-10-01 14:32:51 +0000213 SkRect srcR1 = { 0.0f, 0.0f, 4096.0f, 2040.0f };
214 SkRect dstR1 = { 10.1f, 10.1f, 629.9f, 400.9f };
215
216 SkRect srcR2 = { 4085.0f, 10.0f, 4087.0f, 12.0f };
217 SkRect dstR2 = { 10, 410, 30, 430 };
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000218
219 if (!fUseIRect) {
reede47829b2015-08-06 10:02:53 -0700220 canvas->drawBitmapRect(fBigBitmap, srcR1, dstR1, &paint,
reed84984ef2015-07-17 07:09:43 -0700221 SkCanvas::kStrict_SrcRectConstraint);
reede47829b2015-08-06 10:02:53 -0700222 canvas->drawBitmapRect(fBigBitmap, srcR2, dstR2, &paint,
reed84984ef2015-07-17 07:09:43 -0700223 SkCanvas::kStrict_SrcRectConstraint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000224 } else {
reed84984ef2015-07-17 07:09:43 -0700225 canvas->drawBitmapRect(fBigBitmap, srcR1.roundOut(), dstR1, &paint);
226 canvas->drawBitmapRect(fBigBitmap, srcR2.roundOut(), dstR2, &paint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000227 }
228 }
229
230private:
John Stiles7571f9e2020-09-02 22:42:33 -0400231 using INHERITED = skiagm::GM;
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000232};
233
reedf7869012014-12-01 13:54:01 -0800234class BitmapRectRounding : public skiagm::GM {
235 SkBitmap fBM;
236
237public:
238 BitmapRectRounding() {}
239
240protected:
mtklein36352bf2015-03-25 18:17:31 -0700241 SkString onShortName() override {
reedf7869012014-12-01 13:54:01 -0800242 SkString str;
243 str.printf("bitmaprect_rounding");
244 return str;
245 }
246
mtklein36352bf2015-03-25 18:17:31 -0700247 SkISize onISize() override {
reedf7869012014-12-01 13:54:01 -0800248 return SkISize::Make(640, 480);
249 }
250
mtklein36352bf2015-03-25 18:17:31 -0700251 void onOnceBeforeDraw() override {
reedf7869012014-12-01 13:54:01 -0800252 fBM.allocN32Pixels(10, 10);
253 fBM.eraseColor(SK_ColorBLUE);
254 }
255
256 // This choice of coordinates and matrix land the bottom edge of the clip (and bitmap dst)
257 // at exactly 1/2 pixel boundary. However, drawBitmapRect may lose precision along the way.
258 // If it does, we may see a red-line at the bottom, instead of the bitmap exactly matching
259 // the clip (in which case we should see all blue).
260 // The correct image should be all blue.
mtklein36352bf2015-03-25 18:17:31 -0700261 void onDraw(SkCanvas* canvas) override {
reedf7869012014-12-01 13:54:01 -0800262 SkPaint paint;
263 paint.setColor(SK_ColorRED);
264
265 const SkRect r = SkRect::MakeXYWH(1, 1, 110, 114);
266 canvas->scale(0.9f, 0.9f);
267
268 // the drawRect shows the same problem as clipRect(r) followed by drawcolor(red)
269 canvas->drawRect(r, paint);
reede47829b2015-08-06 10:02:53 -0700270 canvas->drawBitmapRect(fBM, r, nullptr);
reedf7869012014-12-01 13:54:01 -0800271 }
mtklein1c402922015-01-23 11:07:07 -0800272
reedf7869012014-12-01 13:54:01 -0800273private:
John Stiles7571f9e2020-09-02 22:42:33 -0400274 using INHERITED = skiagm::GM;
reedf7869012014-12-01 13:54:01 -0800275};
reed03939122014-12-15 13:42:51 -0800276DEF_GM( return new BitmapRectRounding; )
reedf7869012014-12-01 13:54:01 -0800277
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000278//////////////////////////////////////////////////////////////////////////////
reed@google.com71121732012-09-18 15:14:33 +0000279
scroggo96f16e82015-12-10 13:31:59 -0800280DEF_GM( return new DrawBitmapRect2(false); )
281DEF_GM( return new DrawBitmapRect2(true); )
282DEF_GM( return new DrawBitmapRect3(); )
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000283
robertphillips@google.com653b0d62012-09-26 15:28:04 +0000284#ifndef SK_BUILD_FOR_ANDROID
scroggo96f16e82015-12-10 13:31:59 -0800285DEF_GM( return new DrawBitmapRect4(false); )
286DEF_GM( return new DrawBitmapRect4(true); )
robertphillips@google.com653b0d62012-09-26 15:28:04 +0000287#endif