blob: aa71f48eb09ae115f82b0388ff40bcd83f424086 [file] [log] [blame]
reed@google.com71121732012-09-18 15:14:33 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "gm.h"
9#include "SkCanvas.h"
10#include "SkGradientShader.h"
11#include "SkGraphics.h"
12#include "SkPath.h"
13#include "SkRegion.h"
14#include "SkShader.h"
15
16static void make_bitmap(SkBitmap* bitmap) {
reed@google.comeb9a46c2014-01-25 16:46:20 +000017 bitmap->allocN32Pixels(64, 64);
reed@google.com71121732012-09-18 15:14:33 +000018
mike@reedtribe.org3bd21732012-09-26 02:45:10 +000019 SkCanvas canvas(*bitmap);
reed@google.com71121732012-09-18 15:14:33 +000020
21 canvas.drawColor(SK_ColorRED);
22 SkPaint paint;
23 paint.setAntiAlias(true);
24 const SkPoint pts[] = { { 0, 0 }, { 64, 64 } };
25 const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
26 paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2,
27 SkShader::kClamp_TileMode))->unref();
28 canvas.drawCircle(32, 32, 32, paint);
29}
30
31class DrawBitmapRect2 : public skiagm::GM {
32 bool fUseIRect;
33public:
34 DrawBitmapRect2(bool useIRect) : fUseIRect(useIRect) {
35 }
36
37protected:
mtklein36352bf2015-03-25 18:17:31 -070038 SkString onShortName() override {
reed@google.com71121732012-09-18 15:14:33 +000039 SkString str;
40 str.printf("bitmaprect_%s", fUseIRect ? "i" : "s");
41 return str;
42 }
43
mtklein36352bf2015-03-25 18:17:31 -070044 SkISize onISize() override {
reed@google.com71121732012-09-18 15:14:33 +000045 return SkISize::Make(640, 480);
46 }
47
mtklein36352bf2015-03-25 18:17:31 -070048 void onDraw(SkCanvas* canvas) override {
reed@google.com71121732012-09-18 15:14:33 +000049 canvas->drawColor(0xFFCCCCCC);
50
51 const SkIRect src[] = {
52 { 0, 0, 32, 32 },
53 { 0, 0, 80, 80 },
54 { 32, 32, 96, 96 },
55 { -32, -32, 32, 32, }
56 };
57
58 SkPaint paint;
59 paint.setStyle(SkPaint::kStroke_Style);
reed@google.com71121732012-09-18 15:14:33 +000060
61 SkBitmap bitmap;
62 make_bitmap(&bitmap);
63
64 SkRect dstR = { 0, 200, 128, 380 };
65
66 canvas->translate(16, 40);
67 for (size_t i = 0; i < SK_ARRAY_COUNT(src); i++) {
68 SkRect srcR;
69 srcR.set(src[i]);
70
71 canvas->drawBitmap(bitmap, 0, 0, &paint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +000072 if (!fUseIRect) {
reed@google.com71121732012-09-18 15:14:33 +000073 canvas->drawBitmapRectToRect(bitmap, &srcR, dstR, &paint);
74 } else {
75 canvas->drawBitmapRect(bitmap, &src[i], dstR, &paint);
76 }
77
78 canvas->drawRect(dstR, paint);
79 canvas->drawRect(srcR, paint);
80
81 canvas->translate(160, 0);
82 }
83 }
84
85private:
86 typedef skiagm::GM INHERITED;
87};
88
89//////////////////////////////////////////////////////////////////////////////
reed776c0cd2015-01-27 07:26:51 -080090
robertphillips@google.com21a95f12012-09-26 13:10:19 +000091static void make_3x3_bitmap(SkBitmap* bitmap) {
reed776c0cd2015-01-27 07:26:51 -080092 const int xSize = 3;
93 const int ySize = 3;
robertphillips@google.com21a95f12012-09-26 13:10:19 +000094
reed776c0cd2015-01-27 07:26:51 -080095 const SkColor textureData[xSize][ySize] = {
robertphillips@google.com93f03322012-12-03 17:35:19 +000096 { SK_ColorRED, SK_ColorWHITE, SK_ColorBLUE },
97 { SK_ColorGREEN, SK_ColorBLACK, SK_ColorCYAN },
98 { SK_ColorYELLOW, SK_ColorGRAY, SK_ColorMAGENTA }
robertphillips@google.com21a95f12012-09-26 13:10:19 +000099 };
100
reed776c0cd2015-01-27 07:26:51 -0800101 bitmap->allocN32Pixels(xSize, ySize, true);
102 SkCanvas canvas(*bitmap);
103 SkPaint paint;
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000104
reed776c0cd2015-01-27 07:26:51 -0800105 for (int y = 0; y < ySize; y++) {
106 for (int x = 0; x < xSize; x++) {
107 paint.setColor(textureData[x][y]);
108 canvas.drawIRect(SkIRect::MakeXYWH(x, y, 1, 1), paint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000109 }
110 }
111}
112
113// This GM attempts to make visible any issues drawBitmapRectToRect may have
114// with partial source rects. In this case the eight pixels on the border
115// should be half the width/height of the central pixel, i.e.:
116// __|____|__
117// | |
118// __|____|__
119// | |
120class DrawBitmapRect3 : public skiagm::GM {
121public:
122 DrawBitmapRect3() {
123 this->setBGColor(SK_ColorBLACK);
124 }
125
126protected:
mtklein36352bf2015-03-25 18:17:31 -0700127 SkString onShortName() override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000128 SkString str;
129 str.printf("3x3bitmaprect");
130 return str;
131 }
132
mtklein36352bf2015-03-25 18:17:31 -0700133 SkISize onISize() override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000134 return SkISize::Make(640, 480);
135 }
136
mtklein36352bf2015-03-25 18:17:31 -0700137 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000138
139 SkBitmap bitmap;
140 make_3x3_bitmap(&bitmap);
141
142 SkRect srcR = { 0.5f, 0.5f, 2.5f, 2.5f };
143 SkRect dstR = { 100, 100, 300, 200 };
144
145 canvas->drawBitmapRectToRect(bitmap, &srcR, dstR, NULL);
146 }
147
148private:
149 typedef skiagm::GM INHERITED;
150};
151
152//////////////////////////////////////////////////////////////////////////////
153static void make_big_bitmap(SkBitmap* bitmap) {
154
155 static const int gXSize = 4096;
156 static const int gYSize = 4096;
157 static const int gBorderWidth = 10;
158
reed@google.comeb9a46c2014-01-25 16:46:20 +0000159 bitmap->allocN32Pixels(gXSize, gYSize);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000160 for (int y = 0; y < gYSize; ++y) {
161 for (int x = 0; x < gXSize; ++x) {
skia.committer@gmail.com44d49882012-09-27 02:01:04 +0000162 if (x <= gBorderWidth || x >= gXSize-gBorderWidth ||
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000163 y <= gBorderWidth || y >= gYSize-gBorderWidth) {
reedc6e13d72015-04-02 05:43:09 -0700164 *bitmap->getAddr32(x, y) = SkPreMultiplyColor(0x88FFFFFF);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000165 } else {
reedc6e13d72015-04-02 05:43:09 -0700166 *bitmap->getAddr32(x, y) = SkPreMultiplyColor(0x88FF0000);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000167 }
168 }
169 }
170}
171
172// This GM attempts to reveal any issues we may have when the GPU has to
173// break up a large texture in order to draw it. The XOR transfer mode will
174// create stripes in the image if there is imprecision in the destination
175// tile placement.
176class DrawBitmapRect4 : public skiagm::GM {
177 bool fUseIRect;
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +0000178 SkBitmap fBigBitmap;
179
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000180public:
181 DrawBitmapRect4(bool useIRect) : fUseIRect(useIRect) {
182 this->setBGColor(0x88444444);
183 }
184
185protected:
mtklein36352bf2015-03-25 18:17:31 -0700186 SkString onShortName() override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000187 SkString str;
188 str.printf("bigbitmaprect_%s", fUseIRect ? "i" : "s");
189 return str;
190 }
191
mtklein36352bf2015-03-25 18:17:31 -0700192 SkISize onISize() override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000193 return SkISize::Make(640, 480);
194 }
195
mtklein36352bf2015-03-25 18:17:31 -0700196 void onOnceBeforeDraw() override {
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +0000197 make_big_bitmap(&fBigBitmap);
198 }
199
mtklein36352bf2015-03-25 18:17:31 -0700200 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000201
202 SkXfermode* mode = SkXfermode::Create(SkXfermode::kXor_Mode);
203
204 SkPaint paint;
205 paint.setAlpha(128);
robertphillips@google.com8fdb4c12012-10-02 11:47:16 +0000206 paint.setXfermode(mode)->unref();
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000207
robertphillips@google.comffad46b2012-10-01 14:32:51 +0000208 SkRect srcR1 = { 0.0f, 0.0f, 4096.0f, 2040.0f };
209 SkRect dstR1 = { 10.1f, 10.1f, 629.9f, 400.9f };
210
211 SkRect srcR2 = { 4085.0f, 10.0f, 4087.0f, 12.0f };
212 SkRect dstR2 = { 10, 410, 30, 430 };
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000213
214 if (!fUseIRect) {
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +0000215 canvas->drawBitmapRectToRect(fBigBitmap, &srcR1, dstR1, &paint);
216 canvas->drawBitmapRectToRect(fBigBitmap, &srcR2, dstR2, &paint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000217 } else {
robertphillips@google.comffad46b2012-10-01 14:32:51 +0000218 SkIRect iSrcR1, iSrcR2;
219
220 srcR1.roundOut(&iSrcR1);
221 srcR2.roundOut(&iSrcR2);
222
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +0000223 canvas->drawBitmapRect(fBigBitmap, &iSrcR1, dstR1, &paint);
224 canvas->drawBitmapRect(fBigBitmap, &iSrcR2, dstR2, &paint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000225 }
226 }
227
228private:
229 typedef skiagm::GM INHERITED;
230};
231
reedf7869012014-12-01 13:54:01 -0800232class BitmapRectRounding : public skiagm::GM {
233 SkBitmap fBM;
234
235public:
236 BitmapRectRounding() {}
237
238protected:
mtklein36352bf2015-03-25 18:17:31 -0700239 SkString onShortName() override {
reedf7869012014-12-01 13:54:01 -0800240 SkString str;
241 str.printf("bitmaprect_rounding");
242 return str;
243 }
244
mtklein36352bf2015-03-25 18:17:31 -0700245 SkISize onISize() override {
reedf7869012014-12-01 13:54:01 -0800246 return SkISize::Make(640, 480);
247 }
248
mtklein36352bf2015-03-25 18:17:31 -0700249 void onOnceBeforeDraw() override {
reedf7869012014-12-01 13:54:01 -0800250 fBM.allocN32Pixels(10, 10);
251 fBM.eraseColor(SK_ColorBLUE);
252 }
253
254 // This choice of coordinates and matrix land the bottom edge of the clip (and bitmap dst)
255 // at exactly 1/2 pixel boundary. However, drawBitmapRect may lose precision along the way.
256 // If it does, we may see a red-line at the bottom, instead of the bitmap exactly matching
257 // the clip (in which case we should see all blue).
258 // The correct image should be all blue.
mtklein36352bf2015-03-25 18:17:31 -0700259 void onDraw(SkCanvas* canvas) override {
reedf7869012014-12-01 13:54:01 -0800260 SkPaint paint;
261 paint.setColor(SK_ColorRED);
262
263 const SkRect r = SkRect::MakeXYWH(1, 1, 110, 114);
264 canvas->scale(0.9f, 0.9f);
265
266 // the drawRect shows the same problem as clipRect(r) followed by drawcolor(red)
267 canvas->drawRect(r, paint);
268 canvas->drawBitmapRect(fBM, NULL, r, NULL);
269 }
mtklein1c402922015-01-23 11:07:07 -0800270
reedf7869012014-12-01 13:54:01 -0800271private:
272 typedef skiagm::GM INHERITED;
273};
reed03939122014-12-15 13:42:51 -0800274DEF_GM( return new BitmapRectRounding; )
reedf7869012014-12-01 13:54:01 -0800275
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000276//////////////////////////////////////////////////////////////////////////////
reed@google.com71121732012-09-18 15:14:33 +0000277
278static skiagm::GM* MyFactory0(void*) { return new DrawBitmapRect2(false); }
279static skiagm::GM* MyFactory1(void*) { return new DrawBitmapRect2(true); }
280
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000281static skiagm::GM* MyFactory2(void*) { return new DrawBitmapRect3(); }
282
robertphillips@google.com653b0d62012-09-26 15:28:04 +0000283#ifndef SK_BUILD_FOR_ANDROID
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000284static skiagm::GM* MyFactory3(void*) { return new DrawBitmapRect4(false); }
285static skiagm::GM* MyFactory4(void*) { return new DrawBitmapRect4(true); }
robertphillips@google.com653b0d62012-09-26 15:28:04 +0000286#endif
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000287
reed@google.com71121732012-09-18 15:14:33 +0000288static skiagm::GMRegistry reg0(MyFactory0);
289static skiagm::GMRegistry reg1(MyFactory1);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000290
291static skiagm::GMRegistry reg2(MyFactory2);
292
robertphillips@google.com653b0d62012-09-26 15:28:04 +0000293#ifndef SK_BUILD_FOR_ANDROID
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000294static skiagm::GMRegistry reg3(MyFactory3);
295static skiagm::GMRegistry reg4(MyFactory4);
robertphillips@google.com653b0d62012-09-26 15:28:04 +0000296#endif
reedf7869012014-12-01 13:54:01 -0800297