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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tools/DDLTileHelper.h" |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkDeferredDisplayListRecorder.h" |
| 12 | #include "include/core/SkPicture.h" |
| 13 | #include "include/core/SkSurface.h" |
| 14 | #include "include/core/SkSurfaceCharacterization.h" |
| 15 | #include "src/core/SkDeferredDisplayListPriv.h" |
| 16 | #include "src/core/SkTaskGroup.h" |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/image/SkImage_Gpu.h" |
| 19 | #include "tools/DDLPromiseImageHelper.h" |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 20 | |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 21 | void DDLTileHelper::TileData::init(int id, |
| 22 | sk_sp<SkSurface> dstSurface, |
| 23 | const SkSurfaceCharacterization& dstSurfaceCharacterization, |
| 24 | const SkIRect& clip) { |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 25 | fID = id; |
| 26 | fDstSurface = dstSurface; |
| 27 | fClip = clip; |
| 28 | |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 29 | fCharacterization = dstSurfaceCharacterization.createResized(clip.width(), clip.height()); |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 30 | SkASSERT(fCharacterization.isValid()); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 31 | } |
| 32 | |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 33 | DDLTileHelper::TileData::~TileData() {} |
| 34 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 35 | void DDLTileHelper::TileData::createTileSpecificSKP(SkData* compressedPictureData, |
| 36 | const DDLPromiseImageHelper& helper) { |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 37 | SkASSERT(!fReconstitutedPicture); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 38 | |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 39 | // This is bending the DDLRecorder contract! The promise images in the SKP should be |
| 40 | // created by the same recorder used to create the matching DDL. |
| 41 | SkDeferredDisplayListRecorder recorder(fCharacterization); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 42 | |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 43 | fReconstitutedPicture = helper.reinflateSKP(&recorder, compressedPictureData, &fPromiseImages); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 44 | |
| 45 | std::unique_ptr<SkDeferredDisplayList> ddl = recorder.detach(); |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 46 | if (ddl->priv().numRenderTasks()) { |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 47 | // TODO: remove this once skbug.com/8424 is fixed. If the DDL resulting from the |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 48 | // reinflation of the SKPs contains opsTasks that means some image subset operation |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 49 | // created a draw. |
| 50 | fReconstitutedPicture.reset(); |
| 51 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void DDLTileHelper::TileData::createDDL() { |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame^] | 55 | SkASSERT(!fDisplayList && fReconstitutedPicture); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 56 | |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 57 | SkDeferredDisplayListRecorder recorder(fCharacterization); |
| 58 | |
| 59 | // DDL TODO: the DDLRecorder's GrContext isn't initialized until getCanvas is called. |
| 60 | // Maybe set it up in the ctor? |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame^] | 61 | SkCanvas* recordingCanvas = recorder.getCanvas(); |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 62 | |
| 63 | // Because we cheated in createTileSpecificSKP and used the wrong DDLRecorder, the GrContext's |
| 64 | // stored in fReconstitutedPicture's promise images are incorrect. Patch them with the correct |
| 65 | // one now. |
| 66 | for (int i = 0; i < fPromiseImages.count(); ++i) { |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame^] | 67 | GrContext* newContext = recordingCanvas->getGrContext(); |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 68 | |
| 69 | if (fPromiseImages[i]->isTextureBacked()) { |
Jim Van Verth | 21bd60d | 2018-10-12 15:00:20 -0400 | [diff] [blame] | 70 | SkImage_GpuBase* gpuImage = (SkImage_GpuBase*) fPromiseImages[i].get(); |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 71 | gpuImage->resetContext(sk_ref_sp(newContext)); |
| 72 | } |
| 73 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 74 | |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame^] | 75 | recordingCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height())); |
| 76 | recordingCanvas->translate(-fClip.fLeft, -fClip.fTop); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 77 | |
| 78 | // Note: in this use case we only render a picture to the deferred canvas |
| 79 | // but, more generally, clients will use arbitrary draw calls. |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame^] | 80 | recordingCanvas->drawPicture(fReconstitutedPicture); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 81 | |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 82 | fDisplayList = recorder.detach(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Robert Phillips | 6eb5cb9 | 2020-03-05 12:52:45 -0500 | [diff] [blame] | 85 | void DDLTileHelper::TileData::precompile(GrContext* context) { |
| 86 | SkASSERT(fDisplayList); |
| 87 | |
| 88 | SkDeferredDisplayList::ProgramIterator iter(context, fDisplayList.get()); |
| 89 | for (; !iter.done(); iter.next()) { |
| 90 | iter.compile(); |
| 91 | } |
| 92 | } |
| 93 | |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame^] | 94 | void DDLTileHelper::TileData::drawSKPDirectly(GrContext* context) { |
| 95 | SkASSERT(!fDisplayList && !fImage && fReconstitutedPicture); |
| 96 | |
| 97 | sk_sp<SkSurface> tileSurface = SkSurface::MakeRenderTarget(context, fCharacterization, |
| 98 | SkBudgeted::kYes); |
| 99 | if (tileSurface) { |
| 100 | SkCanvas* tileCanvas = tileSurface->getCanvas(); |
| 101 | |
| 102 | tileCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height())); |
| 103 | tileCanvas->translate(-fClip.fLeft, -fClip.fTop); |
| 104 | |
| 105 | tileCanvas->drawPicture(fReconstitutedPicture); |
| 106 | |
| 107 | fImage = tileSurface->makeImageSnapshot(); |
| 108 | } |
| 109 | } |
| 110 | |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 111 | void DDLTileHelper::TileData::draw(GrContext* context) { |
| 112 | SkASSERT(fDisplayList && !fImage); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 113 | |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 114 | sk_sp<SkSurface> tileSurface = SkSurface::MakeRenderTarget(context, fCharacterization, |
| 115 | SkBudgeted::kYes); |
| 116 | if (tileSurface) { |
| 117 | tileSurface->draw(fDisplayList.get()); |
| 118 | |
| 119 | fImage = tileSurface->makeImageSnapshot(); |
| 120 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 121 | } |
| 122 | |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 123 | // TODO: We should create a single DDL for the composition step and just add replaying it |
| 124 | // as the last GPU task |
| 125 | void DDLTileHelper::TileData::compose() { |
| 126 | SkASSERT(fDstSurface && fImage); |
| 127 | |
| 128 | SkCanvas* canvas = fDstSurface->getCanvas(); |
| 129 | canvas->save(); |
| 130 | canvas->clipRect(SkRect::Make(fClip)); |
Mike Reed | 3eaed8d | 2020-02-15 11:07:37 -0500 | [diff] [blame] | 131 | canvas->drawImage(fImage, fClip.fLeft, fClip.fTop); |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 132 | canvas->restore(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void DDLTileHelper::TileData::reset() { |
| 136 | // TODO: when DDLs are re-renderable we don't need to do this |
| 137 | fDisplayList = nullptr; |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame^] | 138 | fImage = nullptr; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 142 | |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 143 | DDLTileHelper::DDLTileHelper(sk_sp<SkSurface> dstSurface, |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 144 | const SkSurfaceCharacterization& dstChar, |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 145 | const SkIRect& viewport, |
| 146 | int numDivisions) |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 147 | : fNumDivisions(numDivisions) { |
| 148 | SkASSERT(fNumDivisions > 0); |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 149 | fTiles = new TileData[this->numTiles()]; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 150 | |
| 151 | int xTileSize = viewport.width()/fNumDivisions; |
| 152 | int yTileSize = viewport.height()/fNumDivisions; |
| 153 | |
| 154 | // Create the destination tiles |
| 155 | for (int y = 0, yOff = 0; y < fNumDivisions; ++y, yOff += yTileSize) { |
| 156 | int ySize = (y < fNumDivisions-1) ? yTileSize : viewport.height()-yOff; |
| 157 | |
| 158 | for (int x = 0, xOff = 0; x < fNumDivisions; ++x, xOff += xTileSize) { |
| 159 | int xSize = (x < fNumDivisions-1) ? xTileSize : viewport.width()-xOff; |
| 160 | |
| 161 | SkIRect clip = SkIRect::MakeXYWH(xOff, yOff, xSize, ySize); |
| 162 | |
| 163 | SkASSERT(viewport.contains(clip)); |
| 164 | |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 165 | fTiles[y*fNumDivisions+x].init(y*fNumDivisions+x, dstSurface, dstChar, clip); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void DDLTileHelper::createSKPPerTile(SkData* compressedPictureData, |
| 171 | const DDLPromiseImageHelper& helper) { |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 172 | for (int i = 0; i < this->numTiles(); ++i) { |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 173 | fTiles[i].createTileSpecificSKP(compressedPictureData, helper); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void DDLTileHelper::createDDLsInParallel() { |
| 178 | #if 1 |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 179 | SkTaskGroup().batch(this->numTiles(), [&](int i) { fTiles[i].createDDL(); }); |
Robert Phillips | f18c756 | 2018-06-13 09:01:36 -0400 | [diff] [blame] | 180 | SkTaskGroup().wait(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 181 | #else |
| 182 | // Use this code path to debug w/o threads |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame^] | 183 | for (int i = 0; i < this->numTiles(); ++i) { |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 184 | fTiles[i].createDDL(); |
| 185 | } |
| 186 | #endif |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 187 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 188 | |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 189 | // On the gpu thread: |
| 190 | // precompile any programs |
| 191 | // replay the DDL into a surface to make the tile image |
| 192 | // compose the tile image into the main canvas |
| 193 | static void do_gpu_stuff(GrContext* context, DDLTileHelper::TileData* tile) { |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 194 | |
| 195 | // TODO: schedule program compilation as their own tasks |
Robert Phillips | 6eb5cb9 | 2020-03-05 12:52:45 -0500 | [diff] [blame] | 196 | tile->precompile(context); |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 197 | |
| 198 | tile->draw(context); |
| 199 | |
| 200 | // TODO: we should actually have a separate DDL that does |
| 201 | // the final composition draw |
| 202 | tile->compose(); |
| 203 | } |
| 204 | |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 205 | // We expect to have more than one recording thread but just one gpu thread |
| 206 | void DDLTileHelper::kickOffThreadedWork(SkTaskGroup* recordingTaskGroup, |
| 207 | SkTaskGroup* gpuTaskGroup, |
| 208 | GrContext* gpuThreadContext) { |
| 209 | SkASSERT(recordingTaskGroup && gpuTaskGroup && gpuThreadContext); |
| 210 | |
| 211 | for (int i = 0; i < this->numTiles(); ++i) { |
| 212 | TileData* tile = &fTiles[i]; |
| 213 | |
| 214 | // On a recording thread: |
| 215 | // generate the tile's DDL |
| 216 | // schedule gpu-thread processing of the DDL |
| 217 | // Note: a finer grained approach would be add a scheduling task which would evaluate |
| 218 | // which DDLs were ready to be rendered based on their prerequisites |
| 219 | recordingTaskGroup->add([tile, gpuTaskGroup, gpuThreadContext]() { |
| 220 | tile->createDDL(); |
| 221 | |
| 222 | gpuTaskGroup->add([gpuThreadContext, tile]() { |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 223 | do_gpu_stuff(gpuThreadContext, tile); |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 224 | }); |
| 225 | }); |
| 226 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 227 | } |
| 228 | |
Robert Phillips | 6eb5cb9 | 2020-03-05 12:52:45 -0500 | [diff] [blame] | 229 | void DDLTileHelper::precompileAndDrawAllTiles(GrContext* context) { |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 230 | for (int i = 0; i < this->numTiles(); ++i) { |
Robert Phillips | 6eb5cb9 | 2020-03-05 12:52:45 -0500 | [diff] [blame] | 231 | fTiles[i].precompile(context); |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 232 | fTiles[i].draw(context); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 233 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 234 | } |
| 235 | |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame^] | 236 | void DDLTileHelper::interleaveDDLCreationAndDraw(GrContext* context) { |
| 237 | for (int i = 0; i < this->numTiles(); ++i) { |
| 238 | fTiles[i].createDDL(); |
| 239 | fTiles[i].draw(context); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void DDLTileHelper::drawAllTilesDirectly(GrContext* context) { |
| 244 | for (int i = 0; i < this->numTiles(); ++i) { |
| 245 | fTiles[i].drawSKPDirectly(context); |
| 246 | } |
| 247 | } |
| 248 | |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 249 | void DDLTileHelper::composeAllTiles() { |
| 250 | for (int i = 0; i < this->numTiles(); ++i) { |
| 251 | fTiles[i].compose(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | void DDLTileHelper::resetAllTiles() { |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 256 | for (int i = 0; i < this->numTiles(); ++i) { |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 257 | fTiles[i].reset(); |
| 258 | } |
| 259 | } |