2013-10-16 02:48:41 +00:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/desktop_capture/desktop_and_cursor_composer.h"
|
2013-10-16 02:48:41 +00:00
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2017-07-21 14:13:46 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
Use absl::make_unique and absl::WrapUnique directly
Instead of going through our wrappers in ptr_util.h.
This CL was generated by the following script:
git grep -l ptr_util | xargs perl -pi -e 's,#include "rtc_base/ptr_util.h",#include "absl/memory/memory.h",'
git grep -l MakeUnique | xargs perl -pi -e 's,\b(rtc::)?MakeUnique\b,absl::make_unique,g'
git grep -l WrapUnique | xargs perl -pi -e 's,\b(rtc::)?WrapUnique\b,absl::WrapUnique,g'
git checkout -- rtc_base/ptr_util{.h,_unittest.cc}
git cl format
Followed by manually adding dependencies on
//third_party/abseil-cpp/absl/memory until `gn check` stopped
complaining.
Bug: webrtc:9473
Change-Id: I89ccd363f070479b8c431eb2c3d404a46eaacc1c
Reviewed-on: https://webrtc-review.googlesource.com/86600
Commit-Queue: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23850}
2018-07-05 11:40:33 +02:00
|
|
|
#include "absl/memory/memory.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/desktop_capture/desktop_capturer.h"
|
|
|
|
|
#include "modules/desktop_capture/desktop_frame.h"
|
|
|
|
|
#include "modules/desktop_capture/mouse_cursor.h"
|
|
|
|
|
#include "modules/desktop_capture/mouse_cursor_monitor.h"
|
|
|
|
|
#include "rtc_base/checks.h"
|
|
|
|
|
#include "rtc_base/constructormagic.h"
|
2013-10-16 02:48:41 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
// Helper function that blends one image into another. Source image must be
|
|
|
|
|
// pre-multiplied with the alpha channel. Destination is assumed to be opaque.
|
|
|
|
|
void AlphaBlend(uint8_t* dest,
|
|
|
|
|
int dest_stride,
|
|
|
|
|
const uint8_t* src,
|
|
|
|
|
int src_stride,
|
|
|
|
|
const DesktopSize& size) {
|
|
|
|
|
for (int y = 0; y < size.height(); ++y) {
|
|
|
|
|
for (int x = 0; x < size.width(); ++x) {
|
|
|
|
|
uint32_t base_alpha = 255 - src[x * DesktopFrame::kBytesPerPixel + 3];
|
|
|
|
|
if (base_alpha == 255) {
|
|
|
|
|
continue;
|
|
|
|
|
} else if (base_alpha == 0) {
|
|
|
|
|
memcpy(dest + x * DesktopFrame::kBytesPerPixel,
|
|
|
|
|
src + x * DesktopFrame::kBytesPerPixel,
|
|
|
|
|
DesktopFrame::kBytesPerPixel);
|
|
|
|
|
} else {
|
|
|
|
|
dest[x * DesktopFrame::kBytesPerPixel] =
|
|
|
|
|
dest[x * DesktopFrame::kBytesPerPixel] * base_alpha / 255 +
|
|
|
|
|
src[x * DesktopFrame::kBytesPerPixel];
|
|
|
|
|
dest[x * DesktopFrame::kBytesPerPixel + 1] =
|
|
|
|
|
dest[x * DesktopFrame::kBytesPerPixel + 1] * base_alpha / 255 +
|
|
|
|
|
src[x * DesktopFrame::kBytesPerPixel + 1];
|
|
|
|
|
dest[x * DesktopFrame::kBytesPerPixel + 2] =
|
|
|
|
|
dest[x * DesktopFrame::kBytesPerPixel + 2] * base_alpha / 255 +
|
|
|
|
|
src[x * DesktopFrame::kBytesPerPixel + 2];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
src += src_stride;
|
|
|
|
|
dest += dest_stride;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 02:15:47 +00:00
|
|
|
// DesktopFrame wrapper that draws mouse on a frame and restores original
|
|
|
|
|
// content before releasing the underlying frame.
|
|
|
|
|
class DesktopFrameWithCursor : public DesktopFrame {
|
|
|
|
|
public:
|
|
|
|
|
// Takes ownership of |frame|.
|
2016-06-07 16:41:58 -07:00
|
|
|
DesktopFrameWithCursor(std::unique_ptr<DesktopFrame> frame,
|
2013-11-19 02:15:47 +00:00
|
|
|
const MouseCursor& cursor,
|
|
|
|
|
const DesktopVector& position);
|
2016-10-26 13:15:42 -07:00
|
|
|
~DesktopFrameWithCursor() override;
|
2013-11-19 02:15:47 +00:00
|
|
|
|
|
|
|
|
private:
|
Add DesktopFrame::MoveFrameInfoFrom() and DesktopFrame::CopyFrameInfoFrom()
The original change https://chromium-review.googlesource.com/c/575315 and
https://chromium-review.googlesource.com/c/590508 have not been well-considered.
So this change reverts part of two changes and adds a
DesktopFrame::set_top_left() function
A DesktopFrame usually contains a very large chunk of memory, which should be
reused as much as possible to reduce the memory allocations. The size of the
memory usually controls by the DesktopFrame::size(). So it's reasonable to const
DesktopFrame::size_: changing it is wrong if the underly buffer is not large
enough.
But DesktopFrame::top_left() is a different story, same as capturer_id,
capture_time_ms and other information in the DesktopFrame, it can be changed to
any value without needing to reconstruct a DesktopFrame instance. So instead of
adding it to the constructor, a DesktopFrame::set_top_left() is added to adjust
the top-left of the DesktopFrame in the entire display coordinate.
After adding DesktopFrame::set_top_left(), we have five variables in a
DesktopFrame which is not initialized in the constructor. For any kind of
wrapper DesktopFrame, say, SharedDesktopFrame and CroppedDesktopFrame, they
needs to copy these five variables after constructing themselves. This is not
convenient and easily to be broken if an implementation forgot to copy them.
So DesktopFrame::MoveFrameInfoFrom() and DesktopFrame::CopyFrameInfoFrom() are
added to the DesktopFrame to help derived classes to copy or move these
variables in one function call.
The difference between MoveFrameInfoFrom() and CopyFrameInfoFrom() is that the
former one uses DesktopRegion::Swap() to move the DesktopRegion from the source
DesktopFrame to this instance, while the later one uses copy-operator to copy
the DesktopRegion from the source DesktopFrame.
So CopyFrameInfoFrom() is usually used when sharing a source DesktopFrame with
several clients. I.e. the source DesktopFrame should be kept unchanged. For
example, BasicDesktopFrame::CopyOf() and SharedDesktopFrame::Share().
On the other side, MoveFrameInfoFrom() is usually used when wrapping a
DesktopFrame. E.g. CroppedDesktopFrame and DesktopFrameWithCursor.
Bug: webrtc:7950
Change-Id: I8b23418960fb681d2ea1f012d1b453f514da2272
Reviewed-on: https://chromium-review.googlesource.com/622453
Commit-Queue: Zijie He <zijiehe@chromium.org>
Reviewed-by: Jamie Walch <jamiewalch@chromium.org>
Cr-Commit-Position: refs/heads/master@{#19504}
2017-08-24 12:41:53 -07:00
|
|
|
const std::unique_ptr<DesktopFrame> original_frame_;
|
2013-11-19 02:15:47 +00:00
|
|
|
|
|
|
|
|
DesktopVector restore_position_;
|
2016-03-16 15:58:08 -07:00
|
|
|
std::unique_ptr<DesktopFrame> restore_frame_;
|
2013-11-19 02:15:47 +00:00
|
|
|
|
2015-09-16 05:37:44 -07:00
|
|
|
RTC_DISALLOW_COPY_AND_ASSIGN(DesktopFrameWithCursor);
|
2013-11-19 02:15:47 +00:00
|
|
|
};
|
|
|
|
|
|
2016-06-07 16:41:58 -07:00
|
|
|
DesktopFrameWithCursor::DesktopFrameWithCursor(
|
|
|
|
|
std::unique_ptr<DesktopFrame> frame,
|
|
|
|
|
const MouseCursor& cursor,
|
|
|
|
|
const DesktopVector& position)
|
Add DesktopFrame::MoveFrameInfoFrom() and DesktopFrame::CopyFrameInfoFrom()
The original change https://chromium-review.googlesource.com/c/575315 and
https://chromium-review.googlesource.com/c/590508 have not been well-considered.
So this change reverts part of two changes and adds a
DesktopFrame::set_top_left() function
A DesktopFrame usually contains a very large chunk of memory, which should be
reused as much as possible to reduce the memory allocations. The size of the
memory usually controls by the DesktopFrame::size(). So it's reasonable to const
DesktopFrame::size_: changing it is wrong if the underly buffer is not large
enough.
But DesktopFrame::top_left() is a different story, same as capturer_id,
capture_time_ms and other information in the DesktopFrame, it can be changed to
any value without needing to reconstruct a DesktopFrame instance. So instead of
adding it to the constructor, a DesktopFrame::set_top_left() is added to adjust
the top-left of the DesktopFrame in the entire display coordinate.
After adding DesktopFrame::set_top_left(), we have five variables in a
DesktopFrame which is not initialized in the constructor. For any kind of
wrapper DesktopFrame, say, SharedDesktopFrame and CroppedDesktopFrame, they
needs to copy these five variables after constructing themselves. This is not
convenient and easily to be broken if an implementation forgot to copy them.
So DesktopFrame::MoveFrameInfoFrom() and DesktopFrame::CopyFrameInfoFrom() are
added to the DesktopFrame to help derived classes to copy or move these
variables in one function call.
The difference between MoveFrameInfoFrom() and CopyFrameInfoFrom() is that the
former one uses DesktopRegion::Swap() to move the DesktopRegion from the source
DesktopFrame to this instance, while the later one uses copy-operator to copy
the DesktopRegion from the source DesktopFrame.
So CopyFrameInfoFrom() is usually used when sharing a source DesktopFrame with
several clients. I.e. the source DesktopFrame should be kept unchanged. For
example, BasicDesktopFrame::CopyOf() and SharedDesktopFrame::Share().
On the other side, MoveFrameInfoFrom() is usually used when wrapping a
DesktopFrame. E.g. CroppedDesktopFrame and DesktopFrameWithCursor.
Bug: webrtc:7950
Change-Id: I8b23418960fb681d2ea1f012d1b453f514da2272
Reviewed-on: https://chromium-review.googlesource.com/622453
Commit-Queue: Zijie He <zijiehe@chromium.org>
Reviewed-by: Jamie Walch <jamiewalch@chromium.org>
Cr-Commit-Position: refs/heads/master@{#19504}
2017-08-24 12:41:53 -07:00
|
|
|
: DesktopFrame(frame->size(),
|
2016-06-07 16:41:58 -07:00
|
|
|
frame->stride(),
|
|
|
|
|
frame->data(),
|
Add DesktopFrame::MoveFrameInfoFrom() and DesktopFrame::CopyFrameInfoFrom()
The original change https://chromium-review.googlesource.com/c/575315 and
https://chromium-review.googlesource.com/c/590508 have not been well-considered.
So this change reverts part of two changes and adds a
DesktopFrame::set_top_left() function
A DesktopFrame usually contains a very large chunk of memory, which should be
reused as much as possible to reduce the memory allocations. The size of the
memory usually controls by the DesktopFrame::size(). So it's reasonable to const
DesktopFrame::size_: changing it is wrong if the underly buffer is not large
enough.
But DesktopFrame::top_left() is a different story, same as capturer_id,
capture_time_ms and other information in the DesktopFrame, it can be changed to
any value without needing to reconstruct a DesktopFrame instance. So instead of
adding it to the constructor, a DesktopFrame::set_top_left() is added to adjust
the top-left of the DesktopFrame in the entire display coordinate.
After adding DesktopFrame::set_top_left(), we have five variables in a
DesktopFrame which is not initialized in the constructor. For any kind of
wrapper DesktopFrame, say, SharedDesktopFrame and CroppedDesktopFrame, they
needs to copy these five variables after constructing themselves. This is not
convenient and easily to be broken if an implementation forgot to copy them.
So DesktopFrame::MoveFrameInfoFrom() and DesktopFrame::CopyFrameInfoFrom() are
added to the DesktopFrame to help derived classes to copy or move these
variables in one function call.
The difference between MoveFrameInfoFrom() and CopyFrameInfoFrom() is that the
former one uses DesktopRegion::Swap() to move the DesktopRegion from the source
DesktopFrame to this instance, while the later one uses copy-operator to copy
the DesktopRegion from the source DesktopFrame.
So CopyFrameInfoFrom() is usually used when sharing a source DesktopFrame with
several clients. I.e. the source DesktopFrame should be kept unchanged. For
example, BasicDesktopFrame::CopyOf() and SharedDesktopFrame::Share().
On the other side, MoveFrameInfoFrom() is usually used when wrapping a
DesktopFrame. E.g. CroppedDesktopFrame and DesktopFrameWithCursor.
Bug: webrtc:7950
Change-Id: I8b23418960fb681d2ea1f012d1b453f514da2272
Reviewed-on: https://chromium-review.googlesource.com/622453
Commit-Queue: Zijie He <zijiehe@chromium.org>
Reviewed-by: Jamie Walch <jamiewalch@chromium.org>
Cr-Commit-Position: refs/heads/master@{#19504}
2017-08-24 12:41:53 -07:00
|
|
|
frame->shared_memory()),
|
|
|
|
|
original_frame_(std::move(frame)) {
|
|
|
|
|
MoveFrameInfoFrom(original_frame_.get());
|
2013-11-19 02:15:47 +00:00
|
|
|
|
|
|
|
|
DesktopVector image_pos = position.subtract(cursor.hotspot());
|
2013-12-18 02:18:01 +00:00
|
|
|
DesktopRect target_rect = DesktopRect::MakeSize(cursor.image()->size());
|
2013-11-19 02:15:47 +00:00
|
|
|
target_rect.Translate(image_pos);
|
|
|
|
|
DesktopVector target_origin = target_rect.top_left();
|
|
|
|
|
target_rect.IntersectWith(DesktopRect::MakeSize(size()));
|
|
|
|
|
|
|
|
|
|
if (target_rect.is_empty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Copy original screen content under cursor to |restore_frame_|.
|
|
|
|
|
restore_position_ = target_rect.top_left();
|
|
|
|
|
restore_frame_.reset(new BasicDesktopFrame(target_rect.size()));
|
|
|
|
|
restore_frame_->CopyPixelsFrom(*this, target_rect.top_left(),
|
|
|
|
|
DesktopRect::MakeSize(restore_frame_->size()));
|
|
|
|
|
|
|
|
|
|
// Blit the cursor.
|
|
|
|
|
uint8_t* target_rect_data = reinterpret_cast<uint8_t*>(data()) +
|
|
|
|
|
target_rect.top() * stride() +
|
|
|
|
|
target_rect.left() * DesktopFrame::kBytesPerPixel;
|
|
|
|
|
DesktopVector origin_shift = target_rect.top_left().subtract(target_origin);
|
|
|
|
|
AlphaBlend(target_rect_data, stride(),
|
2013-12-18 02:18:01 +00:00
|
|
|
cursor.image()->data() +
|
|
|
|
|
origin_shift.y() * cursor.image()->stride() +
|
2013-11-19 02:15:47 +00:00
|
|
|
origin_shift.x() * DesktopFrame::kBytesPerPixel,
|
2013-12-18 02:18:01 +00:00
|
|
|
cursor.image()->stride(), target_rect.size());
|
2013-11-19 02:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DesktopFrameWithCursor::~DesktopFrameWithCursor() {
|
|
|
|
|
// Restore original content of the frame.
|
2017-08-29 18:03:46 -07:00
|
|
|
if (restore_frame_) {
|
2013-11-19 02:15:47 +00:00
|
|
|
DesktopRect target_rect = DesktopRect::MakeSize(restore_frame_->size());
|
|
|
|
|
target_rect.Translate(restore_position_);
|
|
|
|
|
CopyPixelsFrom(restore_frame_->data(), restore_frame_->stride(),
|
|
|
|
|
target_rect);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-16 02:48:41 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
2017-07-21 14:13:46 -07:00
|
|
|
DesktopAndCursorComposer::DesktopAndCursorComposer(
|
|
|
|
|
std::unique_ptr<DesktopCapturer> desktop_capturer,
|
|
|
|
|
const DesktopCaptureOptions& options)
|
|
|
|
|
: DesktopAndCursorComposer(desktop_capturer.release(),
|
2018-03-29 10:36:14 -07:00
|
|
|
MouseCursorMonitor::Create(options).release()) {}
|
2017-08-29 18:03:46 -07:00
|
|
|
|
|
|
|
|
DesktopAndCursorComposer::DesktopAndCursorComposer(
|
|
|
|
|
DesktopCapturer* desktop_capturer,
|
2018-03-29 10:36:14 -07:00
|
|
|
MouseCursorMonitor* mouse_monitor)
|
|
|
|
|
: desktop_capturer_(desktop_capturer), mouse_monitor_(mouse_monitor) {
|
2017-08-29 18:03:46 -07:00
|
|
|
RTC_DCHECK(desktop_capturer_);
|
|
|
|
|
}
|
2017-07-21 14:13:46 -07:00
|
|
|
|
|
|
|
|
DesktopAndCursorComposer::~DesktopAndCursorComposer() = default;
|
2013-10-16 02:48:41 +00:00
|
|
|
|
|
|
|
|
void DesktopAndCursorComposer::Start(DesktopCapturer::Callback* callback) {
|
|
|
|
|
callback_ = callback;
|
2017-08-29 18:03:46 -07:00
|
|
|
if (mouse_monitor_)
|
2013-10-16 02:48:41 +00:00
|
|
|
mouse_monitor_->Init(this, MouseCursorMonitor::SHAPE_AND_POSITION);
|
|
|
|
|
desktop_capturer_->Start(this);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-09 15:13:26 -08:00
|
|
|
void DesktopAndCursorComposer::SetSharedMemoryFactory(
|
2016-04-27 01:19:58 -07:00
|
|
|
std::unique_ptr<SharedMemoryFactory> shared_memory_factory) {
|
2016-02-09 15:13:26 -08:00
|
|
|
desktop_capturer_->SetSharedMemoryFactory(std::move(shared_memory_factory));
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 16:47:49 -07:00
|
|
|
void DesktopAndCursorComposer::CaptureFrame() {
|
2017-08-29 18:03:46 -07:00
|
|
|
if (mouse_monitor_)
|
2013-10-16 02:48:41 +00:00
|
|
|
mouse_monitor_->Capture();
|
2016-10-13 16:47:49 -07:00
|
|
|
desktop_capturer_->CaptureFrame();
|
2013-10-16 02:48:41 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-26 15:57:43 +00:00
|
|
|
void DesktopAndCursorComposer::SetExcludedWindow(WindowId window) {
|
|
|
|
|
desktop_capturer_->SetExcludedWindow(window);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-07 16:41:58 -07:00
|
|
|
void DesktopAndCursorComposer::OnCaptureResult(
|
|
|
|
|
DesktopCapturer::Result result,
|
|
|
|
|
std::unique_ptr<DesktopFrame> frame) {
|
2017-08-29 18:03:46 -07:00
|
|
|
if (frame && cursor_) {
|
2018-03-29 10:36:14 -07:00
|
|
|
if (frame->rect().Contains(cursor_position_) &&
|
|
|
|
|
!desktop_capturer_->IsOccluded(cursor_position_)) {
|
2018-05-17 10:30:35 -07:00
|
|
|
const float scale = frame->scale_factor();
|
|
|
|
|
DesktopVector relative_position =
|
2018-03-29 10:36:14 -07:00
|
|
|
cursor_position_.subtract(frame->top_left());
|
2018-05-17 10:30:35 -07:00
|
|
|
relative_position.set(relative_position.x() * scale,
|
|
|
|
|
relative_position.y() * scale);
|
Use absl::make_unique and absl::WrapUnique directly
Instead of going through our wrappers in ptr_util.h.
This CL was generated by the following script:
git grep -l ptr_util | xargs perl -pi -e 's,#include "rtc_base/ptr_util.h",#include "absl/memory/memory.h",'
git grep -l MakeUnique | xargs perl -pi -e 's,\b(rtc::)?MakeUnique\b,absl::make_unique,g'
git grep -l WrapUnique | xargs perl -pi -e 's,\b(rtc::)?WrapUnique\b,absl::WrapUnique,g'
git checkout -- rtc_base/ptr_util{.h,_unittest.cc}
git cl format
Followed by manually adding dependencies on
//third_party/abseil-cpp/absl/memory until `gn check` stopped
complaining.
Bug: webrtc:9473
Change-Id: I89ccd363f070479b8c431eb2c3d404a46eaacc1c
Reviewed-on: https://webrtc-review.googlesource.com/86600
Commit-Queue: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23850}
2018-07-05 11:40:33 +02:00
|
|
|
frame = absl::make_unique<DesktopFrameWithCursor>(
|
2018-03-29 10:36:14 -07:00
|
|
|
std::move(frame), *cursor_, relative_position);
|
2017-08-29 18:03:46 -07:00
|
|
|
}
|
2013-10-16 02:48:41 +00:00
|
|
|
}
|
|
|
|
|
|
2016-06-07 16:41:58 -07:00
|
|
|
callback_->OnCaptureResult(result, std::move(frame));
|
2013-10-16 02:48:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DesktopAndCursorComposer::OnMouseCursor(MouseCursor* cursor) {
|
|
|
|
|
cursor_.reset(cursor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DesktopAndCursorComposer::OnMouseCursorPosition(
|
|
|
|
|
MouseCursorMonitor::CursorState state,
|
|
|
|
|
const DesktopVector& position) {
|
2018-03-29 10:36:14 -07:00
|
|
|
RTC_NOTREACHED();
|
2017-08-29 18:03:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DesktopAndCursorComposer::OnMouseCursorPosition(
|
|
|
|
|
const DesktopVector& position) {
|
2018-03-29 10:36:14 -07:00
|
|
|
cursor_position_ = position;
|
2013-10-16 02:48:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|