no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / gfx / 2d / BorrowedContext.h
bloba908b0497c6370c1fa872b80dd6fc0b0cd7814f6
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef _MOZILLA_GFX_BORROWED_CONTEXT_H
8 #define _MOZILLA_GFX_BORROWED_CONTEXT_H
10 #include "2D.h"
12 #ifdef MOZ_X11
13 # include <X11/Xlib.h>
14 # include "X11UndefineNone.h"
15 #endif
17 namespace mozilla {
19 namespace gfx {
21 #ifdef MOZ_X11
22 /* This is a helper class that let's you borrow an Xlib drawable from
23 * a DrawTarget. This is used for drawing themed widgets.
25 * Callers should check the Xlib drawable after constructing the object
26 * to see if it succeeded. The DrawTarget should not be used while
27 * the drawable is borrowed. */
28 class BorrowedXlibDrawable {
29 public:
30 BorrowedXlibDrawable()
31 : mDT(nullptr),
32 mDisplay(nullptr),
33 mDrawable(X11None),
34 mScreen(nullptr),
35 mVisual(nullptr) {}
37 explicit BorrowedXlibDrawable(DrawTarget* aDT)
38 : mDT(nullptr),
39 mDisplay(nullptr),
40 mDrawable(X11None),
41 mScreen(nullptr),
42 mVisual(nullptr) {
43 Init(aDT);
46 // We can optionally Init after construction in
47 // case we don't know what the DT will be at construction
48 // time.
49 bool Init(DrawTarget* aDT);
51 // The caller needs to call Finish if drawable is non-zero when
52 // they are done with the context. This is currently explicit
53 // instead of happening implicitly in the destructor to make
54 // what's happening in the caller more clear. It also
55 // let's you resume using the DrawTarget in the same scope.
56 void Finish();
58 ~BorrowedXlibDrawable() { MOZ_ASSERT(!mDrawable); }
60 Display* GetDisplay() const { return mDisplay; }
61 Drawable GetDrawable() const { return mDrawable; }
62 Screen* GetScreen() const { return mScreen; }
63 Visual* GetVisual() const { return mVisual; }
64 IntSize GetSize() const { return mSize; }
65 Point GetOffset() const { return mOffset; }
67 private:
68 DrawTarget* mDT;
69 Display* mDisplay;
70 Drawable mDrawable;
71 Screen* mScreen;
72 Visual* mVisual;
73 IntSize mSize;
74 Point mOffset;
76 #endif
78 #ifdef XP_DARWIN
79 /* This is a helper class that let's you borrow a CGContextRef from a
80 * DrawTargetCG. This is used for drawing themed widgets.
82 * Callers should check the cg member after constructing the object
83 * to see if it succeeded. The DrawTarget should not be used while
84 * the context is borrowed. */
85 class BorrowedCGContext {
86 public:
87 BorrowedCGContext() : cg(nullptr), mDT(nullptr) {}
89 explicit BorrowedCGContext(DrawTarget* aDT) : mDT(aDT) {
90 MOZ_ASSERT(aDT, "Caller should check for nullptr");
91 cg = BorrowCGContextFromDrawTarget(aDT);
94 // We can optionally Init after construction in
95 // case we don't know what the DT will be at construction
96 // time.
97 CGContextRef Init(DrawTarget* aDT) {
98 MOZ_ASSERT(aDT, "Caller should check for nullptr");
99 MOZ_ASSERT(!mDT, "Can't initialize twice!");
100 mDT = aDT;
101 cg = BorrowCGContextFromDrawTarget(aDT);
102 return cg;
105 // The caller needs to call Finish if cg is non-null when
106 // they are done with the context. This is currently explicit
107 // instead of happening implicitly in the destructor to make
108 // what's happening in the caller more clear. It also
109 // let's you resume using the DrawTarget in the same scope.
110 void Finish() {
111 if (cg) {
112 ReturnCGContextToDrawTarget(mDT, cg);
113 cg = nullptr;
117 ~BorrowedCGContext() { MOZ_ASSERT(!cg); }
119 CGContextRef cg;
121 private:
122 static CGContextRef BorrowCGContextFromDrawTarget(DrawTarget* aDT);
123 static void ReturnCGContextToDrawTarget(DrawTarget* aDT, CGContextRef cg);
124 DrawTarget* mDT;
126 #endif
128 } // namespace gfx
129 } // namespace mozilla
131 #endif // _MOZILLA_GFX_BORROWED_CONTEXT_H