blob: f1bb5a4d62451af61770263c4d7ff3e0bd28b50f [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"
robertphillipsc5035e72016-03-17 06:58:39 -070010#include "SkAutoPixmapStorage.h"
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000011#include "SkData.h"
reedf803da12015-01-23 05:58:07 -080012#include "SkCanvas.h"
13#include "SkRandom.h"
14#include "SkStream.h"
15#include "SkSurface.h"
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000016
robertphillips@google.com97b6b072012-10-31 14:48:39 +000017#if SK_SUPPORT_GPU
18#include "GrContext.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000019#endif
reed@google.com97af1a62012-08-28 12:19:02 +000020
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000021static void drawJpeg(SkCanvas* canvas, const SkISize& size) {
scroggo@google.comccd7afb2013-05-28 16:45:07 +000022 // TODO: Make this draw a file that is checked in, so it can
23 // be exercised on machines other than mike's. Will require a
24 // rebaseline.
reed9ce9d672016-03-17 10:51:11 -070025 sk_sp<SkData> data(SkData::MakeFromFileName("/Users/mike/Downloads/skia.google.jpeg"));
26 if (nullptr == data) {
reed48925e32014-09-18 13:57:05 -070027 return;
28 }
reed9ce9d672016-03-17 10:51:11 -070029 sk_sp<SkImage> image = SkImage::MakeFromEncoded(std::move(data));
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000030 if (image) {
31 SkAutoCanvasRestore acr(canvas, true);
32 canvas->scale(size.width() * 1.0f / image->width(),
33 size.height() * 1.0f / image->height());
halcanary96fcdcc2015-08-27 07:41:13 -070034 canvas->drawImage(image, 0, 0, nullptr);
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000035 }
36}
mike@reedtribe.org70e35902012-07-29 20:38:16 +000037
38static void drawContents(SkSurface* surface, SkColor fillC) {
skia.committer@gmail.com04ba4482012-09-07 02:01:30 +000039 SkSize size = SkSize::Make(SkIntToScalar(surface->width()),
robertphillips@google.com94acc702012-09-06 18:43:21 +000040 SkIntToScalar(surface->height()));
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000041 SkCanvas* canvas = surface->getCanvas();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000042
43 SkScalar stroke = size.fWidth / 10;
44 SkScalar radius = (size.fWidth - stroke) / 2;
45
46 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000047
mike@reedtribe.org70e35902012-07-29 20:38:16 +000048 paint.setAntiAlias(true);
49 paint.setColor(fillC);
50 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000051
mike@reedtribe.org70e35902012-07-29 20:38:16 +000052 paint.setStyle(SkPaint::kStroke_Style);
53 paint.setStrokeWidth(stroke);
54 paint.setColor(SK_ColorBLACK);
55 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
56}
57
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000058static void test_surface(SkCanvas* canvas, SkSurface* surf, bool usePaint) {
mike@reedtribe.org70e35902012-07-29 20:38:16 +000059 drawContents(surf, SK_ColorRED);
reed9ce9d672016-03-17 10:51:11 -070060 sk_sp<SkImage> imgR = surf->makeImageSnapshot();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000061
reed@google.com97af1a62012-08-28 12:19:02 +000062 if (true) {
reed9ce9d672016-03-17 10:51:11 -070063 sk_sp<SkImage> imgR2 = surf->makeImageSnapshot();
reed@google.com97af1a62012-08-28 12:19:02 +000064 SkASSERT(imgR == imgR2);
reed@google.com97af1a62012-08-28 12:19:02 +000065 }
66
mike@reedtribe.org70e35902012-07-29 20:38:16 +000067 drawContents(surf, SK_ColorGREEN);
reed9ce9d672016-03-17 10:51:11 -070068 sk_sp<SkImage> imgG = surf->makeImageSnapshot();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000069
reed@google.com97af1a62012-08-28 12:19:02 +000070 // since we've drawn after we snapped imgR, imgG will be a different obj
71 SkASSERT(imgR != imgG);
72
mike@reedtribe.org70e35902012-07-29 20:38:16 +000073 drawContents(surf, SK_ColorBLUE);
74
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000075 SkPaint paint;
76// paint.setFilterBitmap(true);
77// paint.setAlpha(0x80);
78
reedfe630452016-03-25 09:08:00 -070079 canvas->drawImage(imgR, 0, 0, usePaint ? &paint : nullptr);
80 canvas->drawImage(imgG, 0, 80, usePaint ? &paint : nullptr);
halcanary96fcdcc2015-08-27 07:41:13 -070081 surf->draw(canvas, 0, 160, usePaint ? &paint : nullptr);
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000082
83 SkRect src1, src2, src3;
84 src1.iset(0, 0, surf->width(), surf->height());
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +000085 src2.iset(-surf->width() / 2, -surf->height() / 2,
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000086 surf->width(), surf->height());
87 src3.iset(0, 0, surf->width() / 2, surf->height() / 2);
88
89 SkRect dst1, dst2, dst3, dst4;
90 dst1.set(0, 240, 65, 305);
91 dst2.set(0, 320, 65, 385);
92 dst3.set(0, 400, 65, 465);
93 dst4.set(0, 480, 65, 545);
94
reedfe630452016-03-25 09:08:00 -070095 canvas->drawImageRect(imgR, src1, dst1, usePaint ? &paint : nullptr);
96 canvas->drawImageRect(imgG, src2, dst2, usePaint ? &paint : nullptr);
97 canvas->drawImageRect(imgR, src3, dst3, usePaint ? &paint : nullptr);
98 canvas->drawImageRect(imgG, dst4, usePaint ? &paint : nullptr);
mike@reedtribe.org70e35902012-07-29 20:38:16 +000099}
100
101class ImageGM : public skiagm::GM {
102 void* fBuffer;
reed@google.com58b21ec2012-07-30 18:20:12 +0000103 size_t fBufferSize;
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000104 SkSize fSize;
105 enum {
106 W = 64,
107 H = 64,
108 RB = W * 4 + 8,
109 };
110public:
111 ImageGM() {
reed@google.com58b21ec2012-07-30 18:20:12 +0000112 fBufferSize = RB * H;
113 fBuffer = sk_malloc_throw(fBufferSize);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000114 fSize.set(SkIntToScalar(W), SkIntToScalar(H));
115 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000117 virtual ~ImageGM() {
118 sk_free(fBuffer);
119 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000120
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000121protected:
mtklein36352bf2015-03-25 18:17:31 -0700122 SkString onShortName() override {
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000123 return SkString("image-surface");
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000124 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000125
mtklein36352bf2015-03-25 18:17:31 -0700126 SkISize onISize() override {
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000127 return SkISize::Make(960, 1200);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000128 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129
mtklein36352bf2015-03-25 18:17:31 -0700130 void onDraw(SkCanvas* canvas) override {
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +0000131 drawJpeg(canvas, this->getISize());
132
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +0000133 canvas->scale(2, 2);
134
mtkleindbfd7ab2016-09-01 11:24:54 -0700135 const char* kLabel1 = "Original Img";
136 const char* kLabel2 = "Modified Img";
137 const char* kLabel3 = "Cur Surface";
138 const char* kLabel4 = "Full Crop";
139 const char* kLabel5 = "Over-crop";
140 const char* kLabel6 = "Upper-left";
141 const char* kLabel7 = "No Crop";
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000142
mtkleindbfd7ab2016-09-01 11:24:54 -0700143 const char* kLabel8 = "Pre-Alloc Img";
144 const char* kLabel9 = "New Alloc Img";
145 const char* kLabel10 = "GPU";
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000146
147 SkPaint textPaint;
commit-bot@chromium.orgcae54f12014-04-11 18:34:35 +0000148 textPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700149 sk_tool_utils::set_portable_typeface(&textPaint);
commit-bot@chromium.orgcae54f12014-04-11 18:34:35 +0000150 textPaint.setTextSize(8);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000151
152 canvas->drawText(kLabel1, strlen(kLabel1), 10, 60, textPaint);
153 canvas->drawText(kLabel2, strlen(kLabel2), 10, 140, textPaint);
154 canvas->drawText(kLabel3, strlen(kLabel3), 10, 220, textPaint);
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000155 canvas->drawText(kLabel4, strlen(kLabel4), 10, 300, textPaint);
156 canvas->drawText(kLabel5, strlen(kLabel5), 10, 380, textPaint);
157 canvas->drawText(kLabel6, strlen(kLabel6), 10, 460, textPaint);
158 canvas->drawText(kLabel7, strlen(kLabel7), 10, 540, textPaint);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000159
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000160 canvas->drawText(kLabel8, strlen(kLabel8), 80, 10, textPaint);
161 canvas->drawText(kLabel9, strlen(kLabel9), 160, 10, textPaint);
reeda9cb8712015-01-16 14:21:40 -0800162 canvas->drawText(kLabel10, strlen(kLabel10), 265, 10, textPaint);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000163
164 canvas->translate(80, 20);
165
reed@google.com58b21ec2012-07-30 18:20:12 +0000166 // since we draw into this directly, we need to start fresh
167 sk_bzero(fBuffer, fBufferSize);
168
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000169 SkImageInfo info = SkImageInfo::MakeN32Premul(W, H);
reede8f30622016-03-23 18:59:25 -0700170 sk_sp<SkSurface> surf0(SkSurface::MakeRasterDirect(info, fBuffer, RB));
171 sk_sp<SkSurface> surf1(SkSurface::MakeRaster(info));
172 sk_sp<SkSurface> surf2; // gpu
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000173
reeda9cb8712015-01-16 14:21:40 -0800174#if SK_SUPPORT_GPU
reede8f30622016-03-23 18:59:25 -0700175 surf2 = SkSurface::MakeRenderTarget(canvas->getGrContext(), SkBudgeted::kNo, info);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000176#endif
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000177
reede8f30622016-03-23 18:59:25 -0700178 test_surface(canvas, surf0.get(), true);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000179 canvas->translate(80, 0);
reede8f30622016-03-23 18:59:25 -0700180 test_surface(canvas, surf1.get(), true);
reeda9cb8712015-01-16 14:21:40 -0800181 if (surf2) {
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000182 canvas->translate(80, 0);
reede8f30622016-03-23 18:59:25 -0700183 test_surface(canvas, surf2.get(), true);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000184 }
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000185 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000186
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000187private:
188 typedef skiagm::GM INHERITED;
189};
reeda9cb8712015-01-16 14:21:40 -0800190DEF_GM( return new ImageGM; )
reed09553032015-11-23 12:32:16 -0800191
192///////////////////////////////////////////////////////////////////////////////////////////////////
193
194#include "SkPictureRecorder.h"
195
196static void draw_pixmap(SkCanvas* canvas, const SkPixmap& pmap) {
197 SkBitmap bitmap;
halcanarye36ec872015-12-09 11:36:59 -0800198 bitmap.installPixels(pmap);
reed09553032015-11-23 12:32:16 -0800199 canvas->drawBitmap(bitmap, 0, 0, nullptr);
200}
201
202static void show_scaled_pixels(SkCanvas* canvas, SkImage* image) {
203 SkAutoCanvasRestore acr(canvas, true);
204
205 canvas->drawImage(image, 0, 0, nullptr);
206 canvas->translate(110, 10);
207
208 const SkImageInfo info = SkImageInfo::MakeN32Premul(40, 40);
209 SkAutoPixmapStorage storage;
210 storage.alloc(info);
211
212 const SkImage::CachingHint chints[] = {
reed6868c3f2015-11-24 11:44:47 -0800213 SkImage::kAllow_CachingHint, SkImage::kDisallow_CachingHint,
reed09553032015-11-23 12:32:16 -0800214 };
215 const SkFilterQuality qualities[] = {
216 kNone_SkFilterQuality, kLow_SkFilterQuality, kMedium_SkFilterQuality, kHigh_SkFilterQuality,
217 };
218
219 for (auto ch : chints) {
220 canvas->save();
221 for (auto q : qualities) {
222 if (image->scalePixels(storage, q, ch)) {
223 draw_pixmap(canvas, storage);
224 }
225 canvas->translate(70, 0);
226 }
227 canvas->restore();
228 canvas->translate(0, 45);
229 }
230}
231
232static void draw_contents(SkCanvas* canvas) {
233 SkPaint paint;
234 paint.setStyle(SkPaint::kStroke_Style);
235 paint.setStrokeWidth(20);
236 canvas->drawCircle(50, 50, 35, paint);
237}
238
reed9ce9d672016-03-17 10:51:11 -0700239static sk_sp<SkImage> make_raster(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) {
reede8f30622016-03-23 18:59:25 -0700240 auto surface(SkSurface::MakeRaster(info));
reed7850eb22015-12-02 14:19:47 -0800241 draw(surface->getCanvas());
reed9ce9d672016-03-17 10:51:11 -0700242 return surface->makeImageSnapshot();
reed09553032015-11-23 12:32:16 -0800243}
244
reed9ce9d672016-03-17 10:51:11 -0700245static sk_sp<SkImage> make_picture(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) {
reed09553032015-11-23 12:32:16 -0800246 SkPictureRecorder recorder;
reed7850eb22015-12-02 14:19:47 -0800247 draw(recorder.beginRecording(SkRect::MakeIWH(info.width(), info.height())));
reedca2622b2016-03-18 07:25:55 -0700248 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
249 info.dimensions(), nullptr, nullptr);
reed09553032015-11-23 12:32:16 -0800250}
251
reed9ce9d672016-03-17 10:51:11 -0700252static sk_sp<SkImage> make_codec(const SkImageInfo& info, GrContext*, void (*draw)(SkCanvas*)) {
253 sk_sp<SkImage> image(make_raster(info, nullptr, draw));
254 sk_sp<SkData> data(image->encode());
255 return SkImage::MakeFromEncoded(data);
reed09553032015-11-23 12:32:16 -0800256}
257
reed9ce9d672016-03-17 10:51:11 -0700258static sk_sp<SkImage> make_gpu(const SkImageInfo& info, GrContext* ctx, void (*draw)(SkCanvas*)) {
reed09553032015-11-23 12:32:16 -0800259 if (!ctx) { return nullptr; }
reede8f30622016-03-23 18:59:25 -0700260 auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info));
scroggoe6f0d6e2016-05-13 07:25:44 -0700261 if (!surface) { return nullptr; }
reed7850eb22015-12-02 14:19:47 -0800262 draw(surface->getCanvas());
reed9ce9d672016-03-17 10:51:11 -0700263 return surface->makeImageSnapshot();
reed09553032015-11-23 12:32:16 -0800264}
265
reed9ce9d672016-03-17 10:51:11 -0700266typedef sk_sp<SkImage> (*ImageMakerProc)(const SkImageInfo&, GrContext*, void (*)(SkCanvas*));
reed09553032015-11-23 12:32:16 -0800267
268class ScalePixelsGM : public skiagm::GM {
269public:
270 ScalePixelsGM() {}
271
272protected:
273 SkString onShortName() override {
274 return SkString("scale-pixels");
275 }
276
277 SkISize onISize() override {
278 return SkISize::Make(960, 1200);
279 }
280
281 void onDraw(SkCanvas* canvas) override {
282 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
283
284 const ImageMakerProc procs[] = {
reed6868c3f2015-11-24 11:44:47 -0800285 make_codec, make_raster, make_picture, make_codec, make_gpu,
reed09553032015-11-23 12:32:16 -0800286 };
287 for (auto& proc : procs) {
reed9ce9d672016-03-17 10:51:11 -0700288 sk_sp<SkImage> image(proc(info, canvas->getGrContext(), draw_contents));
reed09553032015-11-23 12:32:16 -0800289 if (image) {
reed9ce9d672016-03-17 10:51:11 -0700290 show_scaled_pixels(canvas, image.get());
reed09553032015-11-23 12:32:16 -0800291 }
292 canvas->translate(0, 120);
293 }
294 }
halcanary9d524f22016-03-29 09:03:52 -0700295
reed09553032015-11-23 12:32:16 -0800296private:
297 typedef skiagm::GM INHERITED;
298};
299DEF_GM( return new ScalePixelsGM; )
reed7850eb22015-12-02 14:19:47 -0800300
301///////////////////////////////////////////////////////////////////////////////////////////////////
302
303#include "SkImageGenerator.h"
304
305static SkImageInfo make_info(SkImage* img) {
brianosman69c166d2016-08-17 14:01:05 -0700306 return SkImageInfo::MakeN32(img->width(), img->height(), img->alphaType());
reed7850eb22015-12-02 14:19:47 -0800307}
308
309// Its simple, but I wonder if we should expose this formally?
310//
311class ImageGeneratorFromImage : public SkImageGenerator {
312public:
313 ImageGeneratorFromImage(SkImage* img) : INHERITED(make_info(img)), fImg(SkRef(img)) {}
314
315protected:
316 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, SkPMColor ctable[],
317 int* ctableCount) override {
318 return fImg->readPixels(info, pixels, rowBytes, 0, 0);
319 }
320
321private:
322 SkAutoTUnref<SkImage> fImg;
323
324 typedef SkImageGenerator INHERITED;
325};
326
327static void draw_opaque_contents(SkCanvas* canvas) {
328 canvas->drawColor(0xFFFF8844);
329
330 SkPaint paint;
331 paint.setStyle(SkPaint::kStroke_Style);
332 paint.setStrokeWidth(20);
333 canvas->drawCircle(50, 50, 35, paint);
334}
335
336static SkImageGenerator* gen_raster(const SkImageInfo& info) {
reede8f30622016-03-23 18:59:25 -0700337 auto surface(SkSurface::MakeRaster(info));
reed7850eb22015-12-02 14:19:47 -0800338 draw_opaque_contents(surface->getCanvas());
reed9ce9d672016-03-17 10:51:11 -0700339 return new ImageGeneratorFromImage(surface->makeImageSnapshot().get());
reed7850eb22015-12-02 14:19:47 -0800340}
341
342static SkImageGenerator* gen_picture(const SkImageInfo& info) {
343 SkPictureRecorder recorder;
344 draw_opaque_contents(recorder.beginRecording(SkRect::MakeIWH(info.width(), info.height())));
reedca2622b2016-03-18 07:25:55 -0700345 sk_sp<SkPicture> pict(recorder.finishRecordingAsPicture());
346 return SkImageGenerator::NewFromPicture(info.dimensions(), pict.get(), nullptr, nullptr);
reed7850eb22015-12-02 14:19:47 -0800347}
348
349static SkImageGenerator* gen_png(const SkImageInfo& info) {
reed9ce9d672016-03-17 10:51:11 -0700350 sk_sp<SkImage> image(make_raster(info, nullptr, draw_opaque_contents));
bungemanffae30d2016-08-03 13:32:32 -0700351 sk_sp<SkData> data(image->encode(SkImageEncoder::kPNG_Type, 100));
352 return SkImageGenerator::NewFromEncoded(data.get());
reed7850eb22015-12-02 14:19:47 -0800353}
354
355static SkImageGenerator* gen_jpg(const SkImageInfo& info) {
reed9ce9d672016-03-17 10:51:11 -0700356 sk_sp<SkImage> image(make_raster(info, nullptr, draw_opaque_contents));
bungemanffae30d2016-08-03 13:32:32 -0700357 sk_sp<SkData> data(image->encode(SkImageEncoder::kJPEG_Type, 100));
358 return SkImageGenerator::NewFromEncoded(data.get());
reed7850eb22015-12-02 14:19:47 -0800359}
360
361typedef SkImageGenerator* (*GeneratorMakerProc)(const SkImageInfo&);
362
363static void show_scaled_generator(SkCanvas* canvas, SkImageGenerator* gen) {
364 const SkImageInfo genInfo = gen->getInfo();
365
366 SkAutoCanvasRestore acr(canvas, true);
367
368 SkBitmap bm;
369 bm.allocPixels(genInfo);
370 if (gen->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())) {
371 canvas->drawBitmap(bm, 0, 0, nullptr);
372 }
373 canvas->translate(110, 0);
374
375 const float scales[] = { 0.75f, 0.5f, 0.25f };
376 for (auto scale : scales) {
377 SkImageGenerator::SupportedSizes sizes;
378 if (gen->computeScaledDimensions(scale, &sizes)) {
379 const SkImageInfo info = SkImageInfo::MakeN32Premul(sizes.fSizes[0].width(),
380 sizes.fSizes[0].height());
381 bm.allocPixels(info);
382 SkPixmap pmap;
383 bm.peekPixels(&pmap);
384 if (gen->generateScaledPixels(pmap)) {
385 canvas->drawBitmap(bm, 0, SkIntToScalar(genInfo.height() - info.height())/2);
386 }
387 }
388 canvas->translate(100, 0);
389 }
390}
391
392class ScaleGeneratorGM : public skiagm::GM {
393public:
394 ScaleGeneratorGM() {}
395
396protected:
397 SkString onShortName() override {
398 return SkString("scale-generator");
399 }
400
401 SkISize onISize() override {
402 return SkISize::Make(500, 500);
403 }
404
405 void onDraw(SkCanvas* canvas) override {
406 canvas->translate(10, 10);
407
408 // explicitly make it opaque, so we can test JPEG (which is only ever opaque)
409 const SkImageInfo info = SkImageInfo::MakeN32(100, 100, kOpaque_SkAlphaType);
410
411 const GeneratorMakerProc procs[] = {
412 gen_raster, gen_picture, gen_png, gen_jpg,
413 };
414 for (auto& proc : procs) {
415 SkAutoTDelete<SkImageGenerator> gen(proc(info));
416 if (gen) {
417 show_scaled_generator(canvas, gen);
418 }
419 canvas->translate(0, 120);
420 }
421 }
422
423private:
424 typedef skiagm::GM INHERITED;
425};
426DEF_GM( return new ScaleGeneratorGM; )
bsalomon8e74f802016-01-30 10:01:40 -0800427
428#if SK_SUPPORT_GPU
429#include "GrContextFactory.h"
430#endif
431
432DEF_SIMPLE_GM(new_texture_image, canvas, 225, 60) {
433 GrContext* context = nullptr;
434#if SK_SUPPORT_GPU
435 context = canvas->getGrContext();
bsalomon3724e572016-03-30 18:56:19 -0700436 sk_gpu_test::GrContextFactory factory;
bsalomon8e74f802016-01-30 10:01:40 -0800437#endif
438 if (!context) {
439 skiagm::GM::DrawGpuOnlyMessage(canvas);
440 return;
441 }
442
443 auto render_image = [](SkCanvas* canvas) {
444 canvas->clear(SK_ColorBLUE);
445 SkPaint paint;
446 paint.setColor(SK_ColorRED);
447 canvas->drawRect(SkRect::MakeXYWH(10.f,10.f,10.f,10.f), paint);
448 paint.setColor(SK_ColorGREEN);
449 canvas->drawRect(SkRect::MakeXYWH(30.f,10.f,10.f,10.f), paint);
450 paint.setColor(SK_ColorYELLOW);
451 canvas->drawRect(SkRect::MakeXYWH(10.f,30.f,10.f,10.f), paint);
452 paint.setColor(SK_ColorCYAN);
453 canvas->drawRect(SkRect::MakeXYWH(30.f,30.f,10.f,10.f), paint);
454 };
455
mtkleindbfd7ab2016-09-01 11:24:54 -0700456 static constexpr int kSize = 50;
bsalomon8e74f802016-01-30 10:01:40 -0800457 SkBitmap bmp;
458 bmp.allocN32Pixels(kSize, kSize);
459 SkCanvas bmpCanvas(bmp);
460 render_image(&bmpCanvas);
461
reed9ce9d672016-03-17 10:51:11 -0700462 std::function<sk_sp<SkImage>()> imageFactories[] = {
bsalomon8e74f802016-01-30 10:01:40 -0800463 // Create sw raster image.
464 [bmp] {
reed9ce9d672016-03-17 10:51:11 -0700465 return SkImage::MakeFromBitmap(bmp);
bsalomon8e74f802016-01-30 10:01:40 -0800466 },
467 // Create encoded image.
468 [bmp] {
reed9ce9d672016-03-17 10:51:11 -0700469 sk_sp<SkData> src(
bsalomon8e74f802016-01-30 10:01:40 -0800470 SkImageEncoder::EncodeData(bmp, SkImageEncoder::kPNG_Type, 100));
reed9ce9d672016-03-17 10:51:11 -0700471 return SkImage::MakeFromEncoded(std::move(src));
bsalomon8e74f802016-01-30 10:01:40 -0800472 },
473 // Create a picture image.
474 [render_image] {
475 SkPictureRecorder recorder;
476 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kSize), SkIntToScalar(kSize));
477 render_image(canvas);
reedca2622b2016-03-18 07:25:55 -0700478 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
479 SkISize::Make(kSize, kSize), nullptr, nullptr);
bsalomon8e74f802016-01-30 10:01:40 -0800480 },
481 // Create a texture image
reed9ce9d672016-03-17 10:51:11 -0700482 [context, render_image]() -> sk_sp<SkImage> {
reede8f30622016-03-23 18:59:25 -0700483 auto surface(
484 SkSurface::MakeRenderTarget(context, SkBudgeted::kYes,
485 SkImageInfo::MakeN32Premul(kSize, kSize)));
bsalomon8e74f802016-01-30 10:01:40 -0800486 if (!surface) {
487 return nullptr;
488 }
489 render_image(surface->getCanvas());
reed9ce9d672016-03-17 10:51:11 -0700490 return surface->makeImageSnapshot();
bsalomon8e74f802016-01-30 10:01:40 -0800491 }
492 };
493
mtkleindbfd7ab2016-09-01 11:24:54 -0700494 constexpr SkScalar kPad = 5.f;
bsalomon8e74f802016-01-30 10:01:40 -0800495 canvas->translate(kPad, kPad);
496 for (auto factory : imageFactories) {
reed9ce9d672016-03-17 10:51:11 -0700497 auto image(factory());
bsalomon8e74f802016-01-30 10:01:40 -0800498 if (!image) {
499 continue;
500 }
501 if (context) {
reed9ce9d672016-03-17 10:51:11 -0700502 sk_sp<SkImage> texImage(image->makeTextureImage(context));
bsalomon8e74f802016-01-30 10:01:40 -0800503 if (texImage) {
504 canvas->drawImage(texImage, 0, 0);
505 }
506 }
507 canvas->translate(image->width() + kPad, 0);
508 }
509}