| 1 |
/* |
| 2 |
* Copyright 2021 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 |
#ifndef AtlasRenderTask_DEFINED |
| 8 |
#define AtlasRenderTask_DEFINED |
| 9 |
|
| 10 |
#include "include/core/SkPath.h" |
| 11 |
#include "include/core/SkRefCnt.h" |
| 12 |
#include "include/private/base/SkAssert.h" |
| 13 |
#include "include/private/base/SkNoncopyable.h" |
| 14 |
#include "src/base/SkBlockAllocator.h" |
| 15 |
#include "src/base/SkTBlockList.h" |
| 16 |
#include "src/core/SkColorData.h" |
| 17 |
#include "src/gpu/ganesh/GrCaps.h" |
| 18 |
#include "src/gpu/ganesh/GrDynamicAtlas.h" |
| 19 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 20 |
#include "src/gpu/ganesh/GrTexture.h" |
| 21 |
#include "src/gpu/ganesh/ops/GrOp.h" |
| 22 |
#include "src/gpu/ganesh/ops/OpsTask.h" |
| 23 |
#include "src/gpu/ganesh/tessellate/PathTessellator.h" |
| 24 |
|
| 25 |
#include <limits> |
| 26 |
#include <memory> |
| 27 |
#include <utility> |
| 28 |
|
| 29 |
class GrArenas; |
| 30 |
class GrOnFlushResourceProvider; |
| 31 |
class GrOpFlushState; |
| 32 |
class GrRecordingContext; |
| 33 |
class GrTextureProxy; |
| 34 |
class SkMatrix; |
| 35 |
struct GrUserStencilSettings; |
| 36 |
struct SkIPoint16; |
| 37 |
struct SkIPoint; |
| 38 |
struct SkIRect; |
| 39 |
struct SkRect; |
| 40 |
|
| 41 |
namespace skgpu::ganesh { |
| 42 |
|
| 43 |
// Represents a GrRenderTask that draws paths into an atlas. This task gets added the DAG and left |
| 44 |
// open, lays out its atlas while future tasks call addPath(), and finally adds its internal draw |
| 45 |
// ops during onMakeClosed(). |
| 46 |
// |
| 47 |
// The atlas texture does not get instantiated automatically. It is the creator's responsibility to |
| 48 |
// call instantiate() at flush time. |
| 49 |
class AtlasRenderTask final : public OpsTask { |
| 50 |
public: |
| 51 |
AtlasRenderTask(GrRecordingContext*, |
| 52 |
sk_sp<GrArenas>, |
| 53 |
std::unique_ptr<GrDynamicAtlas>); |
| 54 |
|
| 55 |
const GrTextureProxy* atlasProxy() const { return fDynamicAtlas->textureProxy(); } |
| 56 |
GrSurfaceProxyView readView(const GrCaps& caps) const { return fDynamicAtlas->readView(caps); } |
| 57 |
|
| 58 |
// Allocates a rectangle for, and stages the given path to be rendered into the atlas. Returns |
| 59 |
// false if there was not room in the atlas. On success, writes out the location of the path's |
| 60 |
// upper-left corner to 'locationInAtlas'. |
| 61 |
bool addPath(const SkMatrix&, const SkPath&, SkIPoint pathDevTopLeft, int widthInAtlas, |
| 62 |
int heightInAtlas, bool transposedInAtlas, SkIPoint16* locationInAtlas); |
| 63 |
|
| 64 |
// Must be called at flush time. The texture proxy is instantiated with 'backingTexture', if |
| 65 |
// provided. See GrDynamicAtlas. |
| 66 |
[[nodiscard]] bool instantiate(GrOnFlushResourceProvider* onFlushRP, |
| 67 |
sk_sp<GrTexture> backingTexture = nullptr) { |
| 68 |
SkASSERT(this->isClosed()); |
| 69 |
return fDynamicAtlas->instantiate(onFlushRP, std::move(backingTexture)); |
| 70 |
} |
| 71 |
|
| 72 |
private: |
| 73 |
// Adds internal ops to render the atlas before deferring to OpsTask::onMakeClosed. |
| 74 |
ExpectedOutcome onMakeClosed(GrRecordingContext*, SkIRect* targetUpdateBounds) override; |
| 75 |
|
| 76 |
void stencilAtlasRect(GrRecordingContext*, const SkRect&, const SkPMColor4f&, |
| 77 |
const GrUserStencilSettings*); |
| 78 |
void addAtlasDrawOp(GrOp::Owner, const GrCaps&); |
| 79 |
|
| 80 |
// Executes the OpsTask and resolves msaa if needed. |
| 81 |
bool onExecute(GrOpFlushState* flushState) override; |
| 82 |
|
| 83 |
const std::unique_ptr<GrDynamicAtlas> fDynamicAtlas; |
| 84 |
|
| 85 |
// Allocate enough inline entries for 16 atlas path draws, then spill to the heap. |
| 86 |
using PathDrawList = PathTessellator::PathDrawList; |
| 87 |
using PathDrawAllocator = SkTBlockList<PathDrawList, 16>; |
| 88 |
PathDrawAllocator fPathDrawAllocator{64, SkBlockAllocator::GrowthPolicy::kFibonacci}; |
| 89 |
|
| 90 |
class AtlasPathList : SkNoncopyable { |
| 91 |
public: |
| 92 |
void add(PathDrawAllocator* alloc, const SkMatrix& pathMatrix, const SkPath& path) { |
| 93 |
SkASSERT(this->canAdd(path)); |
| 94 |
fPathDrawList = &alloc->emplace_back(pathMatrix, path, SK_PMColor4fTRANSPARENT, |
| 95 |
fPathDrawList); |
| 96 |
if (path.isInverseFillType()) { |
| 97 |
// The atlas never has inverse paths. The inversion happens later. |
| 98 |
fPathDrawList->fPath.toggleInverseFillType(); |
| 99 |
} |
| 100 |
fTotalCombinedPathVerbCnt += path.countVerbs(); |
| 101 |
++fPathCount; |
| 102 |
} |
| 103 |
|
| 104 |
bool canAdd(const SkPath& path) const { |
| 105 |
// Return true so long as we won't overflow the total verb count |
| 106 |
return std::numeric_limits<int>::max() - fTotalCombinedPathVerbCnt >= path.countVerbs(); |
| 107 |
} |
| 108 |
|
| 109 |
const PathDrawList* pathDrawList() const { return fPathDrawList; } |
| 110 |
int totalCombinedPathVerbCnt() const { return fTotalCombinedPathVerbCnt; } |
| 111 |
int pathCount() const { return fPathCount; } |
| 112 |
|
| 113 |
private: |
| 114 |
PathDrawList* fPathDrawList = nullptr; |
| 115 |
int fTotalCombinedPathVerbCnt = 0; |
| 116 |
int fPathCount = 0; |
| 117 |
}; |
| 118 |
|
| 119 |
AtlasPathList fWindingPathList; |
| 120 |
AtlasPathList fEvenOddPathList; |
| 121 |
}; |
| 122 |
|
| 123 |
} // namespace skgpu::ganesh |
| 124 |
|
| 125 |
#endif // AtlasRenderTask_DEFINED |