[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "remoting/host/capturer_linux.h" |
| 6 | |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 7 | #include <X11/Xlib.h> |
| 8 | #include <X11/Xutil.h> |
| 9 | #include <X11/extensions/Xdamage.h> |
| 10 | |
| 11 | #include <set> |
| 12 | |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 13 | #include "base/logging.h" |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 14 | #include "remoting/base/types.h" |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 15 | #include "remoting/host/capturer_helper.h" |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 16 | #include "remoting/host/x_server_pixel_buffer.h" |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 17 | |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 18 | namespace remoting { |
| 19 | |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 20 | // Private Implementation pattern to avoid leaking the X11 types into the header |
| 21 | // file. |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 22 | class CapturerLinuxPimpl : public Capturer { |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 23 | public: |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 24 | CapturerLinuxPimpl(); |
| 25 | virtual ~CapturerLinuxPimpl(); |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 26 | |
| 27 | bool Init(); // TODO(ajwong): Do we really want this to be synchronous? |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 28 | |
| 29 | // Capturer interface. |
| 30 | virtual void ScreenConfigurationChanged(); |
| 31 | virtual media::VideoFrame::Format pixel_format() const; |
| 32 | virtual void ClearInvalidRects(); |
| 33 | virtual void InvalidateRects(const InvalidRects& inval_rects); |
| 34 | virtual void InvalidateScreen(const gfx::Size& size); |
| 35 | virtual void InvalidateFullScreen(); |
| 36 | virtual void CaptureInvalidRects(CaptureCompletedCallback* callback); |
| 37 | virtual const gfx::Size& size_most_recent() const; |
| 38 | |
| 39 | private: |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 40 | void CalculateInvalidRects(); |
| 41 | void CaptureRects(const InvalidRects& rects, |
| 42 | Capturer::CaptureCompletedCallback* callback); |
| 43 | |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 44 | void DeinitXlib(); |
| 45 | // We expose two forms of blitting to handle variations in the pixel format. |
| 46 | // In FastBlit, the operation is effectively a memcpy. |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 47 | void FastBlit(uint8* image, const gfx::Rect& rect, CaptureData* capture_data); |
| 48 | void SlowBlit(uint8* image, const gfx::Rect& rect, CaptureData* capture_data); |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 49 | |
| 50 | static const int kBytesPerPixel = 4; |
| 51 | |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 52 | // X11 graphics context. |
| 53 | Display* display_; |
| 54 | GC gc_; |
| 55 | Window root_window_; |
[email protected] | e1a6c46 | 2011-03-04 12:15:31 | [diff] [blame] | 56 | int width_; |
| 57 | int height_; |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 58 | |
| 59 | // XDamage information. |
| 60 | Damage damage_handle_; |
| 61 | int damage_event_base_; |
| 62 | int damage_error_base_; |
| 63 | |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 64 | // Access to the X Server's pixel buffer. |
| 65 | XServerPixelBuffer x_server_pixel_buffer_; |
| 66 | |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 67 | // A thread-safe list of invalid rectangles, and the size of the most |
| 68 | // recently captured screen. |
| 69 | CapturerHelper helper_; |
| 70 | |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 71 | // Capture state. |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 72 | static const int kNumBuffers = 2; |
| 73 | uint8* buffers_[kNumBuffers]; |
| 74 | int current_buffer_; |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 75 | int stride_; |
| 76 | bool capture_fullscreen_; |
[email protected] | 9b4b2d1 | 2011-02-03 00:33:58 | [diff] [blame] | 77 | |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 78 | // Format of pixels returned in buffer. |
| 79 | media::VideoFrame::Format pixel_format_; |
| 80 | |
[email protected] | 9b4b2d1 | 2011-02-03 00:33:58 | [diff] [blame] | 81 | // Invalid rects in the last capture. This is used to synchronize current with |
| 82 | // the previous buffer used. |
| 83 | InvalidRects last_invalid_rects_; |
| 84 | |
| 85 | // Last capture buffer used. |
| 86 | uint8* last_buffer_; |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 87 | }; |
| 88 | |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 89 | CapturerLinux::CapturerLinux() |
| 90 | : pimpl_(new CapturerLinuxPimpl()) { |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 91 | // TODO(ajwong): This should be moved into an Init() method on Capturer |
| 92 | // itself. Then we can remove the CHECK. |
| 93 | CHECK(pimpl_->Init()); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | CapturerLinux::~CapturerLinux() { |
| 97 | } |
| 98 | |
[email protected] | 804c9962 | 2010-07-01 15:02:53 | [diff] [blame] | 99 | void CapturerLinux::ScreenConfigurationChanged() { |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 100 | pimpl_->ScreenConfigurationChanged(); |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 103 | media::VideoFrame::Format CapturerLinux::pixel_format() const { |
| 104 | return pimpl_->pixel_format(); |
[email protected] | 88552a9 | 2010-08-06 22:50:00 | [diff] [blame] | 105 | } |
| 106 | |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 107 | void CapturerLinux::ClearInvalidRects() { |
| 108 | pimpl_->ClearInvalidRects(); |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 111 | void CapturerLinux::InvalidateRects(const InvalidRects& inval_rects) { |
| 112 | pimpl_->InvalidateRects(inval_rects); |
| 113 | } |
| 114 | |
| 115 | void CapturerLinux::InvalidateScreen(const gfx::Size& size) { |
| 116 | pimpl_->InvalidateScreen(size); |
| 117 | } |
| 118 | |
| 119 | void CapturerLinux::InvalidateFullScreen() { |
| 120 | pimpl_->InvalidateFullScreen(); |
| 121 | } |
| 122 | |
| 123 | void CapturerLinux::CaptureInvalidRects(CaptureCompletedCallback* callback) { |
| 124 | pimpl_->CaptureInvalidRects(callback); |
| 125 | } |
| 126 | |
| 127 | const gfx::Size& CapturerLinux::size_most_recent() const { |
| 128 | return pimpl_->size_most_recent(); |
| 129 | } |
| 130 | |
| 131 | CapturerLinuxPimpl::CapturerLinuxPimpl() |
| 132 | : display_(NULL), |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 133 | gc_(NULL), |
| 134 | root_window_(BadValue), |
[email protected] | e1a6c46 | 2011-03-04 12:15:31 | [diff] [blame] | 135 | width_(0), |
| 136 | height_(0), |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 137 | damage_handle_(BadValue), |
| 138 | damage_event_base_(-1), |
| 139 | damage_error_base_(-1), |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 140 | current_buffer_(0), |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 141 | stride_(0), |
[email protected] | 9b4b2d1 | 2011-02-03 00:33:58 | [diff] [blame] | 142 | capture_fullscreen_(true), |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 143 | pixel_format_(media::VideoFrame::RGB32), |
[email protected] | 9b4b2d1 | 2011-02-03 00:33:58 | [diff] [blame] | 144 | last_buffer_(NULL) { |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 145 | for (int i = 0; i < kNumBuffers; i++) { |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 146 | buffers_[i] = NULL; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | CapturerLinuxPimpl::~CapturerLinuxPimpl() { |
| 151 | DeinitXlib(); |
| 152 | |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 153 | for (int i = 0; i < kNumBuffers; i++) { |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 154 | delete [] buffers_[i]; |
| 155 | buffers_[i] = NULL; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | bool CapturerLinuxPimpl::Init() { |
| 160 | // TODO(ajwong): We should specify the display string we are attaching to |
| 161 | // in the constructor. |
| 162 | display_ = XOpenDisplay(NULL); |
| 163 | if (!display_) { |
| 164 | LOG(ERROR) << "Unable to open display"; |
| 165 | return false; |
| 166 | } |
| 167 | |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 168 | x_server_pixel_buffer_.Init(display_); |
| 169 | |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 170 | root_window_ = RootWindow(display_, DefaultScreen(display_)); |
| 171 | if (root_window_ == BadValue) { |
| 172 | LOG(ERROR) << "Unable to get the root window"; |
| 173 | DeinitXlib(); |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | gc_ = XCreateGC(display_, root_window_, 0, NULL); |
| 178 | if (gc_ == NULL) { |
| 179 | LOG(ERROR) << "Unable to get graphics context"; |
| 180 | DeinitXlib(); |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | // Setup XDamage to report changes in the damage window. Mark the whole |
| 185 | // window as invalid. |
| 186 | if (!XDamageQueryExtension(display_, &damage_event_base_, |
| 187 | &damage_error_base_)) { |
| 188 | LOG(ERROR) << "Server does not support XDamage."; |
| 189 | DeinitXlib(); |
| 190 | return false; |
| 191 | } |
| 192 | damage_handle_ = XDamageCreate(display_, root_window_, |
| 193 | XDamageReportDeltaRectangles); |
| 194 | if (damage_handle_ == BadValue) { |
| 195 | LOG(ERROR) << "Unable to create damage handle."; |
| 196 | DeinitXlib(); |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | // TODO(ajwong): We should be able to replace this with a XDamageAdd(). |
| 201 | capture_fullscreen_ = true; |
| 202 | |
| 203 | // Set up the dimensions of the catpure framebuffer. |
| 204 | XWindowAttributes root_attr; |
| 205 | XGetWindowAttributes(display_, root_window_, &root_attr); |
[email protected] | e1a6c46 | 2011-03-04 12:15:31 | [diff] [blame] | 206 | width_ = root_attr.width; |
| 207 | height_ = root_attr.height; |
| 208 | stride_ = width_ * kBytesPerPixel; |
| 209 | VLOG(1) << "Initialized with Geometry: " << width_ << "x" << height_; |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 210 | |
| 211 | // Allocate the screen buffers. |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 212 | for (int i = 0; i < kNumBuffers; i++) { |
[email protected] | e1a6c46 | 2011-03-04 12:15:31 | [diff] [blame] | 213 | buffers_[i] = new uint8[width_ * height_ * kBytesPerPixel]; |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | return true; |
| 217 | } |
| 218 | |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 219 | void CapturerLinuxPimpl::ScreenConfigurationChanged() { |
| 220 | // TODO(ajwong): Support resolution changes. |
| 221 | NOTIMPLEMENTED(); |
| 222 | } |
| 223 | |
| 224 | media::VideoFrame::Format CapturerLinuxPimpl::pixel_format() const { |
| 225 | return pixel_format_; |
| 226 | } |
| 227 | |
| 228 | void CapturerLinuxPimpl::ClearInvalidRects() { |
| 229 | helper_.ClearInvalidRects(); |
| 230 | } |
| 231 | |
| 232 | void CapturerLinuxPimpl::InvalidateRects(const InvalidRects& inval_rects) { |
| 233 | helper_.InvalidateRects(inval_rects); |
| 234 | } |
| 235 | |
| 236 | void CapturerLinuxPimpl::InvalidateScreen(const gfx::Size& size) { |
| 237 | helper_.InvalidateScreen(size); |
| 238 | } |
| 239 | |
| 240 | void CapturerLinuxPimpl::InvalidateFullScreen() { |
| 241 | helper_.InvalidateFullScreen(); |
| 242 | } |
| 243 | |
| 244 | void CapturerLinuxPimpl::CaptureInvalidRects( |
| 245 | CaptureCompletedCallback* callback) { |
| 246 | CalculateInvalidRects(); |
| 247 | |
| 248 | InvalidRects rects; |
| 249 | helper_.SwapInvalidRects(rects); |
| 250 | |
| 251 | CaptureRects(rects, callback); |
| 252 | } |
| 253 | |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 254 | void CapturerLinuxPimpl::CalculateInvalidRects() { |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 255 | if (helper_.IsCaptureFullScreen(gfx::Size(width_, height_))) |
[email protected] | 0544b7fa | 2011-02-09 23:07:15 | [diff] [blame] | 256 | capture_fullscreen_ = true; |
| 257 | |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 258 | // TODO(ajwong): The capture_fullscreen_ logic here is very ugly. Refactor. |
| 259 | |
| 260 | // Find the number of events that are outstanding "now." We don't just loop |
| 261 | // on XPending because we want to guarantee this terminates. |
| 262 | int events_to_process = XPending(display_); |
| 263 | XEvent e; |
| 264 | InvalidRects invalid_rects; |
| 265 | for (int i = 0; i < events_to_process; i++) { |
| 266 | XNextEvent(display_, &e); |
| 267 | if (e.type == damage_event_base_ + XDamageNotify) { |
| 268 | // If we're doing a full screen capture, we should just drain the events. |
| 269 | if (!capture_fullscreen_) { |
| 270 | XDamageNotifyEvent *event = reinterpret_cast<XDamageNotifyEvent*>(&e); |
| 271 | gfx::Rect damage_rect(event->area.x, event->area.y, event->area.width, |
| 272 | event->area.height); |
[email protected] | 97d82ed | 2011-03-07 08:01:48 | [diff] [blame] | 273 | |
| 274 | // TODO(hclam): Perform more checks on the rect. |
| 275 | if (damage_rect.width() <= 0 && damage_rect.height() <= 0) |
| 276 | continue; |
| 277 | |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 278 | invalid_rects.insert(damage_rect); |
[email protected] | 97d82ed | 2011-03-07 08:01:48 | [diff] [blame] | 279 | VLOG(3) << "Damage received for rect at (" |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 280 | << damage_rect.x() << "," << damage_rect.y() << ") size (" |
| 281 | << damage_rect.width() << "," << damage_rect.height() << ")"; |
| 282 | } |
| 283 | } else { |
| 284 | LOG(WARNING) << "Got unknown event type: " << e.type; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if (capture_fullscreen_) { |
[email protected] | 97d82ed | 2011-03-07 08:01:48 | [diff] [blame] | 289 | // TODO(hclam): Check the new dimension again. |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 290 | helper_.InvalidateScreen(gfx::Size(width_, height_)); |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 291 | capture_fullscreen_ = false; |
| 292 | } else { |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 293 | helper_.InvalidateRects(invalid_rects); |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | void CapturerLinuxPimpl::CaptureRects( |
| 298 | const InvalidRects& rects, |
| 299 | Capturer::CaptureCompletedCallback* callback) { |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 300 | scoped_ptr<CaptureCompletedCallback> callback_deleter(callback); |
| 301 | |
| 302 | uint8* buffer = buffers_[current_buffer_]; |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 303 | DataPlanes planes; |
| 304 | planes.data[0] = buffer; |
| 305 | planes.strides[0] = stride_; |
| 306 | |
[email protected] | 467aca5 | 2011-03-10 11:51:08 | [diff] [blame] | 307 | scoped_refptr<CaptureData> capture_data(new CaptureData( |
| 308 | planes, gfx::Size(width_, height_), media::VideoFrame::RGB32)); |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 309 | |
[email protected] | 9b4b2d1 | 2011-02-03 00:33:58 | [diff] [blame] | 310 | // Synchronize the current buffer with the last one since we do not capture |
| 311 | // the entire desktop. Note that encoder may be reading from the previous |
| 312 | // buffer at this time so thread access complaints are false positives. |
| 313 | |
| 314 | // TODO(hclam): We can reduce the amount of copying here by subtracting |
| 315 | // |rects| from |last_invalid_rects_|. |
| 316 | for (InvalidRects::const_iterator it = last_invalid_rects_.begin(); |
| 317 | last_buffer_ && it != last_invalid_rects_.end(); |
| 318 | ++it) { |
[email protected] | 4727723 | 2011-02-04 02:19:17 | [diff] [blame] | 319 | int offset = it->y() * stride_ + it->x() * kBytesPerPixel; |
[email protected] | 9b4b2d1 | 2011-02-03 00:33:58 | [diff] [blame] | 320 | for (int i = 0; i < it->height(); ++i) { |
| 321 | memcpy(buffer + offset, last_buffer_ + offset, |
| 322 | it->width() * kBytesPerPixel); |
[email protected] | e1a6c46 | 2011-03-04 12:15:31 | [diff] [blame] | 323 | offset += width_ * kBytesPerPixel; |
[email protected] | 9b4b2d1 | 2011-02-03 00:33:58 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 327 | for (InvalidRects::const_iterator it = rects.begin(); |
| 328 | it != rects.end(); |
| 329 | ++it) { |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 330 | uint8* image = x_server_pixel_buffer_.CaptureRect(*it); |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 331 | // Check if we can fastpath the blit. |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 332 | int depth = x_server_pixel_buffer_.GetDepth(); |
| 333 | int bpp = x_server_pixel_buffer_.GetBitsPerPixel(); |
| 334 | bool is_rgb = x_server_pixel_buffer_.IsRgb(); |
| 335 | if ((depth == 24 || depth == 32) && bpp == 32 && is_rgb) { |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 336 | VLOG(3) << "Fast blitting"; |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 337 | FastBlit(image, *it, capture_data); |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 338 | } else { |
| 339 | VLOG(3) << "Slow blitting"; |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 340 | SlowBlit(image, *it, capture_data); |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 341 | } |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | // TODO(ajwong): We should only repair the rects that were copied! |
| 345 | XDamageSubtract(display_, damage_handle_, None, None); |
| 346 | |
[email protected] | d3a4b09d | 2011-02-22 18:16:53 | [diff] [blame] | 347 | capture_data->mutable_dirty_rects() = rects; |
| 348 | last_invalid_rects_ = rects; |
[email protected] | 9b4b2d1 | 2011-02-03 00:33:58 | [diff] [blame] | 349 | last_buffer_ = buffer; |
| 350 | |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 351 | current_buffer_ = (current_buffer_ + 1) % kNumBuffers; |
| 352 | helper_.set_size_most_recent(capture_data->size()); |
| 353 | |
| 354 | callback->Run(capture_data); |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | void CapturerLinuxPimpl::DeinitXlib() { |
| 358 | if (gc_) { |
| 359 | XFreeGC(display_, gc_); |
| 360 | gc_ = NULL; |
| 361 | } |
| 362 | |
| 363 | if (display_) { |
| 364 | XCloseDisplay(display_); |
| 365 | display_ = NULL; |
| 366 | } |
| 367 | } |
| 368 | |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 369 | void CapturerLinuxPimpl::FastBlit(uint8* image, const gfx::Rect& rect, |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 370 | CaptureData* capture_data) { |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 371 | uint8* src_pos = image; |
| 372 | int src_stride = x_server_pixel_buffer_.GetStride(); |
| 373 | int dst_x = rect.x(), dst_y = rect.y(); |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 374 | |
| 375 | DataPlanes planes = capture_data->data_planes(); |
| 376 | uint8* dst_buffer = planes.data[0]; |
| 377 | |
| 378 | const int dst_stride = planes.strides[0]; |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 379 | |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 380 | uint8* dst_pos = dst_buffer + dst_stride * dst_y; |
| 381 | dst_pos += dst_x * kBytesPerPixel; |
[email protected] | d3a4b09d | 2011-02-22 18:16:53 | [diff] [blame] | 382 | |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 383 | int height = rect.height(), row_bytes = rect.width() * kBytesPerPixel; |
| 384 | for (int y = 0; y < height; ++y) { |
| 385 | memcpy(dst_pos, src_pos, row_bytes); |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 386 | src_pos += src_stride; |
[email protected] | d3a4b09d | 2011-02-22 18:16:53 | [diff] [blame] | 387 | dst_pos += dst_stride; |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 391 | void CapturerLinuxPimpl::SlowBlit(uint8* image, const gfx::Rect& rect, |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 392 | CaptureData* capture_data) { |
| 393 | DataPlanes planes = capture_data->data_planes(); |
| 394 | uint8* dst_buffer = planes.data[0]; |
| 395 | const int dst_stride = planes.strides[0]; |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 396 | int src_stride = x_server_pixel_buffer_.GetStride(); |
| 397 | int dst_x = rect.x(), dst_y = rect.y(); |
| 398 | int width = rect.width(), height = rect.height(); |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 399 | |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 400 | unsigned int red_mask = x_server_pixel_buffer_.GetRedMask(); |
| 401 | unsigned int blue_mask = x_server_pixel_buffer_.GetBlueMask(); |
| 402 | unsigned int green_mask = x_server_pixel_buffer_.GetGreenMask(); |
| 403 | unsigned int red_shift = x_server_pixel_buffer_.GetRedShift(); |
| 404 | unsigned int blue_shift = x_server_pixel_buffer_.GetBlueShift(); |
| 405 | unsigned int green_shift = x_server_pixel_buffer_.GetGreenShift(); |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 406 | |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 407 | unsigned int max_red = red_mask >> red_shift; |
| 408 | unsigned int max_blue = blue_mask >> blue_shift; |
| 409 | unsigned int max_green = green_mask >> green_shift; |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 410 | |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 411 | unsigned int bits_per_pixel = x_server_pixel_buffer_.GetBitsPerPixel(); |
| 412 | |
| 413 | uint8* dst_pos = dst_buffer + dst_stride * dst_y; |
| 414 | uint8* src_pos = image; |
| 415 | dst_pos += dst_x * kBytesPerPixel; |
| 416 | // TODO(hclam): Optimize, perhaps using MMX code or by converting to |
[email protected] | 3a90dd3a | 2011-02-22 15:06:59 | [diff] [blame] | 417 | // YUV directly |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 418 | for (int y = 0; y < height; y++) { |
[email protected] | 3a90dd3a | 2011-02-22 15:06:59 | [diff] [blame] | 419 | uint32_t* dst_pos_32 = reinterpret_cast<uint32_t*>(dst_pos); |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 420 | uint32_t* src_pos_32 = reinterpret_cast<uint32_t*>(src_pos); |
| 421 | uint16_t* src_pos_16 = reinterpret_cast<uint16_t*>(src_pos); |
| 422 | for (int x = 0; x < width; x++) { |
| 423 | // Dereference through an appropriately-aligned pointer. |
| 424 | uint32_t pixel; |
| 425 | if (bits_per_pixel == 32) |
| 426 | pixel = src_pos_32[x]; |
| 427 | else if (bits_per_pixel == 16) |
| 428 | pixel = src_pos_16[x]; |
| 429 | else |
| 430 | pixel = src_pos[x]; |
| 431 | uint32_t r = (((pixel & red_mask) >> red_shift) * 255) / max_red; |
| 432 | uint32_t b = (((pixel & blue_mask) >> blue_shift) * 255) / max_blue; |
| 433 | uint32_t g = (((pixel & green_mask) >> green_shift) * 255) / max_green; |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 434 | // Write as 32-bit RGB. |
[email protected] | 3a90dd3a | 2011-02-22 15:06:59 | [diff] [blame] | 435 | dst_pos_32[x] = r << 16 | g << 8 | b; |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 436 | } |
[email protected] | 089d186 | 2011-03-21 13:12:34 | [diff] [blame] | 437 | dst_pos += dst_stride; |
| 438 | src_pos += src_stride; |
[email protected] | 66980d8 | 2010-10-21 20:05:01 | [diff] [blame] | 439 | } |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 440 | } |
| 441 | |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 442 | const gfx::Size& CapturerLinuxPimpl::size_most_recent() const { |
| 443 | return helper_.size_most_recent(); |
| 444 | } |
| 445 | |
[email protected] | f901a07 | 2010-11-23 22:18:12 | [diff] [blame] | 446 | // static |
[email protected] | bab76b9b | 2011-03-29 14:40:20 | [diff] [blame^] | 447 | Capturer* Capturer::Create() { |
| 448 | return new CapturerLinux(); |
[email protected] | f901a07 | 2010-11-23 22:18:12 | [diff] [blame] | 449 | } |
| 450 | |
[email protected] | cb3b1f931 | 2010-06-07 19:58:23 | [diff] [blame] | 451 | } // namespace remoting |