Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(270)

Unified Diff: gm/beziereffects.cpp

Issue 23361024: Add GMs for gpu Bezier shaders (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/gpu/effects/GrBezierEffect.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gm/beziereffects.cpp
diff --git a/gm/beziereffects.cpp b/gm/beziereffects.cpp
index 60474da5cc4f1a92b0b38a85fb6a8efb2003dd38..19ba4e298a4596eae73324b793de5261836cb958 100644
--- a/gm/beziereffects.cpp
+++ b/gm/beziereffects.cpp
@@ -10,13 +10,16 @@
#include "gm.h"
-#if SK_SUPPORT_GPU && 0 // Can be enabled when cubic effect is checked in.
+#if SK_SUPPORT_GPU
#include "GrContext.h"
#include "GrPathUtils.h"
#include "GrTest.h"
#include "SkColorPriv.h"
#include "SkDevice.h"
+#include "SkGeometry.h"
+
+#include "effects/GrBezierEffect.h"
// Position & KLM line eq values. These are the vertex attributes for Bezier curves. The last value
// of the Vec4f is ignored.
@@ -33,15 +36,15 @@ namespace skiagm {
/**
* This GM directly exercises effects that draw Bezier curves in the GPU backend.
*/
-class BezierEffects : public GM {
+class BezierCubicEffects : public GM {
public:
- BezierEffects() {
+ BezierCubicEffects() {
this->setBGColor(0xFFFFFFFF);
}
protected:
virtual SkString onShortName() SK_OVERRIDE {
- return SkString("bezier_effects");
+ return SkString("bezier_cubic_effects");
}
virtual SkISize onISize() SK_OVERRIDE {
@@ -70,109 +73,445 @@ protected:
float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
};
- static const int kNumCubics = 10;
+ static const int kNumCubics = 15;
SkMWCRandom rand;
- int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumCubics)));
- int numRows = SkScalarCeilToInt(SkIntToScalar(kNumCubics) / numCols);
+ // Mult by 3 for each edge effect type
+ int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumCubics*3)));
+ int numRows = SkScalarCeilToInt(SkIntToScalar(kNumCubics*3) / numCols);
SkScalar w = SkIntToScalar(rt->width()) / numCols;
SkScalar h = SkIntToScalar(rt->height()) / numRows;
int row = 0;
int col = 0;
for (int i = 0; i < kNumCubics; ++i) {
- SkScalar x = SkScalarMul(col, w);
- SkScalar y = SkScalarMul(row, h);
- SkPoint controlPts[] = {
- {x + rand.nextRangeF(0, w), y + rand.nextRangeF(0, h)},
- {x + rand.nextRangeF(0, w), y + rand.nextRangeF(0, h)},
- {x + rand.nextRangeF(0, w), y + rand.nextRangeF(0, h)},
- {x + rand.nextRangeF(0, w), y + rand.nextRangeF(0, h)}
+ SkPoint baseControlPts[] = {
+ {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
+ {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
+ {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
+ {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
};
- SkPoint chopped[10];
- SkScalar klmEqs[9];
- SkScalar klmSigns[3];
- int cnt = GrPathUtils::chopCubicAtLoopIntersection(controlPts,
- chopped,
- klmEqs,
- klmSigns,
- controlPts);
-
- SkPaint ctrlPtPaint;
- ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
- for (int i = 0; i < 4; ++i) {
- canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
+ for(int edgeType = kFillAA_GrBezierEdgeType; edgeType < 3; ++edgeType) {
+ SkScalar x = SkScalarMul(col, w);
+ SkScalar y = SkScalarMul(row, h);
+ SkPoint controlPts[] = {
+ {x + baseControlPts[0].fX, y + baseControlPts[0].fY},
+ {x + baseControlPts[1].fX, y + baseControlPts[1].fY},
+ {x + baseControlPts[2].fX, y + baseControlPts[2].fY},
+ {x + baseControlPts[3].fX, y + baseControlPts[3].fY}
+ };
+ SkPoint chopped[10];
+ SkScalar klmEqs[9];
+ SkScalar klmSigns[3];
+ int cnt = GrPathUtils::chopCubicAtLoopIntersection(controlPts,
+ chopped,
+ klmEqs,
+ klmSigns);
+
+ SkPaint ctrlPtPaint;
+ ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
+ for (int i = 0; i < 4; ++i) {
+ canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
+ }
+
+ SkPaint polyPaint;
+ polyPaint.setColor(0xffA0A0A0);
+ polyPaint.setStrokeWidth(0);
+ polyPaint.setStyle(SkPaint::kStroke_Style);
+ canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, controlPts, polyPaint);
+
+ SkPaint choppedPtPaint;
+ choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
+
+ for (int c = 0; c < cnt; ++c) {
+ SkPoint* pts = chopped + 3 * c;
+
+ for (int i = 0; i < 4; ++i) {
+ canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
+ }
+
+ SkRect bounds;
+ bounds.set(pts, 4);
+
+ SkPaint boundsPaint;
+ boundsPaint.setColor(0xff808080);
+ boundsPaint.setStrokeWidth(0);
+ boundsPaint.setStyle(SkPaint::kStroke_Style);
+ canvas->drawRect(bounds, boundsPaint);
+
+ Vertex verts[4];
+ verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop,
+ bounds.fRight, bounds.fBottom,
+ sizeof(Vertex));
+ for (int v = 0; v < 4; ++v) {
+ verts[v].fKLM[0] = eval_line(verts[v].fPosition, klmEqs + 0, klmSigns[c]);
+ verts[v].fKLM[1] = eval_line(verts[v].fPosition, klmEqs + 3, klmSigns[c]);
+ verts[v].fKLM[2] = eval_line(verts[v].fPosition, klmEqs + 6, 1.f);
+ }
+
+ GrTestTarget tt;
+ context->getTestTarget(&tt);
+ if (NULL == tt.target()) {
+ continue;
+ }
+ GrDrawState* drawState = tt.target()->drawState();
+ drawState->setVertexAttribs<kAttribs>(2);
+
+ SkAutoTUnref<GrEffectRef> effect(GrCubicEffect::Create(
+ GrBezierEdgeType(edgeType), *tt.target()->caps()));
+ if (!effect) {
+ continue;
+ }
+ drawState->addCoverageEffect(effect, 1);
+ drawState->setRenderTarget(rt);
+ drawState->setColor(0xff000000);
+
+ tt.target()->setVertexSourceToArray(verts, 4);
+ tt.target()->setIndexSourceToBuffer(context->getQuadIndexBuffer());
+ tt.target()->drawIndexed(kTriangleFan_GrPrimitiveType, 0, 0, 4, 6);
+ }
+ ++col;
+ if (numCols == col) {
+ col = 0;
+ ++row;
+ }
}
+ }
+ }
- SkPaint polyPaint;
- polyPaint.setColor(0xffA0A0A0);
- polyPaint.setStrokeWidth(0);
- polyPaint.setStyle(SkPaint::kStroke_Style);
- canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, controlPts, polyPaint);
+private:
+ typedef GM INHERITED;
+};
+
+//////////////////////////////////////////////////////////////////////////////
+
+/**
+ * This GM directly exercises effects that draw Bezier curves in the GPU backend.
+ */
+class BezierConicEffects : public GM {
+public:
+ BezierConicEffects() {
+ this->setBGColor(0xFFFFFFFF);
+ }
- SkPaint choppedPtPaint;
- choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
+protected:
+ virtual SkString onShortName() SK_OVERRIDE {
+ return SkString("bezier_conic_effects");
+ }
- for (int c = 0; c < cnt; ++c) {
- SkPoint* pts = chopped + 3 * c;
+ virtual SkISize onISize() SK_OVERRIDE {
+ return make_isize(800, 800);
+ }
- for (int i = 0; i < 4; ++i) {
- canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
- }
+ virtual uint32_t onGetFlags() const SK_OVERRIDE {
+ // This is a GPU-specific GM.
+ return kGPUOnly_Flag;
+ }
+
+
+ virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+ SkDevice* device = canvas->getTopDevice();
+ GrRenderTarget* rt = device->accessRenderTarget();
+ if (NULL == rt) {
+ return;
+ }
+ GrContext* context = rt->getContext();
+ if (NULL == context) {
+ return;
+ }
+
+ struct Vertex {
+ SkPoint fPosition;
+ float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
+ };
- SkRect bounds;
- bounds.set(pts, 4);
-
- SkPaint boundsPaint;
- boundsPaint.setColor(0xff808080);
- boundsPaint.setStrokeWidth(0);
- boundsPaint.setStyle(SkPaint::kStroke_Style);
- canvas->drawRect(bounds, boundsPaint);
-
- Vertex verts[4];
- verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop,
- bounds.fRight, bounds.fBottom,
- sizeof(Vertex));
- for (int v = 0; v < 4; ++v) {
- verts[v].fKLM[0] = eval_line(verts[v].fPosition, klmEqs + 0, klmSigns[c]);
- verts[v].fKLM[1] = eval_line(verts[v].fPosition, klmEqs + 3, klmSigns[c]);
- verts[v].fKLM[2] = eval_line(verts[v].fPosition, klmEqs + 6, 1.f);
+ static const int kNumConics = 10;
+ SkMWCRandom rand;
+
+ // Mult by 3 for each edge effect type
+ int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumConics*3)));
+ int numRows = SkScalarCeilToInt(SkIntToScalar(kNumConics*3) / numCols);
+ SkScalar w = SkIntToScalar(rt->width()) / numCols;
+ SkScalar h = SkIntToScalar(rt->height()) / numRows;
+ int row = 0;
+ int col = 0;
+
+ for (int i = 0; i < kNumConics; ++i) {
+ SkPoint baseControlPts[] = {
+ {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
+ {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
+ {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
+ };
+ SkScalar weight = rand.nextRangeF(0.f, 2.f);
+ for(int edgeType = kFillAA_GrBezierEdgeType; edgeType < 3; ++edgeType) {
+ SkScalar x = SkScalarMul(col, w);
+ SkScalar y = SkScalarMul(row, h);
+ SkPoint controlPts[] = {
+ {x + baseControlPts[0].fX, y + baseControlPts[0].fY},
+ {x + baseControlPts[1].fX, y + baseControlPts[1].fY},
+ {x + baseControlPts[2].fX, y + baseControlPts[2].fY}
+ };
+ SkConic dst[4];
+ SkScalar klmEqs[9];
+ int cnt = chop_conic(controlPts, dst, weight);
+ GrPathUtils::getConicKLM(controlPts, weight, klmEqs);
+
+ SkPaint ctrlPtPaint;
+ ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
+ for (int i = 0; i < 3; ++i) {
+ canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
}
- GrTestTarget tt;
- context->getTestTarget(&tt);
- if (NULL == tt.target()) {
- continue;
+ SkPaint polyPaint;
+ polyPaint.setColor(0xffA0A0A0);
+ polyPaint.setStrokeWidth(0);
+ polyPaint.setStyle(SkPaint::kStroke_Style);
+ canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
+
+ SkPaint choppedPtPaint;
+ choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
+
+ for (int c = 0; c < cnt; ++c) {
+ SkPoint* pts = dst[c].fPts;
+ for (int i = 0; i < 3; ++i) {
+ canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
+ }
+
+ SkRect bounds;
+ //SkPoint bPts[] = {{0.f, 0.f}, {800.f, 800.f}};
+ //bounds.set(bPts, 2);
+ bounds.set(pts, 3);
+
+ SkPaint boundsPaint;
+ boundsPaint.setColor(0xff808080);
+ boundsPaint.setStrokeWidth(0);
+ boundsPaint.setStyle(SkPaint::kStroke_Style);
+ canvas->drawRect(bounds, boundsPaint);
+
+ Vertex verts[4];
+ verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop,
+ bounds.fRight, bounds.fBottom,
+ sizeof(Vertex));
+ for (int v = 0; v < 4; ++v) {
+ verts[v].fKLM[0] = eval_line(verts[v].fPosition, klmEqs + 0, 1.f);
+ verts[v].fKLM[1] = eval_line(verts[v].fPosition, klmEqs + 3, 1.f);
+ verts[v].fKLM[2] = eval_line(verts[v].fPosition, klmEqs + 6, 1.f);
+ }
+
+ GrTestTarget tt;
+ context->getTestTarget(&tt);
+ if (NULL == tt.target()) {
+ continue;
+ }
+ GrDrawState* drawState = tt.target()->drawState();
+ drawState->setVertexAttribs<kAttribs>(2);
+
+ SkAutoTUnref<GrEffectRef> effect(GrConicEffect::Create(
+ GrBezierEdgeType(edgeType), *tt.target()->caps()));
+ if (!effect) {
+ continue;
+ }
+ drawState->addCoverageEffect(effect, 1);
+ drawState->setRenderTarget(rt);
+ drawState->setColor(0xff000000);
+
+ tt.target()->setVertexSourceToArray(verts, 4);
+ tt.target()->setIndexSourceToBuffer(context->getQuadIndexBuffer());
+ tt.target()->drawIndexed(kTriangleFan_GrPrimitiveType, 0, 0, 4, 6);
}
- GrDrawState* drawState = tt.target()->drawState();
- drawState->setVertexAttribs<kAttribs>(2);
- SkAutoTUnref<GrEffectRef> effect(HairCubicEdgeEffect::Create());
- if (!effect) {
- continue;
+ ++col;
+ if (numCols == col) {
+ col = 0;
+ ++row;
}
- drawState->addCoverageEffect(effect, 1);
- drawState->setRenderTarget(rt);
- drawState->setColor(0xff000000);
+ }
+ }
+ }
- tt.target()->setVertexSourceToArray(verts, 4);
- tt.target()->setIndexSourceToBuffer(context->getQuadIndexBuffer());
- tt.target()->drawIndexed(kTriangleFan_GrPrimitiveType, 0, 0, 4, 6);
+private:
+ // Uses the max curvature function for quads to estimate
+ // where to chop the conic. If the max curvature is not
+ // found along the curve segment it will return 1 and
+ // dst[0] is the original conic. If it returns 2 the dst[0]
+ // and dst[1] are the two new conics.
+ int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
+ SkScalar t = SkFindQuadMaxCurvature(src);
+ if (t == 0) {
+ if (dst) {
+ dst[0].set(src, weight);
}
- ++col;
- if (numCols == col) {
- col = 0;
- ++row;
+ return 1;
+ } else {
+ if (dst) {
+ SkConic conic;
+ conic.set(src, weight);
+ conic.chopAt(t, dst);
}
+ return 2;
}
}
-private:
+ // Calls split_conic on the entire conic and then once more on each subsection.
+ // Most cases will result in either 1 conic (chop point is not within t range)
+ // or 3 points (split once and then one subsection is split again).
+ int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
+ SkConic dstTemp[2];
+ int conicCnt = split_conic(src, dstTemp, weight);
+ if (2 == conicCnt) {
+ int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
+ conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
+ } else {
+ dst[0] = dstTemp[0];
+ }
+ return conicCnt;
+ }
+
typedef GM INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
+/**
+ * This GM directly exercises effects that draw Bezier quad curves in the GPU backend.
+ */
+class BezierQuadEffects : public GM {
+public:
+ BezierQuadEffects() {
+ this->setBGColor(0xFFFFFFFF);
+ }
+
+protected:
+ virtual SkString onShortName() SK_OVERRIDE {
+ return SkString("bezier_quad_effects");
+ }
+
+ virtual SkISize onISize() SK_OVERRIDE {
+ return make_isize(800, 800);
+ }
+
+ virtual uint32_t onGetFlags() const SK_OVERRIDE {
+ // This is a GPU-specific GM.
+ return kGPUOnly_Flag;
+ }
+
+
+ virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+ SkDevice* device = canvas->getTopDevice();
+ GrRenderTarget* rt = device->accessRenderTarget();
+ if (NULL == rt) {
+ return;
+ }
+ GrContext* context = rt->getContext();
+ if (NULL == context) {
+ return;
+ }
+
+ struct Vertex {
+ SkPoint fPosition;
+ float fUV[4]; // The last two values are ignored. The effect expects a vec4f.
+ };
+
+ static const int kNumQuads = 5;
+ SkMWCRandom rand;
+
+ int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumQuads*3)));
+ int numRows = SkScalarCeilToInt(SkIntToScalar(kNumQuads*3) / numCols);
+ SkScalar w = SkIntToScalar(rt->width()) / numCols;
+ SkScalar h = SkIntToScalar(rt->height()) / numRows;
+ int row = 0;
+ int col = 0;
+
+ for (int i = 0; i < kNumQuads; ++i) {
+ SkPoint baseControlPts[] = {
+ {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
+ {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
+ {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
+ };
+ for(int edgeType = kFillAA_GrBezierEdgeType; edgeType < 3; ++edgeType) {
+ SkScalar x = SkScalarMul(col, w);
+ SkScalar y = SkScalarMul(row, h);
+ SkPoint controlPts[] = {
+ {x + baseControlPts[0].fX, y + baseControlPts[0].fY},
+ {x + baseControlPts[1].fX, y + baseControlPts[1].fY},
+ {x + baseControlPts[2].fX, y + baseControlPts[2].fY}
+ };
+ SkPoint chopped[5];
+ int cnt = SkChopQuadAtMaxCurvature(controlPts, chopped);
+
+ SkPaint ctrlPtPaint;
+ ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
+ for (int i = 0; i < 3; ++i) {
+ canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
+ }
+
+ SkPaint polyPaint;
+ polyPaint.setColor(0xffA0A0A0);
+ polyPaint.setStrokeWidth(0);
+ polyPaint.setStyle(SkPaint::kStroke_Style);
+ canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
+
+ SkPaint choppedPtPaint;
+ choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
+
+ for (int c = 0; c < cnt; ++c) {
+ SkPoint* pts = chopped + 2 * c;
+
+ for (int i = 0; i < 3; ++i) {
+ canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
+ }
+
+ SkRect bounds;
+ bounds.set(pts, 3);
+
+ SkPaint boundsPaint;
+ boundsPaint.setColor(0xff808080);
+ boundsPaint.setStrokeWidth(0);
+ boundsPaint.setStyle(SkPaint::kStroke_Style);
+ canvas->drawRect(bounds, boundsPaint);
+
+ Vertex verts[4];
+ verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop,
+ bounds.fRight, bounds.fBottom,
+ sizeof(Vertex));
+
+ GrPathUtils::QuadUVMatrix DevToUV(pts);
+ DevToUV.apply<4, sizeof(Vertex), sizeof(GrPoint)>(verts);
+
+ GrTestTarget tt;
+ context->getTestTarget(&tt);
+ if (NULL == tt.target()) {
+ continue;
+ }
+ GrDrawState* drawState = tt.target()->drawState();
+ drawState->setVertexAttribs<kAttribs>(2);
+ SkAutoTUnref<GrEffectRef> effect(GrQuadEffect::Create(
+ GrBezierEdgeType(edgeType), *tt.target()->caps()));
+ if (!effect) {
+ continue;
+ }
+ drawState->addCoverageEffect(effect, 1);
+ drawState->setRenderTarget(rt);
+ drawState->setColor(0xff000000);
+
+ tt.target()->setVertexSourceToArray(verts, 4);
+ tt.target()->setIndexSourceToBuffer(context->getQuadIndexBuffer());
+ tt.target()->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 4, 6);
+ }
+ ++col;
+ if (numCols == col) {
+ col = 0;
+ ++row;
+ }
+ }
+ }
+ }
+
+private:
+ typedef GM INHERITED;
+};
-DEF_GM( return SkNEW(BezierEffects); )
+DEF_GM( return SkNEW(BezierCubicEffects); )
+DEF_GM( return SkNEW(BezierConicEffects); )
+DEF_GM( return SkNEW(BezierQuadEffects); )
}
« no previous file with comments | « no previous file | src/gpu/effects/GrBezierEffect.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698