| 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 <cstddef> |
| 9 | #include <cstring> |
| 10 | #include <type_traits> |
| 11 | |
| 12 | #include "include/core/SkYUVASizeInfo.h" |
| 13 | #include "include/gpu/GrDirectContext.h" |
| 14 | #include "include/gpu/GrRecordingContext.h" |
| 15 | #include "src/core/SkAutoPixmapStorage.h" |
| 16 | #include "src/core/SkMipmap.h" |
| 17 | #include "src/core/SkScopeExit.h" |
| 18 | #include "src/gpu/GrBitmapTextureMaker.h" |
| 19 | #include "src/gpu/GrClip.h" |
| 20 | #include "src/gpu/GrContextPriv.h" |
| 21 | #include "src/gpu/GrGpu.h" |
| 22 | #include "src/gpu/GrRecordingContextPriv.h" |
| 23 | #include "src/gpu/GrRenderTargetContext.h" |
| 24 | #include "src/gpu/GrTexture.h" |
| 25 | #include "src/gpu/GrTextureProducer.h" |
| 26 | #include "src/gpu/SkGr.h" |
| 27 | #include "src/gpu/effects/GrYUVtoRGBEffect.h" |
| 28 | #include "src/image/SkImage_Gpu.h" |
| 29 | #include "src/image/SkImage_GpuYUVA.h" |
| 30 | |
| 31 | static constexpr auto kAssumedColorType = kRGBA_8888_SkColorType; |
| 32 | |
| 33 | SkImage_GpuYUVA::SkImage_GpuYUVA(sk_sp<GrRecordingContext> context, |
| 34 | SkISize size, |
| 35 | uint32_t uniqueID, |
| 36 | SkYUVColorSpace colorSpace, |
| 37 | GrSurfaceProxyView views[], |
| 38 | int numViews, |
| 39 | const SkYUVAIndex yuvaIndices[4], |
| 40 | GrSurfaceOrigin origin, |
| 41 | sk_sp<SkColorSpace> imageColorSpace) |
| 42 | // CONTEXT TODO: rm this usage of the 'backdoor' to create an image |
| 43 | : INHERITED(sk_ref_sp(context->priv().backdoor()), |
| 44 | size, |
| 45 | uniqueID, |
| 46 | kAssumedColorType, |
| 47 | // If an alpha channel is present we always switch to kPremul. This is because, |
| 48 | // although the planar data is always un-premul, the final interleaved RGB image |
| 49 | // is/would-be premul. |
| 50 | GetAlphaTypeFromYUVAIndices(yuvaIndices), |
| 51 | std::move(imageColorSpace)) |
| 52 | , fNumViews(numViews) |
| 53 | , fYUVColorSpace(colorSpace) |
| 54 | , fOrigin(origin) { |
| 55 | // The caller should have done this work, just verifying |
| 56 | SkDEBUGCODE(int textureCount;) |
| 57 | SkASSERT(SkYUVAIndex::AreValidIndices(yuvaIndices, &textureCount)); |
| 58 | SkASSERT(textureCount == fNumViews); |
| 59 | |
| 60 | for (int i = 0; i < numViews; ++i) { |
| 61 | fViews[i] = std::move(views[i]); |
| 62 | } |
| 63 | memcpy(fYUVAIndices, yuvaIndices, 4 * sizeof(SkYUVAIndex)); |
| 64 | } |
| 65 | |
| 66 | // For onMakeColorSpace() |
| 67 | SkImage_GpuYUVA::SkImage_GpuYUVA(sk_sp<GrContext> context, const SkImage_GpuYUVA* image, |
| 68 | sk_sp<SkColorSpace> targetCS) |
| 69 | : INHERITED(std::move(context), image->dimensions(), kNeedNewImageUniqueID, |
| 70 | kAssumedColorType, |
| 71 | // If an alpha channel is present we always switch to kPremul. This is because, |
| 72 | // although the planar data is always un-premul, the final interleaved RGB image |
| 73 | // is/would-be premul. |
| 74 | GetAlphaTypeFromYUVAIndices(image->fYUVAIndices), std::move(targetCS)) |
| 75 | , fNumViews(image->fNumViews) |
| 76 | , fYUVColorSpace(image->fYUVColorSpace) |
| 77 | , fOrigin(image->fOrigin) |
| 78 | // Since null fFromColorSpace means no GrColorSpaceXform, we turn a null |
| 79 | // image->refColorSpace() into an explicit SRGB. |
| 80 | , fFromColorSpace(image->colorSpace() ? image->refColorSpace() : SkColorSpace::MakeSRGB()) { |
| 81 | // The caller should have done this work, just verifying |
| 82 | SkDEBUGCODE(int textureCount;) |
| 83 | SkASSERT(SkYUVAIndex::AreValidIndices(image->fYUVAIndices, &textureCount)); |
| 84 | SkASSERT(textureCount == fNumViews); |
| 85 | |
| 86 | if (image->fRGBView.proxy()) { |
| 87 | fRGBView = image->fRGBView; // we ref in this case, not move |
| 88 | } else { |
| 89 | for (int i = 0; i < fNumViews; ++i) { |
| 90 | fViews[i] = image->fViews[i]; // we ref in this case, not move |
| 91 | } |
| 92 | } |
| 93 | memcpy(fYUVAIndices, image->fYUVAIndices, 4 * sizeof(SkYUVAIndex)); |
| 94 | } |
| 95 | |
| 96 | bool SkImage_GpuYUVA::setupMipmapsForPlanes(GrRecordingContext* context) const { |
| 97 | // We shouldn't get here if the planes were already flattened to RGBA. |
| 98 | SkASSERT(fViews[0].proxy() && !fRGBView.proxy()); |
| 99 | if (!context || !fContext->priv().matches(context)) { |
| 100 | return false; |
| 101 | } |
| 102 | GrSurfaceProxyView newViews[4]; |
| 103 | if (!context->priv().caps()->mipmapSupport()) { |
| 104 | // We succeed in this case by doing nothing. |
| 105 | return true; |
| 106 | } |
| 107 | for (int i = 0; i < fNumViews; ++i) { |
| 108 | auto* t = fViews[i].asTextureProxy(); |
| 109 | if (t->mipmapped() == GrMipmapped::kNo && (t->width() > 1 || t->height() > 1)) { |
| 110 | if (!(newViews[i] = GrCopyBaseMipMapToView(context, fViews[i]))) { |
| 111 | return false; |
| 112 | } |
| 113 | } else { |
| 114 | newViews[i] = fViews[i]; |
| 115 | } |
| 116 | } |
| 117 | for (int i = 0; i < fNumViews; ++i) { |
| 118 | fViews[i] = std::move(newViews[i]); |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 124 | |
| 125 | GrSemaphoresSubmitted SkImage_GpuYUVA::onFlush(GrDirectContext* dContext, const GrFlushInfo& info) { |
| 126 | if (!fContext->priv().matches(dContext) || dContext->abandoned()) { |
| 127 | if (info.fSubmittedProc) { |
| 128 | info.fSubmittedProc(info.fSubmittedContext, false); |
| 129 | } |
| 130 | if (info.fFinishedProc) { |
| 131 | info.fFinishedProc(info.fFinishedContext); |
| 132 | } |
| 133 | return GrSemaphoresSubmitted::kNo; |
| 134 | } |
| 135 | |
| 136 | GrSurfaceProxy* proxies[4] = {fViews[0].proxy(), fViews[1].proxy(), fViews[2].proxy(), |
| 137 | fViews[3].proxy()}; |
| 138 | int numProxies = fNumViews; |
| 139 | if (fRGBView.proxy()) { |
| 140 | // Either we've already flushed the flattening draw or the flattening is unflushed. In the |
| 141 | // latter case it should still be ok to just pass fRGBView proxy because it in turn depends |
| 142 | // on the planar proxies and will cause all of their work to flush as well. |
| 143 | proxies[0] = fRGBView.proxy(); |
| 144 | numProxies = 1; |
| 145 | } |
| 146 | return dContext->priv().flushSurfaces(proxies, numProxies, info); |
| 147 | } |
| 148 | |
| 149 | GrTextureProxy* SkImage_GpuYUVA::peekProxy() const { return fRGBView.asTextureProxy(); } |
| 150 | |
| 151 | void SkImage_GpuYUVA::flattenToRGB(GrRecordingContext* context) const { |
| 152 | if (fRGBView.proxy()) { |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | if (!context || !fContext->priv().matches(context)) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | // Needs to create a render target in order to draw to it for the yuv->rgb conversion. |
| 161 | auto renderTargetContext = GrRenderTargetContext::Make( |
| 162 | context, GrColorType::kRGBA_8888, this->refColorSpace(), SkBackingFit::kExact, |
| 163 | this->dimensions(), 1, GrMipmapped::kNo, GrProtected::kNo, fOrigin); |
| 164 | if (!renderTargetContext) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | sk_sp<GrColorSpaceXform> colorSpaceXform; |
| 169 | if (fFromColorSpace) { |
| 170 | colorSpaceXform = GrColorSpaceXform::Make(fFromColorSpace.get(), this->alphaType(), |
| 171 | this->colorSpace(), this->alphaType()); |
| 172 | } |
| 173 | const SkRect rect = SkRect::MakeIWH(this->width(), this->height()); |
| 174 | const GrCaps& caps = *context->priv().caps(); |
| 175 | if (!RenderYUVAToRGBA(caps, renderTargetContext.get(), rect, fYUVColorSpace, |
| 176 | std::move(colorSpaceXform), fViews, fYUVAIndices)) { |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | fRGBView = renderTargetContext->readSurfaceView(); |
| 181 | SkASSERT(fRGBView.origin() == fOrigin); |
| 182 | SkASSERT(fRGBView.swizzle() == GrSwizzle()); |
| 183 | for (auto& v : fViews) { |
| 184 | v.reset(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | GrSurfaceProxyView SkImage_GpuYUVA::refMippedView(GrRecordingContext* context) const { |
| 189 | // if invalid or already has miplevels |
| 190 | this->flattenToRGB(context); |
| 191 | if (!fRGBView || fRGBView.asTextureProxy()->mipmapped() == GrMipmapped::kYes) { |
| 192 | return fRGBView; |
| 193 | } |
| 194 | |
| 195 | // need to generate mips for the proxy |
| 196 | auto mippedView = GrCopyBaseMipMapToView(context, fRGBView); |
| 197 | if (!mippedView) { |
| 198 | return {}; |
| 199 | } |
| 200 | |
| 201 | fRGBView = std::move(mippedView); |
| 202 | return fRGBView; |
| 203 | } |
| 204 | |
| 205 | const GrSurfaceProxyView* SkImage_GpuYUVA::view(GrRecordingContext* context) const { |
| 206 | this->flattenToRGB(context); |
| 207 | if (!fRGBView.proxy()) { |
| 208 | return nullptr; |
| 209 | } |
| 210 | return &fRGBView; |
| 211 | } |
| 212 | |
| 213 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 214 | |
| 215 | sk_sp<SkImage> SkImage_GpuYUVA::onMakeColorTypeAndColorSpace( |
| 216 | SkColorType, sk_sp<SkColorSpace> targetCS, GrDirectContext* direct) const { |
| 217 | // We explicitly ignore color type changes, for now. |
| 218 | |
| 219 | // we may need a mutex here but for now we expect usage to be in a single thread |
| 220 | if (fOnMakeColorSpaceTarget && |
| 221 | SkColorSpace::Equals(targetCS.get(), fOnMakeColorSpaceTarget.get())) { |
| 222 | return fOnMakeColorSpaceResult; |
| 223 | } |
| 224 | sk_sp<SkImage> result = sk_sp<SkImage>(new SkImage_GpuYUVA(sk_ref_sp(direct), this, targetCS)); |
| 225 | if (result) { |
| 226 | fOnMakeColorSpaceTarget = targetCS; |
| 227 | fOnMakeColorSpaceResult = result; |
| 228 | } |
| 229 | return result; |
| 230 | } |
| 231 | |
| 232 | sk_sp<SkImage> SkImage_GpuYUVA::onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const { |
| 233 | return sk_make_sp<SkImage_GpuYUVA>(fContext, this->dimensions(), kNeedNewImageUniqueID, |
| 234 | fYUVColorSpace, fViews, fNumViews, fYUVAIndices, fOrigin, |
| 235 | std::move(newCS)); |
| 236 | } |
| 237 | |
| 238 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 239 | |
| 240 | sk_sp<SkImage> SkImage::MakeFromYUVATextures(GrContext* ctx, |
| 241 | SkYUVColorSpace colorSpace, |
| 242 | const GrBackendTexture yuvaTextures[], |
| 243 | const SkYUVAIndex yuvaIndices[4], |
| 244 | SkISize imageSize, |
| 245 | GrSurfaceOrigin imageOrigin, |
| 246 | sk_sp<SkColorSpace> imageColorSpace, |
| 247 | TextureReleaseProc textureReleaseProc, |
| 248 | ReleaseContext releaseContext) { |
| 249 | sk_sp<GrRefCntedCallback> releaseHelper; |
| 250 | if (textureReleaseProc) { |
| 251 | releaseHelper.reset(new GrRefCntedCallback(textureReleaseProc, releaseContext)); |
| 252 | } |
| 253 | |
| 254 | int numTextures; |
| 255 | if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) { |
| 256 | return nullptr; |
| 257 | } |
| 258 | |
| 259 | GrSurfaceProxyView tempViews[4]; |
| 260 | if (!SkImage_GpuBase::MakeTempTextureProxies(ctx, yuvaTextures, numTextures, yuvaIndices, |
| 261 | imageOrigin, tempViews, |
| 262 | std::move(releaseHelper))) { |
| 263 | return nullptr; |
| 264 | } |
| 265 | |
| 266 | return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(ctx), imageSize, kNeedNewImageUniqueID, colorSpace, |
| 267 | tempViews, numTextures, yuvaIndices, imageOrigin, |
| 268 | imageColorSpace); |
| 269 | } |
| 270 | |
| 271 | sk_sp<SkImage> SkImage::MakeFromYUVAPixmaps(GrContext* context, SkYUVColorSpace yuvColorSpace, |
| 272 | const SkPixmap yuvaPixmaps[], |
| 273 | const SkYUVAIndex yuvaIndices[4], SkISize imageSize, |
| 274 | GrSurfaceOrigin imageOrigin, bool buildMips, |
| 275 | bool limitToMaxTextureSize, |
| 276 | sk_sp<SkColorSpace> imageColorSpace) { |
| 277 | if (!context) { |
| 278 | return nullptr; // until we impl this for raster backend |
| 279 | } |
| 280 | |
| 281 | int numPixmaps; |
| 282 | if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numPixmaps)) { |
| 283 | return nullptr; |
| 284 | } |
| 285 | |
| 286 | if (!context->priv().caps()->mipmapSupport()) { |
| 287 | buildMips = false; |
| 288 | } |
| 289 | |
| 290 | // Make proxies |
| 291 | GrSurfaceProxyView tempViews[4]; |
| 292 | for (int i = 0; i < numPixmaps; ++i) { |
| 293 | const SkPixmap* pixmap = &yuvaPixmaps[i]; |
| 294 | SkAutoPixmapStorage resized; |
| 295 | int maxTextureSize = context->priv().caps()->maxTextureSize(); |
| 296 | int maxDim = std::max(yuvaPixmaps[i].width(), yuvaPixmaps[i].height()); |
| 297 | if (limitToMaxTextureSize && maxDim > maxTextureSize) { |
| 298 | float scale = static_cast<float>(maxTextureSize) / maxDim; |
| 299 | int newWidth = std::min(static_cast<int>(yuvaPixmaps[i].width() * scale), maxTextureSize); |
| 300 | int newHeight = |
| 301 | std::min(static_cast<int>(yuvaPixmaps[i].height() * scale), maxTextureSize); |
| 302 | SkImageInfo info = yuvaPixmaps[i].info().makeWH(newWidth, newHeight); |
| 303 | if (!resized.tryAlloc(info) || |
| 304 | !yuvaPixmaps[i].scalePixels(resized, kLow_SkFilterQuality)) { |
| 305 | return nullptr; |
| 306 | } |
| 307 | pixmap = &resized; |
| 308 | } |
| 309 | // Turn the pixmap into a GrTextureProxy |
| 310 | SkBitmap bmp; |
| 311 | bmp.installPixels(*pixmap); |
| 312 | GrBitmapTextureMaker bitmapMaker(context, bmp, GrImageTexGenPolicy::kNew_Uncached_Budgeted); |
| 313 | GrMipmapped mipMapped = buildMips ? GrMipmapped::kYes : GrMipmapped::kNo; |
| 314 | GrSurfaceProxyView view; |
| 315 | tempViews[i] = bitmapMaker.view(mipMapped); |
| 316 | if (!tempViews[i]) { |
| 317 | return nullptr; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(context), imageSize, kNeedNewImageUniqueID, |
| 322 | yuvColorSpace, tempViews, numPixmaps, yuvaIndices, |
| 323 | imageOrigin, imageColorSpace); |
| 324 | } |
| 325 | |
| 326 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
| 327 | sk_sp<SkImage> SkImage_GpuYUVA::MakePromiseYUVATexture( |
| 328 | GrRecordingContext* context, |
| 329 | SkYUVColorSpace yuvColorSpace, |
| 330 | const GrBackendFormat yuvaFormats[], |
| 331 | const SkISize yuvaSizes[], |
| 332 | const SkYUVAIndex yuvaIndices[4], |
| 333 | int imageWidth, |
| 334 | int imageHeight, |
| 335 | GrSurfaceOrigin imageOrigin, |
| 336 | sk_sp<SkColorSpace> imageColorSpace, |
| 337 | PromiseImageTextureFulfillProc textureFulfillProc, |
| 338 | PromiseImageTextureReleaseProc textureReleaseProc, |
| 339 | PromiseImageTextureDoneProc promiseDoneProc, |
| 340 | PromiseImageTextureContext textureContexts[], |
| 341 | PromiseImageApiVersion version) { |
| 342 | int numTextures; |
| 343 | bool valid = SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures); |
| 344 | |
| 345 | // The contract here is that if 'promiseDoneProc' is passed in it should always be called, |
| 346 | // even if creation of the SkImage fails. Once we call MakePromiseImageLazyProxy it takes |
| 347 | // responsibility for calling the done proc. |
| 348 | if (!promiseDoneProc) { |
| 349 | return nullptr; |
| 350 | } |
| 351 | int proxiesCreated = 0; |
| 352 | SkScopeExit callDone([promiseDoneProc, textureContexts, numTextures, &proxiesCreated]() { |
| 353 | for (int i = proxiesCreated; i < numTextures; ++i) { |
| 354 | promiseDoneProc(textureContexts[i]); |
| 355 | } |
| 356 | }); |
| 357 | |
| 358 | if (!valid) { |
| 359 | return nullptr; |
| 360 | } |
| 361 | |
| 362 | if (!context) { |
| 363 | return nullptr; |
| 364 | } |
| 365 | |
| 366 | if (imageWidth <= 0 || imageHeight <= 0) { |
| 367 | return nullptr; |
| 368 | } |
| 369 | |
| 370 | SkAlphaType at = (-1 != yuvaIndices[SkYUVAIndex::kA_Index].fIndex) ? kPremul_SkAlphaType |
| 371 | : kOpaque_SkAlphaType; |
| 372 | SkImageInfo info = |
| 373 | SkImageInfo::Make(imageWidth, imageHeight, kAssumedColorType, at, imageColorSpace); |
| 374 | if (!SkImageInfoIsValid(info)) { |
| 375 | return nullptr; |
| 376 | } |
| 377 | |
| 378 | // verify sizes with expected texture count |
| 379 | for (int i = 0; i < numTextures; ++i) { |
| 380 | if (yuvaSizes[i].isEmpty()) { |
| 381 | return nullptr; |
| 382 | } |
| 383 | } |
| 384 | for (int i = numTextures; i < SkYUVASizeInfo::kMaxCount; ++i) { |
| 385 | if (!yuvaSizes[i].isEmpty()) { |
| 386 | return nullptr; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | // Get lazy proxies |
| 391 | GrSurfaceProxyView views[4]; |
| 392 | for (int texIdx = 0; texIdx < numTextures; ++texIdx) { |
| 393 | auto proxy = MakePromiseImageLazyProxy( |
| 394 | context, yuvaSizes[texIdx].width(), yuvaSizes[texIdx].height(), |
| 395 | yuvaFormats[texIdx], GrMipmapped::kNo, textureFulfillProc, textureReleaseProc, |
| 396 | promiseDoneProc, textureContexts[texIdx], version); |
| 397 | ++proxiesCreated; |
| 398 | if (!proxy) { |
| 399 | return nullptr; |
| 400 | } |
| 401 | views[texIdx] = GrSurfaceProxyView(std::move(proxy), imageOrigin, GrSwizzle("rgba" )); |
| 402 | } |
| 403 | |
| 404 | return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(context), SkISize{imageWidth, imageHeight}, |
| 405 | kNeedNewImageUniqueID, yuvColorSpace, views, numTextures, |
| 406 | yuvaIndices, imageOrigin, std::move(imageColorSpace)); |
| 407 | } |
| 408 | |