* 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 "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
#include "include/pathops/SkPathOps.h"
#include "src/core/SkMakeUnique.h"
#include "src/gpu/GrCaps.h"
#include "src/gpu/GrClip.h"
#include "src/gpu/GrProxyProvider.h"
#include "src/gpu/ccpr/GrCCClipProcessor.h"
#include "src/gpu/ccpr/GrCCDrawPathsOp.h"
#include "src/gpu/ccpr/GrCCPathCache.h"
using PathInstance = GrCCPathProcessor::Instance;
bool GrCoverageCountingPathRenderer::IsSupported(const GrCaps& caps, CoverageType* coverageType) {
const GrShaderCaps& shaderCaps = *caps.shaderCaps();
GrBackendFormat defaultA8Format = caps.getDefaultBackendFormat(GrColorType::kAlpha_8,
GrRenderable::kYes);
if (caps.driverBlacklistCCPR() || !shaderCaps.integerSupport() ||
!caps.instanceAttribSupport() || !shaderCaps.floatIs32Bits() ||
GrCaps::kNone_MapFlags == caps.mapBufferFlags() ||
!defaultA8Format.isValid() ||
!caps.halfFloatVertexAttributeSupport()) {
return false;
}
GrBackendFormat defaultAHalfFormat = caps.getDefaultBackendFormat(GrColorType::kAlpha_F16,
GrRenderable::kYes);
if (caps.allowCoverageCounting() &&
defaultAHalfFormat.isValid()) {
if (coverageType) {
*coverageType = CoverageType::kFP16_CoverageCount;
}
return true;
}
if (!caps.driverBlacklistMSAACCPR() &&
caps.internalMultisampleCount(defaultA8Format) > 1 &&
caps.sampleLocationsSupport() &&
shaderCaps.sampleVariablesStencilSupport()) {
if (coverageType) {
*coverageType = CoverageType::kA8_Multisample;
}
return true;
}
return false;
}
sk_sp<GrCoverageCountingPathRenderer> GrCoverageCountingPathRenderer::CreateIfSupported(
const GrCaps& caps, AllowCaching allowCaching, uint32_t contextUniqueID) {
CoverageType coverageType;
if (IsSupported(caps, &coverageType)) {
return sk_sp<GrCoverageCountingPathRenderer>(new GrCoverageCountingPathRenderer(
coverageType, allowCaching, contextUniqueID));
}
return nullptr;
}
GrCoverageCountingPathRenderer::GrCoverageCountingPathRenderer(
CoverageType coverageType, AllowCaching allowCaching, uint32_t contextUniqueID)
: fCoverageType(coverageType) {
if (AllowCaching::kYes == allowCaching) {
fPathCache = skstd::make_unique<GrCCPathCache>(contextUniqueID);
}
}
GrCCPerOpListPaths* GrCoverageCountingPathRenderer::lookupPendingPaths(uint32_t opListID) {
auto it = fPendingPaths.find(opListID);
if (fPendingPaths.end() == it) {
sk_sp<GrCCPerOpListPaths> paths = sk_make_sp<GrCCPerOpListPaths>();
it = fPendingPaths.insert(std::make_pair(opListID, std::move(paths))).first;
}
return it->second.get();
}
GrPathRenderer::CanDrawPath GrCoverageCountingPathRenderer::onCanDrawPath(
const CanDrawPathArgs& args) const {
const GrShape& shape = *args.fShape;
if (GrAAType::kCoverage != args.fAAType || shape.style().hasPathEffect() ||
args.fViewMatrix->hasPerspective() || shape.inverseFilled()) {
return CanDrawPath::kNo;
}
SkPath path;
shape.asPath(&path);
const SkStrokeRec& stroke = shape.style().strokeRec();
switch (stroke.getStyle()) {
case SkStrokeRec::kFill_Style: {
SkRect devBounds;
args.fViewMatrix->mapRect(&devBounds, path.getBounds());
SkIRect clippedIBounds;
devBounds.roundOut(&clippedIBounds);
if (!clippedIBounds.intersect(*args.fClipConservativeBounds)) {
return CanDrawPath::kYes;
}
int64_t numPixels = sk_64_mul(clippedIBounds.height(), clippedIBounds.width());
if (path.countVerbs() > 1000 && path.countPoints() > numPixels) {
return CanDrawPath::kNo;
}
if (numPixels > 256 * 256) {
return CanDrawPath::kAsBackup;
}
if (args.fShape->hasUnstyledKey() && path.countVerbs() > 50) {
return CanDrawPath::kAsBackup;
}
return CanDrawPath::kYes;
}
case SkStrokeRec::kStroke_Style:
if (!args.fViewMatrix->isSimilarity()) {
return CanDrawPath::kNo;
}
case SkStrokeRec::kHairline_Style: {
if (CoverageType::kFP16_CoverageCount != fCoverageType) {
return CanDrawPath::kNo;
}
float inflationRadius;
GetStrokeDevWidth(*args.fViewMatrix, stroke, &inflationRadius);
if (!(inflationRadius <= kMaxBoundsInflationFromStroke)) {
return CanDrawPath::kNo;
}
SkASSERT(!SkScalarIsNaN(inflationRadius));
if (SkPathPriv::ConicWeightCnt(path)) {
return CanDrawPath::kNo;
}
return CanDrawPath::kYes;
}
case SkStrokeRec::kStrokeAndFill_Style:
return CanDrawPath::kNo;
}
SK_ABORT("Invalid stroke style.");
}
bool GrCoverageCountingPathRenderer::onDrawPath(const DrawPathArgs& args) {
SkASSERT(!fFlushing);
SkIRect clipIBounds;
GrRenderTargetContext* rtc = args.fRenderTargetContext;
args.fClip->getConservativeBounds(rtc->width(), rtc->height(), &clipIBounds, nullptr);
auto op = GrCCDrawPathsOp::Make(args.fContext, clipIBounds, *args.fViewMatrix, *args.fShape,
std::move(args.fPaint));
this->recordOp(std::move(op), args);
return true;
}
void GrCoverageCountingPathRenderer::recordOp(std::unique_ptr<GrCCDrawPathsOp> op,
const DrawPathArgs& args) {
if (op) {
auto addToOwningPerOpListPaths = [this](GrOp* op, uint32_t opListID) {
op->cast<GrCCDrawPathsOp>()->addToOwningPerOpListPaths(
sk_ref_sp(this->lookupPendingPaths(opListID)));
};
args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op), addToOwningPerOpListPaths);
}
}
std::unique_ptr<GrFragmentProcessor> GrCoverageCountingPathRenderer::makeClipProcessor(
uint32_t opListID, const SkPath& deviceSpacePath, const SkIRect& accessRect,
const GrCaps& caps) {
SkASSERT(!fFlushing);
uint32_t key = deviceSpacePath.getGenerationID();
if (CoverageType::kA8_Multisample == fCoverageType) {
key = (key << 1) | (uint32_t)GrFillRuleForSkPath(deviceSpacePath);
}
GrCCClipPath& clipPath =
this->lookupPendingPaths(opListID)->fClipPaths[key];
if (!clipPath.isInitialized()) {
const SkRect& pathDevBounds = deviceSpacePath.getBounds();
if (SkTMax(pathDevBounds.height(), pathDevBounds.width()) > kPathCropThreshold) {
SkPath croppedPath;
int maxRTSize = caps.maxRenderTargetSize();
CropPath(deviceSpacePath, SkIRect::MakeWH(maxRTSize, maxRTSize), &croppedPath);
clipPath.init(croppedPath, accessRect, fCoverageType, caps);
} else {
clipPath.init(deviceSpacePath, accessRect, fCoverageType, caps);
}
} else {
clipPath.addAccess(accessRect);
}
auto isCoverageCount = GrCCClipProcessor::IsCoverageCount(
CoverageType::kFP16_CoverageCount == fCoverageType);
auto mustCheckBounds = GrCCClipProcessor::MustCheckBounds(
!clipPath.pathDevIBounds().contains(accessRect));
return skstd::make_unique<GrCCClipProcessor>(&clipPath, isCoverageCount, mustCheckBounds);
}
void GrCoverageCountingPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
const uint32_t* opListIDs, int numOpListIDs,
SkTArray<sk_sp<GrRenderTargetContext>>* out) {
using DoCopiesToA8Coverage = GrCCDrawPathsOp::DoCopiesToA8Coverage;
SkASSERT(!fFlushing);
SkASSERT(fFlushingPaths.empty());
SkDEBUGCODE(fFlushing = true);
if (fPathCache) {
fPathCache->doPreFlushProcessing();
}
if (fPendingPaths.empty()) {
return;
}
GrCCPerFlushResourceSpecs specs;
int maxPreferredRTSize = onFlushRP->caps()->maxPreferredRenderTargetSize();
specs.fCopyAtlasSpecs.fMaxPreferredTextureSize = SkTMin(2048, maxPreferredRTSize);
SkASSERT(0 == specs.fCopyAtlasSpecs.fMinTextureSize);
specs.fRenderedAtlasSpecs.fMaxPreferredTextureSize = maxPreferredRTSize;
specs.fRenderedAtlasSpecs.fMinTextureSize = SkTMin(512, maxPreferredRTSize);
fFlushingPaths.reserve(numOpListIDs);
for (int i = 0; i < numOpListIDs; ++i) {
auto iter = fPendingPaths.find(opListIDs[i]);
if (fPendingPaths.end() == iter) {
continue;
}
fFlushingPaths.push_back(std::move(iter->second));
fPendingPaths.erase(iter);
for (GrCCDrawPathsOp* op : fFlushingPaths.back()->fDrawOps) {
op->accountForOwnPaths(fPathCache.get(), onFlushRP, &specs);
}
for (const auto& clipsIter : fFlushingPaths.back()->fClipPaths) {
clipsIter.second.accountForOwnPath(&specs);
}
}
if (specs.isEmpty()) {
return;
}
int numCopies = specs.fNumCopiedPaths[GrCCPerFlushResourceSpecs::kFillIdx] +
specs.fNumCopiedPaths[GrCCPerFlushResourceSpecs::kStrokeIdx];
auto doCopies = DoCopiesToA8Coverage(numCopies > 100 ||
specs.fCopyAtlasSpecs.fApproxNumPixels > 256 * 256);
if (numCopies && DoCopiesToA8Coverage::kNo == doCopies) {
specs.cancelCopies();
}
auto resources = sk_make_sp<GrCCPerFlushResources>(onFlushRP, fCoverageType, specs);
if (!resources->isMapped()) {
return;
}
for (const auto& flushingPaths : fFlushingPaths) {
for (GrCCDrawPathsOp* op : flushingPaths->fDrawOps) {
op->setupResources(fPathCache.get(), onFlushRP, resources.get(), doCopies);
}
for (auto& clipsIter : flushingPaths->fClipPaths) {
clipsIter.second.renderPathInAtlas(resources.get(), onFlushRP);
}
}
if (fPathCache) {
fPathCache->purgeInvalidatedAtlasTextures(onFlushRP);
}
if (!resources->finalize(onFlushRP, out)) {
return;
}
for (auto& flushingPaths : fFlushingPaths) {
SkASSERT(!flushingPaths->fFlushResources);
flushingPaths->fFlushResources = resources;
}
}
void GrCoverageCountingPathRenderer::postFlush(GrDeferredUploadToken, const uint32_t* opListIDs,
int numOpListIDs) {
SkASSERT(fFlushing);
if (!fFlushingPaths.empty()) {
for (auto& flushingPaths : fFlushingPaths) {
flushingPaths->fFlushResources = nullptr;
}
fFlushingPaths.reset();
}
SkDEBUGCODE(fFlushing = false);
}
void GrCoverageCountingPathRenderer::purgeCacheEntriesOlderThan(
GrProxyProvider* proxyProvider, const GrStdSteadyClock::time_point& purgeTime) {
if (fPathCache) {
fPathCache->purgeEntriesOlderThan(proxyProvider, purgeTime);
}
}
void GrCoverageCountingPathRenderer::CropPath(const SkPath& path, const SkIRect& cropbox,
SkPath* out) {
SkPath cropboxPath;
cropboxPath.addRect(SkRect::Make(cropbox));
if (!Op(cropboxPath, path, kIntersect_SkPathOp, out)) {
out->reset();
}
out->setIsVolatile(true);
}
float GrCoverageCountingPathRenderer::GetStrokeDevWidth(const SkMatrix& m,
const SkStrokeRec& stroke,
float* inflationRadius) {
float strokeDevWidth;
if (stroke.isHairlineStyle()) {
strokeDevWidth = 1;
} else {
SkASSERT(SkStrokeRec::kStroke_Style == stroke.getStyle());
SkASSERT(m.isSimilarity());
float matrixScaleFactor = SkVector::Length(m.getScaleX(), m.getSkewY());
strokeDevWidth = stroke.getWidth() * matrixScaleFactor;
}
if (inflationRadius) {
*inflationRadius = SkStrokeRec::GetInflationRadius(
stroke.getJoin(), stroke.getMiter(), stroke.getCap(), SkTMax(strokeDevWidth, 1.f));
}
return strokeDevWidth;
}