1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/capturer.h"
#include <ApplicationServices/ApplicationServices.h>
#include <OpenGL/CGLMacro.h>
#include <OpenGL/OpenGL.h>
#include <stddef.h>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "remoting/host/capturer_helper.h"
namespace remoting {
namespace {
// A class to perform capturing for mac.
class CapturerMac : public Capturer {
public:
CapturerMac();
virtual ~CapturerMac();
// Capturer interface.
virtual void ScreenConfigurationChanged() OVERRIDE;
virtual media::VideoFrame::Format pixel_format() const OVERRIDE;
virtual void ClearInvalidRects() OVERRIDE;
virtual void InvalidateRects(const InvalidRects& inval_rects) OVERRIDE;
virtual void InvalidateScreen(const gfx::Size& size) OVERRIDE;
virtual void InvalidateFullScreen() OVERRIDE;
virtual void CaptureInvalidRects(CaptureCompletedCallback* callback) OVERRIDE;
virtual const gfx::Size& size_most_recent() const OVERRIDE;
private:
void CaptureRects(const InvalidRects& rects,
CaptureCompletedCallback* callback);
void ScreenRefresh(CGRectCount count, const CGRect *rect_array);
void ScreenUpdateMove(CGScreenUpdateMoveDelta delta,
size_t count,
const CGRect *rect_array);
static void ScreenRefreshCallback(CGRectCount count,
const CGRect *rect_array,
void *user_parameter);
static void ScreenUpdateMoveCallback(CGScreenUpdateMoveDelta delta,
size_t count,
const CGRect *rect_array,
void *user_parameter);
static void DisplaysReconfiguredCallback(CGDirectDisplayID display,
CGDisplayChangeSummaryFlags flags,
void *user_parameter);
void ReleaseBuffers();
CGLContextObj cgl_context_;
static const int kNumBuffers = 2;
scoped_array<uint8> buffers_[kNumBuffers];
// A thread-safe list of invalid rectangles, and the size of the most
// recently captured screen.
CapturerHelper helper_;
// Screen size.
int width_;
int height_;
int bytes_per_row_;
// The current buffer with valid data for reading.
int current_buffer_;
// Format of pixels returned in buffer.
media::VideoFrame::Format pixel_format_;
DISALLOW_COPY_AND_ASSIGN(CapturerMac);
};
CapturerMac::CapturerMac()
: cgl_context_(NULL),
width_(0),
height_(0),
bytes_per_row_(0),
current_buffer_(0),
pixel_format_(media::VideoFrame::RGB32) {
// TODO(dmaclach): move this initialization out into session_manager,
// or at least have session_manager call into here to initialize it.
CGError err =
CGRegisterScreenRefreshCallback(CapturerMac::ScreenRefreshCallback,
this);
DCHECK_EQ(err, kCGErrorSuccess);
err = CGScreenRegisterMoveCallback(CapturerMac::ScreenUpdateMoveCallback,
this);
DCHECK_EQ(err, kCGErrorSuccess);
err = CGDisplayRegisterReconfigurationCallback(
CapturerMac::DisplaysReconfiguredCallback, this);
DCHECK_EQ(err, kCGErrorSuccess);
ScreenConfigurationChanged();
InvalidateScreen(gfx::Size(width_, height_));
}
CapturerMac::~CapturerMac() {
ReleaseBuffers();
CGUnregisterScreenRefreshCallback(CapturerMac::ScreenRefreshCallback, this);
CGScreenUnregisterMoveCallback(CapturerMac::ScreenUpdateMoveCallback, this);
CGDisplayRemoveReconfigurationCallback(
CapturerMac::DisplaysReconfiguredCallback, this);
}
void CapturerMac::ReleaseBuffers() {
if (cgl_context_) {
CGLDestroyContext(cgl_context_);
cgl_context_ = NULL;
}
}
void CapturerMac::ScreenConfigurationChanged() {
ReleaseBuffers();
CGDirectDisplayID mainDevice = CGMainDisplayID();
width_ = CGDisplayPixelsWide(mainDevice);
height_ = CGDisplayPixelsHigh(mainDevice);
pixel_format_ = media::VideoFrame::RGB32;
bytes_per_row_ = width_ * sizeof(uint32_t);
size_t buffer_size = height_ * bytes_per_row_;
for (int i = 0; i < kNumBuffers; ++i) {
buffers_[i].reset(new uint8[buffer_size]);
}
CGLPixelFormatAttribute attributes[] = {
kCGLPFAFullScreen,
kCGLPFADisplayMask,
(CGLPixelFormatAttribute)CGDisplayIDToOpenGLDisplayMask(mainDevice),
(CGLPixelFormatAttribute)0
};
CGLPixelFormatObj pixel_format = NULL;
GLint matching_pixel_format_count = 0;
CGLError err = CGLChoosePixelFormat(attributes,
&pixel_format,
&matching_pixel_format_count);
DCHECK_EQ(err, kCGLNoError);
err = CGLCreateContext(pixel_format, NULL, &cgl_context_);
DCHECK_EQ(err, kCGLNoError);
CGLDestroyPixelFormat(pixel_format);
CGLSetFullScreen(cgl_context_);
CGLSetCurrentContext(cgl_context_);
}
media::VideoFrame::Format CapturerMac::pixel_format() const {
return pixel_format_;
}
void CapturerMac::ClearInvalidRects() {
helper_.ClearInvalidRects();
}
void CapturerMac::InvalidateRects(const InvalidRects& inval_rects) {
helper_.InvalidateRects(inval_rects);
}
void CapturerMac::InvalidateScreen(const gfx::Size& size) {
helper_.InvalidateScreen(size);
}
void CapturerMac::InvalidateFullScreen() {
helper_.InvalidateFullScreen();
}
void CapturerMac::CaptureInvalidRects(CaptureCompletedCallback* callback) {
InvalidRects rects;
helper_.SwapInvalidRects(rects);
CGLContextObj CGL_MACRO_CONTEXT = cgl_context_;
glReadBuffer(GL_FRONT);
glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
glPixelStorei(GL_PACK_ALIGNMENT, 4); // Force 4-byte alignment.
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
glPixelStorei(GL_PACK_SKIP_ROWS, 0);
glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
// Read a block of pixels from the frame buffer.
uint8* current_buffer = buffers_[current_buffer_].get();
glReadPixels(0, 0, width_, height_, GL_BGRA, GL_UNSIGNED_BYTE,
current_buffer);
glPopClientAttrib();
DataPlanes planes;
planes.data[0] = buffers_[current_buffer_].get() + height_ * bytes_per_row_;
planes.strides[0] = -bytes_per_row_;
scoped_refptr<CaptureData> data(
new CaptureData(planes, gfx::Size(width_, height_), pixel_format()));
data->mutable_dirty_rects() = rects;
current_buffer_ = (current_buffer_ + 1) % kNumBuffers;
helper_.set_size_most_recent(data->size());
callback->Run(data);
delete callback;
}
const gfx::Size& CapturerMac::size_most_recent() const {
return helper_.size_most_recent();
}
void CapturerMac::ScreenRefresh(CGRectCount count, const CGRect *rect_array) {
InvalidRects rects;
for (CGRectCount i = 0; i < count; ++i) {
rects.insert(gfx::Rect(rect_array[i]));
}
InvalidateRects(rects);
}
void CapturerMac::ScreenUpdateMove(CGScreenUpdateMoveDelta delta,
size_t count,
const CGRect *rect_array) {
InvalidRects rects;
for (CGRectCount i = 0; i < count; ++i) {
CGRect rect = rect_array[i];
rects.insert(gfx::Rect(rect));
rect = CGRectOffset(rect, delta.dX, delta.dY);
rects.insert(gfx::Rect(rect));
}
InvalidateRects(rects);
}
void CapturerMac::ScreenRefreshCallback(CGRectCount count,
const CGRect *rect_array,
void *user_parameter) {
CapturerMac *capturer = reinterpret_cast<CapturerMac *>(user_parameter);
capturer->ScreenRefresh(count, rect_array);
}
void CapturerMac::ScreenUpdateMoveCallback(CGScreenUpdateMoveDelta delta,
size_t count,
const CGRect *rect_array,
void *user_parameter) {
CapturerMac *capturer = reinterpret_cast<CapturerMac *>(user_parameter);
capturer->ScreenUpdateMove(delta, count, rect_array);
}
void CapturerMac::DisplaysReconfiguredCallback(
CGDirectDisplayID display,
CGDisplayChangeSummaryFlags flags,
void *user_parameter) {
if ((display == CGMainDisplayID()) &&
!(flags & kCGDisplayBeginConfigurationFlag)) {
CapturerMac *capturer = reinterpret_cast<CapturerMac *>(user_parameter);
capturer->ScreenConfigurationChanged();
}
}
} // namespace
// static
Capturer* Capturer::Create() {
return new CapturerMac();
}
} // namespace remoting
|