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
142
143
144
145
146
147
148
|
/*
* Copyright 2013 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 "SkBitmap.h"
#include "SkRandom.h"
#include "SkShader.h"
#include "SkXfermode.h"
namespace skiagm {
/**
* Renders overlapping shapes with random SkXfermode::Modes against a checkerboard.
*/
class MixedXfermodesGM : public GM {
public:
MixedXfermodesGM() {
}
protected:
virtual SkString onShortName() SK_OVERRIDE {
return SkString("mixed_xfermodes");
}
virtual SkISize onISize() SK_OVERRIDE {
return make_isize(790, 640);
}
void drawShape(SkCanvas* canvas,
const SkPaint& paint,
SkMWCRandom* random) {
static const SkRect kRect = SkRect::MakeXYWH(SkIntToScalar(-50), SkIntToScalar(-50),
SkIntToScalar(75), SkIntToScalar(105));
int shape = random->nextULessThan(5);
switch (shape) {
case 0:
canvas->drawCircle(0, 0, 50, paint);
break;
case 1:
canvas->drawRoundRect(kRect, SkIntToScalar(10), SkIntToScalar(20), paint);
break;
case 2:
canvas->drawRect(kRect, paint);
break;
case 3:
if (fConvexPath.isEmpty()) {
SkPoint points[4];
kRect.toQuad(points);
fConvexPath.moveTo(points[0]);
fConvexPath.quadTo(points[1], points[2]);
fConvexPath.quadTo(points[3], points[0]);
SkASSERT(fConvexPath.isConvex());
}
canvas->drawPath(fConvexPath, paint);
break;
case 4:
if (fConcavePath.isEmpty()) {
SkPoint points[5] = {{0, SkIntToScalar(-50)} };
SkMatrix rot;
rot.setRotate(SkIntToScalar(360) / 5);
for (int i = 1; i < 5; ++i) {
rot.mapPoints(points + i, points + i - 1, 1);
}
fConcavePath.moveTo(points[0]);
for (int i = 0; i < 5; ++i) {
fConcavePath.lineTo(points[(2 * i) % 5]);
}
fConcavePath.setFillType(SkPath::kEvenOdd_FillType);
SkASSERT(!fConcavePath.isConvex());
}
canvas->drawPath(fConcavePath, paint);
break;
}
}
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
if (NULL == fBG.get()) {
static uint32_t kCheckerPixelData[] = { 0xFFFFFFFF,
0xFFCCCCCC,
0xFFCCCCCC,
0xFFFFFFFF };
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, 2, 2, 2 * sizeof(uint32_t));
bitmap.allocPixels();
bitmap.lockPixels();
memcpy(bitmap.getPixels(), kCheckerPixelData, sizeof(kCheckerPixelData));
bitmap.unlockPixels();
fBG.reset(SkShader::CreateBitmapShader(bitmap,
SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode));
}
SkMatrix lm;
lm.setScale(SkIntToScalar(20), SkIntToScalar(20));
fBG->setLocalMatrix(lm);
SkPaint bgPaint;
bgPaint.setShader(fBG.get());
canvas->drawPaint(bgPaint);
SkISize size = canvas->getDeviceSize();
SkScalar maxScale = SkScalarSqrt((SkIntToScalar(size.fWidth * size.fHeight))) / 300;
SkMWCRandom random;
for (int i = 0; i < kNumShapes; ++i) {
SkScalar s = random.nextRangeScalar(SK_Scalar1 / 8, SK_Scalar1) * maxScale;
SkScalar r = random.nextRangeScalar(0, SkIntToScalar(360));
SkScalar dx = random.nextRangeScalar(0, SkIntToScalar(size.fWidth));
SkScalar dy = random.nextRangeScalar(0, SkIntToScalar(size.fHeight));
SkColor color = random.nextU();
SkXfermode::Mode mode =
static_cast<SkXfermode::Mode>(random.nextULessThan(SkXfermode::kLastMode + 1));
SkPaint p;
p.setAntiAlias(true);
p.setColor(color);
p.setXfermodeMode(mode);
canvas->save();
canvas->translate(dx, dy);
canvas->scale(s, s);
canvas->rotate(r);
this->drawShape(canvas, p, &random);
canvas->restore();
}
}
virtual uint32_t onGetFlags() const {
// Skip PDF rasterization since rendering this PDF takes forever.
return kSkipPDFRasterization_Flag;
}
private:
enum {
kNumShapes = 100,
};
SkAutoTUnref<SkShader> fBG;
SkPath fConcavePath;
SkPath fConvexPath;
typedef GM INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
static GM* MyFactory(void*) { return new MixedXfermodesGM; }
static GMRegistry reg(MyFactory);
}
|