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