Bug 933030 - Eliminate thebes use from CanvasRenderingContext2D.cpp r=mattwoodrow

This commit is contained in:
Andreas Gal 2013-11-03 08:28:30 -08:00
parent 001089b93a
commit 6f6a8a6867
6 changed files with 92 additions and 40 deletions

View File

@ -75,6 +75,7 @@
#include "mozilla/dom/TypedArray.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/PathHelpers.h"
#include "mozilla/gfx/DataSurfaceHelpers.h"
#include "mozilla/ipc/DocumentRendererParent.h"
#include "mozilla/ipc/PDocumentRendererParent.h"
#include "mozilla/MathAlgorithms.h"
@ -1060,40 +1061,20 @@ CanvasRenderingContext2D::GetImageBuffer(uint8_t** aImageBuffer,
*aImageBuffer = nullptr;
*aFormat = 0;
nsRefPtr<gfxASurface> surface;
nsresult rv = GetThebesSurface(getter_AddRefs(surface));
if (NS_FAILED(rv)) {
EnsureTarget();
RefPtr<SourceSurface> snapshot = mTarget->Snapshot();
if (!snapshot) {
return;
}
static const fallible_t fallible = fallible_t();
uint8_t* imageBuffer = new (fallible) uint8_t[mWidth * mHeight * 4];
if (!imageBuffer) {
RefPtr<DataSourceSurface> data = snapshot->GetDataSurface();
if (!data) {
return;
}
nsRefPtr<gfxImageSurface> imgsurf =
new gfxImageSurface(imageBuffer,
gfxIntSize(mWidth, mHeight),
mWidth * 4,
gfxImageFormatARGB32);
MOZ_ASSERT(data->GetSize() == IntSize(mWidth, mHeight));
if (!imgsurf || imgsurf->CairoStatus()) {
delete[] imageBuffer;
return;
}
nsRefPtr<gfxContext> ctx = new gfxContext(imgsurf);
if (!ctx || ctx->HasError()) {
delete[] imageBuffer;
return;
}
ctx->SetOperator(gfxContext::OPERATOR_SOURCE);
ctx->SetSource(surface, gfxPoint(0, 0));
ctx->Paint();
*aImageBuffer = imageBuffer;
*aImageBuffer = SurfaceToPackedBGRA(data);
*aFormat = imgIEncoder::INPUT_FORMAT_HOSTARGB;
}

View File

@ -0,0 +1,81 @@
/* -*- 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"
#include "mozilla/mozalloc.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(SourceSurface *aSurface)
{
RefPtr<DataSourceSurface> data = aSurface->GetDataSurface();
if (!data) {
return nullptr;
}
SurfaceFormat format = data->GetFormat();
if (format != FORMAT_B8G8R8A8 && format != FORMAT_B8G8R8X8) {
return nullptr;
}
IntSize size = data->GetSize();
static const fallible_t fallible = fallible_t();
uint8_t* imageBuffer = new (fallible) uint8_t[size.width * size.height * sizeof(uint32_t)];
if (!imageBuffer) {
return nullptr;
}
size_t stride = data->Stride();
uint32_t* src = reinterpret_cast<uint32_t*>(data->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 == FORMAT_B8G8R8X8) {
// Convert BGRX to BGRA by setting a to 255.
ConvertBGRXToBGRA(reinterpret_cast<uint8_t *>(imageBuffer), size, size.width * sizeof(uint32_t));
}
return imageBuffer;
}
}
}

View File

@ -25,6 +25,7 @@
#include "Logging.h"
#include "HelpersSkia.h"
#include "Tools.h"
#include "DataSurfaceHelpers.h"
#include <algorithm>
namespace mozilla {

View File

@ -154,19 +154,6 @@ StrokeOptionsToPaint(SkPaint& aPaint, const StrokeOptions &aOptions)
return true;
}
static inline void
ConvertBGRXToBGRA(unsigned char* 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) {
pixel[column] |= 0xFF000000;
}
pixel += (aStride/4);
}
}
static inline SkXfermode::Mode
GfxOpToSkiaOp(CompositionOp op)
{

View File

@ -10,6 +10,7 @@
#include "skia/SkDevice.h"
#include "HelpersSkia.h"
#include "DrawTargetSkia.h"
#include "DataSurfaceHelpers.h"
namespace mozilla {
namespace gfx {

View File

@ -20,6 +20,7 @@ EXPORTS.mozilla.gfx += [
'BaseSize.h',
'Blur.h',
'BorrowedContext.h',
'DataSurfaceHelpers.h',
'Matrix.h',
'PathHelpers.h',
'Point.h',