blob: da86eac35d78280542d5821bccccfcec15bd579e [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"
Brian Osman47c27512018-06-18 10:58:51 -040010#include "GrColorSpaceXform.h"
reed43fe6182015-09-08 08:37:36 -070011#include "GrContext.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050012#include "GrContextPriv.h"
Greg Danielfb3abcd2018-02-02 15:48:33 -050013#include "GrProxyProvider.h"
Brian Osman11052242016-10-27 14:47:55 -040014#include "GrRenderTargetContext.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050015#include "GrTextureProxy.h"
Hal Canary95e3c052017-01-11 12:44:43 -050016#include "SkAutoMalloc.h"
reed43fe6182015-09-08 08:37:36 -070017#include "SkCachedData.h"
18#include "SkRefCnt.h"
19#include "SkResourceCache.h"
20#include "SkYUVPlanesCache.h"
Robert Phillips94ade752018-10-09 12:32:31 -040021#include "SkYUVAIndex.h"
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050022#include "effects/GrYUVtoRGBEffect.h"
reed43fe6182015-09-08 08:37:36 -070023
Robert Phillipsb4a8eac2018-09-21 08:26:33 -040024sk_sp<SkCachedData> GrYUVProvider::getPlanes(SkYUVSizeInfo* size,
25 SkYUVColorSpace* colorSpace,
Jim Van Verthf99a6742018-10-18 16:13:18 +000026 const void* constPlanes[3]) {
Greg Daniel1445da62018-01-04 10:27:29 -050027 sk_sp<SkCachedData> data;
Robert Phillipsb4a8eac2018-09-21 08:26:33 -040028 SkYUVPlanesCache::Info yuvInfo;
29 data.reset(SkYUVPlanesCache::FindAndRef(this->onGetID(), &yuvInfo));
30
Jim Van Verthf99a6742018-10-18 16:13:18 +000031 void* planes[3];
reed43fe6182015-09-08 08:37:36 -070032
Greg Daniel1445da62018-01-04 10:27:29 -050033 if (data.get()) {
Jim Van Verthf99a6742018-10-18 16:13:18 +000034 planes[0] = (void*)data->data();
35 planes[1] = (uint8_t*)planes[0] + (yuvInfo.fSizeInfo.fWidthBytes[SkYUVSizeInfo::kY] *
36 yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight);
37 planes[2] = (uint8_t*)planes[1] + (yuvInfo.fSizeInfo.fWidthBytes[SkYUVSizeInfo::kU] *
38 yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kU].fHeight);
reed43fe6182015-09-08 08:37:36 -070039 } else {
msarett4984c3c2016-03-10 05:44:43 -080040 // Fetch yuv plane sizes for memory allocation.
Jim Van Verthf99a6742018-10-18 16:13:18 +000041 if (!this->onQueryYUV8(&yuvInfo.fSizeInfo, &yuvInfo.fColorSpace)) {
Greg Daniel1445da62018-01-04 10:27:29 -050042 return nullptr;
reed43fe6182015-09-08 08:37:36 -070043 }
44
Jim Van Verthf99a6742018-10-18 16:13:18 +000045 // Allocate the memory for YUV
reed43fe6182015-09-08 08:37:36 -070046 size_t totalSize(0);
Jim Van Verthf99a6742018-10-18 16:13:18 +000047 for (int i = 0; i < 3; i++) {
Robert Phillipsb4a8eac2018-09-21 08:26:33 -040048 totalSize += yuvInfo.fSizeInfo.fWidthBytes[i] * yuvInfo.fSizeInfo.fSizes[i].fHeight;
reed43fe6182015-09-08 08:37:36 -070049 }
Greg Daniel1445da62018-01-04 10:27:29 -050050 data.reset(SkResourceCache::NewCachedData(totalSize));
51 planes[0] = data->writable_data();
Jim Van Verthf99a6742018-10-18 16:13:18 +000052 planes[1] = (uint8_t*)planes[0] + (yuvInfo.fSizeInfo.fWidthBytes[SkYUVSizeInfo::kY] *
53 yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight);
54 planes[2] = (uint8_t*)planes[1] + (yuvInfo.fSizeInfo.fWidthBytes[SkYUVSizeInfo::kU] *
55 yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kU].fHeight);
reed43fe6182015-09-08 08:37:36 -070056
msarett4984c3c2016-03-10 05:44:43 -080057 // Get the YUV planes.
Jim Van Verthf99a6742018-10-18 16:13:18 +000058 if (!this->onGetYUV8Planes(yuvInfo.fSizeInfo, planes)) {
Greg Daniel1445da62018-01-04 10:27:29 -050059 return nullptr;
reed43fe6182015-09-08 08:37:36 -070060 }
61
Greg Daniel1445da62018-01-04 10:27:29 -050062 // Decoding is done, cache the resulting YUV planes
Robert Phillipsb4a8eac2018-09-21 08:26:33 -040063 SkYUVPlanesCache::Add(this->onGetID(), data.get(), &yuvInfo);
reed43fe6182015-09-08 08:37:36 -070064 }
Robert Phillipsb4a8eac2018-09-21 08:26:33 -040065
66 *size = yuvInfo.fSizeInfo;
67 *colorSpace = yuvInfo.fColorSpace;
68 constPlanes[0] = planes[0];
69 constPlanes[1] = planes[1];
70 constPlanes[2] = planes[2];
Greg Daniel1445da62018-01-04 10:27:29 -050071 return data;
reed43fe6182015-09-08 08:37:36 -070072}
73
Greg Danielfb3abcd2018-02-02 15:48:33 -050074void GrYUVProvider::YUVGen_DataReleaseProc(const void*, void* data) {
75 SkCachedData* cachedData = static_cast<SkCachedData*>(data);
76 SkASSERT(cachedData);
77 cachedData->unref();
78}
79
Christopher Cameron77e96662017-07-08 01:47:47 -070080sk_sp<GrTextureProxy> GrYUVProvider::refAsTextureProxy(GrContext* ctx, const GrSurfaceDesc& desc,
Brian Osman861ea5b2018-06-14 09:14:03 -040081 SkColorSpace* srcColorSpace,
82 SkColorSpace* dstColorSpace) {
Robert Phillipsb4a8eac2018-09-21 08:26:33 -040083 SkYUVSizeInfo yuvSizeInfo;
84 SkYUVColorSpace yuvColorSpace;
Jim Van Verthf99a6742018-10-18 16:13:18 +000085 const void* planes[3];
Greg Daniel1445da62018-01-04 10:27:29 -050086
Jim Van Verthf99a6742018-10-18 16:13:18 +000087 sk_sp<SkCachedData> dataStorage = this->getPlanes(&yuvSizeInfo, &yuvColorSpace, planes);
Greg Daniel1445da62018-01-04 10:27:29 -050088 if (!dataStorage) {
reed43fe6182015-09-08 08:37:36 -070089 return nullptr;
90 }
91
Jim Van Verthf99a6742018-10-18 16:13:18 +000092 sk_sp<GrTextureProxy> yuvTextureProxies[3];
93 for (int i = 0; i < 3; i++) {
Robert Phillipsb4a8eac2018-09-21 08:26:33 -040094 int componentWidth = yuvSizeInfo.fSizes[i].fWidth;
95 int componentHeight = yuvSizeInfo.fSizes[i].fHeight;
Greg Danielfb3abcd2018-02-02 15:48:33 -050096 // If the sizes of the components are not all the same we choose to create exact-match
Robert Phillipsb4a8eac2018-09-21 08:26:33 -040097 // textures for the smaller ones rather than add a texture domain to the draw.
98 // TODO: revisit this decision to improve texture reuse?
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050099 SkBackingFit fit =
Jim Van Verthf99a6742018-10-18 16:13:18 +0000100 (componentWidth != yuvSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth) ||
101 (componentHeight != yuvSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight)
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500102 ? SkBackingFit::kExact : SkBackingFit::kApprox;
103
Greg Danielfb3abcd2018-02-02 15:48:33 -0500104 SkImageInfo imageInfo = SkImageInfo::MakeA8(componentWidth, componentHeight);
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400105 SkPixmap pixmap(imageInfo, planes[i], yuvSizeInfo.fWidthBytes[i]);
Greg Danielfb3abcd2018-02-02 15:48:33 -0500106 SkCachedData* dataStoragePtr = dataStorage.get();
107 // We grab a ref to cached yuv data. When the SkImage we create below goes away it will call
108 // the YUVGen_DataReleaseProc which will release this ref.
109 // DDL TODO: Currently we end up creating a lazy proxy that will hold onto a ref to the
110 // SkImage in its lambda. This means that we'll keep the ref on the YUV data around for the
111 // life time of the proxy and not just upload. For non-DDL draws we should look into
112 // releasing this SkImage after uploads (by deleting the lambda after instantiation).
113 dataStoragePtr->ref();
114 sk_sp<SkImage> yuvImage = SkImage::MakeFromRaster(pixmap, YUVGen_DataReleaseProc,
115 dataStoragePtr);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500116
Greg Danielfb3abcd2018-02-02 15:48:33 -0500117 auto proxyProvider = ctx->contextPriv().proxyProvider();
118 yuvTextureProxies[i] = proxyProvider->createTextureProxy(yuvImage, kNone_GrSurfaceFlags,
Brian Salomonbdecacf2018-02-02 20:32:49 -0500119 1, SkBudgeted::kYes, fit);
Robert Phillipsc1b60662018-06-26 10:20:08 -0400120
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400121 SkASSERT(yuvTextureProxies[i]->width() == yuvSizeInfo.fSizes[i].fWidth);
122 SkASSERT(yuvTextureProxies[i]->height() == yuvSizeInfo.fSizes[i].fHeight);
reed43fe6182015-09-08 08:37:36 -0700123 }
124
Greg Daniele1da1d92017-10-06 15:59:27 -0400125 // TODO: investigate preallocating mip maps here
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500126 sk_sp<GrRenderTargetContext> renderTargetContext(
127 ctx->contextPriv().makeDeferredRenderTargetContext(
Brian Osman9363ac42018-06-01 16:10:53 -0400128 SkBackingFit::kExact, desc.fWidth, desc.fHeight, desc.fConfig, nullptr,
Brian Salomon366093f2018-02-13 09:25:22 -0500129 desc.fSampleCnt, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin));
Brian Osman11052242016-10-27 14:47:55 -0400130 if (!renderTargetContext) {
reed43fe6182015-09-08 08:37:36 -0700131 return nullptr;
132 }
133
Jim Van Verthf99a6742018-10-18 16:13:18 +0000134 // This code path only generates I420 (i.e., 3 separate plane) YUVs
135 SkYUVAIndex yuvaIndices[4] = {
136 { 0, SkColorChannel::kA },
137 { 1, SkColorChannel::kA },
138 { 2, SkColorChannel::kA },
139 { -1, SkColorChannel::kA }, // no alpha
140 };
141
reed43fe6182015-09-08 08:37:36 -0700142 GrPaint paint;
Jim Van Verthf99a6742018-10-18 16:13:18 +0000143 auto yuvToRgbProcessor =
144 GrYUVtoRGBEffect::Make(yuvTextureProxies, yuvaIndices, yuvColorSpace);
bungeman06ca8ec2016-06-09 08:01:03 -0700145 paint.addColorFragmentProcessor(std::move(yuvToRgbProcessor));
brianosman717abfd2016-05-11 11:43:35 -0700146
Christopher Cameron77e96662017-07-08 01:47:47 -0700147 // If the caller expects the pixels in a different color space than the one from the image,
148 // apply a color conversion to do this.
Brian Salomonaff329b2017-08-11 09:40:37 -0400149 std::unique_ptr<GrFragmentProcessor> colorConversionProcessor =
Brian Osman15f0f292018-10-01 14:14:46 -0400150 GrColorSpaceXformEffect::Make(srcColorSpace, kOpaque_SkAlphaType,
151 dstColorSpace, kOpaque_SkAlphaType);
Christopher Cameron77e96662017-07-08 01:47:47 -0700152 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);
Jim Van Verthf99a6742018-10-18 16:13:18 +0000157 const SkRect r = SkRect::MakeIWH(yuvSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth,
158 yuvSizeInfo.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}