GrYUVProvider.cpp revision 6738c702423226619ee0172c12a3a007c5f68e57
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 "GrContext.h"
9#include "GrDrawContext.h"
10#include "GrYUVProvider.h"
11#include "effects/GrGammaEffect.h"
12#include "effects/GrYUVEffect.h"
13
14#include "SkCachedData.h"
15#include "SkRefCnt.h"
16#include "SkResourceCache.h"
17#include "SkYUVPlanesCache.h"
18
19namespace {
20/**
21 *  Helper class to manage the resources used for storing the YUV planar data. Depending on the
22 *  useCache option, we may find (and lock) the data in our ResourceCache, or we may have allocated
23 *  it in scratch storage.
24 */
25class YUVScoper {
26public:
27    bool init(GrYUVProvider*, SkYUVPlanesCache::Info*, void* planes[3], bool useCache);
28
29private:
30    // we only use one or the other of these
31    SkAutoTUnref<SkCachedData>  fCachedData;
32    SkAutoMalloc                fStorage;
33};
34}
35
36bool YUVScoper::init(GrYUVProvider* provider, SkYUVPlanesCache::Info* yuvInfo, void* planes[3],
37                     bool useCache) {
38    if (useCache) {
39        fCachedData.reset(SkYUVPlanesCache::FindAndRef(provider->onGetID(), yuvInfo));
40    }
41
42    if (fCachedData.get()) {
43        planes[0] = (void*)fCachedData->data();
44        planes[1] = (uint8_t*)planes[0] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVSizeInfo::kY] *
45                                           yuvInfo->fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight);
46        planes[2] = (uint8_t*)planes[1] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVSizeInfo::kU] *
47                                           yuvInfo->fSizeInfo.fSizes[SkYUVSizeInfo::kU].fHeight);
48    } else {
49        // Fetch yuv plane sizes for memory allocation.
50        if (!provider->onQueryYUV8(&yuvInfo->fSizeInfo, &yuvInfo->fColorSpace)) {
51            return false;
52        }
53
54        // Allocate the memory for YUV
55        size_t totalSize(0);
56        for (int i = 0; i < 3; i++) {
57            totalSize += yuvInfo->fSizeInfo.fWidthBytes[i] * yuvInfo->fSizeInfo.fSizes[i].fHeight;
58        }
59        if (useCache) {
60            fCachedData.reset(SkResourceCache::NewCachedData(totalSize));
61            planes[0] = fCachedData->writable_data();
62        } else {
63            fStorage.reset(totalSize);
64            planes[0] = fStorage.get();
65        }
66        planes[1] = (uint8_t*)planes[0] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVSizeInfo::kY] *
67                                           yuvInfo->fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight);
68        planes[2] = (uint8_t*)planes[1] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVSizeInfo::kU] *
69                                           yuvInfo->fSizeInfo.fSizes[SkYUVSizeInfo::kU].fHeight);
70
71        // Get the YUV planes.
72        if (!provider->onGetYUV8Planes(yuvInfo->fSizeInfo, planes)) {
73            return false;
74        }
75
76        if (useCache) {
77            // Decoding is done, cache the resulting YUV planes
78            SkYUVPlanesCache::Add(provider->onGetID(), fCachedData, yuvInfo);
79        }
80    }
81    return true;
82}
83
84sk_sp<GrTexture> GrYUVProvider::refAsTexture(GrContext* ctx,
85                                             const GrSurfaceDesc& desc,
86                                             bool useCache) {
87    SkYUVPlanesCache::Info yuvInfo;
88    void* planes[3];
89    YUVScoper scoper;
90    if (!scoper.init(this, &yuvInfo, planes, useCache)) {
91        return nullptr;
92    }
93
94    GrSurfaceDesc yuvDesc;
95    yuvDesc.fConfig = kAlpha_8_GrPixelConfig;
96    SkAutoTUnref<GrTexture> yuvTextures[3];
97    for (int i = 0; i < 3; i++) {
98        yuvDesc.fWidth  = yuvInfo.fSizeInfo.fSizes[i].fWidth;
99        yuvDesc.fHeight = yuvInfo.fSizeInfo.fSizes[i].fHeight;
100        // TODO: why do we need this check?
101        bool needsExactTexture =
102                (yuvDesc.fWidth  != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth) ||
103                (yuvDesc.fHeight != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight);
104        if (needsExactTexture) {
105            yuvTextures[i].reset(ctx->textureProvider()->createTexture(yuvDesc, SkBudgeted::kYes));
106        } else {
107            yuvTextures[i].reset(ctx->textureProvider()->createApproxTexture(yuvDesc));
108        }
109        if (!yuvTextures[i] ||
110            !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight, yuvDesc.fConfig,
111                                         planes[i], yuvInfo.fSizeInfo.fWidthBytes[i])) {
112                return nullptr;
113            }
114    }
115
116    // We never want to perform color-space conversion during the decode
117    sk_sp<GrDrawContext> drawContext(ctx->makeDrawContext(SkBackingFit::kExact,
118                                                          desc.fWidth, desc.fHeight,
119                                                          desc.fConfig, nullptr,
120                                                          desc.fSampleCnt));
121    if (!drawContext) {
122        return nullptr;
123    }
124
125    GrPaint paint;
126    sk_sp<GrFragmentProcessor> yuvToRgbProcessor(
127        GrYUVEffect::MakeYUVToRGB(yuvTextures[0], yuvTextures[1], yuvTextures[2],
128                                  yuvInfo.fSizeInfo.fSizes, yuvInfo.fColorSpace, false));
129    paint.addColorFragmentProcessor(std::move(yuvToRgbProcessor));
130
131    // If we're decoding an sRGB image, the result of our linear math on the YUV planes is already
132    // in sRGB. (The encoding is just math on bytes, with no concept of color spaces.) So, we need
133    // to output the results of that math directly to the buffer that we will then consider sRGB.
134    // If we have sRGB write control, we can just tell the HW not to do the Linear -> sRGB step.
135    // Otherwise, we do our shader math to go from YUV -> sRGB, manually convert sRGB -> Linear,
136    // then let the HW convert Linear -> sRGB.
137    if (GrPixelConfigIsSRGB(desc.fConfig)) {
138        if (ctx->caps()->srgbWriteControl()) {
139            paint.setDisableOutputConversionToSRGB(true);
140        } else {
141            paint.addColorFragmentProcessor(GrGammaEffect::Make(2.2f));
142        }
143    }
144
145    paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
146    const SkRect r = SkRect::MakeIWH(yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth,
147            yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight);
148
149    drawContext->drawRect(GrNoClip(), paint, SkMatrix::I(), r);
150
151    return drawContext->asTexture();
152}
153