blob: ef324125c7feaed4d9dd5c4da27c004636800c33 [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
reed@google.com71121732012-09-18 15:14:33 +00008#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
reed@google.com71121732012-09-18 15:14:33 +000010#include "SkCanvas.h"
11#include "SkGradientShader.h"
12#include "SkGraphics.h"
13#include "SkPath.h"
14#include "SkRegion.h"
15#include "SkShader.h"
16
17static void make_bitmap(SkBitmap* bitmap) {
reed@google.comeb9a46c2014-01-25 16:46:20 +000018 bitmap->allocN32Pixels(64, 64);
reed@google.com71121732012-09-18 15:14:33 +000019
mike@reedtribe.org3bd21732012-09-26 02:45:10 +000020 SkCanvas canvas(*bitmap);
reed@google.com71121732012-09-18 15:14:33 +000021
22 canvas.drawColor(SK_ColorRED);
23 SkPaint paint;
24 paint.setAntiAlias(true);
25 const SkPoint pts[] = { { 0, 0 }, { 64, 64 } };
26 const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
reed2ad1aa62016-03-09 09:50:50 -080027 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
28 SkShader::kClamp_TileMode));
reed@google.com71121732012-09-18 15:14:33 +000029 canvas.drawCircle(32, 32, 32, paint);
30}
31
32class DrawBitmapRect2 : public skiagm::GM {
33 bool fUseIRect;
34public:
35 DrawBitmapRect2(bool useIRect) : fUseIRect(useIRect) {
36 }
37
38protected:
mtklein36352bf2015-03-25 18:17:31 -070039 SkString onShortName() override {
reed@google.com71121732012-09-18 15:14:33 +000040 SkString str;
41 str.printf("bitmaprect_%s", fUseIRect ? "i" : "s");
42 return str;
43 }
44
mtklein36352bf2015-03-25 18:17:31 -070045 SkISize onISize() override {
reed@google.com71121732012-09-18 15:14:33 +000046 return SkISize::Make(640, 480);
47 }
48
mtklein36352bf2015-03-25 18:17:31 -070049 void onDraw(SkCanvas* canvas) override {
caryclark12596012015-07-29 05:27:47 -070050 canvas->drawColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
reed@google.com71121732012-09-18 15:14:33 +000051
52 const SkIRect src[] = {
53 { 0, 0, 32, 32 },
54 { 0, 0, 80, 80 },
55 { 32, 32, 96, 96 },
56 { -32, -32, 32, 32, }
57 };
58
59 SkPaint paint;
60 paint.setStyle(SkPaint::kStroke_Style);
reed@google.com71121732012-09-18 15:14:33 +000061
62 SkBitmap bitmap;
63 make_bitmap(&bitmap);
64
65 SkRect dstR = { 0, 200, 128, 380 };
66
67 canvas->translate(16, 40);
68 for (size_t i = 0; i < SK_ARRAY_COUNT(src); i++) {
69 SkRect srcR;
70 srcR.set(src[i]);
71
72 canvas->drawBitmap(bitmap, 0, 0, &paint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +000073 if (!fUseIRect) {
reede47829b2015-08-06 10:02:53 -070074 canvas->drawBitmapRect(bitmap, srcR, dstR, &paint,
reeda5517e22015-07-14 10:54:12 -070075 SkCanvas::kStrict_SrcRectConstraint);
reed@google.com71121732012-09-18 15:14:33 +000076 } else {
reed84984ef2015-07-17 07:09:43 -070077 canvas->drawBitmapRect(bitmap, src[i], dstR, &paint);
reed@google.com71121732012-09-18 15:14:33 +000078 }
79
80 canvas->drawRect(dstR, paint);
81 canvas->drawRect(srcR, paint);
82
83 canvas->translate(160, 0);
84 }
85 }
86
87private:
88 typedef skiagm::GM INHERITED;
89};
90
91//////////////////////////////////////////////////////////////////////////////
reed776c0cd2015-01-27 07:26:51 -080092
robertphillips@google.com21a95f12012-09-26 13:10:19 +000093static void make_3x3_bitmap(SkBitmap* bitmap) {
reed776c0cd2015-01-27 07:26:51 -080094 const int xSize = 3;
95 const int ySize = 3;
robertphillips@google.com21a95f12012-09-26 13:10:19 +000096
reed776c0cd2015-01-27 07:26:51 -080097 const SkColor textureData[xSize][ySize] = {
robertphillips@google.com93f03322012-12-03 17:35:19 +000098 { SK_ColorRED, SK_ColorWHITE, SK_ColorBLUE },
99 { SK_ColorGREEN, SK_ColorBLACK, SK_ColorCYAN },
100 { SK_ColorYELLOW, SK_ColorGRAY, SK_ColorMAGENTA }
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000101 };
102
reed776c0cd2015-01-27 07:26:51 -0800103 bitmap->allocN32Pixels(xSize, ySize, true);
104 SkCanvas canvas(*bitmap);
105 SkPaint paint;
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000106
reed776c0cd2015-01-27 07:26:51 -0800107 for (int y = 0; y < ySize; y++) {
108 for (int x = 0; x < xSize; x++) {
109 paint.setColor(textureData[x][y]);
110 canvas.drawIRect(SkIRect::MakeXYWH(x, y, 1, 1), paint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000111 }
112 }
113}
114
reeda5517e22015-07-14 10:54:12 -0700115// This GM attempts to make visible any issues drawBitmapRect may have
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000116// with partial source rects. In this case the eight pixels on the border
117// should be half the width/height of the central pixel, i.e.:
118// __|____|__
119// | |
120// __|____|__
121// | |
122class DrawBitmapRect3 : public skiagm::GM {
123public:
124 DrawBitmapRect3() {
125 this->setBGColor(SK_ColorBLACK);
126 }
127
128protected:
mtklein36352bf2015-03-25 18:17:31 -0700129 SkString onShortName() override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000130 SkString str;
131 str.printf("3x3bitmaprect");
132 return str;
133 }
134
mtklein36352bf2015-03-25 18:17:31 -0700135 SkISize onISize() override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000136 return SkISize::Make(640, 480);
137 }
138
mtklein36352bf2015-03-25 18:17:31 -0700139 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000140
141 SkBitmap bitmap;
142 make_3x3_bitmap(&bitmap);
143
144 SkRect srcR = { 0.5f, 0.5f, 2.5f, 2.5f };
145 SkRect dstR = { 100, 100, 300, 200 };
146
reede47829b2015-08-06 10:02:53 -0700147 canvas->drawBitmapRect(bitmap, srcR, dstR, nullptr, SkCanvas::kStrict_SrcRectConstraint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000148 }
149
150private:
151 typedef skiagm::GM INHERITED;
152};
153
154//////////////////////////////////////////////////////////////////////////////
155static void make_big_bitmap(SkBitmap* bitmap) {
156
mtkleindbfd7ab2016-09-01 11:24:54 -0700157 constexpr int gXSize = 4096;
158 constexpr int gYSize = 4096;
159 constexpr int gBorderWidth = 10;
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000160
reed@google.comeb9a46c2014-01-25 16:46:20 +0000161 bitmap->allocN32Pixels(gXSize, gYSize);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000162 for (int y = 0; y < gYSize; ++y) {
163 for (int x = 0; x < gXSize; ++x) {
skia.committer@gmail.com44d49882012-09-27 02:01:04 +0000164 if (x <= gBorderWidth || x >= gXSize-gBorderWidth ||
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000165 y <= gBorderWidth || y >= gYSize-gBorderWidth) {
reedc6e13d72015-04-02 05:43:09 -0700166 *bitmap->getAddr32(x, y) = SkPreMultiplyColor(0x88FFFFFF);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000167 } else {
reedc6e13d72015-04-02 05:43:09 -0700168 *bitmap->getAddr32(x, y) = SkPreMultiplyColor(0x88FF0000);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000169 }
170 }
171 }
172}
173
174// This GM attempts to reveal any issues we may have when the GPU has to
175// break up a large texture in order to draw it. The XOR transfer mode will
176// create stripes in the image if there is imprecision in the destination
177// tile placement.
178class DrawBitmapRect4 : public skiagm::GM {
179 bool fUseIRect;
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +0000180 SkBitmap fBigBitmap;
181
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000182public:
183 DrawBitmapRect4(bool useIRect) : fUseIRect(useIRect) {
184 this->setBGColor(0x88444444);
185 }
186
187protected:
mtklein36352bf2015-03-25 18:17:31 -0700188 SkString onShortName() override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000189 SkString str;
190 str.printf("bigbitmaprect_%s", fUseIRect ? "i" : "s");
191 return str;
192 }
193
mtklein36352bf2015-03-25 18:17:31 -0700194 SkISize onISize() override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000195 return SkISize::Make(640, 480);
196 }
197
mtklein36352bf2015-03-25 18:17:31 -0700198 void onOnceBeforeDraw() override {
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +0000199 make_big_bitmap(&fBigBitmap);
200 }
201
mtklein36352bf2015-03-25 18:17:31 -0700202 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000203 SkPaint paint;
204 paint.setAlpha(128);
reed374772b2016-10-05 17:33:02 -0700205 paint.setBlendMode(SkBlendMode::kXor);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000206
robertphillips@google.comffad46b2012-10-01 14:32:51 +0000207 SkRect srcR1 = { 0.0f, 0.0f, 4096.0f, 2040.0f };
208 SkRect dstR1 = { 10.1f, 10.1f, 629.9f, 400.9f };
209
210 SkRect srcR2 = { 4085.0f, 10.0f, 4087.0f, 12.0f };
211 SkRect dstR2 = { 10, 410, 30, 430 };
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000212
213 if (!fUseIRect) {
reede47829b2015-08-06 10:02:53 -0700214 canvas->drawBitmapRect(fBigBitmap, srcR1, dstR1, &paint,
reed84984ef2015-07-17 07:09:43 -0700215 SkCanvas::kStrict_SrcRectConstraint);
reede47829b2015-08-06 10:02:53 -0700216 canvas->drawBitmapRect(fBigBitmap, srcR2, dstR2, &paint,
reed84984ef2015-07-17 07:09:43 -0700217 SkCanvas::kStrict_SrcRectConstraint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000218 } else {
reed84984ef2015-07-17 07:09:43 -0700219 canvas->drawBitmapRect(fBigBitmap, srcR1.roundOut(), dstR1, &paint);
220 canvas->drawBitmapRect(fBigBitmap, srcR2.roundOut(), dstR2, &paint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000221 }
222 }
223
224private:
225 typedef skiagm::GM INHERITED;
226};
227
reedf7869012014-12-01 13:54:01 -0800228class BitmapRectRounding : public skiagm::GM {
229 SkBitmap fBM;
230
231public:
232 BitmapRectRounding() {}
233
234protected:
mtklein36352bf2015-03-25 18:17:31 -0700235 SkString onShortName() override {
reedf7869012014-12-01 13:54:01 -0800236 SkString str;
237 str.printf("bitmaprect_rounding");
238 return str;
239 }
240
mtklein36352bf2015-03-25 18:17:31 -0700241 SkISize onISize() override {
reedf7869012014-12-01 13:54:01 -0800242 return SkISize::Make(640, 480);
243 }
244
mtklein36352bf2015-03-25 18:17:31 -0700245 void onOnceBeforeDraw() override {
reedf7869012014-12-01 13:54:01 -0800246 fBM.allocN32Pixels(10, 10);
247 fBM.eraseColor(SK_ColorBLUE);
248 }
249
250 // This choice of coordinates and matrix land the bottom edge of the clip (and bitmap dst)
251 // at exactly 1/2 pixel boundary. However, drawBitmapRect may lose precision along the way.
252 // If it does, we may see a red-line at the bottom, instead of the bitmap exactly matching
253 // the clip (in which case we should see all blue).
254 // The correct image should be all blue.
mtklein36352bf2015-03-25 18:17:31 -0700255 void onDraw(SkCanvas* canvas) override {
reedf7869012014-12-01 13:54:01 -0800256 SkPaint paint;
257 paint.setColor(SK_ColorRED);
258
259 const SkRect r = SkRect::MakeXYWH(1, 1, 110, 114);
260 canvas->scale(0.9f, 0.9f);
261
262 // the drawRect shows the same problem as clipRect(r) followed by drawcolor(red)
263 canvas->drawRect(r, paint);
reede47829b2015-08-06 10:02:53 -0700264 canvas->drawBitmapRect(fBM, r, nullptr);
reedf7869012014-12-01 13:54:01 -0800265 }
mtklein1c402922015-01-23 11:07:07 -0800266
reedf7869012014-12-01 13:54:01 -0800267private:
268 typedef skiagm::GM INHERITED;
269};
reed03939122014-12-15 13:42:51 -0800270DEF_GM( return new BitmapRectRounding; )
reedf7869012014-12-01 13:54:01 -0800271
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000272//////////////////////////////////////////////////////////////////////////////
reed@google.com71121732012-09-18 15:14:33 +0000273
scroggo96f16e82015-12-10 13:31:59 -0800274DEF_GM( return new DrawBitmapRect2(false); )
275DEF_GM( return new DrawBitmapRect2(true); )
276DEF_GM( return new DrawBitmapRect3(); )
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000277
robertphillips@google.com653b0d62012-09-26 15:28:04 +0000278#ifndef SK_BUILD_FOR_ANDROID
scroggo96f16e82015-12-10 13:31:59 -0800279DEF_GM( return new DrawBitmapRect4(false); )
280DEF_GM( return new DrawBitmapRect4(true); )
robertphillips@google.com653b0d62012-09-26 15:28:04 +0000281#endif