Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright 2017 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 "Test.h"
      9 #include "SkBitmap.h"
     10 
     11 #if SK_SUPPORT_GPU
     12 #include "GrClip.h"
     13 #include "GrRenderTargetContext.h"
     14 #include "GrStyle.h"
     15 #include "GrTypesPriv.h"
     16 
     17 #include "effects/GrConstColorProcessor.h"
     18 
     19 static void allow_default_and_msaa(GrContextOptions* options) {
     20     options->fGpuPathRenderers = GpuPathRenderers::kMSAA;
     21 }
     22 
     23 static void only_allow_default(GrContextOptions* options) {
     24     options->fGpuPathRenderers = GpuPathRenderers::kNone;
     25 }
     26 
     27 static SkBitmap read_back(GrRenderTargetContext* rtc, int width, int height) {
     28 
     29     SkImageInfo dstII = SkImageInfo::MakeN32Premul(width, height);
     30 
     31     SkBitmap bm;
     32     bm.allocPixels(dstII);
     33 
     34     rtc->readPixels(dstII, bm.getAddr(0, 0), bm.rowBytes(), 0, 0, 0);
     35 
     36     return bm;
     37 }
     38 
     39 static SkPath make_path(const SkRect& outer, int inset, SkPath::FillType fill) {
     40     SkPath p;
     41 
     42     p.addRect(outer, SkPath::kCW_Direction);
     43     p.addRect(outer.makeInset(inset, inset), SkPath::kCCW_Direction);
     44     p.setFillType(fill);
     45     return p;
     46 }
     47 
     48 
     49 static const int kBigSize = 64; // This should be a power of 2
     50 static const int kPad = 3;
     51 
     52 // From crbug.com/769898:
     53 //   create an approx fit render target context that will have extra space (i.e., npot)
     54 //   draw an inverse wound concave path into it - forcing use of the stencil-using path renderer
     55 //   throw the RTC away so the backing GrSurface/GrStencilBuffer can be reused
     56 //   create a new render target context that will reuse the prior GrSurface
     57 //   draw a normally wound concave path that touches outside of the approx fit RTC's content rect
     58 //
     59 // When the bug manifests the GrDefaultPathRenderer/GrMSAAPathRenderer is/was leaving the stencil
     60 // buffer outside of the first content rect in a bad state and the second draw would be incorrect.
     61 
     62 static void run_test(GrContext* ctx, skiatest::Reporter* reporter) {
     63     SkPath invPath = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize),
     64                                kBigSize/2-1, SkPath::kInverseWinding_FillType);
     65     SkPath path = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize),
     66                             kPad, SkPath::kWinding_FillType);
     67 
     68     GrStyle style(SkStrokeRec::kFill_InitStyle);
     69 
     70     {
     71         auto rtc =  ctx->makeDeferredRenderTargetContext(SkBackingFit::kApprox,
     72                                                          kBigSize/2+1, kBigSize/2+1,
     73                                                          kRGBA_8888_GrPixelConfig, nullptr);
     74 
     75         rtc->clear(nullptr, GrColorPackRGBA(0x0, 0x0, 0x0, 0xFF),
     76                    GrRenderTargetContext::CanClearFullscreen::kYes);
     77 
     78         GrPaint paint;
     79 
     80         const GrColor4f color = { 1.0f, 0.0f, 0.0f, 1.0f };
     81         auto fp = GrConstColorProcessor::Make(color, GrConstColorProcessor::InputMode::kIgnore);
     82         paint.addColorFragmentProcessor(std::move(fp));
     83 
     84         rtc->drawPath(GrNoClip(), std::move(paint), GrAA::kNo,
     85                       SkMatrix::I(), invPath, style);
     86 
     87         rtc->prepareForExternalIO(0, nullptr);
     88     }
     89 
     90     {
     91         auto rtc = ctx->makeDeferredRenderTargetContext(SkBackingFit::kExact, kBigSize, kBigSize,
     92                                                         kRGBA_8888_GrPixelConfig, nullptr);
     93 
     94         rtc->clear(nullptr, GrColorPackRGBA(0x0, 0x0, 0x0, 0xFF),
     95                    GrRenderTargetContext::CanClearFullscreen::kYes);
     96 
     97         GrPaint paint;
     98 
     99         const GrColor4f color = { 0.0f, 1.0f, 0.0f, 1.0f };
    100         auto fp = GrConstColorProcessor::Make(color, GrConstColorProcessor::InputMode::kIgnore);
    101         paint.addColorFragmentProcessor(std::move(fp));
    102 
    103         rtc->drawPath(GrNoClip(), std::move(paint), GrAA::kNo,
    104                       SkMatrix::I(), path, style);
    105 
    106         SkBitmap bm = read_back(rtc.get(), kBigSize, kBigSize);
    107 
    108         bool correct = true;
    109         for (int y = kBigSize/2+1; y < kBigSize-kPad-1 && correct; ++y) {
    110             for (int x = kPad+1; x < kBigSize-kPad-1 && correct; ++x) {
    111                 correct = bm.getColor(x, y) == SK_ColorBLACK;
    112                 REPORTER_ASSERT(reporter, correct);
    113             }
    114         }
    115     }
    116 
    117 }
    118 
    119 DEF_GPUTEST_FOR_CONTEXTS(GrDefaultPathRendererTest,
    120                          sk_gpu_test::GrContextFactory::IsRenderingContext,
    121                          reporter, ctxInfo, only_allow_default) {
    122     GrContext* ctx = ctxInfo.grContext();
    123 
    124     run_test(ctx, reporter);
    125 }
    126 
    127 DEF_GPUTEST_FOR_CONTEXTS(GrMSAAPathRendererTest,
    128                          sk_gpu_test::GrContextFactory::IsRenderingContext,
    129                          reporter, ctxInfo, allow_default_and_msaa) {
    130     GrContext* ctx = ctxInfo.grContext();
    131 
    132     if (!ctx->caps()->sampleShadingSupport()) {   // The MSAAPathRenderer requires this
    133         return;
    134     }
    135 
    136     run_test(ctx, reporter);
    137 }
    138 
    139 #endif
    140