Chris Dalton | 83420eb | 2021-06-23 18:47:09 -0600 | [diff] [blame^] | 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 | |
| 8 | #ifndef GrGrAtlasRenderTask_DEFINED |
| 9 | #define GrGrAtlasRenderTask_DEFINED |
| 10 | |
| 11 | #include "include/core/SkPath.h" |
| 12 | #include "src/gpu/GrDynamicAtlas.h" |
| 13 | #include "src/gpu/GrOpsTask.h" |
| 14 | |
| 15 | struct SkIPoint16; |
| 16 | |
| 17 | // Represents a GrRenderTask that draws paths into an atlas. This task gets added the DAG and left |
| 18 | // open, lays out its atlas while future tasks call addPath(), and finally adds its internal draw |
| 19 | // ops during onMakeClosed(). |
| 20 | // |
| 21 | // The atlas texture does not get instantiated automatically. It is the creator's responsibility to |
| 22 | // call instantiate() at flush time. |
| 23 | class GrAtlasRenderTask : public GrOpsTask { |
| 24 | public: |
| 25 | GrAtlasRenderTask(GrRecordingContext*, GrAuditTrail*, sk_sp<GrArenas>, |
| 26 | std::unique_ptr<GrDynamicAtlas>); |
| 27 | |
| 28 | const GrTextureProxy* atlasProxy() const { return fDynamicAtlas->textureProxy(); } |
| 29 | GrSurfaceProxyView readView(const GrCaps& caps) const { return fDynamicAtlas->readView(caps); } |
| 30 | |
| 31 | // Allocates a rectangle for, and stages the given path to be rendered into the atlas. Returns |
| 32 | // false if there was not room in the atlas. On success, writes out the location of the path's |
| 33 | // upper-left corner to 'locationInAtlas'. |
| 34 | bool addPath(const SkMatrix&, const SkPath&, bool antialias, SkIPoint pathDevTopLeft, |
| 35 | int widthInAtlas, int heightInAtlas, bool transposedInAtlas, |
| 36 | SkIPoint16* locationInAtlas); |
| 37 | |
| 38 | // Must be called at flush time. The texture proxy is instantiated with 'backingTexture', if |
| 39 | // provided. See GrDynamicAtlas. |
| 40 | void instantiate(GrOnFlushResourceProvider* onFlushRP, |
| 41 | sk_sp<GrTexture> backingTexture = nullptr) { |
| 42 | SkASSERT(this->isClosed()); |
| 43 | fDynamicAtlas->instantiate(onFlushRP, std::move(backingTexture)); |
| 44 | } |
| 45 | |
| 46 | private: |
| 47 | // Adds internal ops to render the atlas before deferring to GrOpsTask::onMakeClosed. |
| 48 | ExpectedOutcome onMakeClosed(GrRecordingContext*, SkIRect* targetUpdateBounds) override; |
| 49 | |
| 50 | void stencilAtlasRect(GrRecordingContext*, const SkRect&, const SkPMColor4f&, |
| 51 | const GrUserStencilSettings*); |
| 52 | void addAtlasDrawOp(GrOp::Owner, bool usesMSAA, const GrCaps&); |
| 53 | |
| 54 | // Executes the GrOpsTask and resolves msaa if needed. |
| 55 | bool onExecute(GrOpFlushState* flushState) override; |
| 56 | |
| 57 | SkPath* getUberPath(SkPathFillType fillType, bool antialias) { |
| 58 | int idx = (int)antialias << 1; |
| 59 | idx |= (int)fillType & 1; |
| 60 | return &fUberPaths[idx]; |
| 61 | } |
| 62 | |
| 63 | const std::unique_ptr<GrDynamicAtlas> fDynamicAtlas; |
| 64 | SkPath fUberPaths[4]; // 2 fillTypes * 2 antialias modes. |
| 65 | }; |
| 66 | |
| 67 | #endif |