gecko/gfx/2d/DataSurfaceHelpers.h
Jonathan Watt 096699f593 Bug 980472 - Make SurfaceToPackedBGRA take a DataSourceSurface to avoid unnecessary surface conversion. r=Bas
--HG--
extra : rebase_source : c11c7ead8897255b3012495cc66beed27142275a
2014-03-06 23:31:33 +00:00

76 lines
2.0 KiB
C++

/* -*- Mode: C++; tab-width: 20; 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/. */
#pragma once
#include "2D.h"
namespace mozilla {
namespace gfx {
static inline void
ConvertBGRXToBGRA(uint8_t* aData, const IntSize &aSize, int32_t aStride)
{
uint32_t* pixel = reinterpret_cast<uint32_t*>(aData);
for (int row = 0; row < aSize.height; ++row) {
for (int column = 0; column < aSize.width; ++column) {
#ifdef IS_BIG_ENDIAN
pixel[column] |= 0x000000FF;
#else
pixel[column] |= 0xFF000000;
#endif
}
pixel += (aStride/4);
}
}
/**
* Convert aSurface to a packed buffer in BGRA format. The pixel data is
* returned in a buffer allocated with new uint8_t[].
*/
inline uint8_t *
SurfaceToPackedBGRA(DataSourceSurface *aSurface)
{
SurfaceFormat format = aSurface->GetFormat();
if (format != SurfaceFormat::B8G8R8A8 && format != SurfaceFormat::B8G8R8X8) {
return nullptr;
}
IntSize size = aSurface->GetSize();
uint8_t* imageBuffer = new (std::nothrow) uint8_t[size.width * size.height * sizeof(uint32_t)];
if (!imageBuffer) {
return nullptr;
}
size_t stride = aSurface->Stride();
uint32_t* src = reinterpret_cast<uint32_t*>(aSurface->GetData());
uint32_t* dst = reinterpret_cast<uint32_t*>(imageBuffer);
if (stride == size.width * sizeof(uint32_t)) {
// DataSourceSurface is already packed. We can use memcpy.
memcpy(dst, src, size.width * size.height * sizeof(uint32_t));
} else {
for (int row = 0; row < size.height; ++row) {
for (int column = 0; column < size.width; ++column) {
*dst++ = src[column];
}
src += (stride/4);
}
}
if (format == SurfaceFormat::B8G8R8X8) {
// Convert BGRX to BGRA by setting a to 255.
ConvertBGRXToBGRA(reinterpret_cast<uint8_t *>(imageBuffer), size, size.width * sizeof(uint32_t));
}
return imageBuffer;
}
}
}