2010-05-17 21:04:22 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* 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/. */
|
2010-05-17 21:04:22 -07:00
|
|
|
|
|
|
|
#include "gfxUtils.h"
|
2014-06-23 15:48:58 -07:00
|
|
|
|
|
|
|
#include "cairo.h"
|
2010-08-13 06:30:02 -07:00
|
|
|
#include "gfxContext.h"
|
2014-06-14 17:34:01 -07:00
|
|
|
#include "gfxImageSurface.h"
|
2010-08-13 06:30:02 -07:00
|
|
|
#include "gfxPlatform.h"
|
2010-08-13 06:30:27 -07:00
|
|
|
#include "gfxDrawable.h"
|
2014-06-27 04:19:40 -07:00
|
|
|
#include "imgIEncoder.h"
|
|
|
|
#include "mozilla/Base64.h"
|
2014-04-08 02:05:39 -07:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2014-06-27 04:19:40 -07:00
|
|
|
#include "mozilla/gfx/DataSurfaceHelpers.h"
|
2014-04-08 02:05:39 -07:00
|
|
|
#include "mozilla/RefPtr.h"
|
2014-06-27 04:19:40 -07:00
|
|
|
#include "mozilla/Vector.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsIClipboardHelper.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsPresContext.h"
|
2010-09-02 19:31:42 -07:00
|
|
|
#include "nsRegion.h"
|
2014-06-27 04:19:40 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2011-09-27 15:19:24 -07:00
|
|
|
#include "yuv_convert.h"
|
|
|
|
#include "ycbcr_to_rgb565.h"
|
2013-03-18 07:25:50 -07:00
|
|
|
#include "GeckoProfiler.h"
|
2013-10-01 17:57:50 -07:00
|
|
|
#include "ImageContainer.h"
|
2013-11-12 20:32:48 -08:00
|
|
|
#include "gfx2DGlue.h"
|
2014-05-03 17:35:47 -07:00
|
|
|
#include "gfxPrefs.h"
|
2010-08-13 06:30:02 -07:00
|
|
|
|
2011-06-18 02:04:30 -07:00
|
|
|
#ifdef XP_WIN
|
2010-08-13 06:30:02 -07:00
|
|
|
#include "gfxWindowsPlatform.h"
|
|
|
|
#endif
|
2010-05-17 21:04:22 -07:00
|
|
|
|
2011-09-27 15:19:24 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::layers;
|
2011-11-16 19:44:16 -08:00
|
|
|
using namespace mozilla::gfx;
|
2011-09-27 15:19:24 -07:00
|
|
|
|
2014-02-12 07:07:46 -08:00
|
|
|
#include "DeprecatedPremultiplyTables.h"
|
2010-05-17 21:04:22 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static const uint8_t PremultiplyValue(uint8_t a, uint8_t v) {
|
2012-12-16 16:20:01 -08:00
|
|
|
return gfxUtils::sPremultiplyTable[a*256+v];
|
2010-05-17 21:04:22 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static const uint8_t UnpremultiplyValue(uint8_t a, uint8_t v) {
|
2012-12-16 16:20:01 -08:00
|
|
|
return gfxUtils::sUnpremultiplyTable[a*256+v];
|
2010-05-17 21:04:22 -07:00
|
|
|
}
|
|
|
|
|
2014-06-18 17:04:05 -07:00
|
|
|
static void
|
|
|
|
PremultiplyData(const uint8_t* srcData,
|
|
|
|
size_t srcStride, // row-to-row stride in bytes
|
|
|
|
uint8_t* destData,
|
|
|
|
size_t destStride, // row-to-row stride in bytes
|
|
|
|
size_t pixelWidth,
|
|
|
|
size_t rowCount)
|
2014-05-12 19:20:26 -07:00
|
|
|
{
|
2014-06-18 17:04:05 -07:00
|
|
|
MOZ_ASSERT(srcData && destData);
|
|
|
|
|
|
|
|
for (size_t y = 0; y < rowCount; ++y) {
|
|
|
|
const uint8_t* src = srcData + y * srcStride;
|
|
|
|
uint8_t* dest = destData + y * destStride;
|
|
|
|
|
|
|
|
for (size_t x = 0; x < pixelWidth; ++x) {
|
|
|
|
#ifdef IS_LITTLE_ENDIAN
|
|
|
|
uint8_t b = *src++;
|
|
|
|
uint8_t g = *src++;
|
|
|
|
uint8_t r = *src++;
|
|
|
|
uint8_t a = *src++;
|
2014-06-04 15:20:24 -07:00
|
|
|
|
2014-06-18 17:04:05 -07:00
|
|
|
*dest++ = PremultiplyValue(a, b);
|
|
|
|
*dest++ = PremultiplyValue(a, g);
|
|
|
|
*dest++ = PremultiplyValue(a, r);
|
|
|
|
*dest++ = a;
|
|
|
|
#else
|
|
|
|
uint8_t a = *src++;
|
|
|
|
uint8_t r = *src++;
|
|
|
|
uint8_t g = *src++;
|
|
|
|
uint8_t b = *src++;
|
|
|
|
|
|
|
|
*dest++ = a;
|
|
|
|
*dest++ = PremultiplyValue(a, r);
|
|
|
|
*dest++ = PremultiplyValue(a, g);
|
|
|
|
*dest++ = PremultiplyValue(a, b);
|
|
|
|
#endif
|
|
|
|
}
|
2014-05-12 19:20:26 -07:00
|
|
|
}
|
2014-06-18 17:04:05 -07:00
|
|
|
}
|
|
|
|
static void
|
|
|
|
UnpremultiplyData(const uint8_t* srcData,
|
|
|
|
size_t srcStride, // row-to-row stride in bytes
|
|
|
|
uint8_t* destData,
|
|
|
|
size_t destStride, // row-to-row stride in bytes
|
|
|
|
size_t pixelWidth,
|
|
|
|
size_t rowCount)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(srcData && destData);
|
2014-05-12 19:20:26 -07:00
|
|
|
|
2014-06-18 17:04:05 -07:00
|
|
|
for (size_t y = 0; y < rowCount; ++y) {
|
|
|
|
const uint8_t* src = srcData + y * srcStride;
|
|
|
|
uint8_t* dest = destData + y * destStride;
|
2014-05-12 19:20:26 -07:00
|
|
|
|
2014-06-18 17:04:05 -07:00
|
|
|
for (size_t x = 0; x < pixelWidth; ++x) {
|
2014-05-12 19:20:26 -07:00
|
|
|
#ifdef IS_LITTLE_ENDIAN
|
2014-06-18 17:04:05 -07:00
|
|
|
uint8_t b = *src++;
|
|
|
|
uint8_t g = *src++;
|
|
|
|
uint8_t r = *src++;
|
|
|
|
uint8_t a = *src++;
|
|
|
|
|
|
|
|
*dest++ = UnpremultiplyValue(a, b);
|
|
|
|
*dest++ = UnpremultiplyValue(a, g);
|
|
|
|
*dest++ = UnpremultiplyValue(a, r);
|
|
|
|
*dest++ = a;
|
2014-05-12 19:20:26 -07:00
|
|
|
#else
|
2014-06-18 17:04:05 -07:00
|
|
|
uint8_t a = *src++;
|
|
|
|
uint8_t r = *src++;
|
|
|
|
uint8_t g = *src++;
|
|
|
|
uint8_t b = *src++;
|
|
|
|
|
|
|
|
*dest++ = a;
|
|
|
|
*dest++ = UnpremultiplyValue(a, r);
|
|
|
|
*dest++ = UnpremultiplyValue(a, g);
|
|
|
|
*dest++ = UnpremultiplyValue(a, b);
|
2014-05-12 19:20:26 -07:00
|
|
|
#endif
|
2014-06-18 17:04:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
MapSrcDest(DataSourceSurface* srcSurf,
|
|
|
|
DataSourceSurface* destSurf,
|
|
|
|
DataSourceSurface::MappedSurface* out_srcMap,
|
|
|
|
DataSourceSurface::MappedSurface* out_destMap)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(srcSurf && destSurf);
|
|
|
|
MOZ_ASSERT(out_srcMap && out_destMap);
|
|
|
|
|
|
|
|
if (srcSurf->GetFormat() != SurfaceFormat::B8G8R8A8 ||
|
|
|
|
destSurf->GetFormat() != SurfaceFormat::B8G8R8A8)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(false, "Only operate on BGRA8 surfs.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (srcSurf->GetSize().width != destSurf->GetSize().width ||
|
|
|
|
srcSurf->GetSize().height != destSurf->GetSize().height)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(false, "Width and height must match.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (srcSurf == destSurf) {
|
|
|
|
DataSourceSurface::MappedSurface map;
|
|
|
|
if (!srcSurf->Map(DataSourceSurface::MapType::READ_WRITE, &map)) {
|
|
|
|
NS_WARNING("Couldn't Map srcSurf/destSurf.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_srcMap = map;
|
|
|
|
*out_destMap = map;
|
|
|
|
return true;
|
2014-06-04 15:20:24 -07:00
|
|
|
}
|
|
|
|
|
2014-06-18 17:04:05 -07:00
|
|
|
// Map src for reading.
|
|
|
|
DataSourceSurface::MappedSurface srcMap;
|
|
|
|
if (!srcSurf->Map(DataSourceSurface::MapType::READ, &srcMap)) {
|
|
|
|
NS_WARNING("Couldn't Map srcSurf.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map dest for writing.
|
|
|
|
DataSourceSurface::MappedSurface destMap;
|
|
|
|
if (!destSurf->Map(DataSourceSurface::MapType::WRITE, &destMap)) {
|
|
|
|
NS_WARNING("Couldn't Map aDest.");
|
|
|
|
srcSurf->Unmap();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_srcMap = srcMap;
|
|
|
|
*out_destMap = destMap;
|
|
|
|
return true;
|
2014-06-04 15:20:24 -07:00
|
|
|
}
|
|
|
|
|
2014-06-18 17:04:05 -07:00
|
|
|
static void
|
|
|
|
UnmapSrcDest(DataSourceSurface* srcSurf,
|
|
|
|
DataSourceSurface* destSurf)
|
|
|
|
{
|
|
|
|
if (srcSurf == destSurf) {
|
|
|
|
srcSurf->Unmap();
|
|
|
|
} else {
|
|
|
|
srcSurf->Unmap();
|
|
|
|
destSurf->Unmap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
gfxUtils::PremultiplyDataSurface(DataSourceSurface* srcSurf,
|
|
|
|
DataSourceSurface* destSurf)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(srcSurf && destSurf);
|
|
|
|
|
|
|
|
DataSourceSurface::MappedSurface srcMap;
|
|
|
|
DataSourceSurface::MappedSurface destMap;
|
|
|
|
if (!MapSrcDest(srcSurf, destSurf, &srcMap, &destMap))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
PremultiplyData(srcMap.mData, srcMap.mStride,
|
|
|
|
destMap.mData, destMap.mStride,
|
|
|
|
srcSurf->GetSize().width,
|
|
|
|
srcSurf->GetSize().height);
|
|
|
|
|
|
|
|
UnmapSrcDest(srcSurf, destSurf);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
gfxUtils::UnpremultiplyDataSurface(DataSourceSurface* srcSurf,
|
|
|
|
DataSourceSurface* destSurf)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(srcSurf && destSurf);
|
|
|
|
|
|
|
|
DataSourceSurface::MappedSurface srcMap;
|
|
|
|
DataSourceSurface::MappedSurface destMap;
|
|
|
|
if (!MapSrcDest(srcSurf, destSurf, &srcMap, &destMap))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
UnpremultiplyData(srcMap.mData, srcMap.mStride,
|
|
|
|
destMap.mData, destMap.mStride,
|
|
|
|
srcSurf->GetSize().width,
|
|
|
|
srcSurf->GetSize().height);
|
|
|
|
|
|
|
|
UnmapSrcDest(srcSurf, destSurf);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
MapSrcAndCreateMappedDest(DataSourceSurface* srcSurf,
|
|
|
|
RefPtr<DataSourceSurface>* out_destSurf,
|
|
|
|
DataSourceSurface::MappedSurface* out_srcMap,
|
|
|
|
DataSourceSurface::MappedSurface* out_destMap)
|
2014-04-16 22:29:52 -07:00
|
|
|
{
|
2014-06-18 17:04:05 -07:00
|
|
|
MOZ_ASSERT(srcSurf);
|
|
|
|
MOZ_ASSERT(out_destSurf && out_srcMap && out_destMap);
|
|
|
|
|
|
|
|
if (srcSurf->GetFormat() != SurfaceFormat::B8G8R8A8) {
|
|
|
|
MOZ_ASSERT(false, "Only operate on BGRA8.");
|
|
|
|
return false;
|
2014-04-16 22:29:52 -07:00
|
|
|
}
|
|
|
|
|
2014-06-18 17:04:05 -07:00
|
|
|
// Ok, map source for reading.
|
|
|
|
DataSourceSurface::MappedSurface srcMap;
|
|
|
|
if (!srcSurf->Map(DataSourceSurface::MapType::READ, &srcMap)) {
|
|
|
|
MOZ_ASSERT(false, "Couldn't Map srcSurf.");
|
|
|
|
return false;
|
2014-04-16 22:29:52 -07:00
|
|
|
}
|
|
|
|
|
2014-06-18 17:04:05 -07:00
|
|
|
// Make our dest surface based on the src.
|
|
|
|
RefPtr<DataSourceSurface> destSurf =
|
|
|
|
Factory::CreateDataSourceSurfaceWithStride(srcSurf->GetSize(),
|
|
|
|
srcSurf->GetFormat(),
|
|
|
|
srcMap.mStride);
|
2014-04-16 22:29:52 -07:00
|
|
|
|
|
|
|
DataSourceSurface::MappedSurface destMap;
|
2014-06-18 17:04:05 -07:00
|
|
|
if (!destSurf->Map(DataSourceSurface::MapType::WRITE, &destMap)) {
|
|
|
|
MOZ_ASSERT(false, "Couldn't Map destSurf.");
|
|
|
|
srcSurf->Unmap();
|
|
|
|
return false;
|
2014-04-16 22:29:52 -07:00
|
|
|
}
|
|
|
|
|
2014-06-18 17:04:05 -07:00
|
|
|
*out_destSurf = destSurf;
|
|
|
|
*out_srcMap = srcMap;
|
|
|
|
*out_destMap = destMap;
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-16 22:29:52 -07:00
|
|
|
|
2014-06-18 17:04:05 -07:00
|
|
|
TemporaryRef<DataSourceSurface>
|
|
|
|
gfxUtils::CreatePremultipliedDataSurface(DataSourceSurface* srcSurf)
|
|
|
|
{
|
|
|
|
RefPtr<DataSourceSurface> destSurf;
|
|
|
|
DataSourceSurface::MappedSurface srcMap;
|
|
|
|
DataSourceSurface::MappedSurface destMap;
|
|
|
|
if (!MapSrcAndCreateMappedDest(srcSurf, &destSurf, &srcMap, &destMap)) {
|
|
|
|
MOZ_ASSERT(false, "MapSrcAndCreateMappedDest failed.");
|
|
|
|
return srcSurf;
|
|
|
|
}
|
2014-06-04 15:20:24 -07:00
|
|
|
|
2014-06-18 17:04:05 -07:00
|
|
|
PremultiplyData(srcMap.mData, srcMap.mStride,
|
|
|
|
destMap.mData, destMap.mStride,
|
|
|
|
srcSurf->GetSize().width,
|
|
|
|
srcSurf->GetSize().height);
|
|
|
|
|
|
|
|
UnmapSrcDest(srcSurf, destSurf);
|
|
|
|
return destSurf;
|
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryRef<DataSourceSurface>
|
|
|
|
gfxUtils::CreateUnpremultipliedDataSurface(DataSourceSurface* srcSurf)
|
|
|
|
{
|
|
|
|
RefPtr<DataSourceSurface> destSurf;
|
|
|
|
DataSourceSurface::MappedSurface srcMap;
|
|
|
|
DataSourceSurface::MappedSurface destMap;
|
|
|
|
if (!MapSrcAndCreateMappedDest(srcSurf, &destSurf, &srcMap, &destMap)) {
|
|
|
|
MOZ_ASSERT(false, "MapSrcAndCreateMappedDest failed.");
|
|
|
|
return srcSurf;
|
2014-04-16 22:29:52 -07:00
|
|
|
}
|
|
|
|
|
2014-06-18 17:04:05 -07:00
|
|
|
UnpremultiplyData(srcMap.mData, srcMap.mStride,
|
|
|
|
destMap.mData, destMap.mStride,
|
|
|
|
srcSurf->GetSize().width,
|
|
|
|
srcSurf->GetSize().height);
|
|
|
|
|
|
|
|
UnmapSrcDest(srcSurf, destSurf);
|
|
|
|
return destSurf;
|
2014-04-16 22:29:52 -07:00
|
|
|
}
|
|
|
|
|
2014-04-16 22:31:44 -07:00
|
|
|
void
|
|
|
|
gfxUtils::ConvertBGRAtoRGBA(uint8_t* aData, uint32_t aLength)
|
|
|
|
{
|
2014-06-03 04:31:42 -07:00
|
|
|
MOZ_ASSERT((aLength % 4) == 0, "Loop below will pass srcEnd!");
|
|
|
|
|
2014-04-16 22:31:44 -07:00
|
|
|
uint8_t *src = aData;
|
|
|
|
uint8_t *srcEnd = src + aLength;
|
|
|
|
|
|
|
|
uint8_t buffer[4];
|
|
|
|
for (; src != srcEnd; src += 4) {
|
|
|
|
buffer[0] = src[2];
|
|
|
|
buffer[1] = src[1];
|
|
|
|
buffer[2] = src[0];
|
|
|
|
|
|
|
|
src[0] = buffer[0];
|
|
|
|
src[1] = buffer[1];
|
|
|
|
src[2] = buffer[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2010-08-13 06:30:02 -07:00
|
|
|
IsSafeImageTransformComponent(gfxFloat aValue)
|
|
|
|
{
|
|
|
|
return aValue >= -32768 && aValue <= 32767;
|
|
|
|
}
|
|
|
|
|
2013-10-01 18:22:44 -07:00
|
|
|
#ifndef MOZ_GFX_OPTIMIZE_MOBILE
|
2010-10-08 09:02:58 -07:00
|
|
|
/**
|
|
|
|
* This returns the fastest operator to use for solid surfaces which have no
|
|
|
|
* alpha channel or their alpha channel is uniformly opaque.
|
|
|
|
* This differs per render mode.
|
|
|
|
*/
|
|
|
|
static gfxContext::GraphicsOperator
|
|
|
|
OptimalFillOperator()
|
|
|
|
{
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (gfxWindowsPlatform::GetPlatform()->GetRenderMode() ==
|
|
|
|
gfxWindowsPlatform::RENDER_DIRECT2D) {
|
|
|
|
// D2D -really- hates operator source.
|
|
|
|
return gfxContext::OPERATOR_OVER;
|
|
|
|
} else {
|
|
|
|
#endif
|
|
|
|
return gfxContext::OPERATOR_SOURCE;
|
|
|
|
#ifdef XP_WIN
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-08-13 06:30:02 -07:00
|
|
|
// EXTEND_PAD won't help us here; we have to create a temporary surface to hold
|
|
|
|
// the subimage of pixels we're allowed to sample.
|
2010-08-13 06:30:27 -07:00
|
|
|
static already_AddRefed<gfxDrawable>
|
|
|
|
CreateSamplingRestrictedDrawable(gfxDrawable* aDrawable,
|
|
|
|
gfxContext* aContext,
|
|
|
|
const gfxMatrix& aUserSpaceToImageSpace,
|
|
|
|
const gfxRect& aSourceRect,
|
|
|
|
const gfxRect& aSubimage,
|
2014-04-19 18:28:38 -07:00
|
|
|
const SurfaceFormat aFormat)
|
2010-08-13 06:30:02 -07:00
|
|
|
{
|
2014-05-23 14:12:29 -07:00
|
|
|
PROFILER_LABEL("gfxUtils", "CreateSamplingRestricedDrawable",
|
|
|
|
js::ProfileEntry::Category::GRAPHICS);
|
|
|
|
|
2010-08-13 06:30:02 -07:00
|
|
|
gfxRect userSpaceClipExtents = aContext->GetClipExtents();
|
|
|
|
// This isn't optimal --- if aContext has a rotation then GetClipExtents
|
|
|
|
// will have to do a bounding-box computation, and TransformBounds might
|
|
|
|
// too, so we could get a better result if we computed image space clip
|
|
|
|
// extents in one go --- but it doesn't really matter and this is easier
|
|
|
|
// to understand.
|
|
|
|
gfxRect imageSpaceClipExtents =
|
|
|
|
aUserSpaceToImageSpace.TransformBounds(userSpaceClipExtents);
|
|
|
|
// Inflate by one pixel because bilinear filtering will sample at most
|
|
|
|
// one pixel beyond the computed image pixel coordinate.
|
2011-04-18 20:07:51 -07:00
|
|
|
imageSpaceClipExtents.Inflate(1.0);
|
2010-08-13 06:30:02 -07:00
|
|
|
|
|
|
|
gfxRect needed = imageSpaceClipExtents.Intersect(aSourceRect);
|
|
|
|
needed = needed.Intersect(aSubimage);
|
|
|
|
needed.RoundOut();
|
|
|
|
|
|
|
|
// if 'needed' is empty, nothing will be drawn since aFill
|
|
|
|
// must be entirely outside the clip region, so it doesn't
|
|
|
|
// matter what we do here, but we should avoid trying to
|
|
|
|
// create a zero-size surface.
|
|
|
|
if (needed.IsEmpty())
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-08-13 06:30:02 -07:00
|
|
|
|
2013-11-12 20:32:48 -08:00
|
|
|
nsRefPtr<gfxDrawable> drawable;
|
2012-08-22 08:56:38 -07:00
|
|
|
gfxIntSize size(int32_t(needed.Width()), int32_t(needed.Height()));
|
2010-08-13 06:30:02 -07:00
|
|
|
|
2013-09-18 22:23:31 -07:00
|
|
|
nsRefPtr<gfxImageSurface> image = aDrawable->GetAsImageSurface();
|
|
|
|
if (image && gfxRect(0, 0, image->GetSize().width, image->GetSize().height).Contains(needed)) {
|
2013-11-12 20:32:48 -08:00
|
|
|
nsRefPtr<gfxASurface> temp = image->GetSubimage(needed);
|
|
|
|
drawable = new gfxSurfaceDrawable(temp, size, gfxMatrix().Translate(-needed.TopLeft()));
|
2013-09-18 22:23:31 -07:00
|
|
|
} else {
|
2014-04-19 18:28:38 -07:00
|
|
|
RefPtr<DrawTarget> target =
|
2013-11-12 20:32:48 -08:00
|
|
|
gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(ToIntSize(size),
|
2014-04-19 18:28:38 -07:00
|
|
|
aFormat);
|
2013-11-12 20:32:48 -08:00
|
|
|
if (!target) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-09-18 22:23:31 -07:00
|
|
|
|
2013-11-12 20:32:48 -08:00
|
|
|
nsRefPtr<gfxContext> tmpCtx = new gfxContext(target);
|
2013-09-18 22:23:31 -07:00
|
|
|
tmpCtx->SetOperator(OptimalFillOperator());
|
|
|
|
aDrawable->Draw(tmpCtx, needed - needed.TopLeft(), true,
|
2013-10-01 14:01:19 -07:00
|
|
|
GraphicsFilter::FILTER_FAST, gfxMatrix().Translate(needed.TopLeft()));
|
2013-11-12 20:32:48 -08:00
|
|
|
drawable = new gfxSurfaceDrawable(target, size, gfxMatrix().Translate(-needed.TopLeft()));
|
2013-09-18 22:23:31 -07:00
|
|
|
}
|
2010-08-13 06:30:02 -07:00
|
|
|
|
2010-08-13 06:30:27 -07:00
|
|
|
return drawable.forget();
|
2010-08-13 06:30:02 -07:00
|
|
|
}
|
2013-01-21 13:54:42 -08:00
|
|
|
#endif // !MOZ_GFX_OPTIMIZE_MOBILE
|
2010-08-13 06:30:02 -07:00
|
|
|
|
|
|
|
// working around cairo/pixman bug (bug 364968)
|
|
|
|
// Our device-space-to-image-space transform may not be acceptable to pixman.
|
2013-04-11 20:20:37 -07:00
|
|
|
struct MOZ_STACK_CLASS AutoCairoPixmanBugWorkaround
|
2010-08-13 06:30:02 -07:00
|
|
|
{
|
|
|
|
AutoCairoPixmanBugWorkaround(gfxContext* aContext,
|
|
|
|
const gfxMatrix& aDeviceSpaceToImageSpace,
|
|
|
|
const gfxRect& aFill,
|
2012-01-04 23:17:51 -08:00
|
|
|
const gfxASurface* aSurface)
|
2011-10-17 07:59:28 -07:00
|
|
|
: mContext(aContext), mSucceeded(true), mPushedGroup(false)
|
2010-08-13 06:30:02 -07:00
|
|
|
{
|
|
|
|
// Quartz's limits for matrix are much larger than pixman
|
2014-01-23 10:26:40 -08:00
|
|
|
if (!aSurface || aSurface->GetType() == gfxSurfaceType::Quartz)
|
2010-08-13 06:30:02 -07:00
|
|
|
return;
|
|
|
|
|
2014-06-17 10:35:51 -07:00
|
|
|
if (!IsSafeImageTransformComponent(aDeviceSpaceToImageSpace._11) ||
|
|
|
|
!IsSafeImageTransformComponent(aDeviceSpaceToImageSpace._21) ||
|
|
|
|
!IsSafeImageTransformComponent(aDeviceSpaceToImageSpace._12) ||
|
|
|
|
!IsSafeImageTransformComponent(aDeviceSpaceToImageSpace._22)) {
|
2010-08-13 06:30:02 -07:00
|
|
|
NS_WARNING("Scaling up too much, bailing out");
|
2011-10-17 07:59:28 -07:00
|
|
|
mSucceeded = false;
|
2010-08-13 06:30:02 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-17 10:35:51 -07:00
|
|
|
if (IsSafeImageTransformComponent(aDeviceSpaceToImageSpace._31) &&
|
|
|
|
IsSafeImageTransformComponent(aDeviceSpaceToImageSpace._32))
|
2010-08-13 06:30:02 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
// We'll push a group, which will hopefully reduce our transform's
|
|
|
|
// translation so it's in bounds.
|
|
|
|
gfxMatrix currentMatrix = mContext->CurrentMatrix();
|
|
|
|
mContext->Save();
|
|
|
|
|
|
|
|
// Clip the rounded-out-to-device-pixels bounds of the
|
|
|
|
// transformed fill area. This is the area for the group we
|
|
|
|
// want to push.
|
|
|
|
mContext->IdentityMatrix();
|
|
|
|
gfxRect bounds = currentMatrix.TransformBounds(aFill);
|
|
|
|
bounds.RoundOut();
|
|
|
|
mContext->Clip(bounds);
|
|
|
|
mContext->SetMatrix(currentMatrix);
|
2014-01-23 10:26:40 -08:00
|
|
|
mContext->PushGroup(gfxContentType::COLOR_ALPHA);
|
2010-08-13 06:30:02 -07:00
|
|
|
mContext->SetOperator(gfxContext::OPERATOR_OVER);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mPushedGroup = true;
|
2010-08-13 06:30:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
~AutoCairoPixmanBugWorkaround()
|
|
|
|
{
|
|
|
|
if (mPushedGroup) {
|
|
|
|
mContext->PopGroupToSource();
|
|
|
|
mContext->Paint();
|
|
|
|
mContext->Restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool PushedGroup() { return mPushedGroup; }
|
|
|
|
bool Succeeded() { return mSucceeded; }
|
2010-08-13 06:30:02 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
gfxContext* mContext;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mSucceeded;
|
|
|
|
bool mPushedGroup;
|
2010-08-13 06:30:02 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static gfxMatrix
|
|
|
|
DeviceToImageTransform(gfxContext* aContext,
|
|
|
|
const gfxMatrix& aUserSpaceToImageSpace)
|
|
|
|
{
|
|
|
|
gfxFloat deviceX, deviceY;
|
|
|
|
nsRefPtr<gfxASurface> currentTarget =
|
|
|
|
aContext->CurrentSurface(&deviceX, &deviceY);
|
|
|
|
gfxMatrix currentMatrix = aContext->CurrentMatrix();
|
|
|
|
gfxMatrix deviceToUser = gfxMatrix(currentMatrix).Invert();
|
|
|
|
deviceToUser.Translate(-gfxPoint(-deviceX, -deviceY));
|
|
|
|
return gfxMatrix(deviceToUser).Multiply(aUserSpaceToImageSpace);
|
|
|
|
}
|
|
|
|
|
2012-05-01 13:35:04 -07:00
|
|
|
/* These heuristics are based on Source/WebCore/platform/graphics/skia/ImageSkia.cpp:computeResamplingMode() */
|
|
|
|
#ifdef MOZ_GFX_OPTIMIZE_MOBILE
|
2013-10-01 14:01:19 -07:00
|
|
|
static GraphicsFilter ReduceResamplingFilter(GraphicsFilter aFilter,
|
|
|
|
int aImgWidth, int aImgHeight,
|
|
|
|
float aSourceWidth, float aSourceHeight)
|
2012-05-01 13:35:04 -07:00
|
|
|
{
|
|
|
|
// Images smaller than this in either direction are considered "small" and
|
|
|
|
// are not resampled ever (see below).
|
|
|
|
const int kSmallImageSizeThreshold = 8;
|
|
|
|
|
|
|
|
// The amount an image can be stretched in a single direction before we
|
|
|
|
// say that it is being stretched so much that it must be a line or
|
|
|
|
// background that doesn't need resampling.
|
|
|
|
const float kLargeStretch = 3.0f;
|
|
|
|
|
|
|
|
if (aImgWidth <= kSmallImageSizeThreshold
|
|
|
|
|| aImgHeight <= kSmallImageSizeThreshold) {
|
|
|
|
// Never resample small images. These are often used for borders and
|
|
|
|
// rules (think 1x1 images used to make lines).
|
2013-10-01 14:01:19 -07:00
|
|
|
return GraphicsFilter::FILTER_NEAREST;
|
2012-05-01 13:35:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aImgHeight * kLargeStretch <= aSourceHeight || aImgWidth * kLargeStretch <= aSourceWidth) {
|
|
|
|
// Large image tiling detected.
|
|
|
|
|
|
|
|
// Don't resample if it is being tiled a lot in only one direction.
|
|
|
|
// This is trying to catch cases where somebody has created a border
|
|
|
|
// (which might be large) and then is stretching it to fill some part
|
|
|
|
// of the page.
|
|
|
|
if (fabs(aSourceWidth - aImgWidth)/aImgWidth < 0.5 || fabs(aSourceHeight - aImgHeight)/aImgHeight < 0.5)
|
2013-10-01 14:01:19 -07:00
|
|
|
return GraphicsFilter::FILTER_NEAREST;
|
2012-05-01 13:35:04 -07:00
|
|
|
|
|
|
|
// The image is growing a lot and in more than one direction. Resampling
|
|
|
|
// is slow and doesn't give us very much when growing a lot.
|
|
|
|
return aFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Some notes on other heuristics:
|
|
|
|
The Skia backend also uses nearest for backgrounds that are stretched by
|
|
|
|
a large amount. I'm not sure this is common enough for us to worry about
|
|
|
|
now. It also uses nearest for backgrounds/avoids high quality for images
|
|
|
|
that are very slightly scaled. I'm also not sure that very slightly
|
|
|
|
scaled backgrounds are common enough us to worry about.
|
|
|
|
|
|
|
|
We don't currently have much support for doing high quality interpolation.
|
|
|
|
The only place this currently happens is on Quartz and we don't have as
|
|
|
|
much control over it as would be needed. Webkit avoids using high quality
|
|
|
|
resampling during load. It also avoids high quality if the transformation
|
|
|
|
is not just a scale and translation
|
|
|
|
|
|
|
|
WebKit bug #40045 added code to avoid resampling different parts
|
|
|
|
of an image with different methods by using a resampling hint size.
|
|
|
|
It currently looks unused in WebKit but it's something to watch out for.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return aFilter;
|
|
|
|
}
|
|
|
|
#else
|
2013-10-01 14:01:19 -07:00
|
|
|
static GraphicsFilter ReduceResamplingFilter(GraphicsFilter aFilter,
|
|
|
|
int aImgWidth, int aImgHeight,
|
|
|
|
int aSourceWidth, int aSourceHeight)
|
2012-05-01 13:35:04 -07:00
|
|
|
{
|
|
|
|
// Just pass the filter through unchanged
|
|
|
|
return aFilter;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-13 06:30:02 -07:00
|
|
|
/* static */ void
|
|
|
|
gfxUtils::DrawPixelSnapped(gfxContext* aContext,
|
2010-08-13 06:30:27 -07:00
|
|
|
gfxDrawable* aDrawable,
|
2010-08-13 06:30:02 -07:00
|
|
|
const gfxMatrix& aUserSpaceToImageSpace,
|
|
|
|
const gfxRect& aSubimage,
|
|
|
|
const gfxRect& aSourceRect,
|
|
|
|
const gfxRect& aImageRect,
|
|
|
|
const gfxRect& aFill,
|
2014-04-19 18:28:38 -07:00
|
|
|
const SurfaceFormat aFormat,
|
2013-10-01 14:01:19 -07:00
|
|
|
GraphicsFilter aFilter,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aImageFlags)
|
2010-08-13 06:30:02 -07:00
|
|
|
{
|
2014-05-23 14:12:29 -07:00
|
|
|
PROFILER_LABEL("gfxUtils", "DrawPixelSnapped",
|
|
|
|
js::ProfileEntry::Category::GRAPHICS);
|
|
|
|
|
2012-04-17 15:04:15 -07:00
|
|
|
bool doTile = !aImageRect.Contains(aSourceRect) &&
|
|
|
|
!(aImageFlags & imgIContainer::FLAG_CLAMP);
|
2010-08-13 06:30:02 -07:00
|
|
|
|
|
|
|
nsRefPtr<gfxASurface> currentTarget = aContext->CurrentSurface();
|
|
|
|
gfxMatrix deviceSpaceToImageSpace =
|
|
|
|
DeviceToImageTransform(aContext, aUserSpaceToImageSpace);
|
|
|
|
|
|
|
|
AutoCairoPixmanBugWorkaround workaround(aContext, deviceSpaceToImageSpace,
|
2012-01-04 23:17:51 -08:00
|
|
|
aFill, currentTarget);
|
2010-08-13 06:30:02 -07:00
|
|
|
if (!workaround.Succeeded())
|
|
|
|
return;
|
|
|
|
|
2010-08-13 06:30:27 -07:00
|
|
|
nsRefPtr<gfxDrawable> drawable = aDrawable;
|
2010-08-13 06:30:02 -07:00
|
|
|
|
2012-05-01 13:35:04 -07:00
|
|
|
aFilter = ReduceResamplingFilter(aFilter, aImageRect.Width(), aImageRect.Height(), aSourceRect.Width(), aSourceRect.Height());
|
|
|
|
|
2012-05-27 18:11:18 -07:00
|
|
|
gfxMatrix userSpaceToImageSpace = aUserSpaceToImageSpace;
|
|
|
|
|
2012-05-16 07:03:08 -07:00
|
|
|
// On Mobile, we don't ever want to do this; it has the potential for
|
|
|
|
// allocating very large temporary surfaces, especially since we'll
|
|
|
|
// do full-page snapshots often (see bug 749426).
|
2012-05-27 18:11:18 -07:00
|
|
|
#ifdef MOZ_GFX_OPTIMIZE_MOBILE
|
|
|
|
// If the pattern translation is large we can get into trouble with pixman's
|
|
|
|
// 16 bit coordinate limits. For now, we only do this on platforms where
|
|
|
|
// we know we have the pixman limits. 16384.0 is a somewhat arbitrary
|
|
|
|
// large number to make sure we avoid the expensive fmod when we can, but
|
|
|
|
// still maintain a safe margin from the actual limit
|
2014-06-17 10:35:51 -07:00
|
|
|
if (doTile && (userSpaceToImageSpace._32 > 16384.0 || userSpaceToImageSpace._31 > 16384.0)) {
|
|
|
|
userSpaceToImageSpace._31 = fmod(userSpaceToImageSpace._31, aImageRect.width);
|
|
|
|
userSpaceToImageSpace._32 = fmod(userSpaceToImageSpace._32, aImageRect.height);
|
2012-05-27 18:11:18 -07:00
|
|
|
}
|
|
|
|
#else
|
2010-08-13 06:30:02 -07:00
|
|
|
// OK now, the hard part left is to account for the subimage sampling
|
|
|
|
// restriction. If all the transforms involved are just integer
|
|
|
|
// translations, then we assume no resampling will occur so there's
|
|
|
|
// nothing to do.
|
|
|
|
// XXX if only we had source-clipping in cairo!
|
2010-08-13 06:30:27 -07:00
|
|
|
if (aContext->CurrentMatrix().HasNonIntegerTranslation() ||
|
|
|
|
aUserSpaceToImageSpace.HasNonIntegerTranslation()) {
|
2010-08-13 06:30:02 -07:00
|
|
|
if (doTile || !aSubimage.Contains(aImageRect)) {
|
2010-08-13 06:30:27 -07:00
|
|
|
nsRefPtr<gfxDrawable> restrictedDrawable =
|
|
|
|
CreateSamplingRestrictedDrawable(aDrawable, aContext,
|
|
|
|
aUserSpaceToImageSpace, aSourceRect,
|
|
|
|
aSubimage, aFormat);
|
|
|
|
if (restrictedDrawable) {
|
|
|
|
drawable.swap(restrictedDrawable);
|
2010-08-13 06:30:02 -07:00
|
|
|
}
|
|
|
|
}
|
2010-08-13 06:30:27 -07:00
|
|
|
// We no longer need to tile: Either we never needed to, or we already
|
|
|
|
// filled a surface with the tiled pattern; this surface can now be
|
|
|
|
// drawn without tiling.
|
2011-10-17 07:59:28 -07:00
|
|
|
doTile = false;
|
2010-08-13 06:30:02 -07:00
|
|
|
}
|
2012-05-16 07:03:08 -07:00
|
|
|
#endif
|
2010-08-13 06:30:02 -07:00
|
|
|
|
2012-05-27 18:11:18 -07:00
|
|
|
drawable->Draw(aContext, aFill, doTile, aFilter, userSpaceToImageSpace);
|
2010-08-13 06:30:02 -07:00
|
|
|
}
|
|
|
|
|
2010-09-07 17:36:57 -07:00
|
|
|
/* static */ int
|
2013-09-24 13:45:13 -07:00
|
|
|
gfxUtils::ImageFormatToDepth(gfxImageFormat aFormat)
|
2010-09-07 17:36:57 -07:00
|
|
|
{
|
|
|
|
switch (aFormat) {
|
2014-01-23 10:26:40 -08:00
|
|
|
case gfxImageFormat::ARGB32:
|
2010-09-07 17:36:57 -07:00
|
|
|
return 32;
|
2014-01-23 10:26:40 -08:00
|
|
|
case gfxImageFormat::RGB24:
|
2010-09-07 17:36:57 -07:00
|
|
|
return 24;
|
2014-01-23 10:26:40 -08:00
|
|
|
case gfxImageFormat::RGB16_565:
|
2010-09-07 17:36:57 -07:00
|
|
|
return 16;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2011-01-25 22:26:37 -08:00
|
|
|
|
2010-09-02 19:31:42 -07:00
|
|
|
static void
|
2011-01-25 22:26:37 -08:00
|
|
|
PathFromRegionInternal(gfxContext* aContext, const nsIntRegion& aRegion,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aSnap)
|
2010-09-02 19:31:42 -07:00
|
|
|
{
|
|
|
|
aContext->NewPath();
|
|
|
|
nsIntRegionRectIterator iter(aRegion);
|
|
|
|
const nsIntRect* r;
|
2012-07-30 07:20:58 -07:00
|
|
|
while ((r = iter.Next()) != nullptr) {
|
2010-09-02 19:31:42 -07:00
|
|
|
aContext->Rectangle(gfxRect(r->x, r->y, r->width, r->height), aSnap);
|
|
|
|
}
|
2011-01-25 22:26:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ClipToRegionInternal(gfxContext* aContext, const nsIntRegion& aRegion,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aSnap)
|
2011-01-25 22:26:37 -08:00
|
|
|
{
|
|
|
|
PathFromRegionInternal(aContext, aRegion, aSnap);
|
2010-09-02 19:31:42 -07:00
|
|
|
aContext->Clip();
|
|
|
|
}
|
|
|
|
|
Bug 825928: Land layers refactoring. r=jrmuizel,bas,nical,mattwoodrow,roc,nrc,benwa,bjacob,jgilbert,kchen CLOSED TREE
Please contact Bas Schouten <bschouten@mozilla.com>, Nicolas Silva <nsilva@mozilla.com> or Nicholas Cameron <ncameron@mozilla.com> with general questions. Below is a rough list of authors to contact with specific questions.
Authors:
gfx/layers/Compositor.* gfx/layers/Effects.h - Compositor Interface - bas,nrc,nical
gfx/layers/d3d* - D3D9/D3D10 - bas
gfx/layers/ThebesLayer* - ThebesLayers - nrc,bas
gfx/layers/composite/* - CompositeLayers - nrc,nical
gfx/layers/client/* - Client - nrc,nical,bas
gfx/layers/*Image* - nical
gfx/layers/ipc ipc - IPC - nical
gfx/layers/opengl - CompositorOGL - nrc,nical
gfx/2d - bas,nrc
gfx/gl - GLContext - bjacob
dom/* layout/* - DOM - mattwoodrow
2013-04-10 02:20:52 -07:00
|
|
|
static TemporaryRef<Path>
|
2013-11-27 05:03:04 -08:00
|
|
|
PathFromRegionInternal(DrawTarget* aTarget, const nsIntRegion& aRegion,
|
Bug 825928: Land layers refactoring. r=jrmuizel,bas,nical,mattwoodrow,roc,nrc,benwa,bjacob,jgilbert,kchen CLOSED TREE
Please contact Bas Schouten <bschouten@mozilla.com>, Nicolas Silva <nsilva@mozilla.com> or Nicholas Cameron <ncameron@mozilla.com> with general questions. Below is a rough list of authors to contact with specific questions.
Authors:
gfx/layers/Compositor.* gfx/layers/Effects.h - Compositor Interface - bas,nrc,nical
gfx/layers/d3d* - D3D9/D3D10 - bas
gfx/layers/ThebesLayer* - ThebesLayers - nrc,bas
gfx/layers/composite/* - CompositeLayers - nrc,nical
gfx/layers/client/* - Client - nrc,nical,bas
gfx/layers/*Image* - nical
gfx/layers/ipc ipc - IPC - nical
gfx/layers/opengl - CompositorOGL - nrc,nical
gfx/2d - bas,nrc
gfx/gl - GLContext - bjacob
dom/* layout/* - DOM - mattwoodrow
2013-04-10 02:20:52 -07:00
|
|
|
bool aSnap)
|
|
|
|
{
|
|
|
|
Matrix mat = aTarget->GetTransform();
|
|
|
|
const gfxFloat epsilon = 0.000001;
|
|
|
|
#define WITHIN_E(a,b) (fabs((a)-(b)) < epsilon)
|
|
|
|
// We're essentially duplicating the logic in UserToDevicePixelSnapped here.
|
|
|
|
bool shouldNotSnap = !aSnap || (WITHIN_E(mat._11,1.0) &&
|
|
|
|
WITHIN_E(mat._22,1.0) &&
|
|
|
|
WITHIN_E(mat._12,0.0) &&
|
|
|
|
WITHIN_E(mat._21,0.0));
|
|
|
|
#undef WITHIN_E
|
|
|
|
|
|
|
|
RefPtr<PathBuilder> pb = aTarget->CreatePathBuilder();
|
|
|
|
nsIntRegionRectIterator iter(aRegion);
|
|
|
|
|
|
|
|
const nsIntRect* r;
|
|
|
|
if (shouldNotSnap) {
|
|
|
|
while ((r = iter.Next()) != nullptr) {
|
|
|
|
pb->MoveTo(Point(r->x, r->y));
|
|
|
|
pb->LineTo(Point(r->XMost(), r->y));
|
|
|
|
pb->LineTo(Point(r->XMost(), r->YMost()));
|
|
|
|
pb->LineTo(Point(r->x, r->YMost()));
|
|
|
|
pb->Close();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while ((r = iter.Next()) != nullptr) {
|
|
|
|
Rect rect(r->x, r->y, r->width, r->height);
|
|
|
|
|
|
|
|
rect.Round();
|
|
|
|
pb->MoveTo(rect.TopLeft());
|
|
|
|
pb->LineTo(rect.TopRight());
|
|
|
|
pb->LineTo(rect.BottomRight());
|
|
|
|
pb->LineTo(rect.BottomLeft());
|
|
|
|
pb->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RefPtr<Path> path = pb->Finish();
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-11-27 05:03:04 -08:00
|
|
|
ClipToRegionInternal(DrawTarget* aTarget, const nsIntRegion& aRegion,
|
Bug 825928: Land layers refactoring. r=jrmuizel,bas,nical,mattwoodrow,roc,nrc,benwa,bjacob,jgilbert,kchen CLOSED TREE
Please contact Bas Schouten <bschouten@mozilla.com>, Nicolas Silva <nsilva@mozilla.com> or Nicholas Cameron <ncameron@mozilla.com> with general questions. Below is a rough list of authors to contact with specific questions.
Authors:
gfx/layers/Compositor.* gfx/layers/Effects.h - Compositor Interface - bas,nrc,nical
gfx/layers/d3d* - D3D9/D3D10 - bas
gfx/layers/ThebesLayer* - ThebesLayers - nrc,bas
gfx/layers/composite/* - CompositeLayers - nrc,nical
gfx/layers/client/* - Client - nrc,nical,bas
gfx/layers/*Image* - nical
gfx/layers/ipc ipc - IPC - nical
gfx/layers/opengl - CompositorOGL - nrc,nical
gfx/2d - bas,nrc
gfx/gl - GLContext - bjacob
dom/* layout/* - DOM - mattwoodrow
2013-04-10 02:20:52 -07:00
|
|
|
bool aSnap)
|
|
|
|
{
|
|
|
|
RefPtr<Path> path = PathFromRegionInternal(aTarget, aRegion, aSnap);
|
|
|
|
aTarget->PushClip(path);
|
|
|
|
}
|
|
|
|
|
2010-09-02 19:31:42 -07:00
|
|
|
/*static*/ void
|
|
|
|
gfxUtils::ClipToRegion(gfxContext* aContext, const nsIntRegion& aRegion)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
ClipToRegionInternal(aContext, aRegion, false);
|
2010-09-02 19:31:42 -07:00
|
|
|
}
|
|
|
|
|
2013-07-08 21:21:05 -07:00
|
|
|
/*static*/ void
|
|
|
|
gfxUtils::ClipToRegion(DrawTarget* aTarget, const nsIntRegion& aRegion)
|
|
|
|
{
|
|
|
|
ClipToRegionInternal(aTarget, aRegion, false);
|
|
|
|
}
|
|
|
|
|
2010-09-02 19:31:42 -07:00
|
|
|
/*static*/ void
|
|
|
|
gfxUtils::ClipToRegionSnapped(gfxContext* aContext, const nsIntRegion& aRegion)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
ClipToRegionInternal(aContext, aRegion, true);
|
2010-09-02 19:31:42 -07:00
|
|
|
}
|
2010-11-24 01:35:21 -08:00
|
|
|
|
Bug 825928: Land layers refactoring. r=jrmuizel,bas,nical,mattwoodrow,roc,nrc,benwa,bjacob,jgilbert,kchen CLOSED TREE
Please contact Bas Schouten <bschouten@mozilla.com>, Nicolas Silva <nsilva@mozilla.com> or Nicholas Cameron <ncameron@mozilla.com> with general questions. Below is a rough list of authors to contact with specific questions.
Authors:
gfx/layers/Compositor.* gfx/layers/Effects.h - Compositor Interface - bas,nrc,nical
gfx/layers/d3d* - D3D9/D3D10 - bas
gfx/layers/ThebesLayer* - ThebesLayers - nrc,bas
gfx/layers/composite/* - CompositeLayers - nrc,nical
gfx/layers/client/* - Client - nrc,nical,bas
gfx/layers/*Image* - nical
gfx/layers/ipc ipc - IPC - nical
gfx/layers/opengl - CompositorOGL - nrc,nical
gfx/2d - bas,nrc
gfx/gl - GLContext - bjacob
dom/* layout/* - DOM - mattwoodrow
2013-04-10 02:20:52 -07:00
|
|
|
/*static*/ void
|
|
|
|
gfxUtils::ClipToRegionSnapped(DrawTarget* aTarget, const nsIntRegion& aRegion)
|
|
|
|
{
|
|
|
|
ClipToRegionInternal(aTarget, aRegion, true);
|
|
|
|
}
|
|
|
|
|
2011-02-08 12:35:54 -08:00
|
|
|
/*static*/ gfxFloat
|
|
|
|
gfxUtils::ClampToScaleFactor(gfxFloat aVal)
|
|
|
|
{
|
|
|
|
// Arbitary scale factor limitation. We can increase this
|
|
|
|
// for better scaling performance at the cost of worse
|
|
|
|
// quality.
|
|
|
|
static const gfxFloat kScaleResolution = 2;
|
|
|
|
|
|
|
|
// Negative scaling is just a flip and irrelevant to
|
|
|
|
// our resolution calculation.
|
|
|
|
if (aVal < 0.0) {
|
|
|
|
aVal = -aVal;
|
|
|
|
}
|
|
|
|
|
2012-12-16 20:30:51 -08:00
|
|
|
bool inverse = false;
|
|
|
|
if (aVal < 1.0) {
|
|
|
|
inverse = true;
|
|
|
|
aVal = 1 / aVal;
|
|
|
|
}
|
|
|
|
|
2011-02-08 12:35:54 -08:00
|
|
|
gfxFloat power = log(aVal)/log(kScaleResolution);
|
|
|
|
|
2014-07-08 05:07:05 -07:00
|
|
|
// If power is within 1e-5 of an integer, round to nearest to
|
2011-02-08 12:35:54 -08:00
|
|
|
// prevent floating point errors, otherwise round up to the
|
|
|
|
// next integer value.
|
2014-07-08 05:07:05 -07:00
|
|
|
if (fabs(power - NS_round(power)) < 1e-5) {
|
2011-02-08 12:35:54 -08:00
|
|
|
power = NS_round(power);
|
2012-12-16 20:30:51 -08:00
|
|
|
} else if (inverse) {
|
|
|
|
power = floor(power);
|
2011-02-08 12:35:54 -08:00
|
|
|
} else {
|
2011-05-30 12:08:55 -07:00
|
|
|
power = ceil(power);
|
2011-02-08 12:35:54 -08:00
|
|
|
}
|
|
|
|
|
2012-04-29 14:41:13 -07:00
|
|
|
gfxFloat scale = pow(kScaleResolution, power);
|
|
|
|
|
2012-12-16 20:30:51 -08:00
|
|
|
if (inverse) {
|
|
|
|
scale = 1 / scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
return scale;
|
2011-02-08 12:35:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-25 22:26:37 -08:00
|
|
|
/*static*/ void
|
|
|
|
gfxUtils::PathFromRegion(gfxContext* aContext, const nsIntRegion& aRegion)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
PathFromRegionInternal(aContext, aRegion, false);
|
2011-01-25 22:26:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ void
|
|
|
|
gfxUtils::PathFromRegionSnapped(gfxContext* aContext, const nsIntRegion& aRegion)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
PathFromRegionInternal(aContext, aRegion, true);
|
2011-01-25 22:26:37 -08:00
|
|
|
}
|
|
|
|
|
2012-12-06 15:58:13 -08:00
|
|
|
gfxMatrix
|
|
|
|
gfxUtils::TransformRectToRect(const gfxRect& aFrom, const gfxPoint& aToTopLeft,
|
|
|
|
const gfxPoint& aToTopRight, const gfxPoint& aToBottomRight)
|
|
|
|
{
|
|
|
|
gfxMatrix m;
|
|
|
|
if (aToTopRight.y == aToTopLeft.y && aToTopRight.x == aToBottomRight.x) {
|
|
|
|
// Not a rotation, so xy and yx are zero
|
2014-06-17 10:35:51 -07:00
|
|
|
m._21 = m._12 = 0.0;
|
|
|
|
m._11 = (aToBottomRight.x - aToTopLeft.x)/aFrom.width;
|
|
|
|
m._22 = (aToBottomRight.y - aToTopLeft.y)/aFrom.height;
|
|
|
|
m._31 = aToTopLeft.x - m._11*aFrom.x;
|
|
|
|
m._32 = aToTopLeft.y - m._22*aFrom.y;
|
2012-12-06 15:58:13 -08:00
|
|
|
} else {
|
|
|
|
NS_ASSERTION(aToTopRight.y == aToBottomRight.y && aToTopRight.x == aToTopLeft.x,
|
|
|
|
"Destination rectangle not axis-aligned");
|
2014-06-17 10:35:51 -07:00
|
|
|
m._11 = m._22 = 0.0;
|
|
|
|
m._21 = (aToBottomRight.x - aToTopLeft.x)/aFrom.height;
|
|
|
|
m._12 = (aToBottomRight.y - aToTopLeft.y)/aFrom.width;
|
|
|
|
m._31 = aToTopLeft.x - m._21*aFrom.y;
|
|
|
|
m._32 = aToTopLeft.y - m._12*aFrom.x;
|
2012-12-06 15:58:13 -08:00
|
|
|
}
|
|
|
|
return m;
|
|
|
|
}
|
2011-01-25 22:26:37 -08:00
|
|
|
|
2014-01-27 07:27:20 -08:00
|
|
|
Matrix
|
|
|
|
gfxUtils::TransformRectToRect(const gfxRect& aFrom, const IntPoint& aToTopLeft,
|
|
|
|
const IntPoint& aToTopRight, const IntPoint& aToBottomRight)
|
|
|
|
{
|
|
|
|
Matrix m;
|
|
|
|
if (aToTopRight.y == aToTopLeft.y && aToTopRight.x == aToBottomRight.x) {
|
|
|
|
// Not a rotation, so xy and yx are zero
|
|
|
|
m._12 = m._21 = 0.0;
|
|
|
|
m._11 = (aToBottomRight.x - aToTopLeft.x)/aFrom.width;
|
|
|
|
m._22 = (aToBottomRight.y - aToTopLeft.y)/aFrom.height;
|
|
|
|
m._31 = aToTopLeft.x - m._11*aFrom.x;
|
|
|
|
m._32 = aToTopLeft.y - m._22*aFrom.y;
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(aToTopRight.y == aToBottomRight.y && aToTopRight.x == aToTopLeft.x,
|
|
|
|
"Destination rectangle not axis-aligned");
|
|
|
|
m._11 = m._22 = 0.0;
|
|
|
|
m._21 = (aToBottomRight.x - aToTopLeft.x)/aFrom.height;
|
|
|
|
m._12 = (aToBottomRight.y - aToTopLeft.y)/aFrom.width;
|
|
|
|
m._31 = aToTopLeft.x - m._21*aFrom.y;
|
|
|
|
m._32 = aToTopLeft.y - m._12*aFrom.x;
|
|
|
|
}
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2014-05-30 21:39:18 -07:00
|
|
|
/* This function is sort of shitty. We truncate doubles
|
|
|
|
* to ints then convert those ints back to doubles to make sure that
|
|
|
|
* they equal the doubles that we got in. */
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-11-24 01:35:21 -08:00
|
|
|
gfxUtils::GfxRectToIntRect(const gfxRect& aIn, nsIntRect* aOut)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
*aOut = nsIntRect(int32_t(aIn.X()), int32_t(aIn.Y()),
|
|
|
|
int32_t(aIn.Width()), int32_t(aIn.Height()));
|
2011-04-18 20:07:23 -07:00
|
|
|
return gfxRect(aOut->x, aOut->y, aOut->width, aOut->height).IsEqualEdges(aIn);
|
2010-11-24 01:35:21 -08:00
|
|
|
}
|
|
|
|
|
2011-09-27 15:19:24 -07:00
|
|
|
void
|
2013-10-01 17:57:50 -07:00
|
|
|
gfxUtils::GetYCbCrToRGBDestFormatAndSize(const PlanarYCbCrData& aData,
|
2013-09-24 13:45:13 -07:00
|
|
|
gfxImageFormat& aSuggestedFormat,
|
2011-09-27 15:19:24 -07:00
|
|
|
gfxIntSize& aSuggestedSize)
|
|
|
|
{
|
2013-11-27 05:03:04 -08:00
|
|
|
YUVType yuvtype =
|
|
|
|
TypeFromSize(aData.mYSize.width,
|
2011-09-27 15:19:24 -07:00
|
|
|
aData.mYSize.height,
|
|
|
|
aData.mCbCrSize.width,
|
|
|
|
aData.mCbCrSize.height);
|
|
|
|
|
|
|
|
// 'prescale' is true if the scaling is to be done as part of the
|
|
|
|
// YCbCr to RGB conversion rather than on the RGB data when rendered.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool prescale = aSuggestedSize.width > 0 && aSuggestedSize.height > 0 &&
|
2013-12-20 08:46:29 -08:00
|
|
|
ToIntSize(aSuggestedSize) != aData.mPicSize;
|
2011-09-27 15:19:24 -07:00
|
|
|
|
2014-01-23 10:26:40 -08:00
|
|
|
if (aSuggestedFormat == gfxImageFormat::RGB16_565) {
|
2011-09-27 15:19:24 -07:00
|
|
|
#if defined(HAVE_YCBCR_TO_RGB565)
|
|
|
|
if (prescale &&
|
2013-11-27 05:03:04 -08:00
|
|
|
!IsScaleYCbCrToRGB565Fast(aData.mPicX,
|
2011-09-27 15:19:24 -07:00
|
|
|
aData.mPicY,
|
|
|
|
aData.mPicSize.width,
|
|
|
|
aData.mPicSize.height,
|
|
|
|
aSuggestedSize.width,
|
|
|
|
aSuggestedSize.height,
|
|
|
|
yuvtype,
|
2013-11-27 05:03:04 -08:00
|
|
|
FILTER_BILINEAR) &&
|
|
|
|
IsConvertYCbCrToRGB565Fast(aData.mPicX,
|
2011-09-27 15:19:24 -07:00
|
|
|
aData.mPicY,
|
|
|
|
aData.mPicSize.width,
|
|
|
|
aData.mPicSize.height,
|
|
|
|
yuvtype)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
prescale = false;
|
2011-09-27 15:19:24 -07:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
// yuv2rgb16 function not available
|
2014-01-23 10:26:40 -08:00
|
|
|
aSuggestedFormat = gfxImageFormat::RGB24;
|
2011-09-27 15:19:24 -07:00
|
|
|
#endif
|
|
|
|
}
|
2014-01-23 10:26:40 -08:00
|
|
|
else if (aSuggestedFormat != gfxImageFormat::RGB24) {
|
2011-09-27 15:19:24 -07:00
|
|
|
// No other formats are currently supported.
|
2014-01-23 10:26:40 -08:00
|
|
|
aSuggestedFormat = gfxImageFormat::RGB24;
|
2011-09-27 15:19:24 -07:00
|
|
|
}
|
2014-01-23 10:26:40 -08:00
|
|
|
if (aSuggestedFormat == gfxImageFormat::RGB24) {
|
2011-09-27 15:19:24 -07:00
|
|
|
/* ScaleYCbCrToRGB32 does not support a picture offset, nor 4:4:4 data.
|
|
|
|
See bugs 639415 and 640073. */
|
2013-11-27 05:03:04 -08:00
|
|
|
if (aData.mPicX != 0 || aData.mPicY != 0 || yuvtype == YV24)
|
2011-10-17 07:59:28 -07:00
|
|
|
prescale = false;
|
2011-09-27 15:19:24 -07:00
|
|
|
}
|
|
|
|
if (!prescale) {
|
2013-12-20 08:46:29 -08:00
|
|
|
ToIntSize(aSuggestedSize) = aData.mPicSize;
|
2011-09-27 15:19:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-01 17:57:50 -07:00
|
|
|
gfxUtils::ConvertYCbCrToRGB(const PlanarYCbCrData& aData,
|
2013-09-24 13:45:13 -07:00
|
|
|
const gfxImageFormat& aDestFormat,
|
2011-09-27 15:19:24 -07:00
|
|
|
const gfxIntSize& aDestSize,
|
|
|
|
unsigned char* aDestBuffer,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aStride)
|
2011-09-27 15:19:24 -07:00
|
|
|
{
|
2012-10-15 20:03:43 -07:00
|
|
|
// ConvertYCbCrToRGB et al. assume the chroma planes are rounded up if the
|
|
|
|
// luma plane is odd sized.
|
|
|
|
MOZ_ASSERT((aData.mCbCrSize.width == aData.mYSize.width ||
|
|
|
|
aData.mCbCrSize.width == (aData.mYSize.width + 1) >> 1) &&
|
|
|
|
(aData.mCbCrSize.height == aData.mYSize.height ||
|
|
|
|
aData.mCbCrSize.height == (aData.mYSize.height + 1) >> 1));
|
2013-11-27 05:03:04 -08:00
|
|
|
YUVType yuvtype =
|
|
|
|
TypeFromSize(aData.mYSize.width,
|
2011-09-27 15:19:24 -07:00
|
|
|
aData.mYSize.height,
|
|
|
|
aData.mCbCrSize.width,
|
|
|
|
aData.mCbCrSize.height);
|
|
|
|
|
|
|
|
// Convert from YCbCr to RGB now, scaling the image if needed.
|
2013-12-20 08:46:29 -08:00
|
|
|
if (ToIntSize(aDestSize) != aData.mPicSize) {
|
2011-09-27 15:19:24 -07:00
|
|
|
#if defined(HAVE_YCBCR_TO_RGB565)
|
2014-01-23 10:26:40 -08:00
|
|
|
if (aDestFormat == gfxImageFormat::RGB16_565) {
|
2013-11-27 05:03:04 -08:00
|
|
|
ScaleYCbCrToRGB565(aData.mYChannel,
|
2011-09-27 15:19:24 -07:00
|
|
|
aData.mCbChannel,
|
|
|
|
aData.mCrChannel,
|
|
|
|
aDestBuffer,
|
|
|
|
aData.mPicX,
|
|
|
|
aData.mPicY,
|
|
|
|
aData.mPicSize.width,
|
|
|
|
aData.mPicSize.height,
|
|
|
|
aDestSize.width,
|
|
|
|
aDestSize.height,
|
|
|
|
aData.mYStride,
|
|
|
|
aData.mCbCrStride,
|
|
|
|
aStride,
|
|
|
|
yuvtype,
|
2013-11-27 05:03:04 -08:00
|
|
|
FILTER_BILINEAR);
|
2011-09-27 15:19:24 -07:00
|
|
|
} else
|
|
|
|
#endif
|
2013-11-27 05:03:04 -08:00
|
|
|
ScaleYCbCrToRGB32(aData.mYChannel,
|
2011-09-27 15:19:24 -07:00
|
|
|
aData.mCbChannel,
|
|
|
|
aData.mCrChannel,
|
|
|
|
aDestBuffer,
|
|
|
|
aData.mPicSize.width,
|
|
|
|
aData.mPicSize.height,
|
|
|
|
aDestSize.width,
|
|
|
|
aDestSize.height,
|
|
|
|
aData.mYStride,
|
|
|
|
aData.mCbCrStride,
|
|
|
|
aStride,
|
|
|
|
yuvtype,
|
2013-11-27 05:03:04 -08:00
|
|
|
ROTATE_0,
|
|
|
|
FILTER_BILINEAR);
|
2011-09-27 15:19:24 -07:00
|
|
|
} else { // no prescale
|
|
|
|
#if defined(HAVE_YCBCR_TO_RGB565)
|
2014-01-23 10:26:40 -08:00
|
|
|
if (aDestFormat == gfxImageFormat::RGB16_565) {
|
2013-11-27 05:03:04 -08:00
|
|
|
ConvertYCbCrToRGB565(aData.mYChannel,
|
2011-09-27 15:19:24 -07:00
|
|
|
aData.mCbChannel,
|
|
|
|
aData.mCrChannel,
|
|
|
|
aDestBuffer,
|
|
|
|
aData.mPicX,
|
|
|
|
aData.mPicY,
|
|
|
|
aData.mPicSize.width,
|
|
|
|
aData.mPicSize.height,
|
|
|
|
aData.mYStride,
|
|
|
|
aData.mCbCrStride,
|
|
|
|
aStride,
|
|
|
|
yuvtype);
|
2014-01-23 10:26:40 -08:00
|
|
|
} else // aDestFormat != gfxImageFormat::RGB16_565
|
2011-09-27 15:19:24 -07:00
|
|
|
#endif
|
2013-11-27 05:03:04 -08:00
|
|
|
ConvertYCbCrToRGB32(aData.mYChannel,
|
2011-09-27 15:19:24 -07:00
|
|
|
aData.mCbChannel,
|
|
|
|
aData.mCrChannel,
|
|
|
|
aDestBuffer,
|
|
|
|
aData.mPicX,
|
|
|
|
aData.mPicY,
|
|
|
|
aData.mPicSize.width,
|
|
|
|
aData.mPicSize.height,
|
|
|
|
aData.mYStride,
|
|
|
|
aData.mCbCrStride,
|
|
|
|
aStride,
|
|
|
|
yuvtype);
|
|
|
|
}
|
|
|
|
}
|
2011-11-16 19:44:16 -08:00
|
|
|
|
2014-07-05 16:10:49 -07:00
|
|
|
/* static */ void gfxUtils::ClearThebesSurface(gfxASurface* aSurface,
|
|
|
|
nsIntRect* aRect,
|
|
|
|
const gfxRGBA& aColor)
|
2014-06-23 15:48:58 -07:00
|
|
|
{
|
|
|
|
if (aSurface->CairoStatus()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cairo_surface_t* surf = aSurface->CairoSurface();
|
|
|
|
if (cairo_surface_status(surf)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cairo_t* ctx = cairo_create(surf);
|
2014-07-05 16:10:49 -07:00
|
|
|
cairo_set_source_rgba(ctx, aColor.r, aColor.g, aColor.b, aColor.a);
|
|
|
|
cairo_set_operator(ctx, CAIRO_OPERATOR_SOURCE);
|
|
|
|
nsIntRect bounds;
|
|
|
|
if (aRect) {
|
|
|
|
bounds = *aRect;
|
|
|
|
} else {
|
|
|
|
bounds = nsIntRect(nsIntPoint(0, 0), aSurface->GetSize());
|
|
|
|
}
|
|
|
|
cairo_rectangle(ctx, bounds.x, bounds.y, bounds.width, bounds.height);
|
|
|
|
cairo_fill(ctx);
|
2014-06-23 15:48:58 -07:00
|
|
|
cairo_destroy(ctx);
|
|
|
|
}
|
|
|
|
|
2014-04-08 02:05:39 -07:00
|
|
|
/* static */ TemporaryRef<DataSourceSurface>
|
|
|
|
gfxUtils::CopySurfaceToDataSourceSurfaceWithFormat(SourceSurface* aSurface,
|
|
|
|
SurfaceFormat aFormat)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aFormat != aSurface->GetFormat(),
|
|
|
|
"Unnecessary - and very expersive - surface format conversion");
|
|
|
|
|
|
|
|
Rect bounds(0, 0, aSurface->GetSize().width, aSurface->GetSize().height);
|
|
|
|
|
|
|
|
if (aSurface->GetType() != SurfaceType::DATA) {
|
|
|
|
// If the surface is NOT of type DATA then its data is not mapped into main
|
|
|
|
// memory. Format conversion is probably faster on the GPU, and by doing it
|
|
|
|
// there we can avoid any expensive uploads/readbacks except for (possibly)
|
|
|
|
// a single readback due to the unavoidable GetDataSurface() call. Using
|
|
|
|
// CreateOffscreenContentDrawTarget ensures the conversion happens on the
|
|
|
|
// GPU.
|
|
|
|
RefPtr<DrawTarget> dt = gfxPlatform::GetPlatform()->
|
|
|
|
CreateOffscreenContentDrawTarget(aSurface->GetSize(), aFormat);
|
|
|
|
// Using DrawSurface() here rather than CopySurface() because CopySurface
|
|
|
|
// is optimized for memcpy and therefore isn't good for format conversion.
|
|
|
|
// Using OP_OVER since in our case it's equivalent to OP_SOURCE and
|
|
|
|
// generally more optimized.
|
|
|
|
dt->DrawSurface(aSurface, bounds, bounds, DrawSurfaceOptions(),
|
|
|
|
DrawOptions(1.0f, CompositionOp::OP_OVER));
|
|
|
|
RefPtr<SourceSurface> surface = dt->Snapshot();
|
|
|
|
return surface->GetDataSurface();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the surface IS of type DATA then it may or may not be in main memory
|
|
|
|
// depending on whether or not it has been mapped yet. We have no way of
|
|
|
|
// knowing, so we can't be sure if it's best to create a data wrapping
|
|
|
|
// DrawTarget for the conversion or an offscreen content DrawTarget. We could
|
|
|
|
// guess it's not mapped and create an offscreen content DrawTarget, but if
|
|
|
|
// it is then we'll end up uploading the surface data, and most likely the
|
|
|
|
// caller is going to be accessing the resulting surface data, resulting in a
|
|
|
|
// readback (both very expensive operations). Alternatively we could guess
|
|
|
|
// the data is mapped and create a data wrapping DrawTarget and, if the
|
|
|
|
// surface is not in main memory, then we will incure a readback. The latter
|
|
|
|
// of these two "wrong choices" is the least costly (a readback, vs an
|
|
|
|
// upload and a readback), and more than likely the DATA surface that we've
|
|
|
|
// been passed actually IS in main memory anyway. For these reasons it's most
|
|
|
|
// likely best to create a data wrapping DrawTarget here to do the format
|
|
|
|
// conversion.
|
|
|
|
RefPtr<DataSourceSurface> dataSurface =
|
|
|
|
Factory::CreateDataSourceSurface(aSurface->GetSize(), aFormat);
|
|
|
|
DataSourceSurface::MappedSurface map;
|
|
|
|
if (!dataSurface ||
|
|
|
|
!dataSurface->Map(DataSourceSurface::MapType::READ_WRITE, &map)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
RefPtr<DrawTarget> dt =
|
|
|
|
Factory::CreateDrawTargetForData(BackendType::CAIRO,
|
|
|
|
map.mData,
|
|
|
|
dataSurface->GetSize(),
|
|
|
|
map.mStride,
|
|
|
|
aFormat);
|
|
|
|
if (!dt) {
|
|
|
|
dataSurface->Unmap();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// Using DrawSurface() here rather than CopySurface() because CopySurface
|
|
|
|
// is optimized for memcpy and therefore isn't good for format conversion.
|
|
|
|
// Using OP_OVER since in our case it's equivalent to OP_SOURCE and
|
|
|
|
// generally more optimized.
|
|
|
|
dt->DrawSurface(aSurface, bounds, bounds, DrawSurfaceOptions(),
|
|
|
|
DrawOptions(1.0f, CompositionOp::OP_OVER));
|
|
|
|
dataSurface->Unmap();
|
|
|
|
return dataSurface.forget();
|
|
|
|
}
|
|
|
|
|
2014-05-08 13:49:01 -07:00
|
|
|
const uint32_t gfxUtils::sNumFrameColors = 8;
|
|
|
|
|
|
|
|
/* static */ const gfx::Color&
|
|
|
|
gfxUtils::GetColorForFrameNumber(uint64_t aFrameNumber)
|
|
|
|
{
|
|
|
|
static bool initialized = false;
|
|
|
|
static gfx::Color colors[sNumFrameColors];
|
|
|
|
|
|
|
|
if (!initialized) {
|
|
|
|
uint32_t i = 0;
|
|
|
|
colors[i++] = gfx::Color::FromABGR(0xffff0000);
|
|
|
|
colors[i++] = gfx::Color::FromABGR(0xffcc00ff);
|
|
|
|
colors[i++] = gfx::Color::FromABGR(0xff0066cc);
|
|
|
|
colors[i++] = gfx::Color::FromABGR(0xff00ff00);
|
|
|
|
colors[i++] = gfx::Color::FromABGR(0xff33ffff);
|
|
|
|
colors[i++] = gfx::Color::FromABGR(0xffff0099);
|
|
|
|
colors[i++] = gfx::Color::FromABGR(0xff0000ff);
|
|
|
|
colors[i++] = gfx::Color::FromABGR(0xff999999);
|
|
|
|
MOZ_ASSERT(i == sNumFrameColors);
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return colors[aFrameNumber % sNumFrameColors];
|
|
|
|
}
|
|
|
|
|
2014-06-27 04:19:40 -07:00
|
|
|
/* static */ nsresult
|
|
|
|
gfxUtils::EncodeSourceSurface(SourceSurface* aSurface,
|
|
|
|
const nsACString& aMimeType,
|
|
|
|
const nsAString& aOutputOptions,
|
|
|
|
BinaryOrData aBinaryOrData,
|
|
|
|
FILE* aFile)
|
2011-11-16 19:44:16 -08:00
|
|
|
{
|
2014-06-27 04:19:40 -07:00
|
|
|
MOZ_ASSERT(aBinaryOrData == eDataURIEncode || aFile,
|
|
|
|
"Copying binary encoding to clipboard not currently supported");
|
|
|
|
|
|
|
|
const IntSize size = aSurface->GetSize();
|
|
|
|
if (size.IsEmpty()) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
const Size floatSize(size.width, size.height);
|
|
|
|
|
|
|
|
RefPtr<DataSourceSurface> dataSurface;
|
|
|
|
if (aSurface->GetFormat() != SurfaceFormat::B8G8R8A8) {
|
|
|
|
// FIXME bug 995807 (B8G8R8X8), bug 831898 (R5G6B5)
|
|
|
|
dataSurface =
|
|
|
|
CopySurfaceToDataSourceSurfaceWithFormat(aSurface,
|
|
|
|
SurfaceFormat::B8G8R8A8);
|
2011-11-16 19:44:16 -08:00
|
|
|
} else {
|
2014-06-27 04:19:40 -07:00
|
|
|
dataSurface = aSurface->GetDataSurface();
|
|
|
|
}
|
|
|
|
if (!dataSurface) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSourceSurface::MappedSurface map;
|
|
|
|
if (!dataSurface->Map(DataSourceSurface::MapType::READ, &map)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2011-11-16 19:44:16 -08:00
|
|
|
}
|
2014-06-27 04:19:40 -07:00
|
|
|
|
|
|
|
nsAutoCString encoderCID(
|
|
|
|
NS_LITERAL_CSTRING("@mozilla.org/image/encoder;2?type=") + aMimeType);
|
|
|
|
nsCOMPtr<imgIEncoder> encoder = do_CreateInstance(encoderCID.get());
|
|
|
|
if (!encoder) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
int32_t w = std::min(size.width, 8);
|
|
|
|
int32_t h = std::min(size.height, 8);
|
|
|
|
printf("Could not create encoder. Top-left %dx%d pixels contain:\n", w, h);
|
|
|
|
for (int32_t y = 0; y < h; ++y) {
|
|
|
|
for (int32_t x = 0; x < w; ++x) {
|
|
|
|
printf("%x ", reinterpret_cast<uint32_t*>(map.mData)[y*map.mStride+x]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
dataSurface->Unmap();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = encoder->InitFromData(map.mData,
|
|
|
|
BufferSizeFromStrideAndHeight(map.mStride, size.height),
|
|
|
|
size.width,
|
|
|
|
size.height,
|
|
|
|
map.mStride,
|
|
|
|
imgIEncoder::INPUT_FORMAT_HOSTARGB,
|
|
|
|
aOutputOptions);
|
|
|
|
dataSurface->Unmap();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> imgStream;
|
|
|
|
CallQueryInterface(encoder.get(), getter_AddRefs(imgStream));
|
|
|
|
if (!imgStream) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t bufSize64;
|
|
|
|
rv = imgStream->Available(&bufSize64);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(bufSize64 < UINT32_MAX - 16, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
uint32_t bufSize = (uint32_t)bufSize64;
|
|
|
|
|
|
|
|
// ...leave a little extra room so we can call read again and make sure we
|
|
|
|
// got everything. 16 bytes for better padding (maybe)
|
|
|
|
bufSize += 16;
|
|
|
|
uint32_t imgSize = 0;
|
|
|
|
Vector<char> imgData;
|
|
|
|
if (!imgData.initCapacity(bufSize)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
uint32_t numReadThisTime = 0;
|
|
|
|
while ((rv = imgStream->Read(imgData.begin() + imgSize,
|
|
|
|
bufSize - imgSize,
|
|
|
|
&numReadThisTime)) == NS_OK && numReadThisTime > 0)
|
|
|
|
{
|
|
|
|
imgSize += numReadThisTime;
|
|
|
|
if (imgSize == bufSize) {
|
|
|
|
// need a bigger buffer, just double
|
|
|
|
bufSize *= 2;
|
|
|
|
if (!imgData.resizeUninitialized(bufSize)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aBinaryOrData == eBinaryEncode) {
|
|
|
|
if (aFile) {
|
|
|
|
fwrite(imgData.begin(), 1, imgSize, aFile);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// base 64, result will be null-terminated
|
|
|
|
nsCString encodedImg;
|
|
|
|
rv = Base64Encode(Substring(imgData.begin(), imgSize), encodedImg);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCString string("data:");
|
|
|
|
string.Append(aMimeType);
|
|
|
|
string.Append(";base64,");
|
|
|
|
string.Append(encodedImg);
|
|
|
|
|
|
|
|
if (aFile) {
|
|
|
|
#ifdef ANDROID
|
|
|
|
if (aFile == stdout || aFile == stderr) {
|
|
|
|
// ADB logcat cuts off long strings so we will break it down
|
|
|
|
const char* cStr = string.BeginReading();
|
|
|
|
size_t len = strlen(cStr);
|
|
|
|
while (true) {
|
|
|
|
printf_stderr("IMG: %.140s\n", cStr);
|
|
|
|
if (len <= 140)
|
|
|
|
break;
|
|
|
|
len -= 140;
|
|
|
|
cStr += 140;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
fprintf(aFile, "%s", string.BeginReading());
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIClipboardHelper> clipboard(do_GetService("@mozilla.org/widget/clipboardhelper;1", &rv));
|
|
|
|
if (clipboard) {
|
|
|
|
clipboard->CopyString(NS_ConvertASCIItoUTF16(string), nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2011-11-16 19:44:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
2014-06-27 04:19:40 -07:00
|
|
|
gfxUtils::WriteAsPNG(SourceSurface* aSurface, const nsAString& aFile)
|
2011-11-16 19:44:16 -08:00
|
|
|
{
|
2014-06-27 04:19:40 -07:00
|
|
|
WriteAsPNG(aSurface, NS_ConvertUTF16toUTF8(aFile).get());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
gfxUtils::WriteAsPNG(SourceSurface* aSurface, const char* aFile)
|
|
|
|
{
|
|
|
|
FILE* file = fopen(aFile, "wb");
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
// Maybe the directory doesn't exist; try creating it, then fopen again.
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
nsCOMPtr<nsIFile> comFile = do_CreateInstance("@mozilla.org/file/local;1");
|
|
|
|
if (comFile) {
|
|
|
|
NS_ConvertUTF8toUTF16 utf16path((nsDependentCString(aFile)));
|
|
|
|
rv = comFile->InitWithPath(utf16path);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIFile> dirPath;
|
|
|
|
comFile->GetParent(getter_AddRefs(dirPath));
|
|
|
|
if (dirPath) {
|
|
|
|
rv = dirPath->Create(nsIFile::DIRECTORY_TYPE, 0777);
|
|
|
|
if (NS_SUCCEEDED(rv) || rv == NS_ERROR_FILE_ALREADY_EXISTS) {
|
|
|
|
file = fopen(aFile, "wb");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!file) {
|
|
|
|
NS_WARNING("Failed to open file to create PNG!\n");
|
|
|
|
return;
|
|
|
|
}
|
2011-11-16 19:44:16 -08:00
|
|
|
}
|
2014-06-27 04:19:40 -07:00
|
|
|
|
|
|
|
EncodeSourceSurface(aSurface, NS_LITERAL_CSTRING("image/png"),
|
|
|
|
EmptyString(), eBinaryEncode, file);
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
gfxUtils::WriteAsPNG(DrawTarget* aDT, const nsAString& aFile)
|
|
|
|
{
|
|
|
|
WriteAsPNG(aDT, NS_ConvertUTF16toUTF8(aFile).get());
|
2011-11-16 19:44:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
2014-06-27 04:19:40 -07:00
|
|
|
gfxUtils::WriteAsPNG(DrawTarget* aDT, const char* aFile)
|
2011-11-16 19:44:16 -08:00
|
|
|
{
|
2014-06-27 04:19:40 -07:00
|
|
|
RefPtr<SourceSurface> surface = aDT->Snapshot();
|
|
|
|
if (surface) {
|
|
|
|
WriteAsPNG(surface, aFile);
|
2011-11-16 19:44:16 -08:00
|
|
|
} else {
|
2014-06-27 04:19:40 -07:00
|
|
|
NS_WARNING("Failed to get surface!");
|
2011-11-16 19:44:16 -08:00
|
|
|
}
|
|
|
|
}
|
2012-03-01 00:26:09 -08:00
|
|
|
|
2014-02-11 10:41:09 -08:00
|
|
|
/* static */ void
|
2014-06-27 04:19:40 -07:00
|
|
|
gfxUtils::WriteAsPNG(nsIPresShell* aShell, const char* aFile)
|
|
|
|
{
|
|
|
|
int32_t width = 1000, height = 1000;
|
|
|
|
nsRect r(0, 0, aShell->GetPresContext()->DevPixelsToAppUnits(width),
|
|
|
|
aShell->GetPresContext()->DevPixelsToAppUnits(height));
|
|
|
|
|
|
|
|
RefPtr<mozilla::gfx::DrawTarget> dt = gfxPlatform::GetPlatform()->
|
|
|
|
CreateOffscreenContentDrawTarget(IntSize(width, height),
|
|
|
|
SurfaceFormat::B8G8R8A8);
|
|
|
|
NS_ENSURE_TRUE(dt, /*void*/);
|
|
|
|
|
|
|
|
nsRefPtr<gfxContext> context = new gfxContext(dt);
|
|
|
|
aShell->RenderDocument(r, 0, NS_RGB(255, 255, 0), context);
|
|
|
|
WriteAsPNG(dt.get(), aFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
gfxUtils::DumpAsDataURI(SourceSurface* aSurface, FILE* aFile)
|
|
|
|
{
|
|
|
|
EncodeSourceSurface(aSurface, NS_LITERAL_CSTRING("image/png"),
|
|
|
|
EmptyString(), eDataURIEncode, aFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
gfxUtils::DumpAsDataURI(DrawTarget* aDT, FILE* aFile)
|
2014-02-11 10:41:09 -08:00
|
|
|
{
|
2014-06-27 04:19:40 -07:00
|
|
|
RefPtr<SourceSurface> surface = aDT->Snapshot();
|
|
|
|
if (surface) {
|
|
|
|
DumpAsDataURI(surface, aFile);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Failed to get surface!");
|
|
|
|
}
|
2014-02-11 10:41:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
2014-06-27 04:19:40 -07:00
|
|
|
gfxUtils::CopyAsDataURI(SourceSurface* aSurface)
|
2014-02-11 10:41:09 -08:00
|
|
|
{
|
2014-06-27 04:19:40 -07:00
|
|
|
EncodeSourceSurface(aSurface, NS_LITERAL_CSTRING("image/png"),
|
|
|
|
EmptyString(), eDataURIEncode, nullptr);
|
2014-02-11 10:41:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
2014-06-27 04:19:40 -07:00
|
|
|
gfxUtils::CopyAsDataURI(DrawTarget* aDT)
|
2014-02-11 10:41:09 -08:00
|
|
|
{
|
2014-06-27 04:19:40 -07:00
|
|
|
RefPtr<SourceSurface> surface = aDT->Snapshot();
|
|
|
|
if (surface) {
|
|
|
|
CopyAsDataURI(surface);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Failed to get surface!");
|
|
|
|
}
|
2014-02-11 10:41:09 -08:00
|
|
|
}
|
|
|
|
|
2014-06-27 04:19:40 -07:00
|
|
|
#ifdef MOZ_DUMP_PAINTING
|
2014-05-03 17:35:47 -07:00
|
|
|
static bool sDumpPaintList = getenv("MOZ_DUMP_PAINT_LIST") != 0;
|
|
|
|
|
|
|
|
/* static */ bool
|
|
|
|
gfxUtils::DumpPaintList() {
|
|
|
|
return sDumpPaintList || gfxPrefs::LayoutDumpDisplayList();
|
|
|
|
}
|
|
|
|
|
2012-03-28 11:10:28 -07:00
|
|
|
bool gfxUtils::sDumpPainting = getenv("MOZ_DUMP_PAINT") != 0;
|
2012-03-01 00:26:09 -08:00
|
|
|
bool gfxUtils::sDumpPaintingToFile = getenv("MOZ_DUMP_PAINT_TO_FILE") != 0;
|
2013-07-31 08:44:31 -07:00
|
|
|
FILE *gfxUtils::sDumpPaintFile = nullptr;
|
2011-11-16 19:44:16 -08:00
|
|
|
#endif
|