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" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 12 | #include "GrRenderTargetContext.h" |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 13 | #include "GrTextureProxy.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 14 | #include "SkAutoMalloc.h" |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 15 | #include "SkCachedData.h" |
| 16 | #include "SkRefCnt.h" |
| 17 | #include "SkResourceCache.h" |
| 18 | #include "SkYUVPlanesCache.h" |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 19 | #include "effects/GrNonlinearColorSpaceXformEffect.h" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 20 | #include "effects/GrSRGBEffect.h" |
| 21 | #include "effects/GrYUVEffect.h" |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | /** |
| 25 | * Helper class to manage the resources used for storing the YUV planar data. Depending on the |
| 26 | * useCache option, we may find (and lock) the data in our ResourceCache, or we may have allocated |
| 27 | * it in scratch storage. |
| 28 | */ |
| 29 | class YUVScoper { |
| 30 | public: |
| 31 | bool init(GrYUVProvider*, SkYUVPlanesCache::Info*, void* planes[3], bool useCache); |
| 32 | |
| 33 | private: |
| 34 | // we only use one or the other of these |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 35 | sk_sp<SkCachedData> fCachedData; |
| 36 | SkAutoMalloc fStorage; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 37 | }; |
| 38 | } |
| 39 | |
| 40 | bool YUVScoper::init(GrYUVProvider* provider, SkYUVPlanesCache::Info* yuvInfo, void* planes[3], |
| 41 | bool useCache) { |
| 42 | if (useCache) { |
| 43 | fCachedData.reset(SkYUVPlanesCache::FindAndRef(provider->onGetID(), yuvInfo)); |
| 44 | } |
| 45 | |
| 46 | if (fCachedData.get()) { |
| 47 | planes[0] = (void*)fCachedData->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 | } else { |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 53 | // Fetch yuv plane sizes for memory allocation. |
| 54 | if (!provider->onQueryYUV8(&yuvInfo->fSizeInfo, &yuvInfo->fColorSpace)) { |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | |
| 58 | // Allocate the memory for YUV |
| 59 | size_t totalSize(0); |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 60 | for (int i = 0; i < 3; i++) { |
| 61 | totalSize += yuvInfo->fSizeInfo.fWidthBytes[i] * yuvInfo->fSizeInfo.fSizes[i].fHeight; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 62 | } |
| 63 | if (useCache) { |
| 64 | fCachedData.reset(SkResourceCache::NewCachedData(totalSize)); |
| 65 | planes[0] = fCachedData->writable_data(); |
| 66 | } else { |
| 67 | fStorage.reset(totalSize); |
| 68 | planes[0] = fStorage.get(); |
| 69 | } |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 70 | planes[1] = (uint8_t*)planes[0] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVSizeInfo::kY] * |
| 71 | yuvInfo->fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight); |
| 72 | planes[2] = (uint8_t*)planes[1] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVSizeInfo::kU] * |
| 73 | yuvInfo->fSizeInfo.fSizes[SkYUVSizeInfo::kU].fHeight); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 74 | |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 75 | // Get the YUV planes. |
| 76 | if (!provider->onGetYUV8Planes(yuvInfo->fSizeInfo, planes)) { |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 77 | return false; |
| 78 | } |
| 79 | |
| 80 | if (useCache) { |
| 81 | // Decoding is done, cache the resulting YUV planes |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 82 | SkYUVPlanesCache::Add(provider->onGetID(), fCachedData.get(), yuvInfo); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 88 | sk_sp<GrTextureProxy> GrYUVProvider::refAsTextureProxy(GrContext* ctx, const GrSurfaceDesc& desc, |
| 89 | bool useCache, |
| 90 | const SkColorSpace* srcColorSpace, |
| 91 | const SkColorSpace* dstColorSpace) { |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 92 | SkYUVPlanesCache::Info yuvInfo; |
| 93 | void* planes[3]; |
| 94 | YUVScoper scoper; |
| 95 | if (!scoper.init(this, &yuvInfo, planes, useCache)) { |
| 96 | return nullptr; |
| 97 | } |
| 98 | |
| 99 | GrSurfaceDesc yuvDesc; |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 100 | yuvDesc.fOrigin = kTopLeft_GrSurfaceOrigin; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 101 | yuvDesc.fConfig = kAlpha_8_GrPixelConfig; |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 102 | sk_sp<GrSurfaceContext> yuvTextureContexts[3]; |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 103 | for (int i = 0; i < 3; i++) { |
| 104 | yuvDesc.fWidth = yuvInfo.fSizeInfo.fSizes[i].fWidth; |
| 105 | yuvDesc.fHeight = yuvInfo.fSizeInfo.fSizes[i].fHeight; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 106 | // TODO: why do we need this check? |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 107 | SkBackingFit fit = |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 108 | (yuvDesc.fWidth != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth) || |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 109 | (yuvDesc.fHeight != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight) |
| 110 | ? SkBackingFit::kExact : SkBackingFit::kApprox; |
| 111 | |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 112 | yuvTextureContexts[i] = ctx->contextPriv().makeDeferredSurfaceContext(yuvDesc, |
| 113 | GrMipMapped::kNo, |
| 114 | fit, |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 115 | SkBudgeted::kYes); |
| 116 | if (!yuvTextureContexts[i]) { |
| 117 | return nullptr; |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 118 | } |
Robert Phillips | bc7a4fb | 2017-01-23 15:30:35 -0500 | [diff] [blame] | 119 | |
| 120 | const SkImageInfo ii = SkImageInfo::MakeA8(yuvDesc.fWidth, yuvDesc.fHeight); |
| 121 | if (!yuvTextureContexts[i]->writePixels(ii, planes[i], |
| 122 | yuvInfo.fSizeInfo.fWidthBytes[i], 0, 0)) { |
| 123 | return nullptr; |
| 124 | } |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
brianosman | dfe4f2e | 2016-07-21 13:28:36 -0700 | [diff] [blame] | 127 | // We never want to perform color-space conversion during the decode |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 128 | // TODO: investigate preallocating mip maps here |
Robert Phillips | dd3b3f4 | 2017-04-24 10:57:28 -0400 | [diff] [blame] | 129 | sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeDeferredRenderTargetContext( |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 130 | SkBackingFit::kExact, |
| 131 | desc.fWidth, desc.fHeight, |
| 132 | desc.fConfig, nullptr, |
| 133 | desc.fSampleCnt, |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 134 | GrMipMapped::kNo, |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 135 | kTopLeft_GrSurfaceOrigin)); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 136 | if (!renderTargetContext) { |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 137 | return nullptr; |
| 138 | } |
| 139 | |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 140 | GrPaint paint; |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 141 | auto yuvToRgbProcessor = |
| 142 | GrYUVEffect::MakeYUVToRGB(yuvTextureContexts[0]->asTextureProxyRef(), |
| 143 | yuvTextureContexts[1]->asTextureProxyRef(), |
| 144 | yuvTextureContexts[2]->asTextureProxyRef(), |
| 145 | yuvInfo.fSizeInfo.fSizes, yuvInfo.fColorSpace, false); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 146 | paint.addColorFragmentProcessor(std::move(yuvToRgbProcessor)); |
brianosman | 717abfd | 2016-05-11 11:43:35 -0700 | [diff] [blame] | 147 | |
| 148 | // If we're decoding an sRGB image, the result of our linear math on the YUV planes is already |
| 149 | // in sRGB. (The encoding is just math on bytes, with no concept of color spaces.) So, we need |
| 150 | // to output the results of that math directly to the buffer that we will then consider sRGB. |
| 151 | // If we have sRGB write control, we can just tell the HW not to do the Linear -> sRGB step. |
| 152 | // Otherwise, we do our shader math to go from YUV -> sRGB, manually convert sRGB -> Linear, |
| 153 | // then let the HW convert Linear -> sRGB. |
| 154 | if (GrPixelConfigIsSRGB(desc.fConfig)) { |
| 155 | if (ctx->caps()->srgbWriteControl()) { |
| 156 | paint.setDisableOutputConversionToSRGB(true); |
| 157 | } else { |
Mike Reed | 98308fb | 2017-07-07 08:28:13 -0400 | [diff] [blame] | 158 | paint.addColorFragmentProcessor(GrSRGBEffect::Make(GrSRGBEffect::Mode::kSRGBToLinear, |
| 159 | GrSRGBEffect::Alpha::kOpaque)); |
brianosman | 717abfd | 2016-05-11 11:43:35 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 163 | // If the caller expects the pixels in a different color space than the one from the image, |
| 164 | // apply a color conversion to do this. |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 165 | std::unique_ptr<GrFragmentProcessor> colorConversionProcessor = |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 166 | GrNonlinearColorSpaceXformEffect::Make(srcColorSpace, dstColorSpace); |
| 167 | if (colorConversionProcessor) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 168 | paint.addColorFragmentProcessor(std::move(colorConversionProcessor)); |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 169 | } |
| 170 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 171 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 172 | const SkRect r = SkRect::MakeIWH(yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth, |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 173 | yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 174 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 175 | renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), r); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 176 | |
Robert Phillips | 538f1a3 | 2017-03-08 14:32:55 -0500 | [diff] [blame] | 177 | return renderTargetContext->asTextureProxyRef(); |
reed | 43fe618 | 2015-09-08 08:37:36 -0700 | [diff] [blame] | 178 | } |