blob: 2592131334ec48a30af55f0f112861f6f4ee4c57 [file] [log] [blame]
mike@reedtribe.org70e35902012-07-29 20:38:16 +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 */
7
bsalomon8e74f802016-01-30 10:01:40 -08008#include <functional>
mike@reedtribe.org70e35902012-07-29 20:38:16 +00009#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -040010#include "sk_tool_utils.h"
robertphillipsc5035e72016-03-17 06:58:39 -070011#include "SkAutoPixmapStorage.h"
Mike Reed5dd202d2018-02-06 23:05:36 +000012#include "SkColorPriv.h"
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000013#include "SkData.h"
reedf803da12015-01-23 05:58:07 -080014#include "SkCanvas.h"
15#include "SkRandom.h"
16#include "SkStream.h"
17#include "SkSurface.h"
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000018
robertphillips@google.com97b6b072012-10-31 14:48:39 +000019#include "GrContext.h"
reed@google.com97af1a62012-08-28 12:19:02 +000020
mike@reedtribe.org70e35902012-07-29 20:38:16 +000021static void drawContents(SkSurface* surface, SkColor fillC) {
skia.committer@gmail.com04ba4482012-09-07 02:01:30 +000022 SkSize size = SkSize::Make(SkIntToScalar(surface->width()),
robertphillips@google.com94acc702012-09-06 18:43:21 +000023 SkIntToScalar(surface->height()));
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000024 SkCanvas* canvas = surface->getCanvas();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000025
26 SkScalar stroke = size.fWidth / 10;
27 SkScalar radius = (size.fWidth - stroke) / 2;
28
29 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000030
mike@reedtribe.org70e35902012-07-29 20:38:16 +000031 paint.setAntiAlias(true);
32 paint.setColor(fillC);
33 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000034
mike@reedtribe.org70e35902012-07-29 20:38:16 +000035 paint.setStyle(SkPaint::kStroke_Style);
36 paint.setStrokeWidth(stroke);
37 paint.setColor(SK_ColorBLACK);
38 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
39}
40
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000041static void test_surface(SkCanvas* canvas, SkSurface* surf, bool usePaint) {
mike@reedtribe.org70e35902012-07-29 20:38:16 +000042 drawContents(surf, SK_ColorRED);
reed9ce9d672016-03-17 10:51:11 -070043 sk_sp<SkImage> imgR = surf->makeImageSnapshot();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000044
reed@google.com97af1a62012-08-28 12:19:02 +000045 if (true) {
reed9ce9d672016-03-17 10:51:11 -070046 sk_sp<SkImage> imgR2 = surf->makeImageSnapshot();
reed@google.com97af1a62012-08-28 12:19:02 +000047 SkASSERT(imgR == imgR2);
reed@google.com97af1a62012-08-28 12:19:02 +000048 }
49
mike@reedtribe.org70e35902012-07-29 20:38:16 +000050 drawContents(surf, SK_ColorGREEN);
reed9ce9d672016-03-17 10:51:11 -070051 sk_sp<SkImage> imgG = surf->makeImageSnapshot();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000052
reed@google.com97af1a62012-08-28 12:19:02 +000053 // since we've drawn after we snapped imgR, imgG will be a different obj
54 SkASSERT(imgR != imgG);
55
mike@reedtribe.org70e35902012-07-29 20:38:16 +000056 drawContents(surf, SK_ColorBLUE);
57
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000058 SkPaint paint;
59// paint.setFilterBitmap(true);
60// paint.setAlpha(0x80);
61
reedfe630452016-03-25 09:08:00 -070062 canvas->drawImage(imgR, 0, 0, usePaint ? &paint : nullptr);
63 canvas->drawImage(imgG, 0, 80, usePaint ? &paint : nullptr);
halcanary96fcdcc2015-08-27 07:41:13 -070064 surf->draw(canvas, 0, 160, usePaint ? &paint : nullptr);
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000065
66 SkRect src1, src2, src3;
67 src1.iset(0, 0, surf->width(), surf->height());
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +000068 src2.iset(-surf->width() / 2, -surf->height() / 2,
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000069 surf->width(), surf->height());
70 src3.iset(0, 0, surf->width() / 2, surf->height() / 2);
71
72 SkRect dst1, dst2, dst3, dst4;
73 dst1.set(0, 240, 65, 305);
74 dst2.set(0, 320, 65, 385);
75 dst3.set(0, 400, 65, 465);
76 dst4.set(0, 480, 65, 545);
77
reedfe630452016-03-25 09:08:00 -070078 canvas->drawImageRect(imgR, src1, dst1, usePaint ? &paint : nullptr);
79 canvas->drawImageRect(imgG, src2, dst2, usePaint ? &paint : nullptr);
80 canvas->drawImageRect(imgR, src3, dst3, usePaint ? &paint : nullptr);
81 canvas->drawImageRect(imgG, dst4, usePaint ? &paint : nullptr);
mike@reedtribe.org70e35902012-07-29 20:38:16 +000082}
83
84class ImageGM : public skiagm::GM {
85 void* fBuffer;
reed@google.com58b21ec2012-07-30 18:20:12 +000086 size_t fBufferSize;
mike@reedtribe.org70e35902012-07-29 20:38:16 +000087 SkSize fSize;
88 enum {
89 W = 64,
90 H = 64,
91 RB = W * 4 + 8,
92 };
93public:
94 ImageGM() {
reed@google.com58b21ec2012-07-30 18:20:12 +000095 fBufferSize = RB * H;
96 fBuffer = sk_malloc_throw(fBufferSize);
mike@reedtribe.org70e35902012-07-29 20:38:16 +000097 fSize.set(SkIntToScalar(W), SkIntToScalar(H));
98 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000099
Brian Salomond3b65972017-03-22 12:05:03 -0400100 ~ImageGM() override {
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000101 sk_free(fBuffer);
102 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000103
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000104protected:
mtklein36352bf2015-03-25 18:17:31 -0700105 SkString onShortName() override {
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000106 return SkString("image-surface");
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000107 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
mtklein36352bf2015-03-25 18:17:31 -0700109 SkISize onISize() override {
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000110 return SkISize::Make(960, 1200);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000111 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000112
mtklein36352bf2015-03-25 18:17:31 -0700113 void onDraw(SkCanvas* canvas) override {
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +0000114 canvas->scale(2, 2);
115
mtkleindbfd7ab2016-09-01 11:24:54 -0700116 const char* kLabel1 = "Original Img";
117 const char* kLabel2 = "Modified Img";
118 const char* kLabel3 = "Cur Surface";
119 const char* kLabel4 = "Full Crop";
120 const char* kLabel5 = "Over-crop";
121 const char* kLabel6 = "Upper-left";
122 const char* kLabel7 = "No Crop";
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000123
mtkleindbfd7ab2016-09-01 11:24:54 -0700124 const char* kLabel8 = "Pre-Alloc Img";
125 const char* kLabel9 = "New Alloc Img";
126 const char* kLabel10 = "GPU";
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000127
128 SkPaint textPaint;
commit-bot@chromium.orgcae54f12014-04-11 18:34:35 +0000129 textPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700130 sk_tool_utils::set_portable_typeface(&textPaint);
commit-bot@chromium.orgcae54f12014-04-11 18:34:35 +0000131 textPaint.setTextSize(8);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000132
Cary Clark2a475ea2017-04-28 15:35:12 -0400133 canvas->drawString(kLabel1, 10, 60, textPaint);
134 canvas->drawString(kLabel2, 10, 140, textPaint);
135 canvas->drawString(kLabel3, 10, 220, textPaint);
136 canvas->drawString(kLabel4, 10, 300, textPaint);
137 canvas->drawString(kLabel5, 10, 380, textPaint);
138 canvas->drawString(kLabel6, 10, 460, textPaint);
139 canvas->drawString(kLabel7, 10, 540, textPaint);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000140
Cary Clark2a475ea2017-04-28 15:35:12 -0400141 canvas->drawString(kLabel8, 80, 10, textPaint);
142 canvas->drawString(kLabel9, 160, 10, textPaint);
143 canvas->drawString(kLabel10, 265, 10, textPaint);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000144
145 canvas->translate(80, 20);
146
reed@google.com58b21ec2012-07-30 18:20:12 +0000147 // since we draw into this directly, we need to start fresh
148 sk_bzero(fBuffer, fBufferSize);
149
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000150 SkImageInfo info = SkImageInfo::MakeN32Premul(W, H);
reede8f30622016-03-23 18:59:25 -0700151 sk_sp<SkSurface> surf0(SkSurface::MakeRasterDirect(info, fBuffer, RB));
152 sk_sp<SkSurface> surf1(SkSurface::MakeRaster(info));
Brian Osmanc7ad40f2018-05-31 14:27:17 -0400153 sk_sp<SkSurface> surf2(SkSurface::MakeRenderTarget(canvas->getGrContext(),
154 SkBudgeted::kNo, info));
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000155
reede8f30622016-03-23 18:59:25 -0700156 test_surface(canvas, surf0.get(), true);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000157 canvas->translate(80, 0);
reede8f30622016-03-23 18:59:25 -0700158 test_surface(canvas, surf1.get(), true);
reeda9cb8712015-01-16 14:21:40 -0800159 if (surf2) {
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000160 canvas->translate(80, 0);
reede8f30622016-03-23 18:59:25 -0700161 test_surface(canvas, surf2.get(), true);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000162 }
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000163 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000164
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000165private:
166 typedef skiagm::GM INHERITED;
167};
reeda9cb8712015-01-16 14:21:40 -0800168DEF_GM( return new ImageGM; )
reed09553032015-11-23 12:32:16 -0800169
170///////////////////////////////////////////////////////////////////////////////////////////////////
171
172#include "SkPictureRecorder.h"
173
174static void draw_pixmap(SkCanvas* canvas, const SkPixmap& pmap) {
175 SkBitmap bitmap;
halcanarye36ec872015-12-09 11:36:59 -0800176 bitmap.installPixels(pmap);
reed09553032015-11-23 12:32:16 -0800177 canvas->drawBitmap(bitmap, 0, 0, nullptr);
178}
179
180static void show_scaled_pixels(SkCanvas* canvas, SkImage* image) {
181 SkAutoCanvasRestore acr(canvas, true);
182
183 canvas->drawImage(image, 0, 0, nullptr);
184 canvas->translate(110, 10);
185
186 const SkImageInfo info = SkImageInfo::MakeN32Premul(40, 40);
187 SkAutoPixmapStorage storage;
188 storage.alloc(info);
189
190 const SkImage::CachingHint chints[] = {
reed6868c3f2015-11-24 11:44:47 -0800191 SkImage::kAllow_CachingHint, SkImage::kDisallow_CachingHint,
reed09553032015-11-23 12:32:16 -0800192 };
193 const SkFilterQuality qualities[] = {
194 kNone_SkFilterQuality, kLow_SkFilterQuality, kMedium_SkFilterQuality, kHigh_SkFilterQuality,
195 };
196
197 for (auto ch : chints) {
198 canvas->save();
199 for (auto q : qualities) {
200 if (image->scalePixels(storage, q, ch)) {
201 draw_pixmap(canvas, storage);
202 }
203 canvas->translate(70, 0);
204 }
205 canvas->restore();
206 canvas->translate(0, 45);
207 }
208}
209
210static void draw_contents(SkCanvas* canvas) {
211 SkPaint paint;
212 paint.setStyle(SkPaint::kStroke_Style);
213 paint.setStrokeWidth(20);
214 canvas->drawCircle(50, 50, 35, paint);
215}
216
reed9ce9d672016-03-17 10:51:11 -0700217static sk_sp<SkImage> make_raster(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) {
reede8f30622016-03-23 18:59:25 -0700218 auto surface(SkSurface::MakeRaster(info));
reed7850eb22015-12-02 14:19:47 -0800219 draw(surface->getCanvas());
reed9ce9d672016-03-17 10:51:11 -0700220 return surface->makeImageSnapshot();
reed09553032015-11-23 12:32:16 -0800221}
222
reed9ce9d672016-03-17 10:51:11 -0700223static sk_sp<SkImage> make_picture(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) {
reed09553032015-11-23 12:32:16 -0800224 SkPictureRecorder recorder;
reed7850eb22015-12-02 14:19:47 -0800225 draw(recorder.beginRecording(SkRect::MakeIWH(info.width(), info.height())));
reedca2622b2016-03-18 07:25:55 -0700226 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
Matt Sarette94255d2017-01-09 12:38:59 -0500227 info.dimensions(), nullptr, nullptr, SkImage::BitDepth::kU8,
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500228 SkColorSpace::MakeSRGB());
reed09553032015-11-23 12:32:16 -0800229}
230
reed9ce9d672016-03-17 10:51:11 -0700231static sk_sp<SkImage> make_codec(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) {
232 sk_sp<SkImage> image(make_raster(info, nullptr, draw));
Mike Reed6409f842017-07-11 16:03:13 -0400233 return SkImage::MakeFromEncoded(image->encodeToData());
reed09553032015-11-23 12:32:16 -0800234}
235
reed9ce9d672016-03-17 10:51:11 -0700236static sk_sp<SkImage> make_gpu(const SkImageInfo& info, GrContext* ctx, void (*draw)(SkCanvas*)) {
reed09553032015-11-23 12:32:16 -0800237 if (!ctx) { return nullptr; }
reede8f30622016-03-23 18:59:25 -0700238 auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info));
scroggoe6f0d6e2016-05-13 07:25:44 -0700239 if (!surface) { return nullptr; }
reed7850eb22015-12-02 14:19:47 -0800240 draw(surface->getCanvas());
reed9ce9d672016-03-17 10:51:11 -0700241 return surface->makeImageSnapshot();
reed09553032015-11-23 12:32:16 -0800242}
243
reed9ce9d672016-03-17 10:51:11 -0700244typedef sk_sp<SkImage> (*ImageMakerProc)(const SkImageInfo&, GrContext*, void (*)(SkCanvas*));
reed09553032015-11-23 12:32:16 -0800245
246class ScalePixelsGM : public skiagm::GM {
247public:
248 ScalePixelsGM() {}
249
250protected:
251 SkString onShortName() override {
252 return SkString("scale-pixels");
253 }
254
255 SkISize onISize() override {
256 return SkISize::Make(960, 1200);
257 }
258
259 void onDraw(SkCanvas* canvas) override {
260 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
261
262 const ImageMakerProc procs[] = {
reed6868c3f2015-11-24 11:44:47 -0800263 make_codec, make_raster, make_picture, make_codec, make_gpu,
reed09553032015-11-23 12:32:16 -0800264 };
265 for (auto& proc : procs) {
reed9ce9d672016-03-17 10:51:11 -0700266 sk_sp<SkImage> image(proc(info, canvas->getGrContext(), draw_contents));
reed09553032015-11-23 12:32:16 -0800267 if (image) {
reed9ce9d672016-03-17 10:51:11 -0700268 show_scaled_pixels(canvas, image.get());
reed09553032015-11-23 12:32:16 -0800269 }
270 canvas->translate(0, 120);
271 }
272 }
halcanary9d524f22016-03-29 09:03:52 -0700273
reed09553032015-11-23 12:32:16 -0800274private:
275 typedef skiagm::GM INHERITED;
276};
277DEF_GM( return new ScalePixelsGM; )
reed7850eb22015-12-02 14:19:47 -0800278
279///////////////////////////////////////////////////////////////////////////////////////////////////
280
Brian Osmane8827d22017-02-07 12:31:02 -0500281DEF_SIMPLE_GM(new_texture_image, canvas, 280, 60) {
282 GrContext* context = canvas->getGrContext();
Brian Osman041f7df2017-02-07 11:23:28 -0500283 if (!context) {
284 skiagm::GM::DrawGpuOnlyMessage(canvas);
285 return;
286 }
287
288 auto render_image = [](SkCanvas* canvas) {
289 canvas->clear(SK_ColorBLUE);
290 SkPaint paint;
291 paint.setColor(SK_ColorRED);
292 canvas->drawRect(SkRect::MakeXYWH(10.f,10.f,10.f,10.f), paint);
293 paint.setColor(SK_ColorGREEN);
294 canvas->drawRect(SkRect::MakeXYWH(30.f,10.f,10.f,10.f), paint);
295 paint.setColor(SK_ColorYELLOW);
296 canvas->drawRect(SkRect::MakeXYWH(10.f,30.f,10.f,10.f), paint);
297 paint.setColor(SK_ColorCYAN);
298 canvas->drawRect(SkRect::MakeXYWH(30.f,30.f,10.f,10.f), paint);
299 };
300
301 static constexpr int kSize = 50;
302 SkBitmap bmp;
303 bmp.allocPixels(SkImageInfo::MakeS32(kSize, kSize, kPremul_SkAlphaType));
304 SkCanvas bmpCanvas(bmp);
305 render_image(&bmpCanvas);
306
307 std::function<sk_sp<SkImage>()> imageFactories[] = {
308 // Create sw raster image.
309 [bmp] {
310 return SkImage::MakeFromBitmap(bmp);
311 },
312 // Create encoded image.
313 [bmp] {
314 sk_sp<SkData> src(
315 sk_tool_utils::EncodeImageToData(bmp, SkEncodedImageFormat::kPNG, 100));
316 return SkImage::MakeFromEncoded(std::move(src));
317 },
Brian Osmane8827d22017-02-07 12:31:02 -0500318 // Create YUV encoded image.
319 [bmp] {
320 sk_sp<SkData> src(
321 sk_tool_utils::EncodeImageToData(bmp, SkEncodedImageFormat::kJPEG, 100));
322 return SkImage::MakeFromEncoded(std::move(src));
323 },
Brian Osman041f7df2017-02-07 11:23:28 -0500324 // Create a picture image.
325 [render_image] {
326 SkPictureRecorder recorder;
327 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kSize), SkIntToScalar(kSize));
328 render_image(canvas);
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500329 sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::MakeSRGB();
Brian Osman041f7df2017-02-07 11:23:28 -0500330 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
331 SkISize::Make(kSize, kSize), nullptr, nullptr,
332 SkImage::BitDepth::kU8, srgbColorSpace);
333 },
334 // Create a texture image
335 [context, render_image]() -> sk_sp<SkImage> {
336 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kYes,
337 SkImageInfo::MakeS32(kSize, kSize,
338 kPremul_SkAlphaType)));
339 if (!surface) {
340 return nullptr;
341 }
342 render_image(surface->getCanvas());
343 return surface->makeImageSnapshot();
344 }
345 };
346
347 constexpr SkScalar kPad = 5.f;
348 canvas->translate(kPad, kPad);
349 for (auto factory : imageFactories) {
350 auto image(factory());
Brian Osmane8827d22017-02-07 12:31:02 -0500351 if (image) {
Brian Osman041f7df2017-02-07 11:23:28 -0500352 sk_sp<SkImage> texImage(image->makeTextureImage(context,
353 canvas->imageInfo().colorSpace()));
354 if (texImage) {
355 canvas->drawImage(texImage, 0, 0);
356 }
357 }
Robert Phillips5bea6512017-02-10 08:04:06 -0500358 canvas->translate(kSize + kPad, 0);
Brian Osman041f7df2017-02-07 11:23:28 -0500359 }
360}
Mike Reedba61af92017-11-16 10:01:16 -0500361
362static void draw_pixmap(SkCanvas* canvas, const SkPixmap& pm, SkScalar x, SkScalar y) {
363 canvas->drawImage(SkImage::MakeRasterCopy(pm), x, y, nullptr);
364}
365
366static void slam_ff(const SkPixmap& pm) {
367 for (int y = 0; y < pm.height(); ++y) {
368 for (int x = 0; x < pm.width(); ++x) {
369 *pm.writable_addr32(x, y) = *pm.addr32(x, y) | SkPackARGB32(0xFF, 0, 0, 0);
370 }
371 }
372}
373
374DEF_SIMPLE_GM(scalepixels_unpremul, canvas, 1080, 280) {
375 SkImageInfo info = SkImageInfo::MakeN32(16, 16, kUnpremul_SkAlphaType);
376 SkAutoPixmapStorage pm;
377 pm.alloc(info);
378 for (int y = 0; y < 16; ++y) {
379 for (int x = 0; x < 16; ++x) {
380 *pm.writable_addr32(x, y) = SkPackARGB32NoCheck(0, (y << 4) | y, (x << 4) | x, 0xFF);
381 }
382 }
383 SkAutoPixmapStorage pm2;
384 pm2.alloc(SkImageInfo::MakeN32(256, 256, kUnpremul_SkAlphaType));
385
386 const SkFilterQuality qualities[] = {
387 kNone_SkFilterQuality, kLow_SkFilterQuality, kMedium_SkFilterQuality, kHigh_SkFilterQuality
388 };
389
390 for (auto fq : qualities) {
391 pm.scalePixels(pm2, fq);
392 slam_ff(pm2);
393 draw_pixmap(canvas, pm2, 10, 10);
394 canvas->translate(pm2.width() + 10.0f, 0);
395 }
396}
Mike Reedf2c73642018-05-29 15:41:27 -0400397
398///////////////////////////////////////////////////////////////////////////////////////////////////
399
400static sk_sp<SkImage> make_lazy_image(SkSurface* surf) {
401 surf->getCanvas()->drawCircle(100, 100, 100, SkPaint());
402 auto data = surf->makeImageSnapshot()->encodeToData();
403 return SkImage::MakeFromEncoded(data);
404}
405
406#include "SkWriteBuffer.h"
407#include "SkReadBuffer.h"
408static sk_sp<SkImage> serial_deserial(SkImage* img) {
409 SkBinaryWriteBuffer writer;
410 writer.writeImage(img);
411 size_t length = writer.bytesWritten();
412 auto data = SkData::MakeUninitialized(length);
413 writer.writeToMemory(data->writable_data());
414
415 SkReadBuffer reader(data->data(), length);
416 return reader.readImage();
417}
418
419DEF_SIMPLE_GM(image_subset, canvas, 440, 220) {
420 SkImageInfo info = SkImageInfo::MakeN32Premul(200, 200, nullptr);
421 auto surf = sk_tool_utils::makeSurface(canvas, info, nullptr);
422 auto img = make_lazy_image(surf.get());
423
424 canvas->drawImage(img, 10, 10, nullptr);
425 auto sub = img->makeSubset({100, 100, 200, 200});
426 canvas->drawImage(sub, 220, 10);
427 sub = serial_deserial(sub.get());
428 canvas->drawImage(sub, 220+110, 10);
429}