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/DDLPromiseImageHelper.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/SkDeferredDisplayListRecorder.h" |
Robert Phillips | 6694440 | 2019-09-30 13:21:25 -0400 | [diff] [blame] | 11 | #include "include/core/SkPicture.h" |
| 12 | #include "include/core/SkSerialProcs.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkYUVAIndex.h" |
| 14 | #include "include/core/SkYUVASizeInfo.h" |
| 15 | #include "include/gpu/GrContext.h" |
| 16 | #include "src/core/SkCachedData.h" |
Robert Phillips | 4508eb9 | 2020-04-15 15:54:34 -0400 | [diff] [blame] | 17 | #include "src/core/SkMipMap.h" |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 18 | #include "src/core/SkTaskGroup.h" |
| 19 | #include "src/gpu/GrContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/image/SkImage_Base.h" |
| 21 | #include "src/image/SkImage_GpuYUVA.h" |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 22 | |
Robert Phillips | f95e2f4 | 2020-04-17 16:20:55 -0400 | [diff] [blame] | 23 | DDLPromiseImageHelper::PromiseImageInfo::PromiseImageInfo(int index, |
| 24 | uint32_t originalUniqueID, |
| 25 | const SkImageInfo& ii) |
| 26 | : fIndex(index) |
| 27 | , fOriginalUniqueID(originalUniqueID) |
| 28 | , fImageInfo(ii) { |
| 29 | } |
| 30 | |
| 31 | DDLPromiseImageHelper::PromiseImageInfo::PromiseImageInfo(PromiseImageInfo&& other) |
| 32 | : fIndex(other.fIndex) |
| 33 | , fOriginalUniqueID(other.fOriginalUniqueID) |
| 34 | , fImageInfo(other.fImageInfo) |
| 35 | , fBaseLevel(other.fBaseLevel) |
| 36 | , fMipLevels(std::move(other.fMipLevels)) |
| 37 | , fYUVData(std::move(other.fYUVData)) |
| 38 | , fYUVColorSpace(other.fYUVColorSpace) { |
| 39 | memcpy(fYUVAIndices, other.fYUVAIndices, sizeof(fYUVAIndices)); |
| 40 | for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) { |
| 41 | fYUVPlanes[i] = other.fYUVPlanes[i]; |
| 42 | fCallbackContexts[i] = std::move(other.fCallbackContexts[i]); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | DDLPromiseImageHelper::PromiseImageInfo::~PromiseImageInfo() {} |
| 47 | |
| 48 | const std::unique_ptr<SkPixmap[]> DDLPromiseImageHelper::PromiseImageInfo::normalMipLevels() const { |
| 49 | SkASSERT(!this->isYUV()); |
| 50 | std::unique_ptr<SkPixmap[]> pixmaps(new SkPixmap[this->numMipLevels()]); |
| 51 | pixmaps[0] = fBaseLevel.pixmap(); |
| 52 | if (fMipLevels) { |
| 53 | for (int i = 0; i < fMipLevels->countLevels(); ++i) { |
| 54 | SkMipMap::Level mipLevel; |
| 55 | fMipLevels->getLevel(i, &mipLevel); |
| 56 | pixmaps[i+1] = mipLevel.fPixmap; |
| 57 | } |
| 58 | } |
| 59 | return pixmaps; |
| 60 | } |
| 61 | |
| 62 | int DDLPromiseImageHelper::PromiseImageInfo::numMipLevels() const { |
| 63 | SkASSERT(!this->isYUV()); |
| 64 | return fMipLevels ? fMipLevels->countLevels()+1 : 1; |
| 65 | } |
| 66 | |
| 67 | void DDLPromiseImageHelper::PromiseImageInfo::setMipLevels(const SkBitmap& baseLevel, |
| 68 | std::unique_ptr<SkMipMap> mipLevels) { |
| 69 | fBaseLevel = baseLevel; |
| 70 | fMipLevels = std::move(mipLevels); |
| 71 | } |
| 72 | |
| 73 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 74 | PromiseImageCallbackContext::~PromiseImageCallbackContext() { |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 75 | SkASSERT(fDoneCnt == fNumImages); |
| 76 | SkASSERT(!fUnreleasedFulfills); |
| 77 | SkASSERT(fTotalReleases == fTotalFulfills); |
| 78 | SkASSERT(!fTotalFulfills || fDoneCnt); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 79 | |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 80 | if (fPromiseImageTexture) { |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 81 | fContext->deleteBackendTexture(fPromiseImageTexture->backendTexture()); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 85 | void PromiseImageCallbackContext::setBackendTexture(const GrBackendTexture& backendTexture) { |
Brian Salomon | 7d88f31 | 2019-02-28 10:03:03 -0500 | [diff] [blame] | 86 | SkASSERT(!fPromiseImageTexture); |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 87 | SkASSERT(fBackendFormat == backendTexture.getBackendFormat()); |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 88 | fPromiseImageTexture = SkPromiseImageTexture::Make(backendTexture); |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 91 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 92 | |
| 93 | sk_sp<SkData> DDLPromiseImageHelper::deflateSKP(const SkPicture* inputPicture) { |
| 94 | SkSerialProcs procs; |
| 95 | |
| 96 | procs.fImageCtx = this; |
| 97 | procs.fImageProc = [](SkImage* image, void* ctx) -> sk_sp<SkData> { |
| 98 | auto helper = static_cast<DDLPromiseImageHelper*>(ctx); |
| 99 | |
| 100 | int id = helper->findOrDefineImage(image); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 101 | |
Robert Phillips | 6bad705 | 2019-12-16 15:09:57 -0500 | [diff] [blame] | 102 | // Even if 'id' is invalid (i.e., -1) write it to the SKP |
| 103 | return SkData::MakeWithCopy(&id, sizeof(id)); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | return inputPicture->serialize(&procs); |
| 107 | } |
| 108 | |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 109 | static GrBackendTexture create_yuva_texture(GrContext* context, const SkPixmap& pm, |
Jim Van Verth | 60ac5d0 | 2018-12-06 13:11:53 -0500 | [diff] [blame] | 110 | const SkYUVAIndex yuvaIndices[4], int texIndex) { |
| 111 | SkASSERT(texIndex >= 0 && texIndex <= 3); |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 112 | |
| 113 | #ifdef SK_DEBUG |
Jim Van Verth | 60ac5d0 | 2018-12-06 13:11:53 -0500 | [diff] [blame] | 114 | int channelCount = 0; |
| 115 | for (int i = 0; i < SkYUVAIndex::kIndexCount; ++i) { |
| 116 | if (yuvaIndices[i].fIndex == texIndex) { |
| 117 | ++channelCount; |
| 118 | } |
| 119 | } |
Jim Van Verth | 60ac5d0 | 2018-12-06 13:11:53 -0500 | [diff] [blame] | 120 | if (2 == channelCount) { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 121 | SkASSERT(kR8G8_unorm_SkColorType == pm.colorType()); |
Jim Van Verth | 60ac5d0 | 2018-12-06 13:11:53 -0500 | [diff] [blame] | 122 | } |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 123 | #endif |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 124 | bool finishedBECreate = false; |
| 125 | auto markFinished = [](void* context) { |
| 126 | *(bool*)context = true; |
| 127 | }; |
| 128 | auto beTex = context->createBackendTexture(&pm, 1, GrRenderable::kNo, GrProtected::kNo, |
| 129 | markFinished, &finishedBECreate); |
| 130 | if (beTex.isValid()) { |
Greg Daniel | b577655 | 2020-06-19 20:21:08 -0400 | [diff] [blame] | 131 | context->submit(); |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 132 | while (!finishedBECreate) { |
| 133 | context->checkAsyncWorkCompletion(); |
| 134 | } |
| 135 | } |
| 136 | return beTex; |
Jim Van Verth | 60ac5d0 | 2018-12-06 13:11:53 -0500 | [diff] [blame] | 137 | } |
| 138 | |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 139 | /* |
| 140 | * Create backend textures and upload data to them for all the textures required to satisfy |
| 141 | * a single promise image. |
| 142 | * For YUV textures this will result in up to 4 actual textures. |
| 143 | */ |
| 144 | void DDLPromiseImageHelper::CreateBETexturesForPromiseImage(GrContext* context, |
| 145 | PromiseImageInfo* info) { |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 146 | SkASSERT(context->asDirectContext()); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 147 | |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 148 | if (info->isYUV()) { |
| 149 | int numPixmaps; |
| 150 | SkAssertResult(SkYUVAIndex::AreValidIndices(info->yuvaIndices(), &numPixmaps)); |
| 151 | for (int j = 0; j < numPixmaps; ++j) { |
| 152 | const SkPixmap& yuvPixmap = info->yuvPixmap(j); |
| 153 | |
| 154 | PromiseImageCallbackContext* callbackContext = info->callbackContext(j); |
| 155 | SkASSERT(callbackContext); |
| 156 | |
Robert Phillips | 4508eb9 | 2020-04-15 15:54:34 -0400 | [diff] [blame] | 157 | // DDL TODO: what should we do with mipmapped YUV images |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 158 | callbackContext->setBackendTexture(create_yuva_texture(context, yuvPixmap, |
| 159 | info->yuvaIndices(), j)); |
| 160 | SkASSERT(callbackContext->promiseImageTexture()); |
| 161 | } |
| 162 | } else { |
| 163 | PromiseImageCallbackContext* callbackContext = info->callbackContext(0); |
| 164 | if (!callbackContext) { |
| 165 | // This texture would've been too large to fit on the GPU |
| 166 | return; |
| 167 | } |
| 168 | |
Robert Phillips | f95e2f4 | 2020-04-17 16:20:55 -0400 | [diff] [blame] | 169 | std::unique_ptr<SkPixmap[]> mipLevels = info->normalMipLevels(); |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 170 | |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 171 | bool finishedBECreate = false; |
| 172 | auto markFinished = [](void* context) { |
| 173 | *(bool*)context = true; |
| 174 | }; |
| 175 | auto backendTex = context->createBackendTexture(mipLevels.get(), info->numMipLevels(), |
| 176 | GrRenderable::kNo, GrProtected::kNo, |
| 177 | markFinished, &finishedBECreate); |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 178 | SkASSERT(backendTex.isValid()); |
Greg Daniel | b577655 | 2020-06-19 20:21:08 -0400 | [diff] [blame] | 179 | context->submit(); |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 180 | while (!finishedBECreate) { |
| 181 | context->checkAsyncWorkCompletion(); |
| 182 | } |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 183 | |
| 184 | callbackContext->setBackendTexture(backendTex); |
| 185 | } |
| 186 | } |
| 187 | |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 188 | void DDLPromiseImageHelper::DeleteBETexturesForPromiseImage(GrContext* context, |
| 189 | PromiseImageInfo* info) { |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 190 | SkASSERT(context->asDirectContext()); |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 191 | |
| 192 | if (info->isYUV()) { |
| 193 | int numPixmaps; |
| 194 | SkAssertResult(SkYUVAIndex::AreValidIndices(info->yuvaIndices(), &numPixmaps)); |
| 195 | for (int j = 0; j < numPixmaps; ++j) { |
| 196 | PromiseImageCallbackContext* callbackContext = info->callbackContext(j); |
| 197 | SkASSERT(callbackContext); |
| 198 | |
| 199 | callbackContext->destroyBackendTexture(); |
| 200 | SkASSERT(!callbackContext->promiseImageTexture()); |
| 201 | } |
| 202 | } else { |
| 203 | PromiseImageCallbackContext* callbackContext = info->callbackContext(0); |
| 204 | if (!callbackContext) { |
| 205 | // This texture would've been too large to fit on the GPU |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | callbackContext->destroyBackendTexture(); |
| 210 | SkASSERT(!callbackContext->promiseImageTexture()); |
| 211 | } |
| 212 | } |
| 213 | |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 214 | void DDLPromiseImageHelper::createCallbackContexts(GrContext* context) { |
| 215 | const GrCaps* caps = context->priv().caps(); |
| 216 | const int maxDimension = caps->maxTextureSize(); |
| 217 | |
| 218 | for (int i = 0; i < fImageInfo.count(); ++i) { |
| 219 | PromiseImageInfo& info = fImageInfo[i]; |
| 220 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 221 | if (info.isYUV()) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 222 | int numPixmaps; |
| 223 | SkAssertResult(SkYUVAIndex::AreValidIndices(info.yuvaIndices(), &numPixmaps)); |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 224 | |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 225 | for (int j = 0; j < numPixmaps; ++j) { |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 226 | const SkPixmap& yuvPixmap = info.yuvPixmap(j); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 227 | |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 228 | GrBackendFormat backendFormat = context->defaultBackendFormat(yuvPixmap.colorType(), |
| 229 | GrRenderable::kNo); |
| 230 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 231 | sk_sp<PromiseImageCallbackContext> callbackContext( |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 232 | new PromiseImageCallbackContext(context, backendFormat)); |
Jim Van Verth | 60ac5d0 | 2018-12-06 13:11:53 -0500 | [diff] [blame] | 233 | |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 234 | info.setCallbackContext(j, std::move(callbackContext)); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 235 | } |
| 236 | } else { |
Robert Phillips | f95e2f4 | 2020-04-17 16:20:55 -0400 | [diff] [blame] | 237 | const SkBitmap& baseLevel = info.baseLevel(); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 238 | |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 239 | // TODO: explicitly mark the PromiseImageInfo as too big and check in uploadAllToGPU |
Robert Phillips | f95e2f4 | 2020-04-17 16:20:55 -0400 | [diff] [blame] | 240 | if (maxDimension < std::max(baseLevel.width(), baseLevel.height())) { |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 241 | // This won't fit on the GPU. Fallback to a raster-backed image per tile. |
| 242 | continue; |
| 243 | } |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 244 | |
Robert Phillips | f95e2f4 | 2020-04-17 16:20:55 -0400 | [diff] [blame] | 245 | GrBackendFormat backendFormat = context->defaultBackendFormat(baseLevel.colorType(), |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 246 | GrRenderable::kNo); |
| 247 | if (!caps->isFormatTexturable(backendFormat)) { |
| 248 | continue; |
| 249 | } |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 250 | |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 251 | sk_sp<PromiseImageCallbackContext> callbackContext( |
| 252 | new PromiseImageCallbackContext(context, backendFormat)); |
| 253 | |
| 254 | info.setCallbackContext(0, std::move(callbackContext)); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void DDLPromiseImageHelper::uploadAllToGPU(SkTaskGroup* taskGroup, GrContext* context) { |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 260 | SkASSERT(context->asDirectContext()); |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 261 | |
| 262 | if (taskGroup) { |
| 263 | for (int i = 0; i < fImageInfo.count(); ++i) { |
| 264 | PromiseImageInfo* info = &fImageInfo[i]; |
| 265 | |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 266 | taskGroup->add([context, info]() { CreateBETexturesForPromiseImage(context, info); }); |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 267 | } |
| 268 | } else { |
| 269 | for (int i = 0; i < fImageInfo.count(); ++i) { |
| 270 | CreateBETexturesForPromiseImage(context, &fImageInfo[i]); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 271 | } |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 272 | } |
| 273 | } |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 274 | |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 275 | void DDLPromiseImageHelper::deleteAllFromGPU(SkTaskGroup* taskGroup, GrContext* context) { |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 276 | SkASSERT(context->asDirectContext()); |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 277 | |
| 278 | if (taskGroup) { |
| 279 | for (int i = 0; i < fImageInfo.count(); ++i) { |
| 280 | PromiseImageInfo* info = &fImageInfo[i]; |
| 281 | |
| 282 | taskGroup->add([context, info]() { DeleteBETexturesForPromiseImage(context, info); }); |
| 283 | } |
| 284 | } else { |
| 285 | for (int i = 0; i < fImageInfo.count(); ++i) { |
| 286 | DeleteBETexturesForPromiseImage(context, &fImageInfo[i]); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 291 | sk_sp<SkPicture> DDLPromiseImageHelper::reinflateSKP( |
| 292 | SkDeferredDisplayListRecorder* recorder, |
| 293 | SkData* compressedPictureData, |
| 294 | SkTArray<sk_sp<SkImage>>* promiseImages) const { |
| 295 | PerRecorderContext perRecorderContext { recorder, this, promiseImages }; |
| 296 | |
| 297 | SkDeserialProcs procs; |
| 298 | procs.fImageCtx = (void*) &perRecorderContext; |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 299 | procs.fImageProc = CreatePromiseImages; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 300 | |
| 301 | return SkPicture::MakeFromData(compressedPictureData, &procs); |
| 302 | } |
| 303 | |
| 304 | // This generates promise images to replace the indices in the compressed picture. This |
| 305 | // reconstitution is performed separately in each thread so we end up with multiple |
| 306 | // promise images referring to the same GrBackendTexture. |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 307 | sk_sp<SkImage> DDLPromiseImageHelper::CreatePromiseImages(const void* rawData, |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 308 | size_t length, void* ctxIn) { |
| 309 | PerRecorderContext* perRecorderContext = static_cast<PerRecorderContext*>(ctxIn); |
| 310 | const DDLPromiseImageHelper* helper = perRecorderContext->fHelper; |
| 311 | SkDeferredDisplayListRecorder* recorder = perRecorderContext->fRecorder; |
| 312 | |
| 313 | SkASSERT(length == sizeof(int)); |
| 314 | |
| 315 | const int* indexPtr = static_cast<const int*>(rawData); |
Robert Phillips | 6bad705 | 2019-12-16 15:09:57 -0500 | [diff] [blame] | 316 | if (!helper->isValidID(*indexPtr)) { |
| 317 | return nullptr; |
| 318 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 319 | |
| 320 | const DDLPromiseImageHelper::PromiseImageInfo& curImage = helper->getInfo(*indexPtr); |
| 321 | |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 322 | // If there is no callback context that means 'createCallbackContexts' determined the |
| 323 | // texture wouldn't fit on the GPU. Create a separate bitmap-backed image for each thread. |
| 324 | if (!curImage.isYUV() && !curImage.callbackContext(0)) { |
Robert Phillips | f95e2f4 | 2020-04-17 16:20:55 -0400 | [diff] [blame] | 325 | SkASSERT(curImage.baseLevel().isImmutable()); |
| 326 | return SkImage::MakeFromBitmap(curImage.baseLevel()); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 327 | } |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 328 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 329 | SkASSERT(curImage.index() == *indexPtr); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 330 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 331 | sk_sp<SkImage> image; |
| 332 | if (curImage.isYUV()) { |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 333 | GrBackendFormat backendFormats[SkYUVASizeInfo::kMaxCount]; |
| 334 | void* contexts[SkYUVASizeInfo::kMaxCount] = { nullptr, nullptr, nullptr, nullptr }; |
| 335 | SkISize sizes[SkYUVASizeInfo::kMaxCount]; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 336 | // TODO: store this value somewhere? |
| 337 | int textureCount; |
| 338 | SkAssertResult(SkYUVAIndex::AreValidIndices(curImage.yuvaIndices(), &textureCount)); |
| 339 | for (int i = 0; i < textureCount; ++i) { |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 340 | backendFormats[i] = curImage.backendFormat(i); |
Brian Salomon | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 341 | SkASSERT(backendFormats[i].isValid()); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 342 | contexts[i] = curImage.refCallbackContext(i).release(); |
Jim Van Verth | f9f0735 | 2018-10-24 10:32:20 -0400 | [diff] [blame] | 343 | sizes[i].set(curImage.yuvPixmap(i).width(), curImage.yuvPixmap(i).height()); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 344 | } |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 345 | for (int i = textureCount; i < SkYUVASizeInfo::kMaxCount; ++i) { |
Jim Van Verth | f9f0735 | 2018-10-24 10:32:20 -0400 | [diff] [blame] | 346 | sizes[i] = SkISize::MakeEmpty(); |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 347 | } |
Jim Van Verth | f99a674 | 2018-10-18 16:13:18 +0000 | [diff] [blame] | 348 | |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 349 | image = recorder->makeYUVAPromiseTexture( |
| 350 | curImage.yuvColorSpace(), |
| 351 | backendFormats, |
| 352 | sizes, |
| 353 | curImage.yuvaIndices(), |
| 354 | curImage.overallWidth(), |
| 355 | curImage.overallHeight(), |
| 356 | GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin, |
| 357 | curImage.refOverallColorSpace(), |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 358 | PromiseImageCallbackContext::PromiseImageFulfillProc, |
| 359 | PromiseImageCallbackContext::PromiseImageReleaseProc, |
| 360 | PromiseImageCallbackContext::PromiseImageDoneProc, |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 361 | contexts, |
| 362 | SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew); |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 363 | for (int i = 0; i < textureCount; ++i) { |
| 364 | curImage.callbackContext(i)->wasAddedToImage(); |
| 365 | } |
Robert Phillips | 193c421 | 2019-03-04 12:18:53 -0500 | [diff] [blame] | 366 | |
| 367 | #ifdef SK_DEBUG |
| 368 | { |
| 369 | // By the peekProxy contract this image should not have a single backing proxy so |
| 370 | // should return null. The call should also not trigger the conversion to RGBA. |
| 371 | SkImage_GpuYUVA* yuva = reinterpret_cast<SkImage_GpuYUVA*>(image.get()); |
| 372 | SkASSERT(!yuva->peekProxy()); |
| 373 | SkASSERT(!yuva->peekProxy()); // the first call didn't force a conversion to RGBA |
| 374 | } |
| 375 | #endif |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 376 | } else { |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 377 | GrBackendFormat backendFormat = curImage.backendFormat(0); |
Brian Salomon | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 378 | SkASSERT(backendFormat.isValid()); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 379 | |
| 380 | // Each DDL recorder gets its own ref on the promise callback context for the |
| 381 | // promise images it creates. |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 382 | image = recorder->makePromiseTexture( |
| 383 | backendFormat, |
| 384 | curImage.overallWidth(), |
| 385 | curImage.overallHeight(), |
Robert Phillips | f95e2f4 | 2020-04-17 16:20:55 -0400 | [diff] [blame] | 386 | curImage.mipMapped(0), |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 387 | GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin, |
| 388 | curImage.overallColorType(), |
| 389 | curImage.overallAlphaType(), |
| 390 | curImage.refOverallColorSpace(), |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 391 | PromiseImageCallbackContext::PromiseImageFulfillProc, |
| 392 | PromiseImageCallbackContext::PromiseImageReleaseProc, |
| 393 | PromiseImageCallbackContext::PromiseImageDoneProc, |
Brian Salomon | 0cc5754 | 2019-03-08 13:28:46 -0500 | [diff] [blame] | 394 | (void*)curImage.refCallbackContext(0).release(), |
| 395 | SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew); |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 396 | curImage.callbackContext(0)->wasAddedToImage(); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 397 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 398 | perRecorderContext->fPromiseImages->push_back(image); |
| 399 | SkASSERT(image); |
| 400 | return image; |
| 401 | } |
| 402 | |
| 403 | int DDLPromiseImageHelper::findImage(SkImage* image) const { |
| 404 | for (int i = 0; i < fImageInfo.count(); ++i) { |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 405 | if (fImageInfo[i].originalUniqueID() == image->uniqueID()) { // trying to dedup here |
| 406 | SkASSERT(fImageInfo[i].index() == i); |
| 407 | SkASSERT(this->isValidID(i) && this->isValidID(fImageInfo[i].index())); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 408 | return i; |
| 409 | } |
| 410 | } |
| 411 | return -1; |
| 412 | } |
| 413 | |
| 414 | int DDLPromiseImageHelper::addImage(SkImage* image) { |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 415 | SkImage_Base* ib = as_IB(image); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 416 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 417 | SkImageInfo overallII = SkImageInfo::Make(image->width(), image->height(), |
Robert Phillips | 13371a1 | 2019-05-13 15:59:10 -0400 | [diff] [blame] | 418 | image->colorType() == kBGRA_8888_SkColorType |
| 419 | ? kRGBA_8888_SkColorType |
| 420 | : image->colorType(), |
| 421 | image->alphaType(), |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 422 | image->refColorSpace()); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 423 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 424 | PromiseImageInfo& newImageInfo = fImageInfo.emplace_back(fImageInfo.count(), |
| 425 | image->uniqueID(), |
| 426 | overallII); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 427 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 428 | SkYUVASizeInfo yuvaSizeInfo; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 429 | SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount]; |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 430 | SkYUVColorSpace yuvColorSpace; |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 431 | const void* planes[SkYUVASizeInfo::kMaxCount]; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 432 | sk_sp<SkCachedData> yuvData = ib->getPlanes(&yuvaSizeInfo, yuvaIndices, &yuvColorSpace, planes); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 433 | if (yuvData) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 434 | newImageInfo.setYUVData(std::move(yuvData), yuvaIndices, yuvColorSpace); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 435 | |
Jim Van Verth | b7f0b9c | 2018-10-22 14:12:03 -0400 | [diff] [blame] | 436 | // determine colortypes from index data |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 437 | // for testing we only ever use A8, RG_88 |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 438 | SkColorType colorTypes[SkYUVASizeInfo::kMaxCount] = { |
Jim Van Verth | b7f0b9c | 2018-10-22 14:12:03 -0400 | [diff] [blame] | 439 | kUnknown_SkColorType, kUnknown_SkColorType, |
| 440 | kUnknown_SkColorType, kUnknown_SkColorType |
| 441 | }; |
| 442 | for (int yuvIndex = 0; yuvIndex < SkYUVAIndex::kIndexCount; ++yuvIndex) { |
| 443 | int texIdx = yuvaIndices[yuvIndex].fIndex; |
| 444 | if (texIdx < 0) { |
| 445 | SkASSERT(SkYUVAIndex::kA_Index == yuvIndex); |
| 446 | continue; |
| 447 | } |
| 448 | if (kUnknown_SkColorType == colorTypes[texIdx]) { |
| 449 | colorTypes[texIdx] = kAlpha_8_SkColorType; |
| 450 | } else { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 451 | colorTypes[texIdx] = kR8G8_unorm_SkColorType; |
Jim Van Verth | b7f0b9c | 2018-10-22 14:12:03 -0400 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 455 | for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) { |
Jim Van Verth | b7f0b9c | 2018-10-22 14:12:03 -0400 | [diff] [blame] | 456 | if (yuvaSizeInfo.fSizes[i].isEmpty()) { |
| 457 | SkASSERT(!yuvaSizeInfo.fWidthBytes[i] && kUnknown_SkColorType == colorTypes[i]); |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 458 | continue; |
| 459 | } |
| 460 | |
| 461 | SkImageInfo planeII = SkImageInfo::Make(yuvaSizeInfo.fSizes[i].fWidth, |
| 462 | yuvaSizeInfo.fSizes[i].fHeight, |
Jim Van Verth | b7f0b9c | 2018-10-22 14:12:03 -0400 | [diff] [blame] | 463 | colorTypes[i], |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 464 | kUnpremul_SkAlphaType); |
| 465 | newImageInfo.addYUVPlane(i, planeII, planes[i], yuvaSizeInfo.fWidthBytes[i]); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 466 | } |
| 467 | } else { |
| 468 | sk_sp<SkImage> rasterImage = image->makeRasterImage(); // force decoding of lazy images |
Robert Phillips | e84bffc | 2019-12-16 11:22:17 -0500 | [diff] [blame] | 469 | if (!rasterImage) { |
| 470 | return -1; |
| 471 | } |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 472 | |
| 473 | SkBitmap tmp; |
| 474 | tmp.allocPixels(overallII); |
| 475 | |
| 476 | if (!rasterImage->readPixels(tmp.pixmap(), 0, 0)) { |
| 477 | return -1; |
| 478 | } |
| 479 | |
| 480 | tmp.setImmutable(); |
Robert Phillips | f95e2f4 | 2020-04-17 16:20:55 -0400 | [diff] [blame] | 481 | |
| 482 | // Given how the DDL testing harness works (i.e., only modifying the SkImages w/in an |
| 483 | // SKP) we don't know if a given SkImage will require mipmapping. To work around this |
| 484 | // we just try to create all the backend textures as mipmapped but, failing that, fall |
| 485 | // back to un-mipped. |
| 486 | std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(tmp.pixmap(), nullptr)); |
| 487 | |
| 488 | newImageInfo.setMipLevels(tmp, std::move(mipmaps)); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 489 | } |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 490 | // In either case newImageInfo's PromiseImageCallbackContext is filled in by uploadAllToGPU |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 491 | |
| 492 | return fImageInfo.count()-1; |
| 493 | } |
| 494 | |
| 495 | int DDLPromiseImageHelper::findOrDefineImage(SkImage* image) { |
| 496 | int preExistingID = this->findImage(image); |
| 497 | if (preExistingID >= 0) { |
| 498 | SkASSERT(this->isValidID(preExistingID)); |
| 499 | return preExistingID; |
| 500 | } |
| 501 | |
| 502 | int newID = this->addImage(image); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 503 | return newID; |
| 504 | } |