2008-07-13 22:27:43 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 11:12:37 +00: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 22:27:43 +00:00
|
|
|
|
|
|
|
#ifndef GFX_UTILS_H
|
|
|
|
#define GFX_UTILS_H
|
|
|
|
|
2010-05-18 05:43:05 +00:00
|
|
|
#include "gfxTypes.h"
|
2013-10-01 21:01:19 +00:00
|
|
|
#include "GraphicsFilter.h"
|
2012-04-17 22:04:15 +00:00
|
|
|
#include "imgIContainer.h"
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2010-08-13 13:30:27 +00:00
|
|
|
class gfxDrawable;
|
2010-09-03 02:31:42 +00:00
|
|
|
class nsIntRegion;
|
2011-01-20 02:47:39 +00:00
|
|
|
struct nsIntRect;
|
2010-08-13 13:30:27 +00:00
|
|
|
|
2013-10-02 00:57:50 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
class PlanarYCbCrData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-29 21:59:24 +00:00
|
|
|
class gfxUtils {
|
2008-07-13 22:27:43 +00:00
|
|
|
public:
|
2010-05-18 04:04:22 +00: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.
|
|
|
|
*
|
2013-09-24 20:45:13 +00:00
|
|
|
* If the source is not gfxImageFormatARGB32, no operation is performed. If
|
2010-05-18 04:04:22 +00:00
|
|
|
* aDestSurface is given, the data is copied over.
|
|
|
|
*/
|
|
|
|
static void PremultiplyImageSurface(gfxImageSurface *aSourceSurface,
|
2012-07-30 14:20:58 +00:00
|
|
|
gfxImageSurface *aDestSurface = nullptr);
|
2010-05-18 04:04:22 +00:00
|
|
|
static void UnpremultiplyImageSurface(gfxImageSurface *aSurface,
|
2012-07-30 14:20:58 +00:00
|
|
|
gfxImageSurface *aDestSurface = nullptr);
|
2010-08-13 13:30:02 +00:00
|
|
|
|
2012-03-23 22:10:50 +00:00
|
|
|
static void ConvertBGRAtoRGBA(gfxImageSurface *aSourceSurface,
|
2012-07-30 14:20:58 +00:00
|
|
|
gfxImageSurface *aDestSurface = nullptr);
|
2012-03-23 22:10:50 +00:00
|
|
|
|
2010-08-13 13:30:02 +00:00
|
|
|
/**
|
2010-08-13 13:30:27 +00:00
|
|
|
* Draw something drawable while working around limitations like bad support
|
2010-08-13 13:30:02 +00: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 13:30:27 +00:00
|
|
|
gfxDrawable* aDrawable,
|
2010-08-13 13:30:02 +00:00
|
|
|
const gfxMatrix& aUserSpaceToImageSpace,
|
|
|
|
const gfxRect& aSubimage,
|
|
|
|
const gfxRect& aSourceRect,
|
|
|
|
const gfxRect& aImageRect,
|
|
|
|
const gfxRect& aFill,
|
2013-09-24 20:45:13 +00:00
|
|
|
const gfxImageFormat aFormat,
|
2013-10-01 21:01:19 +00:00
|
|
|
GraphicsFilter aFilter,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aImageFlags = imgIContainer::FLAG_NONE);
|
2010-09-03 02:31:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clip aContext to the region aRegion.
|
|
|
|
*/
|
|
|
|
static void ClipToRegion(gfxContext* aContext, const nsIntRegion& aRegion);
|
|
|
|
|
2013-07-09 04:21:05 +00:00
|
|
|
/**
|
|
|
|
* Clip aTarget to the region aRegion.
|
|
|
|
*/
|
|
|
|
static void ClipToRegion(mozilla::gfx::DrawTarget* aTarget, const nsIntRegion& aRegion);
|
|
|
|
|
2010-09-03 02:31:42 +00:00
|
|
|
/**
|
|
|
|
* Clip aContext to the region aRegion, snapping the rectangles.
|
|
|
|
*/
|
|
|
|
static void ClipToRegionSnapped(gfxContext* aContext, const nsIntRegion& aRegion);
|
2010-09-08 00:36:57 +00: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 09:20:52 +00:00
|
|
|
/**
|
2013-07-09 04:21:05 +00: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 09:20:52 +00:00
|
|
|
*/
|
|
|
|
static void ClipToRegionSnapped(mozilla::gfx::DrawTarget* aTarget, const nsIntRegion& aRegion);
|
|
|
|
|
2011-01-26 06:26:37 +00: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-08 00:36:57 +00:00
|
|
|
/*
|
|
|
|
* Convert image format to depth value
|
|
|
|
*/
|
2013-09-24 20:45:13 +00:00
|
|
|
static int ImageFormatToDepth(gfxImageFormat aFormat);
|
2010-11-24 09:35:21 +00:00
|
|
|
|
2012-12-06 23:58:13 +00: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);
|
|
|
|
|
2010-11-24 09:35:21 +00:00
|
|
|
/**
|
|
|
|
* If aIn can be represented exactly using an nsIntRect (i.e.
|
2012-08-22 15:56:38 +00:00
|
|
|
* integer-aligned edges and coordinates in the int32_t range) then we
|
2010-11-24 09:35:21 +00:00
|
|
|
* set aOut to that rectangle, otherwise return failure.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool GfxRectToIntRect(const gfxRect& aIn, nsIntRect* aOut);
|
2011-02-08 20:35:54 +00:00
|
|
|
|
|
|
|
/**
|
2011-06-22 12:11:27 +00:00
|
|
|
* Return the smallest power of kScaleResolution (2) greater than or equal to
|
|
|
|
* aVal.
|
2011-02-08 20:35:54 +00:00
|
|
|
*/
|
|
|
|
static gfxFloat ClampToScaleFactor(gfxFloat aVal);
|
2011-09-27 22:19:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for ConvertYCbCrToRGB that finds the
|
|
|
|
* RGB buffer size and format for given YCbCrImage.
|
2013-09-24 20:45:13 +00:00
|
|
|
* @param aSuggestedFormat will be set to gfxImageFormatRGB24
|
2011-09-27 22:19:24 +00: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-02 00:57:50 +00:00
|
|
|
GetYCbCrToRGBDestFormatAndSize(const mozilla::layers::PlanarYCbCrData& aData,
|
2013-09-24 20:45:13 +00:00
|
|
|
gfxImageFormat& aSuggestedFormat,
|
2011-09-27 22:19:24 +00:00
|
|
|
gfxIntSize& aSuggestedSize);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert YCbCrImage into RGB aDestBuffer
|
|
|
|
* Format and Size parameters must have
|
|
|
|
* been passed to GetYCbCrToRGBDestFormatAndSize
|
|
|
|
*/
|
|
|
|
static void
|
2013-10-02 00:57:50 +00:00
|
|
|
ConvertYCbCrToRGB(const mozilla::layers::PlanarYCbCrData& aData,
|
2013-09-24 20:45:13 +00:00
|
|
|
const gfxImageFormat& aDestFormat,
|
2011-09-27 22:19:24 +00:00
|
|
|
const gfxIntSize& aDestSize,
|
|
|
|
unsigned char* aDestBuffer,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aStride);
|
2011-11-17 03:44:16 +00:00
|
|
|
|
2012-12-17 00:20:01 +00:00
|
|
|
static const uint8_t sUnpremultiplyTable[256*256];
|
|
|
|
static const uint8_t sPremultiplyTable[256*256];
|
2011-11-17 03:44:16 +00: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 18:09:05 +00:00
|
|
|
|
2012-03-28 18:10:28 +00:00
|
|
|
static bool sDumpPaintList;
|
2012-03-05 18:09:05 +00:00
|
|
|
static bool sDumpPainting;
|
|
|
|
static bool sDumpPaintingToFile;
|
|
|
|
static FILE* sDumpPaintFile;
|
2011-11-17 03:44:16 +00:00
|
|
|
#endif
|
2008-07-13 22:27:43 +00:00
|
|
|
};
|
|
|
|
|
2012-02-19 02:23:06 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
2012-03-20 19:47:02 +00:00
|
|
|
|
2012-11-19 18:27:41 +00:00
|
|
|
/* These techniques are suggested by "Bit Twiddling Hacks"
|
2012-02-19 02:23:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if |aNumber| is a power of two
|
2012-11-19 18:27:41 +00:00
|
|
|
* 0 is incorreclty considered a power of two
|
2012-02-19 02:23:06 +00: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 19:47:02 +00:00
|
|
|
#if defined(__arm__)
|
|
|
|
return 1 << (32 - __builtin_clz(aNumber - 1));
|
|
|
|
#else
|
2012-05-04 08:57:59 +00:00
|
|
|
--aNumber;
|
|
|
|
aNumber |= aNumber >> 1;
|
|
|
|
aNumber |= aNumber >> 2;
|
|
|
|
aNumber |= aNumber >> 4;
|
|
|
|
aNumber |= aNumber >> 8;
|
|
|
|
aNumber |= aNumber >> 16;
|
2012-03-20 19:47:02 +00:00
|
|
|
return ++aNumber;
|
|
|
|
#endif
|
2012-02-19 02:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2008-07-13 22:27:43 +00:00
|
|
|
#endif
|