blob: 71fe0e176f094bdab2f047f93f79f2eb640cb6c0 [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"
16
17namespace skiagm {
reed@google.com97af1a62012-08-28 12:19:02 +000018extern GrContext* GetGr();
robertphillips@google.com97b6b072012-10-31 14:48:39 +000019};
20#endif
reed@google.com97af1a62012-08-28 12:19:02 +000021
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000022static SkData* fileToData(const char path[]) {
23 SkFILEStream stream(path);
24 if (!stream.isValid()) {
25 return SkData::NewEmpty();
26 }
27 size_t size = stream.getLength();
28 void* mem = sk_malloc_throw(size);
29 stream.read(mem, size);
30 return SkData::NewFromMalloc(mem, size);
31}
32
33static void drawJpeg(SkCanvas* canvas, const SkISize& size) {
scroggo@google.comccd7afb2013-05-28 16:45:07 +000034 // TODO: Make this draw a file that is checked in, so it can
35 // be exercised on machines other than mike's. Will require a
36 // rebaseline.
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000037 SkAutoDataUnref data(fileToData("/Users/mike/Downloads/skia.google.jpeg"));
38 SkImage* image = SkImage::NewEncodedData(data);
39 if (image) {
40 SkAutoCanvasRestore acr(canvas, true);
41 canvas->scale(size.width() * 1.0f / image->width(),
42 size.height() * 1.0f / image->height());
43 image->draw(canvas,0, 0, NULL);
44 image->unref();
45 }
46}
mike@reedtribe.org70e35902012-07-29 20:38:16 +000047
48static void drawContents(SkSurface* surface, SkColor fillC) {
skia.committer@gmail.com04ba4482012-09-07 02:01:30 +000049 SkSize size = SkSize::Make(SkIntToScalar(surface->width()),
robertphillips@google.com94acc702012-09-06 18:43:21 +000050 SkIntToScalar(surface->height()));
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000051 SkCanvas* canvas = surface->getCanvas();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000052
53 SkScalar stroke = size.fWidth / 10;
54 SkScalar radius = (size.fWidth - stroke) / 2;
55
56 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000057
mike@reedtribe.org70e35902012-07-29 20:38:16 +000058 paint.setAntiAlias(true);
59 paint.setColor(fillC);
60 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000061
mike@reedtribe.org70e35902012-07-29 20:38:16 +000062 paint.setStyle(SkPaint::kStroke_Style);
63 paint.setStrokeWidth(stroke);
64 paint.setColor(SK_ColorBLACK);
65 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
66}
67
68static void test_surface(SkCanvas* canvas, SkSurface* surf) {
69 drawContents(surf, SK_ColorRED);
junov@chromium.org5ee449a2013-04-12 20:20:50 +000070 SkImage* imgR = surf->newImageSnapshot();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000071
reed@google.com97af1a62012-08-28 12:19:02 +000072 if (true) {
junov@chromium.org5ee449a2013-04-12 20:20:50 +000073 SkImage* imgR2 = surf->newImageSnapshot();
reed@google.com97af1a62012-08-28 12:19:02 +000074 SkASSERT(imgR == imgR2);
75 imgR2->unref();
76 }
77
mike@reedtribe.org70e35902012-07-29 20:38:16 +000078 drawContents(surf, SK_ColorGREEN);
junov@chromium.org5ee449a2013-04-12 20:20:50 +000079 SkImage* imgG = surf->newImageSnapshot();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000080
reed@google.com97af1a62012-08-28 12:19:02 +000081 // since we've drawn after we snapped imgR, imgG will be a different obj
82 SkASSERT(imgR != imgG);
83
mike@reedtribe.org70e35902012-07-29 20:38:16 +000084 drawContents(surf, SK_ColorBLUE);
85
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000086 SkPaint paint;
87// paint.setFilterBitmap(true);
88// paint.setAlpha(0x80);
89
90 imgR->draw(canvas, 0, 0, &paint);
91 imgG->draw(canvas, 0, 80, &paint);
92 surf->draw(canvas, 0, 160, &paint);
mike@reedtribe.org70e35902012-07-29 20:38:16 +000093
94 imgG->unref();
95 imgR->unref();
96}
97
98class ImageGM : public skiagm::GM {
99 void* fBuffer;
reed@google.com58b21ec2012-07-30 18:20:12 +0000100 size_t fBufferSize;
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000101 SkSize fSize;
102 enum {
103 W = 64,
104 H = 64,
105 RB = W * 4 + 8,
106 };
107public:
108 ImageGM() {
reed@google.com58b21ec2012-07-30 18:20:12 +0000109 fBufferSize = RB * H;
110 fBuffer = sk_malloc_throw(fBufferSize);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000111 fSize.set(SkIntToScalar(W), SkIntToScalar(H));
112 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000113
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000114 virtual ~ImageGM() {
115 sk_free(fBuffer);
116 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000117
118
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000119protected:
120 virtual SkString onShortName() {
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000121 return SkString("image-surface");
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000122 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000123
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000124 virtual SkISize onISize() {
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000125 return SkISize::Make(800, 500);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000126 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000127
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000128 virtual void onDraw(SkCanvas* canvas) {
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +0000129 drawJpeg(canvas, this->getISize());
130
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +0000131 canvas->scale(2, 2);
132
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000133 static const char* kLabel1 = "Original Img";
134 static const char* kLabel2 = "Modified Img";
135 static const char* kLabel3 = "Cur Surface";
136
137 static const char* kLabel4 = "Pre-Alloc Img";
138 static const char* kLabel5 = "New Alloc Img";
139 static const char* kLabel6 = "SkPicture";
140 static const char* kLabel7 = "GPU";
141
142 SkPaint textPaint;
143
144 canvas->drawText(kLabel1, strlen(kLabel1), 10, 60, textPaint);
145 canvas->drawText(kLabel2, strlen(kLabel2), 10, 140, textPaint);
146 canvas->drawText(kLabel3, strlen(kLabel3), 10, 220, textPaint);
147
148 canvas->drawText(kLabel4, strlen(kLabel4), 80, 10, textPaint);
149 canvas->drawText(kLabel5, strlen(kLabel5), 160, 10, textPaint);
150 canvas->drawText(kLabel6, strlen(kLabel6), 250, 10, textPaint);
151 canvas->drawText(kLabel7, strlen(kLabel7), 340, 10, textPaint);
152
153 canvas->translate(80, 20);
154
reed@google.com58b21ec2012-07-30 18:20:12 +0000155 // since we draw into this directly, we need to start fresh
156 sk_bzero(fBuffer, fBufferSize);
157
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000158 SkImage::Info info;
159
160 info.fWidth = W;
161 info.fHeight = H;
162 info.fColorType = SkImage::kPMColor_ColorType;
163 info.fAlphaType = SkImage::kPremul_AlphaType;
mike@reedtribe.orgb9476252012-11-15 02:37:45 +0000164 SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, fBuffer, RB));
165 SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info));
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000166 SkAutoTUnref<SkSurface> surf2(SkSurface::NewPicture(info.fWidth, info.fHeight));
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000167#if SK_SUPPORT_GPU
168 GrContext* ctx = skiagm::GetGr();
169
mike@reedtribe.orgb9476252012-11-15 02:37:45 +0000170 SkAutoTUnref<SkSurface> surf3(SkSurface::NewRenderTarget(ctx, info, 0));
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000171#endif
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000172
173 test_surface(canvas, surf0);
174 canvas->translate(80, 0);
175 test_surface(canvas, surf1);
176 canvas->translate(80, 0);
177 test_surface(canvas, surf2);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000178#if SK_SUPPORT_GPU
179 if (NULL != ctx) {
180 canvas->translate(80, 0);
181 test_surface(canvas, surf3);
182 }
183#endif
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000184 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000185
reed@google.com97af1a62012-08-28 12:19:02 +0000186 virtual uint32_t onGetFlags() const SK_OVERRIDE {
reed@google.com9c728272012-08-28 15:54:54 +0000187 return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag;
reed@google.com97af1a62012-08-28 12:19:02 +0000188 }
189
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000190private:
191 typedef skiagm::GM INHERITED;
192};
rmistry@google.comae933ce2012-08-23 18:19:56 +0000193
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000194//////////////////////////////////////////////////////////////////////////////
195
196static skiagm::GM* MyFactory(void*) { return new ImageGM; }
197static skiagm::GMRegistry reg(MyFactory);