#include "content/renderer/media/gpu/gpu_video_accelerator_factories_impl.h"
#include <GLES2/gl2.h>
#include <cstddef>
#include <memory>
#include "base/memory/scoped_refptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/gtest_util.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "build/build_config.h"
#include "components/viz/common/gpu/context_cache_controller.h"
#include "components/viz/common/gpu/context_lost_observer.h"
#include "content/public/common/gpu_stream_constants.h"
#include "content/renderer/media/codec_factory.h"
#include "gpu/command_buffer/client/gles2_interface_stub.h"
#include "gpu/command_buffer/client/test_gpu_memory_buffer_manager.h"
#include "gpu/command_buffer/common/capabilities.h"
#include "gpu/command_buffer/common/context_creation_attribs.h"
#include "gpu/command_buffer/common/context_result.h"
#include "gpu/config/gpu_feature_info.h"
#include "gpu/ipc/client/command_buffer_proxy_impl.h"
#include "gpu/ipc/client/gpu_channel_host.h"
#include "gpu/ipc/common/gpu_channel.mojom.h"
#include "gpu/ipc/common/mock_gpu_channel.h"
#include "media/base/decoder.h"
#include "media/base/media_util.h"
#include "media/base/supported_video_decoder_config.h"
#include "media/base/video_codecs.h"
#include "media/base/video_decoder_config.h"
#include "media/mojo/buildflags.h"
#include "media/mojo/mojom/video_encode_accelerator.mojom.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "services/viz/public/cpp/gpu/context_provider_command_buffer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest-death-test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkImage.h"
#if BUILDFLAG(ENABLE_MOJO_VIDEO_DECODER)
#include "content/renderer/media/codec_factory_mojo.h"
#include "media/filters/fake_video_decoder.h"
#include "media/mojo/mojom/interface_factory.mojom.h"
#include "media/mojo/services/mojo_cdm_service_context.h"
#include "media/mojo/services/mojo_media_client.h"
#include "media/mojo/services/mojo_video_decoder_service.h"
#include "mojo/public/cpp/bindings/unique_receiver_set.h"
#endif
#if BUILDFLAG(IS_FUCHSIA)
#include "content/renderer/media/codec_factory_fuchsia.h"
#include "media/mojo/mojom/fuchsia_media.mojom.h"
#endif
_;
Invoke;
NiceMock;
Return;
namespace content {
namespace {
constexpr gfx::Size kCodedSize(320, 240);
constexpr gfx::Rect kVisibleRect(320, 240);
constexpr gfx::Size kNaturalSize(320, 240);
const media::SupportedVideoDecoderConfig kH264MaxSupportedVideoDecoderConfig = …;
const media::VideoDecoderConfig kH264BaseConfig(
media::VideoCodec::kH264,
media::H264PROFILE_MIN,
media::VideoDecoderConfig::AlphaMode::kIsOpaque,
media::VideoColorSpace(),
media::kNoTransformation,
kCodedSize,
kVisibleRect,
kNaturalSize,
media::EmptyExtraData(),
media::EncryptionScheme::kUnencrypted);
const media::VideoDecoderConfig kVP9BaseConfig(
media::VideoCodec::kVP9,
media::VP9PROFILE_MIN,
media::VideoDecoderConfig::AlphaMode::kIsOpaque,
media::VideoColorSpace(),
media::kNoTransformation,
kCodedSize,
kVisibleRect,
kNaturalSize,
media::EmptyExtraData(),
media::EncryptionScheme::kUnencrypted);
}
class TestGpuChannelHost : public gpu::GpuChannelHost { … };
class MockOverlayInfoCbHandler { … };
class MockContextProviderCommandBuffer
: public viz::ContextProviderCommandBuffer { … };
class MockGLESInterface : public gpu::gles2::GLES2InterfaceStub { … };
class FakeVEAProviderImpl
: public media::mojom::VideoEncodeAcceleratorProvider { … };
#if BUILDFLAG(ENABLE_MOJO_VIDEO_DECODER)
class FakeMojoMediaClient : public media::MojoMediaClient { … };
class FakeInterfaceFactory : public media::mojom::InterfaceFactory { … };
#endif
#if BUILDFLAG(IS_FUCHSIA)
class FakeFuchsiaMediaCodecProvide
: public media::mojom::FuchsiaMediaCodecProvider {
public:
~FakeFuchsiaMediaCodecProvide() override = default;
void Bind(
mojo::PendingReceiver<media::mojom::FuchsiaMediaCodecProvider> receiver) {
receiver_.Bind(std::move(receiver));
}
void SetSupportedVideoDecoderConfigs(
media::SupportedVideoDecoderConfigs configs) {
supported_video_decoder_configs_ = configs;
}
void CreateVideoDecoder(
media::VideoCodec codec,
media::mojom::VideoDecoderSecureMemoryMode secure_mode,
fidl::InterfaceRequest<fuchsia::media::StreamProcessor>
stream_processor_request) final {
ADD_FAILURE() << "Not implemented.";
}
void GetSupportedVideoDecoderConfigs(
GetSupportedVideoDecoderConfigsCallback callback) final {
std::move(callback).Run(supported_video_decoder_configs_);
}
private:
media::SupportedVideoDecoderConfigs supported_video_decoder_configs_;
mojo::Receiver<media::mojom::FuchsiaMediaCodecProvider> receiver_{this};
};
#endif
class GpuVideoAcceleratorFactoriesImplTest : public testing::Test { … };
TEST_F(GpuVideoAcceleratorFactoriesImplTest, VideoDecoderAcceleratorDisabled) { … }
TEST_F(GpuVideoAcceleratorFactoriesImplTest, VideoEncoderAcceleratorDisabled) { … }
TEST_F(GpuVideoAcceleratorFactoriesImplTest, EncoderConfigsIsSupported) { … }
TEST_F(GpuVideoAcceleratorFactoriesImplTest, EncoderConfigsIsNotSupported) { … }
TEST_F(GpuVideoAcceleratorFactoriesImplTest, CreateVideoEncodeAccelerator) { … }
#ifdef GTEST_HAS_DEATH_TEST
GpuVideoAcceleratorFactoriesImplDeathTest;
TEST_F(GpuVideoAcceleratorFactoriesImplDeathTest,
CreateVideoEncodeAcceleratorFailed) { … }
TEST_F(GpuVideoAcceleratorFactoriesImplDeathTest, CreateVideoDecoderFailed) { … }
#endif
#if BUILDFLAG(ENABLE_MOJO_VIDEO_DECODER) || BUILDFLAG(IS_FUCHSIA)
TEST_F(GpuVideoAcceleratorFactoriesImplTest, DecoderConfigIsSupported) { … }
TEST_F(GpuVideoAcceleratorFactoriesImplTest, DecoderConfigIsNotSupported) { … }
TEST_F(GpuVideoAcceleratorFactoriesImplTest, CreateVideoDecoder) { … }
#else
TEST_F(GpuVideoAcceleratorFactoriesImplTest, DefaultCodecFactory) {
auto gpu_video_accelerator_factories =
CreateGpuVideoAcceleratorFactories(false, false);
EXPECT_FALSE(
gpu_video_accelerator_factories->IsGpuVideoDecodeAcceleratorEnabled());
EXPECT_TRUE(gpu_video_accelerator_factories->IsDecoderSupportKnown());
base::test::TestFuture<void> future;
gpu_video_accelerator_factories->NotifyDecoderSupportKnown(
future.GetCallback());
EXPECT_TRUE(future.Wait());
EXPECT_EQ(gpu_video_accelerator_factories->IsDecoderConfigSupported(
kH264BaseConfig),
media::GpuVideoAcceleratorFactories::Supported::kFalse);
}
#endif
}