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