From cc26fef5b2baf8f86cecb60bdc288a9d50d76b6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Tue, 19 Feb 2019 15:42:15 +0100 Subject: [PATCH] Use a CopyOnWriteBuffer to back EncodedImage data Intended to make copy construction and assignment of EncodedImage cheaper, but otherwise not have any effect on users of the class. Bug: webrtc:9378, chromium:931692 Change-Id: I22cf8c05f6ef7b7b5cf7ef08fd0dfc5c61211196 Reviewed-on: https://webrtc-review.googlesource.com/c/123442 Reviewed-by: Philip Eliasson Reviewed-by: Karl Wiberg Commit-Queue: Niels Moller Cr-Commit-Position: refs/heads/master@{#26760} --- api/video/DEPS | 5 +++++ api/video/encoded_image.cc | 3 +-- api/video/encoded_image.h | 8 ++++---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/api/video/DEPS b/api/video/DEPS index 4bc9a3901b..6cecc23576 100644 --- a/api/video/DEPS +++ b/api/video/DEPS @@ -6,6 +6,11 @@ specific_include_rules = { "+modules/video_coding/encoded_frame.h", ], + # Used for a private member variable. + "encoded_image\.h": [ + "+rtc_base/copy_on_write_buffer.h", + ], + "i010_buffer\.h": [ "+rtc_base/memory/aligned_malloc.h", ], diff --git a/api/video/encoded_image.cc b/api/video/encoded_image.cc index f96b9c2783..d1fa046472 100644 --- a/api/video/encoded_image.cc +++ b/api/video/encoded_image.cc @@ -42,8 +42,7 @@ EncodedImage& EncodedImage::operator=(const EncodedImage&) = default; void EncodedImage::Retain() { if (buffer_) { - encoded_data_ = std::vector(size_); - memcpy(encoded_data_.data(), buffer_, size_); + encoded_data_.SetData(buffer_, size_); buffer_ = nullptr; } } diff --git a/api/video/encoded_image.h b/api/video/encoded_image.h index 1c91400c89..1d3bd46ef5 100644 --- a/api/video/encoded_image.h +++ b/api/video/encoded_image.h @@ -12,7 +12,6 @@ #define API_VIDEO_ENCODED_IMAGE_H_ #include -#include #include "absl/types/optional.h" #include "api/video/color_space.h" @@ -23,6 +22,7 @@ #include "api/video/video_timing.h" #include "common_types.h" // NOLINT(build/include) #include "rtc_base/checks.h" +#include "rtc_base/copy_on_write_buffer.h" #include "rtc_base/system/rtc_export.h" namespace webrtc { @@ -88,13 +88,13 @@ class RTC_EXPORT EncodedImage { } void Allocate(size_t capacity) { - encoded_data_.resize(capacity); + encoded_data_.SetSize(capacity); buffer_ = nullptr; } uint8_t* data() { return buffer_ ? buffer_ : encoded_data_.data(); } const uint8_t* data() const { - return buffer_ ? buffer_ : encoded_data_.data(); + return buffer_ ? buffer_ : encoded_data_.cdata(); } // TODO(nisse): At some places, code accepts a const ref EncodedImage, but // still writes to it, to clear padding at the end of the encoded data. @@ -141,7 +141,7 @@ class RTC_EXPORT EncodedImage { private: // TODO(bugs.webrtc.org/9378): We're transitioning to always owning the // encoded data. - std::vector encoded_data_; + rtc::CopyOnWriteBuffer encoded_data_; size_t size_; // Size of encoded frame data. // Non-null when used with an un-owned buffer. uint8_t* buffer_;