blob: a98fa3f3331b587e39fb012cdd508ce4c77a77f9 [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
reed@google.com97af1a62012-08-28 12:19:02 +000014extern GrContext* GetGr();
15
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000016static SkData* fileToData(const char path[]) {
17 SkFILEStream stream(path);
18 if (!stream.isValid()) {
19 return SkData::NewEmpty();
20 }
21 size_t size = stream.getLength();
22 void* mem = sk_malloc_throw(size);
23 stream.read(mem, size);
24 return SkData::NewFromMalloc(mem, size);
25}
26
27static void drawJpeg(SkCanvas* canvas, const SkISize& size) {
28 SkAutoDataUnref data(fileToData("/Users/mike/Downloads/skia.google.jpeg"));
29 SkImage* image = SkImage::NewEncodedData(data);
30 if (image) {
31 SkAutoCanvasRestore acr(canvas, true);
32 canvas->scale(size.width() * 1.0f / image->width(),
33 size.height() * 1.0f / image->height());
34 image->draw(canvas,0, 0, NULL);
35 image->unref();
36 }
37}
mike@reedtribe.org70e35902012-07-29 20:38:16 +000038
39static void drawContents(SkSurface* surface, SkColor fillC) {
skia.committer@gmail.com04ba4482012-09-07 02:01:30 +000040 SkSize size = SkSize::Make(SkIntToScalar(surface->width()),
robertphillips@google.com94acc702012-09-06 18:43:21 +000041 SkIntToScalar(surface->height()));
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000042 SkCanvas* canvas = surface->getCanvas();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000043
44 SkScalar stroke = size.fWidth / 10;
45 SkScalar radius = (size.fWidth - stroke) / 2;
46
47 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000048
mike@reedtribe.org70e35902012-07-29 20:38:16 +000049 paint.setAntiAlias(true);
50 paint.setColor(fillC);
51 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000052
mike@reedtribe.org70e35902012-07-29 20:38:16 +000053 paint.setStyle(SkPaint::kStroke_Style);
54 paint.setStrokeWidth(stroke);
55 paint.setColor(SK_ColorBLACK);
56 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
57}
58
59static void test_surface(SkCanvas* canvas, SkSurface* surf) {
60 drawContents(surf, SK_ColorRED);
61 SkImage* imgR = surf->newImageShapshot();
62
reed@google.com97af1a62012-08-28 12:19:02 +000063 if (true) {
64 SkImage* imgR2 = surf->newImageShapshot();
65 SkASSERT(imgR == imgR2);
66 imgR2->unref();
67 }
68
mike@reedtribe.org70e35902012-07-29 20:38:16 +000069 drawContents(surf, SK_ColorGREEN);
70 SkImage* imgG = surf->newImageShapshot();
71
reed@google.com97af1a62012-08-28 12:19:02 +000072 // since we've drawn after we snapped imgR, imgG will be a different obj
73 SkASSERT(imgR != imgG);
74
mike@reedtribe.org70e35902012-07-29 20:38:16 +000075 drawContents(surf, SK_ColorBLUE);
76
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000077 SkPaint paint;
78// paint.setFilterBitmap(true);
79// paint.setAlpha(0x80);
80
81 imgR->draw(canvas, 0, 0, &paint);
82 imgG->draw(canvas, 0, 80, &paint);
83 surf->draw(canvas, 0, 160, &paint);
mike@reedtribe.org70e35902012-07-29 20:38:16 +000084
85 imgG->unref();
86 imgR->unref();
87}
88
89class ImageGM : public skiagm::GM {
90 void* fBuffer;
reed@google.com58b21ec2012-07-30 18:20:12 +000091 size_t fBufferSize;
mike@reedtribe.org70e35902012-07-29 20:38:16 +000092 SkSize fSize;
93 enum {
94 W = 64,
95 H = 64,
96 RB = W * 4 + 8,
97 };
98public:
99 ImageGM() {
reed@google.com58b21ec2012-07-30 18:20:12 +0000100 fBufferSize = RB * H;
101 fBuffer = sk_malloc_throw(fBufferSize);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000102 fSize.set(SkIntToScalar(W), SkIntToScalar(H));
103 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000104
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000105 virtual ~ImageGM() {
106 sk_free(fBuffer);
107 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
109
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000110protected:
111 virtual SkString onShortName() {
112 return SkString("image");
113 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000114
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000115 virtual SkISize onISize() {
116 return SkISize::Make(640, 480);
117 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000118
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000119 virtual void onDraw(SkCanvas* canvas) {
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +0000120 drawJpeg(canvas, this->getISize());
121
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +0000122 canvas->translate(10, 10);
123 canvas->scale(2, 2);
124
reed@google.com58b21ec2012-07-30 18:20:12 +0000125 // since we draw into this directly, we need to start fresh
126 sk_bzero(fBuffer, fBufferSize);
127
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000128 SkImage::Info info;
129
130 info.fWidth = W;
131 info.fHeight = H;
132 info.fColorType = SkImage::kPMColor_ColorType;
133 info.fAlphaType = SkImage::kPremul_AlphaType;
134 SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, NULL, fBuffer, RB));
135 SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info, NULL));
136 SkAutoTUnref<SkSurface> surf2(SkSurface::NewPicture(info.fWidth, info.fHeight));
137
138 test_surface(canvas, surf0);
139 canvas->translate(80, 0);
140 test_surface(canvas, surf1);
141 canvas->translate(80, 0);
142 test_surface(canvas, surf2);
143 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
reed@google.com97af1a62012-08-28 12:19:02 +0000145 virtual uint32_t onGetFlags() const SK_OVERRIDE {
reed@google.com9c728272012-08-28 15:54:54 +0000146 return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag;
reed@google.com97af1a62012-08-28 12:19:02 +0000147 }
148
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000149private:
150 typedef skiagm::GM INHERITED;
151};
rmistry@google.comae933ce2012-08-23 18:19:56 +0000152
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000153//////////////////////////////////////////////////////////////////////////////
154
155static skiagm::GM* MyFactory(void*) { return new ImageGM; }
156static skiagm::GMRegistry reg(MyFactory);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000157