Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/desktop_capture/x11/x_server_pixel_buffer.h" | 11 #include "webrtc/modules/desktop_capture/x11/x_server_pixel_buffer.h" |
| 12 | 12 |
| 13 #include <assert.h> | 13 #include <assert.h> |
| 14 #include <string.h> | 14 #include <string.h> |
| 15 #include <sys/shm.h> | 15 #include <sys/shm.h> |
| 16 | 16 |
| 17 #include "webrtc/modules/desktop_capture/desktop_frame.h" | 17 #include "webrtc/modules/desktop_capture/desktop_frame.h" |
| 18 #include "webrtc/modules/desktop_capture/x11/x_error_trap.h" | 18 #include "webrtc/modules/desktop_capture/x11/x_error_trap.h" |
| 19 #include "webrtc/system_wrappers/include/logging.h" | 19 #include "webrtc/system_wrappers/include/logging.h" |
| 20 | 20 |
| 21 namespace webrtc { | |
| 22 | |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 // Returns the number of bits |mask| has to be shifted left so its last | 25 // 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. | 26 // (most-significant) bit set becomes the most-significant bit of the word. |
| 25 // When |mask| is 0 the function returns 31. | 27 // When |mask| is 0 the function returns 31. |
| 26 uint32_t MaskToShift(uint32_t mask) { | 28 uint32_t MaskToShift(uint32_t mask) { |
| 27 int shift = 0; | 29 int shift = 0; |
| 28 if ((mask & 0xffff0000u) == 0) { | 30 if ((mask & 0xffff0000u) == 0) { |
| 29 mask <<= 16; | 31 mask <<= 16; |
| 30 shift += 16; | 32 shift += 16; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 48 } | 50 } |
| 49 | 51 |
| 50 // Returns true if |image| is in RGB format. | 52 // Returns true if |image| is in RGB format. |
| 51 bool IsXImageRGBFormat(XImage* image) { | 53 bool IsXImageRGBFormat(XImage* image) { |
| 52 return image->bits_per_pixel == 32 && | 54 return image->bits_per_pixel == 32 && |
| 53 image->red_mask == 0xff0000 && | 55 image->red_mask == 0xff0000 && |
| 54 image->green_mask == 0xff00 && | 56 image->green_mask == 0xff00 && |
| 55 image->blue_mask == 0xff; | 57 image->blue_mask == 0xff; |
| 56 } | 58 } |
| 57 | 59 |
| 60 // We expose two forms of blitting to handle variations in the pixel format. | |
| 61 // In FastBlit(), the operation is effectively a memcpy. | |
|
GeorgeZ
2017/04/03 18:59:58
Please explain a little of SlowBlit().
| |
| 62 void FastBlit(XImage* x_image, | |
| 63 uint8_t* src_pos, | |
| 64 const DesktopRect& rect, | |
| 65 DesktopFrame* frame) { | |
| 66 int src_stride = x_image->bytes_per_line; | |
| 67 int dst_x = rect.left(), dst_y = rect.top(); | |
| 68 | |
| 69 uint8_t* dst_pos = frame->data() + frame->stride() * dst_y; | |
| 70 dst_pos += dst_x * DesktopFrame::kBytesPerPixel; | |
| 71 | |
| 72 int height = rect.height(); | |
| 73 int row_bytes = rect.width() * DesktopFrame::kBytesPerPixel; | |
| 74 for (int y = 0; y < height; ++y) { | |
| 75 memcpy(dst_pos, src_pos, row_bytes); | |
| 76 src_pos += src_stride; | |
| 77 dst_pos += frame->stride(); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 void SlowBlit(XImage* x_image, | |
| 82 uint8_t* src_pos, | |
| 83 const DesktopRect& rect, | |
| 84 DesktopFrame* frame) { | |
| 85 int src_stride = x_image->bytes_per_line; | |
| 86 int dst_x = rect.left(), dst_y = rect.top(); | |
| 87 int width = rect.width(), height = rect.height(); | |
| 88 | |
| 89 uint32_t red_mask = x_image->red_mask; | |
| 90 uint32_t green_mask = x_image->red_mask; | |
| 91 uint32_t blue_mask = x_image->blue_mask; | |
| 92 | |
| 93 uint32_t red_shift = MaskToShift(red_mask); | |
| 94 uint32_t green_shift = MaskToShift(green_mask); | |
| 95 uint32_t blue_shift = MaskToShift(blue_mask); | |
| 96 | |
| 97 int bits_per_pixel = x_image->bits_per_pixel; | |
| 98 | |
| 99 uint8_t* dst_pos = frame->data() + frame->stride() * dst_y; | |
| 100 dst_pos += dst_x * DesktopFrame::kBytesPerPixel; | |
| 101 // TODO(hclam): Optimize, perhaps using MMX code or by converting to | |
| 102 // YUV directly. | |
| 103 // TODO(sergeyu): This code doesn't handle XImage byte order properly and | |
| 104 // won't work with 24bpp images. Fix it. | |
| 105 for (int y = 0; y < height; y++) { | |
| 106 uint32_t* dst_pos_32 = reinterpret_cast<uint32_t*>(dst_pos); | |
| 107 uint32_t* src_pos_32 = reinterpret_cast<uint32_t*>(src_pos); | |
| 108 uint16_t* src_pos_16 = reinterpret_cast<uint16_t*>(src_pos); | |
| 109 for (int x = 0; x < width; x++) { | |
| 110 // Dereference through an appropriately-aligned pointer. | |
| 111 uint32_t pixel; | |
| 112 if (bits_per_pixel == 32) { | |
| 113 pixel = src_pos_32[x]; | |
| 114 } else if (bits_per_pixel == 16) { | |
| 115 pixel = src_pos_16[x]; | |
| 116 } else { | |
| 117 pixel = src_pos[x]; | |
| 118 } | |
| 119 uint32_t r = (pixel & red_mask) << red_shift; | |
| 120 uint32_t g = (pixel & green_mask) << green_shift; | |
| 121 uint32_t b = (pixel & blue_mask) << blue_shift; | |
| 122 // Write as 32-bit RGB. | |
| 123 dst_pos_32[x] = | |
| 124 ((r >> 8) & 0xff0000) | ((g >> 16) & 0xff00) | ((b >> 24) & 0xff); | |
| 125 } | |
| 126 dst_pos += frame->stride(); | |
| 127 src_pos += src_stride; | |
| 128 } | |
| 129 } | |
| 130 | |
| 58 } // namespace | 131 } // namespace |
| 59 | 132 |
| 60 namespace webrtc { | |
| 61 | |
| 62 XServerPixelBuffer::XServerPixelBuffer() {} | 133 XServerPixelBuffer::XServerPixelBuffer() {} |
| 63 | 134 |
| 64 XServerPixelBuffer::~XServerPixelBuffer() { | 135 XServerPixelBuffer::~XServerPixelBuffer() { |
| 65 Release(); | 136 Release(); |
| 66 } | 137 } |
| 67 | 138 |
| 68 void XServerPixelBuffer::Release() { | 139 void XServerPixelBuffer::Release() { |
| 69 if (x_image_) { | 140 if (x_image_) { |
| 70 XDestroyImage(x_image_); | 141 XDestroyImage(x_image_); |
| 71 x_image_ = nullptr; | 142 x_image_ = nullptr; |
| 72 } | 143 } |
| 144 if (x_shm_image_) { | |
| 145 XDestroyImage(x_shm_image_); | |
| 146 x_shm_image_ = nullptr; | |
| 147 } | |
| 73 if (shm_pixmap_) { | 148 if (shm_pixmap_) { |
| 74 XFreePixmap(display_, shm_pixmap_); | 149 XFreePixmap(display_, shm_pixmap_); |
| 75 shm_pixmap_ = 0; | 150 shm_pixmap_ = 0; |
| 76 } | 151 } |
| 77 if (shm_gc_) { | 152 if (shm_gc_) { |
| 78 XFreeGC(display_, shm_gc_); | 153 XFreeGC(display_, shm_gc_); |
| 79 shm_gc_ = nullptr; | 154 shm_gc_ = nullptr; |
| 80 } | 155 } |
| 81 | 156 |
| 82 ReleaseSharedMemorySegment(); | 157 ReleaseSharedMemorySegment(); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 if (!XShmQueryVersion(display_, &major, &minor, &have_pixmaps)) { | 199 if (!XShmQueryVersion(display_, &major, &minor, &have_pixmaps)) { |
| 125 // Shared memory not supported. CaptureRect will use the XImage API instead. | 200 // Shared memory not supported. CaptureRect will use the XImage API instead. |
| 126 return; | 201 return; |
| 127 } | 202 } |
| 128 | 203 |
| 129 bool using_shm = false; | 204 bool using_shm = false; |
| 130 shm_segment_info_ = new XShmSegmentInfo; | 205 shm_segment_info_ = new XShmSegmentInfo; |
| 131 shm_segment_info_->shmid = -1; | 206 shm_segment_info_->shmid = -1; |
| 132 shm_segment_info_->shmaddr = nullptr; | 207 shm_segment_info_->shmaddr = nullptr; |
| 133 shm_segment_info_->readOnly = False; | 208 shm_segment_info_->readOnly = False; |
| 134 x_image_ = XShmCreateImage(display_, default_visual, default_depth, ZPixmap, | 209 x_shm_image_ = XShmCreateImage(display_, default_visual, default_depth, |
| 135 0, shm_segment_info_, window_size_.width(), | 210 ZPixmap, 0, shm_segment_info_, |
| 136 window_size_.height()); | 211 window_size_.width(), window_size_.height()); |
| 137 if (x_image_) { | 212 if (x_shm_image_) { |
| 138 shm_segment_info_->shmid = | 213 shm_segment_info_->shmid = |
| 139 shmget(IPC_PRIVATE, x_image_->bytes_per_line * x_image_->height, | 214 shmget(IPC_PRIVATE, x_shm_image_->bytes_per_line * x_shm_image_->height, |
| 140 IPC_CREAT | 0600); | 215 IPC_CREAT | 0600); |
| 141 if (shm_segment_info_->shmid != -1) { | 216 if (shm_segment_info_->shmid != -1) { |
| 142 void* shmat_result = shmat(shm_segment_info_->shmid, 0, 0); | 217 void* shmat_result = shmat(shm_segment_info_->shmid, 0, 0); |
| 143 if (shmat_result != reinterpret_cast<void*>(-1)) { | 218 if (shmat_result != reinterpret_cast<void*>(-1)) { |
| 144 shm_segment_info_->shmaddr = reinterpret_cast<char*>(shmat_result); | 219 shm_segment_info_->shmaddr = reinterpret_cast<char*>(shmat_result); |
| 145 x_image_->data = shm_segment_info_->shmaddr; | 220 x_shm_image_->data = shm_segment_info_->shmaddr; |
| 146 | 221 |
| 147 XErrorTrap error_trap(display_); | 222 XErrorTrap error_trap(display_); |
| 148 using_shm = XShmAttach(display_, shm_segment_info_); | 223 using_shm = XShmAttach(display_, shm_segment_info_); |
| 149 XSync(display_, False); | 224 XSync(display_, False); |
| 150 if (error_trap.GetLastErrorAndDisable() != 0) | 225 if (error_trap.GetLastErrorAndDisable() != 0) |
| 151 using_shm = false; | 226 using_shm = false; |
| 152 if (using_shm) { | 227 if (using_shm) { |
| 153 LOG(LS_VERBOSE) << "Using X shared memory segment " | 228 LOG(LS_VERBOSE) << "Using X shared memory segment " |
| 154 << shm_segment_info_->shmid; | 229 << shm_segment_info_->shmid; |
| 155 } | 230 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 228 } | 303 } |
| 229 return true; | 304 return true; |
| 230 } | 305 } |
| 231 | 306 |
| 232 void XServerPixelBuffer::Synchronize() { | 307 void XServerPixelBuffer::Synchronize() { |
| 233 if (shm_segment_info_ && !shm_pixmap_) { | 308 if (shm_segment_info_ && !shm_pixmap_) { |
| 234 // XShmGetImage can fail if the display is being reconfigured. | 309 // XShmGetImage can fail if the display is being reconfigured. |
| 235 XErrorTrap error_trap(display_); | 310 XErrorTrap error_trap(display_); |
| 236 // XShmGetImage fails if the window is partially out of screen. | 311 // XShmGetImage fails if the window is partially out of screen. |
| 237 xshm_get_image_succeeded_ = | 312 xshm_get_image_succeeded_ = |
| 238 XShmGetImage(display_, window_, x_image_, 0, 0, AllPlanes); | 313 XShmGetImage(display_, window_, x_shm_image_, 0, 0, AllPlanes); |
| 239 } | 314 } |
| 240 } | 315 } |
| 241 | 316 |
| 242 bool XServerPixelBuffer::CaptureRect(const DesktopRect& rect, | 317 bool XServerPixelBuffer::CaptureRect(const DesktopRect& rect, |
| 243 DesktopFrame* frame) { | 318 DesktopFrame* frame) { |
| 244 assert(rect.right() <= window_size_.width()); | 319 assert(rect.right() <= window_size_.width()); |
| 245 assert(rect.bottom() <= window_size_.height()); | 320 assert(rect.bottom() <= window_size_.height()); |
| 246 | 321 |
| 322 XImage* image; | |
| 247 uint8_t* data; | 323 uint8_t* data; |
| 248 | 324 |
| 249 if (shm_segment_info_ && (shm_pixmap_ || xshm_get_image_succeeded_)) { | 325 if (shm_segment_info_ && (shm_pixmap_ || xshm_get_image_succeeded_)) { |
| 250 if (shm_pixmap_) { | 326 if (shm_pixmap_) { |
| 251 XCopyArea(display_, window_, shm_pixmap_, shm_gc_, | 327 XCopyArea(display_, window_, shm_pixmap_, shm_gc_, |
| 252 rect.left(), rect.top(), rect.width(), rect.height(), | 328 rect.left(), rect.top(), rect.width(), rect.height(), |
| 253 rect.left(), rect.top()); | 329 rect.left(), rect.top()); |
| 254 XSync(display_, False); | 330 XSync(display_, False); |
| 255 } | 331 } |
| 256 data = reinterpret_cast<uint8_t*>(x_image_->data) + | 332 |
| 257 rect.top() * x_image_->bytes_per_line + | 333 image = x_shm_image_; |
| 258 rect.left() * x_image_->bits_per_pixel / 8; | 334 data = reinterpret_cast<uint8_t*>(image->data) + |
| 335 rect.top() * image->bytes_per_line + | |
| 336 rect.left() * image->bits_per_pixel / 8; | |
| 337 | |
| 259 } else { | 338 } else { |
| 260 if (x_image_) | 339 if (x_image_) |
| 261 XDestroyImage(x_image_); | 340 XDestroyImage(x_image_); |
| 262 x_image_ = XGetImage(display_, window_, rect.left(), rect.top(), | 341 x_image_ = XGetImage(display_, window_, rect.left(), rect.top(), |
| 263 rect.width(), rect.height(), AllPlanes, ZPixmap); | 342 rect.width(), rect.height(), AllPlanes, ZPixmap); |
| 264 if (!x_image_) | 343 if (!x_image_) |
| 265 return false; | 344 return false; |
| 266 | 345 |
| 267 data = reinterpret_cast<uint8_t*>(x_image_->data); | 346 image = x_image_; |
| 347 data = reinterpret_cast<uint8_t*>(image->data); | |
| 268 } | 348 } |
| 269 | 349 |
| 270 if (IsXImageRGBFormat(x_image_)) { | 350 if (IsXImageRGBFormat(image)) { |
| 271 FastBlit(data, rect, frame); | 351 FastBlit(image, data, rect, frame); |
| 272 } else { | 352 } else { |
| 273 SlowBlit(data, rect, frame); | 353 SlowBlit(image, data, rect, frame); |
| 274 } | 354 } |
| 275 | 355 |
| 276 return true; | 356 return true; |
| 277 } | 357 } |
| 278 | 358 |
| 279 void XServerPixelBuffer::FastBlit(uint8_t* image, | |
| 280 const DesktopRect& rect, | |
| 281 DesktopFrame* frame) { | |
| 282 uint8_t* src_pos = image; | |
| 283 int src_stride = x_image_->bytes_per_line; | |
| 284 int dst_x = rect.left(), dst_y = rect.top(); | |
| 285 | |
| 286 uint8_t* dst_pos = frame->data() + frame->stride() * dst_y; | |
| 287 dst_pos += dst_x * DesktopFrame::kBytesPerPixel; | |
| 288 | |
| 289 int height = rect.height(); | |
| 290 int row_bytes = rect.width() * DesktopFrame::kBytesPerPixel; | |
| 291 for (int y = 0; y < height; ++y) { | |
| 292 memcpy(dst_pos, src_pos, row_bytes); | |
| 293 src_pos += src_stride; | |
| 294 dst_pos += frame->stride(); | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 void XServerPixelBuffer::SlowBlit(uint8_t* image, | |
| 299 const DesktopRect& rect, | |
| 300 DesktopFrame* frame) { | |
| 301 int src_stride = x_image_->bytes_per_line; | |
| 302 int dst_x = rect.left(), dst_y = rect.top(); | |
| 303 int width = rect.width(), height = rect.height(); | |
| 304 | |
| 305 uint32_t red_mask = x_image_->red_mask; | |
| 306 uint32_t green_mask = x_image_->red_mask; | |
| 307 uint32_t blue_mask = x_image_->blue_mask; | |
| 308 | |
| 309 uint32_t red_shift = MaskToShift(red_mask); | |
| 310 uint32_t green_shift = MaskToShift(green_mask); | |
| 311 uint32_t blue_shift = MaskToShift(blue_mask); | |
| 312 | |
| 313 int bits_per_pixel = x_image_->bits_per_pixel; | |
| 314 | |
| 315 uint8_t* dst_pos = frame->data() + frame->stride() * dst_y; | |
| 316 uint8_t* src_pos = image; | |
| 317 dst_pos += dst_x * DesktopFrame::kBytesPerPixel; | |
| 318 // TODO(hclam): Optimize, perhaps using MMX code or by converting to | |
| 319 // YUV directly. | |
| 320 // TODO(sergeyu): This code doesn't handle XImage byte order properly and | |
| 321 // won't work with 24bpp images. Fix it. | |
| 322 for (int y = 0; y < height; y++) { | |
| 323 uint32_t* dst_pos_32 = reinterpret_cast<uint32_t*>(dst_pos); | |
| 324 uint32_t* src_pos_32 = reinterpret_cast<uint32_t*>(src_pos); | |
| 325 uint16_t* src_pos_16 = reinterpret_cast<uint16_t*>(src_pos); | |
| 326 for (int x = 0; x < width; x++) { | |
| 327 // Dereference through an appropriately-aligned pointer. | |
| 328 uint32_t pixel; | |
| 329 if (bits_per_pixel == 32) { | |
| 330 pixel = src_pos_32[x]; | |
| 331 } else if (bits_per_pixel == 16) { | |
| 332 pixel = src_pos_16[x]; | |
| 333 } else { | |
| 334 pixel = src_pos[x]; | |
| 335 } | |
| 336 uint32_t r = (pixel & red_mask) << red_shift; | |
| 337 uint32_t g = (pixel & green_mask) << green_shift; | |
| 338 uint32_t b = (pixel & blue_mask) << blue_shift; | |
| 339 // Write as 32-bit RGB. | |
| 340 dst_pos_32[x] = ((r >> 8) & 0xff0000) | ((g >> 16) & 0xff00) | | |
| 341 ((b >> 24) & 0xff); | |
| 342 } | |
| 343 dst_pos += frame->stride(); | |
| 344 src_pos += src_stride; | |
| 345 } | |
| 346 } | |
| 347 | |
| 348 } // namespace webrtc | 359 } // namespace webrtc |
| OLD | NEW |