| 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 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 | 61 |
| 62 XServerPixelBuffer::XServerPixelBuffer() {} | 62 XServerPixelBuffer::XServerPixelBuffer() {} |
| 63 | 63 |
| 64 XServerPixelBuffer::~XServerPixelBuffer() { | 64 XServerPixelBuffer::~XServerPixelBuffer() { |
| 65 Release(); | 65 Release(); |
| 66 } | 66 } |
| 67 | 67 |
| 68 void XServerPixelBuffer::Release() { | 68 void XServerPixelBuffer::Release() { |
| 69 if (x_image_) { | 69 if (x_image_) { |
| 70 XDestroyImage(x_image_); | 70 XDestroyImage(x_image_); |
| 71 x_image_ = NULL; | 71 x_image_ = nullptr; |
| 72 } | 72 } |
| 73 if (shm_pixmap_) { | 73 if (shm_pixmap_) { |
| 74 XFreePixmap(display_, shm_pixmap_); | 74 XFreePixmap(display_, shm_pixmap_); |
| 75 shm_pixmap_ = 0; | 75 shm_pixmap_ = 0; |
| 76 } | 76 } |
| 77 if (shm_gc_) { | 77 if (shm_gc_) { |
| 78 XFreeGC(display_, shm_gc_); | 78 XFreeGC(display_, shm_gc_); |
| 79 shm_gc_ = NULL; | 79 shm_gc_ = nullptr; |
| 80 } | 80 } |
| 81 if (shm_segment_info_) { | 81 |
| 82 if (shm_segment_info_->shmaddr != reinterpret_cast<char*>(-1)) | 82 ReleaseSharedMemorySegment(); |
| 83 shmdt(shm_segment_info_->shmaddr); | 83 |
| 84 if (shm_segment_info_->shmid != -1) | |
| 85 shmctl(shm_segment_info_->shmid, IPC_RMID, 0); | |
| 86 delete shm_segment_info_; | |
| 87 shm_segment_info_ = NULL; | |
| 88 } | |
| 89 window_ = 0; | 84 window_ = 0; |
| 90 } | 85 } |
| 91 | 86 |
| 87 void XServerPixelBuffer::ReleaseSharedMemorySegment() { |
| 88 if (!shm_segment_info_) |
| 89 return; |
| 90 if (shm_segment_info_->shmaddr != nullptr) |
| 91 shmdt(shm_segment_info_->shmaddr); |
| 92 if (shm_segment_info_->shmid != -1) |
| 93 shmctl(shm_segment_info_->shmid, IPC_RMID, 0); |
| 94 delete shm_segment_info_; |
| 95 shm_segment_info_ = nullptr; |
| 96 } |
| 97 |
| 92 bool XServerPixelBuffer::Init(Display* display, Window window) { | 98 bool XServerPixelBuffer::Init(Display* display, Window window) { |
| 93 Release(); | 99 Release(); |
| 94 display_ = display; | 100 display_ = display; |
| 95 | 101 |
| 96 XWindowAttributes attributes; | 102 XWindowAttributes attributes; |
| 97 { | 103 { |
| 98 XErrorTrap error_trap(display_); | 104 XErrorTrap error_trap(display_); |
| 99 if (!XGetWindowAttributes(display_, window, &attributes) || | 105 if (!XGetWindowAttributes(display_, window, &attributes) || |
| 100 error_trap.GetLastErrorAndDisable() != 0) { | 106 error_trap.GetLastErrorAndDisable() != 0) { |
| 101 return false; | 107 return false; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 116 int major, minor; | 122 int major, minor; |
| 117 Bool have_pixmaps; | 123 Bool have_pixmaps; |
| 118 if (!XShmQueryVersion(display_, &major, &minor, &have_pixmaps)) { | 124 if (!XShmQueryVersion(display_, &major, &minor, &have_pixmaps)) { |
| 119 // Shared memory not supported. CaptureRect will use the XImage API instead. | 125 // Shared memory not supported. CaptureRect will use the XImage API instead. |
| 120 return; | 126 return; |
| 121 } | 127 } |
| 122 | 128 |
| 123 bool using_shm = false; | 129 bool using_shm = false; |
| 124 shm_segment_info_ = new XShmSegmentInfo; | 130 shm_segment_info_ = new XShmSegmentInfo; |
| 125 shm_segment_info_->shmid = -1; | 131 shm_segment_info_->shmid = -1; |
| 126 shm_segment_info_->shmaddr = reinterpret_cast<char*>(-1); | 132 shm_segment_info_->shmaddr = nullptr; |
| 127 shm_segment_info_->readOnly = False; | 133 shm_segment_info_->readOnly = False; |
| 128 x_image_ = XShmCreateImage(display_, default_visual, default_depth, ZPixmap, | 134 x_image_ = XShmCreateImage(display_, default_visual, default_depth, ZPixmap, |
| 129 0, shm_segment_info_, window_size_.width(), | 135 0, shm_segment_info_, window_size_.width(), |
| 130 window_size_.height()); | 136 window_size_.height()); |
| 131 if (x_image_) { | 137 if (x_image_) { |
| 132 shm_segment_info_->shmid = shmget( | 138 shm_segment_info_->shmid = |
| 133 IPC_PRIVATE, x_image_->bytes_per_line * x_image_->height, | 139 shmget(IPC_PRIVATE, x_image_->bytes_per_line * x_image_->height, |
| 134 IPC_CREAT | 0600); | 140 IPC_CREAT | 0600); |
| 135 if (shm_segment_info_->shmid != -1) { | 141 if (shm_segment_info_->shmid != -1) { |
| 136 shm_segment_info_->shmaddr = x_image_->data = | 142 void* shmat_result = shmat(shm_segment_info_->shmid, 0, 0); |
| 137 reinterpret_cast<char*>(shmat(shm_segment_info_->shmid, 0, 0)); | 143 if (shmat_result != reinterpret_cast<void*>(-1)) { |
| 138 if (x_image_->data != reinterpret_cast<char*>(-1)) { | 144 shm_segment_info_->shmaddr = reinterpret_cast<char*>(shmat_result); |
| 145 x_image_->data = shm_segment_info_->shmaddr; |
| 146 |
| 139 XErrorTrap error_trap(display_); | 147 XErrorTrap error_trap(display_); |
| 140 using_shm = XShmAttach(display_, shm_segment_info_); | 148 using_shm = XShmAttach(display_, shm_segment_info_); |
| 141 XSync(display_, False); | 149 XSync(display_, False); |
| 142 if (error_trap.GetLastErrorAndDisable() != 0) | 150 if (error_trap.GetLastErrorAndDisable() != 0) |
| 143 using_shm = false; | 151 using_shm = false; |
| 144 if (using_shm) { | 152 if (using_shm) { |
| 145 LOG(LS_VERBOSE) << "Using X shared memory segment " | 153 LOG(LS_VERBOSE) << "Using X shared memory segment " |
| 146 << shm_segment_info_->shmid; | 154 << shm_segment_info_->shmid; |
| 147 } | 155 } |
| 148 } | 156 } |
| 149 } else { | 157 } else { |
| 150 LOG(LS_WARNING) << "Failed to get shared memory segment. " | 158 LOG(LS_WARNING) << "Failed to get shared memory segment. " |
| 151 "Performance may be degraded."; | 159 "Performance may be degraded."; |
| 152 } | 160 } |
| 153 } | 161 } |
| 154 | 162 |
| 155 if (!using_shm) { | 163 if (!using_shm) { |
| 156 LOG(LS_WARNING) << "Not using shared memory. Performance may be degraded."; | 164 LOG(LS_WARNING) << "Not using shared memory. Performance may be degraded."; |
| 157 Release(); | 165 ReleaseSharedMemorySegment(); |
| 158 return; | 166 return; |
| 159 } | 167 } |
| 160 | 168 |
| 161 if (have_pixmaps) | 169 if (have_pixmaps) |
| 162 have_pixmaps = InitPixmaps(default_depth); | 170 have_pixmaps = InitPixmaps(default_depth); |
| 163 | 171 |
| 164 shmctl(shm_segment_info_->shmid, IPC_RMID, 0); | 172 shmctl(shm_segment_info_->shmid, IPC_RMID, 0); |
| 165 shm_segment_info_->shmid = -1; | 173 shm_segment_info_->shmid = -1; |
| 166 | 174 |
| 167 LOG(LS_VERBOSE) << "Using X shared memory extension v" | 175 LOG(LS_VERBOSE) << "Using X shared memory extension v" |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 // Write as 32-bit RGB. | 339 // Write as 32-bit RGB. |
| 332 dst_pos_32[x] = ((r >> 8) & 0xff0000) | ((g >> 16) & 0xff00) | | 340 dst_pos_32[x] = ((r >> 8) & 0xff0000) | ((g >> 16) & 0xff00) | |
| 333 ((b >> 24) & 0xff); | 341 ((b >> 24) & 0xff); |
| 334 } | 342 } |
| 335 dst_pos += frame->stride(); | 343 dst_pos += frame->stride(); |
| 336 src_pos += src_stride; | 344 src_pos += src_stride; |
| 337 } | 345 } |
| 338 } | 346 } |
| 339 | 347 |
| 340 } // namespace webrtc | 348 } // namespace webrtc |
| OLD | NEW |