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 | |
| 8 | #include "DDLPromiseImageHelper.h" |
| 9 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 10 | #include "GrContext.h" |
| 11 | #include "GrContextPriv.h" |
| 12 | #include "GrGpu.h" |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 13 | #include "SkCachedData.h" |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 14 | #include "SkDeferredDisplayListRecorder.h" |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 15 | #include "SkImage_Base.h" |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 16 | #include "SkYUVAIndex.h" |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame^] | 17 | #include "SkYUVASizeInfo.h" |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 18 | |
| 19 | DDLPromiseImageHelper::PromiseImageCallbackContext::~PromiseImageCallbackContext() { |
| 20 | GrGpu* gpu = fContext->contextPriv().getGpu(); |
| 21 | |
| 22 | if (fBackendTexture.isValid()) { |
| 23 | gpu->deleteTestingOnlyBackendTexture(fBackendTexture); |
| 24 | } |
| 25 | } |
| 26 | |
Timothy Liang | 036fdfe | 2018-06-28 15:50:36 -0400 | [diff] [blame] | 27 | const GrCaps* DDLPromiseImageHelper::PromiseImageCallbackContext::caps() const { |
| 28 | return fContext->contextPriv().caps(); |
| 29 | } |
| 30 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 31 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 32 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 33 | DDLPromiseImageHelper::~DDLPromiseImageHelper() {} |
| 34 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 35 | sk_sp<SkData> DDLPromiseImageHelper::deflateSKP(const SkPicture* inputPicture) { |
| 36 | SkSerialProcs procs; |
| 37 | |
| 38 | procs.fImageCtx = this; |
| 39 | procs.fImageProc = [](SkImage* image, void* ctx) -> sk_sp<SkData> { |
| 40 | auto helper = static_cast<DDLPromiseImageHelper*>(ctx); |
| 41 | |
| 42 | int id = helper->findOrDefineImage(image); |
| 43 | if (id >= 0) { |
| 44 | SkASSERT(helper->isValidID(id)); |
| 45 | return SkData::MakeWithCopy(&id, sizeof(id)); |
| 46 | } |
| 47 | |
| 48 | return nullptr; |
| 49 | }; |
| 50 | |
| 51 | return inputPicture->serialize(&procs); |
| 52 | } |
| 53 | |
| 54 | void DDLPromiseImageHelper::uploadAllToGPU(GrContext* context) { |
| 55 | GrGpu* gpu = context->contextPriv().getGpu(); |
| 56 | SkASSERT(gpu); |
| 57 | |
| 58 | for (int i = 0; i < fImageInfo.count(); ++i) { |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 59 | const PromiseImageInfo& info = fImageInfo[i]; |
| 60 | |
| 61 | // DDL TODO: how can we tell if we need mipmapping! |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 62 | if (info.isYUV()) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 63 | int numPixmaps; |
| 64 | SkAssertResult(SkYUVAIndex::AreValidIndices(info.yuvaIndices(), &numPixmaps)); |
| 65 | for (int j = 0; j < numPixmaps; ++j) { |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 66 | const SkPixmap& yuvPixmap = info.yuvPixmap(j); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 67 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 68 | sk_sp<PromiseImageCallbackContext> callbackContext( |
| 69 | new PromiseImageCallbackContext(context)); |
| 70 | callbackContext->setBackendTexture(gpu->createTestingOnlyBackendTexture( |
| 71 | yuvPixmap.addr(), |
| 72 | yuvPixmap.width(), |
| 73 | yuvPixmap.height(), |
| 74 | yuvPixmap.colorType(), |
| 75 | false, GrMipMapped::kNo, |
| 76 | yuvPixmap.rowBytes())); |
| 77 | SkAssertResult(callbackContext->backendTexture().isValid()); |
| 78 | |
| 79 | fImageInfo[i].setCallbackContext(j, std::move(callbackContext)); |
| 80 | } |
| 81 | } else { |
| 82 | sk_sp<PromiseImageCallbackContext> callbackContext( |
| 83 | new PromiseImageCallbackContext(context)); |
| 84 | |
| 85 | const SkBitmap& bm = info.normalBitmap(); |
| 86 | |
| 87 | callbackContext->setBackendTexture(gpu->createTestingOnlyBackendTexture( |
| 88 | bm.getPixels(), |
| 89 | bm.width(), |
| 90 | bm.height(), |
| 91 | bm.colorType(), |
| 92 | false, GrMipMapped::kNo, |
| 93 | bm.rowBytes())); |
| 94 | // The GMs sometimes request too large an image |
| 95 | //SkAssertResult(callbackContext->backendTexture().isValid()); |
| 96 | |
| 97 | fImageInfo[i].setCallbackContext(0, std::move(callbackContext)); |
| 98 | } |
| 99 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | sk_sp<SkPicture> DDLPromiseImageHelper::reinflateSKP( |
| 104 | SkDeferredDisplayListRecorder* recorder, |
| 105 | SkData* compressedPictureData, |
| 106 | SkTArray<sk_sp<SkImage>>* promiseImages) const { |
| 107 | PerRecorderContext perRecorderContext { recorder, this, promiseImages }; |
| 108 | |
| 109 | SkDeserialProcs procs; |
| 110 | procs.fImageCtx = (void*) &perRecorderContext; |
| 111 | procs.fImageProc = PromiseImageCreator; |
| 112 | |
| 113 | return SkPicture::MakeFromData(compressedPictureData, &procs); |
| 114 | } |
| 115 | |
| 116 | // This generates promise images to replace the indices in the compressed picture. This |
| 117 | // reconstitution is performed separately in each thread so we end up with multiple |
| 118 | // promise images referring to the same GrBackendTexture. |
| 119 | sk_sp<SkImage> DDLPromiseImageHelper::PromiseImageCreator(const void* rawData, |
| 120 | size_t length, void* ctxIn) { |
| 121 | PerRecorderContext* perRecorderContext = static_cast<PerRecorderContext*>(ctxIn); |
| 122 | const DDLPromiseImageHelper* helper = perRecorderContext->fHelper; |
| 123 | SkDeferredDisplayListRecorder* recorder = perRecorderContext->fRecorder; |
| 124 | |
| 125 | SkASSERT(length == sizeof(int)); |
| 126 | |
| 127 | const int* indexPtr = static_cast<const int*>(rawData); |
| 128 | SkASSERT(helper->isValidID(*indexPtr)); |
| 129 | |
| 130 | const DDLPromiseImageHelper::PromiseImageInfo& curImage = helper->getInfo(*indexPtr); |
| 131 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 132 | if (!curImage.backendTexture(0).isValid()) { |
| 133 | SkASSERT(!curImage.isYUV()); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 134 | // We weren't able to make a backend texture for this SkImage. In this case we create |
| 135 | // a separate bitmap-backed image for each thread. |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 136 | SkASSERT(curImage.normalBitmap().isImmutable()); |
| 137 | return SkImage::MakeFromBitmap(curImage.normalBitmap()); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 138 | } |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 139 | SkASSERT(curImage.index() == *indexPtr); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 140 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 141 | const GrCaps* caps = curImage.caps(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 142 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 143 | sk_sp<SkImage> image; |
| 144 | if (curImage.isYUV()) { |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame^] | 145 | GrBackendFormat backendFormats[SkYUVASizeInfo::kMaxCount]; |
| 146 | void* contexts[SkYUVASizeInfo::kMaxCount] = { nullptr, nullptr, nullptr, nullptr }; |
| 147 | SkISize sizes[SkYUVASizeInfo::kMaxCount]; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 148 | // TODO: store this value somewhere? |
| 149 | int textureCount; |
| 150 | SkAssertResult(SkYUVAIndex::AreValidIndices(curImage.yuvaIndices(), &textureCount)); |
| 151 | for (int i = 0; i < textureCount; ++i) { |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 152 | const GrBackendTexture& backendTex = curImage.backendTexture(i); |
| 153 | backendFormats[i] = caps->createFormatFromBackendTexture(backendTex); |
| 154 | |
| 155 | contexts[i] = curImage.refCallbackContext(i).release(); |
Jim Van Verth | f9f0735 | 2018-10-24 10:32:20 -0400 | [diff] [blame] | 156 | sizes[i].set(curImage.yuvPixmap(i).width(), curImage.yuvPixmap(i).height()); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 157 | } |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame^] | 158 | for (int i = textureCount; i < SkYUVASizeInfo::kMaxCount; ++i) { |
Jim Van Verth | f9f0735 | 2018-10-24 10:32:20 -0400 | [diff] [blame] | 159 | sizes[i] = SkISize::MakeEmpty(); |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 160 | } |
Jim Van Verth | f99a674 | 2018-10-18 16:13:18 +0000 | [diff] [blame] | 161 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 162 | image = recorder->makeYUVAPromiseTexture(curImage.yuvColorSpace(), |
| 163 | backendFormats, |
Jim Van Verth | f9f0735 | 2018-10-24 10:32:20 -0400 | [diff] [blame] | 164 | sizes, |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 165 | curImage.yuvaIndices(), |
Jim Van Verth | 21bd60d | 2018-10-12 15:00:20 -0400 | [diff] [blame] | 166 | curImage.overallWidth(), |
| 167 | curImage.overallHeight(), |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 168 | GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin, |
| 169 | curImage.refOverallColorSpace(), |
| 170 | DDLPromiseImageHelper::PromiseImageFulfillProc, |
| 171 | DDLPromiseImageHelper::PromiseImageReleaseProc, |
| 172 | DDLPromiseImageHelper::PromiseImageDoneProc, |
| 173 | contexts); |
| 174 | |
| 175 | } else { |
| 176 | const GrBackendTexture& backendTex = curImage.backendTexture(0); |
| 177 | GrBackendFormat backendFormat = caps->createFormatFromBackendTexture(backendTex); |
| 178 | |
| 179 | // Each DDL recorder gets its own ref on the promise callback context for the |
| 180 | // promise images it creates. |
| 181 | // DDL TODO: sort out mipmapping |
| 182 | image = recorder->makePromiseTexture(backendFormat, |
| 183 | curImage.overallWidth(), |
| 184 | curImage.overallHeight(), |
| 185 | GrMipMapped::kNo, |
| 186 | GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin, |
| 187 | curImage.overallColorType(), |
| 188 | curImage.overallAlphaType(), |
| 189 | curImage.refOverallColorSpace(), |
| 190 | DDLPromiseImageHelper::PromiseImageFulfillProc, |
| 191 | DDLPromiseImageHelper::PromiseImageReleaseProc, |
| 192 | DDLPromiseImageHelper::PromiseImageDoneProc, |
| 193 | (void*) curImage.refCallbackContext(0).release()); |
| 194 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 195 | perRecorderContext->fPromiseImages->push_back(image); |
| 196 | SkASSERT(image); |
| 197 | return image; |
| 198 | } |
| 199 | |
| 200 | int DDLPromiseImageHelper::findImage(SkImage* image) const { |
| 201 | for (int i = 0; i < fImageInfo.count(); ++i) { |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 202 | if (fImageInfo[i].originalUniqueID() == image->uniqueID()) { // trying to dedup here |
| 203 | SkASSERT(fImageInfo[i].index() == i); |
| 204 | SkASSERT(this->isValidID(i) && this->isValidID(fImageInfo[i].index())); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 205 | return i; |
| 206 | } |
| 207 | } |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | int DDLPromiseImageHelper::addImage(SkImage* image) { |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 212 | SkImage_Base* ib = as_IB(image); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 213 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 214 | SkImageInfo overallII = SkImageInfo::Make(image->width(), image->height(), |
| 215 | image->colorType(), image->alphaType(), |
| 216 | image->refColorSpace()); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 217 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 218 | PromiseImageInfo& newImageInfo = fImageInfo.emplace_back(fImageInfo.count(), |
| 219 | image->uniqueID(), |
| 220 | overallII); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 221 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame^] | 222 | SkYUVASizeInfo yuvaSizeInfo; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 223 | SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount]; |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 224 | SkYUVColorSpace yuvColorSpace; |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame^] | 225 | const void* planes[SkYUVASizeInfo::kMaxCount]; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 226 | sk_sp<SkCachedData> yuvData = ib->getPlanes(&yuvaSizeInfo, yuvaIndices, &yuvColorSpace, planes); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 227 | if (yuvData) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 228 | newImageInfo.setYUVData(std::move(yuvData), yuvaIndices, yuvColorSpace); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 229 | |
Jim Van Verth | b7f0b9c | 2018-10-22 14:12:03 -0400 | [diff] [blame] | 230 | // determine colortypes from index data |
| 231 | // for testing we only ever use A8 or RGBA8888 |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame^] | 232 | SkColorType colorTypes[SkYUVASizeInfo::kMaxCount] = { |
Jim Van Verth | b7f0b9c | 2018-10-22 14:12:03 -0400 | [diff] [blame] | 233 | kUnknown_SkColorType, kUnknown_SkColorType, |
| 234 | kUnknown_SkColorType, kUnknown_SkColorType |
| 235 | }; |
| 236 | for (int yuvIndex = 0; yuvIndex < SkYUVAIndex::kIndexCount; ++yuvIndex) { |
| 237 | int texIdx = yuvaIndices[yuvIndex].fIndex; |
| 238 | if (texIdx < 0) { |
| 239 | SkASSERT(SkYUVAIndex::kA_Index == yuvIndex); |
| 240 | continue; |
| 241 | } |
| 242 | if (kUnknown_SkColorType == colorTypes[texIdx]) { |
| 243 | colorTypes[texIdx] = kAlpha_8_SkColorType; |
| 244 | } else { |
| 245 | colorTypes[texIdx] = kRGBA_8888_SkColorType; |
| 246 | } |
| 247 | } |
| 248 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame^] | 249 | for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) { |
Jim Van Verth | b7f0b9c | 2018-10-22 14:12:03 -0400 | [diff] [blame] | 250 | if (yuvaSizeInfo.fSizes[i].isEmpty()) { |
| 251 | SkASSERT(!yuvaSizeInfo.fWidthBytes[i] && kUnknown_SkColorType == colorTypes[i]); |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 252 | continue; |
| 253 | } |
| 254 | |
| 255 | SkImageInfo planeII = SkImageInfo::Make(yuvaSizeInfo.fSizes[i].fWidth, |
| 256 | yuvaSizeInfo.fSizes[i].fHeight, |
Jim Van Verth | b7f0b9c | 2018-10-22 14:12:03 -0400 | [diff] [blame] | 257 | colorTypes[i], |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 258 | kUnpremul_SkAlphaType); |
| 259 | newImageInfo.addYUVPlane(i, planeII, planes[i], yuvaSizeInfo.fWidthBytes[i]); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 260 | } |
| 261 | } else { |
| 262 | sk_sp<SkImage> rasterImage = image->makeRasterImage(); // force decoding of lazy images |
| 263 | |
| 264 | SkBitmap tmp; |
| 265 | tmp.allocPixels(overallII); |
| 266 | |
| 267 | if (!rasterImage->readPixels(tmp.pixmap(), 0, 0)) { |
| 268 | return -1; |
| 269 | } |
| 270 | |
| 271 | tmp.setImmutable(); |
| 272 | newImageInfo.setNormalBitmap(tmp); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 273 | } |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 274 | // In either case newImageInfo's PromiseImageCallbackContext is filled in by uploadAllToGPU |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 275 | |
| 276 | return fImageInfo.count()-1; |
| 277 | } |
| 278 | |
| 279 | int DDLPromiseImageHelper::findOrDefineImage(SkImage* image) { |
| 280 | int preExistingID = this->findImage(image); |
| 281 | if (preExistingID >= 0) { |
| 282 | SkASSERT(this->isValidID(preExistingID)); |
| 283 | return preExistingID; |
| 284 | } |
| 285 | |
| 286 | int newID = this->addImage(image); |
| 287 | SkASSERT(this->isValidID(newID)); |
| 288 | return newID; |
| 289 | } |