2014-01-02 19:17:22 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim: set ts=8 sts=4 et sw=4 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "GLReadTexImageHelper.h"
|
2014-07-30 21:03:07 +04:00
|
|
|
|
|
|
|
#include "gfx2DGlue.h"
|
2015-09-25 05:24:16 +03:00
|
|
|
#include "gfxColor.h"
|
2014-07-30 21:03:07 +04:00
|
|
|
#include "gfxTypes.h"
|
2014-01-02 19:17:22 +04:00
|
|
|
#include "GLContext.h"
|
|
|
|
#include "OGLShaderProgram.h"
|
2014-01-02 19:17:29 +04:00
|
|
|
#include "ScopedGLHelpers.h"
|
2014-07-30 21:03:07 +04:00
|
|
|
|
2014-01-02 19:17:29 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2014-07-30 21:35:30 +04:00
|
|
|
#include "mozilla/Move.h"
|
2014-01-02 19:17:22 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
2014-05-22 14:11:45 +04:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2014-01-02 19:17:22 +04:00
|
|
|
GLReadTexImageHelper::GLReadTexImageHelper(GLContext* gl)
|
|
|
|
: mGL(gl)
|
|
|
|
{
|
|
|
|
mPrograms[0] = 0;
|
|
|
|
mPrograms[1] = 0;
|
|
|
|
mPrograms[2] = 0;
|
|
|
|
mPrograms[3] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLReadTexImageHelper::~GLReadTexImageHelper()
|
|
|
|
{
|
2016-04-14 23:50:04 +03:00
|
|
|
if (!mGL->MakeCurrent())
|
|
|
|
return;
|
|
|
|
|
2014-01-02 19:17:22 +04:00
|
|
|
mGL->fDeleteProgram(mPrograms[0]);
|
|
|
|
mGL->fDeleteProgram(mPrograms[1]);
|
|
|
|
mGL->fDeleteProgram(mPrograms[2]);
|
|
|
|
mGL->fDeleteProgram(mPrograms[3]);
|
|
|
|
}
|
|
|
|
|
2014-01-02 19:17:24 +04:00
|
|
|
static const GLchar
|
|
|
|
readTextureImageVS[] =
|
|
|
|
"attribute vec2 aVertex;\n"
|
2014-01-02 19:17:22 +04:00
|
|
|
"attribute vec2 aTexCoord;\n"
|
|
|
|
"varying vec2 vTexCoord;\n"
|
2014-01-02 19:17:24 +04:00
|
|
|
"void main() { gl_Position = vec4(aVertex, 0, 1); vTexCoord = aTexCoord; }";
|
2014-01-02 19:17:22 +04:00
|
|
|
|
2014-01-02 19:17:24 +04:00
|
|
|
static const GLchar
|
|
|
|
readTextureImageFS_TEXTURE_2D[] =
|
2014-01-02 19:17:22 +04:00
|
|
|
"#ifdef GL_ES\n"
|
|
|
|
"precision mediump float;\n"
|
|
|
|
"#endif\n"
|
|
|
|
"varying vec2 vTexCoord;\n"
|
|
|
|
"uniform sampler2D uTexture;\n"
|
2014-01-02 19:17:24 +04:00
|
|
|
"void main() { gl_FragColor = texture2D(uTexture, vTexCoord); }";
|
|
|
|
|
|
|
|
|
|
|
|
static const GLchar
|
|
|
|
readTextureImageFS_TEXTURE_2D_BGRA[] =
|
2014-01-02 19:17:22 +04:00
|
|
|
"#ifdef GL_ES\n"
|
|
|
|
"precision mediump float;\n"
|
|
|
|
"#endif\n"
|
|
|
|
"varying vec2 vTexCoord;\n"
|
|
|
|
"uniform sampler2D uTexture;\n"
|
2014-01-02 19:17:24 +04:00
|
|
|
"void main() { gl_FragColor = texture2D(uTexture, vTexCoord).bgra; }";
|
|
|
|
|
|
|
|
static const GLchar
|
|
|
|
readTextureImageFS_TEXTURE_EXTERNAL[] =
|
2014-01-02 19:17:22 +04:00
|
|
|
"#extension GL_OES_EGL_image_external : require\n"
|
|
|
|
"#ifdef GL_ES\n"
|
|
|
|
"precision mediump float;\n"
|
|
|
|
"#endif\n"
|
|
|
|
"varying vec2 vTexCoord;\n"
|
|
|
|
"uniform samplerExternalOES uTexture;\n"
|
2014-01-02 19:17:24 +04:00
|
|
|
"void main() { gl_FragColor = texture2D(uTexture, vTexCoord); }";
|
|
|
|
|
|
|
|
static const GLchar
|
|
|
|
readTextureImageFS_TEXTURE_RECTANGLE[] =
|
2014-01-02 19:17:22 +04:00
|
|
|
"#extension GL_ARB_texture_rectangle\n"
|
|
|
|
"#ifdef GL_ES\n"
|
|
|
|
"precision mediump float;\n"
|
|
|
|
"#endif\n"
|
|
|
|
"varying vec2 vTexCoord;\n"
|
|
|
|
"uniform sampler2DRect uTexture;\n"
|
2014-01-02 19:17:24 +04:00
|
|
|
"void main() { gl_FragColor = texture2DRect(uTexture, vTexCoord).bgra; }";
|
2014-01-02 19:17:22 +04:00
|
|
|
|
|
|
|
GLuint
|
2014-07-30 21:03:07 +04:00
|
|
|
GLReadTexImageHelper::TextureImageProgramFor(GLenum aTextureTarget,
|
|
|
|
int aConfig)
|
|
|
|
{
|
2014-01-02 19:17:22 +04:00
|
|
|
int variant = 0;
|
2014-01-02 19:17:24 +04:00
|
|
|
const GLchar* readTextureImageFS = nullptr;
|
2014-07-30 21:03:07 +04:00
|
|
|
if (aTextureTarget == LOCAL_GL_TEXTURE_2D) {
|
|
|
|
if (aConfig & mozilla::layers::ENABLE_TEXTURE_RB_SWAP) {
|
|
|
|
// Need to swizzle R/B.
|
2014-01-02 19:17:24 +04:00
|
|
|
readTextureImageFS = readTextureImageFS_TEXTURE_2D_BGRA;
|
|
|
|
variant = 1;
|
2014-07-30 21:03:07 +04:00
|
|
|
} else {
|
2014-01-02 19:17:24 +04:00
|
|
|
readTextureImageFS = readTextureImageFS_TEXTURE_2D;
|
|
|
|
variant = 0;
|
|
|
|
}
|
2014-01-02 19:17:22 +04:00
|
|
|
} else if (aTextureTarget == LOCAL_GL_TEXTURE_EXTERNAL) {
|
2014-01-02 19:17:24 +04:00
|
|
|
readTextureImageFS = readTextureImageFS_TEXTURE_EXTERNAL;
|
2014-01-02 19:17:22 +04:00
|
|
|
variant = 2;
|
|
|
|
} else if (aTextureTarget == LOCAL_GL_TEXTURE_RECTANGLE) {
|
2014-01-02 19:17:24 +04:00
|
|
|
readTextureImageFS = readTextureImageFS_TEXTURE_RECTANGLE;
|
2014-01-02 19:17:22 +04:00
|
|
|
variant = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This might be overkill, but assure that we don't access out-of-bounds */
|
|
|
|
MOZ_ASSERT((size_t) variant < ArrayLength(mPrograms));
|
|
|
|
if (!mPrograms[variant]) {
|
|
|
|
GLuint vs = mGL->fCreateShader(LOCAL_GL_VERTEX_SHADER);
|
2014-01-02 19:17:24 +04:00
|
|
|
const GLchar* vsSourcePtr = &readTextureImageVS[0];
|
|
|
|
mGL->fShaderSource(vs, 1, &vsSourcePtr, nullptr);
|
2014-01-02 19:17:22 +04:00
|
|
|
mGL->fCompileShader(vs);
|
|
|
|
|
|
|
|
GLuint fs = mGL->fCreateShader(LOCAL_GL_FRAGMENT_SHADER);
|
2014-01-02 19:17:24 +04:00
|
|
|
mGL->fShaderSource(fs, 1, &readTextureImageFS, nullptr);
|
2014-01-02 19:17:22 +04:00
|
|
|
mGL->fCompileShader(fs);
|
|
|
|
|
|
|
|
GLuint program = mGL->fCreateProgram();
|
|
|
|
mGL->fAttachShader(program, vs);
|
|
|
|
mGL->fAttachShader(program, fs);
|
|
|
|
mGL->fBindAttribLocation(program, 0, "aVertex");
|
|
|
|
mGL->fBindAttribLocation(program, 1, "aTexCoord");
|
|
|
|
mGL->fLinkProgram(program);
|
|
|
|
|
|
|
|
GLint success;
|
|
|
|
mGL->fGetProgramiv(program, LOCAL_GL_LINK_STATUS, &success);
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
mGL->fDeleteProgram(program);
|
|
|
|
program = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mGL->fDeleteShader(vs);
|
|
|
|
mGL->fDeleteShader(fs);
|
|
|
|
|
|
|
|
mPrograms[variant] = program;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mPrograms[variant];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GLReadTexImageHelper::DidGLErrorOccur(const char* str)
|
|
|
|
{
|
|
|
|
GLenum error = mGL->fGetError();
|
|
|
|
if (error != LOCAL_GL_NO_ERROR) {
|
|
|
|
printf_stderr("GL ERROR: %s (0x%04x) %s\n",
|
2018-03-16 16:29:15 +03:00
|
|
|
GLContext::GLErrorToString(error), error, str);
|
2014-01-02 19:17:22 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-08 08:01:51 +04:00
|
|
|
bool
|
2014-02-14 03:38:53 +04:00
|
|
|
GetActualReadFormats(GLContext* gl,
|
|
|
|
GLenum destFormat, GLenum destType,
|
2014-10-08 08:01:51 +04:00
|
|
|
GLenum* out_readFormat, GLenum* out_readType)
|
2014-01-02 19:17:29 +04:00
|
|
|
{
|
2014-10-08 08:01:51 +04:00
|
|
|
MOZ_ASSERT(out_readFormat);
|
|
|
|
MOZ_ASSERT(out_readType);
|
|
|
|
|
2014-01-02 19:17:29 +04:00
|
|
|
if (destFormat == LOCAL_GL_RGBA &&
|
|
|
|
destType == LOCAL_GL_UNSIGNED_BYTE)
|
|
|
|
{
|
2014-10-08 08:01:51 +04:00
|
|
|
*out_readFormat = destFormat;
|
|
|
|
*out_readType = destType;
|
2014-01-02 19:17:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool fallback = true;
|
2014-03-31 13:10:49 +04:00
|
|
|
if (gl->IsGLES()) {
|
2014-01-02 19:17:29 +04:00
|
|
|
GLenum auxFormat = 0;
|
|
|
|
GLenum auxType = 0;
|
|
|
|
|
|
|
|
gl->fGetIntegerv(LOCAL_GL_IMPLEMENTATION_COLOR_READ_FORMAT, (GLint*)&auxFormat);
|
|
|
|
gl->fGetIntegerv(LOCAL_GL_IMPLEMENTATION_COLOR_READ_TYPE, (GLint*)&auxType);
|
|
|
|
|
|
|
|
if (destFormat == auxFormat &&
|
|
|
|
destType == auxType)
|
|
|
|
{
|
|
|
|
fallback = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (destFormat) {
|
|
|
|
case LOCAL_GL_RGB: {
|
|
|
|
if (destType == LOCAL_GL_UNSIGNED_SHORT_5_6_5_REV)
|
|
|
|
fallback = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LOCAL_GL_BGRA: {
|
2014-10-08 08:11:54 +04:00
|
|
|
if (destType == LOCAL_GL_UNSIGNED_BYTE ||
|
|
|
|
destType == LOCAL_GL_UNSIGNED_INT_8_8_8_8_REV)
|
|
|
|
{
|
2014-01-02 19:17:29 +04:00
|
|
|
fallback = false;
|
2014-10-08 08:11:54 +04:00
|
|
|
}
|
2014-01-02 19:17:29 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fallback) {
|
2014-10-08 08:01:51 +04:00
|
|
|
*out_readFormat = LOCAL_GL_RGBA;
|
|
|
|
*out_readType = LOCAL_GL_UNSIGNED_BYTE;
|
2014-01-02 19:17:29 +04:00
|
|
|
return false;
|
|
|
|
} else {
|
2014-10-08 08:01:51 +04:00
|
|
|
*out_readFormat = destFormat;
|
|
|
|
*out_readType = destType;
|
2014-01-02 19:17:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-01-02 19:17:24 +04:00
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
void
|
2014-07-30 21:03:07 +04:00
|
|
|
SwapRAndBComponents(DataSourceSurface* surf)
|
2014-01-02 19:17:29 +04:00
|
|
|
{
|
2014-07-30 21:35:30 +04:00
|
|
|
DataSourceSurface::MappedSurface map;
|
2015-06-11 20:06:23 +03:00
|
|
|
if (!surf->Map(DataSourceSurface::MapType::READ_WRITE, &map)) {
|
|
|
|
MOZ_ASSERT(false, "SwapRAndBComponents: Failed to map surface.");
|
|
|
|
return;
|
|
|
|
}
|
2014-07-30 21:35:30 +04:00
|
|
|
MOZ_ASSERT(map.mStride >= 0);
|
|
|
|
|
|
|
|
const size_t rowBytes = surf->GetSize().width*4;
|
|
|
|
const size_t rowHole = map.mStride - rowBytes;
|
|
|
|
|
|
|
|
uint8_t* row = map.mData;
|
2014-07-30 21:03:07 +04:00
|
|
|
if (!row) {
|
2014-07-30 21:35:30 +04:00
|
|
|
MOZ_ASSERT(false, "SwapRAndBComponents: Failed to get data from"
|
|
|
|
" DataSourceSurface.");
|
|
|
|
surf->Unmap();
|
2014-07-30 21:03:07 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-01-02 19:17:29 +04:00
|
|
|
|
2014-07-30 21:35:30 +04:00
|
|
|
const size_t rows = surf->GetSize().height;
|
|
|
|
for (size_t i = 0; i < rows; i++) {
|
2014-07-30 21:03:07 +04:00
|
|
|
const uint8_t* rowEnd = row + rowBytes;
|
2014-01-02 19:17:22 +04:00
|
|
|
|
2014-07-30 21:03:07 +04:00
|
|
|
while (row != rowEnd) {
|
2014-07-30 21:35:30 +04:00
|
|
|
Swap(row[0], row[2]);
|
2014-07-30 21:03:07 +04:00
|
|
|
row += 4;
|
|
|
|
}
|
2014-01-02 19:17:22 +04:00
|
|
|
|
2014-07-30 21:03:07 +04:00
|
|
|
row += rowHole;
|
2014-01-02 19:17:29 +04:00
|
|
|
}
|
2014-07-30 21:35:30 +04:00
|
|
|
|
|
|
|
surf->Unmap();
|
2014-01-02 19:17:22 +04:00
|
|
|
}
|
|
|
|
|
2014-02-14 03:38:53 +04:00
|
|
|
static int
|
2014-05-03 00:22:07 +04:00
|
|
|
CalcRowStride(int width, int pixelSize, int alignment)
|
2014-02-14 03:38:53 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(alignment);
|
|
|
|
|
2014-05-03 00:22:07 +04:00
|
|
|
int rowStride = width * pixelSize;
|
|
|
|
if (rowStride % alignment) { // Extra at the end of the line?
|
|
|
|
int alignmentCount = rowStride / alignment;
|
|
|
|
rowStride = (alignmentCount+1) * alignment;
|
2014-02-14 03:38:53 +04:00
|
|
|
}
|
2014-05-03 00:22:07 +04:00
|
|
|
return rowStride;
|
2014-02-14 03:38:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2014-05-03 00:22:07 +04:00
|
|
|
GuessAlignment(int width, int pixelSize, int rowStride)
|
2014-02-14 03:38:53 +04:00
|
|
|
{
|
|
|
|
int alignment = 8; // Max GLES allows.
|
2014-05-03 00:22:07 +04:00
|
|
|
while (CalcRowStride(width, pixelSize, alignment) != rowStride) {
|
2014-02-14 03:38:53 +04:00
|
|
|
alignment /= 2;
|
|
|
|
if (!alignment) {
|
2014-05-03 00:22:07 +04:00
|
|
|
NS_WARNING("Bad alignment for GLES. Will use temp surf for readback.");
|
|
|
|
return 0;
|
2014-02-14 03:38:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return alignment;
|
|
|
|
}
|
|
|
|
|
2014-05-13 07:47:52 +04:00
|
|
|
void
|
2014-07-30 21:03:07 +04:00
|
|
|
ReadPixelsIntoDataSurface(GLContext* gl, DataSourceSurface* dest)
|
|
|
|
{
|
2014-05-13 07:47:52 +04:00
|
|
|
gl->MakeCurrent();
|
|
|
|
MOZ_ASSERT(dest->GetSize().width != 0);
|
|
|
|
MOZ_ASSERT(dest->GetSize().height != 0);
|
|
|
|
|
|
|
|
bool hasAlpha = dest->GetFormat() == SurfaceFormat::B8G8R8A8 ||
|
|
|
|
dest->GetFormat() == SurfaceFormat::R8G8B8A8;
|
|
|
|
|
|
|
|
int destPixelSize;
|
|
|
|
GLenum destFormat;
|
|
|
|
GLenum destType;
|
|
|
|
|
|
|
|
switch (dest->GetFormat()) {
|
2014-07-30 21:03:07 +04:00
|
|
|
case SurfaceFormat::B8G8R8A8:
|
|
|
|
case SurfaceFormat::B8G8R8X8:
|
|
|
|
// Needs host (little) endian ARGB.
|
|
|
|
destFormat = LOCAL_GL_BGRA;
|
|
|
|
destType = LOCAL_GL_UNSIGNED_INT_8_8_8_8_REV;
|
|
|
|
break;
|
|
|
|
case SurfaceFormat::R8G8B8A8:
|
|
|
|
case SurfaceFormat::R8G8B8X8:
|
|
|
|
// Needs host (little) endian ABGR.
|
|
|
|
destFormat = LOCAL_GL_RGBA;
|
|
|
|
destType = LOCAL_GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
2015-10-23 09:01:31 +03:00
|
|
|
case SurfaceFormat::R5G6B5_UINT16:
|
2014-07-30 21:03:07 +04:00
|
|
|
destFormat = LOCAL_GL_RGB;
|
|
|
|
destType = LOCAL_GL_UNSIGNED_SHORT_5_6_5_REV;
|
|
|
|
break;
|
|
|
|
default:
|
2015-12-10 15:01:00 +03:00
|
|
|
MOZ_CRASH("GFX: Bad format, read pixels.");
|
2014-05-13 07:47:52 +04:00
|
|
|
}
|
|
|
|
destPixelSize = BytesPerPixel(dest->GetFormat());
|
2017-11-09 11:43:31 +03:00
|
|
|
|
|
|
|
Maybe<DataSourceSurface::ScopedMap> map;
|
|
|
|
map.emplace(dest, DataSourceSurface::READ_WRITE);
|
|
|
|
|
|
|
|
MOZ_ASSERT(dest->GetSize().width * destPixelSize <= map->GetStride());
|
2014-05-13 07:47:52 +04:00
|
|
|
|
|
|
|
GLenum readFormat = destFormat;
|
|
|
|
GLenum readType = destType;
|
|
|
|
bool needsTempSurf = !GetActualReadFormats(gl,
|
|
|
|
destFormat, destType,
|
2014-10-08 08:01:51 +04:00
|
|
|
&readFormat, &readType);
|
2014-05-13 07:47:52 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataSourceSurface> tempSurf;
|
2014-05-13 06:20:27 +04:00
|
|
|
DataSourceSurface* readSurf = dest;
|
|
|
|
int readAlignment = GuessAlignment(dest->GetSize().width,
|
|
|
|
destPixelSize,
|
2017-11-09 11:43:31 +03:00
|
|
|
map->GetStride());
|
2014-05-13 06:20:27 +04:00
|
|
|
if (!readAlignment) {
|
|
|
|
needsTempSurf = true;
|
|
|
|
}
|
2014-05-13 07:47:52 +04:00
|
|
|
if (needsTempSurf) {
|
2016-06-07 02:29:08 +03:00
|
|
|
if (GLContext::ShouldSpew()) {
|
2014-05-13 07:47:52 +04:00
|
|
|
NS_WARNING("Needing intermediary surface for ReadPixels. This will be slow!");
|
|
|
|
}
|
|
|
|
SurfaceFormat readFormatGFX;
|
|
|
|
|
2014-05-13 06:20:27 +04:00
|
|
|
switch (readFormat) {
|
2014-07-30 21:35:30 +04:00
|
|
|
case LOCAL_GL_RGBA: {
|
|
|
|
readFormatGFX = hasAlpha ? SurfaceFormat::R8G8B8A8
|
|
|
|
: SurfaceFormat::R8G8B8X8;
|
|
|
|
break;
|
|
|
|
}
|
2014-05-13 06:20:27 +04:00
|
|
|
case LOCAL_GL_BGRA: {
|
|
|
|
readFormatGFX = hasAlpha ? SurfaceFormat::B8G8R8A8
|
|
|
|
: SurfaceFormat::B8G8R8X8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LOCAL_GL_RGB: {
|
|
|
|
MOZ_ASSERT(destPixelSize == 2);
|
|
|
|
MOZ_ASSERT(readType == LOCAL_GL_UNSIGNED_SHORT_5_6_5_REV);
|
2015-10-23 09:01:31 +03:00
|
|
|
readFormatGFX = SurfaceFormat::R5G6B5_UINT16;
|
2014-05-13 06:20:27 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2015-12-10 15:01:00 +03:00
|
|
|
MOZ_CRASH("GFX: Bad read format, read format.");
|
2014-05-13 06:20:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (readType) {
|
|
|
|
case LOCAL_GL_UNSIGNED_BYTE: {
|
|
|
|
MOZ_ASSERT(readFormat == LOCAL_GL_RGBA);
|
|
|
|
readAlignment = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LOCAL_GL_UNSIGNED_INT_8_8_8_8_REV: {
|
|
|
|
MOZ_ASSERT(readFormat == LOCAL_GL_BGRA);
|
|
|
|
readAlignment = 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LOCAL_GL_UNSIGNED_SHORT_5_6_5_REV: {
|
|
|
|
MOZ_ASSERT(readFormat == LOCAL_GL_RGB);
|
|
|
|
readAlignment = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2015-12-10 15:01:00 +03:00
|
|
|
MOZ_CRASH("GFX: Bad read type, read type.");
|
2014-05-13 06:20:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-11 19:14:50 +04:00
|
|
|
int32_t stride = dest->GetSize().width * BytesPerPixel(readFormatGFX);
|
|
|
|
tempSurf = Factory::CreateDataSourceSurfaceWithStride(dest->GetSize(),
|
|
|
|
readFormatGFX,
|
|
|
|
stride);
|
2014-08-27 19:57:43 +04:00
|
|
|
if (NS_WARN_IF(!tempSurf)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-11 19:14:50 +04:00
|
|
|
readSurf = tempSurf;
|
2017-11-09 11:43:31 +03:00
|
|
|
map = Nothing();
|
|
|
|
map.emplace(readSurf, DataSourceSurface::READ_WRITE);
|
2014-03-11 19:14:50 +04:00
|
|
|
}
|
2017-11-09 11:43:30 +03:00
|
|
|
|
2014-03-11 19:14:50 +04:00
|
|
|
MOZ_ASSERT(readAlignment);
|
2017-11-09 11:43:31 +03:00
|
|
|
MOZ_ASSERT(reinterpret_cast<uintptr_t>(map->GetData()) % readAlignment == 0);
|
2014-03-11 19:14:50 +04:00
|
|
|
|
|
|
|
GLsizei width = dest->GetSize().width;
|
|
|
|
GLsizei height = dest->GetSize().height;
|
|
|
|
|
2016-06-16 17:31:57 +03:00
|
|
|
{
|
|
|
|
ScopedPackState safePackState(gl);
|
|
|
|
gl->fPixelStorei(LOCAL_GL_PACK_ALIGNMENT, readAlignment);
|
2014-03-11 19:14:50 +04:00
|
|
|
|
2016-06-16 17:31:57 +03:00
|
|
|
gl->fReadPixels(0, 0,
|
|
|
|
width, height,
|
|
|
|
readFormat, readType,
|
2017-11-09 11:43:31 +03:00
|
|
|
map->GetData());
|
2016-06-16 17:31:57 +03:00
|
|
|
}
|
2014-03-11 19:14:50 +04:00
|
|
|
|
2017-11-09 11:43:31 +03:00
|
|
|
map = Nothing();
|
|
|
|
|
2014-03-11 19:14:50 +04:00
|
|
|
if (readSurf != dest) {
|
|
|
|
MOZ_ASSERT(readFormat == LOCAL_GL_RGBA);
|
|
|
|
MOZ_ASSERT(readType == LOCAL_GL_UNSIGNED_BYTE);
|
2015-12-18 09:52:16 +03:00
|
|
|
gfx::Factory::CopyDataSourceSurface(readSurf, dest);
|
2014-03-11 19:14:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
already_AddRefed<gfx::DataSourceSurface>
|
2017-11-09 11:43:30 +03:00
|
|
|
YInvertImageSurface(gfx::DataSourceSurface* aSurf, uint32_t aStride)
|
2014-01-02 19:17:29 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataSourceSurface> temp =
|
2014-07-30 21:03:07 +04:00
|
|
|
Factory::CreateDataSourceSurfaceWithStride(aSurf->GetSize(),
|
|
|
|
aSurf->GetFormat(),
|
2017-11-09 11:43:30 +03:00
|
|
|
aStride);
|
2014-08-27 19:57:43 +04:00
|
|
|
if (NS_WARN_IF(!temp)) {
|
2014-07-30 21:03:07 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSourceSurface::MappedSurface map;
|
|
|
|
if (!temp->Map(DataSourceSurface::MapType::WRITE, &map)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DrawTarget> dt =
|
2014-07-30 21:03:07 +04:00
|
|
|
Factory::CreateDrawTargetForData(BackendType::CAIRO,
|
|
|
|
map.mData,
|
|
|
|
temp->GetSize(),
|
|
|
|
map.mStride,
|
|
|
|
temp->GetFormat());
|
|
|
|
if (!dt) {
|
|
|
|
temp->Unmap();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-08 02:58:23 +03:00
|
|
|
dt->SetTransform(Matrix::Scaling(1.0, -1.0) *
|
|
|
|
Matrix::Translation(0.0, aSurf->GetSize().height));
|
2014-07-30 21:03:07 +04:00
|
|
|
Rect rect(0, 0, aSurf->GetSize().width, aSurf->GetSize().height);
|
|
|
|
dt->DrawSurface(aSurf, rect, rect, DrawSurfaceOptions(),
|
|
|
|
DrawOptions(1.0, CompositionOp::OP_SOURCE, AntialiasMode::NONE));
|
2014-06-17 21:35:50 +04:00
|
|
|
temp->Unmap();
|
2014-07-30 21:03:07 +04:00
|
|
|
return temp.forget();
|
2014-01-02 19:17:29 +04:00
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<DataSourceSurface>
|
2014-01-08 12:37:58 +04:00
|
|
|
ReadBackSurface(GLContext* gl, GLuint aTexture, bool aYInvert, SurfaceFormat aFormat)
|
2014-01-02 19:17:29 +04:00
|
|
|
{
|
|
|
|
gl->MakeCurrent();
|
|
|
|
gl->GuaranteeResolve();
|
|
|
|
gl->fActiveTexture(LOCAL_GL_TEXTURE0);
|
|
|
|
gl->fBindTexture(LOCAL_GL_TEXTURE_2D, aTexture);
|
|
|
|
|
2014-01-08 12:37:58 +04:00
|
|
|
IntSize size;
|
2014-01-02 19:17:29 +04:00
|
|
|
gl->fGetTexLevelParameteriv(LOCAL_GL_TEXTURE_2D, 0, LOCAL_GL_TEXTURE_WIDTH, &size.width);
|
|
|
|
gl->fGetTexLevelParameteriv(LOCAL_GL_TEXTURE_2D, 0, LOCAL_GL_TEXTURE_HEIGHT, &size.height);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataSourceSurface> surf =
|
2014-01-10 23:06:16 +04:00
|
|
|
Factory::CreateDataSourceSurfaceWithStride(size, SurfaceFormat::B8G8R8A8,
|
2016-08-25 20:57:39 +03:00
|
|
|
GetAlignedStride<4>(size.width, BytesPerPixel(SurfaceFormat::B8G8R8A8)));
|
2014-01-08 12:37:58 +04:00
|
|
|
|
2014-08-27 19:57:43 +04:00
|
|
|
if (NS_WARN_IF(!surf)) {
|
2014-01-02 19:17:29 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t currentPackAlignment = 0;
|
|
|
|
gl->fGetIntegerv(LOCAL_GL_PACK_ALIGNMENT, (GLint*)¤tPackAlignment);
|
|
|
|
if (currentPackAlignment != 4) {
|
|
|
|
gl->fPixelStorei(LOCAL_GL_PACK_ALIGNMENT, 4);
|
|
|
|
}
|
2014-07-30 21:35:30 +04:00
|
|
|
|
2017-11-09 11:43:30 +03:00
|
|
|
DataSourceSurface::ScopedMap map(surf, DataSourceSurface::READ);
|
|
|
|
gl->fGetTexImage(LOCAL_GL_TEXTURE_2D, 0, LOCAL_GL_RGBA, LOCAL_GL_UNSIGNED_BYTE, map.GetData());
|
2014-07-30 21:35:30 +04:00
|
|
|
|
2014-01-02 19:17:29 +04:00
|
|
|
if (currentPackAlignment != 4) {
|
|
|
|
gl->fPixelStorei(LOCAL_GL_PACK_ALIGNMENT, currentPackAlignment);
|
|
|
|
}
|
|
|
|
|
2014-01-10 23:06:16 +04:00
|
|
|
if (aFormat == SurfaceFormat::R8G8B8A8 || aFormat == SurfaceFormat::R8G8B8X8) {
|
2014-07-30 21:35:30 +04:00
|
|
|
SwapRAndBComponents(surf);
|
2014-01-02 19:17:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aYInvert) {
|
2017-11-09 11:43:30 +03:00
|
|
|
surf = YInvertImageSurface(surf, map.GetStride());
|
2014-01-02 19:17:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return surf.forget();
|
|
|
|
}
|
|
|
|
|
2014-01-02 19:17:22 +04:00
|
|
|
#define CLEANUP_IF_GLERROR_OCCURRED(x) \
|
|
|
|
if (DidGLErrorOccur(x)) { \
|
2015-10-12 06:21:03 +03:00
|
|
|
return false; \
|
2014-01-02 19:17:22 +04:00
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<DataSourceSurface>
|
2014-01-02 19:17:22 +04:00
|
|
|
GLReadTexImageHelper::ReadTexImage(GLuint aTextureId,
|
|
|
|
GLenum aTextureTarget,
|
2014-03-11 19:14:50 +04:00
|
|
|
const gfx::IntSize& aSize,
|
2013-11-08 10:22:05 +04:00
|
|
|
/* ShaderConfigOGL.mFeature */ int aConfig,
|
2014-01-02 19:17:22 +04:00
|
|
|
bool aYInvert)
|
|
|
|
{
|
|
|
|
/* Allocate resulting image surface */
|
2014-03-11 19:14:50 +04:00
|
|
|
int32_t stride = aSize.width * BytesPerPixel(SurfaceFormat::R8G8B8A8);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataSourceSurface> isurf =
|
2014-03-11 19:14:50 +04:00
|
|
|
Factory::CreateDataSourceSurfaceWithStride(aSize,
|
|
|
|
SurfaceFormat::R8G8B8A8,
|
|
|
|
stride);
|
2014-09-15 05:05:00 +04:00
|
|
|
if (NS_WARN_IF(!isurf)) {
|
2014-08-27 19:57:43 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2014-01-02 19:17:22 +04:00
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
if (!ReadTexImage(isurf, aTextureId, aTextureTarget, aSize, aConfig, aYInvert)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isurf.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GLReadTexImageHelper::ReadTexImage(DataSourceSurface* aDest,
|
|
|
|
GLuint aTextureId,
|
|
|
|
GLenum aTextureTarget,
|
|
|
|
const gfx::IntSize& aSize,
|
|
|
|
/* ShaderConfigOGL.mFeature */ int aConfig,
|
|
|
|
bool aYInvert)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aTextureTarget == LOCAL_GL_TEXTURE_2D ||
|
|
|
|
aTextureTarget == LOCAL_GL_TEXTURE_EXTERNAL ||
|
|
|
|
aTextureTarget == LOCAL_GL_TEXTURE_RECTANGLE_ARB);
|
|
|
|
|
|
|
|
mGL->MakeCurrent();
|
|
|
|
|
2014-01-02 19:17:22 +04:00
|
|
|
GLint oldrb, oldfb, oldprog, oldTexUnit, oldTex;
|
|
|
|
GLuint rb, fb;
|
|
|
|
|
|
|
|
do {
|
|
|
|
mGL->fGetIntegerv(LOCAL_GL_RENDERBUFFER_BINDING, &oldrb);
|
|
|
|
mGL->fGetIntegerv(LOCAL_GL_FRAMEBUFFER_BINDING, &oldfb);
|
|
|
|
mGL->fGetIntegerv(LOCAL_GL_CURRENT_PROGRAM, &oldprog);
|
|
|
|
mGL->fGetIntegerv(LOCAL_GL_ACTIVE_TEXTURE, &oldTexUnit);
|
|
|
|
mGL->fActiveTexture(LOCAL_GL_TEXTURE0);
|
|
|
|
switch (aTextureTarget) {
|
|
|
|
case LOCAL_GL_TEXTURE_2D:
|
|
|
|
mGL->fGetIntegerv(LOCAL_GL_TEXTURE_BINDING_2D, &oldTex);
|
|
|
|
break;
|
|
|
|
case LOCAL_GL_TEXTURE_EXTERNAL:
|
|
|
|
mGL->fGetIntegerv(LOCAL_GL_TEXTURE_BINDING_EXTERNAL, &oldTex);
|
|
|
|
break;
|
|
|
|
case LOCAL_GL_TEXTURE_RECTANGLE:
|
|
|
|
mGL->fGetIntegerv(LOCAL_GL_TEXTURE_BINDING_RECTANGLE, &oldTex);
|
|
|
|
break;
|
|
|
|
default: /* Already checked above */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-01-13 18:29:10 +04:00
|
|
|
ScopedGLState scopedScissorTestState(mGL, LOCAL_GL_SCISSOR_TEST, false);
|
|
|
|
ScopedGLState scopedBlendState(mGL, LOCAL_GL_BLEND, false);
|
2014-01-31 03:18:15 +04:00
|
|
|
ScopedViewportRect scopedViewportRect(mGL, 0, 0, aSize.width, aSize.height);
|
2014-01-02 19:17:22 +04:00
|
|
|
|
|
|
|
/* Setup renderbuffer */
|
|
|
|
mGL->fGenRenderbuffers(1, &rb);
|
|
|
|
mGL->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, rb);
|
|
|
|
|
|
|
|
GLenum rbInternalFormat =
|
2014-03-31 13:10:49 +04:00
|
|
|
mGL->IsGLES()
|
2014-01-02 19:17:22 +04:00
|
|
|
? (mGL->IsExtensionSupported(GLContext::OES_rgb8_rgba8) ? LOCAL_GL_RGBA8 : LOCAL_GL_RGBA4)
|
|
|
|
: LOCAL_GL_RGBA;
|
|
|
|
mGL->fRenderbufferStorage(LOCAL_GL_RENDERBUFFER, rbInternalFormat, aSize.width, aSize.height);
|
|
|
|
CLEANUP_IF_GLERROR_OCCURRED("when binding and creating renderbuffer");
|
|
|
|
|
|
|
|
/* Setup framebuffer */
|
|
|
|
mGL->fGenFramebuffers(1, &fb);
|
|
|
|
mGL->fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, fb);
|
|
|
|
mGL->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER, LOCAL_GL_COLOR_ATTACHMENT0,
|
|
|
|
LOCAL_GL_RENDERBUFFER, rb);
|
|
|
|
CLEANUP_IF_GLERROR_OCCURRED("when binding and creating framebuffer");
|
|
|
|
|
2014-01-02 19:17:24 +04:00
|
|
|
MOZ_ASSERT(mGL->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER) == LOCAL_GL_FRAMEBUFFER_COMPLETE);
|
2014-01-02 19:17:22 +04:00
|
|
|
|
|
|
|
/* Setup vertex and fragment shader */
|
2013-11-08 10:22:05 +04:00
|
|
|
GLuint program = TextureImageProgramFor(aTextureTarget, aConfig);
|
2014-01-02 19:17:24 +04:00
|
|
|
MOZ_ASSERT(program);
|
2014-01-02 19:17:22 +04:00
|
|
|
|
|
|
|
mGL->fUseProgram(program);
|
|
|
|
CLEANUP_IF_GLERROR_OCCURRED("when using program");
|
|
|
|
mGL->fUniform1i(mGL->fGetUniformLocation(program, "uTexture"), 0);
|
|
|
|
CLEANUP_IF_GLERROR_OCCURRED("when setting uniform location");
|
|
|
|
|
|
|
|
/* Setup quad geometry */
|
|
|
|
mGL->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
float w = (aTextureTarget == LOCAL_GL_TEXTURE_RECTANGLE) ? (float) aSize.width : 1.0f;
|
|
|
|
float h = (aTextureTarget == LOCAL_GL_TEXTURE_RECTANGLE) ? (float) aSize.height : 1.0f;
|
|
|
|
|
|
|
|
const float
|
2014-01-02 19:17:24 +04:00
|
|
|
vertexArray[4*2] = {
|
|
|
|
-1.0f, -1.0f,
|
|
|
|
1.0f, -1.0f,
|
|
|
|
-1.0f, 1.0f,
|
|
|
|
1.0f, 1.0f
|
2014-05-30 02:28:40 +04:00
|
|
|
};
|
|
|
|
ScopedVertexAttribPointer autoAttrib0(mGL, 0, 2, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, 0, 0, vertexArray);
|
2014-01-02 19:17:22 +04:00
|
|
|
|
|
|
|
const float u0 = 0.0f;
|
|
|
|
const float u1 = w;
|
|
|
|
const float v0 = aYInvert ? h : 0.0f;
|
|
|
|
const float v1 = aYInvert ? 0.0f : h;
|
|
|
|
const float texCoordArray[8] = { u0, v0,
|
|
|
|
u1, v0,
|
|
|
|
u0, v1,
|
|
|
|
u1, v1 };
|
2014-05-30 02:28:40 +04:00
|
|
|
ScopedVertexAttribPointer autoAttrib1(mGL, 1, 2, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, 0, 0, texCoordArray);
|
2014-01-02 19:17:22 +04:00
|
|
|
|
|
|
|
/* Bind the texture */
|
|
|
|
if (aTextureId) {
|
|
|
|
mGL->fBindTexture(aTextureTarget, aTextureId);
|
|
|
|
CLEANUP_IF_GLERROR_OCCURRED("when binding texture");
|
|
|
|
}
|
|
|
|
|
|
|
|
mGL->fDrawArrays(LOCAL_GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
CLEANUP_IF_GLERROR_OCCURRED("when drawing texture");
|
|
|
|
|
|
|
|
/* Read-back draw results */
|
2015-10-12 06:21:03 +03:00
|
|
|
ReadPixelsIntoDataSurface(mGL, aDest);
|
2014-01-02 19:17:22 +04:00
|
|
|
CLEANUP_IF_GLERROR_OCCURRED("when reading pixels into surface");
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
/* Restore GL state */
|
|
|
|
mGL->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, oldrb);
|
|
|
|
mGL->fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, oldfb);
|
|
|
|
mGL->fUseProgram(oldprog);
|
|
|
|
|
|
|
|
// note that deleting 0 has no effect in any of these calls
|
|
|
|
mGL->fDeleteRenderbuffers(1, &rb);
|
|
|
|
mGL->fDeleteFramebuffers(1, &fb);
|
|
|
|
|
|
|
|
if (aTextureId)
|
|
|
|
mGL->fBindTexture(aTextureTarget, oldTex);
|
|
|
|
|
|
|
|
if (oldTexUnit != LOCAL_GL_TEXTURE0)
|
|
|
|
mGL->fActiveTexture(oldTexUnit);
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
return true;
|
2014-01-02 19:17:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef CLEANUP_IF_GLERROR_OCCURRED
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gl
|
|
|
|
} // namespace mozilla
|