Commit f691b1d9 authored by Sheng-Hao Tsao's avatar Sheng-Hao Tsao Committed by Commit Bot

Add parameters to jpeg_encode_accelerator_unittest

This CL adds some command line parameters to
jpeg_encode_accelerator_unittest to enable JEAPerf autotest.

BUG=b:67488323
TEST=Verified that JEAPerf collects metrics successfully on eve.

Cq-Include-Trybots: master.tryserver.chromium.android:android_optional_gpu_tests_rel;master.tryserver.chromium.linux:linux_optional_gpu_tests_rel;master.tryserver.chromium.mac:mac_optional_gpu_tests_rel;master.tryserver.chromium.win:win_optional_gpu_tests_rel
Change-Id: Ie369b17302e6f6587e81126ed35d577e71e00d68
Reviewed-on: https://chromium-review.googlesource.com/874493Reviewed-by: default avatarRicky Liang <jcliang@chromium.org>
Reviewed-by: default avatarWu-Cheng Li <wuchengli@chromium.org>
Commit-Queue: Sheng-hao Tsao <shenghao@google.com>
Cr-Commit-Position: refs/heads/master@{#531856}
parent 3b416c2b
......@@ -22,6 +22,7 @@
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread.h"
#include "base/threading/thread_task_runner_handle.h"
......@@ -78,6 +79,171 @@ enum class ClientState {
ERROR,
};
class JpegEncodeAcceleratorTestEnvironment : public ::testing::Environment {
public:
JpegEncodeAcceleratorTestEnvironment(
const base::FilePath::CharType* yuv_filenames,
const base::FilePath log_path,
const int repeat)
: repeat_(repeat), log_path_(log_path) {
user_yuv_files_ = yuv_filenames ? yuv_filenames : kDefaultYuvFilename;
}
void SetUp() override;
void TearDown() override;
void LogToFile(const std::string& key, const std::string& value);
// Read image from |filename| to |image_data|.
void ReadTestYuvImage(base::FilePath& filename, TestImageFile* image_data);
// Returns a file path for a file in what name specified or media/test/data
// directory. If the original file path is existed, returns it first.
base::FilePath GetOriginalOrTestDataFilePath(const std::string& name);
// Parsed data from command line.
std::vector<std::unique_ptr<TestImageFile>> image_data_user_;
// Parsed data of |test_1280x720_yuv_file_|.
std::unique_ptr<TestImageFile> image_data_1280x720_white_;
// Parsed data of |test_640x368_yuv_file_|.
std::unique_ptr<TestImageFile> image_data_640x368_black_;
// Parsed data of |test_640x360_yuv_file_|.
std::unique_ptr<TestImageFile> image_data_640x360_black_;
// Number of times SimpleEncodeTest should repeat for an image.
const size_t repeat_;
private:
// Create black or white test image with |width| and |height| size.
void CreateTestYuvImage(int width,
int height,
bool is_black,
base::FilePath* filename);
const base::FilePath::CharType* user_yuv_files_;
const base::FilePath log_path_;
std::unique_ptr<base::File> log_file_;
// Programatically generated YUV files.
base::FilePath test_1280x720_yuv_file_;
base::FilePath test_640x368_yuv_file_;
base::FilePath test_640x360_yuv_file_;
};
void JpegEncodeAcceleratorTestEnvironment::SetUp() {
if (!log_path_.empty()) {
log_file_.reset(new base::File(
log_path_, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE));
LOG_ASSERT(log_file_->IsValid());
}
CreateTestYuvImage(1280, 720, false, &test_1280x720_yuv_file_);
CreateTestYuvImage(640, 368, true, &test_640x368_yuv_file_);
CreateTestYuvImage(640, 360, true, &test_640x360_yuv_file_);
image_data_1280x720_white_.reset(
new TestImageFile(test_1280x720_yuv_file_.value(), gfx::Size(1280, 720)));
ASSERT_NO_FATAL_FAILURE(ReadTestYuvImage(test_1280x720_yuv_file_,
image_data_1280x720_white_.get()));
image_data_640x368_black_.reset(
new TestImageFile(test_640x368_yuv_file_.value(), gfx::Size(640, 368)));
ASSERT_NO_FATAL_FAILURE(ReadTestYuvImage(test_640x368_yuv_file_,
image_data_640x368_black_.get()));
image_data_640x360_black_.reset(
new TestImageFile(test_640x360_yuv_file_.value(), gfx::Size(640, 360)));
ASSERT_NO_FATAL_FAILURE(ReadTestYuvImage(test_640x360_yuv_file_,
image_data_640x360_black_.get()));
// |user_yuv_files_| may include many files and use ';' as delimiter.
std::vector<base::FilePath::StringType> files =
base::SplitString(user_yuv_files_, base::FilePath::StringType(1, ';'),
base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
for (const auto& file : files) {
std::vector<base::FilePath::StringType> filename_and_size =
base::SplitString(file, base::FilePath::StringType(1, ':'),
base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
ASSERT_EQ(2u, filename_and_size.size());
base::FilePath::StringType filename(filename_and_size[0]);
std::vector<base::FilePath::StringType> image_resolution =
base::SplitString(filename_and_size[1],
base::FilePath::StringType(1, 'x'),
base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
ASSERT_EQ(2u, image_resolution.size());
int width = 0, height = 0;
ASSERT_TRUE(base::StringToInt(image_resolution[0], &width));
ASSERT_TRUE(base::StringToInt(image_resolution[1], &height));
gfx::Size image_size(width, height);
ASSERT_TRUE(!image_size.IsEmpty());
base::FilePath input_file = GetOriginalOrTestDataFilePath(filename);
auto image_data = std::make_unique<TestImageFile>(filename, image_size);
ASSERT_NO_FATAL_FAILURE(ReadTestYuvImage(input_file, image_data.get()));
image_data_user_.push_back(std::move(image_data));
}
}
void JpegEncodeAcceleratorTestEnvironment::TearDown() {
log_file_.reset();
base::DeleteFile(test_1280x720_yuv_file_, false);
base::DeleteFile(test_640x368_yuv_file_, false);
base::DeleteFile(test_640x360_yuv_file_, false);
}
void JpegEncodeAcceleratorTestEnvironment::LogToFile(const std::string& key,
const std::string& value) {
std::string s = base::StringPrintf("%s: %s\n", key.c_str(), value.c_str());
LOG(INFO) << s;
if (log_file_) {
log_file_->WriteAtCurrentPos(s.data(), static_cast<int>(s.length()));
}
}
void JpegEncodeAcceleratorTestEnvironment::CreateTestYuvImage(
int width,
int height,
bool is_black,
base::FilePath* filename) {
std::vector<uint8_t> buffer(width * height * 3 / 2);
size_t size = width * height;
// Fill in Y values.
memset(buffer.data(), is_black ? 0 : 255, size);
// FIll in U and V values.
memset(buffer.data() + size, 128, size / 2);
LOG_ASSERT(base::CreateTemporaryFile(filename));
EXPECT_TRUE(base::AppendToFile(
*filename, reinterpret_cast<char*>(buffer.data()), buffer.size()));
}
void JpegEncodeAcceleratorTestEnvironment::ReadTestYuvImage(
base::FilePath& input_file,
TestImageFile* image_data) {
ASSERT_TRUE(base::ReadFileToString(input_file, &image_data->data_str));
// This is just a placeholder. We will compute the real output size when we
// have encoder instance.
image_data->output_size =
VideoFrame::AllocationSize(PIXEL_FORMAT_I420, image_data->visible_size);
}
base::FilePath
JpegEncodeAcceleratorTestEnvironment::GetOriginalOrTestDataFilePath(
const std::string& name) {
base::FilePath original_file_path = base::FilePath(name);
base::FilePath return_file_path = GetTestDataFilePath(name);
if (PathExists(original_file_path))
return_file_path = original_file_path;
VLOG(3) << "Use file path " << return_file_path.value();
return return_file_path;
}
class JpegClient : public JpegEncodeAccelerator::Client {
public:
JpegClient(const std::vector<TestImageFile*>& test_image_files,
......@@ -190,8 +356,10 @@ void JpegClient::VideoFrameReady(int video_frame_id, size_t hw_encoded_size) {
test_image->visible_size.height(),
&sw_encoded_size, &elapsed_sw));
LOG(INFO) << "HW encode elapsed time: " << elapsed_hw.InMicroseconds()
<< " SW encode elapsed time: " << elapsed_sw.InMicroseconds();
g_env->LogToFile("hw_encode_time",
base::Int64ToString(elapsed_hw.InMicroseconds()));
g_env->LogToFile("sw_encode_time",
base::Int64ToString(elapsed_sw.InMicroseconds()));
if (g_save_to_file) {
SaveToFile(test_image, hw_encoded_size, sw_encoded_size);
......@@ -375,144 +543,6 @@ void JpegClient::StartEncode(int32_t bitstream_buffer_id) {
encoder_->Encode(input_frame_, kJpegDefaultQuality, *encoded_buffer_);
}
class JpegEncodeAcceleratorTestEnvironment : public ::testing::Environment {
public:
JpegEncodeAcceleratorTestEnvironment(
const base::FilePath::CharType* yuv_filenames) {
user_yuv_files_ = yuv_filenames ? yuv_filenames : kDefaultYuvFilename;
}
void SetUp() override;
void TearDown() override;
// Read image from |filename| to |image_data|.
void ReadTestYuvImage(base::FilePath& filename, TestImageFile* image_data);
// Returns a file path for a file in what name specified or media/test/data
// directory. If the original file path is existed, returns it first.
base::FilePath GetOriginalOrTestDataFilePath(const std::string& name);
// Parsed data from command line.
std::vector<std::unique_ptr<TestImageFile>> image_data_user_;
// Parsed data of |test_1280x720_yuv_file_|.
std::unique_ptr<TestImageFile> image_data_1280x720_white_;
// Parsed data of |test_640x368_yuv_file_|.
std::unique_ptr<TestImageFile> image_data_640x368_black_;
// Parsed data of |test_640x360_yuv_file_|.
std::unique_ptr<TestImageFile> image_data_640x360_black_;
private:
// Create black or white test image with |width| and |height| size.
void CreateTestYuvImage(int width,
int height,
bool is_black,
base::FilePath* filename);
const base::FilePath::CharType* user_yuv_files_;
// Programatically generated YUV files.
base::FilePath test_1280x720_yuv_file_;
base::FilePath test_640x368_yuv_file_;
base::FilePath test_640x360_yuv_file_;
};
void JpegEncodeAcceleratorTestEnvironment::SetUp() {
CreateTestYuvImage(1280, 720, false, &test_1280x720_yuv_file_);
CreateTestYuvImage(640, 368, true, &test_640x368_yuv_file_);
CreateTestYuvImage(640, 360, true, &test_640x360_yuv_file_);
image_data_1280x720_white_.reset(
new TestImageFile(test_1280x720_yuv_file_.value(), gfx::Size(1280, 720)));
ASSERT_NO_FATAL_FAILURE(ReadTestYuvImage(test_1280x720_yuv_file_,
image_data_1280x720_white_.get()));
image_data_640x368_black_.reset(
new TestImageFile(test_640x368_yuv_file_.value(), gfx::Size(640, 368)));
ASSERT_NO_FATAL_FAILURE(ReadTestYuvImage(test_640x368_yuv_file_,
image_data_640x368_black_.get()));
image_data_640x360_black_.reset(
new TestImageFile(test_640x360_yuv_file_.value(), gfx::Size(640, 360)));
ASSERT_NO_FATAL_FAILURE(ReadTestYuvImage(test_640x360_yuv_file_,
image_data_640x360_black_.get()));
// |user_yuv_files_| may include many files and use ';' as delimiter.
std::vector<base::FilePath::StringType> files =
base::SplitString(user_yuv_files_, base::FilePath::StringType(1, ';'),
base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
for (const auto& file : files) {
std::vector<base::FilePath::StringType> filename_and_size =
base::SplitString(file, base::FilePath::StringType(1, ':'),
base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
ASSERT_EQ(2u, filename_and_size.size());
base::FilePath::StringType filename(filename_and_size[0]);
std::vector<base::FilePath::StringType> image_resolution =
base::SplitString(filename_and_size[1],
base::FilePath::StringType(1, 'x'),
base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
ASSERT_EQ(2u, image_resolution.size());
int width = 0, height = 0;
ASSERT_TRUE(base::StringToInt(image_resolution[0], &width));
ASSERT_TRUE(base::StringToInt(image_resolution[1], &height));
gfx::Size image_size(width, height);
ASSERT_TRUE(!image_size.IsEmpty());
base::FilePath input_file = GetOriginalOrTestDataFilePath(filename);
auto image_data = std::make_unique<TestImageFile>(filename, image_size);
ASSERT_NO_FATAL_FAILURE(ReadTestYuvImage(input_file, image_data.get()));
image_data_user_.push_back(std::move(image_data));
}
}
void JpegEncodeAcceleratorTestEnvironment::TearDown() {
base::DeleteFile(test_1280x720_yuv_file_, false);
base::DeleteFile(test_640x368_yuv_file_, false);
base::DeleteFile(test_640x360_yuv_file_, false);
}
void JpegEncodeAcceleratorTestEnvironment::CreateTestYuvImage(
int width,
int height,
bool is_black,
base::FilePath* filename) {
std::vector<uint8_t> buffer(width * height * 3 / 2);
size_t size = width * height;
// Fill in Y values.
memset(buffer.data(), is_black ? 0 : 255, size);
// FIll in U and V values.
memset(buffer.data() + size, 128, size / 2);
LOG_ASSERT(base::CreateTemporaryFile(filename));
EXPECT_TRUE(base::AppendToFile(
*filename, reinterpret_cast<char*>(buffer.data()), buffer.size()));
}
void JpegEncodeAcceleratorTestEnvironment::ReadTestYuvImage(
base::FilePath& input_file,
TestImageFile* image_data) {
ASSERT_TRUE(base::ReadFileToString(input_file, &image_data->data_str));
// This is just a placeholder. We will compute the real output size when we
// have encoder instance.
image_data->output_size =
VideoFrame::AllocationSize(PIXEL_FORMAT_I420, image_data->visible_size);
}
base::FilePath
JpegEncodeAcceleratorTestEnvironment::GetOriginalOrTestDataFilePath(
const std::string& name) {
base::FilePath original_file_path = base::FilePath(name);
base::FilePath return_file_path = GetTestDataFilePath(name);
if (PathExists(original_file_path))
return_file_path = original_file_path;
VLOG(3) << "Use file path " << return_file_path.value();
return return_file_path;
}
class JpegEncodeAcceleratorTest : public ::testing::Test {
protected:
JpegEncodeAcceleratorTest() {}
......@@ -572,10 +602,12 @@ void JpegEncodeAcceleratorTest::TestEncode(size_t num_concurrent_encoders) {
}
TEST_F(JpegEncodeAcceleratorTest, SimpleEncode) {
for (size_t i = 0; i < g_env->repeat_; i++) {
for (auto& image : g_env->image_data_user_) {
test_image_files_.push_back(image.get());
expected_status_.push_back(ClientState::ENCODE_PASS);
}
}
TestEncode(1);
}
......@@ -619,6 +651,8 @@ int main(int argc, char** argv) {
DCHECK(cmd_line);
const base::FilePath::CharType* yuv_filenames = nullptr;
base::FilePath log_path;
size_t repeat = 1;
base::CommandLine::SwitchMap switches = cmd_line->GetSwitches();
for (base::CommandLine::SwitchMap::const_iterator it = switches.begin();
it != switches.end(); ++it) {
......@@ -629,6 +663,18 @@ int main(int argc, char** argv) {
yuv_filenames = it->second.c_str();
continue;
}
if (it->first == "output_log") {
log_path = base::FilePath(
base::FilePath::StringType(it->second.begin(), it->second.end()));
continue;
}
if (it->first == "repeat") {
if (!base::StringToSizeT(it->second, &repeat)) {
LOG(INFO) << "Can't parse parameter |repeat|: " << it->second;
repeat = 1;
}
continue;
}
if (it->first == "save_to_file") {
media::g_save_to_file = true;
continue;
......@@ -646,7 +692,8 @@ int main(int argc, char** argv) {
media::g_env = reinterpret_cast<media::JpegEncodeAcceleratorTestEnvironment*>(
testing::AddGlobalTestEnvironment(
new media::JpegEncodeAcceleratorTestEnvironment(yuv_filenames)));
new media::JpegEncodeAcceleratorTestEnvironment(yuv_filenames,
log_path, repeat)));
return RUN_ALL_TESTS();
}
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