blob: 1280ccad6281cb3c9bde7329607bfb82bc351513 [file] [log] [blame]
reed@google.com7eb3a262012-08-07 14:05:14 +00001/*
2 * Copyright 2012 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 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlendMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkColor.h"
12#include "include/core/SkColorSpace.h"
13#include "include/core/SkImage.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkPicture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkPictureRecorder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040017#include "include/core/SkPoint.h"
18#include "include/core/SkRect.h"
19#include "include/core/SkRefCnt.h"
20#include "include/core/SkScalar.h"
21#include "include/core/SkShader.h"
22#include "include/core/SkSize.h"
23#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040025#include "include/core/SkTileMode.h"
Robert Phillips2ab26782023-06-15 14:22:11 -040026#include "include/core/SkTiledImageUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "include/effects/SkGradientShader.h"
Robert Phillips0fb10ab2022-04-20 14:57:03 -040028#include "tools/ToolUtils.h"
reed@google.com7eb3a262012-08-07 14:05:14 +000029
reed76289672015-09-09 11:29:09 -070030static void draw(SkCanvas* canvas, int width, int height, SkColor colors[2]) {
reed4228c1f2015-09-09 10:45:36 -070031 const SkPoint center = { SkIntToScalar(width)/2, SkIntToScalar(height)/2 };
32 const SkScalar radius = 40;
bsalomon@google.comaf562b42013-10-24 17:52:07 +000033 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -070034 paint.setShader(SkGradientShader::MakeRadial(center, radius, colors, nullptr, 2,
Mike Reedfae8fce2019-04-03 10:27:45 -040035 SkTileMode::kMirror));
reed374772b2016-10-05 17:33:02 -070036 paint.setBlendMode(SkBlendMode::kSrc);
reed76289672015-09-09 11:29:09 -070037 canvas->drawPaint(paint);
38}
39
reed9ce9d672016-03-17 10:51:11 -070040static sk_sp<SkImage> make_raster_image(int width, int height, SkColor colors[2]) {
Kevin Lubick5c93acf2023-05-09 12:11:43 -040041 auto surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(width, height)));
reed76289672015-09-09 11:29:09 -070042 draw(surface->getCanvas(), width, height, colors);
reed9ce9d672016-03-17 10:51:11 -070043 return surface->makeImageSnapshot();
reed@google.com7eb3a262012-08-07 14:05:14 +000044}
45
reed9ce9d672016-03-17 10:51:11 -070046static sk_sp<SkImage> make_picture_image(int width, int height, SkColor colors[2]) {
reed76289672015-09-09 11:29:09 -070047 SkPictureRecorder recorder;
48 draw(recorder.beginRecording(SkRect::MakeIWH(width, height)), width, height, colors);
Kevin Lubick77472bf2023-03-24 07:11:17 -040049 return SkImages::DeferredFromPicture(recorder.finishRecordingAsPicture(),
50 {width, height},
51 nullptr,
52 nullptr,
53 SkImages::BitDepth::kU8,
54 SkColorSpace::MakeSRGB());
reed76289672015-09-09 11:29:09 -070055}
56
reed9ce9d672016-03-17 10:51:11 -070057typedef sk_sp<SkImage> (*ImageMakerProc)(int width, int height, SkColor colors[2]);
reed76289672015-09-09 11:29:09 -070058
59static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2],
Robert Phillips2ab26782023-06-15 14:22:11 -040060 ImageMakerProc proc, bool manuallyTile) {
Robert Phillips64cb9c92023-06-28 13:59:07 -040061 sk_sp<SkImage> image = proc(width, height, colors);
Robert Phillips0fb10ab2022-04-20 14:57:03 -040062 if (!image) {
63 return;
64 }
reed@google.com7eb3a262012-08-07 14:05:14 +000065
Brian Salomond93f4a42016-11-14 14:41:58 -050066 SkPaint borderPaint;
reed@google.com7eb3a262012-08-07 14:05:14 +000067
Brian Salomond93f4a42016-11-14 14:41:58 -050068 borderPaint.setStyle(SkPaint::kStroke_Style);
reed@google.com7eb3a262012-08-07 14:05:14 +000069
Brian Salomond93f4a42016-11-14 14:41:58 -050070 SkRect dstRect = SkRect::MakeWH(128.f, 128.f);
reed@google.com7eb3a262012-08-07 14:05:14 +000071
72 canvas->save();
Brian Salomond93f4a42016-11-14 14:41:58 -050073 canvas->clipRect(dstRect);
Robert Phillips2ab26782023-06-15 14:22:11 -040074 if (manuallyTile) {
75 SkTiledImageUtils::DrawImage(canvas, image, 0, 0);
76 } else {
77 canvas->drawImage(image, 0, 0);
78 }
reed@google.com7eb3a262012-08-07 14:05:14 +000079 canvas->restore();
Brian Salomond93f4a42016-11-14 14:41:58 -050080 canvas->drawRect(dstRect, borderPaint);
reed@google.com7eb3a262012-08-07 14:05:14 +000081
Brian Salomond93f4a42016-11-14 14:41:58 -050082 dstRect.offset(SkIntToScalar(150), 0);
83 int hw = width / 2;
84 int hh = height / 2;
Mike Reed8d29ab62021-01-23 18:10:39 -050085 SkRect subset = SkRect::MakeLTRB(hw - 64, hh - 32, hw + 64, hh + 32);
Robert Phillips2ab26782023-06-15 14:22:11 -040086 if (manuallyTile) {
87 SkTiledImageUtils::DrawImageRect(canvas, image, subset, dstRect, {}, nullptr,
88 SkCanvas::kStrict_SrcRectConstraint);
89 } else {
90 canvas->drawImageRect(image, subset, dstRect, {}, nullptr,
91 SkCanvas::kStrict_SrcRectConstraint);
92 }
Brian Salomond93f4a42016-11-14 14:41:58 -050093 canvas->drawRect(dstRect, borderPaint);
reed@google.com7eb3a262012-08-07 14:05:14 +000094
Brian Salomond93f4a42016-11-14 14:41:58 -050095 dstRect.offset(SkIntToScalar(150), 0);
Robert Phillips2ab26782023-06-15 14:22:11 -040096 if (manuallyTile) {
97 SkTiledImageUtils::DrawImageRect(canvas, image, dstRect);
98 } else {
99 canvas->drawImageRect(image, dstRect, {});
100 }
Brian Salomond93f4a42016-11-14 14:41:58 -0500101 canvas->drawRect(dstRect, borderPaint);
reed@google.com7eb3a262012-08-07 14:05:14 +0000102}
103
104class VeryLargeBitmapGM : public skiagm::GM {
reed76289672015-09-09 11:29:09 -0700105 ImageMakerProc fProc;
Robert Phillips2ab26782023-06-15 14:22:11 -0400106 const char* fBaseName;
107 bool fManuallyTile;
reed76289672015-09-09 11:29:09 -0700108
reed@google.com7eb3a262012-08-07 14:05:14 +0000109public:
Robert Phillips2ab26782023-06-15 14:22:11 -0400110 VeryLargeBitmapGM(ImageMakerProc proc, const char baseName[], bool manuallyTile)
111 : fProc(proc)
112 , fBaseName(baseName)
113 , fManuallyTile(manuallyTile) {}
reed@google.com7eb3a262012-08-07 14:05:14 +0000114
Hal Canary594fe852019-07-18 13:35:49 -0400115private:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000116 SkString getName() const override {
Robert Phillips2ab26782023-06-15 14:22:11 -0400117 SkString name(fBaseName);
118
119 if (fManuallyTile) {
120 name.append("_manual");
121 }
122
123 return name;
124 }
reed@google.com7eb3a262012-08-07 14:05:14 +0000125
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000126 SkISize getISize() override { return {500, 600}; }
reed@google.com7eb3a262012-08-07 14:05:14 +0000127
mtklein36352bf2015-03-25 18:17:31 -0700128 void onDraw(SkCanvas* canvas) override {
bsalomon@google.comedf00c72013-10-24 20:55:14 +0000129 int veryBig = 65*1024; // 64K < size
130 int big = 33*1024; // 32K < size < 64K
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000131 // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons.
bsalomon@google.comedf00c72013-10-24 20:55:14 +0000132 int medium = 5*1024;
robertphillips@google.comc3dc12d2013-07-15 15:48:48 +0000133 int small = 150;
reed@google.com7eb3a262012-08-07 14:05:14 +0000134
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000135 SkColor colors[2];
136
reed@google.com7eb3a262012-08-07 14:05:14 +0000137 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000138 colors[0] = SK_ColorRED;
139 colors[1] = SK_ColorGREEN;
Robert Phillips2ab26782023-06-15 14:22:11 -0400140 show_image(canvas, small, small, colors, fProc, fManuallyTile);
reed@google.com7eb3a262012-08-07 14:05:14 +0000141 canvas->translate(0, SkIntToScalar(150));
142
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000143 colors[0] = SK_ColorBLUE;
144 colors[1] = SK_ColorMAGENTA;
Robert Phillips2ab26782023-06-15 14:22:11 -0400145 show_image(canvas, big, small, colors, fProc, fManuallyTile);
reed@google.com7eb3a262012-08-07 14:05:14 +0000146 canvas->translate(0, SkIntToScalar(150));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000147
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000148 colors[0] = SK_ColorMAGENTA;
149 colors[1] = SK_ColorYELLOW;
Robert Phillips2ab26782023-06-15 14:22:11 -0400150 show_image(canvas, medium, medium, colors, fProc, fManuallyTile);
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000151 canvas->translate(0, SkIntToScalar(150));
152
153 colors[0] = SK_ColorGREEN;
154 colors[1] = SK_ColorYELLOW;
Mike Kleindac694d2018-12-18 10:13:52 -0500155 // This used to be big enough that we didn't draw on CPU, but now we do.
Robert Phillips2ab26782023-06-15 14:22:11 -0400156 show_image(canvas, veryBig, small, colors, fProc, fManuallyTile);
reed@google.com7eb3a262012-08-07 14:05:14 +0000157 }
reed@google.com7eb3a262012-08-07 14:05:14 +0000158};
Hal Canary594fe852019-07-18 13:35:49 -0400159
Robert Phillips2ab26782023-06-15 14:22:11 -0400160DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "verylargebitmap",
161 /* manuallyTile= */ false); )
162DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "verylargebitmap",
163 /* manuallyTile= */ true); )
164DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "verylarge_picture_image",
165 /* manuallyTile= */ false); )
166DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "verylarge_picture_image",
167 /* manuallyTile= */ true); )