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" |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 17 | #include "SkYUVSizeInfo.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 | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame^] | 145 | GrBackendFormat backendFormats[SkYUVSizeInfo::kMaxCount]; |
| 146 | void* contexts[SkYUVSizeInfo::kMaxCount] = { nullptr, nullptr, nullptr, nullptr }; |
| 147 | SkYUVSizeInfo sizeInfo; |
| 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 | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame^] | 156 | sizeInfo.fSizes[i].set(curImage.yuvPixmap(i).width(), curImage.yuvPixmap(i).height()); |
| 157 | sizeInfo.fColorTypes[i] = curImage.yuvPixmap(i).colorType(); |
| 158 | sizeInfo.fWidthBytes[i] = curImage.yuvPixmap(i).rowBytes(); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 159 | } |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame^] | 160 | for (int i = textureCount; i < SkYUVSizeInfo::kMaxCount; ++i) { |
| 161 | sizeInfo.fSizes[i] = SkISize::MakeEmpty(); |
| 162 | sizeInfo.fColorTypes[i] = kUnknown_SkColorType; |
| 163 | sizeInfo.fWidthBytes[i] = 0; |
| 164 | } |
Jim Van Verth | f99a674 | 2018-10-18 16:13:18 +0000 | [diff] [blame] | 165 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 166 | image = recorder->makeYUVAPromiseTexture(curImage.yuvColorSpace(), |
| 167 | backendFormats, |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame^] | 168 | sizeInfo, |
| 169 | curImage.yuvaIndices(), |
Jim Van Verth | 21bd60d | 2018-10-12 15:00:20 -0400 | [diff] [blame] | 170 | curImage.overallWidth(), |
| 171 | curImage.overallHeight(), |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 172 | GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin, |
| 173 | curImage.refOverallColorSpace(), |
| 174 | DDLPromiseImageHelper::PromiseImageFulfillProc, |
| 175 | DDLPromiseImageHelper::PromiseImageReleaseProc, |
| 176 | DDLPromiseImageHelper::PromiseImageDoneProc, |
| 177 | contexts); |
| 178 | |
| 179 | } else { |
| 180 | const GrBackendTexture& backendTex = curImage.backendTexture(0); |
| 181 | GrBackendFormat backendFormat = caps->createFormatFromBackendTexture(backendTex); |
| 182 | |
| 183 | // Each DDL recorder gets its own ref on the promise callback context for the |
| 184 | // promise images it creates. |
| 185 | // DDL TODO: sort out mipmapping |
| 186 | image = recorder->makePromiseTexture(backendFormat, |
| 187 | curImage.overallWidth(), |
| 188 | curImage.overallHeight(), |
| 189 | GrMipMapped::kNo, |
| 190 | GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin, |
| 191 | curImage.overallColorType(), |
| 192 | curImage.overallAlphaType(), |
| 193 | curImage.refOverallColorSpace(), |
| 194 | DDLPromiseImageHelper::PromiseImageFulfillProc, |
| 195 | DDLPromiseImageHelper::PromiseImageReleaseProc, |
| 196 | DDLPromiseImageHelper::PromiseImageDoneProc, |
| 197 | (void*) curImage.refCallbackContext(0).release()); |
| 198 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 199 | perRecorderContext->fPromiseImages->push_back(image); |
| 200 | SkASSERT(image); |
| 201 | return image; |
| 202 | } |
| 203 | |
| 204 | int DDLPromiseImageHelper::findImage(SkImage* image) const { |
| 205 | for (int i = 0; i < fImageInfo.count(); ++i) { |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 206 | if (fImageInfo[i].originalUniqueID() == image->uniqueID()) { // trying to dedup here |
| 207 | SkASSERT(fImageInfo[i].index() == i); |
| 208 | SkASSERT(this->isValidID(i) && this->isValidID(fImageInfo[i].index())); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 209 | return i; |
| 210 | } |
| 211 | } |
| 212 | return -1; |
| 213 | } |
| 214 | |
| 215 | int DDLPromiseImageHelper::addImage(SkImage* image) { |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 216 | SkImage_Base* ib = as_IB(image); |
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 | SkImageInfo overallII = SkImageInfo::Make(image->width(), image->height(), |
| 219 | image->colorType(), image->alphaType(), |
| 220 | image->refColorSpace()); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 221 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 222 | PromiseImageInfo& newImageInfo = fImageInfo.emplace_back(fImageInfo.count(), |
| 223 | image->uniqueID(), |
| 224 | overallII); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 225 | |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame^] | 226 | SkYUVSizeInfo yuvaSizeInfo; |
| 227 | SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount]; |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 228 | SkYUVColorSpace yuvColorSpace; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame^] | 229 | const void* planes[SkYUVSizeInfo::kMaxCount]; |
| 230 | sk_sp<SkCachedData> yuvData = ib->getPlanes(&yuvaSizeInfo, yuvaIndices, &yuvColorSpace, planes); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 231 | if (yuvData) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame^] | 232 | newImageInfo.setYUVData(std::move(yuvData), yuvaIndices, yuvColorSpace); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 233 | |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame^] | 234 | for (int i = 0; i < SkYUVSizeInfo::kMaxCount; ++i) { |
| 235 | if (kUnknown_SkColorType == yuvaSizeInfo.fColorTypes[i]) { |
| 236 | SkASSERT(!yuvaSizeInfo.fSizes[i].fWidth && |
| 237 | !yuvaSizeInfo.fSizes[i].fHeight && |
| 238 | !yuvaSizeInfo.fWidthBytes[i]); |
| 239 | continue; |
| 240 | } |
| 241 | |
| 242 | SkImageInfo planeII = SkImageInfo::Make(yuvaSizeInfo.fSizes[i].fWidth, |
| 243 | yuvaSizeInfo.fSizes[i].fHeight, |
| 244 | yuvaSizeInfo.fColorTypes[i], |
| 245 | kUnpremul_SkAlphaType); |
| 246 | newImageInfo.addYUVPlane(i, planeII, planes[i], yuvaSizeInfo.fWidthBytes[i]); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 247 | } |
| 248 | } else { |
| 249 | sk_sp<SkImage> rasterImage = image->makeRasterImage(); // force decoding of lazy images |
| 250 | |
| 251 | SkBitmap tmp; |
| 252 | tmp.allocPixels(overallII); |
| 253 | |
| 254 | if (!rasterImage->readPixels(tmp.pixmap(), 0, 0)) { |
| 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | tmp.setImmutable(); |
| 259 | newImageInfo.setNormalBitmap(tmp); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 260 | } |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 261 | // In either case newImageInfo's PromiseImageCallbackContext is filled in by uploadAllToGPU |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 262 | |
| 263 | return fImageInfo.count()-1; |
| 264 | } |
| 265 | |
| 266 | int DDLPromiseImageHelper::findOrDefineImage(SkImage* image) { |
| 267 | int preExistingID = this->findImage(image); |
| 268 | if (preExistingID >= 0) { |
| 269 | SkASSERT(this->isValidID(preExistingID)); |
| 270 | return preExistingID; |
| 271 | } |
| 272 | |
| 273 | int newID = this->addImage(image); |
| 274 | SkASSERT(this->isValidID(newID)); |
| 275 | return newID; |
| 276 | } |