Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2015 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "gm.h"
      9 #include "sk_tool_utils.h"
     10 #if SK_SUPPORT_GPU
     11 #include "GrContext.h"
     12 #include "GrRenderTargetContextPriv.h"
     13 #include "SkRRect.h"
     14 #include "effects/GrRRectEffect.h"
     15 #include "ops/GrDrawOp.h"
     16 #include "ops/GrRectOpFactory.h"
     17 
     18 namespace skiagm {
     19 
     20 ///////////////////////////////////////////////////////////////////////////////
     21 
     22 class BigRRectAAEffectGM : public GM {
     23 public:
     24     BigRRectAAEffectGM(const SkRRect& rrect, const char* name)
     25         : fRRect(rrect)
     26         , fName(name) {
     27         this->setBGColor(sk_tool_utils::color_to_565(SK_ColorBLUE));
     28         // Each test case draws the rrect with gaps around it.
     29         fTestWidth = SkScalarCeilToInt(rrect.width()) + 2 * kGap;
     30         fTestHeight = SkScalarCeilToInt(rrect.height()) + 2 * kGap;
     31 
     32         // Add a pad between test cases.
     33         fTestOffsetX = fTestWidth + kPad;
     34         fTestOffsetY = fTestHeight + kPad;
     35 
     36         // We draw two tests in x (fill and inv-fill) and pad around
     37         // all four sides of the image.
     38         fWidth = 2 * fTestOffsetX + kPad;
     39         fHeight = fTestOffsetY + kPad;
     40     }
     41 
     42 protected:
     43     SkString onShortName() override {
     44         SkString name;
     45         name.printf("big_rrect_%s_aa_effect", fName);
     46         return name;
     47     }
     48 
     49     SkISize onISize() override { return SkISize::Make(fWidth, fHeight); }
     50 
     51     void onDraw(SkCanvas* canvas) override {
     52         GrRenderTargetContext* renderTargetContext =
     53             canvas->internal_private_accessTopLayerRenderTargetContext();
     54         if (!renderTargetContext) {
     55             skiagm::GM::DrawGpuOnlyMessage(canvas);
     56             return;
     57         }
     58 
     59         SkPaint paint;
     60 
     61         int y = kPad;
     62         int x = kPad;
     63         constexpr GrPrimitiveEdgeType kEdgeTypes[] = {
     64             kFillAA_GrProcessorEdgeType,
     65             kInverseFillAA_GrProcessorEdgeType,
     66         };
     67         SkRect testBounds = SkRect::MakeIWH(fTestWidth, fTestHeight);
     68         for (size_t et = 0; et < SK_ARRAY_COUNT(kEdgeTypes); ++et) {
     69             GrPrimitiveEdgeType edgeType = kEdgeTypes[et];
     70             canvas->save();
     71                 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
     72 
     73                 // Draw a background for the test case
     74                 SkPaint paint;
     75                 paint.setColor(SK_ColorWHITE);
     76                 canvas->drawRect(testBounds, paint);
     77 
     78                 SkRRect rrect = fRRect;
     79                 rrect.offset(SkIntToScalar(x + kGap), SkIntToScalar(y + kGap));
     80                 sk_sp<GrFragmentProcessor> fp(GrRRectEffect::Make(edgeType, rrect));
     81                 SkASSERT(fp);
     82                 if (fp) {
     83                     GrPaint grPaint;
     84                     grPaint.setColor4f(GrColor4f(0, 0, 0, 1.f));
     85                     grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
     86                     grPaint.addCoverageFragmentProcessor(std::move(fp));
     87 
     88                     SkRect bounds = testBounds;
     89                     bounds.offset(SkIntToScalar(x), SkIntToScalar(y));
     90 
     91                     renderTargetContext->priv().testingOnly_addDrawOp(
     92                             GrRectOpFactory::MakeNonAAFill(std::move(grPaint), SkMatrix::I(),
     93                                                            bounds, GrAAType::kNone));
     94                 }
     95             canvas->restore();
     96             x = x + fTestOffsetX;
     97         }
     98     }
     99 
    100 private:
    101     // pad between test cases
    102     static constexpr int kPad = 7;
    103     // gap between rect for each case that is rendered and exterior of rrect
    104     static constexpr int kGap = 3;
    105 
    106     SkRRect fRRect;
    107     int fWidth;
    108     int fHeight;
    109     int fTestWidth;
    110     int fTestHeight;
    111     int fTestOffsetX;
    112     int fTestOffsetY;
    113     const char* fName;
    114     typedef GM INHERITED;
    115 };
    116 
    117 ///////////////////////////////////////////////////////////////////////////////
    118 // This value is motivated by bug chromium:477684. It has to be large to cause overflow in
    119 // the shader
    120 constexpr int kSize = 700;
    121 
    122 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRect(SkRect::MakeIWH(kSize, kSize)), "rect"); )
    123 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize, kSize)), "circle"); )
    124 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize - 1, kSize - 10)), "ellipse"); )
    125 // The next two have small linear segments between the corners
    126 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRectXY(SkRect::MakeIWH(kSize - 1, kSize - 10), kSize/2.f - 10.f, kSize/2.f - 10.f), "circular_corner"); )
    127 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRectXY(SkRect::MakeIWH(kSize - 1, kSize - 10), kSize/2.f - 10.f, kSize/2.f - 15.f), "elliptical_corner"); )
    128 
    129 }
    130 #endif
    131