--- /dev/null
+/*
+ * Copyright 2015 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"
+#if SK_SUPPORT_GPU
+#include "GrTest.h"
+#include "effects/GrRRectEffect.h"
+#include "SkDevice.h"
+#include "SkRRect.h"
+
+namespace skiagm {
+
+///////////////////////////////////////////////////////////////////////////////
+
+class BigRRectAAEffectGM : public GM {
+public:
+ BigRRectAAEffectGM() {
+ this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
+ this->setUpRRects();
+ }
+
+protected:
+ SkString onShortName() override {
+ return SkString("big_rrect_aa_effect");
+ }
+
+ SkISize onISize() override { return SkISize::Make(kImageWidth, kImageHeight); }
+
+ void onDraw(SkCanvas* canvas) override {
+ GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
+ GrContext* context = rt ? rt->getContext() : NULL;
+ if (!context) {
+ this->drawGpuOnlyMessage(canvas);
+ return;
+ }
+
+ SkPaint paint;
+
+#ifdef SK_DEBUG
+ static const SkRect kMaxRRectBound = SkRect::MakeWH(SkIntToScalar(kMaxSize),
+ SkIntToScalar(kMaxSize));
+ static const SkRect kMaxImageBound = SkRect::MakeWH(SkIntToScalar(kImageWidth),
+ SkIntToScalar(kImageHeight));
+#endif
+
+ int y = kPad;
+ int x = kPad;
+ static const GrPrimitiveEdgeType kEdgeTypes[] = {
+ kFillAA_GrProcessorEdgeType,
+ kInverseFillAA_GrProcessorEdgeType,
+ };
+ for (size_t et = 0; et < SK_ARRAY_COUNT(kEdgeTypes); ++et) {
+ GrPrimitiveEdgeType edgeType = kEdgeTypes[et];
+ for (int curRRect = 0; curRRect < fRRects.count(); ++curRRect) {
+#ifdef SK_DEBUG
+ SkASSERT(kMaxRRectBound.contains(fRRects[curRRect].getBounds()));
+ SkRect imageSpaceBounds = fRRects[curRRect].getBounds();
+ imageSpaceBounds.offset(SkIntToScalar(x), SkIntToScalar(y));
+ SkASSERT(kMaxImageBound.contains(imageSpaceBounds));
+#endif
+ canvas->save();
+ canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
+ GrTestTarget tt;
+ context->getTestTarget(&tt);
+ if (NULL == tt.target()) {
+ SkDEBUGFAIL("Couldn't get Gr test target.");
+ return;
+ }
+ GrPipelineBuilder pipelineBuilder;
+
+ SkRRect rrect = fRRects[curRRect];
+ rrect.offset(SkIntToScalar(x), SkIntToScalar(y));
+ SkAutoTUnref<GrFragmentProcessor> fp(GrRRectEffect::Create(edgeType, rrect));
+ SkASSERT(fp);
+ if (fp) {
+ pipelineBuilder.addCoverageProcessor(fp);
+ pipelineBuilder.setRenderTarget(rt);
+
+ SkRect bounds = SkRect::MakeWH(SkIntToScalar(kMaxSize),
+ SkIntToScalar(kMaxSize));
+ bounds.outset(2.f, 2.f);
+ bounds.offset(SkIntToScalar(x), SkIntToScalar(y));
+
+ tt.target()->drawSimpleRect(pipelineBuilder,
+ 0xff000000,
+ SkMatrix::I(),
+ bounds);
+ }
+ canvas->restore();
+ x = x + kDrawOffset;
+ if (x + kMaxSize> kImageWidth) {
+ x = kPad;
+ y += kDrawOffset;
+ }
+ }
+ }
+ }
+
+ void setUpRRects() {
+ SkScalar maxSize = SkIntToScalar(kMaxSize);
+ fRRects.push()->setRect(SkRect::MakeWH(maxSize, maxSize));
+ fRRects.push()->setOval(SkRect::MakeWH(maxSize, maxSize));
+ fRRects.push()->setOval(SkRect::MakeWH(maxSize - 1.f, maxSize - 10.f));
+ fRRects.push()->setRectXY(SkRect::MakeWH(maxSize - 1.f, maxSize - 10.f),
+ maxSize/2.f - 10.f, maxSize/2.f - 10.f);
+ fRRects.push()->setRectXY(SkRect::MakeWH(maxSize - 1.f, maxSize - 10),
+ maxSize/2.f - 10.f, maxSize/2.f - 20.f);
+ }
+
+private:
+ static const int kPad = 5;
+ static const int kMaxSize = 300;
+ static const int kDrawOffset = kMaxSize + kPad;
+
+ static const int kImageWidth = 4 * kDrawOffset + kPad;
+ static const int kImageHeight = 3 * kDrawOffset + kPad;
+
+
+ SkTDArray<SkRRect> fRRects;
+ typedef GM INHERITED;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+DEF_GM( return new BigRRectAAEffectGM (); )
+
+}
+#endif
// fragments near the other three edges will get the correct AA. Fragments in the interior of
// the rrect will have a (0,0) vector at all four corners. So long as the radii > 0.5 they will
// correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas.
+ //
// The code below is a simplified version of the above that performs maxs on the vector
// components before computing distances and alpha values so that only one distance computation
// need be computed to determine the min alpha.
fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
+ // The uniforms with the inv squared radii are highp to prevent underflow.
switch (erre.getRRect().getType()) {
case SkRRect::kSimple_Type: {
const char *invRadiiXYSqdName;
fInvRadiiSqdUniform = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
- kVec2f_GrSLType, kDefault_GrSLPrecision,
+ kVec2f_GrSLType, kHigh_GrSLPrecision,
"invRadiiXY",
&invRadiiXYSqdName);
fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
case SkRRect::kNinePatch_Type: {
const char *invRadiiLTRBSqdName;
fInvRadiiSqdUniform = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
- kVec4f_GrSLType, kDefault_GrSLPrecision,
+ kVec4f_GrSLType, kHigh_GrSLPrecision,
"invRadiiLTRB",
&invRadiiLTRBSqdName);
fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");