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"
|
2015-06-18 09:18:00 +03:00
|
|
|
#include "mozilla/gfx/Logging.h"
|
2015-07-07 05:11:38 +03:00
|
|
|
#include "mozilla/gfx/HelpersCairo.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
|
|
|
|
|
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"
|
|
|
|
#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()
|
2018-11-30 13:46:48 +03:00
|
|
|
: mSurface(nullptr),
|
|
|
|
mFloatingRefs(0),
|
|
|
|
mBytesRecorded(0),
|
|
|
|
mSurfaceValid(false) {
|
|
|
|
MOZ_COUNT_CTOR(gfxASurface);
|
2013-09-25 00:45:13 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
gfxASurface::~gfxASurface() {
|
|
|
|
RecordMemoryFreed();
|
2013-09-25 00:45:13 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
MOZ_COUNT_DTOR(gfxASurface);
|
2013-09-25 00:45:13 +04:00
|
|
|
}
|
|
|
|
|
2007-01-27 04:26:49 +03:00
|
|
|
// Surfaces use refcounting that's tied to the cairo surface refcnt, to avoid
|
|
|
|
// refcount mismatch issues.
|
2018-11-30 13:46:48 +03:00
|
|
|
nsrefcnt gfxASurface::AddRef(void) {
|
|
|
|
if (mSurfaceValid) {
|
|
|
|
if (mFloatingRefs) {
|
|
|
|
// eat a floating ref
|
|
|
|
mFloatingRefs--;
|
|
|
|
} else {
|
|
|
|
cairo_surface_reference(mSurface);
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
|
|
return (nsrefcnt)cairo_surface_get_reference_count(mSurface);
|
|
|
|
}
|
|
|
|
// the surface isn't valid, but we still need to refcount
|
|
|
|
// the gfxASurface
|
|
|
|
return ++mFloatingRefs;
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
nsrefcnt gfxASurface::Release(void) {
|
|
|
|
if (mSurfaceValid) {
|
|
|
|
NS_ASSERTION(
|
|
|
|
mFloatingRefs == 0,
|
|
|
|
"gfxASurface::Release with floating refs still hanging around!");
|
2007-05-10 23:58:09 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
// 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);
|
2007-05-10 23:58:09 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
// |this| may not be valid any more, don't use it!
|
2007-05-10 23:58:09 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
return --refcnt;
|
|
|
|
}
|
|
|
|
if (--mFloatingRefs == 0) {
|
|
|
|
delete this;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return mFloatingRefs;
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +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;
|
2007-01-18 01:20:16 +03:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
gfxASurface *gfxASurface::GetSurfaceWrapper(cairo_surface_t *csurf) {
|
|
|
|
if (!csurf) return nullptr;
|
|
|
|
return (gfxASurface *)cairo_surface_get_user_data(csurf,
|
|
|
|
&gfxasurface_pointer_key);
|
2006-04-05 01:54:22 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void gfxASurface::SetSurfaceWrapper(cairo_surface_t *csurf,
|
|
|
|
gfxASurface *asurf) {
|
|
|
|
if (!csurf) return;
|
|
|
|
cairo_surface_set_user_data(csurf, &gfxasurface_pointer_key, asurf,
|
|
|
|
SurfaceDestroyFunc);
|
2006-04-05 01:54:22 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
already_AddRefed<gfxASurface> gfxASurface::Wrap(cairo_surface_t *csurf,
|
|
|
|
const IntSize &aSize) {
|
|
|
|
RefPtr<gfxASurface> result;
|
2006-04-05 01:54:22 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
/* Do we already have a wrapper for this surface? */
|
|
|
|
result = GetSurfaceWrapper(csurf);
|
|
|
|
if (result) {
|
|
|
|
// fprintf(stderr, "Existing wrapper for %p -> %p\n", csurf, result);
|
|
|
|
return result.forget();
|
|
|
|
}
|
2006-04-05 01:54:22 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
/* No wrapper; figure out the surface type and create it */
|
|
|
|
cairo_surface_type_t stype = cairo_surface_get_type(csurf);
|
2006-04-05 01:54:22 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
if (stype == CAIRO_SURFACE_TYPE_IMAGE) {
|
|
|
|
result = new gfxImageSurface(csurf);
|
|
|
|
}
|
2006-04-05 01:54:22 +04:00
|
|
|
#ifdef CAIRO_HAS_WIN32_SURFACE
|
2018-11-30 13:46:48 +03:00
|
|
|
else if (stype == CAIRO_SURFACE_TYPE_WIN32 ||
|
|
|
|
stype == CAIRO_SURFACE_TYPE_WIN32_PRINTING) {
|
|
|
|
result = new gfxWindowsSurface(csurf);
|
|
|
|
}
|
2006-04-05 01:54:22 +04:00
|
|
|
#endif
|
2009-06-23 07:50:08 +04:00
|
|
|
#ifdef MOZ_X11
|
2018-11-30 13:46:48 +03:00
|
|
|
else if (stype == CAIRO_SURFACE_TYPE_XLIB) {
|
|
|
|
result = new gfxXlibSurface(csurf);
|
|
|
|
}
|
2006-04-05 01:54:22 +04:00
|
|
|
#endif
|
2007-04-04 05:09:15 +04:00
|
|
|
#ifdef CAIRO_HAS_QUARTZ_SURFACE
|
2018-11-30 13:46:48 +03:00
|
|
|
else if (stype == CAIRO_SURFACE_TYPE_QUARTZ) {
|
|
|
|
result = new gfxQuartzSurface(csurf, aSize);
|
|
|
|
}
|
2006-04-05 01:54:22 +04:00
|
|
|
#endif
|
2018-11-30 13:46:48 +03:00
|
|
|
else {
|
|
|
|
result = new gfxUnknownSurface(csurf, aSize);
|
|
|
|
}
|
2006-04-05 01:54:22 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
// fprintf(stderr, "New wrapper for %p -> %p\n", csurf, result);
|
2006-04-05 01:54:22 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
return result.forget();
|
2006-04-05 01:54:22 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void gfxASurface::Init(cairo_surface_t *surface, bool existingSurface) {
|
|
|
|
SetSurfaceWrapper(surface, this);
|
|
|
|
MOZ_ASSERT(surface, "surface should be a valid pointer");
|
2006-04-05 01:54:22 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
mSurface = surface;
|
|
|
|
mSurfaceValid = !cairo_surface_status(surface);
|
|
|
|
if (!mSurfaceValid) {
|
|
|
|
gfxWarning() << "ASurface Init failed with Cairo status "
|
|
|
|
<< cairo_surface_status(surface) << " on " << hexa(surface);
|
|
|
|
}
|
2006-04-05 01:54:22 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
if (existingSurface || !mSurfaceValid) {
|
|
|
|
mFloatingRefs = 0;
|
|
|
|
} else {
|
|
|
|
mFloatingRefs = 1;
|
2011-02-07 21:52:40 +03:00
|
|
|
#ifdef MOZ_TREE_CAIRO
|
2018-11-30 13:46:48 +03:00
|
|
|
if (cairo_surface_get_content(surface) != CAIRO_CONTENT_COLOR) {
|
|
|
|
cairo_surface_set_subpixel_antialiasing(
|
|
|
|
surface, CAIRO_SUBPIXEL_ANTIALIASING_DISABLED);
|
2006-04-05 01:54:22 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
#endif
|
|
|
|
}
|
2006-04-05 01:54:22 +04:00
|
|
|
}
|
2007-01-27 04:26:49 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
gfxSurfaceType gfxASurface::GetType() const {
|
|
|
|
if (!mSurfaceValid) return (gfxSurfaceType)-1;
|
2007-08-09 22:54:17 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
return (gfxSurfaceType)cairo_surface_get_type(mSurface);
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
gfxContentType gfxASurface::GetContentType() const {
|
|
|
|
if (!mSurfaceValid) return (gfxContentType)-1;
|
2007-08-09 22:54:17 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
return (gfxContentType)cairo_surface_get_content(mSurface);
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void gfxASurface::SetDeviceOffset(const gfxPoint &offset) {
|
|
|
|
if (!mSurfaceValid) return;
|
|
|
|
cairo_surface_set_device_offset(mSurface, offset.x, offset.y);
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
gfxPoint gfxASurface::GetDeviceOffset() const {
|
|
|
|
if (!mSurfaceValid) return gfxPoint(0.0, 0.0);
|
|
|
|
gfxPoint pt;
|
|
|
|
cairo_surface_get_device_offset(mSurface, &pt.x, &pt.y);
|
|
|
|
return pt;
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void gfxASurface::Flush() const {
|
|
|
|
if (!mSurfaceValid) return;
|
|
|
|
cairo_surface_flush(mSurface);
|
|
|
|
gfxPlatform::ClearSourceSurfaceForSurface(const_cast<gfxASurface *>(this));
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void gfxASurface::MarkDirty() {
|
|
|
|
if (!mSurfaceValid) return;
|
|
|
|
cairo_surface_mark_dirty(mSurface);
|
|
|
|
gfxPlatform::ClearSourceSurfaceForSurface(this);
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void gfxASurface::MarkDirty(const gfxRect &r) {
|
|
|
|
if (!mSurfaceValid) return;
|
|
|
|
cairo_surface_mark_dirty_rectangle(mSurface, (int)r.X(), (int)r.Y(),
|
|
|
|
(int)r.Width(), (int)r.Height());
|
|
|
|
gfxPlatform::ClearSourceSurfaceForSurface(this);
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void gfxASurface::SetData(const cairo_user_data_key_t *key, void *user_data,
|
|
|
|
thebes_destroy_func_t destroy) {
|
|
|
|
if (!mSurfaceValid) return;
|
|
|
|
cairo_surface_set_user_data(mSurface, key, user_data, destroy);
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void *gfxASurface::GetData(const cairo_user_data_key_t *key) {
|
|
|
|
if (!mSurfaceValid) return nullptr;
|
|
|
|
return cairo_surface_get_user_data(mSurface, key);
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
2007-01-27 04:46:14 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void gfxASurface::Finish() {
|
|
|
|
// null surfaces are allowed here
|
|
|
|
cairo_surface_finish(mSurface);
|
2007-01-27 04:46:14 +03:00
|
|
|
}
|
2007-05-10 23:58:09 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
already_AddRefed<gfxASurface> gfxASurface::CreateSimilarSurface(
|
|
|
|
gfxContentType aContent, const IntSize &aSize) {
|
|
|
|
if (!mSurface || !mSurfaceValid) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2010-06-01 03:37:44 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
cairo_surface_t *surface = cairo_surface_create_similar(
|
|
|
|
mSurface, cairo_content_t(int(aContent)), aSize.width, aSize.height);
|
|
|
|
if (cairo_surface_status(surface)) {
|
2010-06-18 13:21:15 +04:00
|
|
|
cairo_surface_destroy(surface);
|
2018-11-30 13:46:48 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<gfxASurface> result = Wrap(surface, aSize);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
return result.forget();
|
2010-06-01 03:37:44 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
already_AddRefed<gfxImageSurface> gfxASurface::CopyToARGB32ImageSurface() {
|
|
|
|
if (!mSurface || !mSurfaceValid) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-03-06 03:39:48 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
const IntSize size = GetSize();
|
|
|
|
RefPtr<gfxImageSurface> imgSurface =
|
|
|
|
new gfxImageSurface(size, SurfaceFormat::A8R8G8B8_UINT32);
|
2013-03-06 03:39:48 +04:00
|
|
|
|
2018-11-30 13:46:48 +03: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
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
dt->CopySurface(source, IntRect(0, 0, size.width, size.height), IntPoint());
|
2013-03-06 03:39:48 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
return imgSurface.forget();
|
2013-03-06 03:39:48 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
int gfxASurface::CairoStatus() {
|
|
|
|
if (!mSurfaceValid) return -1;
|
2007-05-10 23:58:09 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
return cairo_surface_status(mSurface);
|
2007-05-10 23:58:09 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
nsresult gfxASurface::BeginPrinting(const nsAString &aTitle,
|
|
|
|
const nsAString &aPrintToFileName) {
|
|
|
|
return NS_OK;
|
2016-06-16 19:25:54 +03:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
nsresult gfxASurface::EndPrinting() { return NS_OK; }
|
2016-06-16 19:25:54 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
nsresult gfxASurface::AbortPrinting() { return NS_OK; }
|
2016-06-16 19:25:54 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
nsresult gfxASurface::BeginPage() { return NS_OK; }
|
2016-06-16 19:25:54 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
nsresult gfxASurface::EndPage() { return NS_OK; }
|
2016-06-16 19:25:54 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
gfxContentType gfxASurface::ContentFromFormat(gfxImageFormat format) {
|
|
|
|
switch (format) {
|
|
|
|
case SurfaceFormat::A8R8G8B8_UINT32:
|
|
|
|
return gfxContentType::COLOR_ALPHA;
|
|
|
|
case SurfaceFormat::X8R8G8B8_UINT32:
|
|
|
|
case SurfaceFormat::R5G6B5_UINT16:
|
|
|
|
return gfxContentType::COLOR;
|
|
|
|
case SurfaceFormat::A8:
|
|
|
|
return gfxContentType::ALPHA;
|
|
|
|
|
|
|
|
case SurfaceFormat::UNKNOWN:
|
|
|
|
default:
|
|
|
|
return gfxContentType::COLOR;
|
|
|
|
}
|
2008-04-29 05:15:30 +04:00
|
|
|
}
|
2010-05-22 08:10:14 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
int32_t gfxASurface::BytePerPixelFromFormat(gfxImageFormat format) {
|
|
|
|
switch (format) {
|
|
|
|
case SurfaceFormat::A8R8G8B8_UINT32:
|
|
|
|
case SurfaceFormat::X8R8G8B8_UINT32:
|
|
|
|
return 4;
|
|
|
|
case SurfaceFormat::R5G6B5_UINT16:
|
|
|
|
return 2;
|
|
|
|
case SurfaceFormat::A8:
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
NS_WARNING("Unknown byte per pixel value for Image format");
|
|
|
|
}
|
|
|
|
return 0;
|
2010-06-11 13:33:22 +04:00
|
|
|
}
|
|
|
|
|
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 "
|
2018-11-30 13:46:48 +03:00
|
|
|
"accounted for here aren't counted in vsize, resident, explicit, or any "
|
|
|
|
"of "
|
2011-07-18 17:20:27 +04:00
|
|
|
"the other measurements on this page."},
|
2012-07-30 18:20:58 +04:00
|
|
|
{"gfx-surface-xcb", nullptr},
|
2018-11-30 13:46:48 +03:00
|
|
|
{"gfx-surface-glitz???", nullptr}, // should never be used
|
2012-07-30 18:20:58 +04:00
|
|
|
{"gfx-surface-quartz", nullptr},
|
|
|
|
{"gfx-surface-win32", nullptr},
|
|
|
|
{"gfx-surface-beos", nullptr},
|
2018-11-30 13:46:48 +03:00
|
|
|
{"gfx-surface-directfb???", nullptr}, // should never be used
|
2012-07-30 18:20:58 +04:00
|
|
|
{"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},
|
2010-05-22 08:10:14 +04:00
|
|
|
};
|
|
|
|
|
2016-08-23 18:24:54 +03:00
|
|
|
static_assert(MOZ_ARRAY_LENGTH(sSurfaceMemoryReporterAttrs) ==
|
2018-11-30 13:46:48 +03:00
|
|
|
size_t(gfxSurfaceType::Max),
|
|
|
|
"sSurfaceMemoryReporterAttrs exceeds max capacity");
|
2016-08-23 18:24:54 +03:00
|
|
|
static_assert(uint32_t(CAIRO_SURFACE_TYPE_SKIA) ==
|
2018-11-30 13:46:48 +03:00
|
|
|
uint32_t(gfxSurfaceType::Skia),
|
|
|
|
"CAIRO_SURFACE_TYPE_SKIA not equal to gfxSurfaceType::Skia");
|
2010-05-22 08:10:14 +04:00
|
|
|
|
|
|
|
/* Surface size memory reporting */
|
2012-03-19 01:59:17 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
class SurfaceMemoryReporter final : public nsIMemoryReporter {
|
|
|
|
~SurfaceMemoryReporter() = default;
|
|
|
|
|
|
|
|
// We can touch this array on several different threads, and we don't
|
|
|
|
// want to introduce memory barriers when recording the memory used. To
|
|
|
|
// assure dynamic race checkers like TSan that this is OK, we use
|
|
|
|
// relaxed memory ordering here.
|
|
|
|
static Atomic<size_t, Relaxed>
|
|
|
|
sSurfaceMemoryUsed[size_t(gfxSurfaceType::Max)];
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void AdjustUsedMemory(gfxSurfaceType aType, int32_t aBytes) {
|
|
|
|
// A read-modify-write operation like += would require a memory barrier
|
|
|
|
// here, which would defeat the purpose of using relaxed memory
|
|
|
|
// ordering. So separate out the read and write operations.
|
|
|
|
sSurfaceMemoryUsed[size_t(aType)] =
|
|
|
|
sSurfaceMemoryUsed[size_t(aType)] + aBytes;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This memory reporter is sometimes allocated on the compositor thread,
|
|
|
|
// but always released on the main thread, so its refcounting needs to be
|
|
|
|
// threadsafe.
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD CollectReports(nsIHandleReportCallback *aHandleReport,
|
|
|
|
nsISupports *aData, bool aAnonymize) override {
|
|
|
|
const size_t len = ArrayLength(sSurfaceMemoryReporterAttrs);
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
int64_t amount = sSurfaceMemoryUsed[i];
|
|
|
|
|
|
|
|
if (amount != 0) {
|
|
|
|
const char *path = sSurfaceMemoryReporterAttrs[i].path;
|
|
|
|
const char *desc = sSurfaceMemoryReporterAttrs[i].description;
|
|
|
|
if (!desc) {
|
|
|
|
desc = sDefaultSurfaceDescription;
|
2012-03-19 01:59:17 +04:00
|
|
|
}
|
2011-05-23 06:49:56 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
aHandleReport->Callback(EmptyCString(), nsCString(path), KIND_OTHER,
|
|
|
|
UNITS_BYTES, amount, nsCString(desc), aData);
|
|
|
|
}
|
2010-05-22 08:10:14 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-05-22 08:10:14 +04:00
|
|
|
};
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
Atomic<size_t, Relaxed>
|
|
|
|
SurfaceMemoryReporter::sSurfaceMemoryUsed[size_t(gfxSurfaceType::Max)];
|
2015-08-01 05:43:24 +03:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(SurfaceMemoryReporter, nsIMemoryReporter)
|
2013-12-08 09:39:47 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void gfxASurface::RecordMemoryUsedForSurfaceType(gfxSurfaceType aType,
|
|
|
|
int32_t aBytes) {
|
|
|
|
if (int(aType) < 0 || aType >= gfxSurfaceType::Max) {
|
|
|
|
NS_WARNING("Invalid type to RecordMemoryUsedForSurfaceType!");
|
|
|
|
return;
|
|
|
|
}
|
2010-05-22 08:10:14 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
static bool registered = false;
|
|
|
|
if (!registered) {
|
|
|
|
RegisterStrongMemoryReporter(new SurfaceMemoryReporter());
|
|
|
|
registered = true;
|
|
|
|
}
|
2010-05-22 08:10:14 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
SurfaceMemoryReporter::AdjustUsedMemory(aType, aBytes);
|
2010-05-22 08:10:14 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void gfxASurface::RecordMemoryUsed(int32_t aBytes) {
|
|
|
|
RecordMemoryUsedForSurfaceType(GetType(), aBytes);
|
|
|
|
mBytesRecorded += aBytes;
|
2010-05-22 08:10:14 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void gfxASurface::RecordMemoryFreed() {
|
|
|
|
if (mBytesRecorded) {
|
|
|
|
RecordMemoryUsedForSurfaceType(GetType(), -mBytesRecorded);
|
|
|
|
mBytesRecorded = 0;
|
|
|
|
}
|
2010-05-22 08:10:14 +04:00
|
|
|
}
|
2011-03-25 01:04:46 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
size_t gfxASurface::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
|
|
|
|
// We don't measure mSurface because cairo doesn't allow it.
|
|
|
|
return 0;
|
2012-11-27 04:29:56 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
size_t gfxASurface::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
2012-11-27 04:29:56 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
/* static */ uint8_t gfxASurface::BytesPerPixel(gfxImageFormat aImageFormat) {
|
2012-12-13 02:47:28 +04:00
|
|
|
switch (aImageFormat) {
|
2016-01-08 07:57:38 +03:00
|
|
|
case SurfaceFormat::A8R8G8B8_UINT32:
|
2012-12-13 02:47:28 +04:00
|
|
|
return 4;
|
2016-01-08 07:57:38 +03:00
|
|
|
case SurfaceFormat::X8R8G8B8_UINT32:
|
2012-12-13 02:47:28 +04:00
|
|
|
return 4;
|
2016-01-08 07:57:38 +03:00
|
|
|
case SurfaceFormat::R5G6B5_UINT16:
|
2012-12-13 02:47:28 +04:00
|
|
|
return 2;
|
2016-01-08 07:57:38 +03:00
|
|
|
case SurfaceFormat::A8:
|
2012-12-13 02:47:28 +04:00
|
|
|
return 1;
|
2016-01-08 07:57:38 +03:00
|
|
|
case SurfaceFormat::UNKNOWN:
|
2012-12-13 02:47:28 +04:00
|
|
|
default:
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Not really sure what you want me to say here");
|
2012-12-13 02:47:28 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void gfxASurface::SetOpaqueRect(const gfxRect &aRect) {
|
|
|
|
if (aRect.IsEmpty()) {
|
|
|
|
mOpaqueRect = nullptr;
|
|
|
|
} else if (!!mOpaqueRect) {
|
|
|
|
*mOpaqueRect = aRect;
|
|
|
|
} else {
|
|
|
|
mOpaqueRect = MakeUnique<gfxRect>(aRect);
|
|
|
|
}
|
2013-09-25 00:45:13 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
/* static */ const gfxRect &gfxASurface::GetEmptyOpaqueRect() {
|
2013-09-25 00:45:13 +04:00
|
|
|
static const gfxRect empty(0, 0, 0, 0);
|
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
const IntSize gfxASurface::GetSize() const { return IntSize(-1, -1); }
|
2013-09-25 00:45:13 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
SurfaceFormat gfxASurface::GetSurfaceFormat() const {
|
|
|
|
if (!mSurfaceValid) {
|
|
|
|
return SurfaceFormat::UNKNOWN;
|
|
|
|
}
|
|
|
|
return GfxFormatForCairoSurface(mSurface);
|
2015-07-07 05:11:38 +03:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
already_AddRefed<gfxImageSurface> gfxASurface::GetAsImageSurface() {
|
2013-09-25 00:45:13 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|