blob: 547af5d2f8676d7edef06e2559eae75defb9a7b8 [file] [log] [blame]
reed499abab2015-08-20 12:30:20 -07001/*
2 * Copyright 2015 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"
Mike Reedac9f0c92020-12-23 10:11:33 -05009#include "include/core/SkBitmap.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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkData.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkEncodedImageFormat.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040016#include "include/core/SkImageInfo.h"
17#include "include/core/SkMatrix.h"
18#include "include/core/SkPaint.h"
19#include "include/core/SkPicture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/core/SkPictureRecorder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "include/core/SkRect.h"
22#include "include/core/SkRefCnt.h"
23#include "include/core/SkShader.h"
24#include "include/core/SkSize.h"
25#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040027#include "include/core/SkTileMode.h"
28#include "include/core/SkTypes.h"
29
30#include <utility>
31
reed499abab2015-08-20 12:30:20 -070032static void draw_something(SkCanvas* canvas, const SkRect& bounds) {
33 SkPaint paint;
34 paint.setAntiAlias(true);
35 paint.setColor(SK_ColorRED);
36 paint.setStyle(SkPaint::kStroke_Style);
37 paint.setStrokeWidth(10);
38 canvas->drawRect(bounds, paint);
39 paint.setStyle(SkPaint::kFill_Style);
40 paint.setColor(SK_ColorBLUE);
41 canvas->drawOval(bounds, paint);
42}
43
Robert Phillips16bf7d32020-07-07 10:20:27 -040044typedef sk_sp<SkImage> (*ImageMakerProc)(GrRecordingContext*, SkPicture*, const SkImageInfo&);
reed499abab2015-08-20 12:30:20 -070045
Robert Phillips16bf7d32020-07-07 10:20:27 -040046static sk_sp<SkImage> make_raster(GrRecordingContext*,
47 SkPicture* pic,
48 const SkImageInfo& info) {
reede8f30622016-03-23 18:59:25 -070049 auto surface(SkSurface::MakeRaster(info));
reed499abab2015-08-20 12:30:20 -070050 surface->getCanvas()->clear(0);
51 surface->getCanvas()->drawPicture(pic);
reed9ce9d672016-03-17 10:51:11 -070052 return surface->makeImageSnapshot();
reed499abab2015-08-20 12:30:20 -070053}
54
Robert Phillips16bf7d32020-07-07 10:20:27 -040055static sk_sp<SkImage> make_texture(GrRecordingContext* ctx,
56 SkPicture* pic,
57 const SkImageInfo& info) {
reed499abab2015-08-20 12:30:20 -070058 if (!ctx) {
59 return nullptr;
60 }
reede8f30622016-03-23 18:59:25 -070061 auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info));
scroggoa1b28332016-05-12 06:19:37 -070062 if (!surface) {
63 return nullptr;
64 }
reed499abab2015-08-20 12:30:20 -070065 surface->getCanvas()->clear(0);
66 surface->getCanvas()->drawPicture(pic);
reed9ce9d672016-03-17 10:51:11 -070067 return surface->makeImageSnapshot();
reed499abab2015-08-20 12:30:20 -070068}
69
Robert Phillips16bf7d32020-07-07 10:20:27 -040070static sk_sp<SkImage> make_pict_gen(GrRecordingContext*,
71 SkPicture* pic,
72 const SkImageInfo& info) {
Brian Osman138ea972016-12-16 11:55:18 -050073 return SkImage::MakeFromPicture(sk_ref_sp(pic), info.dimensions(), nullptr, nullptr,
Matt Sarette94255d2017-01-09 12:38:59 -050074 SkImage::BitDepth::kU8,
Matt Sarett77a7a1b2017-02-07 13:56:11 -050075 SkColorSpace::MakeSRGB());
reed499abab2015-08-20 12:30:20 -070076}
77
Robert Phillips16bf7d32020-07-07 10:20:27 -040078static sk_sp<SkImage> make_encode_gen(GrRecordingContext* ctx,
79 SkPicture* pic,
80 const SkImageInfo& info) {
reed9ce9d672016-03-17 10:51:11 -070081 sk_sp<SkImage> src(make_raster(ctx, pic, info));
reed499abab2015-08-20 12:30:20 -070082 if (!src) {
83 return nullptr;
84 }
Mike Reed6409f842017-07-11 16:03:13 -040085 sk_sp<SkData> encoded = src->encodeToData(SkEncodedImageFormat::kPNG, 100);
reed499abab2015-08-20 12:30:20 -070086 if (!encoded) {
87 return nullptr;
88 }
reed9ce9d672016-03-17 10:51:11 -070089 return SkImage::MakeFromEncoded(std::move(encoded));
reed499abab2015-08-20 12:30:20 -070090}
91
92const ImageMakerProc gProcs[] = {
93 make_raster,
94 make_texture,
95 make_pict_gen,
96 make_encode_gen,
97};
98
99/*
100 * Exercise drawing pictures inside an image, showing that the image version is pixelated
101 * (correctly) when it is inside an image.
102 */
103class ImageShaderGM : public skiagm::GM {
reedca2622b2016-03-18 07:25:55 -0700104 sk_sp<SkPicture> fPicture;
reed499abab2015-08-20 12:30:20 -0700105
106public:
107 ImageShaderGM() {}
108
109protected:
110 SkString onShortName() override {
111 return SkString("image-shader");
112 }
113
114 SkISize onISize() override {
115 return SkISize::Make(850, 450);
116 }
117
118 void onOnceBeforeDraw() override {
119 const SkRect bounds = SkRect::MakeWH(100, 100);
120 SkPictureRecorder recorder;
121 draw_something(recorder.beginRecording(bounds), bounds);
reedca2622b2016-03-18 07:25:55 -0700122 fPicture = recorder.finishRecordingAsPicture();
reed499abab2015-08-20 12:30:20 -0700123 }
124
125 void testImage(SkCanvas* canvas, SkImage* image) {
126 SkAutoCanvasRestore acr(canvas, true);
127
128 canvas->drawImage(image, 0, 0);
129 canvas->translate(0, 120);
130
Mike Reedfae8fce2019-04-03 10:27:45 -0400131 const SkTileMode tile = SkTileMode::kRepeat;
Mike Reed1f607332020-05-21 12:11:27 -0400132 const SkMatrix localM = SkMatrix::Translate(-50, -50);
reed499abab2015-08-20 12:30:20 -0700133 SkPaint paint;
Mike Reedb612b6c2020-12-08 21:58:35 -0500134 paint.setShader(image->makeShader(tile, tile, SkSamplingOptions(), &localM));
reed499abab2015-08-20 12:30:20 -0700135 paint.setAntiAlias(true);
reed499abab2015-08-20 12:30:20 -0700136 canvas->drawCircle(50, 50, 50, paint);
137 }
138
139 void onDraw(SkCanvas* canvas) override {
140 canvas->translate(20, 20);
141
142 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
143
144 for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) {
Robert Phillips16bf7d32020-07-07 10:20:27 -0400145 sk_sp<SkImage> image(gProcs[i](canvas->recordingContext(), fPicture.get(), info));
reed499abab2015-08-20 12:30:20 -0700146 if (image) {
reed9ce9d672016-03-17 10:51:11 -0700147 this->testImage(canvas, image.get());
reed499abab2015-08-20 12:30:20 -0700148 }
149 canvas->translate(120, 0);
150 }
151 }
152
153private:
John Stiles7571f9e2020-09-02 22:42:33 -0400154 using INHERITED = skiagm::GM;
reed499abab2015-08-20 12:30:20 -0700155};
156DEF_GM( return new ImageShaderGM; )
Mike Reed198e6ea2020-12-07 12:29:18 -0500157
158//////////////////////////////////////////////////////////////////////////////////////////////
159
160#include "tools/ToolUtils.h"
161
162static sk_sp<SkImage> make_checker_img(int w, int h, SkColor c0, SkColor c1, int size) {
163 SkBitmap bm = ToolUtils::create_checkerboard_bitmap(w, h, c0, c1, size);
164 bm.setImmutable();
Mike Reedac9f0c92020-12-23 10:11:33 -0500165 return bm.asImage();
Mike Reed198e6ea2020-12-07 12:29:18 -0500166}
167
168DEF_SIMPLE_GM(drawimage_sampling, canvas, 500, 500) {
169 constexpr int N = 256;
170 constexpr float kScale = 1.0f/6;
171 const SkRect dst = {0, 0, kScale*N, kScale*N};
172
173 auto img = make_checker_img(N, N, SK_ColorBLACK, SK_ColorWHITE, 7)->withDefaultMipmaps();
174 const SkRect src = SkRect::MakeIWH(img->width(), img->height());
175
Mike Reed2ac6ce82021-01-15 12:26:22 -0500176 SkMatrix mx = SkMatrix::RectToRect(src, dst);
Mike Reed198e6ea2020-12-07 12:29:18 -0500177
178 SkPaint paint;
179
180 for (auto mm : {SkMipmapMode::kNone, SkMipmapMode::kNearest, SkMipmapMode::kLinear}) {
181 for (auto fm : {SkFilterMode::kNearest, SkFilterMode::kLinear}) {
182 SkSamplingOptions sampling(fm, mm);
183
184 canvas->save();
185
186 canvas->save();
187 canvas->concat(mx);
188 canvas->drawImage(img.get(), 0, 0, sampling);
189 canvas->restore();
190
191 canvas->translate(dst.width() + 4, 0);
192
193 paint.setShader(img->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, sampling, &mx));
194 canvas->drawRect(dst, paint);
195
196 canvas->translate(dst.width() + 4, 0);
197
Mike Reed4f23dec2020-12-30 14:22:42 +0000198 canvas->drawImageRect(img.get(), src, dst, sampling, nullptr,
199 SkCanvas::kFast_SrcRectConstraint);
Mike Reed198e6ea2020-12-07 12:29:18 -0500200 canvas->restore();
201
202 canvas->translate(0, dst.height() + 8);
203 }
204 }
205
206}