| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 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/win/screen_capturer_win_gdi.h" | 11 #include "webrtc/modules/desktop_capture/win/screen_capturer_win_gdi.h" |
| 12 | 12 |
| 13 #include <assert.h> | 13 #include <assert.h> |
| 14 | 14 |
| 15 #include <utility> | 15 #include <utility> |
| 16 | 16 |
| 17 #include "webrtc/base/checks.h" |
| 17 #include "webrtc/modules/desktop_capture/desktop_capture_options.h" | 18 #include "webrtc/modules/desktop_capture/desktop_capture_options.h" |
| 18 #include "webrtc/modules/desktop_capture/desktop_frame.h" | 19 #include "webrtc/modules/desktop_capture/desktop_frame.h" |
| 19 #include "webrtc/modules/desktop_capture/desktop_frame_win.h" | 20 #include "webrtc/modules/desktop_capture/desktop_frame_win.h" |
| 20 #include "webrtc/modules/desktop_capture/desktop_region.h" | 21 #include "webrtc/modules/desktop_capture/desktop_region.h" |
| 21 #include "webrtc/modules/desktop_capture/differ.h" | 22 #include "webrtc/modules/desktop_capture/differ.h" |
| 22 #include "webrtc/modules/desktop_capture/mouse_cursor.h" | 23 #include "webrtc/modules/desktop_capture/mouse_cursor.h" |
| 23 #include "webrtc/modules/desktop_capture/win/cursor.h" | 24 #include "webrtc/modules/desktop_capture/win/cursor.h" |
| 24 #include "webrtc/modules/desktop_capture/win/desktop.h" | 25 #include "webrtc/modules/desktop_capture/win/desktop.h" |
| 25 #include "webrtc/modules/desktop_capture/win/screen_capture_utils.h" | 26 #include "webrtc/modules/desktop_capture/win/screen_capture_utils.h" |
| 26 #include "webrtc/system_wrappers/include/logging.h" | 27 #include "webrtc/system_wrappers/include/logging.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 | 91 |
| 91 void ScreenCapturerWinGdi::SetSharedMemoryFactory( | 92 void ScreenCapturerWinGdi::SetSharedMemoryFactory( |
| 92 rtc::scoped_ptr<SharedMemoryFactory> shared_memory_factory) { | 93 rtc::scoped_ptr<SharedMemoryFactory> shared_memory_factory) { |
| 93 shared_memory_factory_ = | 94 shared_memory_factory_ = |
| 94 rtc::ScopedToUnique(std::move(shared_memory_factory)); | 95 rtc::ScopedToUnique(std::move(shared_memory_factory)); |
| 95 } | 96 } |
| 96 | 97 |
| 97 void ScreenCapturerWinGdi::Capture(const DesktopRegion& region) { | 98 void ScreenCapturerWinGdi::Capture(const DesktopRegion& region) { |
| 98 TickTime capture_start_time = TickTime::Now(); | 99 TickTime capture_start_time = TickTime::Now(); |
| 99 | 100 |
| 100 queue_.MoveToNextFrame(); | 101 queue_.MoveToNext(); |
| 102 RTC_DCHECK(!queue_.current() || !queue_.current()->IsShared()); |
| 101 | 103 |
| 102 // Request that the system not power-down the system, or the display hardware. | 104 // Request that the system not power-down the system, or the display hardware. |
| 103 if (!SetThreadExecutionState(ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED)) { | 105 if (!SetThreadExecutionState(ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED)) { |
| 104 if (!set_thread_execution_state_failed_) { | 106 if (!set_thread_execution_state_failed_) { |
| 105 set_thread_execution_state_failed_ = true; | 107 set_thread_execution_state_failed_ = true; |
| 106 LOG_F(LS_WARNING) << "Failed to make system & display power assertion: " | 108 LOG_F(LS_WARNING) << "Failed to make system & display power assertion: " |
| 107 << GetLastError(); | 109 << GetLastError(); |
| 108 } | 110 } |
| 109 } | 111 } |
| 110 | 112 |
| 111 // Make sure the GDI capture resources are up-to-date. | 113 // Make sure the GDI capture resources are up-to-date. |
| 112 PrepareCaptureResources(); | 114 PrepareCaptureResources(); |
| 113 | 115 |
| 114 if (!CaptureImage()) { | 116 if (!CaptureImage()) { |
| 115 callback_->OnCaptureCompleted(NULL); | 117 callback_->OnCaptureCompleted(NULL); |
| 116 return; | 118 return; |
| 117 } | 119 } |
| 118 | 120 |
| 119 const DesktopFrame* current_frame = queue_.current_frame(); | 121 const DesktopFrame* current_frame = queue_.current(); |
| 120 const DesktopFrame* last_frame = queue_.previous_frame(); | 122 const DesktopFrame* last_frame = queue_.previous(); |
| 121 if (last_frame && last_frame->size().equals(current_frame->size())) { | 123 if (last_frame && last_frame->size().equals(current_frame->size())) { |
| 122 // Make sure the differencer is set up correctly for these previous and | 124 // Make sure the differencer is set up correctly for these previous and |
| 123 // current screens. | 125 // current screens. |
| 124 if (!differ_.get() || | 126 if (!differ_.get() || |
| 125 (differ_->width() != current_frame->size().width()) || | 127 (differ_->width() != current_frame->size().width()) || |
| 126 (differ_->height() != current_frame->size().height()) || | 128 (differ_->height() != current_frame->size().height()) || |
| 127 (differ_->bytes_per_row() != current_frame->stride())) { | 129 (differ_->bytes_per_row() != current_frame->stride())) { |
| 128 differ_.reset(new Differ(current_frame->size().width(), | 130 differ_.reset(new Differ(current_frame->size().width(), |
| 129 current_frame->size().height(), | 131 current_frame->size().height(), |
| 130 DesktopFrame::kBytesPerPixel, | 132 DesktopFrame::kBytesPerPixel, |
| 131 current_frame->stride())); | 133 current_frame->stride())); |
| 132 } | 134 } |
| 133 | 135 |
| 134 // Calculate difference between the two last captured frames. | 136 // Calculate difference between the two last captured frames. |
| 135 DesktopRegion region; | 137 DesktopRegion region; |
| 136 differ_->CalcDirtyRegion(last_frame->data(), current_frame->data(), | 138 differ_->CalcDirtyRegion(last_frame->data(), current_frame->data(), |
| 137 ®ion); | 139 ®ion); |
| 138 helper_.InvalidateRegion(region); | 140 helper_.InvalidateRegion(region); |
| 139 } else { | 141 } else { |
| 140 // No previous frame is available, or the screen is resized. Invalidate the | 142 // No previous frame is available, or the screen is resized. Invalidate the |
| 141 // whole screen. | 143 // whole screen. |
| 142 helper_.InvalidateScreen(current_frame->size()); | 144 helper_.InvalidateScreen(current_frame->size()); |
| 143 } | 145 } |
| 144 | 146 |
| 145 helper_.set_size_most_recent(current_frame->size()); | 147 helper_.set_size_most_recent(current_frame->size()); |
| 146 | 148 |
| 147 // Emit the current frame. | 149 // Emit the current frame. |
| 148 DesktopFrame* frame = queue_.current_frame()->Share(); | 150 DesktopFrame* frame = queue_.current()->Share(); |
| 149 frame->set_dpi(DesktopVector( | 151 frame->set_dpi(DesktopVector( |
| 150 GetDeviceCaps(desktop_dc_, LOGPIXELSX), | 152 GetDeviceCaps(desktop_dc_, LOGPIXELSX), |
| 151 GetDeviceCaps(desktop_dc_, LOGPIXELSY))); | 153 GetDeviceCaps(desktop_dc_, LOGPIXELSY))); |
| 152 frame->mutable_updated_region()->Clear(); | 154 frame->mutable_updated_region()->Clear(); |
| 153 helper_.TakeInvalidRegion(frame->mutable_updated_region()); | 155 helper_.TakeInvalidRegion(frame->mutable_updated_region()); |
| 154 frame->set_capture_time_ms( | 156 frame->set_capture_time_ms( |
| 155 (TickTime::Now() - capture_start_time).Milliseconds()); | 157 (TickTime::Now() - capture_start_time).Milliseconds()); |
| 156 callback_->OnCaptureCompleted(frame); | 158 callback_->OnCaptureCompleted(frame); |
| 157 } | 159 } |
| 158 | 160 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 bool ScreenCapturerWinGdi::CaptureImage() { | 253 bool ScreenCapturerWinGdi::CaptureImage() { |
| 252 DesktopRect screen_rect = | 254 DesktopRect screen_rect = |
| 253 GetScreenRect(current_screen_id_, current_device_key_); | 255 GetScreenRect(current_screen_id_, current_device_key_); |
| 254 if (screen_rect.is_empty()) | 256 if (screen_rect.is_empty()) |
| 255 return false; | 257 return false; |
| 256 | 258 |
| 257 DesktopSize size = screen_rect.size(); | 259 DesktopSize size = screen_rect.size(); |
| 258 // If the current buffer is from an older generation then allocate a new one. | 260 // If the current buffer is from an older generation then allocate a new one. |
| 259 // Note that we can't reallocate other buffers at this point, since the caller | 261 // Note that we can't reallocate other buffers at this point, since the caller |
| 260 // may still be reading from them. | 262 // may still be reading from them. |
| 261 if (!queue_.current_frame() || | 263 if (!queue_.current() || |
| 262 !queue_.current_frame()->size().equals(screen_rect.size())) { | 264 !queue_.current()->size().equals(screen_rect.size())) { |
| 263 assert(desktop_dc_ != NULL); | 265 assert(desktop_dc_ != NULL); |
| 264 assert(memory_dc_ != NULL); | 266 assert(memory_dc_ != NULL); |
| 265 | 267 |
| 266 std::unique_ptr<DesktopFrame> buffer(DesktopFrameWin::Create( | 268 std::unique_ptr<DesktopFrame> buffer(DesktopFrameWin::Create( |
| 267 size, shared_memory_factory_.get(), desktop_dc_)); | 269 size, shared_memory_factory_.get(), desktop_dc_)); |
| 268 if (!buffer.get()) | 270 if (!buffer) |
| 269 return false; | 271 return false; |
| 270 queue_.ReplaceCurrentFrame(buffer.release()); | 272 queue_.ReplaceCurrent(SharedDesktopFrame::Wrap(std::move(buffer))); |
| 271 } | 273 } |
| 272 | 274 |
| 273 // Select the target bitmap into the memory dc and copy the rect from desktop | 275 // Select the target bitmap into the memory dc and copy the rect from desktop |
| 274 // to memory. | 276 // to memory. |
| 275 DesktopFrameWin* current = static_cast<DesktopFrameWin*>( | 277 DesktopFrameWin* current = static_cast<DesktopFrameWin*>( |
| 276 queue_.current_frame()->GetUnderlyingFrame()); | 278 queue_.current()->GetUnderlyingFrame()); |
| 277 HGDIOBJ previous_object = SelectObject(memory_dc_, current->bitmap()); | 279 HGDIOBJ previous_object = SelectObject(memory_dc_, current->bitmap()); |
| 278 if (previous_object != NULL) { | 280 if (previous_object != NULL) { |
| 279 BitBlt(memory_dc_, | 281 BitBlt(memory_dc_, |
| 280 0, 0, screen_rect.width(), screen_rect.height(), | 282 0, 0, screen_rect.width(), screen_rect.height(), |
| 281 desktop_dc_, | 283 desktop_dc_, |
| 282 screen_rect.left(), screen_rect.top(), | 284 screen_rect.left(), screen_rect.top(), |
| 283 SRCCOPY | CAPTUREBLT); | 285 SRCCOPY | CAPTUREBLT); |
| 284 | 286 |
| 285 // Select back the previously selected object to that the device contect | 287 // Select back the previously selected object to that the device contect |
| 286 // could be destroyed independently of the bitmap if needed. | 288 // could be destroyed independently of the bitmap if needed. |
| 287 SelectObject(memory_dc_, previous_object); | 289 SelectObject(memory_dc_, previous_object); |
| 288 } | 290 } |
| 289 return true; | 291 return true; |
| 290 } | 292 } |
| 291 | 293 |
| 292 } // namespace webrtc | 294 } // namespace webrtc |
| OLD | NEW |