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