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