Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [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 | #ifndef DDLTileHelper_DEFINED |
| 9 | #define DDLTileHelper_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkRect.h" |
| 12 | #include "include/core/SkRefCnt.h" |
| 13 | #include "include/core/SkSurfaceCharacterization.h" |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 14 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 15 | class DDLPromiseImageHelper; |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 16 | class PromiseImageCallbackContext; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 17 | class SkCanvas; |
| 18 | class SkData; |
| 19 | class SkDeferredDisplayList; |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 20 | class SkDeferredDisplayListRecorder; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 21 | class SkPicture; |
| 22 | class SkSurface; |
| 23 | class SkSurfaceCharacterization; |
| 24 | |
| 25 | class DDLTileHelper { |
| 26 | public: |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 27 | // The TileData class encapsulates the information and behavior of a single tile when |
| 28 | // rendering with DDLs. |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 29 | class TileData { |
| 30 | public: |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 31 | TileData() {} |
| 32 | ~TileData(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 33 | |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 34 | void init(int id, |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 35 | GrContext* context, |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 36 | const SkSurfaceCharacterization& dstChar, |
| 37 | const SkIRect& clip); |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 38 | |
| 39 | // Convert the compressedPictureData into an SkPicture replacing each image-index |
| 40 | // with a promise image. |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 41 | void createTileSpecificSKP(SkData* compressedPictureData, |
| 42 | const DDLPromiseImageHelper& helper); |
| 43 | |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 44 | // Create the DDL for this tile (i.e., fill in 'fDisplayList'). |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 45 | void createDDL(); |
| 46 | |
Robert Phillips | 6eb5cb9 | 2020-03-05 12:52:45 -0500 | [diff] [blame] | 47 | // Precompile all the programs required to draw this tile's DDL |
| 48 | void precompile(GrContext*); |
| 49 | |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame] | 50 | // Just draw the re-inflated per-tile SKP directly into this tile w/o going through a DDL |
| 51 | // first. This is used for determining the overhead of using DDLs (i.e., it replaces |
| 52 | // a 'createDDL' and 'draw' pair. |
| 53 | void drawSKPDirectly(GrContext*); |
| 54 | |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 55 | // Replay the recorded DDL into the tile surface - filling in 'fBackendTexture'. |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 56 | void draw(GrContext*); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 57 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 58 | void reset(); |
| 59 | |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 60 | int id() const { return fID; } |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 61 | SkIRect clipRect() const { return fClip; } |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 62 | |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 63 | SkDeferredDisplayList* ddl() { return fDisplayList.get(); } |
| 64 | |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 65 | sk_sp<SkImage> makePromiseImage(SkDeferredDisplayListRecorder*); |
| 66 | void dropCallbackContext() { fCallbackContext.reset(); } |
| 67 | |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 68 | static void CreateBackendTexture(GrContext*, TileData*); |
| 69 | static void DeleteBackendTexture(GrContext*, TileData*); |
| 70 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 71 | private: |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 72 | sk_sp<SkSurface> makeWrappedTileDest(GrContext* context); |
| 73 | |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 74 | sk_sp<PromiseImageCallbackContext> refCallbackContext() { return fCallbackContext; } |
| 75 | |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 76 | int fID = -1; |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 77 | SkIRect fClip; // in the device space of the final SkSurface |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 78 | SkSurfaceCharacterization fCharacterization; // characterization for the tile's surface |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 79 | |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 80 | // The callback context holds (via its SkPromiseImageTexture) the backend texture |
| 81 | // that is both wrapped in 'fTileSurface' and backs this tile's promise image |
| 82 | // (i.e., the one returned by 'makePromiseImage'). |
| 83 | sk_sp<PromiseImageCallbackContext> fCallbackContext; |
| 84 | // 'fTileSurface' wraps the backend texture in 'fCallbackContext' and must exist until |
| 85 | // after 'fDisplayList' has been flushed (bc it owns the proxy the DDL's destination |
| 86 | // trampoline points at). |
| 87 | // TODO: fix the ref-order so we don't need 'fTileSurface' here |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 88 | sk_sp<SkSurface> fTileSurface; |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 89 | |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 90 | sk_sp<SkPicture> fReconstitutedPicture; |
| 91 | SkTArray<sk_sp<SkImage>> fPromiseImages; // All the promise images in the |
| 92 | // reconstituted picture |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 93 | std::unique_ptr<SkDeferredDisplayList> fDisplayList; |
| 94 | }; |
| 95 | |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 96 | DDLTileHelper(GrContext* context, |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 97 | const SkSurfaceCharacterization& dstChar, |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 98 | const SkIRect& viewport, |
| 99 | int numDivisions); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 100 | |
| 101 | void createSKPPerTile(SkData* compressedPictureData, const DDLPromiseImageHelper& helper); |
| 102 | |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 103 | void kickOffThreadedWork(SkTaskGroup* recordingTaskGroup, |
| 104 | SkTaskGroup* gpuTaskGroup, |
| 105 | GrContext* gpuThreadContext); |
| 106 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 107 | void createDDLsInParallel(); |
| 108 | |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 109 | // Create the DDL that will compose all the tile images into a final result. |
| 110 | void createComposeDDL(); |
| 111 | SkDeferredDisplayList* composeDDL() const { return fComposeDDL.get(); } |
| 112 | |
Robert Phillips | 6eb5cb9 | 2020-03-05 12:52:45 -0500 | [diff] [blame] | 113 | void precompileAndDrawAllTiles(GrContext*); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 114 | |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame] | 115 | // For each tile, create its DDL and then draw it - all on a single thread. This is to allow |
| 116 | // comparison w/ just drawing the SKP directly (i.e., drawAllTilesDirectly). The |
| 117 | // DDL creations and draws are interleaved to prevent starvation of the GPU. |
| 118 | // Note: this is somewhat of a misuse/pessimistic-use of DDLs since they are supposed to |
| 119 | // be created on a separate thread. |
| 120 | void interleaveDDLCreationAndDraw(GrContext*); |
| 121 | |
| 122 | // This draws all the per-tile SKPs directly into all of the tiles w/o converting them to |
| 123 | // DDLs first - all on a single thread. |
| 124 | void drawAllTilesDirectly(GrContext*); |
| 125 | |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 126 | void dropCallbackContexts(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 127 | void resetAllTiles(); |
| 128 | |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 129 | int numTiles() const { return fNumDivisions * fNumDivisions; } |
| 130 | |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 131 | void createBackendTextures(SkTaskGroup*, GrContext*); |
| 132 | void deleteBackendTextures(SkTaskGroup*, GrContext*); |
| 133 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 134 | private: |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 135 | int fNumDivisions; // number of tiles along a side |
Adlai Holler | 8e82123 | 2020-05-11 13:33:59 -0400 | [diff] [blame^] | 136 | SkAutoTArray<TileData> fTiles; // 'fNumDivisions' x 'fNumDivisions' |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 137 | |
| 138 | std::unique_ptr<SkDeferredDisplayList> fComposeDDL; |
| 139 | |
| 140 | const SkSurfaceCharacterization fDstCharacterization; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | #endif |