[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / content / common / gpu / media / vaapi_jpeg_decoder_unittest.cc
blob2e9180f6b6638049fcf9a14649278fa818662b33
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <string>
7 // This has to be included first.
8 // See http://code.google.com/p/googletest/issues/detail?id=371
9 #include "testing/gtest/include/gtest/gtest.h"
11 #include "base/at_exit.h"
12 #include "base/bind.h"
13 #include "base/files/file_util.h"
14 #include "base/logging.h"
15 #include "base/md5.h"
16 #include "base/path_service.h"
17 #include "base/strings/string_piece.h"
18 #include "content/common/gpu/media/vaapi_jpeg_decoder.h"
19 #include "media/base/test_data_util.h"
20 #include "media/base/video_frame.h"
21 #include "media/filters/jpeg_parser.h"
23 namespace content {
24 namespace {
26 const char* kTestFilename = "pixel-1280x720.jpg";
27 const char* kExpectedMd5Sum = "6e9e1716073c9a9a1282e3f0e0dab743";
29 void LogOnError() {
30 LOG(FATAL) << "Oh noes! Decoder failed";
33 class VaapiJpegDecoderTest : public ::testing::Test {
34 protected:
35 VaapiJpegDecoderTest() {}
37 void SetUp() override {
38 base::Closure report_error_cb = base::Bind(&LogOnError);
39 wrapper_ = VaapiWrapper::Create(VaapiWrapper::kDecode,
40 VAProfileJPEGBaseline, report_error_cb);
41 ASSERT_TRUE(wrapper_);
43 base::FilePath input_file = media::GetTestDataFilePath(kTestFilename);
45 ASSERT_TRUE(base::ReadFileToString(input_file, &jpeg_data_))
46 << "failed to read input data from " << input_file.value();
49 void TearDown() override { wrapper_.reset(); }
51 bool VerifyDecode(const media::JpegParseResult& parse_result,
52 const std::string& md5sum);
54 protected:
55 scoped_ptr<VaapiWrapper> wrapper_;
56 std::string jpeg_data_;
59 bool VaapiJpegDecoderTest::VerifyDecode(
60 const media::JpegParseResult& parse_result,
61 const std::string& expected_md5sum) {
62 gfx::Size size(parse_result.frame_header.coded_width,
63 parse_result.frame_header.coded_height);
65 std::vector<VASurfaceID> va_surfaces;
66 if (!wrapper_->CreateSurfaces(size, 1, &va_surfaces))
67 return false;
69 if (!VaapiJpegDecoder::Decode(wrapper_.get(), parse_result, va_surfaces[0])) {
70 LOG(ERROR) << "Decode failed";
71 return false;
74 VAImage image;
75 VAImageFormat format;
76 const uint32_t kI420Fourcc = VA_FOURCC('I', '4', '2', '0');
77 memset(&image, 0, sizeof(image));
78 memset(&format, 0, sizeof(format));
79 format.fourcc = kI420Fourcc;
80 format.byte_order = VA_LSB_FIRST;
81 format.bits_per_pixel = 12; // 12 for I420
83 void* mem;
84 if (!wrapper_->GetVaImage(va_surfaces[0], &format, size, &image, &mem)) {
85 LOG(ERROR) << "Cannot get VAImage";
86 return false;
88 EXPECT_EQ(kI420Fourcc, image.format.fourcc);
90 base::StringPiece result(
91 reinterpret_cast<const char*>(mem),
92 media::VideoFrame::AllocationSize(media::VideoFrame::I420, size));
93 EXPECT_EQ(expected_md5sum, base::MD5String(result));
95 wrapper_->ReturnVaImage(&image);
97 return true;
100 TEST_F(VaapiJpegDecoderTest, DecodeSuccess) {
101 media::JpegParseResult parse_result;
102 ASSERT_TRUE(media::ParseJpegPicture(
103 reinterpret_cast<const uint8_t*>(jpeg_data_.data()), jpeg_data_.size(),
104 &parse_result));
106 EXPECT_TRUE(VerifyDecode(parse_result, kExpectedMd5Sum));
109 TEST_F(VaapiJpegDecoderTest, DecodeFail) {
110 media::JpegParseResult parse_result;
111 ASSERT_TRUE(media::ParseJpegPicture(
112 reinterpret_cast<const uint8_t*>(jpeg_data_.data()), jpeg_data_.size(),
113 &parse_result));
115 // Not supported by VAAPI.
116 parse_result.frame_header.num_components = 1;
117 parse_result.scan.num_components = 1;
119 gfx::Size size(parse_result.frame_header.coded_width,
120 parse_result.frame_header.coded_height);
122 std::vector<VASurfaceID> va_surfaces;
123 ASSERT_TRUE(wrapper_->CreateSurfaces(size, 1, &va_surfaces));
125 EXPECT_FALSE(
126 VaapiJpegDecoder::Decode(wrapper_.get(), parse_result, va_surfaces[0]));
129 } // namespace
130 } // namespace content
132 int main(int argc, char** argv) {
133 testing::InitGoogleTest(&argc, argv);
134 base::AtExitManager exit_manager;
135 content::VaapiWrapper::PreSandboxInitialization();
136 return RUN_ALL_TESTS();