Bug 1703443 - pt 6. Move RunNextCollectorTimer() into CCGCScheduler r=smaug
[gecko.git] / gfx / thebes / gfxASurface.h
blob4968c6b19e329c5340aa24fcabf74b7f04d3d51b
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 #ifndef GFX_ASURFACE_H
7 #define GFX_ASURFACE_H
9 #include "mozilla/MemoryReporting.h"
10 #include "mozilla/UniquePtr.h"
12 #include "gfxPoint.h"
13 #include "gfxRect.h"
14 #include "gfxTypes.h"
15 #include "nscore.h"
16 #include "nsSize.h"
17 #include "mozilla/gfx/Rect.h"
19 #include "nsStringFwd.h"
21 class gfxImageSurface;
23 template <typename T>
24 struct already_AddRefed;
26 /**
27 * A surface is something you can draw on. Instantiate a subclass of this
28 * abstract class, and use gfxContext to draw on this surface.
30 class gfxASurface {
31 public:
32 #ifdef MOZILLA_INTERNAL_API
33 nsrefcnt AddRef(void);
34 nsrefcnt Release(void);
35 #else
36 virtual nsrefcnt AddRef(void);
37 virtual nsrefcnt Release(void);
38 #endif
40 public:
41 /** Wrap the given cairo surface and return a gfxASurface for it.
42 * This adds a reference to csurf (owned by the returned gfxASurface).
44 static already_AddRefed<gfxASurface> Wrap(
45 cairo_surface_t* csurf,
46 const mozilla::gfx::IntSize& aSize = mozilla::gfx::IntSize(-1, -1));
48 /*** this DOES NOT addref the surface */
49 cairo_surface_t* CairoSurface() { return mSurface; }
51 gfxSurfaceType GetType() const;
53 gfxContentType GetContentType() const;
55 void SetDeviceOffset(const gfxPoint& offset);
56 gfxPoint GetDeviceOffset() const;
58 void Flush() const;
59 void MarkDirty();
60 void MarkDirty(const gfxRect& r);
62 /* Printing backend functions */
63 virtual nsresult BeginPrinting(const nsAString& aTitle,
64 const nsAString& aPrintToFileName);
65 virtual nsresult EndPrinting();
66 virtual nsresult AbortPrinting();
67 virtual nsresult BeginPage();
68 virtual nsresult EndPage();
70 void SetData(const cairo_user_data_key_t* key, void* user_data,
71 thebes_destroy_func_t destroy);
72 void* GetData(const cairo_user_data_key_t* key);
74 virtual void Finish();
76 /**
77 * Create an offscreen surface that can be efficiently copied into
78 * this surface (at least if tiling is not involved).
79 * Returns null on error.
81 virtual already_AddRefed<gfxASurface> CreateSimilarSurface(
82 gfxContentType aType, const mozilla::gfx::IntSize& aSize);
84 /**
85 * Returns an image surface for this surface, or nullptr if not supported.
86 * This will not copy image data, just wraps an image surface around
87 * pixel data already available in memory.
89 virtual already_AddRefed<gfxImageSurface> GetAsImageSurface();
91 /**
92 * Creates a new ARGB32 image surface with the same contents as this surface.
93 * Returns null on error.
95 already_AddRefed<gfxImageSurface> CopyToARGB32ImageSurface();
97 int CairoStatus();
99 static gfxContentType ContentFromFormat(gfxImageFormat format);
102 * Record number of bytes for given surface type. Use positive bytes
103 * for allocations and negative bytes for deallocations.
105 static void RecordMemoryUsedForSurfaceType(gfxSurfaceType aType,
106 int32_t aBytes);
109 * Same as above, but use current surface type as returned by GetType().
110 * The bytes will be accumulated until RecordMemoryFreed is called,
111 * in which case the value that was recorded for this surface will
112 * be freed.
114 void RecordMemoryUsed(int32_t aBytes);
115 void RecordMemoryFreed();
117 virtual int32_t KnownMemoryUsed() { return mBytesRecorded; }
119 virtual size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
120 virtual size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
121 // gfxASurface has many sub-classes. This method indicates if a sub-class
122 // is capable of measuring its own size accurately. If not, the caller
123 // must fall back to a computed size. (Note that gfxASurface can actually
124 // measure itself, but we must |return false| here because it serves as the
125 // (conservative) default for all the sub-classes. Therefore, this
126 // function should only be called on a |gfxASurface*| that actually points
127 // to a sub-class of gfxASurface.)
128 virtual bool SizeOfIsMeasured() const { return false; }
130 static int32_t BytePerPixelFromFormat(gfxImageFormat format);
132 virtual const mozilla::gfx::IntSize GetSize() const;
134 virtual mozilla::gfx::SurfaceFormat GetSurfaceFormat() const;
136 void SetOpaqueRect(const gfxRect& aRect);
138 const gfxRect& GetOpaqueRect() {
139 if (!!mOpaqueRect) return *mOpaqueRect;
140 return GetEmptyOpaqueRect();
143 static uint8_t BytesPerPixel(gfxImageFormat aImageFormat);
145 protected:
146 gfxASurface();
148 static gfxASurface* GetSurfaceWrapper(cairo_surface_t* csurf);
149 static void SetSurfaceWrapper(cairo_surface_t* csurf, gfxASurface* asurf);
151 // NB: Init() *must* be called from within subclass's
152 // constructors. It's unsafe to call it after the ctor finishes;
153 // leaks and use-after-frees are possible.
154 void Init(cairo_surface_t* surface, bool existingSurface = false);
156 // out-of-line helper to allow GetOpaqueRect() to be inlined
157 // without including gfxRect.h here
158 static const gfxRect& GetEmptyOpaqueRect();
160 virtual ~gfxASurface();
162 cairo_surface_t* mSurface;
163 mozilla::UniquePtr<gfxRect> mOpaqueRect;
165 private:
166 static void SurfaceDestroyFunc(void* data);
168 int32_t mFloatingRefs;
169 int32_t mBytesRecorded;
171 protected:
172 bool mSurfaceValid;
176 * An Unknown surface; used to wrap unknown cairo_surface_t returns from cairo
178 class gfxUnknownSurface : public gfxASurface {
179 public:
180 gfxUnknownSurface(cairo_surface_t* surf, const mozilla::gfx::IntSize& aSize)
181 : mSize(aSize) {
182 Init(surf, true);
185 virtual ~gfxUnknownSurface() = default;
186 const mozilla::gfx::IntSize GetSize() const override { return mSize; }
188 private:
189 mozilla::gfx::IntSize mSize;
192 #endif /* GFX_ASURFACE_H */