blob: 991b4014f02aa3bfff0a2a3126a29f5210a21591 [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
8#include "gm.h"
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +00009#include "SkData.h"
reedf803da12015-01-23 05:58:07 -080010#include "SkCanvas.h"
11#include "SkRandom.h"
12#include "SkStream.h"
13#include "SkSurface.h"
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000014
robertphillips@google.com97b6b072012-10-31 14:48:39 +000015#if SK_SUPPORT_GPU
16#include "GrContext.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000017#endif
reed@google.com97af1a62012-08-28 12:19:02 +000018
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000019static void drawJpeg(SkCanvas* canvas, const SkISize& size) {
scroggo@google.comccd7afb2013-05-28 16:45:07 +000020 // TODO: Make this draw a file that is checked in, so it can
21 // be exercised on machines other than mike's. Will require a
22 // rebaseline.
reed48925e32014-09-18 13:57:05 -070023 SkAutoDataUnref data(SkData::NewFromFileName("/Users/mike/Downloads/skia.google.jpeg"));
halcanary96fcdcc2015-08-27 07:41:13 -070024 if (nullptr == data.get()) {
reed48925e32014-09-18 13:57:05 -070025 return;
26 }
reed871872f2015-06-22 12:48:26 -070027 SkImage* image = SkImage::NewFromEncoded(data);
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000028 if (image) {
29 SkAutoCanvasRestore acr(canvas, true);
30 canvas->scale(size.width() * 1.0f / image->width(),
31 size.height() * 1.0f / image->height());
halcanary96fcdcc2015-08-27 07:41:13 -070032 canvas->drawImage(image, 0, 0, nullptr);
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000033 image->unref();
34 }
35}
mike@reedtribe.org70e35902012-07-29 20:38:16 +000036
37static void drawContents(SkSurface* surface, SkColor fillC) {
skia.committer@gmail.com04ba4482012-09-07 02:01:30 +000038 SkSize size = SkSize::Make(SkIntToScalar(surface->width()),
robertphillips@google.com94acc702012-09-06 18:43:21 +000039 SkIntToScalar(surface->height()));
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000040 SkCanvas* canvas = surface->getCanvas();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000041
42 SkScalar stroke = size.fWidth / 10;
43 SkScalar radius = (size.fWidth - stroke) / 2;
44
45 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
mike@reedtribe.org70e35902012-07-29 20:38:16 +000047 paint.setAntiAlias(true);
48 paint.setColor(fillC);
49 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000050
mike@reedtribe.org70e35902012-07-29 20:38:16 +000051 paint.setStyle(SkPaint::kStroke_Style);
52 paint.setStrokeWidth(stroke);
53 paint.setColor(SK_ColorBLACK);
54 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
55}
56
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000057static void test_surface(SkCanvas* canvas, SkSurface* surf, bool usePaint) {
mike@reedtribe.org70e35902012-07-29 20:38:16 +000058 drawContents(surf, SK_ColorRED);
junov@chromium.org5ee449a2013-04-12 20:20:50 +000059 SkImage* imgR = surf->newImageSnapshot();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000060
reed@google.com97af1a62012-08-28 12:19:02 +000061 if (true) {
junov@chromium.org5ee449a2013-04-12 20:20:50 +000062 SkImage* imgR2 = surf->newImageSnapshot();
reed@google.com97af1a62012-08-28 12:19:02 +000063 SkASSERT(imgR == imgR2);
64 imgR2->unref();
65 }
66
mike@reedtribe.org70e35902012-07-29 20:38:16 +000067 drawContents(surf, SK_ColorGREEN);
junov@chromium.org5ee449a2013-04-12 20:20:50 +000068 SkImage* imgG = surf->newImageSnapshot();
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
halcanary96fcdcc2015-08-27 07:41:13 -070079 canvas->drawImage(imgR, 0, 0, usePaint ? &paint : nullptr);
80 canvas->drawImage(imgG, 0, 80, usePaint ? &paint : nullptr);
81 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
halcanary96fcdcc2015-08-27 07:41:13 -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 imgG->unref();
101 imgR->unref();
102}
103
104class ImageGM : public skiagm::GM {
105 void* fBuffer;
reed@google.com58b21ec2012-07-30 18:20:12 +0000106 size_t fBufferSize;
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000107 SkSize fSize;
108 enum {
109 W = 64,
110 H = 64,
111 RB = W * 4 + 8,
112 };
113public:
114 ImageGM() {
reed@google.com58b21ec2012-07-30 18:20:12 +0000115 fBufferSize = RB * H;
116 fBuffer = sk_malloc_throw(fBufferSize);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000117 fSize.set(SkIntToScalar(W), SkIntToScalar(H));
118 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000119
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000120 virtual ~ImageGM() {
121 sk_free(fBuffer);
122 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000123
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000124protected:
mtklein36352bf2015-03-25 18:17:31 -0700125 SkString onShortName() override {
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000126 return SkString("image-surface");
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000127 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000128
mtklein36352bf2015-03-25 18:17:31 -0700129 SkISize onISize() override {
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000130 return SkISize::Make(960, 1200);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000131 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132
mtklein36352bf2015-03-25 18:17:31 -0700133 void onDraw(SkCanvas* canvas) override {
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +0000134 drawJpeg(canvas, this->getISize());
135
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +0000136 canvas->scale(2, 2);
137
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000138 static const char* kLabel1 = "Original Img";
139 static const char* kLabel2 = "Modified Img";
140 static const char* kLabel3 = "Cur Surface";
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000141 static const char* kLabel4 = "Full Crop";
142 static const char* kLabel5 = "Over-crop";
143 static const char* kLabel6 = "Upper-left";
144 static const char* kLabel7 = "No Crop";
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000145
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000146 static const char* kLabel8 = "Pre-Alloc Img";
147 static const char* kLabel9 = "New Alloc Img";
reeda9cb8712015-01-16 14:21:40 -0800148 static const char* kLabel10 = "GPU";
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000149
150 SkPaint textPaint;
commit-bot@chromium.orgcae54f12014-04-11 18:34:35 +0000151 textPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700152 sk_tool_utils::set_portable_typeface(&textPaint);
commit-bot@chromium.orgcae54f12014-04-11 18:34:35 +0000153 textPaint.setTextSize(8);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000154
155 canvas->drawText(kLabel1, strlen(kLabel1), 10, 60, textPaint);
156 canvas->drawText(kLabel2, strlen(kLabel2), 10, 140, textPaint);
157 canvas->drawText(kLabel3, strlen(kLabel3), 10, 220, textPaint);
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000158 canvas->drawText(kLabel4, strlen(kLabel4), 10, 300, textPaint);
159 canvas->drawText(kLabel5, strlen(kLabel5), 10, 380, textPaint);
160 canvas->drawText(kLabel6, strlen(kLabel6), 10, 460, textPaint);
161 canvas->drawText(kLabel7, strlen(kLabel7), 10, 540, textPaint);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000162
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000163 canvas->drawText(kLabel8, strlen(kLabel8), 80, 10, textPaint);
164 canvas->drawText(kLabel9, strlen(kLabel9), 160, 10, textPaint);
reeda9cb8712015-01-16 14:21:40 -0800165 canvas->drawText(kLabel10, strlen(kLabel10), 265, 10, textPaint);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000166
167 canvas->translate(80, 20);
168
reed@google.com58b21ec2012-07-30 18:20:12 +0000169 // since we draw into this directly, we need to start fresh
170 sk_bzero(fBuffer, fBufferSize);
171
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000172 SkImageInfo info = SkImageInfo::MakeN32Premul(W, H);
mike@reedtribe.orgb9476252012-11-15 02:37:45 +0000173 SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, fBuffer, RB));
174 SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info));
reeda9cb8712015-01-16 14:21:40 -0800175 SkAutoTUnref<SkSurface> surf2; // gpu
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000176
reeda9cb8712015-01-16 14:21:40 -0800177#if SK_SUPPORT_GPU
178 surf2.reset(SkSurface::NewRenderTarget(canvas->getGrContext(),
179 SkSurface::kNo_Budgeted, info));
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000180#endif
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000181
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000182 test_surface(canvas, surf0, true);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000183 canvas->translate(80, 0);
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000184 test_surface(canvas, surf1, true);
reeda9cb8712015-01-16 14:21:40 -0800185 if (surf2) {
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000186 canvas->translate(80, 0);
reeda9cb8712015-01-16 14:21:40 -0800187 test_surface(canvas, surf2, true);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000188 }
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000189 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000190
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000191private:
192 typedef skiagm::GM INHERITED;
193};
reeda9cb8712015-01-16 14:21:40 -0800194DEF_GM( return new ImageGM; )
reed09553032015-11-23 12:32:16 -0800195
196///////////////////////////////////////////////////////////////////////////////////////////////////
197
198#include "SkPictureRecorder.h"
199
200static void draw_pixmap(SkCanvas* canvas, const SkPixmap& pmap) {
201 SkBitmap bitmap;
202 bitmap.installPixels(pmap.info(), (void*)pmap.addr(), pmap.rowBytes());
203 canvas->drawBitmap(bitmap, 0, 0, nullptr);
204}
205
206static void show_scaled_pixels(SkCanvas* canvas, SkImage* image) {
207 SkAutoCanvasRestore acr(canvas, true);
208
209 canvas->drawImage(image, 0, 0, nullptr);
210 canvas->translate(110, 10);
211
212 const SkImageInfo info = SkImageInfo::MakeN32Premul(40, 40);
213 SkAutoPixmapStorage storage;
214 storage.alloc(info);
215
216 const SkImage::CachingHint chints[] = {
reed6868c3f2015-11-24 11:44:47 -0800217 SkImage::kAllow_CachingHint, SkImage::kDisallow_CachingHint,
reed09553032015-11-23 12:32:16 -0800218 };
219 const SkFilterQuality qualities[] = {
220 kNone_SkFilterQuality, kLow_SkFilterQuality, kMedium_SkFilterQuality, kHigh_SkFilterQuality,
221 };
222
223 for (auto ch : chints) {
224 canvas->save();
225 for (auto q : qualities) {
226 if (image->scalePixels(storage, q, ch)) {
227 draw_pixmap(canvas, storage);
228 }
229 canvas->translate(70, 0);
230 }
231 canvas->restore();
232 canvas->translate(0, 45);
233 }
234}
235
236static void draw_contents(SkCanvas* canvas) {
237 SkPaint paint;
238 paint.setStyle(SkPaint::kStroke_Style);
239 paint.setStrokeWidth(20);
240 canvas->drawCircle(50, 50, 35, paint);
241}
242
243static SkImage* make_raster(const SkImageInfo& info, GrContext*) {
244 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info));
245 draw_contents(surface->getCanvas());
246 return surface->newImageSnapshot();
247}
248
249static SkImage* make_picture(const SkImageInfo& info, GrContext*) {
250 SkPictureRecorder recorder;
251 draw_contents(recorder.beginRecording(SkRect::MakeIWH(info.width(), info.height())));
252 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
253 return SkImage::NewFromPicture(pict, info.dimensions(), nullptr, nullptr);
254}
255
256static SkImage* make_codec(const SkImageInfo& info, GrContext*) {
257 SkAutoTUnref<SkImage> image(make_raster(info, nullptr));
258 SkAutoTUnref<SkData> data(image->encode());
259 return SkImage::NewFromEncoded(data);
260}
261
262static SkImage* make_gpu(const SkImageInfo& info, GrContext* ctx) {
263 if (!ctx) { return nullptr; }
264 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info));
265 draw_contents(surface->getCanvas());
266 return surface->newImageSnapshot();
267}
268
269typedef SkImage* (*ImageMakerProc)(const SkImageInfo&, GrContext*);
270
271class ScalePixelsGM : public skiagm::GM {
272public:
273 ScalePixelsGM() {}
274
275protected:
276 SkString onShortName() override {
277 return SkString("scale-pixels");
278 }
279
280 SkISize onISize() override {
281 return SkISize::Make(960, 1200);
282 }
283
284 void onDraw(SkCanvas* canvas) override {
285 const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
286
287 const ImageMakerProc procs[] = {
reed6868c3f2015-11-24 11:44:47 -0800288 make_codec, make_raster, make_picture, make_codec, make_gpu,
reed09553032015-11-23 12:32:16 -0800289 };
290 for (auto& proc : procs) {
291 SkAutoTUnref<SkImage> image(proc(info, canvas->getGrContext()));
292 if (image) {
293 show_scaled_pixels(canvas, image);
294 }
295 canvas->translate(0, 120);
296 }
297 }
298
299private:
300 typedef skiagm::GM INHERITED;
301};
302DEF_GM( return new ScalePixelsGM; )