2008-07-13 15:27:43 -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/. */
|
2008-07-13 15:27:43 -07:00
|
|
|
|
|
|
|
#ifndef GFX_UTILS_H
|
|
|
|
#define GFX_UTILS_H
|
|
|
|
|
2010-05-17 22:43:05 -07:00
|
|
|
#include "gfxTypes.h"
|
2013-10-01 14:01:19 -07:00
|
|
|
#include "GraphicsFilter.h"
|
2012-04-17 15:04:15 -07:00
|
|
|
#include "imgIContainer.h"
|
2010-05-17 21:04:22 -07:00
|
|
|
|
2010-08-13 06:30:27 -07:00
|
|
|
class gfxDrawable;
|
2010-09-02 19:31:42 -07:00
|
|
|
class nsIntRegion;
|
2011-01-19 18:47:39 -08:00
|
|
|
struct nsIntRect;
|
2010-08-13 06:30:27 -07:00
|
|
|
|
2013-10-01 17:57:50 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
class PlanarYCbCrData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-29 14:59:24 -07:00
|
|
|
class gfxUtils {
|
2008-07-13 15:27:43 -07:00
|
|
|
public:
|
2014-01-27 07:27:20 -08:00
|
|
|
typedef mozilla::gfx::IntPoint IntPoint;
|
|
|
|
typedef mozilla::gfx::Matrix Matrix;
|
2010-05-17 21:04:22 -07:00
|
|
|
/*
|
|
|
|
* Premultiply or Unpremultiply aSourceSurface, writing the result
|
|
|
|
* to aDestSurface or back into aSourceSurface if aDestSurface is null.
|
|
|
|
*
|
|
|
|
* If aDestSurface is given, it must have identical format, dimensions, and
|
|
|
|
* stride as the source.
|
|
|
|
*
|
2014-01-23 10:26:40 -08:00
|
|
|
* If the source is not gfxImageFormat::ARGB32, no operation is performed. If
|
2010-05-17 21:04:22 -07:00
|
|
|
* aDestSurface is given, the data is copied over.
|
|
|
|
*/
|
|
|
|
static void PremultiplyImageSurface(gfxImageSurface *aSourceSurface,
|
2012-07-30 07:20:58 -07:00
|
|
|
gfxImageSurface *aDestSurface = nullptr);
|
2010-05-17 21:04:22 -07:00
|
|
|
static void UnpremultiplyImageSurface(gfxImageSurface *aSurface,
|
2012-07-30 07:20:58 -07:00
|
|
|
gfxImageSurface *aDestSurface = nullptr);
|
2010-08-13 06:30:02 -07:00
|
|
|
|
2012-03-23 15:10:50 -07:00
|
|
|
static void ConvertBGRAtoRGBA(gfxImageSurface *aSourceSurface,
|
2012-07-30 07:20:58 -07:00
|
|
|
gfxImageSurface *aDestSurface = nullptr);
|
2012-03-23 15:10:50 -07:00
|
|
|
|
2010-08-13 06:30:02 -07:00
|
|
|
/**
|
2010-08-13 06:30:27 -07:00
|
|
|
* Draw something drawable while working around limitations like bad support
|
2010-08-13 06:30:02 -07:00
|
|
|
* for EXTEND_PAD, lack of source-clipping, or cairo / pixman bugs with
|
|
|
|
* extreme user-space-to-image-space transforms.
|
|
|
|
*
|
|
|
|
* The input parameters here usually come from the output of our image
|
|
|
|
* snapping algorithm in nsLayoutUtils.cpp.
|
|
|
|
* This method is split from nsLayoutUtils::DrawPixelSnapped to allow for
|
|
|
|
* adjusting the parameters. For example, certain images with transparent
|
|
|
|
* margins only have a drawable subimage. For those images, imgFrame::Draw
|
|
|
|
* will tweak the rects and transforms that it gets from the pixel snapping
|
|
|
|
* algorithm before passing them on to this method.
|
|
|
|
*/
|
|
|
|
static void 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,
|
2013-09-24 13:45:13 -07:00
|
|
|
const gfxImageFormat aFormat,
|
2013-10-01 14:01:19 -07:00
|
|
|
GraphicsFilter aFilter,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aImageFlags = imgIContainer::FLAG_NONE);
|
2010-09-02 19:31:42 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clip aContext to the region aRegion.
|
|
|
|
*/
|
|
|
|
static void ClipToRegion(gfxContext* aContext, const nsIntRegion& aRegion);
|
|
|
|
|
2013-07-08 21:21:05 -07:00
|
|
|
/**
|
|
|
|
* Clip aTarget to the region aRegion.
|
|
|
|
*/
|
|
|
|
static void ClipToRegion(mozilla::gfx::DrawTarget* aTarget, const nsIntRegion& aRegion);
|
|
|
|
|
2010-09-02 19:31:42 -07:00
|
|
|
/**
|
|
|
|
* Clip aContext to the region aRegion, snapping the rectangles.
|
|
|
|
*/
|
|
|
|
static void ClipToRegionSnapped(gfxContext* aContext, const nsIntRegion& aRegion);
|
2010-09-07 17:36:57 -07: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
|
|
|
/**
|
2013-07-08 21:21:05 -07:00
|
|
|
* Clip aTarget to the region aRegion, snapping the rectangles.
|
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 ClipToRegionSnapped(mozilla::gfx::DrawTarget* aTarget, const nsIntRegion& aRegion);
|
|
|
|
|
2011-01-25 22:26:37 -08:00
|
|
|
/**
|
|
|
|
* Create a path consisting of rectangles in |aRegion|.
|
|
|
|
*/
|
|
|
|
static void PathFromRegion(gfxContext* aContext, const nsIntRegion& aRegion);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a path consisting of rectangles in |aRegion|, snapping the rectangles.
|
|
|
|
*/
|
|
|
|
static void PathFromRegionSnapped(gfxContext* aContext, const nsIntRegion& aRegion);
|
|
|
|
|
2010-09-07 17:36:57 -07:00
|
|
|
/*
|
|
|
|
* Convert image format to depth value
|
|
|
|
*/
|
2013-09-24 13:45:13 -07:00
|
|
|
static int ImageFormatToDepth(gfxImageFormat aFormat);
|
2010-11-24 01:35:21 -08:00
|
|
|
|
2012-12-06 15:58:13 -08:00
|
|
|
/**
|
|
|
|
* Return the transform matrix that maps aFrom to the rectangle defined by
|
|
|
|
* aToTopLeft/aToTopRight/aToBottomRight. aFrom must be
|
|
|
|
* nonempty and the destination rectangle must be axis-aligned.
|
|
|
|
*/
|
|
|
|
static gfxMatrix TransformRectToRect(const gfxRect& aFrom,
|
|
|
|
const gfxPoint& aToTopLeft,
|
|
|
|
const gfxPoint& aToTopRight,
|
|
|
|
const gfxPoint& aToBottomRight);
|
|
|
|
|
2014-01-27 07:27:20 -08:00
|
|
|
static Matrix TransformRectToRect(const gfxRect& aFrom,
|
|
|
|
const IntPoint& aToTopLeft,
|
|
|
|
const IntPoint& aToTopRight,
|
|
|
|
const IntPoint& aToBottomRight);
|
|
|
|
|
2010-11-24 01:35:21 -08:00
|
|
|
/**
|
|
|
|
* If aIn can be represented exactly using an nsIntRect (i.e.
|
2012-08-22 08:56:38 -07:00
|
|
|
* integer-aligned edges and coordinates in the int32_t range) then we
|
2010-11-24 01:35:21 -08:00
|
|
|
* set aOut to that rectangle, otherwise return failure.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool GfxRectToIntRect(const gfxRect& aIn, nsIntRect* aOut);
|
2011-02-08 12:35:54 -08:00
|
|
|
|
|
|
|
/**
|
2011-06-22 05:11:27 -07:00
|
|
|
* Return the smallest power of kScaleResolution (2) greater than or equal to
|
|
|
|
* aVal.
|
2011-02-08 12:35:54 -08:00
|
|
|
*/
|
|
|
|
static gfxFloat ClampToScaleFactor(gfxFloat aVal);
|
2011-09-27 15:19:24 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for ConvertYCbCrToRGB that finds the
|
|
|
|
* RGB buffer size and format for given YCbCrImage.
|
2014-01-23 10:26:40 -08:00
|
|
|
* @param aSuggestedFormat will be set to gfxImageFormat::RGB24
|
2011-09-27 15:19:24 -07:00
|
|
|
* if the desired format is not supported.
|
|
|
|
* @param aSuggestedSize will be set to the picture size from aData
|
|
|
|
* if either the suggested size was {0,0}
|
|
|
|
* or simultaneous scaling and conversion is not supported.
|
|
|
|
*/
|
|
|
|
static void
|
2013-10-01 17:57:50 -07:00
|
|
|
GetYCbCrToRGBDestFormatAndSize(const mozilla::layers::PlanarYCbCrData& aData,
|
2013-09-24 13:45:13 -07:00
|
|
|
gfxImageFormat& aSuggestedFormat,
|
2011-09-27 15:19:24 -07:00
|
|
|
gfxIntSize& aSuggestedSize);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert YCbCrImage into RGB aDestBuffer
|
|
|
|
* Format and Size parameters must have
|
|
|
|
* been passed to GetYCbCrToRGBDestFormatAndSize
|
|
|
|
*/
|
|
|
|
static void
|
2013-10-01 17:57:50 -07:00
|
|
|
ConvertYCbCrToRGB(const mozilla::layers::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-11-16 19:44:16 -08:00
|
|
|
|
2012-12-16 16:20:01 -08:00
|
|
|
static const uint8_t sUnpremultiplyTable[256*256];
|
|
|
|
static const uint8_t sPremultiplyTable[256*256];
|
2011-11-16 19:44:16 -08:00
|
|
|
#ifdef MOZ_DUMP_PAINTING
|
|
|
|
/**
|
|
|
|
* Writes a binary PNG file.
|
|
|
|
*/
|
|
|
|
static void WriteAsPNG(mozilla::gfx::DrawTarget* aDT, const char* aFile);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write as a PNG encoded Data URL to stdout.
|
|
|
|
*/
|
|
|
|
static void DumpAsDataURL(mozilla::gfx::DrawTarget* aDT);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy a PNG encoded Data URL to the clipboard.
|
|
|
|
*/
|
|
|
|
static void CopyAsDataURL(mozilla::gfx::DrawTarget* aDT);
|
2012-03-05 10:09:05 -08:00
|
|
|
|
2012-03-28 11:10:28 -07:00
|
|
|
static bool sDumpPaintList;
|
2012-03-05 10:09:05 -08:00
|
|
|
static bool sDumpPainting;
|
|
|
|
static bool sDumpPaintingToFile;
|
|
|
|
static FILE* sDumpPaintFile;
|
2014-02-11 10:41:09 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes a binary PNG file.
|
|
|
|
* Expensive. Creates a DataSourceSurface, then a DrawTarget, then passes to DrawTarget overloads
|
|
|
|
*/
|
|
|
|
static void WriteAsPNG(mozilla::RefPtr<mozilla::gfx::SourceSurface> aSourceSurface, const char* aFile);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write as a PNG encoded Data URL to stdout.
|
|
|
|
* Expensive. Creates a DataSourceSurface, then a DrawTarget, then passes to DrawTarget overloads
|
|
|
|
*/
|
|
|
|
static void DumpAsDataURL(mozilla::RefPtr<mozilla::gfx::SourceSurface> aSourceSurface);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy a PNG encoded Data URL to the clipboard.
|
|
|
|
* Expensive. Creates a DataSourceSurface, then a DrawTarget, then passes to DrawTarget overloads
|
|
|
|
*/
|
|
|
|
static void CopyAsDataURL(mozilla::RefPtr<mozilla::gfx::SourceSurface> aSourceSurface);
|
2011-11-16 19:44:16 -08:00
|
|
|
#endif
|
2008-07-13 15:27:43 -07:00
|
|
|
};
|
|
|
|
|
2012-02-18 18:23:06 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
2012-03-20 12:47:02 -07:00
|
|
|
|
2012-11-19 10:27:41 -08:00
|
|
|
/* These techniques are suggested by "Bit Twiddling Hacks"
|
2012-02-18 18:23:06 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if |aNumber| is a power of two
|
2012-11-19 10:27:41 -08:00
|
|
|
* 0 is incorreclty considered a power of two
|
2012-02-18 18:23:06 -08:00
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
IsPowerOfTwo(int aNumber)
|
|
|
|
{
|
|
|
|
return (aNumber & (aNumber - 1)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the first integer greater than |aNumber| which is a power of two
|
|
|
|
* Undefined for |aNumber| < 0
|
|
|
|
*/
|
|
|
|
static inline int
|
|
|
|
NextPowerOfTwo(int aNumber)
|
|
|
|
{
|
2012-03-20 12:47:02 -07:00
|
|
|
#if defined(__arm__)
|
|
|
|
return 1 << (32 - __builtin_clz(aNumber - 1));
|
|
|
|
#else
|
2012-05-04 01:57:59 -07:00
|
|
|
--aNumber;
|
|
|
|
aNumber |= aNumber >> 1;
|
|
|
|
aNumber |= aNumber >> 2;
|
|
|
|
aNumber |= aNumber >> 4;
|
|
|
|
aNumber |= aNumber >> 8;
|
|
|
|
aNumber |= aNumber >> 16;
|
2012-03-20 12:47:02 -07:00
|
|
|
return ++aNumber;
|
|
|
|
#endif
|
2012-02-18 18:23:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2008-07-13 15:27:43 -07:00
|
|
|
#endif
|