Bug 1874684 - Part 28: Return DateDuration from DifferenceISODateTime. r=mgaudet
[gecko.git] / gfx / thebes / gfxASurface.h
blobd2e6daf771450c16efc7a45920333ec3e8782c1d
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 * Returns an image surface for this surface, or nullptr if not supported.
78 * This will not copy image data, just wraps an image surface around
79 * pixel data already available in memory.
81 virtual already_AddRefed<gfxImageSurface> GetAsImageSurface();
83 /**
84 * Creates a new ARGB32 image surface with the same contents as this surface.
85 * Returns null on error.
87 already_AddRefed<gfxImageSurface> CopyToARGB32ImageSurface();
89 int CairoStatus();
91 static gfxContentType ContentFromFormat(gfxImageFormat format);
93 /**
94 * Record number of bytes for given surface type. Use positive bytes
95 * for allocations and negative bytes for deallocations.
97 static void RecordMemoryUsedForSurfaceType(gfxSurfaceType aType,
98 int32_t aBytes);
101 * Same as above, but use current surface type as returned by GetType().
102 * The bytes will be accumulated until RecordMemoryFreed is called,
103 * in which case the value that was recorded for this surface will
104 * be freed.
106 void RecordMemoryUsed(int32_t aBytes);
107 void RecordMemoryFreed();
109 virtual int32_t KnownMemoryUsed() { return mBytesRecorded; }
111 virtual size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
112 virtual size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
113 // gfxASurface has many sub-classes. This method indicates if a sub-class
114 // is capable of measuring its own size accurately. If not, the caller
115 // must fall back to a computed size. (Note that gfxASurface can actually
116 // measure itself, but we must |return false| here because it serves as the
117 // (conservative) default for all the sub-classes. Therefore, this
118 // function should only be called on a |gfxASurface*| that actually points
119 // to a sub-class of gfxASurface.)
120 virtual bool SizeOfIsMeasured() const { return false; }
122 static int32_t BytePerPixelFromFormat(gfxImageFormat format);
124 virtual const mozilla::gfx::IntSize GetSize() const;
126 virtual mozilla::gfx::SurfaceFormat GetSurfaceFormat() const;
128 void SetOpaqueRect(const gfxRect& aRect);
130 const gfxRect& GetOpaqueRect() {
131 if (!!mOpaqueRect) return *mOpaqueRect;
132 return GetEmptyOpaqueRect();
135 static uint8_t BytesPerPixel(gfxImageFormat aImageFormat);
137 protected:
138 gfxASurface();
140 static gfxASurface* GetSurfaceWrapper(cairo_surface_t* csurf);
141 static void SetSurfaceWrapper(cairo_surface_t* csurf, gfxASurface* asurf);
143 // NB: Init() *must* be called from within subclass's
144 // constructors. It's unsafe to call it after the ctor finishes;
145 // leaks and use-after-frees are possible.
146 void Init(cairo_surface_t* surface, bool existingSurface = false);
148 // out-of-line helper to allow GetOpaqueRect() to be inlined
149 // without including gfxRect.h here
150 static const gfxRect& GetEmptyOpaqueRect();
152 virtual ~gfxASurface();
154 cairo_surface_t* mSurface;
155 mozilla::UniquePtr<gfxRect> mOpaqueRect;
157 private:
158 static void SurfaceDestroyFunc(void* data);
160 int32_t mFloatingRefs;
161 int32_t mBytesRecorded;
163 protected:
164 bool mSurfaceValid;
168 * An Unknown surface; used to wrap unknown cairo_surface_t returns from cairo
170 class gfxUnknownSurface : public gfxASurface {
171 public:
172 gfxUnknownSurface(cairo_surface_t* surf, const mozilla::gfx::IntSize& aSize)
173 : mSize(aSize) {
174 Init(surf, true);
177 virtual ~gfxUnknownSurface() = default;
178 const mozilla::gfx::IntSize GetSize() const override { return mSize; }
180 private:
181 mozilla::gfx::IntSize mSize;
184 #endif /* GFX_ASURFACE_H */