blob: 726ab97a8f9aa5859b774cd4fd5a6584718e655c [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; )
rmistry@google.comae933ce2012-08-23 18:19:56 +0000195
reedf803da12015-01-23 05:58:07 -0800196class ImageResizeGM : public skiagm::GM {
197 enum {
198 W = 100,
199 H = 100,
200 };
201public:
202 ImageResizeGM() {}
203
204protected:
mtklein36352bf2015-03-25 18:17:31 -0700205 SkString onShortName() override { return SkString("image-resize"); }
reedf803da12015-01-23 05:58:07 -0800206
mtklein36352bf2015-03-25 18:17:31 -0700207 SkISize onISize() override { return SkISize::Make(510, 480); }
reedf803da12015-01-23 05:58:07 -0800208
209 void drawIntoImage(SkCanvas* canvas) {
210 SkPaint paint;
211 paint.setAntiAlias(true);
212 paint.setStyle(SkPaint::kStroke_Style);
213 paint.setStrokeWidth(3);
214 SkRandom rand;
215 for (int i = 0; i < 60; ++i) {
216 paint.setColor(rand.nextU());
217 SkScalar x = rand.nextUScalar1() * W;
218 SkScalar y = rand.nextUScalar1() * H;
219 SkScalar r = rand.nextUScalar1() * W / 2;
220 canvas->drawCircle(x, y, r, paint);
221 }
222 }
223
224 SkImage* makeImage(SkCanvas* canvas) {
225 const SkImageInfo info = SkImageInfo::MakeN32Premul(W, H);
226 SkAutoTUnref<SkSurface> surface(canvas->newSurface(info));
227 if (!surface) {
228 surface.reset(SkSurface::NewRaster(info));
229 }
230 this->drawIntoImage(surface->getCanvas());
231 return surface->newImageSnapshot();
232 }
233
234 void drawResized(SkCanvas* canvas, SkImage* image, int newW, int newH, const SkIRect* subset,
235 SkFilterQuality fq) {
236 // canvas method
237 SkPaint paint;
238 paint.setFilterQuality(fq);
239 SkRect dstR = SkRect::MakeWH(SkIntToScalar(newW), SkIntToScalar(newH));
240 SkRect srcR;
241 if (subset) {
242 srcR.set(*subset);
243 }
halcanary96fcdcc2015-08-27 07:41:13 -0700244 canvas->legacy_drawImageRect(image, subset ? &srcR : nullptr, dstR, &paint);
reedf803da12015-01-23 05:58:07 -0800245 canvas->translate(newW + 20.0f, 0);
246
247 // image method
248 SkAutoTUnref<SkImage> image2(image->newImage(newW, newH, subset, fq));
halcanary96fcdcc2015-08-27 07:41:13 -0700249 canvas->drawImage(image2, 0, 0, nullptr);
reedf803da12015-01-23 05:58:07 -0800250 canvas->translate(image2->width() + 20.0f, 0);
251 }
252
253 void drawImage(SkCanvas* canvas, SkImage* image, SkFilterQuality fq) {
254
halcanary96fcdcc2015-08-27 07:41:13 -0700255 canvas->drawImage(image, 0, 0, nullptr);
reedf803da12015-01-23 05:58:07 -0800256 canvas->translate(image->width() + 20.0f, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700257 this->drawResized(canvas, image, image->width()*4/10, image->height()*4/10, nullptr, fq);
reedf803da12015-01-23 05:58:07 -0800258
259 SkIRect subset = SkIRect::MakeLTRB(W/4, H/4, W/2, H/2);
260 this->drawResized(canvas, image, W, H, &subset, fq);
261 }
262
mtklein36352bf2015-03-25 18:17:31 -0700263 void onDraw(SkCanvas* canvas) override {
reedf803da12015-01-23 05:58:07 -0800264 canvas->translate(10, 10);
265
266 SkAutoTUnref<SkImage> image(this->makeImage(canvas));
267
268 const SkFilterQuality fq[] = {
269 kNone_SkFilterQuality,
270 kLow_SkFilterQuality,
271 kMedium_SkFilterQuality,
272 kHigh_SkFilterQuality,
273 };
274 for (size_t i = 0; i < SK_ARRAY_COUNT(fq); ++i) {
275 {
276 SkAutoCanvasRestore acr(canvas, true);
277 this->drawImage(canvas, image, fq[i]);
278 }
279 canvas->translate(0, image->height() + 20.0f);
280 }
281 }
282
283private:
284 typedef skiagm::GM INHERITED;
285};
286DEF_GM( return new ImageResizeGM; )
287