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 | |
| 20 | DDLTileHelper::TileData::TileData(sk_sp<SkSurface> s, const SkIRect& clip) |
| 21 | : fSurface(std::move(s)) |
| 22 | , fClip(clip) { |
| 23 | SkAssertResult(fSurface->characterize(&fCharacterization)); |
| 24 | } |
| 25 | |
| 26 | void DDLTileHelper::TileData::createTileSpecificSKP(SkData* compressedPictureData, |
| 27 | const DDLPromiseImageHelper& helper) { |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 28 | SkASSERT(!fReconstitutedPicture); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 29 | |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 30 | // This is bending the DDLRecorder contract! The promise images in the SKP should be |
| 31 | // created by the same recorder used to create the matching DDL. |
| 32 | SkDeferredDisplayListRecorder recorder(fCharacterization); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 33 | |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 34 | fReconstitutedPicture = helper.reinflateSKP(&recorder, compressedPictureData, &fPromiseImages); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 35 | |
| 36 | std::unique_ptr<SkDeferredDisplayList> ddl = recorder.detach(); |
Robert Phillips | bfa76f2 | 2018-10-03 12:12:26 -0400 | [diff] [blame] | 37 | if (ddl->priv().numOpLists()) { |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 38 | // TODO: remove this once skbug.com/8424 is fixed. If the DDL resulting from the |
| 39 | // reinflation of the SKPs contains opLists that means some image subset operation |
| 40 | // created a draw. |
| 41 | fReconstitutedPicture.reset(); |
| 42 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void DDLTileHelper::TileData::createDDL() { |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 46 | SkASSERT(!fDisplayList); |
| 47 | |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 48 | SkDeferredDisplayListRecorder recorder(fCharacterization); |
| 49 | |
| 50 | // DDL TODO: the DDLRecorder's GrContext isn't initialized until getCanvas is called. |
| 51 | // Maybe set it up in the ctor? |
| 52 | SkCanvas* subCanvas = recorder.getCanvas(); |
| 53 | |
| 54 | // Because we cheated in createTileSpecificSKP and used the wrong DDLRecorder, the GrContext's |
| 55 | // stored in fReconstitutedPicture's promise images are incorrect. Patch them with the correct |
| 56 | // one now. |
| 57 | for (int i = 0; i < fPromiseImages.count(); ++i) { |
| 58 | GrContext* newContext = subCanvas->getGrContext(); |
| 59 | |
| 60 | if (fPromiseImages[i]->isTextureBacked()) { |
Jim Van Verth | 21bd60d | 2018-10-12 15:00:20 -0400 | [diff] [blame] | 61 | SkImage_GpuBase* gpuImage = (SkImage_GpuBase*) fPromiseImages[i].get(); |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 62 | gpuImage->resetContext(sk_ref_sp(newContext)); |
| 63 | } |
| 64 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 65 | |
| 66 | subCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height())); |
| 67 | subCanvas->translate(-fClip.fLeft, -fClip.fTop); |
| 68 | |
| 69 | // Note: in this use case we only render a picture to the deferred canvas |
| 70 | // but, more generally, clients will use arbitrary draw calls. |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 71 | if (fReconstitutedPicture) { |
| 72 | subCanvas->drawPicture(fReconstitutedPicture); |
| 73 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 74 | |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 75 | fDisplayList = recorder.detach(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void DDLTileHelper::TileData::draw() { |
| 79 | SkASSERT(fDisplayList); |
| 80 | |
| 81 | fSurface->draw(fDisplayList.get()); |
| 82 | } |
| 83 | |
| 84 | void DDLTileHelper::TileData::compose(SkCanvas* dst) { |
| 85 | sk_sp<SkImage> img = fSurface->makeImageSnapshot(); |
| 86 | dst->save(); |
| 87 | dst->clipRect(SkRect::Make(fClip)); |
| 88 | dst->drawImage(std::move(img), fClip.fLeft, fClip.fTop); |
| 89 | dst->restore(); |
| 90 | } |
| 91 | |
| 92 | void DDLTileHelper::TileData::reset() { |
| 93 | // TODO: when DDLs are re-renderable we don't need to do this |
| 94 | fDisplayList = nullptr; |
| 95 | } |
| 96 | |
| 97 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 98 | |
| 99 | DDLTileHelper::DDLTileHelper(SkCanvas* canvas, const SkIRect& viewport, int numDivisions) |
| 100 | : fNumDivisions(numDivisions) { |
| 101 | SkASSERT(fNumDivisions > 0); |
| 102 | fTiles.reserve(fNumDivisions*fNumDivisions); |
| 103 | |
| 104 | int xTileSize = viewport.width()/fNumDivisions; |
| 105 | int yTileSize = viewport.height()/fNumDivisions; |
| 106 | |
| 107 | // Create the destination tiles |
| 108 | for (int y = 0, yOff = 0; y < fNumDivisions; ++y, yOff += yTileSize) { |
| 109 | int ySize = (y < fNumDivisions-1) ? yTileSize : viewport.height()-yOff; |
| 110 | |
| 111 | for (int x = 0, xOff = 0; x < fNumDivisions; ++x, xOff += xTileSize) { |
| 112 | int xSize = (x < fNumDivisions-1) ? xTileSize : viewport.width()-xOff; |
| 113 | |
| 114 | SkIRect clip = SkIRect::MakeXYWH(xOff, yOff, xSize, ySize); |
| 115 | |
| 116 | SkASSERT(viewport.contains(clip)); |
| 117 | |
| 118 | SkImageInfo tileII = SkImageInfo::MakeN32Premul(xSize, ySize); |
| 119 | |
| 120 | sk_sp<SkSurface> tileSurface = canvas->makeSurface(tileII); |
| 121 | |
| 122 | // TODO: this is here to deal w/ a resource allocator bug (skbug.com/8007). If all |
| 123 | // the DDLs are flushed at the same time (w/o the composition draws) the allocator |
| 124 | // feels free to reuse the backing GrSurfaces! |
| 125 | tileSurface->flush(); |
| 126 | |
| 127 | fTiles.push_back(TileData(std::move(tileSurface), clip)); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void DDLTileHelper::createSKPPerTile(SkData* compressedPictureData, |
| 133 | const DDLPromiseImageHelper& helper) { |
| 134 | for (int i = 0; i < fTiles.count(); ++i) { |
| 135 | fTiles[i].createTileSpecificSKP(compressedPictureData, helper); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void DDLTileHelper::createDDLsInParallel() { |
| 140 | #if 1 |
Robert Phillips | f18c756 | 2018-06-13 09:01:36 -0400 | [diff] [blame] | 141 | SkTaskGroup().batch(fTiles.count(), [&](int i) { fTiles[i].createDDL(); }); |
| 142 | SkTaskGroup().wait(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 143 | #else |
| 144 | // Use this code path to debug w/o threads |
| 145 | for (int i = 0; i < fTiles.count(); ++i) { |
| 146 | fTiles[i].createDDL(); |
| 147 | } |
| 148 | #endif |
| 149 | |
| 150 | } |
| 151 | |
| 152 | void DDLTileHelper::drawAllTilesAndFlush(GrContext* context, bool flush) { |
| 153 | for (int i = 0; i < fTiles.count(); ++i) { |
| 154 | fTiles[i].draw(); |
| 155 | } |
| 156 | if (flush) { |
| 157 | context->flush(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void DDLTileHelper::composeAllTiles(SkCanvas* dstCanvas) { |
| 162 | for (int i = 0; i < fTiles.count(); ++i) { |
| 163 | fTiles[i].compose(dstCanvas); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void DDLTileHelper::resetAllTiles() { |
| 168 | for (int i = 0; i < fTiles.count(); ++i) { |
| 169 | fTiles[i].reset(); |
| 170 | } |
| 171 | } |