From 809dcb4b3e11f054aa72d25bef19d402e0579d96 Mon Sep 17 00:00:00 2001 From: zijiehe Date: Fri, 22 Apr 2016 16:08:39 -0700 Subject: [PATCH] Modify ScreenCaptureFrameQueue into a template BUG= Committed: https://crrev.com/34cad48cfbd362ae0c9027365550bfe28e2e10ef Cr-Commit-Position: refs/heads/master@{#12458} Review URL: https://codereview.webrtc.org/1902323002 Cr-Commit-Position: refs/heads/master@{#12478} --- webrtc/modules/desktop_capture/BUILD.gn | 1 - .../desktop_capture/desktop_capture.gypi | 1 - .../screen_capture_frame_queue.cc | 44 ------------------- .../screen_capture_frame_queue.h | 36 +++++++++------ .../desktop_capture/screen_capturer_mac.mm | 7 ++- .../desktop_capture/screen_capturer_x11.cc | 6 ++- .../desktop_capture/shared_desktop_frame.cc | 3 +- .../win/screen_capturer_win_gdi.cc | 6 ++- .../win/screen_capturer_win_gdi.h | 3 +- .../win/screen_capturer_win_magnifier.cc | 2 +- .../win/screen_capturer_win_magnifier.h | 3 +- 11 files changed, 42 insertions(+), 70 deletions(-) delete mode 100644 webrtc/modules/desktop_capture/screen_capture_frame_queue.cc diff --git a/webrtc/modules/desktop_capture/BUILD.gn b/webrtc/modules/desktop_capture/BUILD.gn index 9451d8be9f..894d9308e3 100644 --- a/webrtc/modules/desktop_capture/BUILD.gn +++ b/webrtc/modules/desktop_capture/BUILD.gn @@ -60,7 +60,6 @@ source_set("desktop_capture") { "mouse_cursor_monitor.h", "mouse_cursor_monitor_mac.mm", "mouse_cursor_monitor_win.cc", - "screen_capture_frame_queue.cc", "screen_capture_frame_queue.h", "screen_capturer.h", "screen_capturer_helper.cc", diff --git a/webrtc/modules/desktop_capture/desktop_capture.gypi b/webrtc/modules/desktop_capture/desktop_capture.gypi index d11cf7680e..c4fbabf6b2 100644 --- a/webrtc/modules/desktop_capture/desktop_capture.gypi +++ b/webrtc/modules/desktop_capture/desktop_capture.gypi @@ -56,7 +56,6 @@ "mouse_cursor_monitor_mac.mm", "mouse_cursor_monitor_win.cc", "mouse_cursor_monitor_x11.cc", - "screen_capture_frame_queue.cc", "screen_capture_frame_queue.h", "screen_capturer.h", "screen_capturer_helper.cc", diff --git a/webrtc/modules/desktop_capture/screen_capture_frame_queue.cc b/webrtc/modules/desktop_capture/screen_capture_frame_queue.cc deleted file mode 100644 index 94d8a27b13..0000000000 --- a/webrtc/modules/desktop_capture/screen_capture_frame_queue.cc +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h" - -#include -#include - -#include "webrtc/modules/desktop_capture/desktop_frame.h" -#include "webrtc/modules/desktop_capture/shared_desktop_frame.h" -#include "webrtc/system_wrappers/include/logging.h" -#include "webrtc/typedefs.h" - -namespace webrtc { - -ScreenCaptureFrameQueue::ScreenCaptureFrameQueue() : current_(0) {} - -ScreenCaptureFrameQueue::~ScreenCaptureFrameQueue() {} - -void ScreenCaptureFrameQueue::MoveToNextFrame() { - current_ = (current_ + 1) % kQueueLength; - - // Verify that the frame is not shared, i.e. that consumer has released it - // before attempting to capture again. - assert(!frames_[current_].get() || !frames_[current_]->IsShared()); -} - -void ScreenCaptureFrameQueue::ReplaceCurrentFrame(DesktopFrame* frame) { - frames_[current_].reset(SharedDesktopFrame::Wrap(frame)); -} - -void ScreenCaptureFrameQueue::Reset() { - for (int i = 0; i < kQueueLength; ++i) - frames_[i].reset(); -} - -} // namespace webrtc diff --git a/webrtc/modules/desktop_capture/screen_capture_frame_queue.h b/webrtc/modules/desktop_capture/screen_capture_frame_queue.h index 21af0f320f..97f3b810e9 100644 --- a/webrtc/modules/desktop_capture/screen_capture_frame_queue.h +++ b/webrtc/modules/desktop_capture/screen_capture_frame_queue.h @@ -13,12 +13,12 @@ #include -#include "webrtc/modules/desktop_capture/shared_desktop_frame.h" -#include "webrtc/typedefs.h" +#include "webrtc/base/constructormagic.h" +// TODO(zijiehe): These headers are not used in this file, but to avoid build +// break in remoting/host. We should add headers in each individual files. +#include "webrtc/modules/desktop_capture/desktop_frame.h" // Remove +#include "webrtc/modules/desktop_capture/shared_desktop_frame.h" // Remove -namespace webrtc { -class DesktopFrame; -} // namespace webrtc namespace webrtc { @@ -35,28 +35,38 @@ namespace webrtc { // Frame consumer is expected to never hold more than kQueueLength frames // created by this function and it should release the earliest one before trying // to capture a new frame (i.e. before MoveToNextFrame() is called). +template class ScreenCaptureFrameQueue { public: - ScreenCaptureFrameQueue(); - ~ScreenCaptureFrameQueue(); + ScreenCaptureFrameQueue() : current_(0) {} + ~ScreenCaptureFrameQueue() = default; // Moves to the next frame in the queue, moving the 'current' frame to become // the 'previous' one. - void MoveToNextFrame(); + void MoveToNextFrame() { + current_ = (current_ + 1) % kQueueLength; + } // Replaces the current frame with a new one allocated by the caller. The // existing frame (if any) is destroyed. Takes ownership of |frame|. - void ReplaceCurrentFrame(DesktopFrame* frame); + void ReplaceCurrentFrame(FrameType* frame) { + frames_[current_].reset(frame); + } // Marks all frames obsolete and resets the previous frame pointer. No // frames are freed though as the caller can still access them. - void Reset(); + void Reset() { + for (int i = 0; i < kQueueLength; i++) { + frames_[i].reset(); + } + current_ = 0; + } - SharedDesktopFrame* current_frame() const { + FrameType* current_frame() const { return frames_[current_].get(); } - SharedDesktopFrame* previous_frame() const { + FrameType* previous_frame() const { return frames_[(current_ + kQueueLength - 1) % kQueueLength].get(); } @@ -65,7 +75,7 @@ class ScreenCaptureFrameQueue { int current_; static const int kQueueLength = 2; - std::unique_ptr frames_[kQueueLength]; + std::unique_ptr frames_[kQueueLength]; RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCaptureFrameQueue); }; diff --git a/webrtc/modules/desktop_capture/screen_capturer_mac.mm b/webrtc/modules/desktop_capture/screen_capturer_mac.mm index c41dc4d7a3..76de59ef10 100644 --- a/webrtc/modules/desktop_capture/screen_capturer_mac.mm +++ b/webrtc/modules/desktop_capture/screen_capturer_mac.mm @@ -22,6 +22,7 @@ #include #include +#include "webrtc/base/checks.h" #include "webrtc/base/macutils.h" #include "webrtc/modules/desktop_capture/desktop_capture_options.h" #include "webrtc/modules/desktop_capture/desktop_frame.h" @@ -32,6 +33,7 @@ #include "webrtc/modules/desktop_capture/mac/scoped_pixel_buffer_object.h" #include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h" #include "webrtc/modules/desktop_capture/screen_capturer_helper.h" +#include "webrtc/modules/desktop_capture/shared_desktop_frame.h" #include "webrtc/system_wrappers/include/logging.h" #include "webrtc/system_wrappers/include/tick_util.h" @@ -234,7 +236,7 @@ class ScreenCapturerMac : public ScreenCapturer { ScopedPixelBufferObject pixel_buffer_object_; // Queue of the frames buffers. - ScreenCaptureFrameQueue queue_; + ScreenCaptureFrameQueue queue_; // Current display configuration. MacDesktopConfiguration desktop_config_; @@ -384,6 +386,7 @@ void ScreenCapturerMac::Capture(const DesktopRegion& region_to_capture) { TickTime capture_start_time = TickTime::Now(); queue_.MoveToNextFrame(); + RTC_DCHECK(!queue_.current_frame() || !queue_.current_frame()->IsShared()); desktop_config_monitor_->Lock(); MacDesktopConfiguration new_config = @@ -405,7 +408,7 @@ void ScreenCapturerMac::Capture(const DesktopRegion& region_to_capture) { // Note that we can't reallocate other buffers at this point, since the caller // may still be reading from them. if (!queue_.current_frame()) - queue_.ReplaceCurrentFrame(CreateFrame()); + queue_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(CreateFrame())); DesktopFrame* current_frame = queue_.current_frame(); diff --git a/webrtc/modules/desktop_capture/screen_capturer_x11.cc b/webrtc/modules/desktop_capture/screen_capturer_x11.cc index 65e682b6f8..4848235e3f 100644 --- a/webrtc/modules/desktop_capture/screen_capturer_x11.cc +++ b/webrtc/modules/desktop_capture/screen_capturer_x11.cc @@ -26,6 +26,7 @@ #include "webrtc/modules/desktop_capture/differ.h" #include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h" #include "webrtc/modules/desktop_capture/screen_capturer_helper.h" +#include "webrtc/modules/desktop_capture/shared_desktop_frame.h" #include "webrtc/modules/desktop_capture/x11/x_server_pixel_buffer.h" #include "webrtc/system_wrappers/include/logging.h" #include "webrtc/system_wrappers/include/tick_util.h" @@ -106,7 +107,7 @@ class ScreenCapturerLinux : public ScreenCapturer, ScreenCapturerHelper helper_; // Queue of the frames buffers. - ScreenCaptureFrameQueue queue_; + ScreenCaptureFrameQueue queue_; // Invalid region from the previous capture. This is used to synchronize the // current with the last buffer used. @@ -237,6 +238,7 @@ void ScreenCapturerLinux::Capture(const DesktopRegion& region) { TickTime capture_start_time = TickTime::Now(); queue_.MoveToNextFrame(); + RTC_DCHECK(!queue_.current_frame() || !queue_.current_frame()->IsShared()); // Process XEvents for XDamage and cursor shape tracking. options_.x_display()->ProcessPendingXEvents(); @@ -256,7 +258,7 @@ void ScreenCapturerLinux::Capture(const DesktopRegion& region) { if (!queue_.current_frame()) { std::unique_ptr frame( new BasicDesktopFrame(x_server_pixel_buffer_.window_size())); - queue_.ReplaceCurrentFrame(frame.release()); + queue_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(frame.release())); } // Refresh the Differ helper used by CaptureFrame(), if needed. diff --git a/webrtc/modules/desktop_capture/shared_desktop_frame.cc b/webrtc/modules/desktop_capture/shared_desktop_frame.cc index 309bac55ad..13d66c53d7 100644 --- a/webrtc/modules/desktop_capture/shared_desktop_frame.cc +++ b/webrtc/modules/desktop_capture/shared_desktop_frame.cc @@ -48,8 +48,7 @@ class SharedDesktopFrame::Core { SharedDesktopFrame::~SharedDesktopFrame() {} // static -SharedDesktopFrame* SharedDesktopFrame::Wrap( - DesktopFrame* desktop_frame) { +SharedDesktopFrame* SharedDesktopFrame::Wrap(DesktopFrame* desktop_frame) { rtc::scoped_refptr core(new Core(desktop_frame)); return new SharedDesktopFrame(core); } diff --git a/webrtc/modules/desktop_capture/win/screen_capturer_win_gdi.cc b/webrtc/modules/desktop_capture/win/screen_capturer_win_gdi.cc index 31c79cdda3..022e1ce9e6 100644 --- a/webrtc/modules/desktop_capture/win/screen_capturer_win_gdi.cc +++ b/webrtc/modules/desktop_capture/win/screen_capturer_win_gdi.cc @@ -14,6 +14,7 @@ #include +#include "webrtc/base/checks.h" #include "webrtc/modules/desktop_capture/desktop_capture_options.h" #include "webrtc/modules/desktop_capture/desktop_frame.h" #include "webrtc/modules/desktop_capture/desktop_frame_win.h" @@ -82,6 +83,7 @@ void ScreenCapturerWinGdi::Capture(const DesktopRegion& region) { TickTime capture_start_time = TickTime::Now(); queue_.MoveToNextFrame(); + RTC_DCHECK(!queue_.current_frame() || !queue_.current_frame()->IsShared()); // Request that the system not power-down the system, or the display hardware. if (!SetThreadExecutionState(ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED)) { @@ -247,9 +249,9 @@ bool ScreenCapturerWinGdi::CaptureImage() { std::unique_ptr buffer(DesktopFrameWin::Create( size, shared_memory_factory_.get(), desktop_dc_)); - if (!buffer.get()) + if (!buffer) return false; - queue_.ReplaceCurrentFrame(buffer.release()); + queue_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(buffer.release())); } // Select the target bitmap into the memory dc and copy the rect from desktop diff --git a/webrtc/modules/desktop_capture/win/screen_capturer_win_gdi.h b/webrtc/modules/desktop_capture/win/screen_capturer_win_gdi.h index 17cb0aa194..261a018d5f 100644 --- a/webrtc/modules/desktop_capture/win/screen_capturer_win_gdi.h +++ b/webrtc/modules/desktop_capture/win/screen_capturer_win_gdi.h @@ -20,6 +20,7 @@ #include "webrtc/base/scoped_ptr.h" #include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h" #include "webrtc/modules/desktop_capture/screen_capturer_helper.h" +#include "webrtc/modules/desktop_capture/shared_desktop_frame.h" #include "webrtc/modules/desktop_capture/win/scoped_thread_desktop.h" namespace webrtc { @@ -71,7 +72,7 @@ class ScreenCapturerWinGdi : public ScreenCapturer { HDC memory_dc_; // Queue of the frames buffers. - ScreenCaptureFrameQueue queue_; + ScreenCaptureFrameQueue queue_; // Rectangle describing the bounds of the desktop device context, relative to // the primary display's top-left. diff --git a/webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.cc b/webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.cc index 8af9779ce8..4bcd4d1918 100644 --- a/webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.cc +++ b/webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.cc @@ -433,7 +433,7 @@ void ScreenCapturerWinMagnifier::CreateCurrentFrameIfNecessary( ? SharedMemoryDesktopFrame::Create(size, shared_memory_factory_.get()) : std::unique_ptr(new BasicDesktopFrame(size)); - queue_.ReplaceCurrentFrame(frame.release()); + queue_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(frame.release())); } } diff --git a/webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.h b/webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.h index d5e3946d62..ad3ddb18fd 100644 --- a/webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.h +++ b/webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.h @@ -22,6 +22,7 @@ #include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h" #include "webrtc/modules/desktop_capture/screen_capturer.h" #include "webrtc/modules/desktop_capture/screen_capturer_helper.h" +#include "webrtc/modules/desktop_capture/shared_desktop_frame.h" #include "webrtc/modules/desktop_capture/win/scoped_thread_desktop.h" #include "webrtc/system_wrappers/include/atomic32.h" @@ -118,7 +119,7 @@ class ScreenCapturerWinMagnifier : public ScreenCapturer { ScreenCapturerHelper helper_; // Queue of the frames buffers. - ScreenCaptureFrameQueue queue_; + ScreenCaptureFrameQueue queue_; // Class to calculate the difference between two screen bitmaps. std::unique_ptr differ_;