blob: 22279563e0687c39fcc624b9a6c1c150b757a1ba [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());
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000014 SkCanvas* canvas = surface->getCanvas();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000015
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
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000040 SkPaint paint;
41// paint.setFilterBitmap(true);
42// paint.setAlpha(0x80);
43
44 imgR->draw(canvas, 0, 0, &paint);
45 imgG->draw(canvas, 0, 80, &paint);
46 surf->draw(canvas, 0, 160, &paint);
mike@reedtribe.org70e35902012-07-29 20:38:16 +000047
48 imgG->unref();
49 imgR->unref();
50}
51
52class ImageGM : public skiagm::GM {
53 void* fBuffer;
reed@google.com58b21ec2012-07-30 18:20:12 +000054 size_t fBufferSize;
mike@reedtribe.org70e35902012-07-29 20:38:16 +000055 SkSize fSize;
56 enum {
57 W = 64,
58 H = 64,
59 RB = W * 4 + 8,
60 };
61public:
62 ImageGM() {
reed@google.com58b21ec2012-07-30 18:20:12 +000063 fBufferSize = RB * H;
64 fBuffer = sk_malloc_throw(fBufferSize);
mike@reedtribe.org70e35902012-07-29 20:38:16 +000065 fSize.set(SkIntToScalar(W), SkIntToScalar(H));
66 }
67
68 virtual ~ImageGM() {
69 sk_free(fBuffer);
70 }
71
72
73protected:
74 virtual SkString onShortName() {
75 return SkString("image");
76 }
77
78 virtual SkISize onISize() {
79 return SkISize::Make(640, 480);
80 }
81
82 virtual void onDraw(SkCanvas* canvas) {
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000083 canvas->translate(10, 10);
84 canvas->scale(2, 2);
85
reed@google.com58b21ec2012-07-30 18:20:12 +000086 // since we draw into this directly, we need to start fresh
87 sk_bzero(fBuffer, fBufferSize);
88
mike@reedtribe.org70e35902012-07-29 20:38:16 +000089 SkImage::Info info;
90
91 info.fWidth = W;
92 info.fHeight = H;
93 info.fColorType = SkImage::kPMColor_ColorType;
94 info.fAlphaType = SkImage::kPremul_AlphaType;
95 SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, NULL, fBuffer, RB));
96 SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info, NULL));
97 SkAutoTUnref<SkSurface> surf2(SkSurface::NewPicture(info.fWidth, info.fHeight));
98
99 test_surface(canvas, surf0);
100 canvas->translate(80, 0);
101 test_surface(canvas, surf1);
102 canvas->translate(80, 0);
103 test_surface(canvas, surf2);
104 }
105
106private:
107 typedef skiagm::GM INHERITED;
108};
109
110//////////////////////////////////////////////////////////////////////////////
111
112static skiagm::GM* MyFactory(void*) { return new ImageGM; }
113static skiagm::GMRegistry reg(MyFactory);
114