Commit 4d69f359 authored by David Staessens's avatar David Staessens Committed by Commit Bot

media/gpu/test: Move AlignedAllocator to video test helpers.

This CL moves the AlignedAllocator used in the video encode accelerator
unittest to the video test helpers. This allows it to be reused in a
subsequent CL introducing new video encoder tests.

TEST=./video_encode_accelerator_unittest on nocturne

BUG=1045825

Change-Id: I6a4d837d6f365d6b79878a35b69a5f3267a69c8d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2026513
Commit-Queue: David Staessens <dstaessens@chromium.org>
Reviewed-by: default avatarHirokazu Honda <hiroh@chromium.org>
Reviewed-by: default avatarAlexandre Courbot <acourbot@chromium.org>
Cr-Commit-Position: refs/heads/master@{#744873}
parent 0cba24ab
...@@ -8,11 +8,14 @@ ...@@ -8,11 +8,14 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/bits.h"
#include "base/containers/queue.h" #include "base/containers/queue.h"
#include "base/memory/aligned_memory.h"
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/synchronization/condition_variable.h" #include "base/synchronization/condition_variable.h"
#include "base/synchronization/lock.h" #include "base/synchronization/lock.h"
#include "build/build_config.h"
#include "media/base/decoder_buffer.h" #include "media/base/decoder_buffer.h"
#include "media/base/video_codecs.h" #include "media/base/video_codecs.h"
...@@ -100,6 +103,53 @@ class EncodedDataHelper { ...@@ -100,6 +103,53 @@ class EncodedDataHelper {
size_t num_skipped_fragments_ = 0; size_t num_skipped_fragments_ = 0;
}; };
#if defined(ARCH_CPU_ARM_FAMILY)
// ARM performs CPU cache management with CPU cache line granularity. We thus
// need to ensure our buffers are CPU cache line-aligned (64 byte-aligned).
// Otherwise newer kernels will refuse to accept them, and on older kernels
// we'll be treating ourselves to random corruption.
// Moreover, some hardware codecs require 128-byte alignment for physical
// buffers.
constexpr size_t kPlatformBufferAlignment = 128;
#else
constexpr size_t kPlatformBufferAlignment = 8;
#endif
inline static size_t AlignToPlatformRequirements(size_t value) {
return base::bits::Align(value, kPlatformBufferAlignment);
}
// An aligned STL allocator.
template <typename T, size_t ByteAlignment = kPlatformBufferAlignment>
class AlignedAllocator : public std::allocator<T> {
public:
typedef size_t size_type;
typedef T* pointer;
template <class T1>
struct rebind {
typedef AlignedAllocator<T1, ByteAlignment> other;
};
AlignedAllocator() {}
explicit AlignedAllocator(const AlignedAllocator&) {}
template <class T1>
explicit AlignedAllocator(const AlignedAllocator<T1, ByteAlignment>&) {}
~AlignedAllocator() {}
pointer allocate(size_type n, const void* = 0) {
return static_cast<pointer>(base::AlignedAlloc(n, ByteAlignment));
}
void deallocate(pointer p, size_type n) {
base::AlignedFree(static_cast<void*>(p));
}
size_type max_size() const {
return std::numeric_limits<size_t>::max() / sizeof(T);
}
};
} // namespace test } // namespace test
} // namespace media } // namespace media
#endif // MEDIA_GPU_TEST_VIDEO_TEST_HELPERS_H_ #endif // MEDIA_GPU_TEST_VIDEO_TEST_HELPERS_H_
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
#include "base/containers/queue.h" #include "base/containers/queue.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/aligned_memory.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/unsafe_shared_memory_region.h" #include "base/memory/unsafe_shared_memory_region.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
...@@ -220,53 +219,6 @@ VideoEncodeAcceleratorTestEnvironment* g_env; ...@@ -220,53 +219,6 @@ VideoEncodeAcceleratorTestEnvironment* g_env;
// "--num_frames_to_encode". Ignored if 0. // "--num_frames_to_encode". Ignored if 0.
int g_num_frames_to_encode = 0; int g_num_frames_to_encode = 0;
#if defined(ARCH_CPU_ARM_FAMILY)
// ARM performs CPU cache management with CPU cache line granularity. We thus
// need to ensure our buffers are CPU cache line-aligned (64 byte-aligned).
// Otherwise newer kernels will refuse to accept them, and on older kernels
// we'll be treating ourselves to random corruption.
// Moreover, some hardware codecs require 128-byte alignment for physical
// buffers.
const size_t kPlatformBufferAlignment = 128;
#else
const size_t kPlatformBufferAlignment = 8;
#endif
inline static size_t AlignToPlatformRequirements(size_t value) {
return base::bits::Align(value, kPlatformBufferAlignment);
}
// An aligned STL allocator.
template <typename T, size_t ByteAlignment>
class AlignedAllocator : public std::allocator<T> {
public:
typedef size_t size_type;
typedef T* pointer;
template <class T1>
struct rebind {
typedef AlignedAllocator<T1, ByteAlignment> other;
};
AlignedAllocator() {}
explicit AlignedAllocator(const AlignedAllocator&) {}
template <class T1>
explicit AlignedAllocator(const AlignedAllocator<T1, ByteAlignment>&) {}
~AlignedAllocator() {}
pointer allocate(size_type n, const void* = 0) {
return static_cast<pointer>(base::AlignedAlloc(n, ByteAlignment));
}
void deallocate(pointer p, size_type n) {
base::AlignedFree(static_cast<void*>(p));
}
size_type max_size() const {
return std::numeric_limits<size_t>::max() / sizeof(T);
}
};
struct TestStream { struct TestStream {
TestStream() TestStream()
: num_frames(0), : num_frames(0),
...@@ -288,7 +240,8 @@ struct TestStream { ...@@ -288,7 +240,8 @@ struct TestStream {
// A vector used to prepare aligned input buffers of |in_filename|. This // A vector used to prepare aligned input buffers of |in_filename|. This
// makes sure starting addresses of YUV planes are aligned to // makes sure starting addresses of YUV planes are aligned to
// kPlatformBufferAlignment bytes. // kPlatformBufferAlignment bytes.
std::vector<char, AlignedAllocator<char, kPlatformBufferAlignment>> std::vector<char,
test::AlignedAllocator<char, test::kPlatformBufferAlignment>>
aligned_in_file_data; aligned_in_file_data;
// Byte size of a frame of |aligned_in_file_data|. // Byte size of a frame of |aligned_in_file_data|.
...@@ -535,7 +488,8 @@ static void CreateAlignedInputStreamFile(const gfx::Size& coded_size, ...@@ -535,7 +488,8 @@ static void CreateAlignedInputStreamFile(const gfx::Size& coded_size,
VideoFrame::Rows(i, pixel_format, test_stream->visible_size.height()); VideoFrame::Rows(i, pixel_format, test_stream->visible_size.height());
size_t coded_area_size = size_t coded_area_size =
coded_bpl[i] * VideoFrame::Rows(i, pixel_format, coded_size.height()); coded_bpl[i] * VideoFrame::Rows(i, pixel_format, coded_size.height());
const size_t aligned_size = AlignToPlatformRequirements(coded_area_size); const size_t aligned_size =
test::AlignToPlatformRequirements(coded_area_size);
test_stream->aligned_plane_size.push_back(aligned_size); test_stream->aligned_plane_size.push_back(aligned_size);
test_stream->aligned_buffer_size += aligned_size; test_stream->aligned_buffer_size += aligned_size;
} }
...@@ -582,7 +536,7 @@ static void CreateAlignedInputStreamFile(const gfx::Size& coded_size, ...@@ -582,7 +536,7 @@ static void CreateAlignedInputStreamFile(const gfx::Size& coded_size,
const char* src_ptr = &src_data[0]; const char* src_ptr = &src_data[0];
for (size_t i = 0; i < num_planes; i++) { for (size_t i = 0; i < num_planes; i++) {
// Assert that each plane of frame starts at required byte boundary. // Assert that each plane of frame starts at required byte boundary.
ASSERT_EQ(0u, dest_offset & (kPlatformBufferAlignment - 1)) ASSERT_EQ(0u, dest_offset & (test::kPlatformBufferAlignment - 1))
<< "Planes of frame should be mapped per platform requirements"; << "Planes of frame should be mapped per platform requirements";
char* dst_ptr = &test_stream->aligned_in_file_data[dest_offset]; char* dst_ptr = &test_stream->aligned_in_file_data[dest_offset];
for (size_t j = 0; j < visible_plane_rows[i]; j++) { for (size_t j = 0; j < visible_plane_rows[i]; j++) {
...@@ -2676,7 +2630,7 @@ void VEACacheLineUnalignedInputClient::FeedEncoderWithOneInput( ...@@ -2676,7 +2630,7 @@ void VEACacheLineUnalignedInputClient::FeedEncoderWithOneInput(
for (size_t i = 0; i < num_planes; i++) { for (size_t i = 0; i < num_planes; i++) {
size_t plane_size = base::bits::Align( size_t plane_size = base::bits::Align(
VideoFrame::PlaneSize(pixel_format, i, input_coded_size).GetArea(), VideoFrame::PlaneSize(pixel_format, i, input_coded_size).GetArea(),
kPlatformBufferAlignment); test::kPlatformBufferAlignment);
planes[i].stride = planes[i].stride =
VideoFrame::RowBytes(i, pixel_format, input_coded_size.width()); VideoFrame::RowBytes(i, pixel_format, input_coded_size.width());
...@@ -2686,7 +2640,7 @@ void VEACacheLineUnalignedInputClient::FeedEncoderWithOneInput( ...@@ -2686,7 +2640,7 @@ void VEACacheLineUnalignedInputClient::FeedEncoderWithOneInput(
} }
auto layout = VideoFrameLayout::CreateWithPlanes( auto layout = VideoFrameLayout::CreateWithPlanes(
pixel_format, input_coded_size, std::move(planes), pixel_format, input_coded_size, std::move(planes),
kPlatformBufferAlignment); test::kPlatformBufferAlignment);
ASSERT_TRUE(layout); ASSERT_TRUE(layout);
scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateFrameWithLayout( scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateFrameWithLayout(
*layout, gfx::Rect(input_coded_size), input_coded_size, *layout, gfx::Rect(input_coded_size), input_coded_size,
......
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