OLD | NEW |
---|---|
(Empty) | |
1 | |
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 | |
robertphillips
2013/08/20 16:47:34
// Position & KLM coefficient. These are the verte
bsalomon
2013/08/21 14:16:03
Done.
| |
21 extern const GrVertexAttrib kAttribs[] = { | |
22 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, | |
23 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} | |
24 }; | |
25 | |
26 static inline SkScalar eval_line(const SkPoint& p, const SkScalar lineEq[3], SkS calar sign) { | |
27 return sign * (lineEq[0] * p.fX + lineEq[1] * p.fY + lineEq[2]); | |
28 } | |
29 | |
robertphillips
2013/08/20 16:47:34
Should the namespace also enclose kAttribs & eval_
bsalomon
2013/08/21 14:16:03
I don't think so.
| |
30 namespace skiagm { | |
31 | |
robertphillips
2013/08/20 16:47:34
// This GM ...
bsalomon
2013/08/21 14:16:03
Done.
| |
32 class BezierEffects : public GM { | |
33 public: | |
34 BezierEffects() { | |
35 this->setBGColor(0xFFFFFFFF); | |
36 } | |
37 | |
38 protected: | |
39 virtual SkString onShortName() SK_OVERRIDE { | |
40 return SkString("bezier_effects"); | |
41 } | |
42 | |
43 virtual SkISize onISize() SK_OVERRIDE { | |
44 return make_isize(800, 800); | |
45 } | |
46 | |
47 virtual uint32_t onGetFlags() const SK_OVERRIDE { | |
48 // This is a GPU-specific GM. | |
49 return kGPUOnly_Flag; | |
50 } | |
51 | |
52 | |
53 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | |
54 SkDevice* device = canvas->getTopDevice(); | |
55 GrRenderTarget* rt = device->accessRenderTarget(); | |
56 if (NULL == rt) { | |
57 return; | |
58 } | |
59 GrContext* context = rt->getContext(); | |
60 if (NULL == context) { | |
61 return; | |
62 } | |
63 | |
64 struct Vertex { | |
65 SkPoint fPosition; | |
66 float fKLM[4]; // The last value is ignored. The effect expects a vec4f. | |
67 }; | |
68 | |
69 static const int kNumCubics = 10; | |
70 SkMWCRandom rand; | |
71 | |
72 int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumCubics))) ; | |
73 int numRows = SkScalarCeilToInt(SkIntToScalar(kNumCubics) / numCols); | |
74 SkScalar w = SkIntToScalar(rt->width()) / numCols; | |
75 SkScalar h = SkIntToScalar(rt->height()) / numRows; | |
76 int row = 0; | |
77 int col = 0; | |
78 | |
79 for (int i = 0; i < kNumCubics; ++i) { | |
80 SkScalar x = SkScalarMul(col, w); | |
81 SkScalar y = SkScalarMul(row, h); | |
82 SkPoint controlPts[] = { | |
83 {x + rand.nextRangeF(0, w), y + rand.nextRangeF(0, h)}, | |
84 {x + rand.nextRangeF(0, w), y + rand.nextRangeF(0, h)}, | |
85 {x + rand.nextRangeF(0, w), y + rand.nextRangeF(0, h)}, | |
86 {x + rand.nextRangeF(0, w), y + rand.nextRangeF(0, h)} | |
87 }; | |
88 SkPoint chopped[10]; | |
89 SkScalar klmEqs[9]; | |
90 SkScalar klmSigns[3]; | |
91 int cnt = GrPathUtils::chopCubicAtLoopIntersection(controlPts, | |
92 chopped, | |
93 klmEqs, | |
94 klmSigns, | |
95 controlPts); | |
96 | |
97 SkPaint ctrlPtPaint; | |
98 ctrlPtPaint.setColor(rand.nextU() | 0xFF000000); | |
99 for (int i = 0; i < 4; ++i) { | |
100 canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrl PtPaint); | |
101 } | |
102 | |
103 SkPaint polyPaint; | |
104 polyPaint.setColor(0xffA0A0A0); | |
105 polyPaint.setStrokeWidth(0); | |
106 polyPaint.setStyle(SkPaint::kStroke_Style); | |
107 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, controlPts, poly Paint); | |
108 | |
109 SkPaint choppedPtPaint; | |
110 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000); | |
111 | |
112 for (int c = 0; c < cnt; ++c) { | |
113 SkPoint* pts = chopped + 3 * c; | |
114 | |
115 for (int i = 0; i < 4; ++i) { | |
116 canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint ); | |
117 } | |
118 | |
119 Vertex verts[4]; | |
120 SkRect bounds; | |
121 bounds.set(pts, 4); | |
robertphillips
2013/08/20 16:47:34
Doesn't set do all of the following 8 lines?
bsalomon
2013/08/21 14:16:03
oops.. yes... this creeped in when Greg and I were
| |
122 bounds.fLeft = bounds.fRight = pts[0].fX; | |
123 bounds.fTop = bounds.fBottom = pts[0].fY; | |
124 for (int i = 1; i < 4; ++i) { | |
125 bounds.fLeft = SkTMin(bounds.fLeft, pts[i].fX); | |
126 bounds.fRight = SkTMax(bounds.fRight, pts[i].fX); | |
127 bounds.fTop = SkTMin(bounds.fTop, pts[i].fY); | |
128 bounds.fBottom = SkTMax(bounds.fBottom, pts[i].fY); | |
129 }; | |
130 | |
131 SkPaint boundsPaint; | |
132 boundsPaint.setColor(0xff808080); | |
133 boundsPaint.setStrokeWidth(0); | |
134 boundsPaint.setStyle(SkPaint::kStroke_Style); | |
135 canvas->drawRect(bounds, boundsPaint); | |
136 | |
robertphillips
2013/08/20 16:47:34
Move "Vertex verts[4];" down here?
bsalomon
2013/08/21 14:16:03
Done.
| |
137 verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop, | |
138 bounds.fRight, bounds.fBottom, | |
139 sizeof(Vertex)); | |
140 for (int v = 0; v < 4; ++v) { | |
141 verts[v].fKLM[0] = eval_line(verts[v].fPosition, klmEqs + 0, klmSigns[c]); | |
142 verts[v].fKLM[1] = eval_line(verts[v].fPosition, klmEqs + 3, klmSigns[c]); | |
143 verts[v].fKLM[2] = eval_line(verts[v].fPosition, klmEqs + 6, 1.f); | |
144 } | |
145 | |
146 GrTestTarget tt; | |
147 context->getTestTarget(&tt); | |
148 if (NULL == tt.target()) { | |
149 continue; | |
150 } | |
151 GrDrawState* drawState = tt.target()->drawState(); | |
152 drawState->setVertexAttribs<kAttribs>(2); | |
153 SkAutoTUnref<GrEffectRef> effect(HairCubicEdgeEffect::Create()); | |
154 if (!effect) { | |
155 continue; | |
156 } | |
157 drawState->addCoverageEffect(effect, 1); | |
158 drawState->setRenderTarget(rt); | |
159 drawState->setColor(0xff000000); | |
160 | |
161 tt.target()->setVertexSourceToArray(verts, 4); | |
162 tt.target()->setIndexSourceToBuffer(context->getQuadIndexBuffer( )); | |
163 tt.target()->drawIndexed(kTriangleFan_GrPrimitiveType, 0, 0, 4, 6); | |
164 } | |
165 ++col; | |
166 if (numCols == col) { | |
167 col = 0; | |
168 ++row; | |
169 } | |
170 } | |
171 } | |
172 | |
173 private: | |
174 typedef GM INHERITED; | |
175 }; | |
176 | |
177 ////////////////////////////////////////////////////////////////////////////// | |
178 | |
179 DEF_GM( return SkNEW(BezierEffects); ) | |
180 | |
181 } | |
182 | |
183 #endif | |
OLD | NEW |