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