blob: 6b51303488d16b3e2802c0e0268431c792a22b93 [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"
Brian Salomonc65aec92017-03-09 09:03:58 -050019#include "effects/GrSRGBEffect.h"
20#include "effects/GrYUVEffect.h"
reed43fe6182015-09-08 08:37:36 -070021
22namespace {
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 */
28class YUVScoper {
29public:
30 bool init(GrYUVProvider*, SkYUVPlanesCache::Info*, void* planes[3], bool useCache);
31
32private:
33 // we only use one or the other of these
Hal Canary144caf52016-11-07 17:57:18 -050034 sk_sp<SkCachedData> fCachedData;
35 SkAutoMalloc fStorage;
reed43fe6182015-09-08 08:37:36 -070036};
37}
38
39bool 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();
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 } else {
msarett4984c3c2016-03-10 05:44:43 -080052 // Fetch yuv plane sizes for memory allocation.
53 if (!provider->onQueryYUV8(&yuvInfo->fSizeInfo, &yuvInfo->fColorSpace)) {
reed43fe6182015-09-08 08:37:36 -070054 return false;
55 }
56
57 // Allocate the memory for YUV
58 size_t totalSize(0);
msarett4984c3c2016-03-10 05:44:43 -080059 for (int i = 0; i < 3; i++) {
60 totalSize += yuvInfo->fSizeInfo.fWidthBytes[i] * yuvInfo->fSizeInfo.fSizes[i].fHeight;
reed43fe6182015-09-08 08:37:36 -070061 }
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 }
msarett4984c3c2016-03-10 05:44:43 -080069 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);
reed43fe6182015-09-08 08:37:36 -070073
msarett4984c3c2016-03-10 05:44:43 -080074 // Get the YUV planes.
75 if (!provider->onGetYUV8Planes(yuvInfo->fSizeInfo, planes)) {
reed43fe6182015-09-08 08:37:36 -070076 return false;
77 }
78
79 if (useCache) {
80 // Decoding is done, cache the resulting YUV planes
Hal Canary144caf52016-11-07 17:57:18 -050081 SkYUVPlanesCache::Add(provider->onGetID(), fCachedData.get(), yuvInfo);
reed43fe6182015-09-08 08:37:36 -070082 }
83 }
84 return true;
85}
86
Robert Phillips538f1a32017-03-08 14:32:55 -050087sk_sp<GrTextureProxy> GrYUVProvider::refAsTextureProxy(GrContext* ctx,
88 const GrSurfaceDesc& desc,
89 bool useCache) {
reed43fe6182015-09-08 08:37:36 -070090 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 Phillipsbc7a4fb2017-01-23 15:30:35 -050098 yuvDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
reed43fe6182015-09-08 08:37:36 -070099 yuvDesc.fConfig = kAlpha_8_GrPixelConfig;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500100 sk_sp<GrSurfaceContext> yuvTextureContexts[3];
msarett4984c3c2016-03-10 05:44:43 -0800101 for (int i = 0; i < 3; i++) {
102 yuvDesc.fWidth = yuvInfo.fSizeInfo.fSizes[i].fWidth;
103 yuvDesc.fHeight = yuvInfo.fSizeInfo.fSizes[i].fHeight;
reed43fe6182015-09-08 08:37:36 -0700104 // TODO: why do we need this check?
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500105 SkBackingFit fit =
msarett4984c3c2016-03-10 05:44:43 -0800106 (yuvDesc.fWidth != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth) ||
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500107 (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;
reed43fe6182015-09-08 08:37:36 -0700114 }
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500115
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 }
reed43fe6182015-09-08 08:37:36 -0700121 }
122
brianosmandfe4f2e2016-07-21 13:28:36 -0700123 // We never want to perform color-space conversion during the decode
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400124 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400125 SkBackingFit::kExact,
126 desc.fWidth, desc.fHeight,
127 desc.fConfig, nullptr,
128 desc.fSampleCnt));
129 if (!renderTargetContext) {
reed43fe6182015-09-08 08:37:36 -0700130 return nullptr;
131 }
132
reed43fe6182015-09-08 08:37:36 -0700133 GrPaint paint;
jbaumanb445a572016-06-09 13:24:48 -0700134 sk_sp<GrFragmentProcessor> yuvToRgbProcessor(
Robert Phillips296b1cc2017-03-15 10:42:12 -0400135 GrYUVEffect::MakeYUVToRGB(ctx->resourceProvider(),
Robert Phillipsf200a902017-01-30 13:27:37 -0500136 yuvTextureContexts[0]->asTextureProxyRef(),
137 yuvTextureContexts[1]->asTextureProxyRef(),
138 yuvTextureContexts[2]->asTextureProxyRef(),
jbaumanb445a572016-06-09 13:24:48 -0700139 yuvInfo.fSizeInfo.fSizes, yuvInfo.fColorSpace, false));
bungeman06ca8ec2016-06-09 08:01:03 -0700140 paint.addColorFragmentProcessor(std::move(yuvToRgbProcessor));
brianosman717abfd2016-05-11 11:43:35 -0700141
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 Osman964dec32017-01-26 09:32:33 -0500152 paint.addColorFragmentProcessor(GrSRGBEffect::Make(GrSRGBEffect::Mode::kSRGBToLinear));
brianosman717abfd2016-05-11 11:43:35 -0700153 }
154 }
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}