blob: 0ca8cd90bd9fcd88a84b3f06fd144a1b97bbd4f8 [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"
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050021#include "effects/GrYUVtoRGBEffect.h"
reed43fe6182015-09-08 08:37:36 -070022
Greg Daniel1445da62018-01-04 10:27:29 -050023sk_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));
reed43fe6182015-09-08 08:37:36 -070027
Greg Daniel1445da62018-01-04 10:27:29 -050028 if (data.get()) {
29 planes[0] = (void*)data->data();
msarett4984c3c2016-03-10 05:44:43 -080030 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);
reed43fe6182015-09-08 08:37:36 -070034 } else {
msarett4984c3c2016-03-10 05:44:43 -080035 // Fetch yuv plane sizes for memory allocation.
36 if (!provider->onQueryYUV8(&yuvInfo->fSizeInfo, &yuvInfo->fColorSpace)) {
Greg Daniel1445da62018-01-04 10:27:29 -050037 return nullptr;
reed43fe6182015-09-08 08:37:36 -070038 }
39
40 // Allocate the memory for YUV
41 size_t totalSize(0);
msarett4984c3c2016-03-10 05:44:43 -080042 for (int i = 0; i < 3; i++) {
43 totalSize += yuvInfo->fSizeInfo.fWidthBytes[i] * yuvInfo->fSizeInfo.fSizes[i].fHeight;
reed43fe6182015-09-08 08:37:36 -070044 }
Greg Daniel1445da62018-01-04 10:27:29 -050045 data.reset(SkResourceCache::NewCachedData(totalSize));
46 planes[0] = data->writable_data();
msarett4984c3c2016-03-10 05:44:43 -080047 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);
reed43fe6182015-09-08 08:37:36 -070051
msarett4984c3c2016-03-10 05:44:43 -080052 // Get the YUV planes.
53 if (!provider->onGetYUV8Planes(yuvInfo->fSizeInfo, planes)) {
Greg Daniel1445da62018-01-04 10:27:29 -050054 return nullptr;
reed43fe6182015-09-08 08:37:36 -070055 }
56
Greg Daniel1445da62018-01-04 10:27:29 -050057 // Decoding is done, cache the resulting YUV planes
58 SkYUVPlanesCache::Add(provider->onGetID(), data.get(), yuvInfo);
reed43fe6182015-09-08 08:37:36 -070059 }
Greg Daniel1445da62018-01-04 10:27:29 -050060 return data;
reed43fe6182015-09-08 08:37:36 -070061}
62
Christopher Cameron77e96662017-07-08 01:47:47 -070063sk_sp<GrTextureProxy> GrYUVProvider::refAsTextureProxy(GrContext* ctx, const GrSurfaceDesc& desc,
Christopher Cameron77e96662017-07-08 01:47:47 -070064 const SkColorSpace* srcColorSpace,
65 const SkColorSpace* dstColorSpace) {
reed43fe6182015-09-08 08:37:36 -070066 SkYUVPlanesCache::Info yuvInfo;
67 void* planes[3];
Greg Daniel1445da62018-01-04 10:27:29 -050068
69 sk_sp<SkCachedData> dataStorage = init_provider(this, &yuvInfo, planes);
70 if (!dataStorage) {
reed43fe6182015-09-08 08:37:36 -070071 return nullptr;
72 }
73
74 GrSurfaceDesc yuvDesc;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050075 yuvDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
reed43fe6182015-09-08 08:37:36 -070076 yuvDesc.fConfig = kAlpha_8_GrPixelConfig;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050077 sk_sp<GrSurfaceContext> yuvTextureContexts[3];
msarett4984c3c2016-03-10 05:44:43 -080078 for (int i = 0; i < 3; i++) {
79 yuvDesc.fWidth = yuvInfo.fSizeInfo.fSizes[i].fWidth;
80 yuvDesc.fHeight = yuvInfo.fSizeInfo.fSizes[i].fHeight;
reed43fe6182015-09-08 08:37:36 -070081 // TODO: why do we need this check?
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050082 SkBackingFit fit =
msarett4984c3c2016-03-10 05:44:43 -080083 (yuvDesc.fWidth != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth) ||
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050084 (yuvDesc.fHeight != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight)
85 ? SkBackingFit::kExact : SkBackingFit::kApprox;
86
Greg Daniel65c7f662017-10-30 13:39:09 -040087 yuvTextureContexts[i] = ctx->contextPriv().makeDeferredSurfaceContext(yuvDesc,
88 GrMipMapped::kNo,
89 fit,
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050090 SkBudgeted::kYes);
91 if (!yuvTextureContexts[i]) {
92 return nullptr;
reed43fe6182015-09-08 08:37:36 -070093 }
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050094
95 const SkImageInfo ii = SkImageInfo::MakeA8(yuvDesc.fWidth, yuvDesc.fHeight);
96 if (!yuvTextureContexts[i]->writePixels(ii, planes[i],
97 yuvInfo.fSizeInfo.fWidthBytes[i], 0, 0)) {
98 return nullptr;
99 }
reed43fe6182015-09-08 08:37:36 -0700100 }
101
brianosmandfe4f2e2016-07-21 13:28:36 -0700102 // We never want to perform color-space conversion during the decode
Greg Daniele1da1d92017-10-06 15:59:27 -0400103 // TODO: investigate preallocating mip maps here
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400104 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeDeferredRenderTargetContext(
Robert Phillipse44ef102017-07-21 15:37:19 -0400105 SkBackingFit::kExact,
106 desc.fWidth, desc.fHeight,
107 desc.fConfig, nullptr,
108 desc.fSampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400109 GrMipMapped::kNo,
Robert Phillipse44ef102017-07-21 15:37:19 -0400110 kTopLeft_GrSurfaceOrigin));
Brian Osman11052242016-10-27 14:47:55 -0400111 if (!renderTargetContext) {
reed43fe6182015-09-08 08:37:36 -0700112 return nullptr;
113 }
114
reed43fe6182015-09-08 08:37:36 -0700115 GrPaint paint;
Brian Salomonaff329b2017-08-11 09:40:37 -0400116 auto yuvToRgbProcessor =
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500117 GrYUVtoRGBEffect::Make(yuvTextureContexts[0]->asTextureProxyRef(),
118 yuvTextureContexts[1]->asTextureProxyRef(),
119 yuvTextureContexts[2]->asTextureProxyRef(),
120 yuvInfo.fSizeInfo.fSizes, yuvInfo.fColorSpace, false);
bungeman06ca8ec2016-06-09 08:01:03 -0700121 paint.addColorFragmentProcessor(std::move(yuvToRgbProcessor));
brianosman717abfd2016-05-11 11:43:35 -0700122
123 // If we're decoding an sRGB image, the result of our linear math on the YUV planes is already
124 // in sRGB. (The encoding is just math on bytes, with no concept of color spaces.) So, we need
125 // to output the results of that math directly to the buffer that we will then consider sRGB.
126 // If we have sRGB write control, we can just tell the HW not to do the Linear -> sRGB step.
127 // Otherwise, we do our shader math to go from YUV -> sRGB, manually convert sRGB -> Linear,
128 // then let the HW convert Linear -> sRGB.
129 if (GrPixelConfigIsSRGB(desc.fConfig)) {
130 if (ctx->caps()->srgbWriteControl()) {
131 paint.setDisableOutputConversionToSRGB(true);
132 } else {
Mike Reed98308fb2017-07-07 08:28:13 -0400133 paint.addColorFragmentProcessor(GrSRGBEffect::Make(GrSRGBEffect::Mode::kSRGBToLinear,
134 GrSRGBEffect::Alpha::kOpaque));
brianosman717abfd2016-05-11 11:43:35 -0700135 }
136 }
137
Christopher Cameron77e96662017-07-08 01:47:47 -0700138 // If the caller expects the pixels in a different color space than the one from the image,
139 // apply a color conversion to do this.
Brian Salomonaff329b2017-08-11 09:40:37 -0400140 std::unique_ptr<GrFragmentProcessor> colorConversionProcessor =
Christopher Cameron77e96662017-07-08 01:47:47 -0700141 GrNonlinearColorSpaceXformEffect::Make(srcColorSpace, dstColorSpace);
142 if (colorConversionProcessor) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400143 paint.addColorFragmentProcessor(std::move(colorConversionProcessor));
Christopher Cameron77e96662017-07-08 01:47:47 -0700144 }
145
reed374772b2016-10-05 17:33:02 -0700146 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
msarett4984c3c2016-03-10 05:44:43 -0800147 const SkRect r = SkRect::MakeIWH(yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth,
Robert Phillips67c18d62017-01-20 12:44:06 -0500148 yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight);
reed43fe6182015-09-08 08:37:36 -0700149
Brian Salomon82f44312017-01-11 13:42:54 -0500150 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), r);
reed43fe6182015-09-08 08:37:36 -0700151
Robert Phillips538f1a32017-03-08 14:32:55 -0500152 return renderTargetContext->asTextureProxyRef();
reed43fe6182015-09-08 08:37:36 -0700153}