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
|
// 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 "base/logging.h"
#include "remoting/host/capturer_mac.h"
#include <stddef.h>
#include <OpenGL/CGLMacro.h>
namespace remoting {
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]);
}
flip_buffer_.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* flip_buffer = flip_buffer_.get();
uint8* current_buffer = buffers_[current_buffer_].get();
glReadPixels(0, 0, width_, height_, GL_BGRA, GL_UNSIGNED_BYTE, flip_buffer);
glPopClientAttrib();
// OpenGL reads with a vertical flip, and sadly there is no optimized
// way to get it flipped automatically.
for (int y = 0; y < height_; ++y) {
uint8* flip_row = &(flip_buffer[y * bytes_per_row_]);
uint8* current_row =
&(current_buffer[(height_ - (y + 1)) * bytes_per_row_]);
memcpy(current_row, flip_row, bytes_per_row_);
}
DataPlanes planes;
planes.data[0] = buffers_[current_buffer_].get();
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();
}
}
// static
Capturer* Capturer::Create() {
return new CapturerMac();
}
} // namespace remoting
|