blob: 8947f0a4b7ab74330ca0ff37e17475996721d4ad [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"
11
12static void drawContents(SkSurface* surface, SkColor fillC) {
13 SkSize size = SkSize::Make(surface->width(), surface->height());
14 SkAutoTUnref<SkCanvas> canvas(surface->newCanvas());
15
16 SkScalar stroke = size.fWidth / 10;
17 SkScalar radius = (size.fWidth - stroke) / 2;
18
19 SkPaint paint;
20
21 paint.setAntiAlias(true);
22 paint.setColor(fillC);
23 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
24
25 paint.setStyle(SkPaint::kStroke_Style);
26 paint.setStrokeWidth(stroke);
27 paint.setColor(SK_ColorBLACK);
28 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
29}
30
31static void test_surface(SkCanvas* canvas, SkSurface* surf) {
32 drawContents(surf, SK_ColorRED);
33 SkImage* imgR = surf->newImageShapshot();
34
35 drawContents(surf, SK_ColorGREEN);
36 SkImage* imgG = surf->newImageShapshot();
37
38 drawContents(surf, SK_ColorBLUE);
39
40 imgR->draw(canvas, 0, 0, NULL);
41 imgG->draw(canvas, 0, 80, NULL);
42 surf->draw(canvas, 0, 160, NULL);
43
44 imgG->unref();
45 imgR->unref();
46}
47
48class ImageGM : public skiagm::GM {
49 void* fBuffer;
reed@google.com58b21ec2012-07-30 18:20:12 +000050 size_t fBufferSize;
mike@reedtribe.org70e35902012-07-29 20:38:16 +000051 SkSize fSize;
52 enum {
53 W = 64,
54 H = 64,
55 RB = W * 4 + 8,
56 };
57public:
58 ImageGM() {
reed@google.com58b21ec2012-07-30 18:20:12 +000059 fBufferSize = RB * H;
60 fBuffer = sk_malloc_throw(fBufferSize);
mike@reedtribe.org70e35902012-07-29 20:38:16 +000061 fSize.set(SkIntToScalar(W), SkIntToScalar(H));
62 }
63
64 virtual ~ImageGM() {
65 sk_free(fBuffer);
66 }
67
68
69protected:
70 virtual SkString onShortName() {
71 return SkString("image");
72 }
73
74 virtual SkISize onISize() {
75 return SkISize::Make(640, 480);
76 }
77
78 virtual void onDraw(SkCanvas* canvas) {
reed@google.com58b21ec2012-07-30 18:20:12 +000079 // since we draw into this directly, we need to start fresh
80 sk_bzero(fBuffer, fBufferSize);
81
mike@reedtribe.org70e35902012-07-29 20:38:16 +000082 SkImage::Info info;
83
84 info.fWidth = W;
85 info.fHeight = H;
86 info.fColorType = SkImage::kPMColor_ColorType;
87 info.fAlphaType = SkImage::kPremul_AlphaType;
88 SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, NULL, fBuffer, RB));
89 SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info, NULL));
90 SkAutoTUnref<SkSurface> surf2(SkSurface::NewPicture(info.fWidth, info.fHeight));
91
92 test_surface(canvas, surf0);
93 canvas->translate(80, 0);
94 test_surface(canvas, surf1);
95 canvas->translate(80, 0);
96 test_surface(canvas, surf2);
97 }
98
99private:
100 typedef skiagm::GM INHERITED;
101};
102
103//////////////////////////////////////////////////////////////////////////////
104
105static skiagm::GM* MyFactory(void*) { return new ImageGM; }
106static skiagm::GMRegistry reg(MyFactory);
107