blob: f5f5228c61073ec1c17e824d3ba9eef948e94c3e [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 SkData* fileToData(const char path[]) {
19 SkFILEStream stream(path);
20 if (!stream.isValid()) {
21 return SkData::NewEmpty();
22 }
23 size_t size = stream.getLength();
24 void* mem = sk_malloc_throw(size);
25 stream.read(mem, size);
26 return SkData::NewFromMalloc(mem, size);
27}
28
29static void drawJpeg(SkCanvas* canvas, const SkISize& size) {
scroggo@google.comccd7afb2013-05-28 16:45:07 +000030 // TODO: Make this draw a file that is checked in, so it can
31 // be exercised on machines other than mike's. Will require a
32 // rebaseline.
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000033 SkAutoDataUnref data(fileToData("/Users/mike/Downloads/skia.google.jpeg"));
34 SkImage* image = SkImage::NewEncodedData(data);
35 if (image) {
36 SkAutoCanvasRestore acr(canvas, true);
37 canvas->scale(size.width() * 1.0f / image->width(),
38 size.height() * 1.0f / image->height());
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000039 image->draw(canvas, 0, 0, NULL);
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000040 image->unref();
41 }
42}
mike@reedtribe.org70e35902012-07-29 20:38:16 +000043
44static void drawContents(SkSurface* surface, SkColor fillC) {
skia.committer@gmail.com04ba4482012-09-07 02:01:30 +000045 SkSize size = SkSize::Make(SkIntToScalar(surface->width()),
robertphillips@google.com94acc702012-09-06 18:43:21 +000046 SkIntToScalar(surface->height()));
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000047 SkCanvas* canvas = surface->getCanvas();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000048
49 SkScalar stroke = size.fWidth / 10;
50 SkScalar radius = (size.fWidth - stroke) / 2;
51
52 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000053
mike@reedtribe.org70e35902012-07-29 20:38:16 +000054 paint.setAntiAlias(true);
55 paint.setColor(fillC);
56 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000057
mike@reedtribe.org70e35902012-07-29 20:38:16 +000058 paint.setStyle(SkPaint::kStroke_Style);
59 paint.setStrokeWidth(stroke);
60 paint.setColor(SK_ColorBLACK);
61 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
62}
63
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000064static void test_surface(SkCanvas* canvas, SkSurface* surf, bool usePaint) {
mike@reedtribe.org70e35902012-07-29 20:38:16 +000065 drawContents(surf, SK_ColorRED);
junov@chromium.org5ee449a2013-04-12 20:20:50 +000066 SkImage* imgR = surf->newImageSnapshot();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000067
reed@google.com97af1a62012-08-28 12:19:02 +000068 if (true) {
junov@chromium.org5ee449a2013-04-12 20:20:50 +000069 SkImage* imgR2 = surf->newImageSnapshot();
reed@google.com97af1a62012-08-28 12:19:02 +000070 SkASSERT(imgR == imgR2);
71 imgR2->unref();
72 }
73
mike@reedtribe.org70e35902012-07-29 20:38:16 +000074 drawContents(surf, SK_ColorGREEN);
junov@chromium.org5ee449a2013-04-12 20:20:50 +000075 SkImage* imgG = surf->newImageSnapshot();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000076
reed@google.com97af1a62012-08-28 12:19:02 +000077 // since we've drawn after we snapped imgR, imgG will be a different obj
78 SkASSERT(imgR != imgG);
79
mike@reedtribe.org70e35902012-07-29 20:38:16 +000080 drawContents(surf, SK_ColorBLUE);
81
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000082 SkPaint paint;
83// paint.setFilterBitmap(true);
84// paint.setAlpha(0x80);
85
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000086 imgR->draw(canvas, 0, 0, usePaint ? &paint : NULL);
87 imgG->draw(canvas, 0, 80, usePaint ? &paint : NULL);
88 surf->draw(canvas, 0, 160, usePaint ? &paint : NULL);
89
90 SkRect src1, src2, src3;
91 src1.iset(0, 0, surf->width(), surf->height());
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +000092 src2.iset(-surf->width() / 2, -surf->height() / 2,
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000093 surf->width(), surf->height());
94 src3.iset(0, 0, surf->width() / 2, surf->height() / 2);
95
96 SkRect dst1, dst2, dst3, dst4;
97 dst1.set(0, 240, 65, 305);
98 dst2.set(0, 320, 65, 385);
99 dst3.set(0, 400, 65, 465);
100 dst4.set(0, 480, 65, 545);
101
102 imgR->draw(canvas, &src1, dst1, usePaint ? &paint : NULL);
103 imgG->draw(canvas, &src2, dst2, usePaint ? &paint : NULL);
104 imgR->draw(canvas, &src3, dst3, usePaint ? &paint : NULL);
105 imgG->draw(canvas, NULL, dst4, usePaint ? &paint : NULL);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000106
107 imgG->unref();
108 imgR->unref();
109}
110
111class ImageGM : public skiagm::GM {
112 void* fBuffer;
reed@google.com58b21ec2012-07-30 18:20:12 +0000113 size_t fBufferSize;
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000114 SkSize fSize;
115 enum {
116 W = 64,
117 H = 64,
118 RB = W * 4 + 8,
119 };
120public:
121 ImageGM() {
reed@google.com58b21ec2012-07-30 18:20:12 +0000122 fBufferSize = RB * H;
123 fBuffer = sk_malloc_throw(fBufferSize);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000124 fSize.set(SkIntToScalar(W), SkIntToScalar(H));
125 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000126
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000127 virtual ~ImageGM() {
128 sk_free(fBuffer);
129 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000130
131
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000132protected:
133 virtual SkString onShortName() {
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000134 return SkString("image-surface");
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000135 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000136
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000137 virtual SkISize onISize() {
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000138 return SkISize::Make(960, 1200);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000139 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000140
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000141 virtual void onDraw(SkCanvas* canvas) {
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +0000142 drawJpeg(canvas, this->getISize());
143
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +0000144 canvas->scale(2, 2);
145
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000146 static const char* kLabel1 = "Original Img";
147 static const char* kLabel2 = "Modified Img";
148 static const char* kLabel3 = "Cur Surface";
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000149 static const char* kLabel4 = "Full Crop";
150 static const char* kLabel5 = "Over-crop";
151 static const char* kLabel6 = "Upper-left";
152 static const char* kLabel7 = "No Crop";
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000153
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000154 static const char* kLabel8 = "Pre-Alloc Img";
155 static const char* kLabel9 = "New Alloc Img";
156 static const char* kLabel10 = "SkPicture";
157 static const char* kLabel11 = "Null Paint";
158 static const char* kLabel12 = "GPU";
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000159
160 SkPaint textPaint;
161
162 canvas->drawText(kLabel1, strlen(kLabel1), 10, 60, textPaint);
163 canvas->drawText(kLabel2, strlen(kLabel2), 10, 140, textPaint);
164 canvas->drawText(kLabel3, strlen(kLabel3), 10, 220, textPaint);
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000165 canvas->drawText(kLabel4, strlen(kLabel4), 10, 300, textPaint);
166 canvas->drawText(kLabel5, strlen(kLabel5), 10, 380, textPaint);
167 canvas->drawText(kLabel6, strlen(kLabel6), 10, 460, textPaint);
168 canvas->drawText(kLabel7, strlen(kLabel7), 10, 540, textPaint);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000169
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000170 canvas->drawText(kLabel8, strlen(kLabel8), 80, 10, textPaint);
171 canvas->drawText(kLabel9, strlen(kLabel9), 160, 10, textPaint);
172 canvas->drawText(kLabel10, strlen(kLabel10), 250, 10, textPaint);
173 canvas->drawText(kLabel11, strlen(kLabel11), 320, 10, textPaint);
174 canvas->drawText(kLabel12, strlen(kLabel12), 410, 10, textPaint);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000175
176 canvas->translate(80, 20);
177
reed@google.com58b21ec2012-07-30 18:20:12 +0000178 // since we draw into this directly, we need to start fresh
179 sk_bzero(fBuffer, fBufferSize);
180
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000181 SkImageInfo info = SkImageInfo::MakeN32Premul(W, H);
mike@reedtribe.orgb9476252012-11-15 02:37:45 +0000182 SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, fBuffer, RB));
183 SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info));
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000184 SkAutoTUnref<SkSurface> surf2(SkSurface::NewPicture(info.fWidth, info.fHeight));
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000185 SkAutoTUnref<SkSurface> surf3(SkSurface::NewPicture(info.fWidth, info.fHeight));
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000186#if SK_SUPPORT_GPU
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000187 GrContext* ctx = canvas->getGrContext();
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000188
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000189 SkAutoTUnref<SkSurface> surf4(SkSurface::NewRenderTarget(ctx, info, 0));
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000190#endif
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000191
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000192 test_surface(canvas, surf0, true);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000193 canvas->translate(80, 0);
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000194 test_surface(canvas, surf1, true);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000195 canvas->translate(80, 0);
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000196 test_surface(canvas, surf2, true);
197 canvas->translate(80, 0);
198 test_surface(canvas, surf3, false);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000199#if SK_SUPPORT_GPU
200 if (NULL != ctx) {
201 canvas->translate(80, 0);
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000202 test_surface(canvas, surf4, true);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000203 }
204#endif
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000205 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000206
reed@google.com97af1a62012-08-28 12:19:02 +0000207 virtual uint32_t onGetFlags() const SK_OVERRIDE {
reed@google.com9c728272012-08-28 15:54:54 +0000208 return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag;
reed@google.com97af1a62012-08-28 12:19:02 +0000209 }
210
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000211private:
212 typedef skiagm::GM INHERITED;
213};
rmistry@google.comae933ce2012-08-23 18:19:56 +0000214
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000215//////////////////////////////////////////////////////////////////////////////
216
217static skiagm::GM* MyFactory(void*) { return new ImageGM; }
218static skiagm::GMRegistry reg(MyFactory);