OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrTextureToYUVPlanes.h" | 8 #include "GrTextureToYUVPlanes.h" |
9 #include "effects/GrSimpleTextureEffect.h" | 9 #include "effects/GrSimpleTextureEffect.h" |
10 #include "effects/GrYUVEffect.h" | 10 #include "effects/GrYUVEffect.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 paint.addColorFragmentProcessor(fp); | 45 paint.addColorFragmentProcessor(fp); |
46 dst->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), SkRect::MakeIWH(dstW
, dstH)); | 46 dst->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), SkRect::MakeIWH(dstW
, dstH)); |
47 return true; | 47 return true; |
48 } | 48 } |
49 | 49 |
50 bool GrTextureToYUVPlanes(GrTexture* texture, const SkISize sizes[3], void* cons
t planes[3], | 50 bool GrTextureToYUVPlanes(GrTexture* texture, const SkISize sizes[3], void* cons
t planes[3], |
51 const size_t rowBytes[3], SkYUVColorSpace colorSpace)
{ | 51 const size_t rowBytes[3], SkYUVColorSpace colorSpace)
{ |
52 if (GrContext* context = texture->getContext()) { | 52 if (GrContext* context = texture->getContext()) { |
53 // Depending on the relative sizes of the y, u, and v planes we may do 1
to 3 draws/ | 53 // Depending on the relative sizes of the y, u, and v planes we may do 1
to 3 draws/ |
54 // readbacks. | 54 // readbacks. |
55 SkAutoTUnref<GrTexture> yuvTex; | 55 sk_sp<GrDrawContext> yuvDrawContext; |
56 SkAutoTUnref<GrTexture> yTex; | 56 sk_sp<GrDrawContext> yDrawContext; |
57 SkAutoTUnref<GrTexture> uvTex; | 57 sk_sp<GrDrawContext> uvDrawContext; |
58 SkAutoTUnref<GrTexture> uTex; | 58 sk_sp<GrDrawContext> uDrawContext; |
59 SkAutoTUnref<GrTexture> vTex; | 59 sk_sp<GrDrawContext> vDrawContext; |
60 | 60 |
61 GrPixelConfig singleChannelPixelConfig; | 61 GrPixelConfig singleChannelPixelConfig; |
62 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false))
{ | 62 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false))
{ |
63 singleChannelPixelConfig = kAlpha_8_GrPixelConfig; | 63 singleChannelPixelConfig = kAlpha_8_GrPixelConfig; |
64 } else { | 64 } else { |
65 singleChannelPixelConfig = kRGBA_8888_GrPixelConfig; | 65 singleChannelPixelConfig = kRGBA_8888_GrPixelConfig; |
66 } | 66 } |
67 | 67 |
68 // We issue draw(s) to convert from RGBA to Y, U, and V. All three plane
s may have different | 68 // We issue draw(s) to convert from RGBA to Y, U, and V. All three plane
s may have different |
69 // sizes however we optimize for two other cases - all planes are the sa
me (1 draw to YUV), | 69 // sizes however we optimize for two other cases - all planes are the sa
me (1 draw to YUV), |
70 // and U and V are the same but Y differs (2 draws, one for Y, one for U
V). | 70 // and U and V are the same but Y differs (2 draws, one for Y, one for U
V). |
71 if (sizes[0] == sizes[1] && sizes[1] == sizes[2]) { | 71 if (sizes[0] == sizes[1] && sizes[1] == sizes[2]) { |
72 GrSurfaceDesc yuvDesc; | 72 yuvDrawContext = context->newDrawContext(SkBackingFit::kApprox, |
73 yuvDesc.fConfig = kRGBA_8888_GrPixelConfig; | 73 sizes[0].fWidth, sizes[0].f
Height, |
74 yuvDesc.fFlags = kRenderTarget_GrSurfaceFlag; | 74 kRGBA_8888_GrPixelConfig); |
75 yuvDesc.fWidth = sizes[0].fWidth; | 75 if (!yuvDrawContext) { |
76 yuvDesc.fHeight = sizes[0].fHeight; | |
77 yuvTex.reset(context->textureProvider()->createApproxTexture(yuvDesc
)); | |
78 if (!yuvTex) { | |
79 return false; | 76 return false; |
80 } | 77 } |
81 } else { | 78 } else { |
82 GrSurfaceDesc yDesc; | 79 yDrawContext = context->newDrawContext(SkBackingFit::kApprox, |
83 yDesc.fConfig = singleChannelPixelConfig; | 80 sizes[0].fWidth, sizes[0].fHe
ight, |
84 yDesc.fFlags = kRenderTarget_GrSurfaceFlag; | 81 singleChannelPixelConfig); |
85 yDesc.fWidth = sizes[0].fWidth; | 82 if (!yDrawContext) { |
86 yDesc.fHeight = sizes[0].fHeight; | |
87 yTex.reset(context->textureProvider()->createApproxTexture(yDesc)); | |
88 if (!yTex) { | |
89 return false; | 83 return false; |
90 } | 84 } |
91 if (sizes[1] == sizes[2]) { | 85 if (sizes[1] == sizes[2]) { |
92 GrSurfaceDesc uvDesc; | |
93 // TODO: Add support for GL_RG when available. | 86 // TODO: Add support for GL_RG when available. |
94 uvDesc.fConfig = kRGBA_8888_GrPixelConfig; | 87 uvDrawContext = context->newDrawContext(SkBackingFit::kApprox, |
95 uvDesc.fFlags = kRenderTarget_GrSurfaceFlag; | 88 sizes[1].fWidth, sizes[1
].fHeight, |
96 uvDesc.fWidth = sizes[1].fWidth; | 89 kRGBA_8888_GrPixelConfig
); |
97 uvDesc.fHeight = sizes[1].fHeight; | 90 if (!uvDrawContext) { |
98 uvTex.reset(context->textureProvider()->createApproxTexture(uvDe
sc)); | |
99 if (!uvTex) { | |
100 return false; | 91 return false; |
101 } | 92 } |
102 } else { | 93 } else { |
103 GrSurfaceDesc uvDesc; | 94 uDrawContext = context->newDrawContext(SkBackingFit::kApprox, |
104 uvDesc.fConfig = singleChannelPixelConfig; | 95 sizes[1].fWidth, sizes[1]
.fHeight, |
105 uvDesc.fFlags = kRenderTarget_GrSurfaceFlag; | 96 singleChannelPixelConfig)
; |
106 uvDesc.fWidth = sizes[1].fWidth; | 97 vDrawContext = context->newDrawContext(SkBackingFit::kApprox, |
107 uvDesc.fHeight = sizes[1].fHeight; | 98 sizes[2].fWidth, sizes[2]
.fHeight, |
108 uTex.reset(context->textureProvider()->createApproxTexture(uvDes
c)); | 99 singleChannelPixelConfig)
; |
109 uvDesc.fWidth = sizes[2].fWidth; | 100 if (!uDrawContext || !vDrawContext) { |
110 uvDesc.fHeight = sizes[2].fHeight; | |
111 vTex.reset(context->textureProvider()->createApproxTexture(uvDes
c)); | |
112 if (!uTex || !vTex) { | |
113 return false; | 101 return false; |
114 } | 102 } |
115 } | 103 } |
116 } | 104 } |
117 | 105 |
118 // Do all the draws before any readback. | 106 // Do all the draws before any readback. |
119 if (yuvTex) { | 107 if (yuvDrawContext) { |
120 sk_sp<GrDrawContext> dc(context->drawContext(sk_ref_sp(yuvTex->asRen
derTarget()))); | 108 if (!convert_texture(texture, yuvDrawContext.get(), |
121 if (!dc) { | 109 sizes[0].fWidth, sizes[0].fHeight, |
| 110 colorSpace, GrYUVEffect::CreateRGBToYUV)) { |
122 return false; | 111 return false; |
123 } | 112 } |
124 if (!convert_texture(texture, dc.get(), sizes[0].fWidth, sizes[0].fH
eight, colorSpace, | 113 } else { |
125 GrYUVEffect::CreateRGBToYUV)) { | 114 SkASSERT(yDrawContext); |
| 115 if (!convert_texture(texture, yDrawContext.get(), |
| 116 sizes[0].fWidth, sizes[0].fHeight, |
| 117 colorSpace, GrYUVEffect::CreateRGBToY)) { |
126 return false; | 118 return false; |
127 } | 119 } |
128 | 120 if (uvDrawContext) { |
129 } else { | 121 if (!convert_texture(texture, uvDrawContext.get(), |
130 SkASSERT(yTex); | 122 sizes[1].fWidth, sizes[1].fHeight, |
131 sk_sp<GrDrawContext> dc(context->drawContext(sk_ref_sp(yTex->asRende
rTarget()))); | |
132 if (!dc) { | |
133 return false; | |
134 } | |
135 if (!convert_texture(texture, dc.get(), sizes[0].fWidth, sizes[0].fH
eight, colorSpace, | |
136 GrYUVEffect::CreateRGBToY)) { | |
137 return false; | |
138 } | |
139 if (uvTex) { | |
140 dc = context->drawContext(sk_ref_sp(uvTex->asRenderTarget())); | |
141 if (!dc) { | |
142 return false; | |
143 } | |
144 if (!convert_texture(texture, dc.get(), sizes[1].fWidth, sizes[1
].fHeight, | |
145 colorSpace, GrYUVEffect::CreateRGBToUV)) { | 123 colorSpace, GrYUVEffect::CreateRGBToUV)) { |
146 return false; | 124 return false; |
147 } | 125 } |
148 } else { | 126 } else { |
149 SkASSERT(uTex && vTex); | 127 SkASSERT(uDrawContext && vDrawContext); |
150 dc = context->drawContext(sk_ref_sp(uTex->asRenderTarget())); | 128 if (!convert_texture(texture, uDrawContext.get(), |
151 if (!dc) { | 129 sizes[1].fWidth, sizes[1].fHeight, |
152 return false; | |
153 } | |
154 if (!convert_texture(texture, dc.get(), sizes[1].fWidth, sizes[1
].fHeight, | |
155 colorSpace, GrYUVEffect::CreateRGBToU)) { | 130 colorSpace, GrYUVEffect::CreateRGBToU)) { |
156 return false; | 131 return false; |
157 } | 132 } |
158 dc = context->drawContext(sk_ref_sp(vTex->asRenderTarget())); | 133 if (!convert_texture(texture, vDrawContext.get(), |
159 if (!dc) { | 134 sizes[2].fWidth, sizes[2].fHeight, |
160 return false; | |
161 } | |
162 if (!convert_texture(texture, dc.get(), sizes[2].fWidth, sizes[2
].fHeight, | |
163 colorSpace, GrYUVEffect::CreateRGBToV)) { | 135 colorSpace, GrYUVEffect::CreateRGBToV)) { |
164 return false; | 136 return false; |
165 } | 137 } |
166 } | 138 } |
167 } | 139 } |
168 | 140 |
169 if (yuvTex) { | 141 if (yuvDrawContext) { |
170 SkASSERT(sizes[0] == sizes[1] && sizes[1] == sizes[2]); | 142 SkASSERT(sizes[0] == sizes[1] && sizes[1] == sizes[2]); |
| 143 sk_sp<GrTexture> yuvTex(yuvDrawContext->asTexture()); |
| 144 SkASSERT(yuvTex); |
171 SkISize yuvSize = sizes[0]; | 145 SkISize yuvSize = sizes[0]; |
172 // We have no kRGB_888 pixel format, so readback rgba and then copy
three channels. | 146 // We have no kRGB_888 pixel format, so readback rgba and then copy
three channels. |
173 SkAutoSTMalloc<128 * 128, uint32_t> tempYUV(yuvSize.fWidth * yuvSize
.fHeight); | 147 SkAutoSTMalloc<128 * 128, uint32_t> tempYUV(yuvSize.fWidth * yuvSize
.fHeight); |
174 if (!yuvTex->readPixels(0, 0, yuvSize.fWidth, yuvSize.fHeight, | 148 if (!yuvTex->readPixels(0, 0, yuvSize.fWidth, yuvSize.fHeight, |
175 kRGBA_8888_GrPixelConfig, tempYUV.get(), 0))
{ | 149 kRGBA_8888_GrPixelConfig, tempYUV.get(), 0))
{ |
176 return false; | 150 return false; |
177 } | 151 } |
178 size_t yRowBytes = rowBytes[0] ? rowBytes[0] : yuvSize.fWidth; | 152 size_t yRowBytes = rowBytes[0] ? rowBytes[0] : yuvSize.fWidth; |
179 size_t uRowBytes = rowBytes[1] ? rowBytes[1] : yuvSize.fWidth; | 153 size_t uRowBytes = rowBytes[1] ? rowBytes[1] : yuvSize.fWidth; |
180 size_t vRowBytes = rowBytes[2] ? rowBytes[2] : yuvSize.fWidth; | 154 size_t vRowBytes = rowBytes[2] ? rowBytes[2] : yuvSize.fWidth; |
(...skipping 10 matching lines...) Expand all Loading... |
191 uint8_t* yLoc = ((uint8_t*)planes[0]) + j * yRowBytes + i; | 165 uint8_t* yLoc = ((uint8_t*)planes[0]) + j * yRowBytes + i; |
192 uint8_t* uLoc = ((uint8_t*)planes[1]) + j * uRowBytes + i; | 166 uint8_t* uLoc = ((uint8_t*)planes[1]) + j * uRowBytes + i; |
193 uint8_t* vLoc = ((uint8_t*)planes[2]) + j * vRowBytes + i; | 167 uint8_t* vLoc = ((uint8_t*)planes[2]) + j * vRowBytes + i; |
194 *yLoc = y; | 168 *yLoc = y; |
195 *uLoc = u; | 169 *uLoc = u; |
196 *vLoc = v; | 170 *vLoc = v; |
197 } | 171 } |
198 } | 172 } |
199 return true; | 173 return true; |
200 } else { | 174 } else { |
| 175 SkASSERT(yDrawContext); |
| 176 sk_sp<GrTexture> yTex(yDrawContext->asTexture()); |
201 SkASSERT(yTex); | 177 SkASSERT(yTex); |
202 if (!yTex->readPixels(0, 0, sizes[0].fWidth, sizes[0].fHeight, | 178 if (!yTex->readPixels(0, 0, sizes[0].fWidth, sizes[0].fHeight, |
203 kAlpha_8_GrPixelConfig, planes[0], rowBytes[0]
)) { | 179 kAlpha_8_GrPixelConfig, planes[0], rowBytes[0]
)) { |
204 return false; | 180 return false; |
205 } | 181 } |
206 if (uvTex) { | 182 if (uvDrawContext) { |
207 SkASSERT(sizes[1].fWidth == sizes[2].fWidth); | 183 SkASSERT(sizes[1].fWidth == sizes[2].fWidth); |
| 184 sk_sp<GrTexture> uvTex(uvDrawContext->asTexture()); |
| 185 SkASSERT(uvTex); |
208 SkISize uvSize = sizes[1]; | 186 SkISize uvSize = sizes[1]; |
209 // We have no kRG_88 pixel format, so readback rgba and then cop
y two channels. | 187 // We have no kRG_88 pixel format, so readback rgba and then cop
y two channels. |
210 SkAutoSTMalloc<128 * 128, uint32_t> tempUV(uvSize.fWidth * uvSiz
e.fHeight); | 188 SkAutoSTMalloc<128 * 128, uint32_t> tempUV(uvSize.fWidth * uvSiz
e.fHeight); |
211 if (!uvTex->readPixels(0, 0, uvSize.fWidth, uvSize.fHeight, | 189 if (!uvTex->readPixels(0, 0, uvSize.fWidth, uvSize.fHeight, |
212 kRGBA_8888_GrPixelConfig, tempUV.get(), 0
)) { | 190 kRGBA_8888_GrPixelConfig, tempUV.get(), 0
)) { |
213 return false; | 191 return false; |
214 } | 192 } |
215 | 193 |
216 size_t uRowBytes = rowBytes[1] ? rowBytes[1] : uvSize.fWidth; | 194 size_t uRowBytes = rowBytes[1] ? rowBytes[1] : uvSize.fWidth; |
217 size_t vRowBytes = rowBytes[2] ? rowBytes[2] : uvSize.fWidth; | 195 size_t vRowBytes = rowBytes[2] ? rowBytes[2] : uvSize.fWidth; |
218 if (uRowBytes < (size_t)uvSize.fWidth || vRowBytes < (size_t)uvS
ize.fWidth) { | 196 if (uRowBytes < (size_t)uvSize.fWidth || vRowBytes < (size_t)uvS
ize.fWidth) { |
219 return false; | 197 return false; |
220 } | 198 } |
221 for (int j = 0; j < uvSize.fHeight; ++j) { | 199 for (int j = 0; j < uvSize.fHeight; ++j) { |
222 for (int i = 0; i < uvSize.fWidth; ++i) { | 200 for (int i = 0; i < uvSize.fWidth; ++i) { |
223 // These writes could surely be made more efficient. | 201 // These writes could surely be made more efficient. |
224 uint32_t u = GrColorUnpackR(tempUV.get()[j * uvSize.fWid
th + i]); | 202 uint32_t u = GrColorUnpackR(tempUV.get()[j * uvSize.fWid
th + i]); |
225 uint32_t v = GrColorUnpackG(tempUV.get()[j * uvSize.fWid
th + i]); | 203 uint32_t v = GrColorUnpackG(tempUV.get()[j * uvSize.fWid
th + i]); |
226 uint8_t* uLoc = ((uint8_t*)planes[1]) + j * uRowBytes +
i; | 204 uint8_t* uLoc = ((uint8_t*)planes[1]) + j * uRowBytes +
i; |
227 uint8_t* vLoc = ((uint8_t*)planes[2]) + j * vRowBytes +
i; | 205 uint8_t* vLoc = ((uint8_t*)planes[2]) + j * vRowBytes +
i; |
228 *uLoc = u; | 206 *uLoc = u; |
229 *vLoc = v; | 207 *vLoc = v; |
230 } | 208 } |
231 } | 209 } |
232 return true; | 210 return true; |
233 } else { | 211 } else { |
234 SkASSERT(uTex && vTex); | 212 SkASSERT(uDrawContext && vDrawContext); |
235 if (!uTex->readPixels(0, 0, sizes[1].fWidth, sizes[1].fHeight, | 213 sk_sp<GrTexture> tex(uDrawContext->asTexture()); |
236 kAlpha_8_GrPixelConfig, planes[1], rowByte
s[1])) { | 214 SkASSERT(tex); |
| 215 if (!tex->readPixels(0, 0, sizes[1].fWidth, sizes[1].fHeight, |
| 216 kAlpha_8_GrPixelConfig, planes[1], rowBytes
[1])) { |
237 return false; | 217 return false; |
238 } | 218 } |
239 if (!vTex->readPixels(0, 0, sizes[2].fWidth, sizes[2].fHeight, | 219 tex = vDrawContext->asTexture(); |
240 kAlpha_8_GrPixelConfig, planes[2], rowByte
s[2])) { | 220 SkASSERT(tex); |
| 221 if (!tex->readPixels(0, 0, sizes[2].fWidth, sizes[2].fHeight, |
| 222 kAlpha_8_GrPixelConfig, planes[2], rowBytes
[2])) { |
241 return false; | 223 return false; |
242 } | 224 } |
243 return true; | 225 return true; |
244 } | 226 } |
245 } | 227 } |
246 } | 228 } |
247 return false; | 229 return false; |
248 } | 230 } |
OLD | NEW |