1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm.h"
#include "SkSurface.h"
#include "SkCanvas.h"
#include "SkStream.h"
#include "SkData.h"
static SkData* fileToData(const char path[]) {
SkFILEStream stream(path);
if (!stream.isValid()) {
return SkData::NewEmpty();
}
size_t size = stream.getLength();
void* mem = sk_malloc_throw(size);
stream.read(mem, size);
return SkData::NewFromMalloc(mem, size);
}
static void drawJpeg(SkCanvas* canvas, const SkISize& size) {
SkAutoDataUnref data(fileToData("/Users/mike/Downloads/skia.google.jpeg"));
SkImage* image = SkImage::NewEncodedData(data);
if (image) {
SkAutoCanvasRestore acr(canvas, true);
canvas->scale(size.width() * 1.0f / image->width(),
size.height() * 1.0f / image->height());
image->draw(canvas,0, 0, NULL);
image->unref();
}
}
static void drawContents(SkSurface* surface, SkColor fillC) {
SkSize size = SkSize::Make(surface->width(), surface->height());
SkCanvas* canvas = surface->getCanvas();
SkScalar stroke = size.fWidth / 10;
SkScalar radius = (size.fWidth - stroke) / 2;
SkPaint paint;
paint.setAntiAlias(true);
paint.setColor(fillC);
canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(stroke);
paint.setColor(SK_ColorBLACK);
canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
}
static void test_surface(SkCanvas* canvas, SkSurface* surf) {
drawContents(surf, SK_ColorRED);
SkImage* imgR = surf->newImageShapshot();
drawContents(surf, SK_ColorGREEN);
SkImage* imgG = surf->newImageShapshot();
drawContents(surf, SK_ColorBLUE);
SkPaint paint;
// paint.setFilterBitmap(true);
// paint.setAlpha(0x80);
imgR->draw(canvas, 0, 0, &paint);
imgG->draw(canvas, 0, 80, &paint);
surf->draw(canvas, 0, 160, &paint);
imgG->unref();
imgR->unref();
}
class ImageGM : public skiagm::GM {
void* fBuffer;
size_t fBufferSize;
SkSize fSize;
enum {
W = 64,
H = 64,
RB = W * 4 + 8,
};
public:
ImageGM() {
fBufferSize = RB * H;
fBuffer = sk_malloc_throw(fBufferSize);
fSize.set(SkIntToScalar(W), SkIntToScalar(H));
}
virtual ~ImageGM() {
sk_free(fBuffer);
}
protected:
virtual SkString onShortName() {
return SkString("image");
}
virtual SkISize onISize() {
return SkISize::Make(640, 480);
}
virtual void onDraw(SkCanvas* canvas) {
drawJpeg(canvas, this->getISize());
canvas->translate(10, 10);
canvas->scale(2, 2);
// since we draw into this directly, we need to start fresh
sk_bzero(fBuffer, fBufferSize);
SkImage::Info info;
info.fWidth = W;
info.fHeight = H;
info.fColorType = SkImage::kPMColor_ColorType;
info.fAlphaType = SkImage::kPremul_AlphaType;
SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, NULL, fBuffer, RB));
SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info, NULL));
SkAutoTUnref<SkSurface> surf2(SkSurface::NewPicture(info.fWidth, info.fHeight));
test_surface(canvas, surf0);
canvas->translate(80, 0);
test_surface(canvas, surf1);
canvas->translate(80, 0);
test_surface(canvas, surf2);
}
private:
typedef skiagm::GM INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
static skiagm::GM* MyFactory(void*) { return new ImageGM; }
static skiagm::GMRegistry reg(MyFactory);
|