blob: d10152987fb3b7b8239875d983f2b28aed7a17fb [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"
9#include "SkSurface.h"
10#include "SkCanvas.h"
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000011#include "SkStream.h"
12#include "SkData.h"
13
robertphillips@google.com97b6b072012-10-31 14:48:39 +000014#if SK_SUPPORT_GPU
15#include "GrContext.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000016#endif
reed@google.com97af1a62012-08-28 12:19:02 +000017
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000018static void drawJpeg(SkCanvas* canvas, const SkISize& size) {
scroggo@google.comccd7afb2013-05-28 16:45:07 +000019 // TODO: Make this draw a file that is checked in, so it can
20 // be exercised on machines other than mike's. Will require a
21 // rebaseline.
reed48925e32014-09-18 13:57:05 -070022 SkAutoDataUnref data(SkData::NewFromFileName("/Users/mike/Downloads/skia.google.jpeg"));
23 if (NULL == data.get()) {
24 return;
25 }
reed5965c8a2015-01-07 18:04:45 -080026 SkImage* image = SkImage::NewFromData(data);
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000027 if (image) {
28 SkAutoCanvasRestore acr(canvas, true);
29 canvas->scale(size.width() * 1.0f / image->width(),
30 size.height() * 1.0f / image->height());
piotaixrb5fae932014-09-24 13:03:30 -070031 canvas->drawImage(image, 0, 0, NULL);
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000032 image->unref();
33 }
34}
mike@reedtribe.org70e35902012-07-29 20:38:16 +000035
36static void drawContents(SkSurface* surface, SkColor fillC) {
skia.committer@gmail.com04ba4482012-09-07 02:01:30 +000037 SkSize size = SkSize::Make(SkIntToScalar(surface->width()),
robertphillips@google.com94acc702012-09-06 18:43:21 +000038 SkIntToScalar(surface->height()));
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000039 SkCanvas* canvas = surface->getCanvas();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000040
41 SkScalar stroke = size.fWidth / 10;
42 SkScalar radius = (size.fWidth - stroke) / 2;
43
44 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000045
mike@reedtribe.org70e35902012-07-29 20:38:16 +000046 paint.setAntiAlias(true);
47 paint.setColor(fillC);
48 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000049
mike@reedtribe.org70e35902012-07-29 20:38:16 +000050 paint.setStyle(SkPaint::kStroke_Style);
51 paint.setStrokeWidth(stroke);
52 paint.setColor(SK_ColorBLACK);
53 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
54}
55
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000056static void test_surface(SkCanvas* canvas, SkSurface* surf, bool usePaint) {
mike@reedtribe.org70e35902012-07-29 20:38:16 +000057 drawContents(surf, SK_ColorRED);
junov@chromium.org5ee449a2013-04-12 20:20:50 +000058 SkImage* imgR = surf->newImageSnapshot();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000059
reed@google.com97af1a62012-08-28 12:19:02 +000060 if (true) {
junov@chromium.org5ee449a2013-04-12 20:20:50 +000061 SkImage* imgR2 = surf->newImageSnapshot();
reed@google.com97af1a62012-08-28 12:19:02 +000062 SkASSERT(imgR == imgR2);
63 imgR2->unref();
64 }
65
mike@reedtribe.org70e35902012-07-29 20:38:16 +000066 drawContents(surf, SK_ColorGREEN);
junov@chromium.org5ee449a2013-04-12 20:20:50 +000067 SkImage* imgG = surf->newImageSnapshot();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000068
reed@google.com97af1a62012-08-28 12:19:02 +000069 // since we've drawn after we snapped imgR, imgG will be a different obj
70 SkASSERT(imgR != imgG);
71
mike@reedtribe.org70e35902012-07-29 20:38:16 +000072 drawContents(surf, SK_ColorBLUE);
73
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000074 SkPaint paint;
75// paint.setFilterBitmap(true);
76// paint.setAlpha(0x80);
77
piotaixrb5fae932014-09-24 13:03:30 -070078 canvas->drawImage(imgR, 0, 0, usePaint ? &paint : NULL);
79 canvas->drawImage(imgG, 0, 80, usePaint ? &paint : NULL);
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000080 surf->draw(canvas, 0, 160, usePaint ? &paint : NULL);
81
82 SkRect src1, src2, src3;
83 src1.iset(0, 0, surf->width(), surf->height());
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +000084 src2.iset(-surf->width() / 2, -surf->height() / 2,
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000085 surf->width(), surf->height());
86 src3.iset(0, 0, surf->width() / 2, surf->height() / 2);
87
88 SkRect dst1, dst2, dst3, dst4;
89 dst1.set(0, 240, 65, 305);
90 dst2.set(0, 320, 65, 385);
91 dst3.set(0, 400, 65, 465);
92 dst4.set(0, 480, 65, 545);
93
piotaixrb5fae932014-09-24 13:03:30 -070094 canvas->drawImageRect(imgR, &src1, dst1, usePaint ? &paint : NULL);
95 canvas->drawImageRect(imgG, &src2, dst2, usePaint ? &paint : NULL);
96 canvas->drawImageRect(imgR, &src3, dst3, usePaint ? &paint : NULL);
97 canvas->drawImageRect(imgG, NULL, dst4, usePaint ? &paint : NULL);
mike@reedtribe.org70e35902012-07-29 20:38:16 +000098
99 imgG->unref();
100 imgR->unref();
101}
102
103class ImageGM : public skiagm::GM {
104 void* fBuffer;
reed@google.com58b21ec2012-07-30 18:20:12 +0000105 size_t fBufferSize;
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000106 SkSize fSize;
107 enum {
108 W = 64,
109 H = 64,
110 RB = W * 4 + 8,
111 };
112public:
113 ImageGM() {
reed@google.com58b21ec2012-07-30 18:20:12 +0000114 fBufferSize = RB * H;
115 fBuffer = sk_malloc_throw(fBufferSize);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000116 fSize.set(SkIntToScalar(W), SkIntToScalar(H));
117 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000118
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000119 virtual ~ImageGM() {
120 sk_free(fBuffer);
121 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
123
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000124protected:
125 virtual SkString onShortName() {
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
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000129 virtual SkISize onISize() {
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
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000133 virtual void onDraw(SkCanvas* canvas) {
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";
commit-bot@chromium.orgcae54f12014-04-11 18:34:35 +0000148 static const char* kLabel10 = "Null Paint";
149 static const char* kLabel11 = "GPU";
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000150
151 SkPaint textPaint;
commit-bot@chromium.orgcae54f12014-04-11 18:34:35 +0000152 textPaint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400153 sk_tool_utils::set_portable_typeface(&textPaint);
commit-bot@chromium.orgcae54f12014-04-11 18:34:35 +0000154 textPaint.setTextSize(8);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000155
156 canvas->drawText(kLabel1, strlen(kLabel1), 10, 60, textPaint);
157 canvas->drawText(kLabel2, strlen(kLabel2), 10, 140, textPaint);
158 canvas->drawText(kLabel3, strlen(kLabel3), 10, 220, textPaint);
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000159 canvas->drawText(kLabel4, strlen(kLabel4), 10, 300, textPaint);
160 canvas->drawText(kLabel5, strlen(kLabel5), 10, 380, textPaint);
161 canvas->drawText(kLabel6, strlen(kLabel6), 10, 460, textPaint);
162 canvas->drawText(kLabel7, strlen(kLabel7), 10, 540, textPaint);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000163
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000164 canvas->drawText(kLabel8, strlen(kLabel8), 80, 10, textPaint);
165 canvas->drawText(kLabel9, strlen(kLabel9), 160, 10, textPaint);
166 canvas->drawText(kLabel10, strlen(kLabel10), 250, 10, textPaint);
167 canvas->drawText(kLabel11, strlen(kLabel11), 320, 10, textPaint);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000168
169 canvas->translate(80, 20);
170
reed@google.com58b21ec2012-07-30 18:20:12 +0000171 // since we draw into this directly, we need to start fresh
172 sk_bzero(fBuffer, fBufferSize);
173
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000174 SkImageInfo info = SkImageInfo::MakeN32Premul(W, H);
mike@reedtribe.orgb9476252012-11-15 02:37:45 +0000175 SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, fBuffer, RB));
176 SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info));
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000177#if SK_SUPPORT_GPU
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000178 GrContext* ctx = canvas->getGrContext();
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000179
reed4a8126e2014-09-22 07:29:03 -0700180 SkAutoTUnref<SkSurface> surf4(SkSurface::NewRenderTarget(ctx, info));
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000181#endif
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000182
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000183 test_surface(canvas, surf0, true);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000184 canvas->translate(80, 0);
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000185 test_surface(canvas, surf1, true);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000186#if SK_SUPPORT_GPU
bsalomon49f085d2014-09-05 13:34:00 -0700187 if (ctx) {
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000188 canvas->translate(80, 0);
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000189 test_surface(canvas, surf4, true);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000190 }
191#endif
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000192 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000193
mtklein72c9faa2015-01-09 10:06:39 -0800194 uint32_t onGetFlags() const SK_OVERRIDE {
reed@google.com9c728272012-08-28 15:54:54 +0000195 return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag;
reed@google.com97af1a62012-08-28 12:19:02 +0000196 }
197
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000198private:
199 typedef skiagm::GM INHERITED;
200};
rmistry@google.comae933ce2012-08-23 18:19:56 +0000201
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000202//////////////////////////////////////////////////////////////////////////////
203
204static skiagm::GM* MyFactory(void*) { return new ImageGM; }
205static skiagm::GMRegistry reg(MyFactory);