blob: 2a813004dff5205a74ec1e0444e6428724046b55 [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"
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 };
reed2ad1aa62016-03-09 09:50:50 -080026 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
27 SkShader::kClamp_TileMode));
reed@google.com71121732012-09-18 15:14:33 +000028 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 {
caryclark12596012015-07-29 05:27:47 -070049 canvas->drawColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
reed@google.com71121732012-09-18 15:14:33 +000050
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) {
reede47829b2015-08-06 10:02:53 -070073 canvas->drawBitmapRect(bitmap, srcR, dstR, &paint,
reeda5517e22015-07-14 10:54:12 -070074 SkCanvas::kStrict_SrcRectConstraint);
reed@google.com71121732012-09-18 15:14:33 +000075 } else {
reed84984ef2015-07-17 07:09:43 -070076 canvas->drawBitmapRect(bitmap, src[i], dstR, &paint);
reed@google.com71121732012-09-18 15:14:33 +000077 }
78
79 canvas->drawRect(dstR, paint);
80 canvas->drawRect(srcR, paint);
81
82 canvas->translate(160, 0);
83 }
84 }
85
86private:
87 typedef skiagm::GM INHERITED;
88};
89
90//////////////////////////////////////////////////////////////////////////////
reed776c0cd2015-01-27 07:26:51 -080091
robertphillips@google.com21a95f12012-09-26 13:10:19 +000092static void make_3x3_bitmap(SkBitmap* bitmap) {
reed776c0cd2015-01-27 07:26:51 -080093 const int xSize = 3;
94 const int ySize = 3;
robertphillips@google.com21a95f12012-09-26 13:10:19 +000095
reed776c0cd2015-01-27 07:26:51 -080096 const SkColor textureData[xSize][ySize] = {
robertphillips@google.com93f03322012-12-03 17:35:19 +000097 { SK_ColorRED, SK_ColorWHITE, SK_ColorBLUE },
98 { SK_ColorGREEN, SK_ColorBLACK, SK_ColorCYAN },
99 { SK_ColorYELLOW, SK_ColorGRAY, SK_ColorMAGENTA }
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000100 };
101
reed776c0cd2015-01-27 07:26:51 -0800102 bitmap->allocN32Pixels(xSize, ySize, true);
103 SkCanvas canvas(*bitmap);
104 SkPaint paint;
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000105
reed776c0cd2015-01-27 07:26:51 -0800106 for (int y = 0; y < ySize; y++) {
107 for (int x = 0; x < xSize; x++) {
108 paint.setColor(textureData[x][y]);
109 canvas.drawIRect(SkIRect::MakeXYWH(x, y, 1, 1), paint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000110 }
111 }
112}
113
reeda5517e22015-07-14 10:54:12 -0700114// This GM attempts to make visible any issues drawBitmapRect may have
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000115// with partial source rects. In this case the eight pixels on the border
116// should be half the width/height of the central pixel, i.e.:
117// __|____|__
118// | |
119// __|____|__
120// | |
121class DrawBitmapRect3 : public skiagm::GM {
122public:
123 DrawBitmapRect3() {
124 this->setBGColor(SK_ColorBLACK);
125 }
126
127protected:
mtklein36352bf2015-03-25 18:17:31 -0700128 SkString onShortName() override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000129 SkString str;
130 str.printf("3x3bitmaprect");
131 return str;
132 }
133
mtklein36352bf2015-03-25 18:17:31 -0700134 SkISize onISize() override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000135 return SkISize::Make(640, 480);
136 }
137
mtklein36352bf2015-03-25 18:17:31 -0700138 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000139
140 SkBitmap bitmap;
141 make_3x3_bitmap(&bitmap);
142
143 SkRect srcR = { 0.5f, 0.5f, 2.5f, 2.5f };
144 SkRect dstR = { 100, 100, 300, 200 };
145
reede47829b2015-08-06 10:02:53 -0700146 canvas->drawBitmapRect(bitmap, srcR, dstR, nullptr, SkCanvas::kStrict_SrcRectConstraint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000147 }
148
149private:
150 typedef skiagm::GM INHERITED;
151};
152
153//////////////////////////////////////////////////////////////////////////////
154static void make_big_bitmap(SkBitmap* bitmap) {
155
mtkleindbfd7ab2016-09-01 11:24:54 -0700156 constexpr int gXSize = 4096;
157 constexpr int gYSize = 4096;
158 constexpr int gBorderWidth = 10;
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000159
reed@google.comeb9a46c2014-01-25 16:46:20 +0000160 bitmap->allocN32Pixels(gXSize, gYSize);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000161 for (int y = 0; y < gYSize; ++y) {
162 for (int x = 0; x < gXSize; ++x) {
skia.committer@gmail.com44d49882012-09-27 02:01:04 +0000163 if (x <= gBorderWidth || x >= gXSize-gBorderWidth ||
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000164 y <= gBorderWidth || y >= gYSize-gBorderWidth) {
reedc6e13d72015-04-02 05:43:09 -0700165 *bitmap->getAddr32(x, y) = SkPreMultiplyColor(0x88FFFFFF);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000166 } else {
reedc6e13d72015-04-02 05:43:09 -0700167 *bitmap->getAddr32(x, y) = SkPreMultiplyColor(0x88FF0000);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000168 }
169 }
170 }
171}
172
173// This GM attempts to reveal any issues we may have when the GPU has to
174// break up a large texture in order to draw it. The XOR transfer mode will
175// create stripes in the image if there is imprecision in the destination
176// tile placement.
177class DrawBitmapRect4 : public skiagm::GM {
178 bool fUseIRect;
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +0000179 SkBitmap fBigBitmap;
180
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000181public:
182 DrawBitmapRect4(bool useIRect) : fUseIRect(useIRect) {
183 this->setBGColor(0x88444444);
184 }
185
186protected:
mtklein36352bf2015-03-25 18:17:31 -0700187 SkString onShortName() override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000188 SkString str;
189 str.printf("bigbitmaprect_%s", fUseIRect ? "i" : "s");
190 return str;
191 }
192
mtklein36352bf2015-03-25 18:17:31 -0700193 SkISize onISize() override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000194 return SkISize::Make(640, 480);
195 }
196
mtklein36352bf2015-03-25 18:17:31 -0700197 void onOnceBeforeDraw() override {
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +0000198 make_big_bitmap(&fBigBitmap);
199 }
200
mtklein36352bf2015-03-25 18:17:31 -0700201 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000202 SkPaint paint;
203 paint.setAlpha(128);
reedcfb6bdf2016-03-29 11:32:50 -0700204 paint.setXfermode(SkXfermode::Make(SkXfermode::kXor_Mode));
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000205
robertphillips@google.comffad46b2012-10-01 14:32:51 +0000206 SkRect srcR1 = { 0.0f, 0.0f, 4096.0f, 2040.0f };
207 SkRect dstR1 = { 10.1f, 10.1f, 629.9f, 400.9f };
208
209 SkRect srcR2 = { 4085.0f, 10.0f, 4087.0f, 12.0f };
210 SkRect dstR2 = { 10, 410, 30, 430 };
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000211
212 if (!fUseIRect) {
reede47829b2015-08-06 10:02:53 -0700213 canvas->drawBitmapRect(fBigBitmap, srcR1, dstR1, &paint,
reed84984ef2015-07-17 07:09:43 -0700214 SkCanvas::kStrict_SrcRectConstraint);
reede47829b2015-08-06 10:02:53 -0700215 canvas->drawBitmapRect(fBigBitmap, srcR2, dstR2, &paint,
reed84984ef2015-07-17 07:09:43 -0700216 SkCanvas::kStrict_SrcRectConstraint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000217 } else {
reed84984ef2015-07-17 07:09:43 -0700218 canvas->drawBitmapRect(fBigBitmap, srcR1.roundOut(), dstR1, &paint);
219 canvas->drawBitmapRect(fBigBitmap, srcR2.roundOut(), dstR2, &paint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000220 }
221 }
222
223private:
224 typedef skiagm::GM INHERITED;
225};
226
reedf7869012014-12-01 13:54:01 -0800227class BitmapRectRounding : public skiagm::GM {
228 SkBitmap fBM;
229
230public:
231 BitmapRectRounding() {}
232
233protected:
mtklein36352bf2015-03-25 18:17:31 -0700234 SkString onShortName() override {
reedf7869012014-12-01 13:54:01 -0800235 SkString str;
236 str.printf("bitmaprect_rounding");
237 return str;
238 }
239
mtklein36352bf2015-03-25 18:17:31 -0700240 SkISize onISize() override {
reedf7869012014-12-01 13:54:01 -0800241 return SkISize::Make(640, 480);
242 }
243
mtklein36352bf2015-03-25 18:17:31 -0700244 void onOnceBeforeDraw() override {
reedf7869012014-12-01 13:54:01 -0800245 fBM.allocN32Pixels(10, 10);
246 fBM.eraseColor(SK_ColorBLUE);
247 }
248
249 // This choice of coordinates and matrix land the bottom edge of the clip (and bitmap dst)
250 // at exactly 1/2 pixel boundary. However, drawBitmapRect may lose precision along the way.
251 // If it does, we may see a red-line at the bottom, instead of the bitmap exactly matching
252 // the clip (in which case we should see all blue).
253 // The correct image should be all blue.
mtklein36352bf2015-03-25 18:17:31 -0700254 void onDraw(SkCanvas* canvas) override {
reedf7869012014-12-01 13:54:01 -0800255 SkPaint paint;
256 paint.setColor(SK_ColorRED);
257
258 const SkRect r = SkRect::MakeXYWH(1, 1, 110, 114);
259 canvas->scale(0.9f, 0.9f);
260
261 // the drawRect shows the same problem as clipRect(r) followed by drawcolor(red)
262 canvas->drawRect(r, paint);
reede47829b2015-08-06 10:02:53 -0700263 canvas->drawBitmapRect(fBM, r, nullptr);
reedf7869012014-12-01 13:54:01 -0800264 }
mtklein1c402922015-01-23 11:07:07 -0800265
reedf7869012014-12-01 13:54:01 -0800266private:
267 typedef skiagm::GM INHERITED;
268};
reed03939122014-12-15 13:42:51 -0800269DEF_GM( return new BitmapRectRounding; )
reedf7869012014-12-01 13:54:01 -0800270
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000271//////////////////////////////////////////////////////////////////////////////
reed@google.com71121732012-09-18 15:14:33 +0000272
scroggo96f16e82015-12-10 13:31:59 -0800273DEF_GM( return new DrawBitmapRect2(false); )
274DEF_GM( return new DrawBitmapRect2(true); )
275DEF_GM( return new DrawBitmapRect3(); )
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000276
robertphillips@google.com653b0d62012-09-26 15:28:04 +0000277#ifndef SK_BUILD_FOR_ANDROID
scroggo96f16e82015-12-10 13:31:59 -0800278DEF_GM( return new DrawBitmapRect4(false); )
279DEF_GM( return new DrawBitmapRect4(true); )
robertphillips@google.com653b0d62012-09-26 15:28:04 +0000280#endif