2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
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()
|
|
|
|
: mSurface(nullptr),
|
|
|
|
mFloatingRefs(0),
|
|
|
|
mBytesRecorded(0),
|
2016-06-08 22:26:56 +03:00
|
|
|
mSurfaceValid(false) {
|
2013-09-25 00:45:13 +04:00
|
|
|
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);
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-05-10 23:58:09 +04:00
|
|
|
return (nsrefcnt)cairo_surface_get_reference_count(mSurface);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2017-02-09 15:39:27 +03: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;
|
|
|
|
}
|
2017-02-09 15:39:27 +03:00
|
|
|
if (--mFloatingRefs == 0) {
|
|
|
|
delete this;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return mFloatingRefs;
|
2007-01-27 04:26:49 +03:00
|
|
|
}
|
|
|
|
|
2007-01-18 01:20:16 +03:00
|
|
|
void gfxASurface::SurfaceDestroyFunc(void *data) {
|
|
|
|
gfxASurface *surf = (gfxASurface *)data;
|
2018-11-28 12:16:55 +03:00
|
|
|
// fprintf (stderr, "Deleting wrapper for %p (wrapper: %p)\n", surf->mSurface,
|
|
|
|
// data);
|
2007-01-18 01:20:16 +03:00
|
|
|
delete surf;
|
|
|
|
}
|
|
|
|
|
2006-04-05 01:54:22 +04:00
|
|
|
gfxASurface *gfxASurface::GetSurfaceWrapper(cairo_surface_t *csurf) {
|
2013-07-31 19:44:31 +04:00
|
|
|
if (!csurf) 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
|
|
|
}
|
|
|
|
|
2015-06-01 11:26:19 +03:00
|
|
|
already_AddRefed<gfxASurface> gfxASurface::Wrap(cairo_surface_t *csurf,
|
|
|
|
const IntSize &aSize) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<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
|
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
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else {
|
2016-09-27 21:52:17 +03: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
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
void gfxASurface::Init(cairo_surface_t *surface, bool existingSurface) {
|
2007-01-18 01:20:16 +03:00
|
|
|
SetSurfaceWrapper(surface, this);
|
2015-12-07 09:36:00 +03:00
|
|
|
MOZ_ASSERT(surface, "surface should be a valid pointer");
|
2006-04-05 01:54:22 +04:00
|
|
|
|
|
|
|
mSurface = surface;
|
2015-12-07 09:36:00 +03:00
|
|
|
mSurfaceValid = !cairo_surface_status(surface);
|
2015-06-18 09:18:00 +03:00
|
|
|
if (!mSurfaceValid) {
|
|
|
|
gfxWarning() << "ASurface Init failed with Cairo status "
|
|
|
|
<< cairo_surface_status(surface) << " on " << hexa(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);
|
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
|
|
|
|
|
|
|
gfxSurfaceType 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxContentType 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);
|
|
|
|
}
|
|
|
|
|
2007-02-08 23:47:48 +03:00
|
|
|
void gfxASurface::SetDeviceOffset(const gfxPoint &offset) {
|
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;
|
|
|
|
}
|
|
|
|
|
2010-08-09 06:19:17 +04:00
|
|
|
void gfxASurface::Flush() const {
|
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, (int)r.X(), (int)r.Y(),
|
2011-04-19 07:07:21 +04:00
|
|
|
(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) {
|
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) 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(
|
2015-05-28 16:00:17 +03:00
|
|
|
gfxContentType aContent, const IntSize &aSize) {
|
2010-12-16 11:07:20 +03:00
|
|
|
if (!mSurface || !mSurfaceValid) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-06-01 03:37:44 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24: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);
|
2013-07-31 19:44:31 +04:00
|
|
|
return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxASurface> result = Wrap(surface, aSize);
|
2010-06-01 03:37:44 +04:00
|
|
|
cairo_surface_destroy(surface);
|
2010-06-18 13:21:15 +04:00
|
|
|
return result.forget();
|
2010-06-01 03:37:44 +04:00
|
|
|
}
|
|
|
|
|
2013-03-06 03:39:48 +04:00
|
|
|
already_AddRefed<gfxImageSurface> gfxASurface::CopyToARGB32ImageSurface() {
|
|
|
|
if (!mSurface || !mSurfaceValid) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-05-28 16:00:17 +03:00
|
|
|
const IntSize size = GetSize();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxImageSurface> imgSurface =
|
2016-01-08 07:57:38 +03:00
|
|
|
new gfxImageSurface(size, SurfaceFormat::A8R8G8B8_UINT32);
|
2013-03-06 03:39:48 +04:00
|
|
|
|
2015-10-18 08:24: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
|
|
|
|
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-11 00:50:16 +04:00
|
|
|
int gfxASurface::CairoStatus() {
|
2007-05-10 23:58:09 +04:00
|
|
|
if (!mSurfaceValid) return -1;
|
|
|
|
|
|
|
|
return cairo_surface_status(mSurface);
|
|
|
|
}
|
|
|
|
|
2016-06-16 19:25:54 +03:00
|
|
|
nsresult gfxASurface::BeginPrinting(const nsAString &aTitle,
|
|
|
|
const nsAString &aPrintToFileName) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult gfxASurface::EndPrinting() { return NS_OK; }
|
|
|
|
|
|
|
|
nsresult gfxASurface::AbortPrinting() { return NS_OK; }
|
|
|
|
|
|
|
|
nsresult gfxASurface::BeginPage() { return NS_OK; }
|
|
|
|
|
|
|
|
nsresult gfxASurface::EndPage() { return NS_OK; }
|
|
|
|
|
2008-04-29 05:15:30 +04:00
|
|
|
gfxContentType gfxASurface::ContentFromFormat(gfxImageFormat format) {
|
|
|
|
switch (format) {
|
2016-01-08 07:57:38 +03:00
|
|
|
case SurfaceFormat::A8R8G8B8_UINT32:
|
2014-01-23 22:26:40 +04:00
|
|
|
return gfxContentType::COLOR_ALPHA;
|
2016-01-08 07:57:38 +03:00
|
|
|
case SurfaceFormat::X8R8G8B8_UINT32:
|
|
|
|
case SurfaceFormat::R5G6B5_UINT16:
|
2014-01-23 22:26:40 +04:00
|
|
|
return gfxContentType::COLOR;
|
2016-01-08 07:57:38 +03:00
|
|
|
case SurfaceFormat::A8:
|
2014-01-23 22:26:40 +04:00
|
|
|
return gfxContentType::ALPHA;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-01-08 07:57:38 +03:00
|
|
|
case SurfaceFormat::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
|
|
|
|
2010-06-11 13:33:22 +04:00
|
|
|
int32_t gfxASurface::BytePerPixelFromFormat(gfxImageFormat format) {
|
|
|
|
switch (format) {
|
2016-01-08 07:57:38 +03:00
|
|
|
case SurfaceFormat::A8R8G8B8_UINT32:
|
|
|
|
case SurfaceFormat::X8R8G8B8_UINT32:
|
2010-06-11 13:33:22 +04:00
|
|
|
return 4;
|
2016-01-08 07:57:38 +03:00
|
|
|
case SurfaceFormat::R5G6B5_UINT16:
|
2010-06-11 13:33:22 +04:00
|
|
|
return 2;
|
2016-01-08 07:57:38 +03:00
|
|
|
case SurfaceFormat::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},
|
2010-05-22 08:10:14 +04:00
|
|
|
};
|
|
|
|
|
2016-08-23 18:24:54 +03:00
|
|
|
static_assert(MOZ_ARRAY_LENGTH(sSurfaceMemoryReporterAttrs) ==
|
|
|
|
size_t(gfxSurfaceType::Max),
|
|
|
|
"sSurfaceMemoryReporterAttrs exceeds max capacity");
|
|
|
|
static_assert(uint32_t(CAIRO_SURFACE_TYPE_SKIA) ==
|
|
|
|
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
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class SurfaceMemoryReporter final : public nsIMemoryReporter {
|
2016-11-15 12:04:18 +03:00
|
|
|
~SurfaceMemoryReporter() = default;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-01 05:43:24 +03:00
|
|
|
// 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)];
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-05-22 08:10:14 +04:00
|
|
|
public:
|
2015-08-01 05:43:24 +03:00
|
|
|
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;
|
|
|
|
};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-05-30 08:04:30 +03:00
|
|
|
// 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
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
NS_IMETHOD CollectReports(nsIHandleReportCallback *aHandleReport,
|
|
|
|
nsISupports *aData, bool aAnonymize) override {
|
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++) {
|
2015-08-01 05:43:24 +03:00
|
|
|
int64_t amount = sSurfaceMemoryUsed[i];
|
2018-11-30 13:46:48 +03:00
|
|
|
|
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;
|
|
|
|
}
|
2011-05-23 06:49:56 +04:00
|
|
|
|
2010-05-22 08:10:14 +04:00
|
|
|
aHandleReport->Callback(EmptyCString(), nsCString(path), KIND_OTHER,
|
|
|
|
UNITS_BYTES, amount, nsCString(desc), aData);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-05-22 08:10:14 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-16 19:25:54 +03:00
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-05-22 08:10:14 +04:00
|
|
|
};
|
|
|
|
|
2015-08-01 05:43:24 +03:00
|
|
|
Atomic<size_t, Relaxed>
|
|
|
|
SurfaceMemoryReporter::sSurfaceMemoryUsed[size_t(gfxSurfaceType::Max)];
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(SurfaceMemoryReporter, nsIMemoryReporter)
|
2013-12-08 09:39:47 +04:00
|
|
|
|
2013-09-25 00:45:13 +04:00
|
|
|
void gfxASurface::RecordMemoryUsedForSurfaceType(gfxSurfaceType aType,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aBytes) {
|
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
|
|
|
}
|
|
|
|
|
2015-08-01 05:43:24 +03:00
|
|
|
SurfaceMemoryReporter::AdjustUsedMemory(aType, aBytes);
|
2010-05-22 08:10:14 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void 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
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
size_t 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;
|
|
|
|
}
|
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
size_t 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) {
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 IntSize gfxASurface::GetSize() const { return IntSize(-1, -1); }
|
|
|
|
|
2015-07-07 05:11:38 +03:00
|
|
|
SurfaceFormat gfxASurface::GetSurfaceFormat() const {
|
|
|
|
if (!mSurfaceValid) {
|
|
|
|
return SurfaceFormat::UNKNOWN;
|
|
|
|
}
|
|
|
|
return GfxFormatForCairoSurface(mSurface);
|
|
|
|
}
|
|
|
|
|
2013-09-25 00:45:13 +04:00
|
|
|
already_AddRefed<gfxImageSurface> gfxASurface::GetAsImageSurface() {
|
|
|
|
return nullptr;
|
|
|
|
}
|