Coverage Report

Created: 2018-09-25 14:53

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