2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #include "webrtc/modules/desktop_capture/x11/x_server_pixel_buffer.h"
17 #include "webrtc/modules/desktop_capture/desktop_frame.h"
18 #include "webrtc/modules/desktop_capture/x11/x_error_trap.h"
19 #include "webrtc/system_wrappers/interface/logging.h"
23 // Returns the number of bits |mask| has to be shifted left so its last
24 // (most-significant) bit set becomes the most-significant bit of the word.
25 // When |mask| is 0 the function returns 31.
26 uint32_t MaskToShift(uint32_t mask) {
28 if ((mask & 0xffff0000u) == 0) {
32 if ((mask & 0xff000000u) == 0) {
36 if ((mask & 0xf0000000u) == 0) {
40 if ((mask & 0xc0000000u) == 0) {
44 if ((mask & 0x80000000u) == 0)
50 // Returns true if |image| is in RGB format.
51 bool IsXImageRGBFormat(XImage* image) {
52 return image->bits_per_pixel == 32 &&
53 image->red_mask == 0xff0000 &&
54 image->green_mask == 0xff00 &&
55 image->blue_mask == 0xff;
62 XServerPixelBuffer::XServerPixelBuffer()
63 : display_(NULL), window_(0),
65 shm_segment_info_(NULL), shm_pixmap_(0), shm_gc_(NULL) {
68 XServerPixelBuffer::~XServerPixelBuffer() {
72 void XServerPixelBuffer::Release() {
74 XDestroyImage(x_image_);
78 XFreePixmap(display_, shm_pixmap_);
82 XFreeGC(display_, shm_gc_);
85 if (shm_segment_info_) {
86 if (shm_segment_info_->shmaddr != reinterpret_cast<char*>(-1))
87 shmdt(shm_segment_info_->shmaddr);
88 if (shm_segment_info_->shmid != -1)
89 shmctl(shm_segment_info_->shmid, IPC_RMID, 0);
90 delete shm_segment_info_;
91 shm_segment_info_ = NULL;
96 bool XServerPixelBuffer::Init(Display* display, Window window) {
100 XWindowAttributes attributes;
102 XErrorTrap error_trap(display_);
103 if (!XGetWindowAttributes(display_, window, &attributes) ||
104 error_trap.GetLastErrorAndDisable() != 0) {
109 window_size_ = DesktopSize(attributes.width, attributes.height);
116 void XServerPixelBuffer::InitShm(const XWindowAttributes& attributes) {
117 Visual* default_visual = attributes.visual;
118 int default_depth = attributes.depth;
122 if (!XShmQueryVersion(display_, &major, &minor, &have_pixmaps)) {
123 // Shared memory not supported. CaptureRect will use the XImage API instead.
127 bool using_shm = false;
128 shm_segment_info_ = new XShmSegmentInfo;
129 shm_segment_info_->shmid = -1;
130 shm_segment_info_->shmaddr = reinterpret_cast<char*>(-1);
131 shm_segment_info_->readOnly = False;
132 x_image_ = XShmCreateImage(display_, default_visual, default_depth, ZPixmap,
133 0, shm_segment_info_, window_size_.width(),
134 window_size_.height());
136 shm_segment_info_->shmid = shmget(
137 IPC_PRIVATE, x_image_->bytes_per_line * x_image_->height,
139 if (shm_segment_info_->shmid != -1) {
140 shm_segment_info_->shmaddr = x_image_->data =
141 reinterpret_cast<char*>(shmat(shm_segment_info_->shmid, 0, 0));
142 if (x_image_->data != reinterpret_cast<char*>(-1)) {
143 XErrorTrap error_trap(display_);
144 using_shm = XShmAttach(display_, shm_segment_info_);
145 XSync(display_, False);
146 if (error_trap.GetLastErrorAndDisable() != 0)
149 LOG(LS_VERBOSE) << "Using X shared memory segment "
150 << shm_segment_info_->shmid;
154 LOG(LS_WARNING) << "Failed to get shared memory segment. "
155 "Performance may be degraded.";
160 LOG(LS_WARNING) << "Not using shared memory. Performance may be degraded.";
166 have_pixmaps = InitPixmaps(default_depth);
168 shmctl(shm_segment_info_->shmid, IPC_RMID, 0);
169 shm_segment_info_->shmid = -1;
171 LOG(LS_VERBOSE) << "Using X shared memory extension v"
172 << major << "." << minor
173 << " with" << (have_pixmaps ? "" : "out") << " pixmaps.";
176 bool XServerPixelBuffer::InitPixmaps(int depth) {
177 if (XShmPixmapFormat(display_) != ZPixmap)
181 XErrorTrap error_trap(display_);
182 shm_pixmap_ = XShmCreatePixmap(display_, window_,
183 shm_segment_info_->shmaddr,
185 window_size_.width(),
186 window_size_.height(), depth);
187 XSync(display_, False);
188 if (error_trap.GetLastErrorAndDisable() != 0) {
189 // |shm_pixmap_| is not not valid because the request was not processed
190 // by the X Server, so zero it.
197 XErrorTrap error_trap(display_);
198 XGCValues shm_gc_values;
199 shm_gc_values.subwindow_mode = IncludeInferiors;
200 shm_gc_values.graphics_exposures = False;
201 shm_gc_ = XCreateGC(display_, window_,
202 GCSubwindowMode | GCGraphicsExposures,
204 XSync(display_, False);
205 if (error_trap.GetLastErrorAndDisable() != 0) {
206 XFreePixmap(display_, shm_pixmap_);
208 shm_gc_ = 0; // See shm_pixmap_ comment above.
216 bool XServerPixelBuffer::IsWindowValid() const {
217 XWindowAttributes attributes;
219 XErrorTrap error_trap(display_);
220 if (!XGetWindowAttributes(display_, window_, &attributes) ||
221 error_trap.GetLastErrorAndDisable() != 0) {
228 void XServerPixelBuffer::Synchronize() {
229 if (shm_segment_info_ && !shm_pixmap_) {
230 // XShmGetImage can fail if the display is being reconfigured.
231 XErrorTrap error_trap(display_);
232 XShmGetImage(display_, window_, x_image_, 0, 0, AllPlanes);
236 void XServerPixelBuffer::CaptureRect(const DesktopRect& rect,
237 DesktopFrame* frame) {
238 assert(rect.right() <= window_size_.width());
239 assert(rect.bottom() <= window_size_.height());
243 if (shm_segment_info_) {
245 XCopyArea(display_, window_, shm_pixmap_, shm_gc_,
246 rect.left(), rect.top(), rect.width(), rect.height(),
247 rect.left(), rect.top());
248 XSync(display_, False);
250 data = reinterpret_cast<uint8_t*>(x_image_->data) +
251 rect.top() * x_image_->bytes_per_line +
252 rect.left() * x_image_->bits_per_pixel / 8;
255 XDestroyImage(x_image_);
256 x_image_ = XGetImage(display_, window_, rect.left(), rect.top(),
257 rect.width(), rect.height(), AllPlanes, ZPixmap);
258 data = reinterpret_cast<uint8_t*>(x_image_->data);
261 if (IsXImageRGBFormat(x_image_)) {
262 FastBlit(data, rect, frame);
264 SlowBlit(data, rect, frame);
268 void XServerPixelBuffer::FastBlit(uint8_t* image,
269 const DesktopRect& rect,
270 DesktopFrame* frame) {
271 uint8_t* src_pos = image;
272 int src_stride = x_image_->bytes_per_line;
273 int dst_x = rect.left(), dst_y = rect.top();
275 uint8_t* dst_pos = frame->data() + frame->stride() * dst_y;
276 dst_pos += dst_x * DesktopFrame::kBytesPerPixel;
278 int height = rect.height();
279 int row_bytes = rect.width() * DesktopFrame::kBytesPerPixel;
280 for (int y = 0; y < height; ++y) {
281 memcpy(dst_pos, src_pos, row_bytes);
282 src_pos += src_stride;
283 dst_pos += frame->stride();
287 void XServerPixelBuffer::SlowBlit(uint8_t* image,
288 const DesktopRect& rect,
289 DesktopFrame* frame) {
290 int src_stride = x_image_->bytes_per_line;
291 int dst_x = rect.left(), dst_y = rect.top();
292 int width = rect.width(), height = rect.height();
294 uint32_t red_mask = x_image_->red_mask;
295 uint32_t green_mask = x_image_->red_mask;
296 uint32_t blue_mask = x_image_->blue_mask;
298 uint32_t red_shift = MaskToShift(red_mask);
299 uint32_t green_shift = MaskToShift(green_mask);
300 uint32_t blue_shift = MaskToShift(blue_mask);
302 int bits_per_pixel = x_image_->bits_per_pixel;
304 uint8_t* dst_pos = frame->data() + frame->stride() * dst_y;
305 uint8_t* src_pos = image;
306 dst_pos += dst_x * DesktopFrame::kBytesPerPixel;
307 // TODO(hclam): Optimize, perhaps using MMX code or by converting to
309 // TODO(sergeyu): This code doesn't handle XImage byte order properly and
310 // won't work with 24bpp images. Fix it.
311 for (int y = 0; y < height; y++) {
312 uint32_t* dst_pos_32 = reinterpret_cast<uint32_t*>(dst_pos);
313 uint32_t* src_pos_32 = reinterpret_cast<uint32_t*>(src_pos);
314 uint16_t* src_pos_16 = reinterpret_cast<uint16_t*>(src_pos);
315 for (int x = 0; x < width; x++) {
316 // Dereference through an appropriately-aligned pointer.
318 if (bits_per_pixel == 32) {
319 pixel = src_pos_32[x];
320 } else if (bits_per_pixel == 16) {
321 pixel = src_pos_16[x];
325 uint32_t r = (pixel & red_mask) << red_shift;
326 uint32_t g = (pixel & green_mask) << green_shift;
327 uint32_t b = (pixel & blue_mask) << blue_shift;
328 // Write as 32-bit RGB.
329 dst_pos_32[x] = ((r >> 8) & 0xff0000) | ((g >> 16) & 0xff00) |
332 dst_pos += frame->stride();
333 src_pos += src_stride;
337 } // namespace webrtc