Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

205 lines
6.8 KiB
C++
Raw Normal View History

/*
* 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 "modules/desktop_capture/desktop_frame.h"
#include <string.h>
#include <cmath>
Use std::make_unique instead of absl::make_unique. WebRTC is now using C++14 so there is no need to use the Abseil version of std::make_unique. This CL has been created with the following steps: git grep -l absl::make_unique | sort | uniq > /tmp/make_unique.txt git grep -l absl::WrapUnique | sort | uniq > /tmp/wrap_unique.txt git grep -l "#include <memory>" | sort | uniq > /tmp/memory.txt diff --new-line-format="" --unchanged-line-format="" \ /tmp/make_unique.txt /tmp/wrap_unique.txt | sort | \ uniq > /tmp/only_make_unique.txt diff --new-line-format="" --unchanged-line-format="" \ /tmp/only_make_unique.txt /tmp/memory.txt | \ xargs grep -l "absl/memory" > /tmp/add-memory.txt git grep -l "\babsl::make_unique\b" | \ xargs sed -i "s/\babsl::make_unique\b/std::make_unique/g" git checkout PRESUBMIT.py abseil-in-webrtc.md cat /tmp/add-memory.txt | \ xargs sed -i \ 's/#include "absl\/memory\/memory.h"/#include <memory>/g' git cl format # Manual fix order of the new inserted #include <memory> cat /tmp/only_make_unique | xargs grep -l "#include <memory>" | \ xargs sed -i '/#include "absl\/memory\/memory.h"/d' git ls-files | grep BUILD.gn | \ xargs sed -i '/\/\/third_party\/abseil-cpp\/absl\/memory/d' python tools_webrtc/gn_check_autofix.py \ -m tryserver.webrtc -b linux_rel # Repead the gn_check_autofix step for other platforms git ls-files | grep BUILD.gn | \ xargs sed -i 's/absl\/memory:memory/absl\/memory/g' git cl format Bug: webrtc:10945 Change-Id: I3fe28ea80f4dd3ba3cf28effd151d5e1f19aff89 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/153221 Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Alessio Bazzica <alessiob@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Cr-Commit-Position: refs/heads/master@{#29209}
2019-09-17 17:06:18 +02:00
#include <memory>
#include <utility>
#include "modules/desktop_capture/desktop_capture_types.h"
#include "modules/desktop_capture/desktop_geometry.h"
#include "rtc_base/checks.h"
namespace webrtc {
DesktopFrame::DesktopFrame(DesktopSize size,
int stride,
uint8_t* data,
SharedMemory* shared_memory)
: data_(data),
shared_memory_(shared_memory),
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
size_(size),
stride_(stride),
capture_time_ms_(0),
capturer_id_(DesktopCapturerId::kUnknown) {
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
RTC_DCHECK(size_.width() >= 0);
RTC_DCHECK(size_.height() >= 0);
}
DesktopFrame::~DesktopFrame() = default;
void DesktopFrame::CopyPixelsFrom(const uint8_t* src_buffer,
int src_stride,
const DesktopRect& dest_rect) {
RTC_CHECK(DesktopRect::MakeSize(size()).ContainsRect(dest_rect));
uint8_t* dest = GetFrameDataAtPos(dest_rect.top_left());
for (int y = 0; y < dest_rect.height(); ++y) {
memcpy(dest, src_buffer, DesktopFrame::kBytesPerPixel * dest_rect.width());
src_buffer += src_stride;
dest += stride();
}
}
void DesktopFrame::CopyPixelsFrom(const DesktopFrame& src_frame,
const DesktopVector& src_pos,
const DesktopRect& dest_rect) {
RTC_CHECK(DesktopRect::MakeSize(src_frame.size())
.ContainsRect(
DesktopRect::MakeOriginSize(src_pos, dest_rect.size())));
CopyPixelsFrom(src_frame.GetFrameDataAtPos(src_pos), src_frame.stride(),
dest_rect);
}
bool DesktopFrame::CopyIntersectingPixelsFrom(const DesktopFrame& src_frame,
double horizontal_scale,
double vertical_scale) {
const DesktopVector& origin = top_left();
const DesktopVector& src_frame_origin = src_frame.top_left();
DesktopVector src_frame_offset = src_frame_origin.subtract(origin);
// Determine the intersection, first adjusting its origin to account for any
// DPI scaling.
DesktopRect intersection_rect = src_frame.rect();
if (horizontal_scale != 1.0 || vertical_scale != 1.0) {
DesktopVector origin_adjustment(
static_cast<int>(
std::round((horizontal_scale - 1.0) * src_frame_offset.x())),
static_cast<int>(
std::round((vertical_scale - 1.0) * src_frame_offset.y())));
intersection_rect.Translate(origin_adjustment);
src_frame_offset = src_frame_offset.add(origin_adjustment);
}
intersection_rect.IntersectWith(rect());
if (intersection_rect.is_empty()) {
return false;
}
// Translate the intersection rect to be relative to the outer rect.
intersection_rect.Translate(-origin.x(), -origin.y());
// Determine source position for the copy (offsets of outer frame from
// source origin, if positive).
int32_t src_pos_x = std::max(0, -src_frame_offset.x());
int32_t src_pos_y = std::max(0, -src_frame_offset.y());
CopyPixelsFrom(src_frame, DesktopVector(src_pos_x, src_pos_y),
intersection_rect);
return true;
}
DesktopRect DesktopFrame::rect() const {
Reland "[desktopCapture] Unify the position info in DIP coordinates on Mac." This is a reland of 89653d5db46419d2a80898635cb27fed64898db2 Original change's description: > [desktopCapture] Unify the position info in DIP coordinates on Mac. > > On OSX, the logical(DIP) and physical coordinates are used mixingly. > For example, the captured image has its size in physical pixels(2x) and > location in logical(DIP) pixels. Same to the cursor position. This > causes trouble when we check the relative position of image and cursor > when there are multiple monitors with different DIP setting connected. > > This cl proposed a solution to use DIP pixel for any location info, > i.e. top-left of a frame and cursor position. Also propose a method to > get the current scale factor of a window across multiple monitors. And > save the current scale factor in DPI of the capture frame. > Then we can check relative position of cursor and frame correctly > in DIP pixel and compose them in physical pixel. > > Bug: webrtc:9178 > Change-Id: I3c076aeac2d6f2c1f63d000d7fff03500aa375ac > Reviewed-on: https://webrtc-review.googlesource.com/71621 > Reviewed-by: Jamie Walch <jamiewalch@chromium.org> > Reviewed-by: Zijie He <zijiehe@chromium.org> > Commit-Queue: Brave Yao <braveyao@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#23263} Bug: webrtc:9178 Change-Id: I97d9150f7b9a4ed6671733b75613ea9c315d5c1d Reviewed-on: https://webrtc-review.googlesource.com/77481 Reviewed-by: Zijie He <zijiehe@chromium.org> Commit-Queue: Brave Yao <braveyao@webrtc.org> Cr-Commit-Position: refs/heads/master@{#23289}
2018-05-17 10:30:35 -07:00
const float scale = scale_factor();
// Only scale the size.
return DesktopRect::MakeXYWH(top_left().x(), top_left().y(),
size().width() / scale, size().height() / scale);
}
float DesktopFrame::scale_factor() const {
float scale = 1.0f;
#if defined(WEBRTC_MAC)
// At least on Windows the logical and physical pixel are the same
// See http://crbug.com/948362.
Reland "[desktopCapture] Unify the position info in DIP coordinates on Mac." This is a reland of 89653d5db46419d2a80898635cb27fed64898db2 Original change's description: > [desktopCapture] Unify the position info in DIP coordinates on Mac. > > On OSX, the logical(DIP) and physical coordinates are used mixingly. > For example, the captured image has its size in physical pixels(2x) and > location in logical(DIP) pixels. Same to the cursor position. This > causes trouble when we check the relative position of image and cursor > when there are multiple monitors with different DIP setting connected. > > This cl proposed a solution to use DIP pixel for any location info, > i.e. top-left of a frame and cursor position. Also propose a method to > get the current scale factor of a window across multiple monitors. And > save the current scale factor in DPI of the capture frame. > Then we can check relative position of cursor and frame correctly > in DIP pixel and compose them in physical pixel. > > Bug: webrtc:9178 > Change-Id: I3c076aeac2d6f2c1f63d000d7fff03500aa375ac > Reviewed-on: https://webrtc-review.googlesource.com/71621 > Reviewed-by: Jamie Walch <jamiewalch@chromium.org> > Reviewed-by: Zijie He <zijiehe@chromium.org> > Commit-Queue: Brave Yao <braveyao@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#23263} Bug: webrtc:9178 Change-Id: I97d9150f7b9a4ed6671733b75613ea9c315d5c1d Reviewed-on: https://webrtc-review.googlesource.com/77481 Reviewed-by: Zijie He <zijiehe@chromium.org> Commit-Queue: Brave Yao <braveyao@webrtc.org> Cr-Commit-Position: refs/heads/master@{#23289}
2018-05-17 10:30:35 -07:00
if (!dpi().is_zero() && dpi().x() == dpi().y())
scale = dpi().x() / kStandardDPI;
#endif
Reland "[desktopCapture] Unify the position info in DIP coordinates on Mac." This is a reland of 89653d5db46419d2a80898635cb27fed64898db2 Original change's description: > [desktopCapture] Unify the position info in DIP coordinates on Mac. > > On OSX, the logical(DIP) and physical coordinates are used mixingly. > For example, the captured image has its size in physical pixels(2x) and > location in logical(DIP) pixels. Same to the cursor position. This > causes trouble when we check the relative position of image and cursor > when there are multiple monitors with different DIP setting connected. > > This cl proposed a solution to use DIP pixel for any location info, > i.e. top-left of a frame and cursor position. Also propose a method to > get the current scale factor of a window across multiple monitors. And > save the current scale factor in DPI of the capture frame. > Then we can check relative position of cursor and frame correctly > in DIP pixel and compose them in physical pixel. > > Bug: webrtc:9178 > Change-Id: I3c076aeac2d6f2c1f63d000d7fff03500aa375ac > Reviewed-on: https://webrtc-review.googlesource.com/71621 > Reviewed-by: Jamie Walch <jamiewalch@chromium.org> > Reviewed-by: Zijie He <zijiehe@chromium.org> > Commit-Queue: Brave Yao <braveyao@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#23263} Bug: webrtc:9178 Change-Id: I97d9150f7b9a4ed6671733b75613ea9c315d5c1d Reviewed-on: https://webrtc-review.googlesource.com/77481 Reviewed-by: Zijie He <zijiehe@chromium.org> Commit-Queue: Brave Yao <braveyao@webrtc.org> Cr-Commit-Position: refs/heads/master@{#23289}
2018-05-17 10:30:35 -07:00
return scale;
}
uint8_t* DesktopFrame::GetFrameDataAtPos(const DesktopVector& pos) const {
return data() + stride() * pos.y() + DesktopFrame::kBytesPerPixel * pos.x();
}
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
void DesktopFrame::CopyFrameInfoFrom(const DesktopFrame& other) {
set_dpi(other.dpi());
set_capture_time_ms(other.capture_time_ms());
set_capturer_id(other.capturer_id());
*mutable_updated_region() = other.updated_region();
set_top_left(other.top_left());
set_icc_profile(other.icc_profile());
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
}
void DesktopFrame::MoveFrameInfoFrom(DesktopFrame* other) {
set_dpi(other->dpi());
set_capture_time_ms(other->capture_time_ms());
set_capturer_id(other->capturer_id());
mutable_updated_region()->Swap(other->mutable_updated_region());
set_top_left(other->top_left());
set_icc_profile(other->icc_profile());
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
}
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
BasicDesktopFrame::BasicDesktopFrame(DesktopSize size)
: DesktopFrame(size,
kBytesPerPixel * size.width(),
new uint8_t[kBytesPerPixel * size.width() * size.height()](),
nullptr) {}
BasicDesktopFrame::~BasicDesktopFrame() {
delete[] 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
// static
DesktopFrame* BasicDesktopFrame::CopyOf(const DesktopFrame& frame) {
DesktopFrame* result = new BasicDesktopFrame(frame.size());
for (int y = 0; y < frame.size().height(); ++y) {
memcpy(result->data() + y * result->stride(),
frame.data() + y * frame.stride(),
frame.size().width() * kBytesPerPixel);
}
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
result->CopyFrameInfoFrom(frame);
return result;
}
// static
std::unique_ptr<DesktopFrame> SharedMemoryDesktopFrame::Create(
DesktopSize size,
SharedMemoryFactory* shared_memory_factory) {
RTC_DCHECK(shared_memory_factory);
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
size_t buffer_size = size.height() * size.width() * kBytesPerPixel;
std::unique_ptr<SharedMemory> shared_memory =
shared_memory_factory->CreateSharedMemory(buffer_size);
if (!shared_memory)
return nullptr;
Use std::make_unique instead of absl::make_unique. WebRTC is now using C++14 so there is no need to use the Abseil version of std::make_unique. This CL has been created with the following steps: git grep -l absl::make_unique | sort | uniq > /tmp/make_unique.txt git grep -l absl::WrapUnique | sort | uniq > /tmp/wrap_unique.txt git grep -l "#include <memory>" | sort | uniq > /tmp/memory.txt diff --new-line-format="" --unchanged-line-format="" \ /tmp/make_unique.txt /tmp/wrap_unique.txt | sort | \ uniq > /tmp/only_make_unique.txt diff --new-line-format="" --unchanged-line-format="" \ /tmp/only_make_unique.txt /tmp/memory.txt | \ xargs grep -l "absl/memory" > /tmp/add-memory.txt git grep -l "\babsl::make_unique\b" | \ xargs sed -i "s/\babsl::make_unique\b/std::make_unique/g" git checkout PRESUBMIT.py abseil-in-webrtc.md cat /tmp/add-memory.txt | \ xargs sed -i \ 's/#include "absl\/memory\/memory.h"/#include <memory>/g' git cl format # Manual fix order of the new inserted #include <memory> cat /tmp/only_make_unique | xargs grep -l "#include <memory>" | \ xargs sed -i '/#include "absl\/memory\/memory.h"/d' git ls-files | grep BUILD.gn | \ xargs sed -i '/\/\/third_party\/abseil-cpp\/absl\/memory/d' python tools_webrtc/gn_check_autofix.py \ -m tryserver.webrtc -b linux_rel # Repead the gn_check_autofix step for other platforms git ls-files | grep BUILD.gn | \ xargs sed -i 's/absl\/memory:memory/absl\/memory/g' git cl format Bug: webrtc:10945 Change-Id: I3fe28ea80f4dd3ba3cf28effd151d5e1f19aff89 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/153221 Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Alessio Bazzica <alessiob@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Cr-Commit-Position: refs/heads/master@{#29209}
2019-09-17 17:06:18 +02:00
return std::make_unique<SharedMemoryDesktopFrame>(
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
size, size.width() * kBytesPerPixel, std::move(shared_memory));
}
SharedMemoryDesktopFrame::SharedMemoryDesktopFrame(DesktopSize size,
int stride,
SharedMemory* shared_memory)
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(size,
stride,
reinterpret_cast<uint8_t*>(shared_memory->data()),
shared_memory) {}
SharedMemoryDesktopFrame::SharedMemoryDesktopFrame(
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
DesktopSize size,
int stride,
std::unique_ptr<SharedMemory> shared_memory)
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
: SharedMemoryDesktopFrame(size, stride, shared_memory.release()) {}
SharedMemoryDesktopFrame::~SharedMemoryDesktopFrame() {
delete shared_memory_;
}
} // namespace webrtc