blob: 683bd638d2ab4a50c954dd384313162cad0d7693 [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
Brian Osman11052242016-10-27 14:47:55 -040022static bool convert_texture(GrTexture* src, GrRenderTargetContext* dst, int dstW, int dstH,
bungeman06ca8ec2016-06-09 08:01:03 -070023 SkYUVColorSpace colorSpace, MakeFPProc proc) {
bsalomonf267c1e2016-02-01 13:16:14 -080024
Robert Phillips67c18d62017-01-20 12:44:06 -050025 SkScalar xScale = SkIntToScalar(src->width()) / dstW;
26 SkScalar yScale = SkIntToScalar(src->height()) / dstH;
Brian Salomon514baff2016-11-17 15:17:07 -050027 GrSamplerParams::FilterMode filter;
bsalomonf267c1e2016-02-01 13:16:14 -080028 if (dstW == src->width() && dstW == src->height()) {
Brian Salomon514baff2016-11-17 15:17:07 -050029 filter = GrSamplerParams::kNone_FilterMode;
bsalomonf267c1e2016-02-01 13:16:14 -080030 } else {
Brian Salomon514baff2016-11-17 15:17:07 -050031 filter = GrSamplerParams::kBilerp_FilterMode;
bsalomonf267c1e2016-02-01 13:16:14 -080032 }
33
bungeman06ca8ec2016-06-09 08:01:03 -070034 sk_sp<GrFragmentProcessor> fp(
brianosman54f30c12016-07-18 10:53:52 -070035 GrSimpleTextureEffect::Make(src, nullptr, SkMatrix::MakeScale(xScale, yScale), filter));
bsalomonf267c1e2016-02-01 13:16:14 -080036 if (!fp) {
37 return false;
38 }
bungeman06ca8ec2016-06-09 08:01:03 -070039 fp = proc(std::move(fp), colorSpace);
bsalomonf267c1e2016-02-01 13:16:14 -080040 if (!fp) {
41 return false;
42 }
43 GrPaint paint;
reed374772b2016-10-05 17:33:02 -070044 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
bungeman06ca8ec2016-06-09 08:01:03 -070045 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomon82f44312017-01-11 13:42:54 -050046 dst->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
47 SkRect::MakeIWH(dstW, dstH));
bsalomonf267c1e2016-02-01 13:16:14 -080048 return true;
49}
50
51bool GrTextureToYUVPlanes(GrTexture* texture, const SkISize sizes[3], void* const planes[3],
52 const size_t rowBytes[3], SkYUVColorSpace colorSpace) {
53 if (GrContext* context = texture->getContext()) {
54 // Depending on the relative sizes of the y, u, and v planes we may do 1 to 3 draws/
55 // readbacks.
Brian Osman11052242016-10-27 14:47:55 -040056 sk_sp<GrRenderTargetContext> yuvRenderTargetContext;
57 sk_sp<GrRenderTargetContext> yRenderTargetContext;
58 sk_sp<GrRenderTargetContext> uvRenderTargetContext;
59 sk_sp<GrRenderTargetContext> uRenderTargetContext;
60 sk_sp<GrRenderTargetContext> vRenderTargetContext;
bsalomonf267c1e2016-02-01 13:16:14 -080061
bsalomonf267c1e2016-02-01 13:16:14 -080062 // We issue draw(s) to convert from RGBA to Y, U, and V. All three planes may have different
63 // sizes however we optimize for two other cases - all planes are the same (1 draw to YUV),
64 // and U and V are the same but Y differs (2 draws, one for Y, one for UV).
65 if (sizes[0] == sizes[1] && sizes[1] == sizes[2]) {
Brian Osman11052242016-10-27 14:47:55 -040066 yuvRenderTargetContext = context->makeRenderTargetContextWithFallback(
67 SkBackingFit::kApprox,
68 sizes[0].fWidth,
69 sizes[0].fHeight,
70 kRGBA_8888_GrPixelConfig,
71 nullptr);
72 if (!yuvRenderTargetContext) {
bsalomonf267c1e2016-02-01 13:16:14 -080073 return false;
74 }
75 } else {
Brian Osman11052242016-10-27 14:47:55 -040076 yRenderTargetContext = context->makeRenderTargetContextWithFallback(
77 SkBackingFit::kApprox,
78 sizes[0].fWidth,
79 sizes[0].fHeight,
80 kAlpha_8_GrPixelConfig,
81 nullptr);
82 if (!yRenderTargetContext) {
bsalomonf267c1e2016-02-01 13:16:14 -080083 return false;
84 }
85 if (sizes[1] == sizes[2]) {
bsalomonf267c1e2016-02-01 13:16:14 -080086 // TODO: Add support for GL_RG when available.
Brian Osman11052242016-10-27 14:47:55 -040087 uvRenderTargetContext = context->makeRenderTargetContextWithFallback(
88 SkBackingFit::kApprox,
89 sizes[1].fWidth,
90 sizes[1].fHeight,
91 kRGBA_8888_GrPixelConfig,
92 nullptr);
93 if (!uvRenderTargetContext) {
bsalomonf267c1e2016-02-01 13:16:14 -080094 return false;
95 }
96 } else {
Brian Osman11052242016-10-27 14:47:55 -040097 uRenderTargetContext = context->makeRenderTargetContextWithFallback(
98 SkBackingFit::kApprox,
99 sizes[1].fWidth,
100 sizes[1].fHeight,
101 kAlpha_8_GrPixelConfig,
102 nullptr);
103 vRenderTargetContext = context->makeRenderTargetContextWithFallback(
104 SkBackingFit::kApprox,
105 sizes[2].fWidth,
106 sizes[2].fHeight,
107 kAlpha_8_GrPixelConfig,
108 nullptr);
109 if (!uRenderTargetContext || !vRenderTargetContext) {
bsalomonf267c1e2016-02-01 13:16:14 -0800110 return false;
111 }
112 }
113 }
114
115 // Do all the draws before any readback.
Brian Osman11052242016-10-27 14:47:55 -0400116 if (yuvRenderTargetContext) {
117 if (!convert_texture(texture, yuvRenderTargetContext.get(),
robertphillipsebf30e82016-05-11 08:34:39 -0700118 sizes[0].fWidth, sizes[0].fHeight,
bungeman06ca8ec2016-06-09 08:01:03 -0700119 colorSpace, GrYUVEffect::MakeRGBToYUV)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800120 return false;
121 }
bsalomonf267c1e2016-02-01 13:16:14 -0800122 } else {
Brian Osman11052242016-10-27 14:47:55 -0400123 SkASSERT(yRenderTargetContext);
124 if (!convert_texture(texture, yRenderTargetContext.get(),
robertphillipsebf30e82016-05-11 08:34:39 -0700125 sizes[0].fWidth, sizes[0].fHeight,
bungeman06ca8ec2016-06-09 08:01:03 -0700126 colorSpace, GrYUVEffect::MakeRGBToY)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800127 return false;
128 }
Brian Osman11052242016-10-27 14:47:55 -0400129 if (uvRenderTargetContext) {
130 if (!convert_texture(texture, uvRenderTargetContext.get(),
robertphillipsebf30e82016-05-11 08:34:39 -0700131 sizes[1].fWidth, sizes[1].fHeight,
bungeman06ca8ec2016-06-09 08:01:03 -0700132 colorSpace, GrYUVEffect::MakeRGBToUV)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800133 return false;
134 }
135 } else {
Brian Osman11052242016-10-27 14:47:55 -0400136 SkASSERT(uRenderTargetContext && vRenderTargetContext);
137 if (!convert_texture(texture, uRenderTargetContext.get(),
robertphillipsebf30e82016-05-11 08:34:39 -0700138 sizes[1].fWidth, sizes[1].fHeight,
bungeman06ca8ec2016-06-09 08:01:03 -0700139 colorSpace, GrYUVEffect::MakeRGBToU)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800140 return false;
141 }
Brian Osman11052242016-10-27 14:47:55 -0400142 if (!convert_texture(texture, vRenderTargetContext.get(),
robertphillipsebf30e82016-05-11 08:34:39 -0700143 sizes[2].fWidth, sizes[2].fHeight,
bungeman06ca8ec2016-06-09 08:01:03 -0700144 colorSpace, GrYUVEffect::MakeRGBToV)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800145 return false;
146 }
147 }
148 }
149
Brian Osman11052242016-10-27 14:47:55 -0400150 if (yuvRenderTargetContext) {
bsalomonf267c1e2016-02-01 13:16:14 -0800151 SkASSERT(sizes[0] == sizes[1] && sizes[1] == sizes[2]);
152 SkISize yuvSize = sizes[0];
153 // We have no kRGB_888 pixel format, so readback rgba and then copy three channels.
154 SkAutoSTMalloc<128 * 128, uint32_t> tempYUV(yuvSize.fWidth * yuvSize.fHeight);
Robert Phillips276be052017-01-13 14:55:50 -0500155
156 const SkImageInfo ii = SkImageInfo::Make(yuvSize.fWidth, yuvSize.fHeight,
157 kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
158 if (!yuvRenderTargetContext->readPixels(ii, tempYUV.get(), 0, 0, 0)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800159 return false;
160 }
161 size_t yRowBytes = rowBytes[0] ? rowBytes[0] : yuvSize.fWidth;
162 size_t uRowBytes = rowBytes[1] ? rowBytes[1] : yuvSize.fWidth;
163 size_t vRowBytes = rowBytes[2] ? rowBytes[2] : yuvSize.fWidth;
164 if (yRowBytes < (size_t)yuvSize.fWidth || uRowBytes < (size_t)yuvSize.fWidth ||
165 vRowBytes < (size_t)yuvSize.fWidth) {
166 return false;
167 }
168 for (int j = 0; j < yuvSize.fHeight; ++j) {
169 for (int i = 0; i < yuvSize.fWidth; ++i) {
170 // These writes could surely be made more efficient.
171 uint32_t y = GrColorUnpackR(tempYUV.get()[j * yuvSize.fWidth + i]);
172 uint32_t u = GrColorUnpackG(tempYUV.get()[j * yuvSize.fWidth + i]);
173 uint32_t v = GrColorUnpackB(tempYUV.get()[j * yuvSize.fWidth + i]);
174 uint8_t* yLoc = ((uint8_t*)planes[0]) + j * yRowBytes + i;
175 uint8_t* uLoc = ((uint8_t*)planes[1]) + j * uRowBytes + i;
176 uint8_t* vLoc = ((uint8_t*)planes[2]) + j * vRowBytes + i;
177 *yLoc = y;
178 *uLoc = u;
179 *vLoc = v;
180 }
181 }
182 return true;
183 } else {
Brian Osman11052242016-10-27 14:47:55 -0400184 SkASSERT(yRenderTargetContext);
Robert Phillips276be052017-01-13 14:55:50 -0500185
186 SkImageInfo ii = SkImageInfo::MakeA8(sizes[0].fWidth, sizes[0].fHeight);
187 if (!yRenderTargetContext->readPixels(ii, planes[0], rowBytes[0], 0, 0)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800188 return false;
189 }
Robert Phillips276be052017-01-13 14:55:50 -0500190
Brian Osman11052242016-10-27 14:47:55 -0400191 if (uvRenderTargetContext) {
bsalomonf267c1e2016-02-01 13:16:14 -0800192 SkASSERT(sizes[1].fWidth == sizes[2].fWidth);
193 SkISize uvSize = sizes[1];
194 // We have no kRG_88 pixel format, so readback rgba and then copy two channels.
195 SkAutoSTMalloc<128 * 128, uint32_t> tempUV(uvSize.fWidth * uvSize.fHeight);
Robert Phillips276be052017-01-13 14:55:50 -0500196
197 ii = SkImageInfo::Make(uvSize.fWidth, uvSize.fHeight,
198 kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
199
200 if (!uvRenderTargetContext->readPixels(ii, tempUV.get(), 0, 0, 0)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800201 return false;
202 }
203
204 size_t uRowBytes = rowBytes[1] ? rowBytes[1] : uvSize.fWidth;
205 size_t vRowBytes = rowBytes[2] ? rowBytes[2] : uvSize.fWidth;
206 if (uRowBytes < (size_t)uvSize.fWidth || vRowBytes < (size_t)uvSize.fWidth) {
207 return false;
208 }
209 for (int j = 0; j < uvSize.fHeight; ++j) {
210 for (int i = 0; i < uvSize.fWidth; ++i) {
211 // These writes could surely be made more efficient.
212 uint32_t u = GrColorUnpackR(tempUV.get()[j * uvSize.fWidth + i]);
213 uint32_t v = GrColorUnpackG(tempUV.get()[j * uvSize.fWidth + i]);
214 uint8_t* uLoc = ((uint8_t*)planes[1]) + j * uRowBytes + i;
215 uint8_t* vLoc = ((uint8_t*)planes[2]) + j * vRowBytes + i;
216 *uLoc = u;
217 *vLoc = v;
218 }
219 }
220 return true;
221 } else {
Brian Osman11052242016-10-27 14:47:55 -0400222 SkASSERT(uRenderTargetContext && vRenderTargetContext);
Robert Phillips276be052017-01-13 14:55:50 -0500223
224 ii = SkImageInfo::MakeA8(sizes[1].fWidth, sizes[1].fHeight);
225 if (!uRenderTargetContext->readPixels(ii, planes[1], rowBytes[1], 0, 0)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800226 return false;
227 }
Robert Phillips276be052017-01-13 14:55:50 -0500228
229 ii = SkImageInfo::MakeA8(sizes[2].fWidth, sizes[2].fHeight);
230 if (!vRenderTargetContext->readPixels(ii, planes[2], rowBytes[2], 0, 0)) {
bsalomonf267c1e2016-02-01 13:16:14 -0800231 return false;
232 }
Robert Phillips276be052017-01-13 14:55:50 -0500233
bsalomonf267c1e2016-02-01 13:16:14 -0800234 return true;
235 }
236 }
237 }
238 return false;
239}