1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsIMemoryReporter.h"
8 #include "mozilla/ArrayUtils.h"
9 #include "mozilla/Base64.h"
10 #include "mozilla/CheckedInt.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/MemoryReporting.h"
13 #include "nsISupportsImpl.h"
14 #include "mozilla/gfx/2D.h"
15 #include "mozilla/gfx/Logging.h"
16 #include "mozilla/gfx/HelpersCairo.h"
17 #include "gfx2DGlue.h"
19 #include "gfxASurface.h"
20 #include "gfxContext.h"
21 #include "gfxImageSurface.h"
22 #include "gfxPlatform.h"
28 #ifdef CAIRO_HAS_WIN32_SURFACE
29 # include "gfxWindowsSurface.h"
33 # include "gfxXlibSurface.h"
36 #ifdef CAIRO_HAS_QUARTZ_SURFACE
37 # include "gfxQuartzSurface.h"
43 #include "nsComponentManagerUtils.h"
44 #include "nsISupportsUtils.h"
46 #include "nsServiceManagerUtils.h"
49 using namespace mozilla
;
50 using namespace mozilla::gfx
;
52 static cairo_user_data_key_t gfxasurface_pointer_key
;
54 gfxASurface::gfxASurface()
58 mSurfaceValid(false) {
59 MOZ_COUNT_CTOR(gfxASurface
);
62 gfxASurface::~gfxASurface() {
65 MOZ_COUNT_DTOR(gfxASurface
);
68 // Surfaces use refcounting that's tied to the cairo surface refcnt, to avoid
69 // refcount mismatch issues.
70 nsrefcnt
gfxASurface::AddRef(void) {
76 cairo_surface_reference(mSurface
);
79 return (nsrefcnt
)cairo_surface_get_reference_count(mSurface
);
81 // the surface isn't valid, but we still need to refcount
83 return ++mFloatingRefs
;
86 nsrefcnt
gfxASurface::Release(void) {
90 "gfxASurface::Release with floating refs still hanging around!");
92 // Note that there is a destructor set on user data for mSurface,
93 // which will delete this gfxASurface wrapper when the surface's refcount
95 nsrefcnt refcnt
= (nsrefcnt
)cairo_surface_get_reference_count(mSurface
);
96 cairo_surface_destroy(mSurface
);
98 // |this| may not be valid any more, don't use it!
102 if (--mFloatingRefs
== 0) {
106 return mFloatingRefs
;
109 void gfxASurface::SurfaceDestroyFunc(void* data
) {
110 gfxASurface
* surf
= (gfxASurface
*)data
;
111 // fprintf (stderr, "Deleting wrapper for %p (wrapper: %p)\n", surf->mSurface,
116 gfxASurface
* gfxASurface::GetSurfaceWrapper(cairo_surface_t
* csurf
) {
117 if (!csurf
) return nullptr;
118 return (gfxASurface
*)cairo_surface_get_user_data(csurf
,
119 &gfxasurface_pointer_key
);
122 void gfxASurface::SetSurfaceWrapper(cairo_surface_t
* csurf
,
123 gfxASurface
* asurf
) {
125 cairo_surface_set_user_data(csurf
, &gfxasurface_pointer_key
, asurf
,
129 already_AddRefed
<gfxASurface
> gfxASurface::Wrap(cairo_surface_t
* csurf
,
130 const IntSize
& aSize
) {
131 RefPtr
<gfxASurface
> result
;
133 /* Do we already have a wrapper for this surface? */
134 result
= GetSurfaceWrapper(csurf
);
136 // fprintf(stderr, "Existing wrapper for %p -> %p\n", csurf, result);
137 return result
.forget();
140 /* No wrapper; figure out the surface type and create it */
141 cairo_surface_type_t stype
= cairo_surface_get_type(csurf
);
143 if (stype
== CAIRO_SURFACE_TYPE_IMAGE
) {
144 result
= new gfxImageSurface(csurf
);
146 #ifdef CAIRO_HAS_WIN32_SURFACE
147 else if (stype
== CAIRO_SURFACE_TYPE_WIN32
||
148 stype
== CAIRO_SURFACE_TYPE_WIN32_PRINTING
) {
149 result
= new gfxWindowsSurface(csurf
);
153 else if (stype
== CAIRO_SURFACE_TYPE_XLIB
) {
154 result
= new gfxXlibSurface(csurf
);
157 #ifdef CAIRO_HAS_QUARTZ_SURFACE
158 else if (stype
== CAIRO_SURFACE_TYPE_QUARTZ
) {
159 result
= new gfxQuartzSurface(csurf
, aSize
);
163 result
= new gfxUnknownSurface(csurf
, aSize
);
166 // fprintf(stderr, "New wrapper for %p -> %p\n", csurf, result);
168 return result
.forget();
171 void gfxASurface::Init(cairo_surface_t
* surface
, bool existingSurface
) {
172 SetSurfaceWrapper(surface
, this);
173 MOZ_ASSERT(surface
, "surface should be a valid pointer");
176 mSurfaceValid
= !cairo_surface_status(surface
);
177 if (!mSurfaceValid
) {
178 gfxWarning() << "ASurface Init failed with Cairo status "
179 << cairo_surface_status(surface
) << " on " << hexa(surface
);
182 if (existingSurface
|| !mSurfaceValid
) {
186 if (cairo_surface_get_content(surface
) != CAIRO_CONTENT_COLOR
) {
187 cairo_surface_set_subpixel_antialiasing(
188 surface
, CAIRO_SUBPIXEL_ANTIALIASING_DISABLED
);
193 gfxSurfaceType
gfxASurface::GetType() const {
194 if (!mSurfaceValid
) return (gfxSurfaceType
)-1;
196 return (gfxSurfaceType
)cairo_surface_get_type(mSurface
);
199 gfxContentType
gfxASurface::GetContentType() const {
200 if (!mSurfaceValid
) return (gfxContentType
)-1;
202 return (gfxContentType
)cairo_surface_get_content(mSurface
);
205 void gfxASurface::SetDeviceOffset(const gfxPoint
& offset
) {
206 if (!mSurfaceValid
) return;
207 cairo_surface_set_device_offset(mSurface
, offset
.x
, offset
.y
);
210 gfxPoint
gfxASurface::GetDeviceOffset() const {
211 if (!mSurfaceValid
) return gfxPoint(0.0, 0.0);
213 cairo_surface_get_device_offset(mSurface
, &pt
.x
, &pt
.y
);
217 void gfxASurface::Flush() const {
218 if (!mSurfaceValid
) return;
219 cairo_surface_flush(mSurface
);
220 gfxPlatform::ClearSourceSurfaceForSurface(const_cast<gfxASurface
*>(this));
223 void gfxASurface::MarkDirty() {
224 if (!mSurfaceValid
) return;
225 cairo_surface_mark_dirty(mSurface
);
226 gfxPlatform::ClearSourceSurfaceForSurface(this);
229 void gfxASurface::MarkDirty(const gfxRect
& r
) {
230 if (!mSurfaceValid
) return;
231 cairo_surface_mark_dirty_rectangle(mSurface
, (int)r
.X(), (int)r
.Y(),
232 (int)r
.Width(), (int)r
.Height());
233 gfxPlatform::ClearSourceSurfaceForSurface(this);
236 void gfxASurface::SetData(const cairo_user_data_key_t
* key
, void* user_data
,
237 thebes_destroy_func_t destroy
) {
238 if (!mSurfaceValid
) return;
239 cairo_surface_set_user_data(mSurface
, key
, user_data
, destroy
);
242 void* gfxASurface::GetData(const cairo_user_data_key_t
* key
) {
243 if (!mSurfaceValid
) return nullptr;
244 return cairo_surface_get_user_data(mSurface
, key
);
247 void gfxASurface::Finish() {
248 // null surfaces are allowed here
249 cairo_surface_finish(mSurface
);
252 already_AddRefed
<gfxImageSurface
> gfxASurface::CopyToARGB32ImageSurface() {
253 if (!mSurface
|| !mSurfaceValid
) {
257 const IntSize size
= GetSize();
258 RefPtr
<gfxImageSurface
> imgSurface
=
259 new gfxImageSurface(size
, SurfaceFormat::A8R8G8B8_UINT32
);
261 RefPtr
<DrawTarget
> dt
= gfxPlatform::CreateDrawTargetForSurface(
262 imgSurface
, IntSize(size
.width
, size
.height
));
263 RefPtr
<SourceSurface
> source
=
264 gfxPlatform::GetSourceSurfaceForSurface(dt
, this);
266 dt
->CopySurface(source
, IntRect(0, 0, size
.width
, size
.height
), IntPoint());
268 return imgSurface
.forget();
271 int gfxASurface::CairoStatus() {
272 if (!mSurfaceValid
) return -1;
274 return cairo_surface_status(mSurface
);
277 nsresult
gfxASurface::BeginPrinting(const nsAString
& aTitle
,
278 const nsAString
& aPrintToFileName
) {
282 nsresult
gfxASurface::EndPrinting() { return NS_OK
; }
284 nsresult
gfxASurface::AbortPrinting() { return NS_OK
; }
286 nsresult
gfxASurface::BeginPage() { return NS_OK
; }
288 nsresult
gfxASurface::EndPage() { return NS_OK
; }
290 gfxContentType
gfxASurface::ContentFromFormat(gfxImageFormat format
) {
292 case SurfaceFormat::A8R8G8B8_UINT32
:
293 return gfxContentType::COLOR_ALPHA
;
294 case SurfaceFormat::X8R8G8B8_UINT32
:
295 case SurfaceFormat::R5G6B5_UINT16
:
296 return gfxContentType::COLOR
;
297 case SurfaceFormat::A8
:
298 return gfxContentType::ALPHA
;
300 case SurfaceFormat::UNKNOWN
:
302 return gfxContentType::COLOR
;
306 int32_t gfxASurface::BytePerPixelFromFormat(gfxImageFormat format
) {
308 case SurfaceFormat::A8R8G8B8_UINT32
:
309 case SurfaceFormat::X8R8G8B8_UINT32
:
311 case SurfaceFormat::R5G6B5_UINT16
:
313 case SurfaceFormat::A8
:
316 NS_WARNING("Unknown byte per pixel value for Image format");
321 /** Memory reporting **/
323 static const char* sDefaultSurfaceDescription
=
324 "Memory used by gfx surface of the given type.";
326 struct SurfaceMemoryReporterAttrs
{
328 const char* description
;
331 static const SurfaceMemoryReporterAttrs sSurfaceMemoryReporterAttrs
[] = {
332 {"gfx-surface-image", nullptr},
333 {"gfx-surface-pdf", nullptr},
334 {"gfx-surface-ps", nullptr},
336 "Memory used by xlib surfaces to store pixmaps. This memory lives in "
337 "the X server's process rather than in this application, so the bytes "
338 "accounted for here aren't counted in vsize, resident, explicit, or any "
340 "the other measurements on this page."},
341 {"gfx-surface-xcb", nullptr},
342 {"gfx-surface-glitz???", nullptr}, // should never be used
343 {"gfx-surface-quartz", nullptr},
344 {"gfx-surface-win32", nullptr},
345 {"gfx-surface-beos", nullptr},
346 {"gfx-surface-directfb???", nullptr}, // should never be used
347 {"gfx-surface-svg", nullptr},
348 {"gfx-surface-os2", nullptr},
349 {"gfx-surface-win32printing", nullptr},
350 {"gfx-surface-quartzimage", nullptr},
351 {"gfx-surface-script", nullptr},
352 {"gfx-surface-qpainter", nullptr},
353 {"gfx-surface-recording", nullptr},
354 {"gfx-surface-vg", nullptr},
355 {"gfx-surface-gl", nullptr},
356 {"gfx-surface-drm", nullptr},
357 {"gfx-surface-tee", nullptr},
358 {"gfx-surface-xml", nullptr},
359 {"gfx-surface-skia", nullptr},
360 {"gfx-surface-subsurface", nullptr},
363 static_assert(MOZ_ARRAY_LENGTH(sSurfaceMemoryReporterAttrs
) ==
364 size_t(gfxSurfaceType::Max
),
365 "sSurfaceMemoryReporterAttrs exceeds max capacity");
366 static_assert(uint32_t(CAIRO_SURFACE_TYPE_SKIA
) ==
367 uint32_t(gfxSurfaceType::Skia
),
368 "CAIRO_SURFACE_TYPE_SKIA not equal to gfxSurfaceType::Skia");
370 /* Surface size memory reporting */
372 class SurfaceMemoryReporter final
: public nsIMemoryReporter
{
373 ~SurfaceMemoryReporter() = default;
375 // We can touch this array on several different threads, and we don't
376 // want to introduce memory barriers when recording the memory used. To
377 // assure dynamic race checkers like TSan that this is OK, we use
378 // relaxed memory ordering here.
379 static Atomic
<size_t, Relaxed
>
380 sSurfaceMemoryUsed
[size_t(gfxSurfaceType::Max
)];
383 static void AdjustUsedMemory(gfxSurfaceType aType
, int32_t aBytes
) {
384 // A read-modify-write operation like += would require a memory barrier
385 // here, which would defeat the purpose of using relaxed memory
386 // ordering. So separate out the read and write operations.
387 sSurfaceMemoryUsed
[size_t(aType
)] =
388 sSurfaceMemoryUsed
[size_t(aType
)] + aBytes
;
391 // This memory reporter is sometimes allocated on the compositor thread,
392 // but always released on the main thread, so its refcounting needs to be
394 NS_DECL_THREADSAFE_ISUPPORTS
396 NS_IMETHOD
CollectReports(nsIHandleReportCallback
* aHandleReport
,
397 nsISupports
* aData
, bool aAnonymize
) override
{
398 const size_t len
= ArrayLength(sSurfaceMemoryReporterAttrs
);
399 for (size_t i
= 0; i
< len
; i
++) {
400 int64_t amount
= sSurfaceMemoryUsed
[i
];
403 const char* path
= sSurfaceMemoryReporterAttrs
[i
].path
;
404 const char* desc
= sSurfaceMemoryReporterAttrs
[i
].description
;
406 desc
= sDefaultSurfaceDescription
;
409 aHandleReport
->Callback(""_ns
, nsCString(path
), KIND_OTHER
, UNITS_BYTES
,
410 amount
, nsCString(desc
), aData
);
418 Atomic
<size_t, Relaxed
>
419 SurfaceMemoryReporter::sSurfaceMemoryUsed
[size_t(gfxSurfaceType::Max
)];
421 NS_IMPL_ISUPPORTS(SurfaceMemoryReporter
, nsIMemoryReporter
)
423 void gfxASurface::RecordMemoryUsedForSurfaceType(gfxSurfaceType aType
,
425 if (int(aType
) < 0 || aType
>= gfxSurfaceType::Max
) {
426 NS_WARNING("Invalid type to RecordMemoryUsedForSurfaceType!");
430 static bool registered
= false;
432 RegisterStrongMemoryReporter(new SurfaceMemoryReporter());
436 SurfaceMemoryReporter::AdjustUsedMemory(aType
, aBytes
);
439 void gfxASurface::RecordMemoryUsed(int32_t aBytes
) {
440 RecordMemoryUsedForSurfaceType(GetType(), aBytes
);
441 mBytesRecorded
+= aBytes
;
444 void gfxASurface::RecordMemoryFreed() {
445 if (mBytesRecorded
) {
446 RecordMemoryUsedForSurfaceType(GetType(), -mBytesRecorded
);
451 size_t gfxASurface::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf
) const {
452 // We don't measure mSurface because cairo doesn't allow it.
456 size_t gfxASurface::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf
) const {
457 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf
);
461 uint8_t gfxASurface::BytesPerPixel(gfxImageFormat aImageFormat
) {
462 switch (aImageFormat
) {
463 case SurfaceFormat::A8R8G8B8_UINT32
:
465 case SurfaceFormat::X8R8G8B8_UINT32
:
467 case SurfaceFormat::R5G6B5_UINT16
:
469 case SurfaceFormat::A8
:
471 case SurfaceFormat::UNKNOWN
:
473 MOZ_ASSERT_UNREACHABLE("Not really sure what you want me to say here");
478 void gfxASurface::SetOpaqueRect(const gfxRect
& aRect
) {
479 if (aRect
.IsEmpty()) {
480 mOpaqueRect
= nullptr;
481 } else if (!!mOpaqueRect
) {
482 *mOpaqueRect
= aRect
;
484 mOpaqueRect
= MakeUnique
<gfxRect
>(aRect
);
488 /* static */ const gfxRect
& gfxASurface::GetEmptyOpaqueRect() {
489 static const gfxRect
empty(0, 0, 0, 0);
493 const IntSize
gfxASurface::GetSize() const { return IntSize(-1, -1); }
495 SurfaceFormat
gfxASurface::GetSurfaceFormat() const {
496 if (!mSurfaceValid
) {
497 return SurfaceFormat::UNKNOWN
;
499 return GfxFormatForCairoSurface(mSurface
);
502 already_AddRefed
<gfxImageSurface
> gfxASurface::GetAsImageSurface() {