2007-03-22 10:30:00 -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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef GFX_ASURFACE_H
|
|
|
|
#define GFX_ASURFACE_H
|
|
|
|
|
2013-06-23 05:03:39 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2014-01-06 07:49:03 -08:00
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "gfxTypes.h"
|
2013-09-24 13:45:13 -07:00
|
|
|
#include "nscore.h"
|
2014-05-27 17:59:10 -07:00
|
|
|
#include "nsSize.h"
|
2013-09-24 13:45:13 -07:00
|
|
|
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
#include "nsStringFwd.h"
|
|
|
|
#else
|
|
|
|
#include "nsStringAPI.h"
|
|
|
|
#endif
|
2012-01-31 18:18:30 -08:00
|
|
|
|
2009-06-25 13:30:56 -07:00
|
|
|
class gfxImageSurface;
|
2011-03-09 09:27:37 -08:00
|
|
|
struct nsIntPoint;
|
|
|
|
struct nsIntRect;
|
2013-09-24 13:45:13 -07:00
|
|
|
struct gfxRect;
|
|
|
|
struct gfxPoint;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct already_AddRefed;
|
2009-06-25 13:30:56 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* A surface is something you can draw on. Instantiate a subclass of this
|
|
|
|
* abstract class, and use gfxContext to draw on this surface.
|
|
|
|
*/
|
2013-05-29 14:59:24 -07:00
|
|
|
class gfxASurface {
|
2007-03-22 10:30:00 -07:00
|
|
|
public:
|
2012-01-06 06:44:25 -08:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2007-03-22 10:30:00 -07:00
|
|
|
nsrefcnt AddRef(void);
|
|
|
|
nsrefcnt Release(void);
|
|
|
|
|
2012-01-06 06:44:25 -08:00
|
|
|
// These functions exist so that browsercomps can refcount a gfxASurface
|
2013-09-24 13:45:13 -07:00
|
|
|
virtual nsrefcnt AddRefExternal(void);
|
|
|
|
virtual nsrefcnt ReleaseExternal(void);
|
2012-01-06 06:44:25 -08:00
|
|
|
#else
|
2012-07-27 07:03:25 -07:00
|
|
|
virtual nsrefcnt AddRef(void);
|
|
|
|
virtual nsrefcnt Release(void);
|
2012-01-06 06:44:25 -08:00
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
public:
|
|
|
|
|
2010-06-18 02:21:15 -07:00
|
|
|
/** Wrap the given cairo surface and return a gfxASurface for it.
|
|
|
|
* This adds a reference to csurf (owned by the returned gfxASurface).
|
|
|
|
*/
|
2014-06-02 22:18:25 -07:00
|
|
|
static already_AddRefed<gfxASurface> Wrap(cairo_surface_t *csurf, const gfxIntSize& aSize = gfxIntSize(-1, -1));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*** this DOES NOT addref the surface */
|
2007-05-10 12:58:09 -07:00
|
|
|
cairo_surface_t *CairoSurface() {
|
|
|
|
return mSurface;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
gfxSurfaceType GetType() const;
|
|
|
|
|
|
|
|
gfxContentType GetContentType() const;
|
|
|
|
|
|
|
|
void SetDeviceOffset(const gfxPoint& offset);
|
|
|
|
gfxPoint GetDeviceOffset() const;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual bool GetRotateForLandscape() { return false; }
|
2011-07-14 10:02:20 -07:00
|
|
|
|
2010-08-08 19:19:17 -07:00
|
|
|
void Flush() const;
|
2007-03-22 10:30:00 -07:00
|
|
|
void MarkDirty();
|
|
|
|
void MarkDirty(const gfxRect& r);
|
|
|
|
|
|
|
|
/* Printing backend functions */
|
2007-09-25 13:46:08 -07:00
|
|
|
virtual nsresult BeginPrinting(const nsAString& aTitle, const nsAString& aPrintToFileName);
|
|
|
|
virtual nsresult EndPrinting();
|
|
|
|
virtual nsresult AbortPrinting();
|
|
|
|
virtual nsresult BeginPage();
|
|
|
|
virtual nsresult EndPage();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void SetData(const cairo_user_data_key_t *key,
|
|
|
|
void *user_data,
|
|
|
|
thebes_destroy_func_t destroy);
|
|
|
|
void *GetData(const cairo_user_data_key_t *key);
|
|
|
|
|
|
|
|
virtual void Finish();
|
|
|
|
|
2010-05-31 16:37:44 -07:00
|
|
|
/**
|
|
|
|
* Create an offscreen surface that can be efficiently copied into
|
|
|
|
* this surface (at least if tiling is not involved).
|
|
|
|
* Returns null on error.
|
|
|
|
*/
|
|
|
|
virtual already_AddRefed<gfxASurface> CreateSimilarSurface(gfxContentType aType,
|
2013-09-24 13:45:13 -07:00
|
|
|
const nsIntSize& aSize);
|
2010-11-11 12:31:22 -08:00
|
|
|
|
|
|
|
/**
|
2012-07-30 07:20:58 -07:00
|
|
|
* Returns an image surface for this surface, or nullptr if not supported.
|
2010-11-11 12:31:22 -08:00
|
|
|
* This will not copy image data, just wraps an image surface around
|
|
|
|
* pixel data already available in memory.
|
|
|
|
*/
|
2013-09-24 13:45:13 -07:00
|
|
|
virtual already_AddRefed<gfxImageSurface> GetAsImageSurface();
|
2010-11-11 12:31:22 -08:00
|
|
|
|
2013-03-05 15:39:48 -08:00
|
|
|
/**
|
|
|
|
* Returns a read-only ARGB32 image surface for this surface. If this is an
|
|
|
|
* optimized surface this may require a copy.
|
|
|
|
* Returns null on error.
|
|
|
|
*/
|
|
|
|
virtual already_AddRefed<gfxImageSurface> GetAsReadableARGB32ImageSurface();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new ARGB32 image surface with the same contents as this surface.
|
|
|
|
* Returns null on error.
|
|
|
|
*/
|
|
|
|
already_AddRefed<gfxImageSurface> CopyToARGB32ImageSurface();
|
|
|
|
|
2007-05-10 13:50:16 -07:00
|
|
|
int CairoStatus();
|
2007-05-10 12:58:09 -07:00
|
|
|
|
|
|
|
/* Make sure that the given dimensions don't overflow a 32-bit signed int
|
|
|
|
* using 4 bytes per pixel; optionally, make sure that either dimension
|
|
|
|
* doesn't exceed the given limit.
|
|
|
|
*/
|
2013-09-24 13:45:13 -07:00
|
|
|
static bool CheckSurfaceSize(const nsIntSize& sz, int32_t limit = 0);
|
2007-05-10 12:58:09 -07:00
|
|
|
|
2011-03-15 15:01:00 -07:00
|
|
|
/* Provide a stride value that will respect all alignment requirements of
|
|
|
|
* the accelerated image-rendering code.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
static int32_t FormatStrideForWidth(gfxImageFormat format, int32_t width);
|
2011-03-15 15:01:00 -07:00
|
|
|
|
2008-04-28 18:15:30 -07:00
|
|
|
static gfxContentType ContentFromFormat(gfxImageFormat format);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
void SetSubpixelAntialiasingEnabled(bool aEnabled);
|
|
|
|
bool GetSubpixelAntialiasingEnabled();
|
2011-01-02 17:48:08 -08:00
|
|
|
|
2010-05-21 21:10:14 -07:00
|
|
|
/**
|
|
|
|
* Record number of bytes for given surface type. Use positive bytes
|
|
|
|
* for allocations and negative bytes for deallocations.
|
|
|
|
*/
|
2013-09-24 13:45:13 -07:00
|
|
|
static void RecordMemoryUsedForSurfaceType(gfxSurfaceType aType,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aBytes);
|
2010-05-21 21:10:14 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Same as above, but use current surface type as returned by GetType().
|
|
|
|
* The bytes will be accumulated until RecordMemoryFreed is called,
|
|
|
|
* in which case the value that was recorded for this surface will
|
|
|
|
* be freed.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
void RecordMemoryUsed(int32_t aBytes);
|
2010-05-21 21:10:14 -07:00
|
|
|
void RecordMemoryFreed();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
virtual int32_t KnownMemoryUsed() { return mBytesRecorded; }
|
2011-07-18 06:20:27 -07:00
|
|
|
|
2013-06-23 05:03:39 -07:00
|
|
|
virtual size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
|
|
|
virtual size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
2012-12-18 21:59:30 -08:00
|
|
|
// gfxASurface has many sub-classes. This method indicates if a sub-class
|
|
|
|
// is capable of measuring its own size accurately. If not, the caller
|
|
|
|
// must fall back to a computed size. (Note that gfxASurface can actually
|
|
|
|
// measure itself, but we must |return false| here because it serves as the
|
|
|
|
// (conservative) default for all the sub-classes. Therefore, this
|
|
|
|
// function should only be called on a |gfxASurface*| that actually points
|
|
|
|
// to a sub-class of gfxASurface.)
|
|
|
|
virtual bool SizeOfIsMeasured() const { return false; }
|
2012-11-26 16:29:56 -08:00
|
|
|
|
2011-07-18 06:20:27 -07:00
|
|
|
/**
|
|
|
|
* Where does this surface's memory live? By default, we say it's in this
|
|
|
|
* process's heap.
|
|
|
|
*/
|
2013-09-24 13:45:14 -07:00
|
|
|
virtual gfxMemoryLocation GetMemoryLocation() const;
|
2010-05-21 21:10:14 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static int32_t BytePerPixelFromFormat(gfxImageFormat format);
|
2010-06-11 02:33:22 -07:00
|
|
|
|
2013-09-24 13:45:13 -07:00
|
|
|
virtual const nsIntSize GetSize() const;
|
2010-09-07 17:36:57 -07:00
|
|
|
|
2013-09-24 13:45:13 -07:00
|
|
|
void SetOpaqueRect(const gfxRect& aRect);
|
|
|
|
|
2011-01-02 17:48:08 -08:00
|
|
|
const gfxRect& GetOpaqueRect() {
|
2013-09-24 13:45:13 -07:00
|
|
|
if (!!mOpaqueRect)
|
2011-01-02 17:48:08 -08:00
|
|
|
return *mOpaqueRect;
|
2013-09-24 13:45:13 -07:00
|
|
|
return GetEmptyOpaqueRect();
|
2011-01-02 17:48:08 -08:00
|
|
|
}
|
|
|
|
|
2011-01-02 17:48:09 -08:00
|
|
|
/**
|
|
|
|
* Mark the surface as being allowed/not allowed to be used as a source.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
void SetAllowUseAsSource(bool aAllow) { mAllowUseAsSource = aAllow; }
|
|
|
|
bool GetAllowUseAsSource() { return mAllowUseAsSource; }
|
2011-01-02 17:48:09 -08:00
|
|
|
|
2012-12-12 14:47:28 -08:00
|
|
|
static uint8_t BytesPerPixel(gfxImageFormat aImageFormat);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
protected:
|
2013-09-24 13:45:13 -07:00
|
|
|
gfxASurface();
|
2007-05-10 12:58:09 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static gfxASurface* GetSurfaceWrapper(cairo_surface_t *csurf);
|
|
|
|
static void SetSurfaceWrapper(cairo_surface_t *csurf, gfxASurface *asurf);
|
|
|
|
|
2011-01-04 08:40:54 -08:00
|
|
|
// NB: Init() *must* be called from within subclass's
|
|
|
|
// constructors. It's unsafe to call it after the ctor finishes;
|
|
|
|
// leaks and use-after-frees are possible.
|
2011-09-28 23:19:26 -07:00
|
|
|
void Init(cairo_surface_t *surface, bool existingSurface = false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-09-24 13:45:13 -07:00
|
|
|
// out-of-line helper to allow GetOpaqueRect() to be inlined
|
|
|
|
// without including gfxRect.h here
|
|
|
|
static const gfxRect& GetEmptyOpaqueRect();
|
2010-06-18 02:21:15 -07:00
|
|
|
|
2013-09-24 13:45:13 -07:00
|
|
|
virtual ~gfxASurface();
|
2010-05-21 21:10:14 -07:00
|
|
|
|
2010-05-31 16:37:44 -07:00
|
|
|
cairo_surface_t *mSurface;
|
2014-01-06 07:49:03 -08:00
|
|
|
mozilla::UniquePtr<gfxRect> mOpaqueRect;
|
2010-05-31 16:37:44 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
private:
|
2007-05-10 12:58:09 -07:00
|
|
|
static void SurfaceDestroyFunc(void *data);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mFloatingRefs;
|
|
|
|
int32_t mBytesRecorded;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-10 12:58:09 -07:00
|
|
|
protected:
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mSurfaceValid;
|
|
|
|
bool mAllowUseAsSource;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An Unknown surface; used to wrap unknown cairo_surface_t returns from cairo
|
|
|
|
*/
|
2013-05-29 14:59:24 -07:00
|
|
|
class gfxUnknownSurface : public gfxASurface {
|
2007-03-22 10:30:00 -07:00
|
|
|
public:
|
2014-06-02 22:18:25 -07:00
|
|
|
gfxUnknownSurface(cairo_surface_t *surf, const gfxIntSize& aSize)
|
|
|
|
: mSize(aSize)
|
2014-05-27 17:59:10 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
Init(surf, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~gfxUnknownSurface() { }
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual const nsIntSize GetSize() const override { return mSize; }
|
2014-05-27 17:59:10 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsIntSize mSize;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* GFX_ASURFACE_H */
|