Bug 1728955: part 6) Log result of Windows' `OleSetClipboardResult`. r=masayuki
[gecko.git] / gfx / thebes / gfxASurface.cpp
blobf0ff2334bd5dd9936007bf63a06e6cfc8a0f6b57
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"
7 #include "nsMemory.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"
23 #include "gfxRect.h"
25 #include "cairo.h"
26 #include <algorithm>
28 #ifdef CAIRO_HAS_WIN32_SURFACE
29 # include "gfxWindowsSurface.h"
30 #endif
32 #ifdef MOZ_X11
33 # include "gfxXlibSurface.h"
34 #endif
36 #ifdef CAIRO_HAS_QUARTZ_SURFACE
37 # include "gfxQuartzSurface.h"
38 #endif
40 #include <stdio.h>
41 #include <limits.h>
43 #include "nsComponentManagerUtils.h"
44 #include "nsISupportsUtils.h"
45 #include "nsCOMPtr.h"
46 #include "nsServiceManagerUtils.h"
47 #include "nsString.h"
49 using namespace mozilla;
50 using namespace mozilla::gfx;
52 static cairo_user_data_key_t gfxasurface_pointer_key;
54 gfxASurface::gfxASurface()
55 : mSurface(nullptr),
56 mFloatingRefs(0),
57 mBytesRecorded(0),
58 mSurfaceValid(false) {
59 MOZ_COUNT_CTOR(gfxASurface);
62 gfxASurface::~gfxASurface() {
63 RecordMemoryFreed();
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) {
71 if (mSurfaceValid) {
72 if (mFloatingRefs) {
73 // eat a floating ref
74 mFloatingRefs--;
75 } else {
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
82 // the gfxASurface
83 return ++mFloatingRefs;
86 nsrefcnt gfxASurface::Release(void) {
87 if (mSurfaceValid) {
88 NS_ASSERTION(
89 mFloatingRefs == 0,
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
94 // goes out of scope.
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!
100 return --refcnt;
102 if (--mFloatingRefs == 0) {
103 delete this;
104 return 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,
112 // data);
113 delete surf;
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) {
124 if (!csurf) return;
125 cairo_surface_set_user_data(csurf, &gfxasurface_pointer_key, asurf,
126 SurfaceDestroyFunc);
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);
135 if (result) {
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);
151 #endif
152 #ifdef MOZ_X11
153 else if (stype == CAIRO_SURFACE_TYPE_XLIB) {
154 result = new gfxXlibSurface(csurf);
156 #endif
157 #ifdef CAIRO_HAS_QUARTZ_SURFACE
158 else if (stype == CAIRO_SURFACE_TYPE_QUARTZ) {
159 result = new gfxQuartzSurface(csurf, aSize);
161 #endif
162 else {
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");
175 mSurface = surface;
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) {
183 mFloatingRefs = 0;
184 } else {
185 mFloatingRefs = 1;
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);
212 gfxPoint pt;
213 cairo_surface_get_device_offset(mSurface, &pt.x, &pt.y);
214 return pt;
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) {
254 return nullptr;
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) {
279 return NS_OK;
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) {
291 switch (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:
301 default:
302 return gfxContentType::COLOR;
306 int32_t gfxASurface::BytePerPixelFromFormat(gfxImageFormat format) {
307 switch (format) {
308 case SurfaceFormat::A8R8G8B8_UINT32:
309 case SurfaceFormat::X8R8G8B8_UINT32:
310 return 4;
311 case SurfaceFormat::R5G6B5_UINT16:
312 return 2;
313 case SurfaceFormat::A8:
314 return 1;
315 default:
316 NS_WARNING("Unknown byte per pixel value for Image format");
318 return 0;
321 /** Memory reporting **/
323 static const char* sDefaultSurfaceDescription =
324 "Memory used by gfx surface of the given type.";
326 struct SurfaceMemoryReporterAttrs {
327 const char* path;
328 const char* description;
331 static const SurfaceMemoryReporterAttrs sSurfaceMemoryReporterAttrs[] = {
332 {"gfx-surface-image", nullptr},
333 {"gfx-surface-pdf", nullptr},
334 {"gfx-surface-ps", nullptr},
335 {"gfx-surface-xlib",
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 "
339 "of "
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)];
382 public:
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
393 // threadsafe.
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];
402 if (amount != 0) {
403 const char* path = sSurfaceMemoryReporterAttrs[i].path;
404 const char* desc = sSurfaceMemoryReporterAttrs[i].description;
405 if (!desc) {
406 desc = sDefaultSurfaceDescription;
409 aHandleReport->Callback(""_ns, nsCString(path), KIND_OTHER, UNITS_BYTES,
410 amount, nsCString(desc), aData);
414 return NS_OK;
418 Atomic<size_t, Relaxed>
419 SurfaceMemoryReporter::sSurfaceMemoryUsed[size_t(gfxSurfaceType::Max)];
421 NS_IMPL_ISUPPORTS(SurfaceMemoryReporter, nsIMemoryReporter)
423 void gfxASurface::RecordMemoryUsedForSurfaceType(gfxSurfaceType aType,
424 int32_t aBytes) {
425 if (int(aType) < 0 || aType >= gfxSurfaceType::Max) {
426 NS_WARNING("Invalid type to RecordMemoryUsedForSurfaceType!");
427 return;
430 static bool registered = false;
431 if (!registered) {
432 RegisterStrongMemoryReporter(new SurfaceMemoryReporter());
433 registered = true;
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);
447 mBytesRecorded = 0;
451 size_t gfxASurface::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
452 // We don't measure mSurface because cairo doesn't allow it.
453 return 0;
456 size_t gfxASurface::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
457 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
460 /* static */
461 uint8_t gfxASurface::BytesPerPixel(gfxImageFormat aImageFormat) {
462 switch (aImageFormat) {
463 case SurfaceFormat::A8R8G8B8_UINT32:
464 return 4;
465 case SurfaceFormat::X8R8G8B8_UINT32:
466 return 4;
467 case SurfaceFormat::R5G6B5_UINT16:
468 return 2;
469 case SurfaceFormat::A8:
470 return 1;
471 case SurfaceFormat::UNKNOWN:
472 default:
473 MOZ_ASSERT_UNREACHABLE("Not really sure what you want me to say here");
474 return 0;
478 void gfxASurface::SetOpaqueRect(const gfxRect& aRect) {
479 if (aRect.IsEmpty()) {
480 mOpaqueRect = nullptr;
481 } else if (!!mOpaqueRect) {
482 *mOpaqueRect = aRect;
483 } else {
484 mOpaqueRect = MakeUnique<gfxRect>(aRect);
488 /* static */ const gfxRect& gfxASurface::GetEmptyOpaqueRect() {
489 static const gfxRect empty(0, 0, 0, 0);
490 return empty;
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() {
503 return nullptr;