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}
diff --git a/webrtc/modules/desktop_capture/BUILD.gn b/webrtc/modules/desktop_capture/BUILD.gn
index 9451d8b..894d930 100644
--- a/webrtc/modules/desktop_capture/BUILD.gn
+++ b/webrtc/modules/desktop_capture/BUILD.gn
@@ -60,7 +60,6 @@
     "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 d11cf76..c4fbabf 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 94d8a27..0000000
--- 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 <assert.h>
-#include <algorithm>
-
-#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 21af0f3..97f3b81 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 <memory>
 
-#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 @@
 // 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 <typename FrameType>
 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 @@
   int current_;
 
   static const int kQueueLength = 2;
-  std::unique_ptr<SharedDesktopFrame> frames_[kQueueLength];
+  std::unique_ptr<FrameType> 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 c41dc4d..76de59e 100644
--- a/webrtc/modules/desktop_capture/screen_capturer_mac.mm
+++ b/webrtc/modules/desktop_capture/screen_capturer_mac.mm
@@ -22,6 +22,7 @@
 #include <OpenGL/CGLMacro.h>
 #include <OpenGL/OpenGL.h>
 
+#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 @@
   ScopedPixelBufferObject pixel_buffer_object_;
 
   // Queue of the frames buffers.
-  ScreenCaptureFrameQueue queue_;
+  ScreenCaptureFrameQueue<SharedDesktopFrame> queue_;
 
   // Current display configuration.
   MacDesktopConfiguration desktop_config_;
@@ -384,6 +386,7 @@
   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 @@
   // 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 65e682b..4848235 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 @@
   ScreenCapturerHelper helper_;
 
   // Queue of the frames buffers.
-  ScreenCaptureFrameQueue queue_;
+  ScreenCaptureFrameQueue<SharedDesktopFrame> queue_;
 
   // Invalid region from the previous capture. This is used to synchronize the
   // current with the last buffer used.
@@ -237,6 +238,7 @@
   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 @@
   if (!queue_.current_frame()) {
     std::unique_ptr<DesktopFrame> 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 309bac5..13d66c5 100644
--- a/webrtc/modules/desktop_capture/shared_desktop_frame.cc
+++ b/webrtc/modules/desktop_capture/shared_desktop_frame.cc
@@ -48,8 +48,7 @@
 SharedDesktopFrame::~SharedDesktopFrame() {}
 
 // static
-SharedDesktopFrame* SharedDesktopFrame::Wrap(
-    DesktopFrame* desktop_frame) {
+SharedDesktopFrame* SharedDesktopFrame::Wrap(DesktopFrame* desktop_frame) {
   rtc::scoped_refptr<Core> 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 31c79cd..022e1ce 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 <utility>
 
+#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 @@
   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 @@
 
     std::unique_ptr<DesktopFrame> 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 17cb0aa..261a018 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 @@
   HDC memory_dc_;
 
   // Queue of the frames buffers.
-  ScreenCaptureFrameQueue queue_;
+  ScreenCaptureFrameQueue<SharedDesktopFrame> 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 8af9779..4bcd4d1 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 @@
             ? SharedMemoryDesktopFrame::Create(size,
                                                shared_memory_factory_.get())
             : std::unique_ptr<DesktopFrame>(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 d5e3946..ad3ddb1 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 @@
   ScreenCapturerHelper helper_;
 
   // Queue of the frames buffers.
-  ScreenCaptureFrameQueue queue_;
+  ScreenCaptureFrameQueue<SharedDesktopFrame> queue_;
 
   // Class to calculate the difference between two screen bitmaps.
   std::unique_ptr<Differ> differ_;