blob: 022ce6b24e1e065b006c9efe00061bdc8d7cb659 [file] [log] [blame]
Chris Dalton1a325d22017-07-14 15:17:41 -06001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
Chris Dalton03730e62021-03-11 19:41:40 -07009#include <memory>
Chris Dalton1a325d22017-07-14 15:17:41 -060010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/pathops/SkPathOps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrProxyProvider.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050014#include "src/gpu/GrSurfaceDrawContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/ccpr/GrCCClipProcessor.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060016
Chris Dalton03730e62021-03-11 19:41:40 -070017bool GrCoverageCountingPathRenderer::IsSupported(const GrCaps& caps) {
Chris Dalton1a325d22017-07-14 15:17:41 -060018 const GrShaderCaps& shaderCaps = *caps.shaderCaps();
Greg Daniel0258c902019-08-01 13:08:33 -040019 GrBackendFormat defaultA8Format = caps.getDefaultBackendFormat(GrColorType::kAlpha_8,
20 GrRenderable::kYes);
Chris Dalton94310792021-03-12 11:10:24 -070021 if (caps.driverDisableMSAAClipAtlas() || !shaderCaps.integerSupport() ||
Chris Daltona77cdee2020-04-03 14:50:43 -060022 !caps.drawInstancedSupport() || !shaderCaps.floatIs32Bits() ||
Greg Daniel0258c902019-08-01 13:08:33 -040023 !defaultA8Format.isValid() || // This checks both texturable and renderable
Chris Daltona8fbeba2019-03-30 00:31:23 -060024 !caps.halfFloatVertexAttributeSupport()) {
25 return false;
26 }
Chris Daltonc3318f02019-07-19 14:20:53 -060027
Chris Dalton94310792021-03-12 11:10:24 -070028 if (caps.internalMultisampleCount(defaultA8Format) > 1 &&
Chris Daltonc3318f02019-07-19 14:20:53 -060029 caps.sampleLocationsSupport() &&
Chris Dalton8a64a442019-10-29 18:54:58 -060030 shaderCaps.sampleMaskSupport()) {
Chris Daltonc3318f02019-07-19 14:20:53 -060031 return true;
32 }
33
34 return false;
Chris Dalton1a325d22017-07-14 15:17:41 -060035}
36
Chris Dalton03730e62021-03-11 19:41:40 -070037std::unique_ptr<GrCoverageCountingPathRenderer> GrCoverageCountingPathRenderer::CreateIfSupported(
38 const GrCaps& caps) {
39 if (IsSupported(caps)) {
40 return std::make_unique<GrCoverageCountingPathRenderer>();
Chris Daltonc3318f02019-07-19 14:20:53 -060041 }
42 return nullptr;
Chris Daltona2b5b642018-06-24 13:08:57 -060043}
44
Greg Danielf41b2bd2019-08-22 16:19:24 -040045GrCCPerOpsTaskPaths* GrCoverageCountingPathRenderer::lookupPendingPaths(uint32_t opsTaskID) {
46 auto it = fPendingPaths.find(opsTaskID);
Chris Daltond7e22272018-05-23 10:17:17 -060047 if (fPendingPaths.end() == it) {
Greg Danielf41b2bd2019-08-22 16:19:24 -040048 sk_sp<GrCCPerOpsTaskPaths> paths = sk_make_sp<GrCCPerOpsTaskPaths>();
49 it = fPendingPaths.insert(std::make_pair(opsTaskID, std::move(paths))).first;
Chris Daltond7e22272018-05-23 10:17:17 -060050 }
51 return it->second.get();
Chris Dalton5ba36ba2018-05-09 01:08:38 -060052}
53
Chris Dalton383a2ef2018-01-08 17:21:41 -050054std::unique_ptr<GrFragmentProcessor> GrCoverageCountingPathRenderer::makeClipProcessor(
John Stiles956ec8a2020-06-19 15:32:16 -040055 std::unique_ptr<GrFragmentProcessor> inputFP, uint32_t opsTaskID,
56 const SkPath& deviceSpacePath, const SkIRect& accessRect, const GrCaps& caps) {
Chris Dalton1e7b2e52021-03-09 21:29:32 -070057#ifdef SK_DEBUG
Chris Daltona32a3c32017-12-05 10:05:21 -070058 SkASSERT(!fFlushing);
Chris Dalton1e7b2e52021-03-09 21:29:32 -070059 SkIRect pathIBounds;
60 deviceSpacePath.getBounds().roundOut(&pathIBounds);
61 SkIRect maskBounds;
62 if (maskBounds.intersect(accessRect, pathIBounds)) {
63 SkASSERT(maskBounds.height64() * maskBounds.width64() <= kMaxClipPathArea);
64 }
65#endif
Chris Daltona32a3c32017-12-05 10:05:21 -070066
Chris Daltonc3318f02019-07-19 14:20:53 -060067 uint32_t key = deviceSpacePath.getGenerationID();
Chris Dalton03730e62021-03-11 19:41:40 -070068 key = (key << 1) | (uint32_t)GrFillRuleForSkPath(deviceSpacePath);
Chris Dalton5ba36ba2018-05-09 01:08:38 -060069 GrCCClipPath& clipPath =
Greg Danielf41b2bd2019-08-22 16:19:24 -040070 this->lookupPendingPaths(opsTaskID)->fClipPaths[key];
Chris Dalton5ba36ba2018-05-09 01:08:38 -060071 if (!clipPath.isInitialized()) {
Chris Daltona32a3c32017-12-05 10:05:21 -070072 // This ClipPath was just created during lookup. Initialize it.
Chris Dalton5ba36ba2018-05-09 01:08:38 -060073 const SkRect& pathDevBounds = deviceSpacePath.getBounds();
Brian Osman788b9162020-02-07 10:36:46 -050074 if (std::max(pathDevBounds.height(), pathDevBounds.width()) > kPathCropThreshold) {
Chris Dalton5ba36ba2018-05-09 01:08:38 -060075 // The path is too large. Crop it or analytic AA can run out of fp32 precision.
76 SkPath croppedPath;
Chris Dalton4c458b12018-06-16 17:22:59 -060077 int maxRTSize = caps.maxRenderTargetSize();
Chris Dalton09a7bb22018-08-31 19:53:15 +080078 CropPath(deviceSpacePath, SkIRect::MakeWH(maxRTSize, maxRTSize), &croppedPath);
Chris Dalton03730e62021-03-11 19:41:40 -070079 clipPath.init(croppedPath, accessRect, caps);
Chris Dalton5ba36ba2018-05-09 01:08:38 -060080 } else {
Chris Dalton03730e62021-03-11 19:41:40 -070081 clipPath.init(deviceSpacePath, accessRect, caps);
Chris Dalton5ba36ba2018-05-09 01:08:38 -060082 }
Chris Daltona32a3c32017-12-05 10:05:21 -070083 } else {
84 clipPath.addAccess(accessRect);
85 }
86
Chris Daltonc3318f02019-07-19 14:20:53 -060087 auto mustCheckBounds = GrCCClipProcessor::MustCheckBounds(
88 !clipPath.pathDevIBounds().contains(accessRect));
Chris Dalton2603c1f2021-03-10 20:20:26 -070089 return std::make_unique<GrCCClipProcessor>(std::move(inputFP), caps, &clipPath,
90 mustCheckBounds);
Chris Daltona32a3c32017-12-05 10:05:21 -070091}
92
Brian Salomonbf6b9792019-08-21 09:38:10 -040093void GrCoverageCountingPathRenderer::preFlush(
Adlai Holler9902cff2020-11-11 08:51:25 -050094 GrOnFlushResourceProvider* onFlushRP, SkSpan<const uint32_t> taskIDs) {
Chris Daltona32a3c32017-12-05 10:05:21 -070095 SkASSERT(!fFlushing);
Chris Daltond7e22272018-05-23 10:17:17 -060096 SkASSERT(fFlushingPaths.empty());
Chris Dalton383a2ef2018-01-08 17:21:41 -050097 SkDEBUGCODE(fFlushing = true);
Chris Daltona32a3c32017-12-05 10:05:21 -070098
Chris Daltond7e22272018-05-23 10:17:17 -060099 if (fPendingPaths.empty()) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500100 return; // Nothing to draw.
Chris Daltona32a3c32017-12-05 10:05:21 -0700101 }
Chris Daltonc1e59632017-09-05 00:30:07 -0600102
Chris Dalton7d592cd2021-03-11 22:49:33 -0700103 GrCCAtlas::Specs specs;
Chris Dalton42c21152018-06-13 15:28:19 -0600104 int maxPreferredRTSize = onFlushRP->caps()->maxPreferredRenderTargetSize();
Chris Dalton7d592cd2021-03-11 22:49:33 -0700105 specs.fMaxPreferredTextureSize = maxPreferredRTSize;
106 specs.fMinTextureSize = std::min(512, maxPreferredRTSize);
Chris Dalton42c21152018-06-13 15:28:19 -0600107
Greg Danielf41b2bd2019-08-22 16:19:24 -0400108 // Move the per-opsTask paths that are about to be flushed from fPendingPaths to fFlushingPaths,
Chris Dalton42c21152018-06-13 15:28:19 -0600109 // and count them up so we can preallocate buffers.
Adlai Holler9902cff2020-11-11 08:51:25 -0500110 fFlushingPaths.reserve_back(taskIDs.count());
111 for (uint32_t taskID : taskIDs) {
112 auto iter = fPendingPaths.find(taskID);
Chris Daltond7e22272018-05-23 10:17:17 -0600113 if (fPendingPaths.end() == iter) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400114 continue; // No paths on this opsTask.
Chris Dalton1a325d22017-07-14 15:17:41 -0600115 }
Chris Daltona32a3c32017-12-05 10:05:21 -0700116
tzikbdb49562018-05-28 14:58:00 +0900117 fFlushingPaths.push_back(std::move(iter->second));
Chris Daltond7e22272018-05-23 10:17:17 -0600118 fPendingPaths.erase(iter);
119
Chris Daltond7e22272018-05-23 10:17:17 -0600120 for (const auto& clipsIter : fFlushingPaths.back()->fClipPaths) {
Chris Dalton4da70192018-06-18 09:51:36 -0600121 clipsIter.second.accountForOwnPath(&specs);
Chris Daltona32a3c32017-12-05 10:05:21 -0700122 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600123 }
124
Chris Dalton7d592cd2021-03-11 22:49:33 -0700125 fPerFlushResources = std::make_unique<GrCCPerFlushResources>(onFlushRP, specs);
Chris Dalton1a325d22017-07-14 15:17:41 -0600126
Chris Dalton7d592cd2021-03-11 22:49:33 -0700127 // Layout the atlas(es) and render paths.
Chris Daltond7e22272018-05-23 10:17:17 -0600128 for (const auto& flushingPaths : fFlushingPaths) {
Chris Daltond7e22272018-05-23 10:17:17 -0600129 for (auto& clipsIter : flushingPaths->fClipPaths) {
Chris Dalton7d592cd2021-03-11 22:49:33 -0700130 clipsIter.second.renderPathInAtlas(fPerFlushResources.get(), onFlushRP);
Chris Daltonc1e59632017-09-05 00:30:07 -0600131 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600132 }
133
Chris Dalton351e80c2019-01-06 22:51:00 -0700134 // Allocate resources and then render the atlas(es).
Chris Dalton7d592cd2021-03-11 22:49:33 -0700135 fPerFlushResources->finalize(onFlushRP);
Chris Dalton1a325d22017-07-14 15:17:41 -0600136}
137
Adlai Holler9902cff2020-11-11 08:51:25 -0500138void GrCoverageCountingPathRenderer::postFlush(GrDeferredUploadToken,
139 SkSpan<const uint32_t> /* taskIDs */) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600140 SkASSERT(fFlushing);
Robert Phillips774168e2018-05-31 12:43:27 -0400141
Chris Dalton7d592cd2021-03-11 22:49:33 -0700142 fPerFlushResources.reset();
Chris Dalton4da70192018-06-18 09:51:36 -0600143
Chris Dalton7d592cd2021-03-11 22:49:33 -0700144 if (!fFlushingPaths.empty()) {
Chris Dalton4da70192018-06-18 09:51:36 -0600145 // We wait to erase these until after flush, once Ops and FPs are done accessing their data.
146 fFlushingPaths.reset();
Robert Phillips774168e2018-05-31 12:43:27 -0400147 }
148
Chris Dalton383a2ef2018-01-08 17:21:41 -0500149 SkDEBUGCODE(fFlushing = false);
Chris Dalton1a325d22017-07-14 15:17:41 -0600150}
Chris Dalton09a7bb22018-08-31 19:53:15 +0800151
152void GrCoverageCountingPathRenderer::CropPath(const SkPath& path, const SkIRect& cropbox,
153 SkPath* out) {
154 SkPath cropboxPath;
155 cropboxPath.addRect(SkRect::Make(cropbox));
156 if (!Op(cropboxPath, path, kIntersect_SkPathOp, out)) {
157 // This can fail if the PathOps encounter NaN or infinities.
158 out->reset();
159 }
160 out->setIsVolatile(true);
161}