blob: 60474da5cc4f1a92b0b38a85fb6a8efb2003dd38 [file] [log] [blame]
commit-bot@chromium.org78a10782013-08-21 19:27:48 +00001
2/*
3 * Copyright 2013 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9// This test only works with the GPU backend.
10
11#include "gm.h"
12
13#if SK_SUPPORT_GPU && 0 // Can be enabled when cubic effect is checked in.
14
15#include "GrContext.h"
16#include "GrPathUtils.h"
17#include "GrTest.h"
18#include "SkColorPriv.h"
19#include "SkDevice.h"
20
21// Position & KLM line eq values. These are the vertex attributes for Bezier curves. The last value
22// of the Vec4f is ignored.
23extern const GrVertexAttrib kAttribs[] = {
24 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
25 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
26};
27
28static inline SkScalar eval_line(const SkPoint& p, const SkScalar lineEq[3], SkScalar sign) {
29 return sign * (lineEq[0] * p.fX + lineEq[1] * p.fY + lineEq[2]);
30}
31
32namespace skiagm {
33/**
34 * This GM directly exercises effects that draw Bezier curves in the GPU backend.
35 */
36class BezierEffects : public GM {
37public:
38 BezierEffects() {
39 this->setBGColor(0xFFFFFFFF);
40 }
41
42protected:
43 virtual SkString onShortName() SK_OVERRIDE {
44 return SkString("bezier_effects");
45 }
46
47 virtual SkISize onISize() SK_OVERRIDE {
48 return make_isize(800, 800);
49 }
50
51 virtual uint32_t onGetFlags() const SK_OVERRIDE {
52 // This is a GPU-specific GM.
53 return kGPUOnly_Flag;
54 }
55
56
57 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
58 SkDevice* device = canvas->getTopDevice();
59 GrRenderTarget* rt = device->accessRenderTarget();
60 if (NULL == rt) {
61 return;
62 }
63 GrContext* context = rt->getContext();
64 if (NULL == context) {
65 return;
66 }
67
68 struct Vertex {
69 SkPoint fPosition;
70 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
71 };
72
73 static const int kNumCubics = 10;
74 SkMWCRandom rand;
75
76 int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumCubics)));
77 int numRows = SkScalarCeilToInt(SkIntToScalar(kNumCubics) / numCols);
78 SkScalar w = SkIntToScalar(rt->width()) / numCols;
79 SkScalar h = SkIntToScalar(rt->height()) / numRows;
80 int row = 0;
81 int col = 0;
82
83 for (int i = 0; i < kNumCubics; ++i) {
84 SkScalar x = SkScalarMul(col, w);
85 SkScalar y = SkScalarMul(row, h);
86 SkPoint controlPts[] = {
87 {x + rand.nextRangeF(0, w), y + rand.nextRangeF(0, h)},
88 {x + rand.nextRangeF(0, w), y + rand.nextRangeF(0, h)},
89 {x + rand.nextRangeF(0, w), y + rand.nextRangeF(0, h)},
90 {x + rand.nextRangeF(0, w), y + rand.nextRangeF(0, h)}
91 };
92 SkPoint chopped[10];
93 SkScalar klmEqs[9];
94 SkScalar klmSigns[3];
95 int cnt = GrPathUtils::chopCubicAtLoopIntersection(controlPts,
96 chopped,
97 klmEqs,
98 klmSigns,
99 controlPts);
100
101 SkPaint ctrlPtPaint;
102 ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
103 for (int i = 0; i < 4; ++i) {
104 canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
105 }
106
107 SkPaint polyPaint;
108 polyPaint.setColor(0xffA0A0A0);
109 polyPaint.setStrokeWidth(0);
110 polyPaint.setStyle(SkPaint::kStroke_Style);
111 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, controlPts, polyPaint);
112
113 SkPaint choppedPtPaint;
114 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
115
116 for (int c = 0; c < cnt; ++c) {
117 SkPoint* pts = chopped + 3 * c;
118
119 for (int i = 0; i < 4; ++i) {
120 canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
121 }
122
123 SkRect bounds;
124 bounds.set(pts, 4);
125
126 SkPaint boundsPaint;
127 boundsPaint.setColor(0xff808080);
128 boundsPaint.setStrokeWidth(0);
129 boundsPaint.setStyle(SkPaint::kStroke_Style);
130 canvas->drawRect(bounds, boundsPaint);
131
132 Vertex verts[4];
133 verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop,
134 bounds.fRight, bounds.fBottom,
135 sizeof(Vertex));
136 for (int v = 0; v < 4; ++v) {
137 verts[v].fKLM[0] = eval_line(verts[v].fPosition, klmEqs + 0, klmSigns[c]);
138 verts[v].fKLM[1] = eval_line(verts[v].fPosition, klmEqs + 3, klmSigns[c]);
139 verts[v].fKLM[2] = eval_line(verts[v].fPosition, klmEqs + 6, 1.f);
140 }
141
142 GrTestTarget tt;
143 context->getTestTarget(&tt);
144 if (NULL == tt.target()) {
145 continue;
146 }
147 GrDrawState* drawState = tt.target()->drawState();
148 drawState->setVertexAttribs<kAttribs>(2);
149 SkAutoTUnref<GrEffectRef> effect(HairCubicEdgeEffect::Create());
150 if (!effect) {
151 continue;
152 }
153 drawState->addCoverageEffect(effect, 1);
154 drawState->setRenderTarget(rt);
155 drawState->setColor(0xff000000);
156
157 tt.target()->setVertexSourceToArray(verts, 4);
158 tt.target()->setIndexSourceToBuffer(context->getQuadIndexBuffer());
159 tt.target()->drawIndexed(kTriangleFan_GrPrimitiveType, 0, 0, 4, 6);
160 }
161 ++col;
162 if (numCols == col) {
163 col = 0;
164 ++row;
165 }
166 }
167 }
168
169private:
170 typedef GM INHERITED;
171};
172
173//////////////////////////////////////////////////////////////////////////////
174
175DEF_GM( return SkNEW(BezierEffects); )
176
177}
178
179#endif