2013-12-03 22:44:38 +04:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40; -*- */
|
|
|
|
/* 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 "GLUploadHelpers.h"
|
|
|
|
|
|
|
|
#include "GLContext.h"
|
|
|
|
#include "mozilla/gfx/2D.h"
|
2016-07-22 10:51:18 +03:00
|
|
|
#include "gfxUtils.h"
|
2014-02-14 15:40:26 +04:00
|
|
|
#include "mozilla/gfx/Tools.h" // For BytesPerPixel
|
2013-12-03 22:44:38 +04:00
|
|
|
#include "nsRegion.h"
|
2016-01-27 00:03:37 +03:00
|
|
|
#include "GfxTexturesReporter.h"
|
|
|
|
#include "mozilla/gfx/Logging.h"
|
2013-12-03 22:44:38 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-02-14 15:40:26 +04:00
|
|
|
using namespace gfx;
|
2013-12-03 22:44:38 +04:00
|
|
|
|
|
|
|
namespace gl {
|
|
|
|
|
|
|
|
static unsigned int
|
2016-06-11 04:05:40 +03:00
|
|
|
DataOffset(const IntPoint& aPoint, int32_t aStride, SurfaceFormat aFormat)
|
2013-12-03 22:44:38 +04:00
|
|
|
{
|
|
|
|
unsigned int data = aPoint.y * aStride;
|
2014-02-14 15:40:26 +04:00
|
|
|
data += aPoint.x * BytesPerPixel(aFormat);
|
2013-12-03 22:44:38 +04:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2013-12-03 22:44:38 +04:00
|
|
|
static GLint GetAddressAlignment(ptrdiff_t aAddress)
|
|
|
|
{
|
|
|
|
if (!(aAddress & 0x7)) {
|
|
|
|
return 8;
|
|
|
|
} else if (!(aAddress & 0x3)) {
|
|
|
|
return 4;
|
|
|
|
} else if (!(aAddress & 0x1)) {
|
|
|
|
return 2;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take texture data in a given buffer and copy it into a larger buffer,
|
|
|
|
// padding out the edge pixels for filtering if necessary
|
|
|
|
static void
|
|
|
|
CopyAndPadTextureData(const GLvoid* srcBuffer,
|
|
|
|
GLvoid* dstBuffer,
|
|
|
|
GLsizei srcWidth, GLsizei srcHeight,
|
|
|
|
GLsizei dstWidth, GLsizei dstHeight,
|
|
|
|
GLsizei stride, GLint pixelsize)
|
|
|
|
{
|
2016-06-11 04:37:07 +03:00
|
|
|
unsigned char* rowDest = static_cast<unsigned char*>(dstBuffer);
|
|
|
|
const unsigned char* source = static_cast<const unsigned char*>(srcBuffer);
|
2013-12-03 22:44:38 +04:00
|
|
|
|
|
|
|
for (GLsizei h = 0; h < srcHeight; ++h) {
|
|
|
|
memcpy(rowDest, source, srcWidth * pixelsize);
|
|
|
|
rowDest += dstWidth * pixelsize;
|
|
|
|
source += stride;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLsizei padHeight = srcHeight;
|
|
|
|
|
|
|
|
// Pad out an extra row of pixels so that edge filtering doesn't use garbage data
|
|
|
|
if (dstHeight > srcHeight) {
|
|
|
|
memcpy(rowDest, source - stride, srcWidth * pixelsize);
|
|
|
|
padHeight++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pad out an extra column of pixels
|
|
|
|
if (dstWidth > srcWidth) {
|
|
|
|
rowDest = static_cast<unsigned char*>(dstBuffer) + srcWidth * pixelsize;
|
|
|
|
for (GLsizei h = 0; h < padHeight; ++h) {
|
|
|
|
memcpy(rowDest, rowDest - pixelsize, pixelsize);
|
|
|
|
rowDest += dstWidth * pixelsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-03 22:44:38 +04:00
|
|
|
// In both of these cases (for the Adreno at least) it is impossible
|
|
|
|
// to determine good or bad driver versions for POT texture uploads,
|
|
|
|
// so blacklist them all. Newer drivers use a different rendering
|
|
|
|
// string in the form "Adreno (TM) 200" and the drivers we've seen so
|
|
|
|
// far work fine with NPOT textures, so don't blacklist those until we
|
|
|
|
// have evidence of any problems with them.
|
|
|
|
bool
|
|
|
|
CanUploadSubTextures(GLContext* gl)
|
|
|
|
{
|
|
|
|
if (!gl->WorkAroundDriverBugs())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// There are certain GPUs that we don't want to use glTexSubImage2D on
|
|
|
|
// because that function can be very slow and/or buggy
|
2014-01-10 22:55:23 +04:00
|
|
|
if (gl->Renderer() == GLRenderer::Adreno200 ||
|
|
|
|
gl->Renderer() == GLRenderer::Adreno205)
|
2013-12-03 22:44:38 +04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// On PowerVR glTexSubImage does a readback, so it will be slower
|
|
|
|
// than just doing a glTexImage2D() directly. i.e. 26ms vs 10ms
|
2014-01-10 22:55:23 +04:00
|
|
|
if (gl->Renderer() == GLRenderer::SGX540 ||
|
|
|
|
gl->Renderer() == GLRenderer::SGX530)
|
2013-12-03 22:44:38 +04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-03 22:44:38 +04:00
|
|
|
static void
|
|
|
|
TexSubImage2DWithUnpackSubimageGLES(GLContext* gl,
|
|
|
|
GLenum target, GLint level,
|
|
|
|
GLint xoffset, GLint yoffset,
|
|
|
|
GLsizei width, GLsizei height,
|
|
|
|
GLsizei stride, GLint pixelsize,
|
|
|
|
GLenum format, GLenum type,
|
|
|
|
const GLvoid* pixels)
|
|
|
|
{
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT,
|
|
|
|
std::min(GetAddressAlignment((ptrdiff_t)pixels),
|
|
|
|
GetAddressAlignment((ptrdiff_t)stride)));
|
|
|
|
// When using GL_UNPACK_ROW_LENGTH, we need to work around a Tegra
|
|
|
|
// driver crash where the driver apparently tries to read
|
|
|
|
// (stride - width * pixelsize) bytes past the end of the last input
|
|
|
|
// row. We only upload the first height-1 rows using GL_UNPACK_ROW_LENGTH,
|
|
|
|
// and then we upload the final row separately. See bug 697990.
|
|
|
|
int rowLength = stride/pixelsize;
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ROW_LENGTH, rowLength);
|
|
|
|
gl->fTexSubImage2D(target,
|
|
|
|
level,
|
|
|
|
xoffset,
|
|
|
|
yoffset,
|
|
|
|
width,
|
|
|
|
height-1,
|
|
|
|
format,
|
|
|
|
type,
|
|
|
|
pixels);
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ROW_LENGTH, 0);
|
|
|
|
gl->fTexSubImage2D(target,
|
|
|
|
level,
|
|
|
|
xoffset,
|
|
|
|
yoffset+height-1,
|
|
|
|
width,
|
|
|
|
1,
|
|
|
|
format,
|
|
|
|
type,
|
2016-06-11 04:37:07 +03:00
|
|
|
(const unsigned char*)pixels+(height-1)*stride);
|
2013-12-03 22:44:38 +04:00
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
TexSubImage2DWithoutUnpackSubimage(GLContext* gl,
|
|
|
|
GLenum target, GLint level,
|
|
|
|
GLint xoffset, GLint yoffset,
|
|
|
|
GLsizei width, GLsizei height,
|
|
|
|
GLsizei stride, GLint pixelsize,
|
|
|
|
GLenum format, GLenum type,
|
|
|
|
const GLvoid* pixels)
|
|
|
|
{
|
|
|
|
// Not using the whole row of texture data and GL_UNPACK_ROW_LENGTH
|
|
|
|
// isn't supported. We make a copy of the texture data we're using,
|
|
|
|
// such that we're using the whole row of data in the copy. This turns
|
|
|
|
// out to be more efficient than uploading row-by-row; see bug 698197.
|
2016-06-11 04:37:07 +03:00
|
|
|
unsigned char* newPixels = new unsigned char[width*height*pixelsize];
|
|
|
|
unsigned char* rowDest = newPixels;
|
|
|
|
const unsigned char* rowSource = (const unsigned char*)pixels;
|
2013-12-03 22:44:38 +04:00
|
|
|
for (int h = 0; h < height; h++) {
|
|
|
|
memcpy(rowDest, rowSource, width*pixelsize);
|
|
|
|
rowDest += width*pixelsize;
|
|
|
|
rowSource += stride;
|
|
|
|
}
|
|
|
|
|
|
|
|
stride = width*pixelsize;
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT,
|
|
|
|
std::min(GetAddressAlignment((ptrdiff_t)newPixels),
|
|
|
|
GetAddressAlignment((ptrdiff_t)stride)));
|
|
|
|
gl->fTexSubImage2D(target,
|
|
|
|
level,
|
|
|
|
xoffset,
|
|
|
|
yoffset,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
format,
|
|
|
|
type,
|
|
|
|
newPixels);
|
|
|
|
delete [] newPixels;
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-06-11 04:37:07 +03:00
|
|
|
TexSubImage2DHelper(GLContext* gl,
|
2013-12-03 22:44:38 +04:00
|
|
|
GLenum target, GLint level,
|
|
|
|
GLint xoffset, GLint yoffset,
|
|
|
|
GLsizei width, GLsizei height, GLsizei stride,
|
|
|
|
GLint pixelsize, GLenum format,
|
|
|
|
GLenum type, const GLvoid* pixels)
|
|
|
|
{
|
2014-03-31 13:10:49 +04:00
|
|
|
if (gl->IsGLES()) {
|
2013-12-03 22:44:38 +04:00
|
|
|
if (stride == width * pixelsize) {
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT,
|
|
|
|
std::min(GetAddressAlignment((ptrdiff_t)pixels),
|
|
|
|
GetAddressAlignment((ptrdiff_t)stride)));
|
|
|
|
gl->fTexSubImage2D(target,
|
|
|
|
level,
|
|
|
|
xoffset,
|
|
|
|
yoffset,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
format,
|
|
|
|
type,
|
|
|
|
pixels);
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
} else if (gl->IsExtensionSupported(GLContext::EXT_unpack_subimage)) {
|
|
|
|
TexSubImage2DWithUnpackSubimageGLES(gl, target, level, xoffset, yoffset,
|
|
|
|
width, height, stride,
|
|
|
|
pixelsize, format, type, pixels);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
TexSubImage2DWithoutUnpackSubimage(gl, target, level, xoffset, yoffset,
|
|
|
|
width, height, stride,
|
|
|
|
pixelsize, format, type, pixels);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// desktop GL (non-ES) path
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT,
|
|
|
|
std::min(GetAddressAlignment((ptrdiff_t)pixels),
|
|
|
|
GetAddressAlignment((ptrdiff_t)stride)));
|
|
|
|
int rowLength = stride/pixelsize;
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ROW_LENGTH, rowLength);
|
|
|
|
gl->fTexSubImage2D(target,
|
|
|
|
level,
|
|
|
|
xoffset,
|
|
|
|
yoffset,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
format,
|
|
|
|
type,
|
|
|
|
pixels);
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ROW_LENGTH, 0);
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-06-11 04:37:07 +03:00
|
|
|
TexImage2DHelper(GLContext* gl,
|
2013-12-03 22:44:38 +04:00
|
|
|
GLenum target, GLint level, GLint internalformat,
|
|
|
|
GLsizei width, GLsizei height, GLsizei stride,
|
|
|
|
GLint pixelsize, GLint border, GLenum format,
|
2016-06-11 04:37:07 +03:00
|
|
|
GLenum type, const GLvoid* pixels)
|
2013-12-03 22:44:38 +04:00
|
|
|
{
|
2014-03-31 13:10:49 +04:00
|
|
|
if (gl->IsGLES()) {
|
2013-12-03 22:44:38 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(format == (GLenum)internalformat,
|
|
|
|
"format and internalformat not the same for glTexImage2D on GLES2");
|
|
|
|
|
2016-07-22 10:51:18 +03:00
|
|
|
MOZ_ASSERT(width >= 0 && height >= 0);
|
2013-12-03 22:44:38 +04:00
|
|
|
if (!CanUploadNonPowerOfTwo(gl)
|
2013-12-03 22:44:38 +04:00
|
|
|
&& (stride != width * pixelsize
|
2016-07-22 10:51:18 +03:00
|
|
|
|| !IsPowerOfTwo((uint32_t)width)
|
|
|
|
|| !IsPowerOfTwo((uint32_t)height))) {
|
2013-12-03 22:44:38 +04:00
|
|
|
|
|
|
|
// Pad out texture width and height to the next power of two
|
|
|
|
// as we don't support/want non power of two texture uploads
|
2016-07-22 11:47:44 +03:00
|
|
|
GLsizei paddedWidth = RoundUpPow2((uint32_t)width);
|
|
|
|
GLsizei paddedHeight = RoundUpPow2((uint32_t)height);
|
2013-12-03 22:44:38 +04:00
|
|
|
|
|
|
|
GLvoid* paddedPixels = new unsigned char[paddedWidth * paddedHeight * pixelsize];
|
|
|
|
|
|
|
|
// Pad out texture data to be in a POT sized buffer for uploading to
|
|
|
|
// a POT sized texture
|
|
|
|
CopyAndPadTextureData(pixels, paddedPixels, width, height,
|
|
|
|
paddedWidth, paddedHeight, stride, pixelsize);
|
|
|
|
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT,
|
|
|
|
std::min(GetAddressAlignment((ptrdiff_t)paddedPixels),
|
|
|
|
GetAddressAlignment((ptrdiff_t)paddedWidth * pixelsize)));
|
|
|
|
gl->fTexImage2D(target,
|
|
|
|
border,
|
|
|
|
internalformat,
|
|
|
|
paddedWidth,
|
|
|
|
paddedHeight,
|
|
|
|
border,
|
|
|
|
format,
|
|
|
|
type,
|
|
|
|
paddedPixels);
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
|
|
|
|
delete[] static_cast<unsigned char*>(paddedPixels);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stride == width * pixelsize) {
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT,
|
|
|
|
std::min(GetAddressAlignment((ptrdiff_t)pixels),
|
|
|
|
GetAddressAlignment((ptrdiff_t)stride)));
|
|
|
|
gl->fTexImage2D(target,
|
|
|
|
border,
|
|
|
|
internalformat,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
border,
|
|
|
|
format,
|
|
|
|
type,
|
|
|
|
pixels);
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
} else {
|
|
|
|
// Use GLES-specific workarounds for GL_UNPACK_ROW_LENGTH; these are
|
|
|
|
// implemented in TexSubImage2D.
|
|
|
|
gl->fTexImage2D(target,
|
|
|
|
border,
|
|
|
|
internalformat,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
border,
|
|
|
|
format,
|
|
|
|
type,
|
|
|
|
nullptr);
|
|
|
|
TexSubImage2DHelper(gl,
|
|
|
|
target,
|
|
|
|
level,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
stride,
|
|
|
|
pixelsize,
|
|
|
|
format,
|
|
|
|
type,
|
|
|
|
pixels);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// desktop GL (non-ES) path
|
|
|
|
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT,
|
|
|
|
std::min(GetAddressAlignment((ptrdiff_t)pixels),
|
|
|
|
GetAddressAlignment((ptrdiff_t)stride)));
|
|
|
|
int rowLength = stride/pixelsize;
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ROW_LENGTH, rowLength);
|
|
|
|
gl->fTexImage2D(target,
|
|
|
|
level,
|
|
|
|
internalformat,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
border,
|
|
|
|
format,
|
|
|
|
type,
|
|
|
|
pixels);
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ROW_LENGTH, 0);
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-03 22:44:38 +04:00
|
|
|
SurfaceFormat
|
|
|
|
UploadImageDataToTexture(GLContext* gl,
|
|
|
|
unsigned char* aData,
|
|
|
|
int32_t aStride,
|
2014-02-14 15:40:26 +04:00
|
|
|
SurfaceFormat aFormat,
|
2013-12-03 22:44:38 +04:00
|
|
|
const nsIntRegion& aDstRegion,
|
|
|
|
GLuint& aTexture,
|
2016-01-27 00:03:37 +03:00
|
|
|
size_t* aOutUploadSize,
|
2016-01-27 19:09:01 +03:00
|
|
|
bool aNeedInit,
|
2013-12-03 22:44:38 +04:00
|
|
|
bool aPixelBuffer,
|
|
|
|
GLenum aTextureUnit,
|
|
|
|
GLenum aTextureTarget)
|
|
|
|
{
|
2016-01-27 19:09:01 +03:00
|
|
|
bool textureInited = aNeedInit ? false : true;
|
2013-12-03 22:44:38 +04:00
|
|
|
gl->MakeCurrent();
|
|
|
|
gl->fActiveTexture(aTextureUnit);
|
|
|
|
|
|
|
|
if (!aTexture) {
|
|
|
|
gl->fGenTextures(1, &aTexture);
|
|
|
|
gl->fBindTexture(aTextureTarget, aTexture);
|
|
|
|
gl->fTexParameteri(aTextureTarget,
|
|
|
|
LOCAL_GL_TEXTURE_MIN_FILTER,
|
|
|
|
LOCAL_GL_LINEAR);
|
|
|
|
gl->fTexParameteri(aTextureTarget,
|
|
|
|
LOCAL_GL_TEXTURE_MAG_FILTER,
|
|
|
|
LOCAL_GL_LINEAR);
|
|
|
|
gl->fTexParameteri(aTextureTarget,
|
|
|
|
LOCAL_GL_TEXTURE_WRAP_S,
|
|
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
gl->fTexParameteri(aTextureTarget,
|
|
|
|
LOCAL_GL_TEXTURE_WRAP_T,
|
|
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
textureInited = false;
|
|
|
|
} else {
|
|
|
|
gl->fBindTexture(aTextureTarget, aTexture);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIntRegion paintRegion;
|
|
|
|
if (!textureInited) {
|
|
|
|
paintRegion = nsIntRegion(aDstRegion.GetBounds());
|
|
|
|
} else {
|
|
|
|
paintRegion = aDstRegion;
|
|
|
|
}
|
|
|
|
|
2014-02-20 17:05:34 +04:00
|
|
|
GLenum format = 0;
|
|
|
|
GLenum internalFormat = 0;
|
|
|
|
GLenum type = 0;
|
2014-02-14 15:40:26 +04:00
|
|
|
int32_t pixelSize = BytesPerPixel(aFormat);
|
2014-02-20 17:05:34 +04:00
|
|
|
SurfaceFormat surfaceFormat = gfx::SurfaceFormat::UNKNOWN;
|
2013-12-03 22:44:38 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(gl->GetPreferredARGB32Format() == LOCAL_GL_BGRA ||
|
|
|
|
gl->GetPreferredARGB32Format() == LOCAL_GL_RGBA);
|
2014-10-08 08:11:54 +04:00
|
|
|
|
2013-12-03 22:44:38 +04:00
|
|
|
switch (aFormat) {
|
2014-02-14 15:40:26 +04:00
|
|
|
case SurfaceFormat::B8G8R8A8:
|
2013-12-03 22:44:38 +04:00
|
|
|
if (gl->GetPreferredARGB32Format() == LOCAL_GL_BGRA) {
|
|
|
|
format = LOCAL_GL_BGRA;
|
2014-02-14 15:40:26 +04:00
|
|
|
surfaceFormat = SurfaceFormat::R8G8B8A8;
|
2013-12-03 22:44:38 +04:00
|
|
|
type = LOCAL_GL_UNSIGNED_INT_8_8_8_8_REV;
|
|
|
|
} else {
|
|
|
|
format = LOCAL_GL_RGBA;
|
2014-02-14 15:40:26 +04:00
|
|
|
surfaceFormat = SurfaceFormat::B8G8R8A8;
|
2013-12-03 22:44:38 +04:00
|
|
|
type = LOCAL_GL_UNSIGNED_BYTE;
|
|
|
|
}
|
|
|
|
internalFormat = LOCAL_GL_RGBA;
|
|
|
|
break;
|
2014-02-14 15:40:26 +04:00
|
|
|
case SurfaceFormat::B8G8R8X8:
|
|
|
|
// Treat BGRX surfaces as BGRA except for the surface
|
2013-12-03 22:44:38 +04:00
|
|
|
// format used.
|
|
|
|
if (gl->GetPreferredARGB32Format() == LOCAL_GL_BGRA) {
|
|
|
|
format = LOCAL_GL_BGRA;
|
2014-02-14 15:40:26 +04:00
|
|
|
surfaceFormat = SurfaceFormat::R8G8B8X8;
|
2013-12-03 22:44:38 +04:00
|
|
|
type = LOCAL_GL_UNSIGNED_INT_8_8_8_8_REV;
|
|
|
|
} else {
|
|
|
|
format = LOCAL_GL_RGBA;
|
2014-02-14 15:40:26 +04:00
|
|
|
surfaceFormat = SurfaceFormat::B8G8R8X8;
|
2013-12-03 22:44:38 +04:00
|
|
|
type = LOCAL_GL_UNSIGNED_BYTE;
|
|
|
|
}
|
|
|
|
internalFormat = LOCAL_GL_RGBA;
|
|
|
|
break;
|
2014-10-08 08:11:54 +04:00
|
|
|
case SurfaceFormat::R8G8B8A8:
|
|
|
|
if (gl->GetPreferredARGB32Format() == LOCAL_GL_BGRA) {
|
2014-10-09 00:04:19 +04:00
|
|
|
// Upload our RGBA as BGRA, but store that the uploaded format is
|
|
|
|
// BGRA. (sample from R to get B)
|
2014-10-08 08:11:54 +04:00
|
|
|
format = LOCAL_GL_BGRA;
|
|
|
|
type = LOCAL_GL_UNSIGNED_INT_8_8_8_8_REV;
|
|
|
|
surfaceFormat = SurfaceFormat::B8G8R8A8;
|
|
|
|
} else {
|
|
|
|
format = LOCAL_GL_RGBA;
|
|
|
|
type = LOCAL_GL_UNSIGNED_BYTE;
|
|
|
|
surfaceFormat = SurfaceFormat::R8G8B8A8;
|
|
|
|
}
|
|
|
|
internalFormat = LOCAL_GL_RGBA;
|
|
|
|
break;
|
|
|
|
case SurfaceFormat::R8G8B8X8:
|
|
|
|
// Treat RGBX surfaces as RGBA except for the surface
|
|
|
|
// format used.
|
|
|
|
if (gl->GetPreferredARGB32Format() == LOCAL_GL_BGRA) {
|
|
|
|
format = LOCAL_GL_BGRA;
|
|
|
|
type = LOCAL_GL_UNSIGNED_INT_8_8_8_8_REV;
|
|
|
|
surfaceFormat = SurfaceFormat::B8G8R8X8;
|
|
|
|
} else {
|
|
|
|
format = LOCAL_GL_RGBA;
|
|
|
|
type = LOCAL_GL_UNSIGNED_BYTE;
|
|
|
|
surfaceFormat = SurfaceFormat::R8G8B8X8;
|
|
|
|
}
|
|
|
|
internalFormat = LOCAL_GL_RGBA;
|
|
|
|
break;
|
2015-10-23 09:01:31 +03:00
|
|
|
case SurfaceFormat::R5G6B5_UINT16:
|
2013-12-03 22:44:38 +04:00
|
|
|
internalFormat = format = LOCAL_GL_RGB;
|
|
|
|
type = LOCAL_GL_UNSIGNED_SHORT_5_6_5;
|
2015-10-23 09:01:31 +03:00
|
|
|
surfaceFormat = SurfaceFormat::R5G6B5_UINT16;
|
2013-12-03 22:44:38 +04:00
|
|
|
break;
|
2014-02-14 15:40:26 +04:00
|
|
|
case SurfaceFormat::A8:
|
2013-12-03 22:44:38 +04:00
|
|
|
internalFormat = format = LOCAL_GL_LUMINANCE;
|
|
|
|
type = LOCAL_GL_UNSIGNED_BYTE;
|
|
|
|
// We don't have a specific luminance shader
|
2014-02-14 15:40:26 +04:00
|
|
|
surfaceFormat = SurfaceFormat::A8;
|
2013-12-03 22:44:38 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ASSERTION(false, "Unhandled image surface format!");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Top left point of the region's bounding rectangle.
|
2015-04-21 18:04:57 +03:00
|
|
|
IntPoint topLeft = paintRegion.GetBounds().TopLeft();
|
2013-12-03 22:44:38 +04:00
|
|
|
|
2016-01-27 00:03:37 +03:00
|
|
|
if (aOutUploadSize) {
|
|
|
|
*aOutUploadSize = 0;
|
|
|
|
}
|
|
|
|
|
2016-01-19 04:20:58 +03:00
|
|
|
for (auto iter = paintRegion.RectIter(); !iter.Done(); iter.Next()) {
|
|
|
|
const IntRect& rect = iter.Get();
|
2013-12-03 22:44:38 +04:00
|
|
|
// The inital data pointer is at the top left point of the region's
|
|
|
|
// bounding rectangle. We need to find the offset of this rect
|
|
|
|
// within the region and adjust the data pointer accordingly.
|
2016-06-11 04:37:07 +03:00
|
|
|
unsigned char* rectData =
|
2016-01-19 04:20:58 +03:00
|
|
|
aData + DataOffset(rect.TopLeft() - topLeft, aStride, aFormat);
|
2013-12-03 22:44:38 +04:00
|
|
|
|
2016-01-19 04:20:58 +03:00
|
|
|
NS_ASSERTION(textureInited || (rect.x == 0 && rect.y == 0),
|
2013-12-03 22:44:38 +04:00
|
|
|
"Must be uploading to the origin when we don't have an existing texture");
|
|
|
|
|
2013-12-03 22:44:38 +04:00
|
|
|
if (textureInited && CanUploadSubTextures(gl)) {
|
2013-12-03 22:44:38 +04:00
|
|
|
TexSubImage2DHelper(gl,
|
|
|
|
aTextureTarget,
|
|
|
|
0,
|
2016-01-19 04:20:58 +03:00
|
|
|
rect.x,
|
|
|
|
rect.y,
|
|
|
|
rect.width,
|
|
|
|
rect.height,
|
2013-12-03 22:44:38 +04:00
|
|
|
aStride,
|
|
|
|
pixelSize,
|
|
|
|
format,
|
|
|
|
type,
|
|
|
|
rectData);
|
2013-12-03 22:44:38 +04:00
|
|
|
} else {
|
2013-12-03 22:44:38 +04:00
|
|
|
TexImage2DHelper(gl,
|
|
|
|
aTextureTarget,
|
|
|
|
0,
|
|
|
|
internalFormat,
|
2016-01-19 04:20:58 +03:00
|
|
|
rect.width,
|
|
|
|
rect.height,
|
2013-12-03 22:44:38 +04:00
|
|
|
aStride,
|
|
|
|
pixelSize,
|
|
|
|
0,
|
|
|
|
format,
|
|
|
|
type,
|
|
|
|
rectData);
|
2013-12-03 22:44:38 +04:00
|
|
|
}
|
|
|
|
|
2016-01-27 00:03:37 +03:00
|
|
|
if (aOutUploadSize && !textureInited) {
|
|
|
|
uint32_t texelSize = GetBytesPerTexel(internalFormat, type);
|
|
|
|
size_t numTexels = size_t(rect.width) * size_t(rect.height);
|
|
|
|
*aOutUploadSize += texelSize * numTexels;
|
|
|
|
}
|
2013-12-03 22:44:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return surfaceFormat;
|
|
|
|
}
|
|
|
|
|
2014-02-14 22:30:39 +04:00
|
|
|
SurfaceFormat
|
|
|
|
UploadSurfaceToTexture(GLContext* gl,
|
2016-01-27 00:03:37 +03:00
|
|
|
DataSourceSurface* aSurface,
|
2013-12-03 22:44:38 +04:00
|
|
|
const nsIntRegion& aDstRegion,
|
|
|
|
GLuint& aTexture,
|
2016-01-27 00:03:37 +03:00
|
|
|
size_t* aOutUploadSize,
|
2016-01-27 19:09:01 +03:00
|
|
|
bool aNeedInit,
|
2015-04-21 18:04:57 +03:00
|
|
|
const gfx::IntPoint& aSrcPoint,
|
2013-12-03 22:44:38 +04:00
|
|
|
bool aPixelBuffer,
|
|
|
|
GLenum aTextureUnit,
|
|
|
|
GLenum aTextureTarget)
|
|
|
|
{
|
|
|
|
unsigned char* data = aPixelBuffer ? nullptr : aSurface->GetData();
|
|
|
|
int32_t stride = aSurface->Stride();
|
2014-02-14 15:40:26 +04:00
|
|
|
SurfaceFormat format = aSurface->GetFormat();
|
2013-12-03 22:44:38 +04:00
|
|
|
data += DataOffset(aSrcPoint, stride, format);
|
|
|
|
return UploadImageDataToTexture(gl, data, stride, format,
|
2016-01-27 00:03:37 +03:00
|
|
|
aDstRegion, aTexture, aOutUploadSize,
|
2016-01-27 19:09:01 +03:00
|
|
|
aNeedInit, aPixelBuffer, aTextureUnit,
|
2013-12-03 22:44:38 +04:00
|
|
|
aTextureTarget);
|
|
|
|
}
|
|
|
|
|
2013-12-03 22:44:38 +04:00
|
|
|
bool
|
|
|
|
CanUploadNonPowerOfTwo(GLContext* gl)
|
|
|
|
{
|
|
|
|
if (!gl->WorkAroundDriverBugs())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Some GPUs driver crash when uploading non power of two 565 textures.
|
2014-01-10 22:55:23 +04:00
|
|
|
return gl->Renderer() != GLRenderer::Adreno200 &&
|
|
|
|
gl->Renderer() != GLRenderer::Adreno205;
|
2013-12-03 22:44:38 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gl
|
|
|
|
} // namespace mozilla
|