blob: 930a33e961f31d82384226021ad77c5afa391eab [file] [log] [blame]
reed43fe6182015-09-08 08:37:36 -07001/*
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 Salomonc65aec92017-03-09 09:03:58 -05008#include "GrYUVProvider.h"
9#include "GrClip.h"
reed43fe6182015-09-08 08:37:36 -070010#include "GrContext.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050011#include "GrContextPriv.h"
Brian Osman11052242016-10-27 14:47:55 -040012#include "GrRenderTargetContext.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050013#include "GrTextureProxy.h"
Hal Canary95e3c052017-01-11 12:44:43 -050014#include "SkAutoMalloc.h"
reed43fe6182015-09-08 08:37:36 -070015#include "SkCachedData.h"
16#include "SkRefCnt.h"
17#include "SkResourceCache.h"
18#include "SkYUVPlanesCache.h"
Christopher Cameron77e96662017-07-08 01:47:47 -070019#include "effects/GrNonlinearColorSpaceXformEffect.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050020#include "effects/GrSRGBEffect.h"
21#include "effects/GrYUVEffect.h"
reed43fe6182015-09-08 08:37:36 -070022
23namespace {
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 */
29class YUVScoper {
30public:
31 bool init(GrYUVProvider*, SkYUVPlanesCache::Info*, void* planes[3], bool useCache);
32
33private:
34 // we only use one or the other of these
Hal Canary144caf52016-11-07 17:57:18 -050035 sk_sp<SkCachedData> fCachedData;
36 SkAutoMalloc fStorage;
reed43fe6182015-09-08 08:37:36 -070037};
38}
39
40bool 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();
msarett4984c3c2016-03-10 05:44:43 -080048 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);
reed43fe6182015-09-08 08:37:36 -070052 } else {
msarett4984c3c2016-03-10 05:44:43 -080053 // Fetch yuv plane sizes for memory allocation.
54 if (!provider->onQueryYUV8(&yuvInfo->fSizeInfo, &yuvInfo->fColorSpace)) {
reed43fe6182015-09-08 08:37:36 -070055 return false;
56 }
57
58 // Allocate the memory for YUV
59 size_t totalSize(0);
msarett4984c3c2016-03-10 05:44:43 -080060 for (int i = 0; i < 3; i++) {
61 totalSize += yuvInfo->fSizeInfo.fWidthBytes[i] * yuvInfo->fSizeInfo.fSizes[i].fHeight;
reed43fe6182015-09-08 08:37:36 -070062 }
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 }
msarett4984c3c2016-03-10 05:44:43 -080070 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);
reed43fe6182015-09-08 08:37:36 -070074
msarett4984c3c2016-03-10 05:44:43 -080075 // Get the YUV planes.
76 if (!provider->onGetYUV8Planes(yuvInfo->fSizeInfo, planes)) {
reed43fe6182015-09-08 08:37:36 -070077 return false;
78 }
79
80 if (useCache) {
81 // Decoding is done, cache the resulting YUV planes
Hal Canary144caf52016-11-07 17:57:18 -050082 SkYUVPlanesCache::Add(provider->onGetID(), fCachedData.get(), yuvInfo);
reed43fe6182015-09-08 08:37:36 -070083 }
84 }
85 return true;
86}
87
Christopher Cameron77e96662017-07-08 01:47:47 -070088sk_sp<GrTextureProxy> GrYUVProvider::refAsTextureProxy(GrContext* ctx, const GrSurfaceDesc& desc,
89 bool useCache,
90 const SkColorSpace* srcColorSpace,
91 const SkColorSpace* dstColorSpace) {
reed43fe6182015-09-08 08:37:36 -070092 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 Phillipsbc7a4fb2017-01-23 15:30:35 -0500100 yuvDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
reed43fe6182015-09-08 08:37:36 -0700101 yuvDesc.fConfig = kAlpha_8_GrPixelConfig;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500102 sk_sp<GrSurfaceContext> yuvTextureContexts[3];
msarett4984c3c2016-03-10 05:44:43 -0800103 for (int i = 0; i < 3; i++) {
104 yuvDesc.fWidth = yuvInfo.fSizeInfo.fSizes[i].fWidth;
105 yuvDesc.fHeight = yuvInfo.fSizeInfo.fSizes[i].fHeight;
reed43fe6182015-09-08 08:37:36 -0700106 // TODO: why do we need this check?
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500107 SkBackingFit fit =
msarett4984c3c2016-03-10 05:44:43 -0800108 (yuvDesc.fWidth != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth) ||
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500109 (yuvDesc.fHeight != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight)
110 ? SkBackingFit::kExact : SkBackingFit::kApprox;
111
Greg Daniel65c7f662017-10-30 13:39:09 -0400112 yuvTextureContexts[i] = ctx->contextPriv().makeDeferredSurfaceContext(yuvDesc,
113 GrMipMapped::kNo,
114 fit,
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500115 SkBudgeted::kYes);
116 if (!yuvTextureContexts[i]) {
117 return nullptr;
reed43fe6182015-09-08 08:37:36 -0700118 }
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500119
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 }
reed43fe6182015-09-08 08:37:36 -0700125 }
126
brianosmandfe4f2e2016-07-21 13:28:36 -0700127 // We never want to perform color-space conversion during the decode
Greg Daniele1da1d92017-10-06 15:59:27 -0400128 // TODO: investigate preallocating mip maps here
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400129 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeDeferredRenderTargetContext(
Robert Phillipse44ef102017-07-21 15:37:19 -0400130 SkBackingFit::kExact,
131 desc.fWidth, desc.fHeight,
132 desc.fConfig, nullptr,
133 desc.fSampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400134 GrMipMapped::kNo,
Robert Phillipse44ef102017-07-21 15:37:19 -0400135 kTopLeft_GrSurfaceOrigin));
Brian Osman11052242016-10-27 14:47:55 -0400136 if (!renderTargetContext) {
reed43fe6182015-09-08 08:37:36 -0700137 return nullptr;
138 }
139
reed43fe6182015-09-08 08:37:36 -0700140 GrPaint paint;
Brian Salomonaff329b2017-08-11 09:40:37 -0400141 auto yuvToRgbProcessor =
142 GrYUVEffect::MakeYUVToRGB(yuvTextureContexts[0]->asTextureProxyRef(),
143 yuvTextureContexts[1]->asTextureProxyRef(),
144 yuvTextureContexts[2]->asTextureProxyRef(),
145 yuvInfo.fSizeInfo.fSizes, yuvInfo.fColorSpace, false);
bungeman06ca8ec2016-06-09 08:01:03 -0700146 paint.addColorFragmentProcessor(std::move(yuvToRgbProcessor));
brianosman717abfd2016-05-11 11:43:35 -0700147
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 Reed98308fb2017-07-07 08:28:13 -0400158 paint.addColorFragmentProcessor(GrSRGBEffect::Make(GrSRGBEffect::Mode::kSRGBToLinear,
159 GrSRGBEffect::Alpha::kOpaque));
brianosman717abfd2016-05-11 11:43:35 -0700160 }
161 }
162
Christopher Cameron77e96662017-07-08 01:47:47 -0700163 // 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 Salomonaff329b2017-08-11 09:40:37 -0400165 std::unique_ptr<GrFragmentProcessor> colorConversionProcessor =
Christopher Cameron77e96662017-07-08 01:47:47 -0700166 GrNonlinearColorSpaceXformEffect::Make(srcColorSpace, dstColorSpace);
167 if (colorConversionProcessor) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400168 paint.addColorFragmentProcessor(std::move(colorConversionProcessor));
Christopher Cameron77e96662017-07-08 01:47:47 -0700169 }
170
reed374772b2016-10-05 17:33:02 -0700171 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
msarett4984c3c2016-03-10 05:44:43 -0800172 const SkRect r = SkRect::MakeIWH(yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth,
Robert Phillips67c18d62017-01-20 12:44:06 -0500173 yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight);
reed43fe6182015-09-08 08:37:36 -0700174
Brian Salomon82f44312017-01-11 13:42:54 -0500175 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), r);
reed43fe6182015-09-08 08:37:36 -0700176
Robert Phillips538f1a32017-03-08 14:32:55 -0500177 return renderTargetContext->asTextureProxyRef();
reed43fe6182015-09-08 08:37:36 -0700178}