Bug 948765 - Add a LayerUtils file in gfx/layers. r=nical

This file is currently just a helper for doing PremultiplySurface in
Moz2D. It corresponds to an existing Thebes one in the gfxUtils class.

An upcoming patch will require this PremultiplySurface method. The
existing one in gfxUtils has been renamed internally to
DeprecatedPremultiplyTables.
This commit is contained in:
Tor Arvid Lund 2014-02-12 10:07:46 -05:00
parent e5fc47840c
commit b272755e2d
9 changed files with 124 additions and 4 deletions

79
gfx/layers/LayerUtils.cpp Normal file
View File

@ -0,0 +1,79 @@
/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "LayerUtils.h"
#include "PremultiplyTables.h"
namespace mozilla {
namespace layers {
using namespace mozilla::gfx;
static inline const uint8_t PremultiplyValue(uint8_t a, uint8_t v) {
return PremultiplyTable[a*256+v];
}
static inline const uint8_t UnpremultiplyValue(uint8_t a, uint8_t v) {
return UnpremultiplyTable[a*256+v];
}
void
PremultiplySurface(DataSourceSurface* srcSurface,
DataSourceSurface* destSurface)
{
if (!destSurface)
destSurface = srcSurface;
IntSize srcSize = srcSurface->GetSize();
MOZ_ASSERT(srcSurface->GetFormat() == destSurface->GetFormat() &&
srcSize.width == destSurface->GetSize().width &&
srcSize.height == destSurface->GetSize().height &&
srcSurface->Stride() == destSurface->Stride(),
"Source and destination surfaces don't have identical characteristics");
MOZ_ASSERT(srcSurface->Stride() == srcSize.width * 4,
"Source surface stride isn't tightly packed");
// Only premultiply ARGB32
if (srcSurface->GetFormat() != SurfaceFormat::B8G8R8A8) {
if (destSurface != srcSurface) {
memcpy(destSurface->GetData(), srcSurface->GetData(),
srcSurface->Stride() * srcSize.height);
}
return;
}
uint8_t *src = srcSurface->GetData();
uint8_t *dst = destSurface->GetData();
uint32_t dim = srcSize.width * srcSize.height;
for (uint32_t i = 0; i < dim; ++i) {
#ifdef IS_LITTLE_ENDIAN
uint8_t b = *src++;
uint8_t g = *src++;
uint8_t r = *src++;
uint8_t a = *src++;
*dst++ = PremultiplyValue(a, b);
*dst++ = PremultiplyValue(a, g);
*dst++ = PremultiplyValue(a, r);
*dst++ = a;
#else
uint8_t a = *src++;
uint8_t r = *src++;
uint8_t g = *src++;
uint8_t b = *src++;
*dst++ = a;
*dst++ = PremultiplyValue(a, r);
*dst++ = PremultiplyValue(a, g);
*dst++ = PremultiplyValue(a, b);
#endif
}
}
}
}

21
gfx/layers/LayerUtils.h Normal file
View File

@ -0,0 +1,21 @@
/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#ifndef MOZILLA_LAYERS_LAYERUTILS_H_
#define MOZILLA_LAYERS_LAYERUTILS_H_
#include "mozilla/gfx/2D.h"
namespace mozilla {
namespace layers {
void
PremultiplySurface(gfx::DataSourceSurface* srcSurface,
gfx::DataSourceSurface* destSurface = nullptr);
}
}
#endif /* MOZILLA_LAYERS_LAYERUTILS_H_ */

View File

@ -13,3 +13,6 @@ CXXFLAGS += \
$(NULL)
CXXFLAGS += $(MOZ_CAIRO_CFLAGS) $(TK_CFLAGS)
PremultiplyTables.h: $(srcdir)/genTables.py
$(PYTHON) $(srcdir)/genTables.py

12
gfx/layers/genTables.py Normal file
View File

@ -0,0 +1,12 @@
#!/usr/bin/python
def table_generator(f):
return ",\n".join([", ".join(["0x%2.2x" % h for h in [f(i) for i in range(r,r+16)]]) for r in range(0, 65536, 16)])
with open("PremultiplyTables.h", "w") as f:
f.write("const uint8_t PremultiplyTable[256*256] = {\n");
f.write(table_generator(lambda i: ((i / 256) * (i % 256) + 254) / 255) + "\n")
f.write("};\n");
f.write("const uint8_t UnpremultiplyTable[256*256] = {\n");
f.write(table_generator(lambda i: (i % 256) * 255 / ((i / 256) if (i / 256) > 0 else 255) % 256) + "\n")
f.write("};\n");

View File

@ -261,6 +261,7 @@ UNIFIED_SOURCES += [
'LayerScope.cpp',
'LayersLogging.cpp',
'LayerSorter.cpp',
'LayerUtils.cpp',
'opengl/CompositingRenderTargetOGL.cpp',
'opengl/CompositorOGL.cpp',
'opengl/OGLShaderProgram.cpp',
@ -324,3 +325,7 @@ if CONFIG['MOZ_DEBUG']:
if CONFIG['MOZ_ENABLE_D3D10_LAYER']:
DEFINES['MOZ_ENABLE_D3D10_LAYER'] = True
GENERATED_FILES = [
'PremultiplyTables.h',
]

View File

@ -41,5 +41,5 @@ gfxAlphaRecoverySSE2.$(OBJ_SUFFIX): OS_CXXFLAGS += -xarch=sse2 -xO4
endif
endif
PremultiplyTables.h: $(srcdir)/genTables.py
DeprecatedPremultiplyTables.h: $(srcdir)/genTables.py
$(PYTHON) $(srcdir)/genTables.py

View File

@ -3,7 +3,7 @@
def table_generator(f):
return ",\n".join([", ".join(["0x%2.2x" % h for h in [f(i) for i in range(r,r+16)]]) for r in range(0, 65536, 16)])
with open("PremultiplyTables.h", "w") as f:
with open("DeprecatedPremultiplyTables.h", "w") as f:
f.write("const uint8_t gfxUtils::sPremultiplyTable[256*256] = {\n");
f.write(table_generator(lambda i: ((i / 256) * (i % 256) + 254) / 255) + "\n")
f.write("};\n");

View File

@ -22,7 +22,7 @@ using namespace mozilla;
using namespace mozilla::layers;
using namespace mozilla::gfx;
#include "PremultiplyTables.h"
#include "DeprecatedPremultiplyTables.h"
static const uint8_t PremultiplyValue(uint8_t a, uint8_t v) {
return gfxUtils::sPremultiplyTable[a*256+v];

View File

@ -268,7 +268,7 @@ include('/ipc/chromium/chromium-config.mozbuild')
FINAL_LIBRARY = 'xul'
GENERATED_FILES = [
'PremultiplyTables.h',
'DeprecatedPremultiplyTables.h',
]
LOCAL_INCLUDES += [