skia2/tests/DefaultPathRendererTest.cpp

140 lines
4.8 KiB
C++
Raw Normal View History

/*
* 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/GrContext.h"
#include "include/gpu/GrContextOptions.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(GrRenderTargetContext* rtc, int width, int height) {
SkImageInfo dstII = SkImageInfo::MakeN32Premul(width, height);
SkBitmap bm;
bm.allocPixels(dstII);
rtc->readPixels(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(GrContext* ctx, 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(
ctx, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox,
{kBigSize/2 + 1, kBigSize/2 + 1});
Reland "Simplify GrRTC::clean APIs" This reverts commit 4730f29993783fc9e96e45098d7e708fe08f1f26. Reason for revert: Fix WIP Original change's description: > Revert "Simplify GrRTC::clean APIs" > > This reverts commit 6cbd7c2e57af9c499f7e99feb215b890fdd3a10a. > > Reason for revert: mac/generated files failures > > Original change's description: > > Simplify GrRTC::clean APIs > > > > The CanClearFullscreen enum type is removed. Most usages of clear() had > > kYes because a null scissor rect was provided, or had kNo because the > > scissor was really critical to the behavior. A few places did provide a > > scissor and kYes (e.g. for initializing the target). > > > > To simplify this, the public GrRTC has two variants of clear(). One with > > only a color (for fullscreen clears), and one with a rect for partial > > clears. The private API also adds a clearAtLeast() function that replaces > > the several cases where we'd have a scissor but could expand to fullscreen. > > > > I find the current control flow in internalClear() to be hard to > > follow (albeit I was the one to make it that way...), but later CLs > > will improve it. > > > > Bug: skia:10205 > > Change-Id: I87cf8d688c58fbe58ee854fbc4ffe22482d969c6 > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/290256 > > Reviewed-by: Brian Salomon <bsalomon@google.com> > > Commit-Queue: Michael Ludwig <michaelludwig@google.com> > > TBR=bsalomon@google.com,csmartdalton@google.com,michaelludwig@google.com > > Change-Id: I7131df6f5323f4f9c120cbcfd9bc57e627e2eb65 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: skia:10205 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/291842 > Reviewed-by: Michael Ludwig <michaelludwig@google.com> > Commit-Queue: Michael Ludwig <michaelludwig@google.com> # Not skipping CQ checks because this is a reland. Bug: skia:10205 Change-Id: Id5db153d7c2500279cca8478818b66f67a53e143 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/291844 Reviewed-by: Michael Ludwig <michaelludwig@google.com> Reviewed-by: Brian Salomon <bsalomon@google.com> Commit-Queue: Michael Ludwig <michaelludwig@google.com>
2020-05-26 20:57:38 +00:00
rtc->clear(SK_PMColor4fBLACK);
GrPaint paint;
const SkPMColor4f color = { 1.0f, 0.0f, 0.0f, 1.0f };
auto fp = GrConstColorProcessor::Make(/*inputFP=*/nullptr, color,
GrConstColorProcessor::InputMode::kIgnore);
paint.addColorFragmentProcessor(std::move(fp));
Reland "GrClips provided as pointers to GrRTC" This reverts commit 074414fed53efcab7f33b06454958707419e37d8. Reason for revert: updated to guard against nullptr before calling quickContains(rrect). Original change's description: > Revert "GrClips provided as pointers to GrRTC" > > This reverts commit 226b689471a0fbb7400bc166032458278957541b. > > Reason for revert: Breaks Android roller > > Original change's description: > > GrClips provided as pointers to GrRTC > > > > A null clip represents no high-level clipping is necessary (the implicit > > clip to the render target's logical dimensions is fine). > > > > This also removes GrNoClip and GrFixedClip::Disabled() since they are > > replaced with just nullptr. > > > > By allowing nullptr to represent no intended clipping, it makes it easier > > to require GrClip and GrAppliedClip objects to know about the dimensions > > of the device. If we required a non-null clip object to represent no > > clipping, we'd have to have an instance for each device based on its > > size and that just became cumbersome. > > > > Bug: skia:10205 > > Change-Id: Ie30cc71820b92d99356d393a4c98c8677082e761 > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/290539 > > Commit-Queue: Michael Ludwig <michaelludwig@google.com> > > Reviewed-by: Brian Salomon <bsalomon@google.com> > > TBR=bsalomon@google.com,csmartdalton@google.com,michaelludwig@google.com > > Change-Id: I42c4828bcf016ee3d30d5c20b771be96e125817b > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: skia:10205 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/292856 > Reviewed-by: Weston Tracey <westont@google.com> > Commit-Queue: Weston Tracey <westont@google.com> TBR=bsalomon@google.com,csmartdalton@google.com,michaelludwig@google.com,westont@google.com # Not skipping CQ checks because this is a reland. Bug: skia:10205 Change-Id: I5715a4de3b7c8847b73020dc4937d3816d879803 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/292876 Reviewed-by: Michael Ludwig <michaelludwig@google.com> Commit-Queue: Michael Ludwig <michaelludwig@google.com>
2020-05-29 13:54:07 +00:00
rtc->drawPath(nullptr, std::move(paint), GrAA::kNo,
SkMatrix::I(), invPath, style);
rtc->flush(SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(), nullptr);
}
{
auto rtc = GrRenderTargetContext::Make(
ctx, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, {kBigSize, kBigSize});
Reland "Simplify GrRTC::clean APIs" This reverts commit 4730f29993783fc9e96e45098d7e708fe08f1f26. Reason for revert: Fix WIP Original change's description: > Revert "Simplify GrRTC::clean APIs" > > This reverts commit 6cbd7c2e57af9c499f7e99feb215b890fdd3a10a. > > Reason for revert: mac/generated files failures > > Original change's description: > > Simplify GrRTC::clean APIs > > > > The CanClearFullscreen enum type is removed. Most usages of clear() had > > kYes because a null scissor rect was provided, or had kNo because the > > scissor was really critical to the behavior. A few places did provide a > > scissor and kYes (e.g. for initializing the target). > > > > To simplify this, the public GrRTC has two variants of clear(). One with > > only a color (for fullscreen clears), and one with a rect for partial > > clears. The private API also adds a clearAtLeast() function that replaces > > the several cases where we'd have a scissor but could expand to fullscreen. > > > > I find the current control flow in internalClear() to be hard to > > follow (albeit I was the one to make it that way...), but later CLs > > will improve it. > > > > Bug: skia:10205 > > Change-Id: I87cf8d688c58fbe58ee854fbc4ffe22482d969c6 > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/290256 > > Reviewed-by: Brian Salomon <bsalomon@google.com> > > Commit-Queue: Michael Ludwig <michaelludwig@google.com> > > TBR=bsalomon@google.com,csmartdalton@google.com,michaelludwig@google.com > > Change-Id: I7131df6f5323f4f9c120cbcfd9bc57e627e2eb65 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: skia:10205 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/291842 > Reviewed-by: Michael Ludwig <michaelludwig@google.com> > Commit-Queue: Michael Ludwig <michaelludwig@google.com> # Not skipping CQ checks because this is a reland. Bug: skia:10205 Change-Id: Id5db153d7c2500279cca8478818b66f67a53e143 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/291844 Reviewed-by: Michael Ludwig <michaelludwig@google.com> Reviewed-by: Brian Salomon <bsalomon@google.com> Commit-Queue: Michael Ludwig <michaelludwig@google.com>
2020-05-26 20:57:38 +00:00
rtc->clear(SK_PMColor4fBLACK);
GrPaint paint;
const SkPMColor4f color = { 0.0f, 1.0f, 0.0f, 1.0f };
auto fp = GrConstColorProcessor::Make(/*inputFP=*/nullptr, color,
GrConstColorProcessor::InputMode::kIgnore);
paint.addColorFragmentProcessor(std::move(fp));
Reland "GrClips provided as pointers to GrRTC" This reverts commit 074414fed53efcab7f33b06454958707419e37d8. Reason for revert: updated to guard against nullptr before calling quickContains(rrect). Original change's description: > Revert "GrClips provided as pointers to GrRTC" > > This reverts commit 226b689471a0fbb7400bc166032458278957541b. > > Reason for revert: Breaks Android roller > > Original change's description: > > GrClips provided as pointers to GrRTC > > > > A null clip represents no high-level clipping is necessary (the implicit > > clip to the render target's logical dimensions is fine). > > > > This also removes GrNoClip and GrFixedClip::Disabled() since they are > > replaced with just nullptr. > > > > By allowing nullptr to represent no intended clipping, it makes it easier > > to require GrClip and GrAppliedClip objects to know about the dimensions > > of the device. If we required a non-null clip object to represent no > > clipping, we'd have to have an instance for each device based on its > > size and that just became cumbersome. > > > > Bug: skia:10205 > > Change-Id: Ie30cc71820b92d99356d393a4c98c8677082e761 > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/290539 > > Commit-Queue: Michael Ludwig <michaelludwig@google.com> > > Reviewed-by: Brian Salomon <bsalomon@google.com> > > TBR=bsalomon@google.com,csmartdalton@google.com,michaelludwig@google.com > > Change-Id: I42c4828bcf016ee3d30d5c20b771be96e125817b > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: skia:10205 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/292856 > Reviewed-by: Weston Tracey <westont@google.com> > Commit-Queue: Weston Tracey <westont@google.com> TBR=bsalomon@google.com,csmartdalton@google.com,michaelludwig@google.com,westont@google.com # Not skipping CQ checks because this is a reland. Bug: skia:10205 Change-Id: I5715a4de3b7c8847b73020dc4937d3816d879803 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/292876 Reviewed-by: Michael Ludwig <michaelludwig@google.com> Commit-Queue: Michael Ludwig <michaelludwig@google.com>
2020-05-29 13:54:07 +00:00
rtc->drawPath(nullptr, std::move(paint), GrAA::kNo,
SkMatrix::I(), path, style);
SkBitmap bm = read_back(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) {
GrContext* ctx = ctxInfo.grContext();
run_test(ctx, reporter);
}