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