skia2/tests/DefaultPathRendererTest.cpp
Adlai Holler c95b589112 Reland "Migrate GrSurfaceContext readPixels to take direct context"
This reverts commit cf0d08e149.

Reason for revert: fix codegen

Original change's description:
> Revert "Migrate GrSurfaceContext readPixels to take direct context"
>
> This reverts commit d169e1915c.
>
> Reason for revert: broke chrome via code generator
>
> Original change's description:
> > Migrate GrSurfaceContext readPixels to take direct context
> >
> > After this lands we'll proceed up the stack and add the direct
> > context requirement to the public API and SkImage.
> >
> > Bug: skia:104662
> > Change-Id: I4b2d779a7fcd65eec68e631757821ac8e136ddba
> > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/309044
> > Commit-Queue: Adlai Holler <adlai@google.com>
> > Reviewed-by: Robert Phillips <robertphillips@google.com>
>
> TBR=robertphillips@google.com,adlai@google.com
>
> Change-Id: I6126f2dca4bc902c903512ac486e22841cc472e5
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: skia:104662
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/309281
> Reviewed-by: Adlai Holler <adlai@google.com>
> Commit-Queue: Adlai Holler <adlai@google.com>

TBR=robertphillips@google.com,adlai@google.com


Bug: skia:104662
Change-Id: If899edab54d031a3619a4bbab90d13738679c037
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/309319
Commit-Queue: Adlai Holler <adlai@google.com>
Reviewed-by: Adlai Holler <adlai@google.com>
2020-08-12 19:24:02 +00:00

139 lines
4.7 KiB
C++

/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkBitmap.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPath.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkStrokeRec.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTypes.h"
#include "include/gpu/GrBackendSurface.h"
#include "include/gpu/GrContextOptions.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/GrTypes.h"
#include "include/private/GrTypesPriv.h"
#include "include/private/SkColorData.h"
#include "src/gpu/GrCaps.h"
#include "src/gpu/GrContextPriv.h"
#include "src/gpu/GrFragmentProcessor.h"
#include "src/gpu/GrImageInfo.h"
#include "src/gpu/GrPaint.h"
#include "src/gpu/GrRenderTargetContext.h"
#include "src/gpu/GrStyle.h"
#include "src/gpu/effects/generated/GrConstColorProcessor.h"
#include "tests/Test.h"
#include "tools/gpu/GrContextFactory.h"
#include <utility>
static void only_allow_default(GrContextOptions* options) {
options->fGpuPathRenderers = GpuPathRenderers::kNone;
}
static SkBitmap read_back(GrDirectContext* dContext, GrRenderTargetContext* rtc,
int width, int height) {
SkImageInfo dstII = SkImageInfo::MakeN32Premul(width, height);
SkBitmap bm;
bm.allocPixels(dstII);
rtc->readPixels(dContext, dstII, bm.getAddr(0, 0), bm.rowBytes(), {0, 0});
return bm;
}
static SkPath make_path(const SkRect& outer, int inset, SkPathFillType fill) {
SkPath p;
p.addRect(outer, SkPathDirection::kCW);
p.addRect(outer.makeInset(inset, inset), SkPathDirection::kCCW);
p.setFillType(fill);
return p;
}
static const int kBigSize = 64; // This should be a power of 2
static const int kPad = 3;
// From crbug.com/769898:
// create an approx fit render target context that will have extra space (i.e., npot)
// draw an inverse wound concave path into it - forcing use of the stencil-using path renderer
// throw the RTC away so the backing GrSurface/GrStencilBuffer can be reused
// create a new render target context that will reuse the prior GrSurface
// draw a normally wound concave path that touches outside of the approx fit RTC's content rect
//
// When the bug manifests the GrDefaultPathRenderer/GrMSAAPathRenderer is/was leaving the stencil
// buffer outside of the first content rect in a bad state and the second draw would be incorrect.
static void run_test(GrDirectContext* dContext, skiatest::Reporter* reporter) {
SkPath invPath = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize),
kBigSize/2-1, SkPathFillType::kInverseWinding);
SkPath path = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize),
kPad, SkPathFillType::kWinding);
GrStyle style(SkStrokeRec::kFill_InitStyle);
{
auto rtc = GrRenderTargetContext::Make(
dContext, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox,
{kBigSize/2 + 1, kBigSize/2 + 1});
rtc->clear(SK_PMColor4fBLACK);
GrPaint paint;
const SkPMColor4f color = { 1.0f, 0.0f, 0.0f, 1.0f };
auto fp = GrConstColorProcessor::Make(color);
paint.setColorFragmentProcessor(std::move(fp));
rtc->drawPath(nullptr, std::move(paint), GrAA::kNo,
SkMatrix::I(), invPath, style);
rtc->flush(SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(), nullptr);
}
{
auto rtc = GrRenderTargetContext::Make(
dContext, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, {kBigSize, kBigSize});
rtc->clear(SK_PMColor4fBLACK);
GrPaint paint;
const SkPMColor4f color = { 0.0f, 1.0f, 0.0f, 1.0f };
auto fp = GrConstColorProcessor::Make(color);
paint.setColorFragmentProcessor(std::move(fp));
rtc->drawPath(nullptr, std::move(paint), GrAA::kNo,
SkMatrix::I(), path, style);
SkBitmap bm = read_back(dContext, rtc.get(), kBigSize, kBigSize);
bool correct = true;
for (int y = kBigSize/2+1; y < kBigSize-kPad-1 && correct; ++y) {
for (int x = kPad+1; x < kBigSize-kPad-1 && correct; ++x) {
correct = bm.getColor(x, y) == SK_ColorBLACK;
REPORTER_ASSERT(reporter, correct);
}
}
}
}
DEF_GPUTEST_FOR_CONTEXTS(GrDefaultPathRendererTest,
sk_gpu_test::GrContextFactory::IsRenderingContext,
reporter, ctxInfo, only_allow_default) {
auto ctx = ctxInfo.directContext();
run_test(ctx, reporter);
}