blob: c6f9794192454c85ac0ecac15628209b45a2cbdc [file] [log] [blame]
bsalomonf267c1e2016-02-01 13:16:14 -08001/*
2 * Copyright 2016 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 "GrTextureToYUVPlanes.h"
9#include "effects/GrSimpleTextureEffect.h"
10#include "effects/GrYUVEffect.h"
11#include "GrClip.h"
12#include "GrContext.h"
bsalomonf267c1e2016-02-01 13:16:14 -080013#include "GrPaint.h"
Brian Osman32342f02017-03-04 08:12:46 -050014#include "GrRenderTargetContext.h"
15#include "GrResourceProvider.h"
bsalomonf267c1e2016-02-01 13:16:14 -080016
17namespace {
bungeman06ca8ec2016-06-09 08:01:03 -070018 using MakeFPProc = sk_sp<GrFragmentProcessor> (*)(sk_sp<GrFragmentProcessor>,
19 SkYUVColorSpace colorSpace);
bsalomonf267c1e2016-02-01 13:16:14 -080020};
21
Robert Phillips296b1cc2017-03-15 10:42:12 -040022static bool convert_proxy(sk_sp<GrTextureProxy> src,
Robert Phillips538f1a32017-03-08 14:32:55 -050023 GrRenderTargetContext* dst, int dstW, int dstH,
24 SkYUVColorSpace colorSpace, MakeFPProc proc) {
bsalomonf267c1e2016-02-01 13:16:14 -080025
Robert Phillips67c18d62017-01-20 12:44:06 -050026 SkScalar xScale = SkIntToScalar(src->width()) / dstW;
27 SkScalar yScale = SkIntToScalar(src->height()) / dstH;
Brian Salomon514baff2016-11-17 15:17:07 -050028 GrSamplerParams::FilterMode filter;
bsalomonf267c1e2016-02-01 13:16:14 -080029 if (dstW == src->width() && dstW == src->height()) {
Brian Salomon514baff2016-11-17 15:17:07 -050030 filter = GrSamplerParams::kNone_FilterMode;
bsalomonf267c1e2016-02-01 13:16:14 -080031 } else {
Brian Salomon514baff2016-11-17 15:17:07 -050032 filter = GrSamplerParams::kBilerp_FilterMode;
bsalomonf267c1e2016-02-01 13:16:14 -080033 }
34
Robert Phillips296b1cc2017-03-15 10:42:12 -040035 GrResourceProvider* resourceProvider = dst->resourceProvider();
36
37 sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(resourceProvider, std::move(src),
38 nullptr,
Robert Phillips538f1a32017-03-08 14:32:55 -050039 SkMatrix::MakeScale(xScale, yScale),
40 filter));
bsalomonf267c1e2016-02-01 13:16:14 -080041 if (!fp) {
42 return false;
43 }
bungeman06ca8ec2016-06-09 08:01:03 -070044 fp = proc(std::move(fp), colorSpace);
bsalomonf267c1e2016-02-01 13:16:14 -080045 if (!fp) {
46 return false;
47 }
48 GrPaint paint;
reed374772b2016-10-05 17:33:02 -070049 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
bungeman06ca8ec2016-06-09 08:01:03 -070050 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomon82f44312017-01-11 13:42:54 -050051 dst->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
52 SkRect::MakeIWH(dstW, dstH));
bsalomonf267c1e2016-02-01 13:16:14 -080053 return true;
54}
55
Robert Phillips538f1a32017-03-08 14:32:55 -050056bool GrTextureToYUVPlanes(GrContext* context, sk_sp<GrTextureProxy> proxy,
57 const SkISize sizes[3], void* const planes[3],
bsalomonf267c1e2016-02-01 13:16:14 -080058 const size_t rowBytes[3], SkYUVColorSpace colorSpace) {
Robert Phillips538f1a32017-03-08 14:32:55 -050059 if (!context) {
60 return false;
61 }
62
63 {
bsalomonf267c1e2016-02-01 13:16:14 -080064 // Depending on the relative sizes of the y, u, and v planes we may do 1 to 3 draws/
65 // readbacks.
Brian Osman11052242016-10-27 14:47:55 -040066 sk_sp<GrRenderTargetContext> yuvRenderTargetContext;
67 sk_sp<GrRenderTargetContext> yRenderTargetContext;
68 sk_sp<GrRenderTargetContext> uvRenderTargetContext;
69 sk_sp<GrRenderTargetContext> uRenderTargetContext;
70 sk_sp<GrRenderTargetContext> vRenderTargetContext;
bsalomonf267c1e2016-02-01 13:16:14 -080071
bsalomonf267c1e2016-02-01 13:16:14 -080072 // We issue draw(s) to convert from RGBA to Y, U, and V. All three planes may have different
73 // sizes however we optimize for two other cases - all planes are the same (1 draw to YUV),
74 // and U and V are the same but Y differs (2 draws, one for Y, one for UV).
75 if (sizes[0] == sizes[1] && sizes[1] == sizes[2]) {
Robert Phillipsd83ec042017-04-10 11:10:22 -040076 yuvRenderTargetContext = context->makeDeferredRenderTargetContextWithFallback(
Brian Osman11052242016-10-27 14:47:55 -040077 SkBackingFit::kApprox,
78 sizes[0].fWidth,
79 sizes[0].fHeight,
80 kRGBA_8888_GrPixelConfig,
81 nullptr);
82 if (!yuvRenderTargetContext) {
bsalomonf267c1e2016-02-01 13:16:14 -080083 return false;
84 }
85 } else {
Robert Phillipsd83ec042017-04-10 11:10:22 -040086 yRenderTargetContext = context->makeDeferredRenderTargetContextWithFallback(
Brian Osman11052242016-10-27 14:47:55 -040087 SkBackingFit::kApprox,
88 sizes[0].fWidth,
89 sizes[0].fHeight,
90 kAlpha_8_GrPixelConfig,
91 nullptr);
92 if (!yRenderTargetContext) {
bsalomonf267c1e2016-02-01 13:16:14 -080093 return false;
94 }
95 if (sizes[1] == sizes[2]) {
bsalomonf267c1e2016-02-01 13:16:14 -080096 // TODO: Add support for GL_RG when available.
Robert Phillipsd83ec042017-04-10 11:10:22 -040097 uvRenderTargetContext = context->makeDeferredRenderTargetContextWithFallback(
Brian Osman11052242016-10-27 14:47:55 -040098 SkBackingFit::kApprox,
99 sizes[1].fWidth,
100 sizes[1].fHeight,
101 kRGBA_8888_GrPixelConfig,
102 nullptr);
103 if (!uvRenderTargetContext) {
bsalomonf267c1e2016-02-01 13:16:14 -0800104 return false;
105 }
106 } else {
Robert Phillipsd83ec042017-04-10 11:10:22 -0400107 uRenderTargetContext = context->makeDeferredRenderTargetContextWithFallback(
Brian Osman11052242016-10-27 14:47:55 -0400108 SkBackingFit::kApprox,
109 sizes[1].fWidth,
110 sizes[1].fHeight,
111 kAlpha_8_GrPixelConfig,
112 nullptr);
Robert Phillipsd83ec042017-04-10 11:10:22 -0400113 vRenderTargetContext = context->makeDeferredRenderTargetContextWithFallback(
Brian Osman11052242016-10-27 14:47:55 -0400114 SkBackingFit::kApprox,
115 sizes[2].fWidth,
116 sizes[2].fHeight,
117 kAlpha_8_GrPixelConfig,
118 nullptr);
119 if (!uRenderTargetContext || !vRenderTargetContext) {
bsalomonf267c1e2016-02-01 13:16:14 -0800120 return false;
121 }
122 }
123 }
124
125 // Do all the draws before any readback.
Brian Osman11052242016-10-27 14:47:55 -0400126 if (yuvRenderTargetContext) {
Robert Phillips296b1cc2017-03-15 10:42:12 -0400127 if (!convert_proxy(std::move(proxy), yuvRenderTargetContext.get(),
Robert Phillips538f1a32017-03-08 14:32:55 -0500128 sizes[0].fWidth, sizes[0].fHeight,
129 colorSpace, GrYUVEffect::MakeRGBToYUV)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800130 return false;
131 }
bsalomonf267c1e2016-02-01 13:16:14 -0800132 } else {
Brian Osman11052242016-10-27 14:47:55 -0400133 SkASSERT(yRenderTargetContext);
Robert Phillips296b1cc2017-03-15 10:42:12 -0400134 if (!convert_proxy(proxy, yRenderTargetContext.get(),
Robert Phillips538f1a32017-03-08 14:32:55 -0500135 sizes[0].fWidth, sizes[0].fHeight,
136 colorSpace, GrYUVEffect::MakeRGBToY)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800137 return false;
138 }
Brian Osman11052242016-10-27 14:47:55 -0400139 if (uvRenderTargetContext) {
Robert Phillips296b1cc2017-03-15 10:42:12 -0400140 if (!convert_proxy(std::move(proxy), uvRenderTargetContext.get(),
Robert Phillips538f1a32017-03-08 14:32:55 -0500141 sizes[1].fWidth, sizes[1].fHeight,
142 colorSpace, GrYUVEffect::MakeRGBToUV)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800143 return false;
144 }
145 } else {
Brian Osman11052242016-10-27 14:47:55 -0400146 SkASSERT(uRenderTargetContext && vRenderTargetContext);
Robert Phillips296b1cc2017-03-15 10:42:12 -0400147 if (!convert_proxy(proxy, uRenderTargetContext.get(),
Robert Phillips538f1a32017-03-08 14:32:55 -0500148 sizes[1].fWidth, sizes[1].fHeight,
149 colorSpace, GrYUVEffect::MakeRGBToU)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800150 return false;
151 }
Robert Phillips296b1cc2017-03-15 10:42:12 -0400152 if (!convert_proxy(std::move(proxy), vRenderTargetContext.get(),
Robert Phillips538f1a32017-03-08 14:32:55 -0500153 sizes[2].fWidth, sizes[2].fHeight,
154 colorSpace, GrYUVEffect::MakeRGBToV)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800155 return false;
156 }
157 }
158 }
159
Brian Osman11052242016-10-27 14:47:55 -0400160 if (yuvRenderTargetContext) {
bsalomonf267c1e2016-02-01 13:16:14 -0800161 SkASSERT(sizes[0] == sizes[1] && sizes[1] == sizes[2]);
162 SkISize yuvSize = sizes[0];
163 // We have no kRGB_888 pixel format, so readback rgba and then copy three channels.
164 SkAutoSTMalloc<128 * 128, uint32_t> tempYUV(yuvSize.fWidth * yuvSize.fHeight);
Robert Phillips276be052017-01-13 14:55:50 -0500165
166 const SkImageInfo ii = SkImageInfo::Make(yuvSize.fWidth, yuvSize.fHeight,
167 kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
168 if (!yuvRenderTargetContext->readPixels(ii, tempYUV.get(), 0, 0, 0)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800169 return false;
170 }
171 size_t yRowBytes = rowBytes[0] ? rowBytes[0] : yuvSize.fWidth;
172 size_t uRowBytes = rowBytes[1] ? rowBytes[1] : yuvSize.fWidth;
173 size_t vRowBytes = rowBytes[2] ? rowBytes[2] : yuvSize.fWidth;
174 if (yRowBytes < (size_t)yuvSize.fWidth || uRowBytes < (size_t)yuvSize.fWidth ||
175 vRowBytes < (size_t)yuvSize.fWidth) {
176 return false;
177 }
178 for (int j = 0; j < yuvSize.fHeight; ++j) {
179 for (int i = 0; i < yuvSize.fWidth; ++i) {
180 // These writes could surely be made more efficient.
181 uint32_t y = GrColorUnpackR(tempYUV.get()[j * yuvSize.fWidth + i]);
182 uint32_t u = GrColorUnpackG(tempYUV.get()[j * yuvSize.fWidth + i]);
183 uint32_t v = GrColorUnpackB(tempYUV.get()[j * yuvSize.fWidth + i]);
184 uint8_t* yLoc = ((uint8_t*)planes[0]) + j * yRowBytes + i;
185 uint8_t* uLoc = ((uint8_t*)planes[1]) + j * uRowBytes + i;
186 uint8_t* vLoc = ((uint8_t*)planes[2]) + j * vRowBytes + i;
187 *yLoc = y;
188 *uLoc = u;
189 *vLoc = v;
190 }
191 }
192 return true;
193 } else {
Brian Osman11052242016-10-27 14:47:55 -0400194 SkASSERT(yRenderTargetContext);
Robert Phillips276be052017-01-13 14:55:50 -0500195
196 SkImageInfo ii = SkImageInfo::MakeA8(sizes[0].fWidth, sizes[0].fHeight);
197 if (!yRenderTargetContext->readPixels(ii, planes[0], rowBytes[0], 0, 0)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800198 return false;
199 }
Robert Phillips276be052017-01-13 14:55:50 -0500200
Brian Osman11052242016-10-27 14:47:55 -0400201 if (uvRenderTargetContext) {
bsalomonf267c1e2016-02-01 13:16:14 -0800202 SkASSERT(sizes[1].fWidth == sizes[2].fWidth);
203 SkISize uvSize = sizes[1];
204 // We have no kRG_88 pixel format, so readback rgba and then copy two channels.
205 SkAutoSTMalloc<128 * 128, uint32_t> tempUV(uvSize.fWidth * uvSize.fHeight);
Robert Phillips276be052017-01-13 14:55:50 -0500206
207 ii = SkImageInfo::Make(uvSize.fWidth, uvSize.fHeight,
208 kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
209
210 if (!uvRenderTargetContext->readPixels(ii, tempUV.get(), 0, 0, 0)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800211 return false;
212 }
213
214 size_t uRowBytes = rowBytes[1] ? rowBytes[1] : uvSize.fWidth;
215 size_t vRowBytes = rowBytes[2] ? rowBytes[2] : uvSize.fWidth;
216 if (uRowBytes < (size_t)uvSize.fWidth || vRowBytes < (size_t)uvSize.fWidth) {
217 return false;
218 }
219 for (int j = 0; j < uvSize.fHeight; ++j) {
220 for (int i = 0; i < uvSize.fWidth; ++i) {
221 // These writes could surely be made more efficient.
222 uint32_t u = GrColorUnpackR(tempUV.get()[j * uvSize.fWidth + i]);
223 uint32_t v = GrColorUnpackG(tempUV.get()[j * uvSize.fWidth + i]);
224 uint8_t* uLoc = ((uint8_t*)planes[1]) + j * uRowBytes + i;
225 uint8_t* vLoc = ((uint8_t*)planes[2]) + j * vRowBytes + i;
226 *uLoc = u;
227 *vLoc = v;
228 }
229 }
230 return true;
231 } else {
Brian Osman11052242016-10-27 14:47:55 -0400232 SkASSERT(uRenderTargetContext && vRenderTargetContext);
Robert Phillips276be052017-01-13 14:55:50 -0500233
234 ii = SkImageInfo::MakeA8(sizes[1].fWidth, sizes[1].fHeight);
235 if (!uRenderTargetContext->readPixels(ii, planes[1], rowBytes[1], 0, 0)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800236 return false;
237 }
Robert Phillips276be052017-01-13 14:55:50 -0500238
239 ii = SkImageInfo::MakeA8(sizes[2].fWidth, sizes[2].fHeight);
240 if (!vRenderTargetContext->readPixels(ii, planes[2], rowBytes[2], 0, 0)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800241 return false;
242 }
Robert Phillips276be052017-01-13 14:55:50 -0500243
bsalomonf267c1e2016-02-01 13:16:14 -0800244 return true;
245 }
246 }
247 }
248 return false;
249}