Commit cf13f763 authored by Matthew Cary's avatar Matthew Cary Committed by Commit Bot

media: upgrade base::SharedMemory in video capture decoder

This replaces a legacy base::SharedMemory member with a
base::UnsafeSharedMemoryRegion and base::WritableMemoryMapping
pair. Functionality is unchanged, other than having separate
error messages for failure on region creation and region mapping.

Bug: 849207


Change-Id: I7f0123fddb02aa6f1cd093d1c142a780728822e0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1697601Reviewed-by: default avatarRicky Liang <jcliang@chromium.org>
Commit-Queue: Matthew Cary (CET) <mattcary@chromium.org>
Cr-Commit-Position: refs/heads/master@{#681749}
parent bad4f9cb
...@@ -74,24 +74,33 @@ void VideoCaptureJpegDecoderImpl::DecodeCapturedData( ...@@ -74,24 +74,33 @@ void VideoCaptureJpegDecoderImpl::DecodeCapturedData(
} }
// Enlarge input buffer if necessary. // Enlarge input buffer if necessary.
if (!in_shared_memory_.get() || if (!in_shared_region_.IsValid() || !in_shared_mapping_.IsValid() ||
in_buffer_size > in_shared_memory_->mapped_size()) { in_buffer_size > in_shared_mapping_.size()) {
// Reserve 2x space to avoid frequent reallocations for initial frames. // Reserve 2x space to avoid frequent reallocations for initial frames.
const size_t reserved_size = 2 * in_buffer_size; const size_t reserved_size = 2 * in_buffer_size;
in_shared_memory_.reset(new base::SharedMemory); in_shared_region_ = base::UnsafeSharedMemoryRegion::Create(reserved_size);
if (!in_shared_memory_->CreateAndMapAnonymous(reserved_size)) { if (!in_shared_region_.IsValid()) {
base::AutoLock lock(lock_); base::AutoLock lock(lock_);
decoder_status_ = FAILED; decoder_status_ = FAILED;
LOG(WARNING) << "CreateAndMapAnonymous failed, size=" << reserved_size; LOG(WARNING) << "UnsafeSharedMemoryRegion::Create failed, size="
<< reserved_size;
return;
}
in_shared_mapping_ = in_shared_region_.Map();
if (!in_shared_mapping_.IsValid()) {
base::AutoLock lock(lock_);
decoder_status_ = FAILED;
LOG(WARNING) << "UnsafeSharedMemoryRegion::Map failed, size="
<< reserved_size;
return; return;
} }
} }
memcpy(in_shared_memory_->memory(), data, in_buffer_size); memcpy(in_shared_mapping_.memory(), data, in_buffer_size);
// No need to lock for |in_buffer_id_| since IsDecoding_Locked() is false. // No need to lock for |in_buffer_id_| since IsDecoding_Locked() is false.
in_buffer_id_ = next_bitstream_buffer_id_; in_buffer_id_ = next_bitstream_buffer_id_;
media::BitstreamBuffer in_buffer(in_buffer_id_, in_shared_memory_->handle(), media::BitstreamBuffer in_buffer(in_buffer_id_, in_shared_region_.Duplicate(),
false /* read_only */, in_buffer_size); in_buffer_size);
// Mask against 30 bits, to avoid (undefined) wraparound on signed integer. // Mask against 30 bits, to avoid (undefined) wraparound on signed integer.
next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF; next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF;
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h" #include "base/sequence_checker.h"
#include "components/chromeos_camera/mojo_mjpeg_decode_accelerator.h" #include "components/chromeos_camera/mojo_mjpeg_decode_accelerator.h"
...@@ -26,7 +27,7 @@ namespace media { ...@@ -26,7 +27,7 @@ namespace media {
// Implementation of media::VideoCaptureJpegDecoder that delegates to a // Implementation of media::VideoCaptureJpegDecoder that delegates to a
// chromeos_camera::mojom::MjpegDecodeAccelerator. When a frame is received in // chromeos_camera::mojom::MjpegDecodeAccelerator. When a frame is received in
// DecodeCapturedData(), it is copied to |in_shared_memory| for IPC transport // DecodeCapturedData(), it is copied to |in_shared_region_| for IPC transport
// to |decoder_|. When the decoder is finished with the frame, |decode_done_cb_| // to |decoder_|. When the decoder is finished with the frame, |decode_done_cb_|
// is invoked. Until |decode_done_cb_| is invoked, subsequent calls to // is invoked. Until |decode_done_cb_| is invoked, subsequent calls to
// DecodeCapturedData() are ignored. // DecodeCapturedData() are ignored.
...@@ -100,7 +101,8 @@ class CAPTURE_EXPORT VideoCaptureJpegDecoderImpl ...@@ -100,7 +101,8 @@ class CAPTURE_EXPORT VideoCaptureJpegDecoderImpl
// Shared memory to store JPEG stream buffer. The input BitstreamBuffer is // Shared memory to store JPEG stream buffer. The input BitstreamBuffer is
// backed by this. // backed by this.
std::unique_ptr<base::SharedMemory> in_shared_memory_; base::UnsafeSharedMemoryRegion in_shared_region_;
base::WritableSharedMemoryMapping in_shared_mapping_;
STATUS decoder_status_; STATUS decoder_status_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment