reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 8 | #include "GrYUVProvider.h" |
| 9 | #include "GrClip.h" |
Brian Osman | 47c2751 | 2018-06-18 10:58:51 -0400 | [diff] [blame] | 10 | #include "GrColorSpaceXform.h" |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 11 | #include "GrContext.h" |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 12 | #include "GrContextPriv.h" |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 13 | #include "GrProxyProvider.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 14 | #include "GrRenderTargetContext.h" |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 15 | #include "GrTextureProxy.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 16 | #include "SkAutoMalloc.h" |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 17 | #include "SkCachedData.h" |
| 18 | #include "SkRefCnt.h" |
| 19 | #include "SkResourceCache.h" |
| 20 | #include "SkYUVPlanesCache.h" |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 21 | #include "effects/GrYUVtoRGBEffect.h" |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 22 | |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 23 | sk_sp<SkCachedData> init_provider(GrYUVProvider* provider, SkYUVPlanesCache::Info* yuvInfo, |
| 24 | void* planes[3]) { |
| 25 | sk_sp<SkCachedData> data; |
| 26 | data.reset(SkYUVPlanesCache::FindAndRef(provider->onGetID(), yuvInfo)); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 27 | |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 28 | if (data.get()) { |
| 29 | planes[0] = (void*)data->data(); |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 30 | planes[1] = (uint8_t*)planes[0] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVSizeInfo::kY] * |
| 31 | yuvInfo->fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight); |
| 32 | planes[2] = (uint8_t*)planes[1] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVSizeInfo::kU] * |
| 33 | yuvInfo->fSizeInfo.fSizes[SkYUVSizeInfo::kU].fHeight); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 34 | } else { |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 35 | // Fetch yuv plane sizes for memory allocation. |
| 36 | if (!provider->onQueryYUV8(&yuvInfo->fSizeInfo, &yuvInfo->fColorSpace)) { |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 37 | return nullptr; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // Allocate the memory for YUV |
| 41 | size_t totalSize(0); |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 42 | for (int i = 0; i < 3; i++) { |
| 43 | totalSize += yuvInfo->fSizeInfo.fWidthBytes[i] * yuvInfo->fSizeInfo.fSizes[i].fHeight; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 44 | } |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 45 | data.reset(SkResourceCache::NewCachedData(totalSize)); |
| 46 | planes[0] = data->writable_data(); |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 47 | planes[1] = (uint8_t*)planes[0] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVSizeInfo::kY] * |
| 48 | yuvInfo->fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight); |
| 49 | planes[2] = (uint8_t*)planes[1] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVSizeInfo::kU] * |
| 50 | yuvInfo->fSizeInfo.fSizes[SkYUVSizeInfo::kU].fHeight); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 51 | |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 52 | // Get the YUV planes. |
| 53 | if (!provider->onGetYUV8Planes(yuvInfo->fSizeInfo, planes)) { |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 54 | return nullptr; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 57 | // Decoding is done, cache the resulting YUV planes |
| 58 | SkYUVPlanesCache::Add(provider->onGetID(), data.get(), yuvInfo); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 59 | } |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 60 | return data; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 63 | void GrYUVProvider::YUVGen_DataReleaseProc(const void*, void* data) { |
| 64 | SkCachedData* cachedData = static_cast<SkCachedData*>(data); |
| 65 | SkASSERT(cachedData); |
| 66 | cachedData->unref(); |
| 67 | } |
| 68 | |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 69 | sk_sp<GrTextureProxy> GrYUVProvider::refAsTextureProxy(GrContext* ctx, const GrSurfaceDesc& desc, |
Brian Osman | 861ea5b | 2018-06-14 09:14:03 -0400 | [diff] [blame] | 70 | SkColorSpace* srcColorSpace, |
| 71 | SkColorSpace* dstColorSpace) { |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 72 | SkYUVPlanesCache::Info yuvInfo; |
| 73 | void* planes[3]; |
Greg Daniel | 1445da6 | 2018-01-04 10:27:29 -0500 | [diff] [blame] | 74 | |
| 75 | sk_sp<SkCachedData> dataStorage = init_provider(this, &yuvInfo, planes); |
| 76 | if (!dataStorage) { |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 77 | return nullptr; |
| 78 | } |
| 79 | |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 80 | sk_sp<GrTextureProxy> yuvTextureProxies[3]; |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 81 | for (int i = 0; i < 3; i++) { |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 82 | int componentWidth = yuvInfo.fSizeInfo.fSizes[i].fWidth; |
| 83 | int componentHeight = yuvInfo.fSizeInfo.fSizes[i].fHeight; |
| 84 | // If the sizes of the components are not all the same we choose to create exact-match |
| 85 | // textures for the smaller onces rather than add a texture domain to the draw. |
| 86 | // TODO: revisit this decision to imporve texture reuse? |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 87 | SkBackingFit fit = |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 88 | (componentWidth != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth) || |
| 89 | (componentHeight != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight) |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 90 | ? SkBackingFit::kExact : SkBackingFit::kApprox; |
| 91 | |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 92 | SkImageInfo imageInfo = SkImageInfo::MakeA8(componentWidth, componentHeight); |
| 93 | SkPixmap pixmap(imageInfo, planes[i], yuvInfo.fSizeInfo.fWidthBytes[i]); |
| 94 | SkCachedData* dataStoragePtr = dataStorage.get(); |
| 95 | // We grab a ref to cached yuv data. When the SkImage we create below goes away it will call |
| 96 | // the YUVGen_DataReleaseProc which will release this ref. |
| 97 | // DDL TODO: Currently we end up creating a lazy proxy that will hold onto a ref to the |
| 98 | // SkImage in its lambda. This means that we'll keep the ref on the YUV data around for the |
| 99 | // life time of the proxy and not just upload. For non-DDL draws we should look into |
| 100 | // releasing this SkImage after uploads (by deleting the lambda after instantiation). |
| 101 | dataStoragePtr->ref(); |
| 102 | sk_sp<SkImage> yuvImage = SkImage::MakeFromRaster(pixmap, YUVGen_DataReleaseProc, |
| 103 | dataStoragePtr); |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 104 | |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 105 | auto proxyProvider = ctx->contextPriv().proxyProvider(); |
| 106 | yuvTextureProxies[i] = proxyProvider->createTextureProxy(yuvImage, kNone_GrSurfaceFlags, |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 107 | 1, SkBudgeted::kYes, fit); |
Robert Phillips | c1b6066 | 2018-06-26 10:20:08 -0400 | [diff] [blame] | 108 | |
| 109 | SkASSERT(yuvTextureProxies[i]->width() == yuvInfo.fSizeInfo.fSizes[i].fWidth); |
| 110 | SkASSERT(yuvTextureProxies[i]->height() == yuvInfo.fSizeInfo.fSizes[i].fHeight); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 113 | // TODO: investigate preallocating mip maps here |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 114 | sk_sp<GrRenderTargetContext> renderTargetContext( |
| 115 | ctx->contextPriv().makeDeferredRenderTargetContext( |
Brian Osman | 9363ac4 | 2018-06-01 16:10:53 -0400 | [diff] [blame] | 116 | SkBackingFit::kExact, desc.fWidth, desc.fHeight, desc.fConfig, nullptr, |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 117 | desc.fSampleCnt, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin)); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 118 | if (!renderTargetContext) { |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 119 | return nullptr; |
| 120 | } |
| 121 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 122 | GrPaint paint; |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 123 | auto yuvToRgbProcessor = |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 124 | GrYUVtoRGBEffect::Make(std::move(yuvTextureProxies[0]), |
| 125 | std::move(yuvTextureProxies[1]), |
| 126 | std::move(yuvTextureProxies[2]), |
Weiliang Chen | 3e95e57 | 2018-05-30 15:15:23 -0400 | [diff] [blame] | 127 | yuvInfo.fColorSpace, false); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 128 | paint.addColorFragmentProcessor(std::move(yuvToRgbProcessor)); |
brianosman | 717abfd | 2016-05-11 11:43:35 -0700 | [diff] [blame] | 129 | |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 130 | // If the caller expects the pixels in a different color space than the one from the image, |
| 131 | // apply a color conversion to do this. |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 132 | std::unique_ptr<GrFragmentProcessor> colorConversionProcessor = |
Brian Osman | 21fc5ce | 2018-08-27 20:36:19 +0000 | [diff] [blame] | 133 | GrColorSpaceXformEffect::Make(srcColorSpace, kPremul_SkAlphaType, dstColorSpace); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 134 | if (colorConversionProcessor) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 135 | paint.addColorFragmentProcessor(std::move(colorConversionProcessor)); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 136 | } |
| 137 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 138 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 139 | const SkRect r = SkRect::MakeIWH(yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth, |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 140 | yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 141 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 142 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), r); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 143 | |
Robert Phillips | 538f1a3 | 2017-03-08 14:32:55 -0500 | [diff] [blame] | 144 | return renderTargetContext->asTextureProxyRef(); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 145 | } |