2006-04-05 01:54:22 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 15:12:37 +04: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/. */
|
2006-04-05 01:54:22 +04:00
|
|
|
|
2010-05-22 08:10:14 +04:00
|
|
|
#include "nsIMemoryReporter.h"
|
|
|
|
#include "nsMemory.h"
|
2014-01-30 22:26:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2012-12-03 00:33:59 +04:00
|
|
|
#include "mozilla/Base64.h"
|
2012-05-14 23:50:20 +04:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2012-06-13 08:14:28 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2014-02-27 01:36:35 +04:00
|
|
|
#include "nsISupportsImpl.h"
|
2013-10-16 05:00:31 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2014-05-08 03:23:44 +04:00
|
|
|
#include "gfx2DGlue.h"
|
2010-05-22 08:10:14 +04:00
|
|
|
|
2006-04-05 01:54:22 +04:00
|
|
|
#include "gfxASurface.h"
|
2011-03-09 20:27:37 +03:00
|
|
|
#include "gfxContext.h"
|
2006-04-05 01:54:22 +04:00
|
|
|
#include "gfxImageSurface.h"
|
2013-07-12 06:44:29 +04:00
|
|
|
#include "gfxPlatform.h"
|
2013-09-25 00:45:13 +04:00
|
|
|
#include "gfxRect.h"
|
2011-03-09 20:27:37 +03:00
|
|
|
|
2007-01-27 04:26:49 +03:00
|
|
|
#include "cairo.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2007-01-27 04:26:49 +03:00
|
|
|
|
2006-04-05 01:54:22 +04:00
|
|
|
#ifdef CAIRO_HAS_WIN32_SURFACE
|
|
|
|
#include "gfxWindowsSurface.h"
|
|
|
|
#endif
|
2010-02-26 09:36:07 +03:00
|
|
|
#ifdef CAIRO_HAS_D2D_SURFACE
|
|
|
|
#include "gfxD2DSurface.h"
|
|
|
|
#endif
|
2006-04-05 01:54:22 +04:00
|
|
|
|
2009-06-23 07:50:08 +04:00
|
|
|
#ifdef MOZ_X11
|
2006-04-05 01:54:22 +04:00
|
|
|
#include "gfxXlibSurface.h"
|
|
|
|
#endif
|
|
|
|
|
2007-04-04 05:09:15 +04:00
|
|
|
#ifdef CAIRO_HAS_QUARTZ_SURFACE
|
2006-04-05 01:54:22 +04:00
|
|
|
#include "gfxQuartzSurface.h"
|
2008-02-06 09:48:47 +03:00
|
|
|
#include "gfxQuartzImageSurface.h"
|
2006-04-05 01:54:22 +04:00
|
|
|
#endif
|
|
|
|
|
2010-09-21 03:02:00 +04:00
|
|
|
#if defined(CAIRO_HAS_QT_SURFACE) && defined(MOZ_WIDGET_QT)
|
2008-04-19 17:34:30 +04:00
|
|
|
#include "gfxQPainterSurface.h"
|
|
|
|
#endif
|
|
|
|
|
2007-01-27 04:26:49 +03:00
|
|
|
#include <stdio.h>
|
2007-10-24 10:01:16 +04:00
|
|
|
#include <limits.h>
|
2007-01-27 04:26:49 +03:00
|
|
|
|
2011-03-25 01:04:46 +03:00
|
|
|
#include "imgIEncoder.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsISupportsUtils.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-09-23 21:25:00 +04:00
|
|
|
#include "nsString.h"
|
2011-10-26 07:19:13 +04:00
|
|
|
#include "nsIClipboardHelper.h"
|
2011-03-25 01:04:46 +03:00
|
|
|
|
2012-12-03 00:33:59 +04:00
|
|
|
using namespace mozilla;
|
2013-10-16 05:00:31 +04:00
|
|
|
using namespace mozilla::gfx;
|
2011-04-12 23:22:45 +04:00
|
|
|
|
2006-04-05 01:54:22 +04:00
|
|
|
static cairo_user_data_key_t gfxasurface_pointer_key;
|
|
|
|
|
2013-09-25 00:45:13 +04:00
|
|
|
gfxASurface::gfxASurface()
|
|
|
|
: mSurface(nullptr), mFloatingRefs(0), mBytesRecorded(0),
|
|
|
|
mSurfaceValid(false), mAllowUseAsSource(true)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(gfxASurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxASurface::~gfxASurface()
|
|
|
|
{
|
|
|
|
RecordMemoryFreed();
|
|
|
|
|
|
|
|
MOZ_COUNT_DTOR(gfxASurface);
|
|
|
|
}
|
|
|
|
|
2007-01-27 04:26:49 +03:00
|
|
|
// Surfaces use refcounting that's tied to the cairo surface refcnt, to avoid
|
|
|
|
// refcount mismatch issues.
|
|
|
|
nsrefcnt
|
|
|
|
gfxASurface::AddRef(void)
|
|
|
|
{
|
2007-05-10 23:58:09 +04:00
|
|
|
if (mSurfaceValid) {
|
|
|
|
if (mFloatingRefs) {
|
|
|
|
// eat a floating ref
|
|
|
|
mFloatingRefs--;
|
|
|
|
} else {
|
|
|
|
cairo_surface_reference(mSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (nsrefcnt) cairo_surface_get_reference_count(mSurface);
|
2007-01-27 04:26:49 +03:00
|
|
|
} else {
|
2007-05-10 23:58:09 +04:00
|
|
|
// the surface isn't valid, but we still need to refcount
|
|
|
|
// the gfxASurface
|
|
|
|
return ++mFloatingRefs;
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsrefcnt
|
|
|
|
gfxASurface::Release(void)
|
|
|
|
{
|
2007-05-10 23:58:09 +04:00
|
|
|
if (mSurfaceValid) {
|
|
|
|
NS_ASSERTION(mFloatingRefs == 0, "gfxASurface::Release with floating refs still hanging around!");
|
|
|
|
|
|
|
|
// Note that there is a destructor set on user data for mSurface,
|
|
|
|
// which will delete this gfxASurface wrapper when the surface's refcount goes
|
|
|
|
// out of scope.
|
|
|
|
nsrefcnt refcnt = (nsrefcnt) cairo_surface_get_reference_count(mSurface);
|
|
|
|
cairo_surface_destroy(mSurface);
|
|
|
|
|
|
|
|
// |this| may not be valid any more, don't use it!
|
|
|
|
|
|
|
|
return --refcnt;
|
|
|
|
} else {
|
|
|
|
if (--mFloatingRefs == 0) {
|
|
|
|
delete this;
|
|
|
|
return 0;
|
|
|
|
}
|
2007-01-27 04:26:49 +03:00
|
|
|
|
2007-05-10 23:58:09 +04:00
|
|
|
return mFloatingRefs;
|
|
|
|
}
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
|
|
|
|
2013-09-25 00:45:13 +04:00
|
|
|
nsrefcnt
|
|
|
|
gfxASurface::AddRefExternal(void)
|
|
|
|
{
|
|
|
|
return AddRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsrefcnt
|
|
|
|
gfxASurface::ReleaseExternal(void)
|
|
|
|
{
|
|
|
|
return Release();
|
|
|
|
}
|
|
|
|
|
2007-01-18 01:20:16 +03:00
|
|
|
void
|
|
|
|
gfxASurface::SurfaceDestroyFunc(void *data) {
|
|
|
|
gfxASurface *surf = (gfxASurface*) data;
|
|
|
|
// fprintf (stderr, "Deleting wrapper for %p (wrapper: %p)\n", surf->mSurface, data);
|
|
|
|
delete surf;
|
|
|
|
}
|
|
|
|
|
2006-04-05 01:54:22 +04:00
|
|
|
gfxASurface*
|
|
|
|
gfxASurface::GetSurfaceWrapper(cairo_surface_t *csurf)
|
|
|
|
{
|
2011-07-22 07:41:39 +04:00
|
|
|
if (!csurf)
|
2013-07-31 19:44:31 +04:00
|
|
|
return nullptr;
|
2006-04-05 01:54:22 +04:00
|
|
|
return (gfxASurface*) cairo_surface_get_user_data(csurf, &gfxasurface_pointer_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxASurface::SetSurfaceWrapper(cairo_surface_t *csurf, gfxASurface *asurf)
|
|
|
|
{
|
2011-07-22 07:41:39 +04:00
|
|
|
if (!csurf)
|
|
|
|
return;
|
2007-01-18 01:20:16 +03:00
|
|
|
cairo_surface_set_user_data(csurf, &gfxasurface_pointer_key, asurf, SurfaceDestroyFunc);
|
2006-04-05 01:54:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxASurface>
|
2014-06-03 09:18:25 +04:00
|
|
|
gfxASurface::Wrap (cairo_surface_t *csurf, const gfxIntSize& aSize)
|
2006-04-05 01:54:22 +04:00
|
|
|
{
|
2013-04-22 15:15:59 +04:00
|
|
|
nsRefPtr<gfxASurface> result;
|
2006-04-05 01:54:22 +04:00
|
|
|
|
|
|
|
/* Do we already have a wrapper for this surface? */
|
|
|
|
result = GetSurfaceWrapper(csurf);
|
|
|
|
if (result) {
|
2007-01-18 01:20:16 +03:00
|
|
|
// fprintf(stderr, "Existing wrapper for %p -> %p\n", csurf, result);
|
2013-04-22 15:15:59 +04:00
|
|
|
return result.forget();
|
2006-04-05 01:54:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No wrapper; figure out the surface type and create it */
|
|
|
|
cairo_surface_type_t stype = cairo_surface_get_type(csurf);
|
|
|
|
|
|
|
|
if (stype == CAIRO_SURFACE_TYPE_IMAGE) {
|
|
|
|
result = new gfxImageSurface(csurf);
|
|
|
|
}
|
|
|
|
#ifdef CAIRO_HAS_WIN32_SURFACE
|
2007-09-25 02:50:44 +04:00
|
|
|
else if (stype == CAIRO_SURFACE_TYPE_WIN32 ||
|
|
|
|
stype == CAIRO_SURFACE_TYPE_WIN32_PRINTING) {
|
2006-04-05 01:54:22 +04:00
|
|
|
result = new gfxWindowsSurface(csurf);
|
|
|
|
}
|
|
|
|
#endif
|
2010-02-26 09:36:07 +03:00
|
|
|
#ifdef CAIRO_HAS_D2D_SURFACE
|
|
|
|
else if (stype == CAIRO_SURFACE_TYPE_D2D) {
|
|
|
|
result = new gfxD2DSurface(csurf);
|
|
|
|
}
|
|
|
|
#endif
|
2009-06-23 07:50:08 +04:00
|
|
|
#ifdef MOZ_X11
|
2006-04-05 01:54:22 +04:00
|
|
|
else if (stype == CAIRO_SURFACE_TYPE_XLIB) {
|
|
|
|
result = new gfxXlibSurface(csurf);
|
|
|
|
}
|
|
|
|
#endif
|
2007-04-04 05:09:15 +04:00
|
|
|
#ifdef CAIRO_HAS_QUARTZ_SURFACE
|
|
|
|
else if (stype == CAIRO_SURFACE_TYPE_QUARTZ) {
|
2014-06-03 09:18:25 +04:00
|
|
|
result = new gfxQuartzSurface(csurf, aSize);
|
2006-04-05 01:54:22 +04:00
|
|
|
}
|
2008-02-06 09:48:47 +03:00
|
|
|
else if (stype == CAIRO_SURFACE_TYPE_QUARTZ_IMAGE) {
|
|
|
|
result = new gfxQuartzImageSurface(csurf);
|
|
|
|
}
|
2008-04-19 17:34:30 +04:00
|
|
|
#endif
|
2010-09-21 03:02:00 +04:00
|
|
|
#if defined(CAIRO_HAS_QT_SURFACE) && defined(MOZ_WIDGET_QT)
|
2009-09-26 19:51:08 +04:00
|
|
|
else if (stype == CAIRO_SURFACE_TYPE_QT) {
|
2008-04-19 17:34:30 +04:00
|
|
|
result = new gfxQPainterSurface(csurf);
|
|
|
|
}
|
2006-04-05 01:54:22 +04:00
|
|
|
#endif
|
|
|
|
else {
|
2014-06-03 09:18:25 +04:00
|
|
|
result = new gfxUnknownSurface(csurf, aSize);
|
2006-04-05 01:54:22 +04:00
|
|
|
}
|
|
|
|
|
2007-01-18 01:20:16 +03:00
|
|
|
// fprintf(stderr, "New wrapper for %p -> %p\n", csurf, result);
|
2006-04-05 01:54:22 +04:00
|
|
|
|
2013-04-22 15:15:59 +04:00
|
|
|
return result.forget();
|
2006-04-05 01:54:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-29 10:19:26 +04:00
|
|
|
gfxASurface::Init(cairo_surface_t* surface, bool existingSurface)
|
2006-04-05 01:54:22 +04:00
|
|
|
{
|
2007-01-18 01:20:16 +03:00
|
|
|
SetSurfaceWrapper(surface, this);
|
2006-04-05 01:54:22 +04:00
|
|
|
|
|
|
|
mSurface = surface;
|
2011-09-13 18:49:01 +04:00
|
|
|
mSurfaceValid = surface && !cairo_surface_status(surface);
|
2006-04-05 01:54:22 +04:00
|
|
|
|
2011-09-13 18:49:01 +04:00
|
|
|
if (existingSurface || !mSurfaceValid) {
|
2007-05-10 23:58:09 +04:00
|
|
|
mFloatingRefs = 0;
|
2007-01-18 01:20:16 +03:00
|
|
|
} else {
|
2007-05-10 23:58:09 +04:00
|
|
|
mFloatingRefs = 1;
|
2011-02-07 21:52:40 +03:00
|
|
|
#ifdef MOZ_TREE_CAIRO
|
2011-01-03 04:48:08 +03:00
|
|
|
if (cairo_surface_get_content(surface) != CAIRO_CONTENT_COLOR) {
|
|
|
|
cairo_surface_set_subpixel_antialiasing(surface, CAIRO_SUBPIXEL_ANTIALIASING_DISABLED);
|
|
|
|
}
|
2011-02-07 21:52:40 +03:00
|
|
|
#endif
|
2006-04-05 01:54:22 +04:00
|
|
|
}
|
|
|
|
}
|
2007-01-27 04:26:49 +03:00
|
|
|
|
2013-09-25 00:45:13 +04:00
|
|
|
gfxSurfaceType
|
2007-01-27 04:26:49 +03:00
|
|
|
gfxASurface::GetType() const
|
|
|
|
{
|
2007-08-09 22:54:17 +04:00
|
|
|
if (!mSurfaceValid)
|
|
|
|
return (gfxSurfaceType)-1;
|
|
|
|
|
2007-01-27 04:26:49 +03:00
|
|
|
return (gfxSurfaceType)cairo_surface_get_type(mSurface);
|
|
|
|
}
|
|
|
|
|
2013-09-25 00:45:13 +04:00
|
|
|
gfxContentType
|
2007-01-27 04:26:49 +03:00
|
|
|
gfxASurface::GetContentType() const
|
|
|
|
{
|
2007-08-09 22:54:17 +04:00
|
|
|
if (!mSurfaceValid)
|
|
|
|
return (gfxContentType)-1;
|
|
|
|
|
2007-01-27 04:26:49 +03:00
|
|
|
return (gfxContentType)cairo_surface_get_content(mSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-02-08 23:47:48 +03:00
|
|
|
gfxASurface::SetDeviceOffset(const gfxPoint& offset)
|
2007-01-27 04:26:49 +03:00
|
|
|
{
|
2011-07-22 07:41:39 +04:00
|
|
|
if (!mSurfaceValid)
|
|
|
|
return;
|
2007-01-27 04:26:49 +03:00
|
|
|
cairo_surface_set_device_offset(mSurface,
|
|
|
|
offset.x, offset.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxPoint
|
|
|
|
gfxASurface::GetDeviceOffset() const
|
|
|
|
{
|
2011-07-22 07:41:39 +04:00
|
|
|
if (!mSurfaceValid)
|
|
|
|
return gfxPoint(0.0, 0.0);
|
2007-01-27 04:26:49 +03:00
|
|
|
gfxPoint pt;
|
|
|
|
cairo_surface_get_device_offset(mSurface, &pt.x, &pt.y);
|
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-08-09 06:19:17 +04:00
|
|
|
gfxASurface::Flush() const
|
2007-01-27 04:26:49 +03:00
|
|
|
{
|
2011-07-22 07:41:39 +04:00
|
|
|
if (!mSurfaceValid)
|
|
|
|
return;
|
2007-01-27 04:26:49 +03:00
|
|
|
cairo_surface_flush(mSurface);
|
2013-07-12 06:44:29 +04:00
|
|
|
gfxPlatform::ClearSourceSurfaceForSurface(const_cast<gfxASurface*>(this));
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxASurface::MarkDirty()
|
|
|
|
{
|
2011-07-22 07:41:39 +04:00
|
|
|
if (!mSurfaceValid)
|
|
|
|
return;
|
2007-01-27 04:26:49 +03:00
|
|
|
cairo_surface_mark_dirty(mSurface);
|
2013-07-12 06:44:29 +04:00
|
|
|
gfxPlatform::ClearSourceSurfaceForSurface(this);
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxASurface::MarkDirty(const gfxRect& r)
|
|
|
|
{
|
2011-07-22 07:41:39 +04:00
|
|
|
if (!mSurfaceValid)
|
|
|
|
return;
|
2007-01-27 04:26:49 +03:00
|
|
|
cairo_surface_mark_dirty_rectangle(mSurface,
|
2011-04-19 07:07:21 +04:00
|
|
|
(int) r.X(), (int) r.Y(),
|
|
|
|
(int) r.Width(), (int) r.Height());
|
2013-07-12 06:44:29 +04:00
|
|
|
gfxPlatform::ClearSourceSurfaceForSurface(this);
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxASurface::SetData(const cairo_user_data_key_t *key,
|
|
|
|
void *user_data,
|
2007-01-29 10:25:05 +03:00
|
|
|
thebes_destroy_func_t destroy)
|
2007-01-27 04:26:49 +03:00
|
|
|
{
|
2011-07-22 07:41:39 +04:00
|
|
|
if (!mSurfaceValid)
|
|
|
|
return;
|
2007-01-27 04:26:49 +03:00
|
|
|
cairo_surface_set_user_data(mSurface, key, user_data, destroy);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
gfxASurface::GetData(const cairo_user_data_key_t *key)
|
|
|
|
{
|
2011-07-22 07:41:39 +04:00
|
|
|
if (!mSurfaceValid)
|
2013-07-31 19:44:31 +04:00
|
|
|
return nullptr;
|
2007-01-27 04:26:49 +03:00
|
|
|
return cairo_surface_get_user_data(mSurface, key);
|
|
|
|
}
|
2007-01-27 04:46:14 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
gfxASurface::Finish()
|
|
|
|
{
|
2011-07-22 07:41:39 +04:00
|
|
|
// null surfaces are allowed here
|
2007-01-27 04:46:14 +03:00
|
|
|
cairo_surface_finish(mSurface);
|
|
|
|
}
|
2007-05-10 23:58:09 +04:00
|
|
|
|
2010-06-01 03:37:44 +04:00
|
|
|
already_AddRefed<gfxASurface>
|
|
|
|
gfxASurface::CreateSimilarSurface(gfxContentType aContent,
|
2013-09-25 00:45:13 +04:00
|
|
|
const nsIntSize& aSize)
|
2010-06-01 03:37:44 +04:00
|
|
|
{
|
2010-12-16 11:07:20 +03:00
|
|
|
if (!mSurface || !mSurfaceValid) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-12-16 11:07:20 +03:00
|
|
|
}
|
|
|
|
|
2010-06-01 03:37:44 +04:00
|
|
|
cairo_surface_t *surface =
|
2014-01-23 22:26:40 +04:00
|
|
|
cairo_surface_create_similar(mSurface, cairo_content_t(int(aContent)),
|
2010-06-01 03:37:44 +04:00
|
|
|
aSize.width, aSize.height);
|
|
|
|
if (cairo_surface_status(surface)) {
|
|
|
|
cairo_surface_destroy(surface);
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-06-01 03:37:44 +04:00
|
|
|
}
|
|
|
|
|
2014-06-03 09:18:25 +04:00
|
|
|
nsRefPtr<gfxASurface> result = Wrap(surface, aSize);
|
2010-06-18 13:21:15 +04:00
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
return result.forget();
|
2010-06-01 03:37:44 +04:00
|
|
|
}
|
|
|
|
|
2013-03-06 03:39:48 +04:00
|
|
|
already_AddRefed<gfxImageSurface>
|
|
|
|
gfxASurface::GetAsReadableARGB32ImageSurface()
|
|
|
|
{
|
|
|
|
nsRefPtr<gfxImageSurface> imgSurface = GetAsImageSurface();
|
2014-01-23 22:26:40 +04:00
|
|
|
if (!imgSurface || imgSurface->Format() != gfxImageFormat::ARGB32) {
|
2013-03-06 03:39:48 +04:00
|
|
|
imgSurface = CopyToARGB32ImageSurface();
|
|
|
|
}
|
|
|
|
return imgSurface.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxImageSurface>
|
|
|
|
gfxASurface::CopyToARGB32ImageSurface()
|
|
|
|
{
|
|
|
|
if (!mSurface || !mSurfaceValid) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-09-25 00:45:13 +04:00
|
|
|
const nsIntSize size = GetSize();
|
2013-03-06 03:39:48 +04:00
|
|
|
nsRefPtr<gfxImageSurface> imgSurface =
|
2014-01-23 22:26:40 +04:00
|
|
|
new gfxImageSurface(size, gfxImageFormat::ARGB32);
|
2013-03-06 03:39:48 +04:00
|
|
|
|
2014-03-19 06:36:58 +04:00
|
|
|
RefPtr<DrawTarget> dt = gfxPlatform::GetPlatform()->CreateDrawTargetForSurface(imgSurface, IntSize(size.width, size.height));
|
|
|
|
RefPtr<SourceSurface> source = gfxPlatform::GetPlatform()->GetSourceSurfaceForSurface(dt, this);
|
2013-10-16 05:00:31 +04:00
|
|
|
|
2014-03-19 06:36:58 +04:00
|
|
|
dt->CopySurface(source, IntRect(0, 0, size.width, size.height), IntPoint());
|
2013-03-06 03:39:48 +04:00
|
|
|
|
|
|
|
return imgSurface.forget();
|
|
|
|
}
|
|
|
|
|
2007-05-10 23:58:09 +04:00
|
|
|
int
|
2007-05-11 00:50:16 +04:00
|
|
|
gfxASurface::CairoStatus()
|
2007-05-10 23:58:09 +04:00
|
|
|
{
|
|
|
|
if (!mSurfaceValid)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return cairo_surface_status(mSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-09-25 00:45:13 +04:00
|
|
|
gfxASurface::CheckSurfaceSize(const nsIntSize& sz, int32_t limit)
|
2007-05-10 23:58:09 +04:00
|
|
|
{
|
2007-05-16 00:27:10 +04:00
|
|
|
if (sz.width < 0 || sz.height < 0) {
|
2007-07-03 13:16:50 +04:00
|
|
|
NS_WARNING("Surface width or height < 0!");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-05-16 00:27:10 +04:00
|
|
|
}
|
2007-05-10 23:58:09 +04:00
|
|
|
|
2011-04-12 23:22:45 +04:00
|
|
|
// reject images with sides bigger than limit
|
|
|
|
if (limit && (sz.width > limit || sz.height > limit)) {
|
|
|
|
NS_WARNING("Surface size too large (exceeds caller's limit)!");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-04-12 23:22:45 +04:00
|
|
|
}
|
|
|
|
|
2007-10-24 10:01:16 +04:00
|
|
|
#if defined(XP_MACOSX)
|
2011-04-12 23:22:45 +04:00
|
|
|
// CoreGraphics is limited to images < 32K in *height*,
|
|
|
|
// so clamp all surfaces on the Mac to that height
|
2007-10-24 10:01:16 +04:00
|
|
|
if (sz.height > SHRT_MAX) {
|
2011-04-12 23:22:45 +04:00
|
|
|
NS_WARNING("Surface size too large (exceeds CoreGraphics limit)!");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-10-24 10:01:16 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
// make sure the surface area doesn't overflow a int32_t
|
|
|
|
CheckedInt<int32_t> tmp = sz.width;
|
2011-04-12 23:22:45 +04:00
|
|
|
tmp *= sz.height;
|
2012-05-14 23:50:20 +04:00
|
|
|
if (!tmp.isValid()) {
|
2007-07-03 13:16:50 +04:00
|
|
|
NS_WARNING("Surface size too large (would overflow)!");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-05-16 00:27:10 +04:00
|
|
|
}
|
2007-05-10 23:58:09 +04:00
|
|
|
|
2011-04-12 23:22:45 +04:00
|
|
|
// assuming 4-byte stride, make sure the allocation size
|
2012-08-22 19:56:38 +04:00
|
|
|
// doesn't overflow a int32_t either
|
2011-04-12 23:22:45 +04:00
|
|
|
tmp *= 4;
|
2012-05-14 23:50:20 +04:00
|
|
|
if (!tmp.isValid()) {
|
2011-04-12 23:22:45 +04:00
|
|
|
NS_WARNING("Allocation too large (would overflow)!");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-05-16 00:27:10 +04:00
|
|
|
}
|
2007-05-10 23:58:09 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2007-05-10 23:58:09 +04:00
|
|
|
}
|
2007-09-26 00:46:08 +04:00
|
|
|
|
2011-03-16 01:01:00 +03:00
|
|
|
/* static */
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
|
|
|
gfxASurface::FormatStrideForWidth(gfxImageFormat format, int32_t width)
|
2011-03-16 01:01:00 +03:00
|
|
|
{
|
2014-01-23 22:26:40 +04:00
|
|
|
return cairo_format_stride_for_width((cairo_format_t)(int)format, (int)width);
|
2011-03-16 01:01:00 +03:00
|
|
|
}
|
|
|
|
|
2007-09-26 00:46:08 +04:00
|
|
|
nsresult
|
|
|
|
gfxASurface::BeginPrinting(const nsAString& aTitle, const nsAString& aPrintToFileName)
|
|
|
|
{
|
2008-03-28 10:13:37 +03:00
|
|
|
return NS_OK;
|
2007-09-26 00:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
gfxASurface::EndPrinting()
|
|
|
|
{
|
2008-03-28 10:13:37 +03:00
|
|
|
return NS_OK;
|
2007-09-26 00:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
gfxASurface::AbortPrinting()
|
|
|
|
{
|
2008-03-28 10:13:37 +03:00
|
|
|
return NS_OK;
|
2007-09-26 00:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
gfxASurface::BeginPage()
|
|
|
|
{
|
2008-03-28 10:13:37 +03:00
|
|
|
return NS_OK;
|
2007-09-26 00:46:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
gfxASurface::EndPage()
|
|
|
|
{
|
2008-03-28 10:13:37 +03:00
|
|
|
return NS_OK;
|
2007-09-26 00:46:08 +04:00
|
|
|
}
|
2008-04-29 05:15:30 +04:00
|
|
|
|
2013-09-25 00:45:13 +04:00
|
|
|
gfxContentType
|
2008-04-29 05:15:30 +04:00
|
|
|
gfxASurface::ContentFromFormat(gfxImageFormat format)
|
|
|
|
{
|
|
|
|
switch (format) {
|
2014-01-23 22:26:40 +04:00
|
|
|
case gfxImageFormat::ARGB32:
|
2014-01-23 22:26:40 +04:00
|
|
|
return gfxContentType::COLOR_ALPHA;
|
2014-01-23 22:26:40 +04:00
|
|
|
case gfxImageFormat::RGB24:
|
|
|
|
case gfxImageFormat::RGB16_565:
|
2014-01-23 22:26:40 +04:00
|
|
|
return gfxContentType::COLOR;
|
2014-01-23 22:26:40 +04:00
|
|
|
case gfxImageFormat::A8:
|
|
|
|
case gfxImageFormat::A1:
|
2014-01-23 22:26:40 +04:00
|
|
|
return gfxContentType::ALPHA;
|
2013-09-25 00:45:13 +04:00
|
|
|
|
2014-01-23 22:26:40 +04:00
|
|
|
case gfxImageFormat::Unknown:
|
2008-04-29 05:15:30 +04:00
|
|
|
default:
|
2014-01-23 22:26:40 +04:00
|
|
|
return gfxContentType::COLOR;
|
2008-04-29 05:15:30 +04:00
|
|
|
}
|
|
|
|
}
|
2010-05-22 08:10:14 +04:00
|
|
|
|
2011-01-03 04:48:08 +03:00
|
|
|
void
|
2011-09-29 10:19:26 +04:00
|
|
|
gfxASurface::SetSubpixelAntialiasingEnabled(bool aEnabled)
|
2011-01-03 04:48:08 +03:00
|
|
|
{
|
2011-02-07 21:52:40 +03:00
|
|
|
#ifdef MOZ_TREE_CAIRO
|
2011-01-03 04:48:08 +03:00
|
|
|
if (!mSurfaceValid)
|
|
|
|
return;
|
|
|
|
cairo_surface_set_subpixel_antialiasing(mSurface,
|
|
|
|
aEnabled ? CAIRO_SUBPIXEL_ANTIALIASING_ENABLED : CAIRO_SUBPIXEL_ANTIALIASING_DISABLED);
|
2011-02-07 21:52:40 +03:00
|
|
|
#endif
|
2011-01-03 04:48:08 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2011-01-03 04:48:08 +03:00
|
|
|
gfxASurface::GetSubpixelAntialiasingEnabled()
|
|
|
|
{
|
|
|
|
if (!mSurfaceValid)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-02-07 21:52:40 +03:00
|
|
|
#ifdef MOZ_TREE_CAIRO
|
2011-01-03 04:48:08 +03:00
|
|
|
return cairo_surface_get_subpixel_antialiasing(mSurface) == CAIRO_SUBPIXEL_ANTIALIASING_ENABLED;
|
2011-02-07 21:52:40 +03:00
|
|
|
#else
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-02-07 21:52:40 +03:00
|
|
|
#endif
|
2011-01-03 04:48:08 +03:00
|
|
|
}
|
|
|
|
|
2013-09-25 00:45:13 +04:00
|
|
|
gfxMemoryLocation
|
2011-07-18 17:20:27 +04:00
|
|
|
gfxASurface::GetMemoryLocation() const
|
|
|
|
{
|
2014-01-23 22:26:40 +04:00
|
|
|
return gfxMemoryLocation::IN_PROCESS_HEAP;
|
2011-07-18 17:20:27 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2010-06-11 13:33:22 +04:00
|
|
|
gfxASurface::BytePerPixelFromFormat(gfxImageFormat format)
|
|
|
|
{
|
|
|
|
switch (format) {
|
2014-01-23 22:26:40 +04:00
|
|
|
case gfxImageFormat::ARGB32:
|
|
|
|
case gfxImageFormat::RGB24:
|
2010-06-11 13:33:22 +04:00
|
|
|
return 4;
|
2014-01-23 22:26:40 +04:00
|
|
|
case gfxImageFormat::RGB16_565:
|
2010-06-11 13:33:22 +04:00
|
|
|
return 2;
|
2014-01-23 22:26:40 +04:00
|
|
|
case gfxImageFormat::A8:
|
2010-06-11 13:33:22 +04:00
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
NS_WARNING("Unknown byte per pixel value for Image format");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-22 08:10:14 +04:00
|
|
|
/** Memory reporting **/
|
|
|
|
|
2011-07-18 17:20:27 +04:00
|
|
|
static const char *sDefaultSurfaceDescription =
|
|
|
|
"Memory used by gfx surface of the given type.";
|
|
|
|
|
|
|
|
struct SurfaceMemoryReporterAttrs {
|
2012-03-19 01:59:17 +04:00
|
|
|
const char *path;
|
2011-07-18 17:20:27 +04:00
|
|
|
const char *description;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SurfaceMemoryReporterAttrs sSurfaceMemoryReporterAttrs[] = {
|
2012-07-30 18:20:58 +04:00
|
|
|
{"gfx-surface-image", nullptr},
|
|
|
|
{"gfx-surface-pdf", nullptr},
|
|
|
|
{"gfx-surface-ps", nullptr},
|
2011-07-18 17:20:27 +04:00
|
|
|
{"gfx-surface-xlib",
|
|
|
|
"Memory used by xlib surfaces to store pixmaps. This memory lives in "
|
|
|
|
"the X server's process rather than in this application, so the bytes "
|
|
|
|
"accounted for here aren't counted in vsize, resident, explicit, or any of "
|
|
|
|
"the other measurements on this page."},
|
2012-07-30 18:20:58 +04:00
|
|
|
{"gfx-surface-xcb", nullptr},
|
|
|
|
{"gfx-surface-glitz???", nullptr}, // should never be used
|
|
|
|
{"gfx-surface-quartz", nullptr},
|
|
|
|
{"gfx-surface-win32", nullptr},
|
|
|
|
{"gfx-surface-beos", nullptr},
|
|
|
|
{"gfx-surface-directfb???", nullptr}, // should never be used
|
|
|
|
{"gfx-surface-svg", nullptr},
|
|
|
|
{"gfx-surface-os2", nullptr},
|
|
|
|
{"gfx-surface-win32printing", nullptr},
|
|
|
|
{"gfx-surface-quartzimage", nullptr},
|
|
|
|
{"gfx-surface-script", nullptr},
|
|
|
|
{"gfx-surface-qpainter", nullptr},
|
|
|
|
{"gfx-surface-recording", nullptr},
|
|
|
|
{"gfx-surface-vg", nullptr},
|
|
|
|
{"gfx-surface-gl", nullptr},
|
|
|
|
{"gfx-surface-drm", nullptr},
|
|
|
|
{"gfx-surface-tee", nullptr},
|
|
|
|
{"gfx-surface-xml", nullptr},
|
|
|
|
{"gfx-surface-skia", nullptr},
|
|
|
|
{"gfx-surface-subsurface", nullptr},
|
|
|
|
{"gfx-surface-d2d", nullptr},
|
2010-05-22 08:10:14 +04:00
|
|
|
};
|
|
|
|
|
2014-01-30 22:26:54 +04:00
|
|
|
PR_STATIC_ASSERT(MOZ_ARRAY_LENGTH(sSurfaceMemoryReporterAttrs) ==
|
2014-01-23 22:26:40 +04:00
|
|
|
size_t(gfxSurfaceType::Max));
|
2010-09-09 11:50:18 +04:00
|
|
|
#ifdef CAIRO_HAS_D2D_SURFACE
|
2012-08-22 19:56:38 +04:00
|
|
|
PR_STATIC_ASSERT(uint32_t(CAIRO_SURFACE_TYPE_D2D) ==
|
2014-01-23 22:26:40 +04:00
|
|
|
uint32_t(gfxSurfaceType::D2D));
|
2010-09-09 11:50:18 +04:00
|
|
|
#endif
|
2012-08-22 19:56:38 +04:00
|
|
|
PR_STATIC_ASSERT(uint32_t(CAIRO_SURFACE_TYPE_SKIA) ==
|
2014-01-23 22:26:40 +04:00
|
|
|
uint32_t(gfxSurfaceType::Skia));
|
2010-05-22 08:10:14 +04:00
|
|
|
|
|
|
|
/* Surface size memory reporting */
|
2012-03-19 01:59:17 +04:00
|
|
|
|
2014-01-23 22:26:40 +04:00
|
|
|
static int64_t gSurfaceMemoryUsed[size_t(gfxSurfaceType::Max)] = { 0 };
|
2010-05-22 08:10:14 +04:00
|
|
|
|
2013-12-08 09:39:47 +04:00
|
|
|
class SurfaceMemoryReporter MOZ_FINAL : public nsIMemoryReporter
|
2010-05-22 08:10:14 +04:00
|
|
|
{
|
2014-06-23 22:49:07 +04:00
|
|
|
~SurfaceMemoryReporter() {}
|
|
|
|
|
2010-05-22 08:10:14 +04:00
|
|
|
public:
|
2013-12-08 09:39:47 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
2010-05-22 08:10:14 +04:00
|
|
|
|
2013-08-28 03:24:51 +04:00
|
|
|
NS_IMETHOD CollectReports(nsIMemoryReporterCallback *aCb,
|
2015-01-06 23:12:41 +03:00
|
|
|
nsISupports *aClosure, bool aAnonymize) MOZ_OVERRIDE
|
2012-03-19 01:59:17 +04:00
|
|
|
{
|
2014-01-30 22:26:54 +04:00
|
|
|
const size_t len = ArrayLength(sSurfaceMemoryReporterAttrs);
|
2012-03-19 01:59:17 +04:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t amount = gSurfaceMemoryUsed[i];
|
2012-03-19 01:59:17 +04:00
|
|
|
|
|
|
|
if (amount != 0) {
|
|
|
|
const char *path = sSurfaceMemoryReporterAttrs[i].path;
|
|
|
|
const char *desc = sSurfaceMemoryReporterAttrs[i].description;
|
|
|
|
if (!desc) {
|
|
|
|
desc = sDefaultSurfaceDescription;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = aCb->Callback(EmptyCString(), nsCString(path),
|
2013-12-08 09:39:47 +04:00
|
|
|
KIND_OTHER, UNITS_BYTES,
|
2012-03-19 01:59:17 +04:00
|
|
|
gSurfaceMemoryUsed[i],
|
|
|
|
nsCString(desc), aClosure);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
2011-05-23 06:49:56 +04:00
|
|
|
|
2010-05-22 08:10:14 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(SurfaceMemoryReporter, nsIMemoryReporter)
|
2013-12-08 09:39:47 +04:00
|
|
|
|
2010-05-22 08:10:14 +04:00
|
|
|
void
|
2013-09-25 00:45:13 +04:00
|
|
|
gfxASurface::RecordMemoryUsedForSurfaceType(gfxSurfaceType aType,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aBytes)
|
2010-05-22 08:10:14 +04:00
|
|
|
{
|
2014-01-23 22:26:40 +04:00
|
|
|
if (int(aType) < 0 || aType >= gfxSurfaceType::Max) {
|
2010-05-22 08:10:14 +04:00
|
|
|
NS_WARNING("Invalid type to RecordMemoryUsedForSurfaceType!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-19 01:59:17 +04:00
|
|
|
static bool registered = false;
|
|
|
|
if (!registered) {
|
2013-11-07 09:35:30 +04:00
|
|
|
RegisterStrongMemoryReporter(new SurfaceMemoryReporter());
|
2012-03-19 01:59:17 +04:00
|
|
|
registered = true;
|
2010-05-22 08:10:14 +04:00
|
|
|
}
|
|
|
|
|
2014-01-23 22:26:40 +04:00
|
|
|
gSurfaceMemoryUsed[size_t(aType)] += aBytes;
|
2010-05-22 08:10:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
gfxASurface::RecordMemoryUsed(int32_t aBytes)
|
2010-05-22 08:10:14 +04:00
|
|
|
{
|
|
|
|
RecordMemoryUsedForSurfaceType(GetType(), aBytes);
|
|
|
|
mBytesRecorded += aBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxASurface::RecordMemoryFreed()
|
|
|
|
{
|
|
|
|
if (mBytesRecorded) {
|
|
|
|
RecordMemoryUsedForSurfaceType(GetType(), -mBytesRecorded);
|
|
|
|
mBytesRecorded = 0;
|
|
|
|
}
|
|
|
|
}
|
2011-03-25 01:04:46 +03:00
|
|
|
|
2012-11-27 04:29:56 +04:00
|
|
|
size_t
|
2013-06-23 16:03:39 +04:00
|
|
|
gfxASurface::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
2012-11-27 04:29:56 +04:00
|
|
|
{
|
|
|
|
// We don't measure mSurface because cairo doesn't allow it.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2013-06-23 16:03:39 +04:00
|
|
|
gfxASurface::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
2012-11-27 04:29:56 +04:00
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2012-12-13 02:47:28 +04:00
|
|
|
/* static */ uint8_t
|
|
|
|
gfxASurface::BytesPerPixel(gfxImageFormat aImageFormat)
|
|
|
|
{
|
|
|
|
switch (aImageFormat) {
|
2014-01-23 22:26:40 +04:00
|
|
|
case gfxImageFormat::ARGB32:
|
2012-12-13 02:47:28 +04:00
|
|
|
return 4;
|
2014-01-23 22:26:40 +04:00
|
|
|
case gfxImageFormat::RGB24:
|
2012-12-13 02:47:28 +04:00
|
|
|
return 4;
|
2014-01-23 22:26:40 +04:00
|
|
|
case gfxImageFormat::RGB16_565:
|
2012-12-13 02:47:28 +04:00
|
|
|
return 2;
|
2014-01-23 22:26:40 +04:00
|
|
|
case gfxImageFormat::A8:
|
2012-12-13 02:47:28 +04:00
|
|
|
return 1;
|
2014-01-23 22:26:40 +04:00
|
|
|
case gfxImageFormat::A1:
|
2012-12-13 02:47:28 +04:00
|
|
|
return 1; // Close enough
|
2014-01-23 22:26:40 +04:00
|
|
|
case gfxImageFormat::Unknown:
|
2012-12-13 02:47:28 +04:00
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Not really sure what you want me to say here");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-25 00:45:13 +04:00
|
|
|
void
|
|
|
|
gfxASurface::SetOpaqueRect(const gfxRect& aRect)
|
|
|
|
{
|
|
|
|
if (aRect.IsEmpty()) {
|
|
|
|
mOpaqueRect = nullptr;
|
|
|
|
} else if (!!mOpaqueRect) {
|
|
|
|
*mOpaqueRect = aRect;
|
|
|
|
} else {
|
2014-01-06 19:49:03 +04:00
|
|
|
mOpaqueRect = MakeUnique<gfxRect>(aRect);
|
2013-09-25 00:45:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */const gfxRect&
|
|
|
|
gfxASurface::GetEmptyOpaqueRect()
|
|
|
|
{
|
|
|
|
static const gfxRect empty(0, 0, 0, 0);
|
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsIntSize
|
|
|
|
gfxASurface::GetSize() const
|
|
|
|
{
|
|
|
|
return nsIntSize(-1, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxImageSurface>
|
|
|
|
gfxASurface::GetAsImageSurface()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|