blob: 4ed66a2c56050020012ca1c2adfe78864454cd62 [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"
Kevin Lubick4c491702023-04-24 09:08:06 -040013#include "include/core/SkImage.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040014#include "include/core/SkPaint.h"
15#include "include/core/SkPoint.h"
16#include "include/core/SkRect.h"
Kevin Lubick4c491702023-04-24 09:08:06 -040017#include "include/core/SkRefCnt.h"
18#include "include/core/SkSamplingOptions.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040019#include "include/core/SkSize.h"
20#include "include/core/SkString.h"
Mike Reed6dbeac52021-01-13 13:14:23 -050021#include "include/core/SkSurface.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040022#include "include/core/SkTileMode.h"
23#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/effects/SkGradientShader.h"
Kevin Lubick9b028372023-10-05 15:04:54 -040025#include "tools/GpuToolUtils.h"
Robert Phillips0fb10ab2022-04-20 14:57:03 -040026#include "tools/ToolUtils.h"
reed@google.com71121732012-09-18 15:14:33 +000027
Kevin Lubick4c491702023-04-24 09:08:06 -040028#include <cstddef>
29#include <iterator>
30
Robert Phillips0fb10ab2022-04-20 14:57:03 -040031static sk_sp<SkImage> make_image(SkCanvas* destCanvas) {
Kevin Lubick5c93acf2023-05-09 12:11:43 -040032 auto surf = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(64, 64));
Robert Phillips0fb10ab2022-04-20 14:57:03 -040033 auto tmpCanvas = surf->getCanvas();
reed@google.com71121732012-09-18 15:14:33 +000034
Robert Phillips0fb10ab2022-04-20 14:57:03 -040035 tmpCanvas->drawColor(SK_ColorRED);
reed@google.com71121732012-09-18 15:14:33 +000036 SkPaint paint;
37 paint.setAntiAlias(true);
38 const SkPoint pts[] = { { 0, 0 }, { 64, 64 } };
39 const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
Mike Reedfae8fce2019-04-03 10:27:45 -040040 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp));
Robert Phillips0fb10ab2022-04-20 14:57:03 -040041 tmpCanvas->drawCircle(32, 32, 32, paint);
Mike Reed6dbeac52021-01-13 13:14:23 -050042
Robert Phillips0fb10ab2022-04-20 14:57:03 -040043 return ToolUtils::MakeTextureImage(destCanvas, surf->makeImageSnapshot());
reed@google.com71121732012-09-18 15:14:33 +000044}
45
46class DrawBitmapRect2 : public skiagm::GM {
47 bool fUseIRect;
48public:
49 DrawBitmapRect2(bool useIRect) : fUseIRect(useIRect) {
50 }
51
52protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000053 SkString getName() const override {
reed@google.com71121732012-09-18 15:14:33 +000054 SkString str;
55 str.printf("bitmaprect_%s", fUseIRect ? "i" : "s");
56 return str;
57 }
58
Leandro Lovisolo8f023882023-08-15 21:13:52 +000059 SkISize getISize() override { return SkISize::Make(640, 480); }
reed@google.com71121732012-09-18 15:14:33 +000060
mtklein36352bf2015-03-25 18:17:31 -070061 void onDraw(SkCanvas* canvas) override {
Mike Kleind46dce32018-08-16 10:17:03 -040062 canvas->drawColor(0xFFCCCCCC);
reed@google.com71121732012-09-18 15:14:33 +000063
64 const SkIRect src[] = {
65 { 0, 0, 32, 32 },
66 { 0, 0, 80, 80 },
67 { 32, 32, 96, 96 },
68 { -32, -32, 32, 32, }
69 };
70
71 SkPaint paint;
72 paint.setStyle(SkPaint::kStroke_Style);
Mike Reede02d7f82021-01-21 22:25:21 -050073 auto sampling = SkSamplingOptions();
reed@google.com71121732012-09-18 15:14:33 +000074
Robert Phillips0fb10ab2022-04-20 14:57:03 -040075 auto image = make_image(canvas);
reed@google.com71121732012-09-18 15:14:33 +000076
77 SkRect dstR = { 0, 200, 128, 380 };
78
79 canvas->translate(16, 40);
Herb Derbyc37b3862022-06-21 09:49:17 -040080 for (size_t i = 0; i < std::size(src); i++) {
reed@google.com71121732012-09-18 15:14:33 +000081 SkRect srcR;
82 srcR.set(src[i]);
83
Mike Reede02d7f82021-01-21 22:25:21 -050084 canvas->drawImage(image, 0, 0, sampling, &paint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +000085 if (!fUseIRect) {
Mike Reede02d7f82021-01-21 22:25:21 -050086 canvas->drawImageRect(image.get(), srcR, dstR, sampling, &paint,
Mike Reed6dbeac52021-01-13 13:14:23 -050087 SkCanvas::kStrict_SrcRectConstraint);
reed@google.com71121732012-09-18 15:14:33 +000088 } else {
Mike Reede02d7f82021-01-21 22:25:21 -050089 canvas->drawImageRect(image.get(), SkRect::Make(src[i]), dstR, sampling, &paint,
90 SkCanvas::kStrict_SrcRectConstraint);
reed@google.com71121732012-09-18 15:14:33 +000091 }
92
93 canvas->drawRect(dstR, paint);
94 canvas->drawRect(srcR, paint);
95
96 canvas->translate(160, 0);
97 }
98 }
99
100private:
John Stiles7571f9e2020-09-02 22:42:33 -0400101 using INHERITED = skiagm::GM;
reed@google.com71121732012-09-18 15:14:33 +0000102};
103
104//////////////////////////////////////////////////////////////////////////////
reed776c0cd2015-01-27 07:26:51 -0800105
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000106static void make_3x3_bitmap(SkBitmap* bitmap) {
reed776c0cd2015-01-27 07:26:51 -0800107 const int xSize = 3;
108 const int ySize = 3;
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000109
reed776c0cd2015-01-27 07:26:51 -0800110 const SkColor textureData[xSize][ySize] = {
robertphillips@google.com93f03322012-12-03 17:35:19 +0000111 { SK_ColorRED, SK_ColorWHITE, SK_ColorBLUE },
112 { SK_ColorGREEN, SK_ColorBLACK, SK_ColorCYAN },
113 { SK_ColorYELLOW, SK_ColorGRAY, SK_ColorMAGENTA }
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000114 };
115
reed776c0cd2015-01-27 07:26:51 -0800116 bitmap->allocN32Pixels(xSize, ySize, true);
117 SkCanvas canvas(*bitmap);
118 SkPaint paint;
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000119
reed776c0cd2015-01-27 07:26:51 -0800120 for (int y = 0; y < ySize; y++) {
121 for (int x = 0; x < xSize; x++) {
122 paint.setColor(textureData[x][y]);
123 canvas.drawIRect(SkIRect::MakeXYWH(x, y, 1, 1), paint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000124 }
125 }
126}
127
reeda5517e22015-07-14 10:54:12 -0700128// This GM attempts to make visible any issues drawBitmapRect may have
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000129// with partial source rects. In this case the eight pixels on the border
130// should be half the width/height of the central pixel, i.e.:
131// __|____|__
132// | |
133// __|____|__
134// | |
135class DrawBitmapRect3 : public skiagm::GM {
136public:
137 DrawBitmapRect3() {
138 this->setBGColor(SK_ColorBLACK);
139 }
140
141protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000142 SkString getName() const override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000143 SkString str;
144 str.printf("3x3bitmaprect");
145 return str;
146 }
147
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000148 SkISize getISize() override { return SkISize::Make(640, 480); }
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000149
mtklein36352bf2015-03-25 18:17:31 -0700150 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000151
152 SkBitmap bitmap;
153 make_3x3_bitmap(&bitmap);
154
155 SkRect srcR = { 0.5f, 0.5f, 2.5f, 2.5f };
156 SkRect dstR = { 100, 100, 300, 200 };
157
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400158 canvas->drawImageRect(ToolUtils::MakeTextureImage(canvas, bitmap.asImage()),
159 srcR, dstR, SkSamplingOptions(),
Mike Reedfa582c82021-01-23 22:07:05 -0500160 nullptr, SkCanvas::kStrict_SrcRectConstraint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000161 }
162
163private:
John Stiles7571f9e2020-09-02 22:42:33 -0400164 using INHERITED = skiagm::GM;
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000165};
166
167//////////////////////////////////////////////////////////////////////////////
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400168static sk_sp<SkImage> make_big_bitmap(SkCanvas* canvas) {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000169
mtkleindbfd7ab2016-09-01 11:24:54 -0700170 constexpr int gXSize = 4096;
171 constexpr int gYSize = 4096;
172 constexpr int gBorderWidth = 10;
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000173
Mike Reedfa582c82021-01-23 22:07:05 -0500174 SkBitmap bitmap;
175 bitmap.allocN32Pixels(gXSize, gYSize);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000176 for (int y = 0; y < gYSize; ++y) {
177 for (int x = 0; x < gXSize; ++x) {
skia.committer@gmail.com44d49882012-09-27 02:01:04 +0000178 if (x <= gBorderWidth || x >= gXSize-gBorderWidth ||
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000179 y <= gBorderWidth || y >= gYSize-gBorderWidth) {
Mike Reedfa582c82021-01-23 22:07:05 -0500180 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(0x88FFFFFF);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000181 } else {
Mike Reedfa582c82021-01-23 22:07:05 -0500182 *bitmap.getAddr32(x, y) = SkPreMultiplyColor(0x88FF0000);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000183 }
184 }
185 }
Mike Reedfa582c82021-01-23 22:07:05 -0500186 bitmap.setImmutable();
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400187 return ToolUtils::MakeTextureImage(canvas, bitmap.asImage());
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000188}
189
190// This GM attempts to reveal any issues we may have when the GPU has to
191// break up a large texture in order to draw it. The XOR transfer mode will
192// create stripes in the image if there is imprecision in the destination
193// tile placement.
194class DrawBitmapRect4 : public skiagm::GM {
195 bool fUseIRect;
Mike Reedfa582c82021-01-23 22:07:05 -0500196 sk_sp<SkImage> fBigImage;
commit-bot@chromium.org9a558d42014-05-30 15:06:24 +0000197
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000198public:
199 DrawBitmapRect4(bool useIRect) : fUseIRect(useIRect) {
200 this->setBGColor(0x88444444);
201 }
202
203protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000204 SkString getName() const override {
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000205 SkString str;
206 str.printf("bigbitmaprect_%s", fUseIRect ? "i" : "s");
207 return str;
208 }
209
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000210 SkISize getISize() override { return SkISize::Make(640, 480); }
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000211
Robert Phillips163a7ea2022-04-19 20:55:02 +0000212 void onDraw(SkCanvas* canvas) override {
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400213 if (!fBigImage) {
214 fBigImage = make_big_bitmap(canvas);
215 }
216
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000217 SkPaint paint;
218 paint.setAlpha(128);
reed374772b2016-10-05 17:33:02 -0700219 paint.setBlendMode(SkBlendMode::kXor);
Mike Reedfa582c82021-01-23 22:07:05 -0500220 SkSamplingOptions sampling;
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000221
robertphillips@google.comffad46b2012-10-01 14:32:51 +0000222 SkRect srcR1 = { 0.0f, 0.0f, 4096.0f, 2040.0f };
223 SkRect dstR1 = { 10.1f, 10.1f, 629.9f, 400.9f };
224
225 SkRect srcR2 = { 4085.0f, 10.0f, 4087.0f, 12.0f };
226 SkRect dstR2 = { 10, 410, 30, 430 };
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000227
228 if (!fUseIRect) {
Mike Reedfa582c82021-01-23 22:07:05 -0500229 canvas->drawImageRect(fBigImage, srcR1, dstR1, sampling, &paint,
reed84984ef2015-07-17 07:09:43 -0700230 SkCanvas::kStrict_SrcRectConstraint);
Mike Reedfa582c82021-01-23 22:07:05 -0500231 canvas->drawImageRect(fBigImage, srcR2, dstR2, sampling, &paint,
reed84984ef2015-07-17 07:09:43 -0700232 SkCanvas::kStrict_SrcRectConstraint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000233 } else {
Mike Reedfa582c82021-01-23 22:07:05 -0500234 canvas->drawImageRect(fBigImage, SkRect::Make(srcR1.roundOut()), dstR1, sampling,
235 &paint, SkCanvas::kStrict_SrcRectConstraint);
236 canvas->drawImageRect(fBigImage, SkRect::Make(srcR2.roundOut()), dstR2, sampling,
237 &paint, SkCanvas::kStrict_SrcRectConstraint);
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000238 }
239 }
240
241private:
John Stiles7571f9e2020-09-02 22:42:33 -0400242 using INHERITED = skiagm::GM;
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000243};
244
reedf7869012014-12-01 13:54:01 -0800245class BitmapRectRounding : public skiagm::GM {
246 SkBitmap fBM;
247
248public:
249 BitmapRectRounding() {}
250
251protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000252 SkString getName() const override {
reedf7869012014-12-01 13:54:01 -0800253 SkString str;
254 str.printf("bitmaprect_rounding");
255 return str;
256 }
257
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000258 SkISize getISize() override { return SkISize::Make(640, 480); }
reedf7869012014-12-01 13:54:01 -0800259
mtklein36352bf2015-03-25 18:17:31 -0700260 void onOnceBeforeDraw() override {
reedf7869012014-12-01 13:54:01 -0800261 fBM.allocN32Pixels(10, 10);
262 fBM.eraseColor(SK_ColorBLUE);
263 }
264
265 // This choice of coordinates and matrix land the bottom edge of the clip (and bitmap dst)
266 // at exactly 1/2 pixel boundary. However, drawBitmapRect may lose precision along the way.
267 // If it does, we may see a red-line at the bottom, instead of the bitmap exactly matching
268 // the clip (in which case we should see all blue).
269 // The correct image should be all blue.
mtklein36352bf2015-03-25 18:17:31 -0700270 void onDraw(SkCanvas* canvas) override {
reedf7869012014-12-01 13:54:01 -0800271 SkPaint paint;
272 paint.setColor(SK_ColorRED);
273
274 const SkRect r = SkRect::MakeXYWH(1, 1, 110, 114);
275 canvas->scale(0.9f, 0.9f);
276
277 // the drawRect shows the same problem as clipRect(r) followed by drawcolor(red)
278 canvas->drawRect(r, paint);
Mike Reedfa582c82021-01-23 22:07:05 -0500279 canvas->drawImageRect(fBM.asImage(), r, SkSamplingOptions());
reedf7869012014-12-01 13:54:01 -0800280 }
mtklein1c402922015-01-23 11:07:07 -0800281
reedf7869012014-12-01 13:54:01 -0800282private:
John Stiles7571f9e2020-09-02 22:42:33 -0400283 using INHERITED = skiagm::GM;
reedf7869012014-12-01 13:54:01 -0800284};
reed03939122014-12-15 13:42:51 -0800285DEF_GM( return new BitmapRectRounding; )
reedf7869012014-12-01 13:54:01 -0800286
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000287//////////////////////////////////////////////////////////////////////////////
reed@google.com71121732012-09-18 15:14:33 +0000288
scroggo96f16e82015-12-10 13:31:59 -0800289DEF_GM( return new DrawBitmapRect2(false); )
290DEF_GM( return new DrawBitmapRect2(true); )
291DEF_GM( return new DrawBitmapRect3(); )
robertphillips@google.com21a95f12012-09-26 13:10:19 +0000292
robertphillips@google.com653b0d62012-09-26 15:28:04 +0000293#ifndef SK_BUILD_FOR_ANDROID
scroggo96f16e82015-12-10 13:31:59 -0800294DEF_GM( return new DrawBitmapRect4(false); )
295DEF_GM( return new DrawBitmapRect4(true); )
robertphillips@google.com653b0d62012-09-26 15:28:04 +0000296#endif