Line data Source code
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/. */
5 :
6 : #ifndef _MOZILLA_GFX_BORROWED_CONTEXT_H
7 : #define _MOZILLA_GFX_BORROWED_CONTEXT_H
8 :
9 : #include "2D.h"
10 :
11 : #ifdef MOZ_X11
12 : #include <X11/extensions/Xrender.h>
13 : #include <X11/Xlib.h>
14 : #include "X11UndefineNone.h"
15 : #endif
16 :
17 : struct _cairo;
18 : typedef struct _cairo cairo_t;
19 :
20 : namespace mozilla {
21 :
22 : namespace gfx {
23 :
24 : /* This is a helper class that let's you borrow a cairo_t from a
25 : * DrawTargetCairo. This is used for drawing themed widgets.
26 : *
27 : * Callers should check the cr member after constructing the object
28 : * to see if it succeeded. The DrawTarget should not be used while
29 : * the context is borrowed. */
30 : class BorrowedCairoContext
31 : {
32 : public:
33 : BorrowedCairoContext()
34 : : mCairo(nullptr)
35 : , mDT(nullptr)
36 : { }
37 :
38 0 : explicit BorrowedCairoContext(DrawTarget *aDT)
39 0 : : mDT(aDT)
40 : {
41 0 : mCairo = BorrowCairoContextFromDrawTarget(aDT);
42 0 : }
43 :
44 : // We can optionally Init after construction in
45 : // case we don't know what the DT will be at construction
46 : // time.
47 : cairo_t *Init(DrawTarget *aDT)
48 : {
49 : MOZ_ASSERT(!mDT, "Can't initialize twice!");
50 : mDT = aDT;
51 : return mCairo = BorrowCairoContextFromDrawTarget(aDT);
52 : }
53 :
54 : // The caller needs to call Finish if cr is non-null when
55 : // they are done with the context. This is currently explicit
56 : // instead of happening implicitly in the destructor to make
57 : // what's happening in the caller more clear. It also
58 : // let's you resume using the DrawTarget in the same scope.
59 0 : void Finish()
60 : {
61 0 : if (mCairo) {
62 0 : ReturnCairoContextToDrawTarget(mDT, mCairo);
63 0 : mCairo = nullptr;
64 : }
65 0 : }
66 :
67 0 : ~BorrowedCairoContext() {
68 0 : MOZ_ASSERT(!mCairo);
69 0 : }
70 :
71 : cairo_t *mCairo;
72 : private:
73 : static cairo_t* BorrowCairoContextFromDrawTarget(DrawTarget *aDT);
74 : static void ReturnCairoContextToDrawTarget(DrawTarget *aDT, cairo_t *aCairo);
75 : DrawTarget *mDT;
76 : };
77 :
78 : #ifdef MOZ_X11
79 : /* This is a helper class that let's you borrow an Xlib drawable from
80 : * a DrawTarget. This is used for drawing themed widgets.
81 : *
82 : * Callers should check the Xlib drawable after constructing the object
83 : * to see if it succeeded. The DrawTarget should not be used while
84 : * the drawable is borrowed. */
85 : class BorrowedXlibDrawable
86 : {
87 : public:
88 : BorrowedXlibDrawable()
89 : : mDT(nullptr),
90 : mDisplay(nullptr),
91 : mDrawable(X11None),
92 : mScreen(nullptr),
93 : mVisual(nullptr),
94 : mXRenderFormat(nullptr)
95 : {}
96 :
97 18 : explicit BorrowedXlibDrawable(DrawTarget *aDT)
98 18 : : mDT(nullptr),
99 : mDisplay(nullptr),
100 : mDrawable(X11None),
101 : mScreen(nullptr),
102 : mVisual(nullptr),
103 18 : mXRenderFormat(nullptr)
104 : {
105 18 : Init(aDT);
106 18 : }
107 :
108 : // We can optionally Init after construction in
109 : // case we don't know what the DT will be at construction
110 : // time.
111 : bool Init(DrawTarget *aDT);
112 :
113 : // The caller needs to call Finish if drawable is non-zero when
114 : // they are done with the context. This is currently explicit
115 : // instead of happening implicitly in the destructor to make
116 : // what's happening in the caller more clear. It also
117 : // let's you resume using the DrawTarget in the same scope.
118 : void Finish();
119 :
120 36 : ~BorrowedXlibDrawable() {
121 18 : MOZ_ASSERT(!mDrawable);
122 18 : }
123 :
124 0 : Display *GetDisplay() const { return mDisplay; }
125 18 : Drawable GetDrawable() const { return mDrawable; }
126 0 : Screen *GetScreen() const { return mScreen; }
127 0 : Visual *GetVisual() const { return mVisual; }
128 0 : IntSize GetSize() const { return mSize; }
129 0 : Point GetOffset() const { return mOffset; }
130 :
131 0 : XRenderPictFormat* GetXRenderFormat() const { return mXRenderFormat; }
132 :
133 : private:
134 : DrawTarget *mDT;
135 : Display *mDisplay;
136 : Drawable mDrawable;
137 : Screen *mScreen;
138 : Visual *mVisual;
139 : XRenderPictFormat *mXRenderFormat;
140 : IntSize mSize;
141 : Point mOffset;
142 : };
143 : #endif
144 :
145 : #ifdef XP_DARWIN
146 : /* This is a helper class that let's you borrow a CGContextRef from a
147 : * DrawTargetCG. This is used for drawing themed widgets.
148 : *
149 : * Callers should check the cg member after constructing the object
150 : * to see if it succeeded. The DrawTarget should not be used while
151 : * the context is borrowed. */
152 : class BorrowedCGContext
153 : {
154 : public:
155 : BorrowedCGContext()
156 : : cg(nullptr)
157 : , mDT(nullptr)
158 : { }
159 :
160 : explicit BorrowedCGContext(DrawTarget *aDT)
161 : : mDT(aDT)
162 : {
163 : MOZ_ASSERT(aDT, "Caller should check for nullptr");
164 : cg = BorrowCGContextFromDrawTarget(aDT);
165 : }
166 :
167 : // We can optionally Init after construction in
168 : // case we don't know what the DT will be at construction
169 : // time.
170 : CGContextRef Init(DrawTarget *aDT)
171 : {
172 : MOZ_ASSERT(aDT, "Caller should check for nullptr");
173 : MOZ_ASSERT(!mDT, "Can't initialize twice!");
174 : mDT = aDT;
175 : cg = BorrowCGContextFromDrawTarget(aDT);
176 : return cg;
177 : }
178 :
179 : // The caller needs to call Finish if cg is non-null when
180 : // they are done with the context. This is currently explicit
181 : // instead of happening implicitly in the destructor to make
182 : // what's happening in the caller more clear. It also
183 : // let's you resume using the DrawTarget in the same scope.
184 : void Finish()
185 : {
186 : if (cg) {
187 : ReturnCGContextToDrawTarget(mDT, cg);
188 : cg = nullptr;
189 : }
190 : }
191 :
192 : ~BorrowedCGContext() {
193 : MOZ_ASSERT(!cg);
194 : }
195 :
196 : CGContextRef cg;
197 : private:
198 : #ifdef USE_SKIA
199 : static CGContextRef BorrowCGContextFromDrawTarget(DrawTarget *aDT);
200 : static void ReturnCGContextToDrawTarget(DrawTarget *aDT, CGContextRef cg);
201 : #else
202 : static CGContextRef BorrowCGContextFromDrawTarget(DrawTarget *aDT) {
203 : MOZ_CRASH("Not supported without Skia");
204 : }
205 :
206 : static void ReturnCGContextToDrawTarget(DrawTarget *aDT, CGContextRef cg) {
207 : MOZ_CRASH("not supported without Skia");
208 : }
209 : #endif
210 : DrawTarget *mDT;
211 : };
212 : #endif
213 :
214 : } // namespace gfx
215 : } // namespace mozilla
216 :
217 : #endif // _MOZILLA_GFX_BORROWED_CONTEXT_H
|