GrYUVProvider.cpp revision 45d6303f6e8403db9499ab28494f672b2bcd034e
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
8#include "GrYUVProvider.h"
9#include "GrClip.h"
10#include "GrContext.h"
11#include "GrContextPriv.h"
12#include "GrRenderTargetContext.h"
13#include "GrTextureProxy.h"
14#include "SkAutoMalloc.h"
15#include "SkCachedData.h"
16#include "SkRefCnt.h"
17#include "SkResourceCache.h"
18#include "SkYUVPlanesCache.h"
19#include "effects/GrNonlinearColorSpaceXformEffect.h"
20#include "effects/GrSRGBEffect.h"
21#include "effects/GrYUVEffect.h"
22
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
35    sk_sp<SkCachedData>  fCachedData;
36    SkAutoMalloc         fStorage;
37};
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();
48        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);
52    } else {
53        // Fetch yuv plane sizes for memory allocation.
54        if (!provider->onQueryYUV8(&yuvInfo->fSizeInfo, &yuvInfo->fColorSpace)) {
55            return false;
56        }
57
58        // Allocate the memory for YUV
59        size_t totalSize(0);
60        for (int i = 0; i < 3; i++) {
61            totalSize += yuvInfo->fSizeInfo.fWidthBytes[i] * yuvInfo->fSizeInfo.fSizes[i].fHeight;
62        }
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        }
70        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);
74
75        // Get the YUV planes.
76        if (!provider->onGetYUV8Planes(yuvInfo->fSizeInfo, planes)) {
77            return false;
78        }
79
80        if (useCache) {
81            // Decoding is done, cache the resulting YUV planes
82            SkYUVPlanesCache::Add(provider->onGetID(), fCachedData.get(), yuvInfo);
83        }
84    }
85    return true;
86}
87
88sk_sp<GrTextureProxy> GrYUVProvider::refAsTextureProxy(GrContext* ctx, const GrSurfaceDesc& desc,
89                                                       bool useCache,
90                                                       const SkColorSpace* srcColorSpace,
91                                                       const SkColorSpace* dstColorSpace) {
92    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;
100    yuvDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
101    yuvDesc.fConfig = kAlpha_8_GrPixelConfig;
102    sk_sp<GrSurfaceContext> yuvTextureContexts[3];
103    for (int i = 0; i < 3; i++) {
104        yuvDesc.fWidth  = yuvInfo.fSizeInfo.fSizes[i].fWidth;
105        yuvDesc.fHeight = yuvInfo.fSizeInfo.fSizes[i].fHeight;
106        // TODO: why do we need this check?
107        SkBackingFit fit =
108                (yuvDesc.fWidth  != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth) ||
109                (yuvDesc.fHeight != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight)
110                    ? SkBackingFit::kExact : SkBackingFit::kApprox;
111
112        yuvTextureContexts[i] = ctx->contextPriv().makeDeferredSurfaceContext(yuvDesc,
113                                                                              GrMipMapped::kNo,
114                                                                              fit,
115                                                                              SkBudgeted::kYes);
116        if (!yuvTextureContexts[i]) {
117            return nullptr;
118        }
119
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        }
125    }
126
127    // We never want to perform color-space conversion during the decode
128    // TODO: investigate preallocating mip maps here
129    sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeDeferredRenderTargetContext(
130                                                                    SkBackingFit::kExact,
131                                                                    desc.fWidth, desc.fHeight,
132                                                                    desc.fConfig, nullptr,
133                                                                    desc.fSampleCnt,
134                                                                    GrMipMapped::kNo,
135                                                                    kTopLeft_GrSurfaceOrigin));
136    if (!renderTargetContext) {
137        return nullptr;
138    }
139
140    GrPaint paint;
141    auto yuvToRgbProcessor =
142            GrYUVEffect::MakeYUVToRGB(yuvTextureContexts[0]->asTextureProxyRef(),
143                                      yuvTextureContexts[1]->asTextureProxyRef(),
144                                      yuvTextureContexts[2]->asTextureProxyRef(),
145                                      yuvInfo.fSizeInfo.fSizes, yuvInfo.fColorSpace, false);
146    paint.addColorFragmentProcessor(std::move(yuvToRgbProcessor));
147
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 {
158            paint.addColorFragmentProcessor(GrSRGBEffect::Make(GrSRGBEffect::Mode::kSRGBToLinear,
159                                                               GrSRGBEffect::Alpha::kOpaque));
160        }
161    }
162
163    // 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.
165    std::unique_ptr<GrFragmentProcessor> colorConversionProcessor =
166            GrNonlinearColorSpaceXformEffect::Make(srcColorSpace, dstColorSpace);
167    if (colorConversionProcessor) {
168        paint.addColorFragmentProcessor(std::move(colorConversionProcessor));
169    }
170
171    paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
172    const SkRect r = SkRect::MakeIWH(yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth,
173                                     yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight);
174
175    renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), r);
176
177    return renderTargetContext->asTextureProxyRef();
178}
179