1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "GLReadTexImageHelper.h"
8
9 #include <utility>
10
11 #include "GLContext.h"
12 #include "OGLShaderProgram.h"
13 #include "ScopedGLHelpers.h"
14 #include "gfx2DGlue.h"
15 #include "gfxColor.h"
16 #include "gfxTypes.h"
17 #include "mozilla/gfx/2D.h"
18
19 namespace mozilla {
20 namespace gl {
21
22 using namespace mozilla::gfx;
23
GLReadTexImageHelper(GLContext * gl)24 GLReadTexImageHelper::GLReadTexImageHelper(GLContext* gl) : mGL(gl) {
25 mPrograms[0] = 0;
26 mPrograms[1] = 0;
27 mPrograms[2] = 0;
28 mPrograms[3] = 0;
29 }
30
~GLReadTexImageHelper()31 GLReadTexImageHelper::~GLReadTexImageHelper() {
32 if (!mGL->MakeCurrent()) return;
33
34 mGL->fDeleteProgram(mPrograms[0]);
35 mGL->fDeleteProgram(mPrograms[1]);
36 mGL->fDeleteProgram(mPrograms[2]);
37 mGL->fDeleteProgram(mPrograms[3]);
38 }
39
40 static const GLchar readTextureImageVS[] =
41 "attribute vec2 aVertex;\n"
42 "attribute vec2 aTexCoord;\n"
43 "varying vec2 vTexCoord;\n"
44 "void main() { gl_Position = vec4(aVertex, 0, 1); vTexCoord = aTexCoord; }";
45
46 static const GLchar readTextureImageFS_TEXTURE_2D[] =
47 "#ifdef GL_ES\n"
48 "precision mediump float;\n"
49 "#endif\n"
50 "varying vec2 vTexCoord;\n"
51 "uniform sampler2D uTexture;\n"
52 "void main() { gl_FragColor = texture2D(uTexture, vTexCoord); }";
53
54 static const GLchar readTextureImageFS_TEXTURE_2D_BGRA[] =
55 "#ifdef GL_ES\n"
56 "precision mediump float;\n"
57 "#endif\n"
58 "varying vec2 vTexCoord;\n"
59 "uniform sampler2D uTexture;\n"
60 "void main() { gl_FragColor = texture2D(uTexture, vTexCoord).bgra; }";
61
62 static const GLchar readTextureImageFS_TEXTURE_EXTERNAL[] =
63 "#extension GL_OES_EGL_image_external : require\n"
64 "#ifdef GL_ES\n"
65 "precision mediump float;\n"
66 "#endif\n"
67 "varying vec2 vTexCoord;\n"
68 "uniform samplerExternalOES uTexture;\n"
69 "void main() { gl_FragColor = texture2D(uTexture, vTexCoord); }";
70
71 static const GLchar readTextureImageFS_TEXTURE_RECTANGLE[] =
72 "#extension GL_ARB_texture_rectangle\n"
73 "#ifdef GL_ES\n"
74 "precision mediump float;\n"
75 "#endif\n"
76 "varying vec2 vTexCoord;\n"
77 "uniform sampler2DRect uTexture;\n"
78 "void main() { gl_FragColor = texture2DRect(uTexture, vTexCoord).bgra; }";
79
TextureImageProgramFor(GLenum aTextureTarget,int aConfig)80 GLuint GLReadTexImageHelper::TextureImageProgramFor(GLenum aTextureTarget,
81 int aConfig) {
82 int variant = 0;
83 const GLchar* readTextureImageFS = nullptr;
84 if (aTextureTarget == LOCAL_GL_TEXTURE_2D) {
85 if (aConfig & mozilla::layers::ENABLE_TEXTURE_RB_SWAP) {
86 // Need to swizzle R/B.
87 readTextureImageFS = readTextureImageFS_TEXTURE_2D_BGRA;
88 variant = 1;
89 } else {
90 readTextureImageFS = readTextureImageFS_TEXTURE_2D;
91 variant = 0;
92 }
93 } else if (aTextureTarget == LOCAL_GL_TEXTURE_EXTERNAL) {
94 readTextureImageFS = readTextureImageFS_TEXTURE_EXTERNAL;
95 variant = 2;
96 } else if (aTextureTarget == LOCAL_GL_TEXTURE_RECTANGLE) {
97 readTextureImageFS = readTextureImageFS_TEXTURE_RECTANGLE;
98 variant = 3;
99 }
100
101 /* This might be overkill, but assure that we don't access out-of-bounds */
102 MOZ_ASSERT((size_t)variant < ArrayLength(mPrograms));
103 if (!mPrograms[variant]) {
104 GLuint vs = mGL->fCreateShader(LOCAL_GL_VERTEX_SHADER);
105 const GLchar* vsSourcePtr = &readTextureImageVS[0];
106 mGL->fShaderSource(vs, 1, &vsSourcePtr, nullptr);
107 mGL->fCompileShader(vs);
108
109 GLuint fs = mGL->fCreateShader(LOCAL_GL_FRAGMENT_SHADER);
110 mGL->fShaderSource(fs, 1, &readTextureImageFS, nullptr);
111 mGL->fCompileShader(fs);
112
113 GLuint program = mGL->fCreateProgram();
114 mGL->fAttachShader(program, vs);
115 mGL->fAttachShader(program, fs);
116 mGL->fBindAttribLocation(program, 0, "aVertex");
117 mGL->fBindAttribLocation(program, 1, "aTexCoord");
118 mGL->fLinkProgram(program);
119
120 GLint success;
121 mGL->fGetProgramiv(program, LOCAL_GL_LINK_STATUS, &success);
122
123 if (!success) {
124 mGL->fDeleteProgram(program);
125 program = 0;
126 }
127
128 mGL->fDeleteShader(vs);
129 mGL->fDeleteShader(fs);
130
131 mPrograms[variant] = program;
132 }
133
134 return mPrograms[variant];
135 }
136
DidGLErrorOccur(const char * str)137 bool GLReadTexImageHelper::DidGLErrorOccur(const char* str) {
138 GLenum error = mGL->fGetError();
139 if (error != LOCAL_GL_NO_ERROR) {
140 printf_stderr("GL ERROR: %s %s\n",
141 GLContext::GLErrorToString(error).c_str(), str);
142 return true;
143 }
144
145 return false;
146 }
147
GetActualReadFormats(GLContext * gl,GLenum destFormat,GLenum destType,GLenum * out_readFormat,GLenum * out_readType)148 bool GetActualReadFormats(GLContext* gl, GLenum destFormat, GLenum destType,
149 GLenum* out_readFormat, GLenum* out_readType) {
150 MOZ_ASSERT(out_readFormat);
151 MOZ_ASSERT(out_readType);
152
153 if (destFormat == LOCAL_GL_RGBA && destType == LOCAL_GL_UNSIGNED_BYTE) {
154 *out_readFormat = destFormat;
155 *out_readType = destType;
156 return true;
157 }
158
159 bool fallback = true;
160 if (gl->IsGLES()) {
161 GLenum auxFormat = 0;
162 GLenum auxType = 0;
163
164 gl->fGetIntegerv(LOCAL_GL_IMPLEMENTATION_COLOR_READ_FORMAT,
165 (GLint*)&auxFormat);
166 gl->fGetIntegerv(LOCAL_GL_IMPLEMENTATION_COLOR_READ_TYPE, (GLint*)&auxType);
167
168 if (destFormat == auxFormat && destType == auxType) {
169 fallback = false;
170 }
171 } else {
172 switch (destFormat) {
173 case LOCAL_GL_RGB: {
174 if (destType == LOCAL_GL_UNSIGNED_SHORT_5_6_5_REV) fallback = false;
175 break;
176 }
177 case LOCAL_GL_BGRA: {
178 if (destType == LOCAL_GL_UNSIGNED_BYTE ||
179 destType == LOCAL_GL_UNSIGNED_INT_8_8_8_8_REV) {
180 fallback = false;
181 }
182 break;
183 }
184 }
185 }
186
187 if (fallback) {
188 *out_readFormat = LOCAL_GL_RGBA;
189 *out_readType = LOCAL_GL_UNSIGNED_BYTE;
190 return false;
191 } else {
192 *out_readFormat = destFormat;
193 *out_readType = destType;
194 return true;
195 }
196 }
197
SwapRAndBComponents(DataSourceSurface * surf)198 void SwapRAndBComponents(DataSourceSurface* surf) {
199 DataSourceSurface::MappedSurface map;
200 if (!surf->Map(DataSourceSurface::MapType::READ_WRITE, &map)) {
201 MOZ_ASSERT(false, "SwapRAndBComponents: Failed to map surface.");
202 return;
203 }
204 MOZ_ASSERT(map.mStride >= 0);
205
206 const size_t rowBytes = surf->GetSize().width * 4;
207 const size_t rowHole = map.mStride - rowBytes;
208
209 uint8_t* row = map.mData;
210 if (!row) {
211 MOZ_ASSERT(false,
212 "SwapRAndBComponents: Failed to get data from"
213 " DataSourceSurface.");
214 surf->Unmap();
215 return;
216 }
217
218 const size_t rows = surf->GetSize().height;
219 for (size_t i = 0; i < rows; i++) {
220 const uint8_t* rowEnd = row + rowBytes;
221
222 while (row != rowEnd) {
223 std::swap(row[0], row[2]);
224 row += 4;
225 }
226
227 row += rowHole;
228 }
229
230 surf->Unmap();
231 }
232
CalcRowStride(int width,int pixelSize,int alignment)233 static int CalcRowStride(int width, int pixelSize, int alignment) {
234 MOZ_ASSERT(alignment);
235
236 int rowStride = width * pixelSize;
237 if (rowStride % alignment) { // Extra at the end of the line?
238 int alignmentCount = rowStride / alignment;
239 rowStride = (alignmentCount + 1) * alignment;
240 }
241 return rowStride;
242 }
243
GuessAlignment(int width,int pixelSize,int rowStride)244 static int GuessAlignment(int width, int pixelSize, int rowStride) {
245 int alignment = 8; // Max GLES allows.
246 while (CalcRowStride(width, pixelSize, alignment) != rowStride) {
247 alignment /= 2;
248 if (!alignment) {
249 NS_WARNING("Bad alignment for GLES. Will use temp surf for readback.");
250 return 0;
251 }
252 }
253 return alignment;
254 }
255
ReadPixelsIntoDataSurface(GLContext * gl,DataSourceSurface * dest)256 void ReadPixelsIntoDataSurface(GLContext* gl, DataSourceSurface* dest) {
257 gl->MakeCurrent();
258 MOZ_ASSERT(dest->GetSize().width != 0);
259 MOZ_ASSERT(dest->GetSize().height != 0);
260
261 bool hasAlpha = dest->GetFormat() == SurfaceFormat::B8G8R8A8 ||
262 dest->GetFormat() == SurfaceFormat::R8G8B8A8;
263
264 int destPixelSize;
265 GLenum destFormat;
266 GLenum destType;
267
268 switch (dest->GetFormat()) {
269 case SurfaceFormat::B8G8R8A8:
270 case SurfaceFormat::B8G8R8X8:
271 // Needs host (little) endian ARGB.
272 destFormat = LOCAL_GL_BGRA;
273 destType = LOCAL_GL_UNSIGNED_INT_8_8_8_8_REV;
274 break;
275 case SurfaceFormat::R8G8B8A8:
276 case SurfaceFormat::R8G8B8X8:
277 // Needs host (little) endian ABGR.
278 destFormat = LOCAL_GL_RGBA;
279 destType = LOCAL_GL_UNSIGNED_BYTE;
280 break;
281 case SurfaceFormat::R5G6B5_UINT16:
282 destFormat = LOCAL_GL_RGB;
283 destType = LOCAL_GL_UNSIGNED_SHORT_5_6_5_REV;
284 break;
285 default:
286 MOZ_CRASH("GFX: Bad format, read pixels.");
287 }
288 destPixelSize = BytesPerPixel(dest->GetFormat());
289
290 Maybe<DataSourceSurface::ScopedMap> map;
291 map.emplace(dest, DataSourceSurface::READ_WRITE);
292
293 MOZ_ASSERT(dest->GetSize().width * destPixelSize <= map->GetStride());
294
295 GLenum readFormat = destFormat;
296 GLenum readType = destType;
297 bool needsTempSurf =
298 !GetActualReadFormats(gl, destFormat, destType, &readFormat, &readType);
299
300 RefPtr<DataSourceSurface> tempSurf;
301 DataSourceSurface* readSurf = dest;
302 int readAlignment =
303 GuessAlignment(dest->GetSize().width, destPixelSize, map->GetStride());
304 if (!readAlignment) {
305 needsTempSurf = true;
306 }
307 if (needsTempSurf) {
308 if (GLContext::ShouldSpew()) {
309 NS_WARNING(
310 "Needing intermediary surface for ReadPixels. This will be slow!");
311 }
312 SurfaceFormat readFormatGFX;
313
314 switch (readFormat) {
315 case LOCAL_GL_RGBA: {
316 readFormatGFX =
317 hasAlpha ? SurfaceFormat::R8G8B8A8 : SurfaceFormat::R8G8B8X8;
318 break;
319 }
320 case LOCAL_GL_BGRA: {
321 readFormatGFX =
322 hasAlpha ? SurfaceFormat::B8G8R8A8 : SurfaceFormat::B8G8R8X8;
323 break;
324 }
325 case LOCAL_GL_RGB: {
326 MOZ_ASSERT(destPixelSize == 2);
327 MOZ_ASSERT(readType == LOCAL_GL_UNSIGNED_SHORT_5_6_5_REV);
328 readFormatGFX = SurfaceFormat::R5G6B5_UINT16;
329 break;
330 }
331 default: {
332 MOZ_CRASH("GFX: Bad read format, read format.");
333 }
334 }
335
336 switch (readType) {
337 case LOCAL_GL_UNSIGNED_BYTE: {
338 MOZ_ASSERT(readFormat == LOCAL_GL_RGBA);
339 readAlignment = 1;
340 break;
341 }
342 case LOCAL_GL_UNSIGNED_INT_8_8_8_8_REV: {
343 MOZ_ASSERT(readFormat == LOCAL_GL_BGRA);
344 readAlignment = 4;
345 break;
346 }
347 case LOCAL_GL_UNSIGNED_SHORT_5_6_5_REV: {
348 MOZ_ASSERT(readFormat == LOCAL_GL_RGB);
349 readAlignment = 2;
350 break;
351 }
352 default: {
353 MOZ_CRASH("GFX: Bad read type, read type.");
354 }
355 }
356
357 int32_t stride = dest->GetSize().width * BytesPerPixel(readFormatGFX);
358 tempSurf = Factory::CreateDataSourceSurfaceWithStride(
359 dest->GetSize(), readFormatGFX, stride);
360 if (NS_WARN_IF(!tempSurf)) {
361 return;
362 }
363
364 readSurf = tempSurf;
365 map = Nothing();
366 map.emplace(readSurf, DataSourceSurface::READ_WRITE);
367 }
368
369 MOZ_ASSERT(readAlignment);
370 MOZ_ASSERT(reinterpret_cast<uintptr_t>(map->GetData()) % readAlignment == 0);
371
372 GLsizei width = dest->GetSize().width;
373 GLsizei height = dest->GetSize().height;
374
375 {
376 ScopedPackState safePackState(gl);
377 gl->fPixelStorei(LOCAL_GL_PACK_ALIGNMENT, readAlignment);
378
379 gl->fReadPixels(0, 0, width, height, readFormat, readType, map->GetData());
380 }
381
382 map = Nothing();
383
384 if (readSurf != dest) {
385 MOZ_ASSERT(readFormat == LOCAL_GL_RGBA);
386 MOZ_ASSERT(readType == LOCAL_GL_UNSIGNED_BYTE);
387 gfx::Factory::CopyDataSourceSurface(readSurf, dest);
388 }
389 }
390
YInvertImageSurface(gfx::DataSourceSurface * aSurf,uint32_t aStride)391 already_AddRefed<gfx::DataSourceSurface> YInvertImageSurface(
392 gfx::DataSourceSurface* aSurf, uint32_t aStride) {
393 RefPtr<DataSourceSurface> temp = Factory::CreateDataSourceSurfaceWithStride(
394 aSurf->GetSize(), aSurf->GetFormat(), aStride);
395 if (NS_WARN_IF(!temp)) {
396 return nullptr;
397 }
398
399 DataSourceSurface::MappedSurface map;
400 if (!temp->Map(DataSourceSurface::MapType::WRITE, &map)) {
401 return nullptr;
402 }
403
404 RefPtr<DrawTarget> dt = Factory::CreateDrawTargetForData(
405 BackendType::CAIRO, map.mData, temp->GetSize(), map.mStride,
406 temp->GetFormat());
407 if (!dt) {
408 temp->Unmap();
409 return nullptr;
410 }
411
412 dt->SetTransform(Matrix::Scaling(1.0, -1.0) *
413 Matrix::Translation(0.0, aSurf->GetSize().height));
414 Rect rect(0, 0, aSurf->GetSize().width, aSurf->GetSize().height);
415 dt->DrawSurface(
416 aSurf, rect, rect, DrawSurfaceOptions(),
417 DrawOptions(1.0, CompositionOp::OP_SOURCE, AntialiasMode::NONE));
418 temp->Unmap();
419 return temp.forget();
420 }
421
ReadBackSurface(GLContext * gl,GLuint aTexture,bool aYInvert,SurfaceFormat aFormat)422 already_AddRefed<DataSourceSurface> ReadBackSurface(GLContext* gl,
423 GLuint aTexture,
424 bool aYInvert,
425 SurfaceFormat aFormat) {
426 gl->MakeCurrent();
427 gl->fActiveTexture(LOCAL_GL_TEXTURE0);
428 gl->fBindTexture(LOCAL_GL_TEXTURE_2D, aTexture);
429
430 IntSize size;
431 gl->fGetTexLevelParameteriv(LOCAL_GL_TEXTURE_2D, 0, LOCAL_GL_TEXTURE_WIDTH,
432 &size.width);
433 gl->fGetTexLevelParameteriv(LOCAL_GL_TEXTURE_2D, 0, LOCAL_GL_TEXTURE_HEIGHT,
434 &size.height);
435
436 RefPtr<DataSourceSurface> surf = Factory::CreateDataSourceSurfaceWithStride(
437 size, SurfaceFormat::B8G8R8A8,
438 GetAlignedStride<4>(size.width, BytesPerPixel(SurfaceFormat::B8G8R8A8)));
439
440 if (NS_WARN_IF(!surf)) {
441 return nullptr;
442 }
443
444 uint32_t currentPackAlignment = 0;
445 gl->fGetIntegerv(LOCAL_GL_PACK_ALIGNMENT, (GLint*)¤tPackAlignment);
446 if (currentPackAlignment != 4) {
447 gl->fPixelStorei(LOCAL_GL_PACK_ALIGNMENT, 4);
448 }
449
450 DataSourceSurface::ScopedMap map(surf, DataSourceSurface::READ);
451 gl->fGetTexImage(LOCAL_GL_TEXTURE_2D, 0, LOCAL_GL_RGBA,
452 LOCAL_GL_UNSIGNED_BYTE, map.GetData());
453
454 if (currentPackAlignment != 4) {
455 gl->fPixelStorei(LOCAL_GL_PACK_ALIGNMENT, currentPackAlignment);
456 }
457
458 if (aFormat == SurfaceFormat::R8G8B8A8 ||
459 aFormat == SurfaceFormat::R8G8B8X8) {
460 SwapRAndBComponents(surf);
461 }
462
463 if (aYInvert) {
464 surf = YInvertImageSurface(surf, map.GetStride());
465 }
466
467 return surf.forget();
468 }
469
470 #define CLEANUP_IF_GLERROR_OCCURRED(x) \
471 if (DidGLErrorOccur(x)) { \
472 return false; \
473 }
474
ReadTexImage(GLuint aTextureId,GLenum aTextureTarget,const gfx::IntSize & aSize,int aConfig,bool aYInvert)475 already_AddRefed<DataSourceSurface> GLReadTexImageHelper::ReadTexImage(
476 GLuint aTextureId, GLenum aTextureTarget, const gfx::IntSize& aSize,
477 /* ShaderConfigOGL.mFeature */ int aConfig, bool aYInvert) {
478 /* Allocate resulting image surface */
479 int32_t stride = aSize.width * BytesPerPixel(SurfaceFormat::R8G8B8A8);
480 RefPtr<DataSourceSurface> isurf = Factory::CreateDataSourceSurfaceWithStride(
481 aSize, SurfaceFormat::R8G8B8A8, stride);
482 if (NS_WARN_IF(!isurf)) {
483 return nullptr;
484 }
485
486 if (!ReadTexImage(isurf, aTextureId, aTextureTarget, aSize, aConfig,
487 aYInvert)) {
488 return nullptr;
489 }
490
491 return isurf.forget();
492 }
493
ReadTexImage(DataSourceSurface * aDest,GLuint aTextureId,GLenum aTextureTarget,const gfx::IntSize & aSize,int aConfig,bool aYInvert)494 bool GLReadTexImageHelper::ReadTexImage(
495 DataSourceSurface* aDest, GLuint aTextureId, GLenum aTextureTarget,
496 const gfx::IntSize& aSize,
497 /* ShaderConfigOGL.mFeature */ int aConfig, bool aYInvert) {
498 MOZ_ASSERT(aTextureTarget == LOCAL_GL_TEXTURE_2D ||
499 aTextureTarget == LOCAL_GL_TEXTURE_EXTERNAL ||
500 aTextureTarget == LOCAL_GL_TEXTURE_RECTANGLE_ARB);
501
502 mGL->MakeCurrent();
503
504 GLint oldrb, oldfb, oldprog, oldTexUnit, oldTex;
505 GLuint rb, fb;
506
507 do {
508 mGL->fGetIntegerv(LOCAL_GL_RENDERBUFFER_BINDING, &oldrb);
509 mGL->fGetIntegerv(LOCAL_GL_FRAMEBUFFER_BINDING, &oldfb);
510 mGL->fGetIntegerv(LOCAL_GL_CURRENT_PROGRAM, &oldprog);
511 mGL->fGetIntegerv(LOCAL_GL_ACTIVE_TEXTURE, &oldTexUnit);
512 mGL->fActiveTexture(LOCAL_GL_TEXTURE0);
513 switch (aTextureTarget) {
514 case LOCAL_GL_TEXTURE_2D:
515 mGL->fGetIntegerv(LOCAL_GL_TEXTURE_BINDING_2D, &oldTex);
516 break;
517 case LOCAL_GL_TEXTURE_EXTERNAL:
518 mGL->fGetIntegerv(LOCAL_GL_TEXTURE_BINDING_EXTERNAL, &oldTex);
519 break;
520 case LOCAL_GL_TEXTURE_RECTANGLE:
521 mGL->fGetIntegerv(LOCAL_GL_TEXTURE_BINDING_RECTANGLE, &oldTex);
522 break;
523 default: /* Already checked above */
524 break;
525 }
526
527 ScopedGLState scopedScissorTestState(mGL, LOCAL_GL_SCISSOR_TEST, false);
528 ScopedGLState scopedBlendState(mGL, LOCAL_GL_BLEND, false);
529 ScopedViewportRect scopedViewportRect(mGL, 0, 0, aSize.width, aSize.height);
530
531 /* Setup renderbuffer */
532 mGL->fGenRenderbuffers(1, &rb);
533 mGL->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, rb);
534
535 GLenum rbInternalFormat =
536 mGL->IsGLES() ? (mGL->IsExtensionSupported(GLContext::OES_rgb8_rgba8)
537 ? LOCAL_GL_RGBA8
538 : LOCAL_GL_RGBA4)
539 : LOCAL_GL_RGBA;
540 mGL->fRenderbufferStorage(LOCAL_GL_RENDERBUFFER, rbInternalFormat,
541 aSize.width, aSize.height);
542 CLEANUP_IF_GLERROR_OCCURRED("when binding and creating renderbuffer");
543
544 /* Setup framebuffer */
545 mGL->fGenFramebuffers(1, &fb);
546 mGL->fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, fb);
547 mGL->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER,
548 LOCAL_GL_COLOR_ATTACHMENT0,
549 LOCAL_GL_RENDERBUFFER, rb);
550 CLEANUP_IF_GLERROR_OCCURRED("when binding and creating framebuffer");
551
552 MOZ_ASSERT(mGL->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER) ==
553 LOCAL_GL_FRAMEBUFFER_COMPLETE);
554
555 /* Setup vertex and fragment shader */
556 GLuint program = TextureImageProgramFor(aTextureTarget, aConfig);
557 MOZ_ASSERT(program);
558
559 mGL->fUseProgram(program);
560 CLEANUP_IF_GLERROR_OCCURRED("when using program");
561 mGL->fUniform1i(mGL->fGetUniformLocation(program, "uTexture"), 0);
562 CLEANUP_IF_GLERROR_OCCURRED("when setting uniform location");
563
564 /* Setup quad geometry */
565 mGL->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, 0);
566
567 float w = (aTextureTarget == LOCAL_GL_TEXTURE_RECTANGLE)
568 ? (float)aSize.width
569 : 1.0f;
570 float h = (aTextureTarget == LOCAL_GL_TEXTURE_RECTANGLE)
571 ? (float)aSize.height
572 : 1.0f;
573
574 const float vertexArray[4 * 2] = {-1.0f, -1.0f, 1.0f, -1.0f,
575 -1.0f, 1.0f, 1.0f, 1.0f};
576 ScopedVertexAttribPointer autoAttrib0(mGL, 0, 2, LOCAL_GL_FLOAT,
577 LOCAL_GL_FALSE, 0, 0, vertexArray);
578
579 const float u0 = 0.0f;
580 const float u1 = w;
581 const float v0 = aYInvert ? h : 0.0f;
582 const float v1 = aYInvert ? 0.0f : h;
583 const float texCoordArray[8] = {u0, v0, u1, v0, u0, v1, u1, v1};
584 ScopedVertexAttribPointer autoAttrib1(mGL, 1, 2, LOCAL_GL_FLOAT,
585 LOCAL_GL_FALSE, 0, 0, texCoordArray);
586
587 /* Bind the texture */
588 if (aTextureId) {
589 mGL->fBindTexture(aTextureTarget, aTextureId);
590 CLEANUP_IF_GLERROR_OCCURRED("when binding texture");
591 }
592
593 mGL->fDrawArrays(LOCAL_GL_TRIANGLE_STRIP, 0, 4);
594 CLEANUP_IF_GLERROR_OCCURRED("when drawing texture");
595
596 /* Read-back draw results */
597 ReadPixelsIntoDataSurface(mGL, aDest);
598 CLEANUP_IF_GLERROR_OCCURRED("when reading pixels into surface");
599 } while (false);
600
601 /* Restore GL state */
602 mGL->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, oldrb);
603 mGL->fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, oldfb);
604 mGL->fUseProgram(oldprog);
605
606 // note that deleting 0 has no effect in any of these calls
607 mGL->fDeleteRenderbuffers(1, &rb);
608 mGL->fDeleteFramebuffers(1, &fb);
609
610 if (aTextureId) mGL->fBindTexture(aTextureTarget, oldTex);
611
612 if (oldTexUnit != LOCAL_GL_TEXTURE0) mGL->fActiveTexture(oldTexUnit);
613
614 return true;
615 }
616
617 #undef CLEANUP_IF_GLERROR_OCCURRED
618
619 } // namespace gl
620 } // namespace mozilla
621