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