Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 "GrCCPerFlushResources.h" |
| 9 | |
| 10 | #include "GrOnFlushResourceProvider.h" |
| 11 | #include "GrRenderTargetContext.h" |
| 12 | #include "SkIPoint16.h" |
| 13 | |
| 14 | using PathInstance = GrCCPathProcessor::Instance; |
| 15 | |
| 16 | GrCCPerFlushResources::GrCCPerFlushResources(GrOnFlushResourceProvider* onFlushRP, |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame^] | 17 | const GrCCPerFlushResourceSpecs& specs) |
| 18 | : fPathParser(sk_make_sp<GrCCPathParser>(specs.fNumRenderedPaths + specs.fNumClipPaths, |
| 19 | specs.fParsingPathStats)) |
| 20 | , fAtlasSpecs(specs.fAtlasSpecs) |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 21 | , fIndexBuffer(GrCCPathProcessor::FindIndexBuffer(onFlushRP)) |
| 22 | , fVertexBuffer(GrCCPathProcessor::FindVertexBuffer(onFlushRP)) |
| 23 | , fInstanceBuffer(onFlushRP->makeBuffer(kVertex_GrBufferType, |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame^] | 24 | specs.fNumRenderedPaths * sizeof(PathInstance))) { |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 25 | if (!fIndexBuffer) { |
| 26 | SkDebugf("WARNING: failed to allocate CCPR index buffer. No paths will be drawn.\n"); |
| 27 | return; |
| 28 | } |
| 29 | if (!fVertexBuffer) { |
| 30 | SkDebugf("WARNING: failed to allocate CCPR vertex buffer. No paths will be drawn.\n"); |
| 31 | return; |
| 32 | } |
| 33 | if (!fInstanceBuffer) { |
| 34 | SkDebugf("WARNING: failed to allocate CCPR instance buffer. No paths will be drawn.\n"); |
| 35 | return; |
| 36 | } |
| 37 | fPathInstanceData = static_cast<PathInstance*>(fInstanceBuffer->map()); |
| 38 | SkASSERT(fPathInstanceData); |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame^] | 39 | SkDEBUGCODE(fPathInstanceBufferCount = specs.fNumRenderedPaths); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 40 | } |
| 41 | |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame^] | 42 | GrCCAtlas* GrCCPerFlushResources::renderPathInAtlas(const SkIRect& clipIBounds, const SkMatrix& m, |
| 43 | const SkPath& path, SkRect* devBounds, |
| 44 | SkRect* devBounds45, int16_t* atlasOffsetX, |
| 45 | int16_t* atlasOffsetY) { |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 46 | SkASSERT(this->isMapped()); |
| 47 | SkIRect devIBounds; |
| 48 | fPathParser->parsePath(m, path, devBounds, devBounds45); |
| 49 | devBounds->roundOut(&devIBounds); |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame^] | 50 | return this->placeParsedPathInAtlas(clipIBounds, devIBounds, atlasOffsetX, atlasOffsetY); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 51 | } |
| 52 | |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame^] | 53 | GrCCAtlas* GrCCPerFlushResources::renderDeviceSpacePathInAtlas(const SkIRect& clipIBounds, |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 54 | const SkPath& devPath, |
| 55 | const SkIRect& devPathIBounds, |
| 56 | int16_t* atlasOffsetX, |
| 57 | int16_t* atlasOffsetY) { |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 58 | SkASSERT(this->isMapped()); |
| 59 | fPathParser->parseDeviceSpacePath(devPath); |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame^] | 60 | return this->placeParsedPathInAtlas(clipIBounds, devPathIBounds, atlasOffsetX, atlasOffsetY); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 61 | } |
| 62 | |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame^] | 63 | GrCCAtlas* GrCCPerFlushResources::placeParsedPathInAtlas(const SkIRect& clipIBounds, |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 64 | const SkIRect& pathIBounds, |
| 65 | int16_t* atlasOffsetX, |
| 66 | int16_t* atlasOffsetY) { |
| 67 | using ScissorMode = GrCCPathParser::ScissorMode; |
| 68 | ScissorMode scissorMode; |
| 69 | SkIRect clippedPathIBounds; |
| 70 | if (clipIBounds.contains(pathIBounds)) { |
| 71 | clippedPathIBounds = pathIBounds; |
| 72 | scissorMode = ScissorMode::kNonScissored; |
| 73 | } else if (clippedPathIBounds.intersect(clipIBounds, pathIBounds)) { |
| 74 | scissorMode = ScissorMode::kScissored; |
| 75 | } else { |
| 76 | fPathParser->discardParsedPath(); |
| 77 | return nullptr; |
| 78 | } |
| 79 | |
| 80 | SkIPoint16 atlasLocation; |
| 81 | int h = clippedPathIBounds.height(), w = clippedPathIBounds.width(); |
| 82 | if (fAtlases.empty() || !fAtlases.back().addRect(w, h, &atlasLocation)) { |
| 83 | if (!fAtlases.empty()) { |
| 84 | // The atlas is out of room and can't grow any bigger. |
| 85 | auto coverageCountBatchID = fPathParser->closeCurrentBatch(); |
| 86 | fAtlases.back().setCoverageCountBatchID(coverageCountBatchID); |
| 87 | } |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame^] | 88 | fAtlases.emplace_back(fAtlasSpecs); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 89 | SkAssertResult(fAtlases.back().addRect(w, h, &atlasLocation)); |
| 90 | } |
| 91 | |
| 92 | *atlasOffsetX = atlasLocation.x() - static_cast<int16_t>(clippedPathIBounds.left()); |
| 93 | *atlasOffsetY = atlasLocation.y() - static_cast<int16_t>(clippedPathIBounds.top()); |
| 94 | fPathParser->saveParsedPath(scissorMode, clippedPathIBounds, *atlasOffsetX, *atlasOffsetY); |
| 95 | |
| 96 | return &fAtlases.back(); |
| 97 | } |
| 98 | |
| 99 | bool GrCCPerFlushResources::finalize(GrOnFlushResourceProvider* onFlushRP, |
| 100 | SkTArray<sk_sp<GrRenderTargetContext>>* atlasDraws) { |
| 101 | SkASSERT(this->isMapped()); |
| 102 | fInstanceBuffer->unmap(); |
| 103 | fPathInstanceData = nullptr; |
| 104 | |
| 105 | if (!fAtlases.empty()) { |
| 106 | auto coverageCountBatchID = fPathParser->closeCurrentBatch(); |
| 107 | fAtlases.back().setCoverageCountBatchID(coverageCountBatchID); |
| 108 | } |
| 109 | |
| 110 | if (!fPathParser->finalize(onFlushRP)) { |
| 111 | SkDebugf("WARNING: failed to allocate GPU buffers for CCPR. No paths will be drawn.\n"); |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | // Draw the atlas(es). |
| 116 | GrTAllocator<GrCCAtlas>::Iter atlasIter(&fAtlases); |
| 117 | while (atlasIter.next()) { |
| 118 | if (auto rtc = atlasIter.get()->finalize(onFlushRP, fPathParser)) { |
| 119 | atlasDraws->push_back(std::move(rtc)); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return true; |
| 124 | } |