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(
}
// Enlarge input buffer if necessary.
if (!in_shared_memory_.get() ||
in_buffer_size > in_shared_memory_->mapped_size()) {
if (!in_shared_region_.IsValid() || !in_shared_mapping_.IsValid() ||
in_buffer_size > in_shared_mapping_.size()) {
// Reserve 2x space to avoid frequent reallocations for initial frames.
const size_t reserved_size = 2 * in_buffer_size;
in_shared_memory_.reset(new base::SharedMemory);
if (!in_shared_memory_->CreateAndMapAnonymous(reserved_size)) {
in_shared_region_ = base::UnsafeSharedMemoryRegion::Create(reserved_size);
if (!in_shared_region_.IsValid()) {
base::AutoLock lock(lock_);
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;
}
}
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.
in_buffer_id_ = next_bitstream_buffer_id_;
media::BitstreamBuffer in_buffer(in_buffer_id_, in_shared_memory_->handle(),
false /* read_only */, in_buffer_size);
media::BitstreamBuffer in_buffer(in_buffer_id_, in_shared_region_.Duplicate(),
in_buffer_size);
// Mask against 30 bits, to avoid (undefined) wraparound on signed integer.
next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF;
......
......@@ -13,6 +13,7 @@
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "components/chromeos_camera/mojo_mjpeg_decode_accelerator.h"
......@@ -26,7 +27,7 @@ namespace media {
// Implementation of media::VideoCaptureJpegDecoder that delegates to a
// 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_|
// is invoked. Until |decode_done_cb_| is invoked, subsequent calls to
// DecodeCapturedData() are ignored.
......@@ -100,7 +101,8 @@ class CAPTURE_EXPORT VideoCaptureJpegDecoderImpl
// Shared memory to store JPEG stream buffer. The input BitstreamBuffer is
// backed by this.
std::unique_ptr<base::SharedMemory> in_shared_memory_;
base::UnsafeSharedMemoryRegion in_shared_region_;
base::WritableSharedMemoryMapping in_shared_mapping_;
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