blob: 62ff0d0e3ecc72fcb7f190bee27d8118d058c918 [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"
Greg Danielfb3abcd2018-02-02 15:48:33 -050012#include "GrProxyProvider.h"
Brian Osman11052242016-10-27 14:47:55 -040013#include "GrRenderTargetContext.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050014#include "GrTextureProxy.h"
Hal Canary95e3c052017-01-11 12:44:43 -050015#include "SkAutoMalloc.h"
reed43fe6182015-09-08 08:37:36 -070016#include "SkCachedData.h"
17#include "SkRefCnt.h"
18#include "SkResourceCache.h"
19#include "SkYUVPlanesCache.h"
Christopher Cameron77e96662017-07-08 01:47:47 -070020#include "effects/GrNonlinearColorSpaceXformEffect.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050021#include "effects/GrSRGBEffect.h"
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050022#include "effects/GrYUVtoRGBEffect.h"
reed43fe6182015-09-08 08:37:36 -070023
Greg Daniel1445da62018-01-04 10:27:29 -050024sk_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));
reed43fe6182015-09-08 08:37:36 -070028
Greg Daniel1445da62018-01-04 10:27:29 -050029 if (data.get()) {
30 planes[0] = (void*)data->data();
msarett4984c3c2016-03-10 05:44:43 -080031 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);
reed43fe6182015-09-08 08:37:36 -070035 } else {
msarett4984c3c2016-03-10 05:44:43 -080036 // Fetch yuv plane sizes for memory allocation.
37 if (!provider->onQueryYUV8(&yuvInfo->fSizeInfo, &yuvInfo->fColorSpace)) {
Greg Daniel1445da62018-01-04 10:27:29 -050038 return nullptr;
reed43fe6182015-09-08 08:37:36 -070039 }
40
41 // Allocate the memory for YUV
42 size_t totalSize(0);
msarett4984c3c2016-03-10 05:44:43 -080043 for (int i = 0; i < 3; i++) {
44 totalSize += yuvInfo->fSizeInfo.fWidthBytes[i] * yuvInfo->fSizeInfo.fSizes[i].fHeight;
reed43fe6182015-09-08 08:37:36 -070045 }
Greg Daniel1445da62018-01-04 10:27:29 -050046 data.reset(SkResourceCache::NewCachedData(totalSize));
47 planes[0] = data->writable_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
msarett4984c3c2016-03-10 05:44:43 -080053 // Get the YUV planes.
54 if (!provider->onGetYUV8Planes(yuvInfo->fSizeInfo, planes)) {
Greg Daniel1445da62018-01-04 10:27:29 -050055 return nullptr;
reed43fe6182015-09-08 08:37:36 -070056 }
57
Greg Daniel1445da62018-01-04 10:27:29 -050058 // Decoding is done, cache the resulting YUV planes
59 SkYUVPlanesCache::Add(provider->onGetID(), data.get(), yuvInfo);
reed43fe6182015-09-08 08:37:36 -070060 }
Greg Daniel1445da62018-01-04 10:27:29 -050061 return data;
reed43fe6182015-09-08 08:37:36 -070062}
63
Greg Danielfb3abcd2018-02-02 15:48:33 -050064void GrYUVProvider::YUVGen_DataReleaseProc(const void*, void* data) {
65 SkCachedData* cachedData = static_cast<SkCachedData*>(data);
66 SkASSERT(cachedData);
67 cachedData->unref();
68}
69
Christopher Cameron77e96662017-07-08 01:47:47 -070070sk_sp<GrTextureProxy> GrYUVProvider::refAsTextureProxy(GrContext* ctx, const GrSurfaceDesc& desc,
Christopher Cameron77e96662017-07-08 01:47:47 -070071 const SkColorSpace* srcColorSpace,
72 const SkColorSpace* dstColorSpace) {
reed43fe6182015-09-08 08:37:36 -070073 SkYUVPlanesCache::Info yuvInfo;
74 void* planes[3];
Greg Daniel1445da62018-01-04 10:27:29 -050075
76 sk_sp<SkCachedData> dataStorage = init_provider(this, &yuvInfo, planes);
77 if (!dataStorage) {
reed43fe6182015-09-08 08:37:36 -070078 return nullptr;
79 }
80
Greg Danielfb3abcd2018-02-02 15:48:33 -050081 sk_sp<GrTextureProxy> yuvTextureProxies[3];
msarett4984c3c2016-03-10 05:44:43 -080082 for (int i = 0; i < 3; i++) {
Greg Danielfb3abcd2018-02-02 15:48:33 -050083 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 Phillipsbc7a4fb2017-01-23 15:30:35 -050088 SkBackingFit fit =
Greg Danielfb3abcd2018-02-02 15:48:33 -050089 (componentWidth != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth) ||
90 (componentHeight != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight)
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050091 ? SkBackingFit::kExact : SkBackingFit::kApprox;
92
Greg Danielfb3abcd2018-02-02 15:48:33 -050093 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 Phillipsbc7a4fb2017-01-23 15:30:35 -0500105
Greg Danielfb3abcd2018-02-02 15:48:33 -0500106 auto proxyProvider = ctx->contextPriv().proxyProvider();
107 yuvTextureProxies[i] = proxyProvider->createTextureProxy(yuvImage, kNone_GrSurfaceFlags,
108 kTopLeft_GrSurfaceOrigin,
109 0, SkBudgeted::kYes, fit);
reed43fe6182015-09-08 08:37:36 -0700110 }
111
brianosmandfe4f2e2016-07-21 13:28:36 -0700112 // We never want to perform color-space conversion during the decode
Greg Daniele1da1d92017-10-06 15:59:27 -0400113 // TODO: investigate preallocating mip maps here
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400114 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeDeferredRenderTargetContext(
Robert Phillipse44ef102017-07-21 15:37:19 -0400115 SkBackingFit::kExact,
116 desc.fWidth, desc.fHeight,
117 desc.fConfig, nullptr,
118 desc.fSampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400119 GrMipMapped::kNo,
Robert Phillipse44ef102017-07-21 15:37:19 -0400120 kTopLeft_GrSurfaceOrigin));
Brian Osman11052242016-10-27 14:47:55 -0400121 if (!renderTargetContext) {
reed43fe6182015-09-08 08:37:36 -0700122 return nullptr;
123 }
124
reed43fe6182015-09-08 08:37:36 -0700125 GrPaint paint;
Brian Salomonaff329b2017-08-11 09:40:37 -0400126 auto yuvToRgbProcessor =
Greg Danielfb3abcd2018-02-02 15:48:33 -0500127 GrYUVtoRGBEffect::Make(std::move(yuvTextureProxies[0]),
128 std::move(yuvTextureProxies[1]),
129 std::move(yuvTextureProxies[2]),
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500130 yuvInfo.fSizeInfo.fSizes, yuvInfo.fColorSpace, false);
bungeman06ca8ec2016-06-09 08:01:03 -0700131 paint.addColorFragmentProcessor(std::move(yuvToRgbProcessor));
brianosman717abfd2016-05-11 11:43:35 -0700132
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 Reed98308fb2017-07-07 08:28:13 -0400143 paint.addColorFragmentProcessor(GrSRGBEffect::Make(GrSRGBEffect::Mode::kSRGBToLinear,
144 GrSRGBEffect::Alpha::kOpaque));
brianosman717abfd2016-05-11 11:43:35 -0700145 }
146 }
147
Christopher Cameron77e96662017-07-08 01:47:47 -0700148 // 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 Salomonaff329b2017-08-11 09:40:37 -0400150 std::unique_ptr<GrFragmentProcessor> colorConversionProcessor =
Christopher Cameron77e96662017-07-08 01:47:47 -0700151 GrNonlinearColorSpaceXformEffect::Make(srcColorSpace, dstColorSpace);
152 if (colorConversionProcessor) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400153 paint.addColorFragmentProcessor(std::move(colorConversionProcessor));
Christopher Cameron77e96662017-07-08 01:47:47 -0700154 }
155
reed374772b2016-10-05 17:33:02 -0700156 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
msarett4984c3c2016-03-10 05:44:43 -0800157 const SkRect r = SkRect::MakeIWH(yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth,
Robert Phillips67c18d62017-01-20 12:44:06 -0500158 yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight);
reed43fe6182015-09-08 08:37:36 -0700159
Brian Salomon82f44312017-01-11 13:42:54 -0500160 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), r);
reed43fe6182015-09-08 08:37:36 -0700161
Robert Phillips538f1a32017-03-08 14:32:55 -0500162 return renderTargetContext->asTextureProxyRef();
reed43fe6182015-09-08 08:37:36 -0700163}