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