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, |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 22 | GrContext* context, |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 23 | const SkSurfaceCharacterization& dstSurfaceCharacterization, |
| 24 | const SkIRect& clip) { |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 25 | fID = id; |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 26 | fClip = clip; |
| 27 | |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 28 | fCharacterization = dstSurfaceCharacterization.createResized(clip.width(), clip.height()); |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 29 | SkASSERT(fCharacterization.isValid()); |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 30 | |
| 31 | GrBackendFormat backendFormat = context->defaultBackendFormat(fCharacterization.colorType(), |
| 32 | GrRenderable::kYes); |
| 33 | SkDEBUGCODE(const GrCaps* caps = context->priv().caps()); |
| 34 | SkASSERT(caps->isFormatTexturable(backendFormat)); |
| 35 | |
| 36 | fCallbackContext.reset(new PromiseImageCallbackContext(context, backendFormat)); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 37 | } |
| 38 | |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 39 | DDLTileHelper::TileData::~TileData() {} |
| 40 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 41 | void DDLTileHelper::TileData::createTileSpecificSKP(SkData* compressedPictureData, |
| 42 | const DDLPromiseImageHelper& helper) { |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 43 | SkASSERT(!fReconstitutedPicture); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 44 | |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 45 | // This is bending the DDLRecorder contract! The promise images in the SKP should be |
| 46 | // created by the same recorder used to create the matching DDL. |
| 47 | SkDeferredDisplayListRecorder recorder(fCharacterization); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 48 | |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 49 | fReconstitutedPicture = helper.reinflateSKP(&recorder, compressedPictureData, &fPromiseImages); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 50 | |
| 51 | std::unique_ptr<SkDeferredDisplayList> ddl = recorder.detach(); |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 52 | if (ddl->priv().numRenderTasks()) { |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 53 | // 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] | 54 | // reinflation of the SKPs contains opsTasks that means some image subset operation |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 55 | // created a draw. |
| 56 | fReconstitutedPicture.reset(); |
| 57 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void DDLTileHelper::TileData::createDDL() { |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame] | 61 | SkASSERT(!fDisplayList && fReconstitutedPicture); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 62 | |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 63 | SkDeferredDisplayListRecorder recorder(fCharacterization); |
| 64 | |
| 65 | // DDL TODO: the DDLRecorder's GrContext isn't initialized until getCanvas is called. |
| 66 | // Maybe set it up in the ctor? |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame] | 67 | SkCanvas* recordingCanvas = recorder.getCanvas(); |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 68 | |
| 69 | // Because we cheated in createTileSpecificSKP and used the wrong DDLRecorder, the GrContext's |
| 70 | // stored in fReconstitutedPicture's promise images are incorrect. Patch them with the correct |
| 71 | // one now. |
| 72 | for (int i = 0; i < fPromiseImages.count(); ++i) { |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame] | 73 | GrContext* newContext = recordingCanvas->getGrContext(); |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 74 | |
| 75 | if (fPromiseImages[i]->isTextureBacked()) { |
Jim Van Verth | 21bd60d | 2018-10-12 15:00:20 -0400 | [diff] [blame] | 76 | SkImage_GpuBase* gpuImage = (SkImage_GpuBase*) fPromiseImages[i].get(); |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 77 | gpuImage->resetContext(sk_ref_sp(newContext)); |
| 78 | } |
| 79 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 80 | |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame] | 81 | recordingCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height())); |
| 82 | recordingCanvas->translate(-fClip.fLeft, -fClip.fTop); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 83 | |
| 84 | // Note: in this use case we only render a picture to the deferred canvas |
| 85 | // but, more generally, clients will use arbitrary draw calls. |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame] | 86 | recordingCanvas->drawPicture(fReconstitutedPicture); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 87 | |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 88 | fDisplayList = recorder.detach(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 91 | void DDLTileHelper::createComposeDDL() { |
| 92 | SkASSERT(!fComposeDDL); |
| 93 | |
| 94 | SkDeferredDisplayListRecorder recorder(fDstCharacterization); |
| 95 | |
| 96 | SkCanvas* recordingCanvas = recorder.getCanvas(); |
| 97 | |
| 98 | for (int i = 0; i < this->numTiles(); ++i) { |
| 99 | TileData* tile = &fTiles[i]; |
| 100 | |
| 101 | sk_sp<SkImage> promiseImage = tile->makePromiseImage(&recorder); |
| 102 | |
| 103 | SkIRect clipRect = tile->clipRect(); |
| 104 | |
| 105 | SkASSERT(clipRect.width() == promiseImage->width() && |
| 106 | clipRect.height() == promiseImage->height()); |
| 107 | |
| 108 | recordingCanvas->drawImage(promiseImage, clipRect.fLeft, clipRect.fTop); |
| 109 | } |
| 110 | |
| 111 | fComposeDDL = recorder.detach(); |
Robert Phillips | 889d613 | 2020-06-16 11:11:33 -0400 | [diff] [blame^] | 112 | SkASSERT(fComposeDDL); |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Robert Phillips | 6eb5cb9 | 2020-03-05 12:52:45 -0500 | [diff] [blame] | 115 | void DDLTileHelper::TileData::precompile(GrContext* context) { |
| 116 | SkASSERT(fDisplayList); |
| 117 | |
| 118 | SkDeferredDisplayList::ProgramIterator iter(context, fDisplayList.get()); |
| 119 | for (; !iter.done(); iter.next()) { |
| 120 | iter.compile(); |
| 121 | } |
| 122 | } |
| 123 | |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 124 | sk_sp<SkSurface> DDLTileHelper::TileData::makeWrappedTileDest(GrContext* context) { |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 125 | SkASSERT(fCallbackContext && fCallbackContext->promiseImageTexture()); |
| 126 | |
| 127 | auto promiseImageTexture = fCallbackContext->promiseImageTexture(); |
| 128 | if (!promiseImageTexture->backendTexture().isValid()) { |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 129 | return nullptr; |
| 130 | } |
Robert Phillips | 7ae9d2f | 2020-04-15 10:58:15 -0400 | [diff] [blame] | 131 | |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 132 | // Here we are, unfortunately, aliasing the backend texture held by the SkPromiseImageTexture. |
| 133 | // Both the tile's destination surface and the promise image used to draw the tile will be |
| 134 | // backed by the same backendTexture - unbeknownst to Ganesh. |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 135 | return SkSurface::MakeFromBackendTexture(context, |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 136 | promiseImageTexture->backendTexture(), |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 137 | fCharacterization.origin(), |
| 138 | fCharacterization.sampleCount(), |
| 139 | fCharacterization.colorType(), |
| 140 | fCharacterization.refColorSpace(), |
| 141 | &fCharacterization.surfaceProps()); |
| 142 | } |
| 143 | |
| 144 | void DDLTileHelper::TileData::drawSKPDirectly(GrContext* context) { |
| 145 | SkASSERT(!fDisplayList && !fTileSurface && fReconstitutedPicture); |
| 146 | |
| 147 | fTileSurface = this->makeWrappedTileDest(context); |
| 148 | if (fTileSurface) { |
| 149 | SkCanvas* tileCanvas = fTileSurface->getCanvas(); |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame] | 150 | |
| 151 | tileCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height())); |
| 152 | tileCanvas->translate(-fClip.fLeft, -fClip.fTop); |
| 153 | |
| 154 | tileCanvas->drawPicture(fReconstitutedPicture); |
| 155 | |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 156 | // We can't snap an image here bc, since we're using wrapped backend textures for the |
| 157 | // surfaces, that would incur a copy. |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 161 | void DDLTileHelper::TileData::draw(GrContext* context) { |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 162 | SkASSERT(fDisplayList && !fTileSurface); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 163 | |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 164 | // The tile's surface needs to be held until after the DDL is flushed bc the DDL doesn't take |
| 165 | // a ref on its destination proxy. |
| 166 | // TODO: make the DDL (or probably the drawing manager) take a ref on the destination proxy |
| 167 | // (maybe in GrDrawingManager::addDDLTarget). |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 168 | fTileSurface = this->makeWrappedTileDest(context); |
| 169 | if (fTileSurface) { |
| 170 | fTileSurface->draw(fDisplayList.get()); |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 171 | |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 172 | // We can't snap an image here bc, since we're using wrapped backend textures for the |
| 173 | // surfaces, that would incur a copy. |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 174 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 177 | void DDLTileHelper::TileData::reset() { |
| 178 | // TODO: when DDLs are re-renderable we don't need to do this |
| 179 | fDisplayList = nullptr; |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 180 | |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 181 | fTileSurface = nullptr; |
| 182 | } |
| 183 | |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 184 | sk_sp<SkImage> DDLTileHelper::TileData::makePromiseImage(SkDeferredDisplayListRecorder* recorder) { |
| 185 | SkASSERT(fCallbackContext); |
| 186 | |
| 187 | // The promise image gets a ref on the promise callback context |
| 188 | sk_sp<SkImage> promiseImage = recorder->makePromiseTexture( |
| 189 | fCallbackContext->backendFormat(), |
| 190 | fClip.width(), |
| 191 | fClip.height(), |
| 192 | GrMipMapped::kNo, |
| 193 | GrSurfaceOrigin::kBottomLeft_GrSurfaceOrigin, |
| 194 | fCharacterization.colorType(), |
| 195 | kPremul_SkAlphaType, |
| 196 | fCharacterization.refColorSpace(), |
| 197 | PromiseImageCallbackContext::PromiseImageFulfillProc, |
| 198 | PromiseImageCallbackContext::PromiseImageReleaseProc, |
| 199 | PromiseImageCallbackContext::PromiseImageDoneProc, |
| 200 | (void*)this->refCallbackContext().release(), |
| 201 | SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew); |
| 202 | fCallbackContext->wasAddedToImage(); |
| 203 | |
| 204 | return promiseImage; |
| 205 | } |
| 206 | |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 207 | void DDLTileHelper::TileData::CreateBackendTexture(GrContext* context, TileData* tile) { |
| 208 | SkASSERT(context->priv().asDirectContext()); |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 209 | SkASSERT(tile->fCallbackContext && !tile->fCallbackContext->promiseImageTexture()); |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 210 | |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 211 | GrBackendTexture beTex = context->createBackendTexture(tile->fCharacterization); |
| 212 | tile->fCallbackContext->setBackendTexture(beTex); |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void DDLTileHelper::TileData::DeleteBackendTexture(GrContext* context, TileData* tile) { |
| 216 | SkASSERT(context->priv().asDirectContext()); |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 217 | SkASSERT(tile->fCallbackContext); |
| 218 | |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 219 | // TODO: it seems that, on the Linux bots, backend texture creation is failing |
| 220 | // a lot (skbug.com/10142) |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 221 | SkASSERT(!tile->fCallbackContext->promiseImageTexture() || |
| 222 | tile->fCallbackContext->promiseImageTexture()->backendTexture().isValid()); |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 223 | |
| 224 | tile->fTileSurface = nullptr; |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 225 | |
| 226 | SkASSERT(tile->fCallbackContext->unique()); |
| 227 | tile->fCallbackContext.reset(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 231 | |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 232 | DDLTileHelper::DDLTileHelper(GrContext* context, |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 233 | const SkSurfaceCharacterization& dstChar, |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 234 | const SkIRect& viewport, |
| 235 | int numDivisions) |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 236 | : fNumDivisions(numDivisions) |
Adlai Holler | 8e82123 | 2020-05-11 13:33:59 -0400 | [diff] [blame] | 237 | , fTiles(numDivisions * numDivisions) |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 238 | , fDstCharacterization(dstChar) { |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 239 | SkASSERT(fNumDivisions > 0); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 240 | |
| 241 | int xTileSize = viewport.width()/fNumDivisions; |
| 242 | int yTileSize = viewport.height()/fNumDivisions; |
| 243 | |
| 244 | // Create the destination tiles |
| 245 | for (int y = 0, yOff = 0; y < fNumDivisions; ++y, yOff += yTileSize) { |
| 246 | int ySize = (y < fNumDivisions-1) ? yTileSize : viewport.height()-yOff; |
| 247 | |
| 248 | for (int x = 0, xOff = 0; x < fNumDivisions; ++x, xOff += xTileSize) { |
| 249 | int xSize = (x < fNumDivisions-1) ? xTileSize : viewport.width()-xOff; |
| 250 | |
| 251 | SkIRect clip = SkIRect::MakeXYWH(xOff, yOff, xSize, ySize); |
| 252 | |
| 253 | SkASSERT(viewport.contains(clip)); |
| 254 | |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 255 | fTiles[y*fNumDivisions+x].init(y*fNumDivisions+x, context, dstChar, clip); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void DDLTileHelper::createSKPPerTile(SkData* compressedPictureData, |
| 261 | const DDLPromiseImageHelper& helper) { |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 262 | for (int i = 0; i < this->numTiles(); ++i) { |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 263 | fTiles[i].createTileSpecificSKP(compressedPictureData, helper); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | void DDLTileHelper::createDDLsInParallel() { |
| 268 | #if 1 |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 269 | SkTaskGroup().batch(this->numTiles(), [&](int i) { fTiles[i].createDDL(); }); |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 270 | SkTaskGroup().add([this]{ this->createComposeDDL(); }); |
Robert Phillips | f18c756 | 2018-06-13 09:01:36 -0400 | [diff] [blame] | 271 | SkTaskGroup().wait(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 272 | #else |
| 273 | // Use this code path to debug w/o threads |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame] | 274 | for (int i = 0; i < this->numTiles(); ++i) { |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 275 | fTiles[i].createDDL(); |
| 276 | } |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 277 | this->createComposeDDL(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 278 | #endif |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 279 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 280 | |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 281 | // On the gpu thread: |
| 282 | // precompile any programs |
| 283 | // replay the DDL into a surface to make the tile image |
| 284 | // compose the tile image into the main canvas |
| 285 | static void do_gpu_stuff(GrContext* context, DDLTileHelper::TileData* tile) { |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 286 | |
| 287 | // TODO: schedule program compilation as their own tasks |
Robert Phillips | 6eb5cb9 | 2020-03-05 12:52:45 -0500 | [diff] [blame] | 288 | tile->precompile(context); |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 289 | |
| 290 | tile->draw(context); |
Robert Phillips | 5dbcca5 | 2020-05-29 10:41:33 -0400 | [diff] [blame] | 291 | |
| 292 | // TODO: remove this flush once DDLs are reffed by the drawing manager |
| 293 | context->flushAndSubmit(); |
| 294 | |
| 295 | tile->dropDDL(); |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 296 | } |
| 297 | |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 298 | // We expect to have more than one recording thread but just one gpu thread |
| 299 | void DDLTileHelper::kickOffThreadedWork(SkTaskGroup* recordingTaskGroup, |
| 300 | SkTaskGroup* gpuTaskGroup, |
| 301 | GrContext* gpuThreadContext) { |
| 302 | SkASSERT(recordingTaskGroup && gpuTaskGroup && gpuThreadContext); |
| 303 | |
| 304 | for (int i = 0; i < this->numTiles(); ++i) { |
| 305 | TileData* tile = &fTiles[i]; |
| 306 | |
| 307 | // On a recording thread: |
| 308 | // generate the tile's DDL |
| 309 | // schedule gpu-thread processing of the DDL |
| 310 | // Note: a finer grained approach would be add a scheduling task which would evaluate |
| 311 | // which DDLs were ready to be rendered based on their prerequisites |
| 312 | recordingTaskGroup->add([tile, gpuTaskGroup, gpuThreadContext]() { |
| 313 | tile->createDDL(); |
| 314 | |
| 315 | gpuTaskGroup->add([gpuThreadContext, tile]() { |
Robert Phillips | 7b0ed55 | 2020-02-20 12:45:19 -0500 | [diff] [blame] | 316 | do_gpu_stuff(gpuThreadContext, tile); |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 317 | }); |
| 318 | }); |
| 319 | } |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 320 | |
| 321 | recordingTaskGroup->add([this] { this->createComposeDDL(); }); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 322 | } |
| 323 | |
Robert Phillips | 5dbcca5 | 2020-05-29 10:41:33 -0400 | [diff] [blame] | 324 | // Only called from ViaDDL |
Robert Phillips | 6eb5cb9 | 2020-03-05 12:52:45 -0500 | [diff] [blame] | 325 | void DDLTileHelper::precompileAndDrawAllTiles(GrContext* context) { |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 326 | for (int i = 0; i < this->numTiles(); ++i) { |
Robert Phillips | 6eb5cb9 | 2020-03-05 12:52:45 -0500 | [diff] [blame] | 327 | fTiles[i].precompile(context); |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 328 | fTiles[i].draw(context); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 329 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 330 | } |
| 331 | |
Robert Phillips | 5dbcca5 | 2020-05-29 10:41:33 -0400 | [diff] [blame] | 332 | // Only called from skpbench |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame] | 333 | void DDLTileHelper::interleaveDDLCreationAndDraw(GrContext* context) { |
| 334 | for (int i = 0; i < this->numTiles(); ++i) { |
| 335 | fTiles[i].createDDL(); |
| 336 | fTiles[i].draw(context); |
| 337 | } |
| 338 | } |
| 339 | |
Robert Phillips | 5dbcca5 | 2020-05-29 10:41:33 -0400 | [diff] [blame] | 340 | // Only called from skpbench |
Robert Phillips | 24a8e9e | 2020-03-06 20:26:28 +0000 | [diff] [blame] | 341 | void DDLTileHelper::drawAllTilesDirectly(GrContext* context) { |
| 342 | for (int i = 0; i < this->numTiles(); ++i) { |
| 343 | fTiles[i].drawSKPDirectly(context); |
| 344 | } |
| 345 | } |
| 346 | |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 347 | void DDLTileHelper::dropCallbackContexts() { |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 348 | for (int i = 0; i < this->numTiles(); ++i) { |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 349 | fTiles[i].dropCallbackContext(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | void DDLTileHelper::resetAllTiles() { |
Robert Phillips | a865a3a | 2020-02-14 10:49:39 -0500 | [diff] [blame] | 354 | for (int i = 0; i < this->numTiles(); ++i) { |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 355 | fTiles[i].reset(); |
| 356 | } |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 357 | fComposeDDL.reset(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 358 | } |
Robert Phillips | 8472a3d | 2020-04-16 16:27:45 -0400 | [diff] [blame] | 359 | |
| 360 | void DDLTileHelper::createBackendTextures(SkTaskGroup* taskGroup, GrContext* context) { |
| 361 | SkASSERT(context->priv().asDirectContext()); |
| 362 | |
| 363 | if (taskGroup) { |
| 364 | for (int i = 0; i < this->numTiles(); ++i) { |
| 365 | TileData* tile = &fTiles[i]; |
| 366 | |
| 367 | taskGroup->add([context, tile]() { TileData::CreateBackendTexture(context, tile); }); |
| 368 | } |
| 369 | } else { |
| 370 | for (int i = 0; i < this->numTiles(); ++i) { |
| 371 | TileData::CreateBackendTexture(context, &fTiles[i]); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | void DDLTileHelper::deleteBackendTextures(SkTaskGroup* taskGroup, GrContext* context) { |
| 377 | SkASSERT(context->priv().asDirectContext()); |
| 378 | |
| 379 | if (taskGroup) { |
| 380 | for (int i = 0; i < this->numTiles(); ++i) { |
| 381 | TileData* tile = &fTiles[i]; |
| 382 | |
| 383 | taskGroup->add([context, tile]() { TileData::DeleteBackendTexture(context, tile); }); |
| 384 | } |
| 385 | } else { |
| 386 | for (int i = 0; i < this->numTiles(); ++i) { |
| 387 | TileData::DeleteBackendTexture(context, &fTiles[i]); |
| 388 | } |
| 389 | } |
| 390 | } |