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
13 # include <X11/extensions/Xrender.h>
14 # include <X11/Xlib.h>
15 # include "X11UndefineNone.h"
19 typedef struct _cairo cairo_t
;
25 /* This is a helper class that let's you borrow a cairo_t from a
26 * DrawTargetCairo. This is used for drawing themed widgets.
28 * Callers should check the cr member after constructing the object
29 * to see if it succeeded. The DrawTarget should not be used while
30 * the context is borrowed. */
31 class BorrowedCairoContext
{
33 BorrowedCairoContext() : mCairo(nullptr), mDT(nullptr) {}
35 explicit BorrowedCairoContext(DrawTarget
* aDT
) : mDT(aDT
) {
36 mCairo
= BorrowCairoContextFromDrawTarget(aDT
);
39 // We can optionally Init after construction in
40 // case we don't know what the DT will be at construction
42 cairo_t
* Init(DrawTarget
* aDT
) {
43 MOZ_ASSERT(!mDT
, "Can't initialize twice!");
45 return mCairo
= BorrowCairoContextFromDrawTarget(aDT
);
48 // The caller needs to call Finish if cr is non-null when
49 // they are done with the context. This is currently explicit
50 // instead of happening implicitly in the destructor to make
51 // what's happening in the caller more clear. It also
52 // let's you resume using the DrawTarget in the same scope.
55 ReturnCairoContextToDrawTarget(mDT
, mCairo
);
60 ~BorrowedCairoContext() { MOZ_ASSERT(!mCairo
); }
65 static cairo_t
* BorrowCairoContextFromDrawTarget(DrawTarget
* aDT
);
66 static void ReturnCairoContextToDrawTarget(DrawTarget
* aDT
, cairo_t
* aCairo
);
71 /* This is a helper class that let's you borrow an Xlib drawable from
72 * a DrawTarget. This is used for drawing themed widgets.
74 * Callers should check the Xlib drawable after constructing the object
75 * to see if it succeeded. The DrawTarget should not be used while
76 * the drawable is borrowed. */
77 class BorrowedXlibDrawable
{
79 BorrowedXlibDrawable()
85 mXRenderFormat(nullptr) {}
87 explicit BorrowedXlibDrawable(DrawTarget
* aDT
)
93 mXRenderFormat(nullptr) {
97 // We can optionally Init after construction in
98 // case we don't know what the DT will be at construction
100 bool Init(DrawTarget
* aDT
);
102 // The caller needs to call Finish if drawable is non-zero when
103 // they are done with the context. This is currently explicit
104 // instead of happening implicitly in the destructor to make
105 // what's happening in the caller more clear. It also
106 // let's you resume using the DrawTarget in the same scope.
109 ~BorrowedXlibDrawable() { MOZ_ASSERT(!mDrawable
); }
111 Display
* GetDisplay() const { return mDisplay
; }
112 Drawable
GetDrawable() const { return mDrawable
; }
113 Screen
* GetScreen() const { return mScreen
; }
114 Visual
* GetVisual() const { return mVisual
; }
115 IntSize
GetSize() const { return mSize
; }
116 Point
GetOffset() const { return mOffset
; }
118 XRenderPictFormat
* GetXRenderFormat() const { return mXRenderFormat
; }
126 XRenderPictFormat
* mXRenderFormat
;
133 /* This is a helper class that let's you borrow a CGContextRef from a
134 * DrawTargetCG. This is used for drawing themed widgets.
136 * Callers should check the cg member after constructing the object
137 * to see if it succeeded. The DrawTarget should not be used while
138 * the context is borrowed. */
139 class BorrowedCGContext
{
141 BorrowedCGContext() : cg(nullptr), mDT(nullptr) {}
143 explicit BorrowedCGContext(DrawTarget
* aDT
) : mDT(aDT
) {
144 MOZ_ASSERT(aDT
, "Caller should check for nullptr");
145 cg
= BorrowCGContextFromDrawTarget(aDT
);
148 // We can optionally Init after construction in
149 // case we don't know what the DT will be at construction
151 CGContextRef
Init(DrawTarget
* aDT
) {
152 MOZ_ASSERT(aDT
, "Caller should check for nullptr");
153 MOZ_ASSERT(!mDT
, "Can't initialize twice!");
155 cg
= BorrowCGContextFromDrawTarget(aDT
);
159 // The caller needs to call Finish if cg is non-null when
160 // they are done with the context. This is currently explicit
161 // instead of happening implicitly in the destructor to make
162 // what's happening in the caller more clear. It also
163 // let's you resume using the DrawTarget in the same scope.
166 ReturnCGContextToDrawTarget(mDT
, cg
);
171 ~BorrowedCGContext() { MOZ_ASSERT(!cg
); }
177 static CGContextRef
BorrowCGContextFromDrawTarget(DrawTarget
* aDT
);
178 static void ReturnCGContextToDrawTarget(DrawTarget
* aDT
, CGContextRef cg
);
180 static CGContextRef
BorrowCGContextFromDrawTarget(DrawTarget
* aDT
) {
181 MOZ_CRASH("Not supported without Skia");
184 static void ReturnCGContextToDrawTarget(DrawTarget
* aDT
, CGContextRef cg
) {
185 MOZ_CRASH("not supported without Skia");
193 } // namespace mozilla
195 #endif // _MOZILLA_GFX_BORROWED_CONTEXT_H