Avi Drissman | d387f092 | 2022-09-14 20:51:31 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "media/video/renderable_gpu_memory_buffer_video_frame_pool.h" |
| 6 | |
Avi Drissman | d70f89a | 2023-01-11 23:52:55 | [diff] [blame] | 7 | #include "base/functional/callback_helpers.h" |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 8 | #include "base/memory/weak_ptr.h" |
Ben Wagner | b114256 | 2021-12-15 10:44:12 | [diff] [blame] | 9 | #include "base/task/thread_pool.h" |
| 10 | #include "base/test/bind.h" |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 11 | #include "base/test/scoped_feature_list.h" |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 12 | #include "base/test/task_environment.h" |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 13 | #include "components/viz/common/resources/shared_image_format.h" |
Mingjing Zhang | 38dff4f | 2024-01-08 21:08:26 | [diff] [blame^] | 14 | #include "gpu/command_buffer/client/client_shared_image.h" |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 15 | #include "gpu/config/gpu_finch_features.h" |
| 16 | #include "media/base/media_switches.h" |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 17 | #include "media/base/video_frame.h" |
| 18 | #include "media/video/fake_gpu_memory_buffer.h" |
| 19 | #include "testing/gmock/include/gmock/gmock.h" |
| 20 | #include "testing/gtest/include/gtest/gtest.h" |
| 21 | |
| 22 | using ::testing::_; |
| 23 | |
| 24 | namespace media { |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | class FakeContext : public RenderableGpuMemoryBufferVideoFramePool::Context { |
| 29 | public: |
| 30 | FakeContext() : weak_factory_(this) {} |
| 31 | ~FakeContext() override = default; |
| 32 | |
| 33 | std::unique_ptr<gfx::GpuMemoryBuffer> CreateGpuMemoryBuffer( |
| 34 | const gfx::Size& size, |
| 35 | gfx::BufferFormat format, |
| 36 | gfx::BufferUsage usage) override { |
| 37 | DoCreateGpuMemoryBuffer(size, format); |
| 38 | return std::make_unique<FakeGpuMemoryBuffer>(size, format); |
| 39 | } |
Mingjing Zhang | 38dff4f | 2024-01-08 21:08:26 | [diff] [blame^] | 40 | scoped_refptr<gpu::ClientSharedImage> CreateSharedImage( |
| 41 | gfx::GpuMemoryBuffer* gpu_memory_buffer, |
| 42 | const viz::SharedImageFormat& si_format, |
| 43 | const gfx::ColorSpace& color_space, |
| 44 | GrSurfaceOrigin surface_origin, |
| 45 | SkAlphaType alpha_type, |
| 46 | uint32_t usage, |
| 47 | gpu::SyncToken& sync_token) override { |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 48 | DoCreateSharedImage(si_format, gpu_memory_buffer->GetSize(), color_space, |
| 49 | surface_origin, alpha_type, usage, |
| 50 | gpu_memory_buffer->CloneHandle()); |
Mingjing Zhang | 38dff4f | 2024-01-08 21:08:26 | [diff] [blame^] | 51 | return base::MakeRefCounted<gpu::ClientSharedImage>( |
| 52 | gpu::Mailbox::GenerateForSharedImage()); |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 53 | } |
Mingjing Zhang | 38dff4f | 2024-01-08 21:08:26 | [diff] [blame^] | 54 | scoped_refptr<gpu::ClientSharedImage> CreateSharedImage( |
| 55 | gfx::GpuMemoryBuffer* gpu_memory_buffer, |
| 56 | gfx::BufferPlane plane, |
| 57 | const gfx::ColorSpace& color_space, |
| 58 | GrSurfaceOrigin surface_origin, |
| 59 | SkAlphaType alpha_type, |
| 60 | uint32_t usage, |
| 61 | gpu::SyncToken& sync_token) override { |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 62 | DoCreateSharedImage(gpu_memory_buffer, plane, color_space, surface_origin, |
| 63 | alpha_type, usage); |
Mingjing Zhang | 38dff4f | 2024-01-08 21:08:26 | [diff] [blame^] | 64 | return base::MakeRefCounted<gpu::ClientSharedImage>( |
| 65 | gpu::Mailbox::GenerateForSharedImage()); |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | MOCK_METHOD2(DoCreateGpuMemoryBuffer, |
| 69 | void(const gfx::Size& size, gfx::BufferFormat format)); |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 70 | MOCK_METHOD7(DoCreateSharedImage, |
| 71 | void(viz::SharedImageFormat format, |
| 72 | const gfx::Size& size, |
| 73 | const gfx::ColorSpace& color_space, |
| 74 | GrSurfaceOrigin surface_origin, |
| 75 | SkAlphaType alpha_type, |
| 76 | uint32_t usage, |
| 77 | gfx::GpuMemoryBufferHandle buffer_handle)); |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 78 | MOCK_METHOD6(DoCreateSharedImage, |
| 79 | void(gfx::GpuMemoryBuffer* gpu_memory_buffer, |
| 80 | gfx::BufferPlane plane, |
| 81 | const gfx::ColorSpace& color_space, |
| 82 | GrSurfaceOrigin surface_origin, |
| 83 | SkAlphaType alpha_type, |
| 84 | uint32_t usage)); |
| 85 | MOCK_METHOD2(DestroySharedImage, |
| 86 | void(const gpu::SyncToken& sync_token, |
Mingjing Zhang | 38dff4f | 2024-01-08 21:08:26 | [diff] [blame^] | 87 | scoped_refptr<gpu::ClientSharedImage> shared_image)); |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 88 | |
| 89 | base::WeakPtr<FakeContext> GetWeakPtr() { return weak_factory_.GetWeakPtr(); } |
| 90 | |
| 91 | private: |
| 92 | base::WeakPtrFactory<FakeContext> weak_factory_; |
| 93 | }; |
| 94 | |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 95 | class RenderableGpuMemoryBufferVideoFramePoolTest |
| 96 | : public testing::TestWithParam<bool> { |
| 97 | public: |
| 98 | RenderableGpuMemoryBufferVideoFramePoolTest() { |
| 99 | if (GetParam()) { |
| 100 | scoped_feature_list_.InitWithFeatures( |
Saifuddin Hitawala | 0fddfcf | 2023-10-06 19:02:12 | [diff] [blame] | 101 | {kUseMultiPlaneFormatForHardwareVideo}, {}); |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 102 | } else { |
| 103 | scoped_feature_list_.InitWithFeatures( |
Saifuddin Hitawala | 0fddfcf | 2023-10-06 19:02:12 | [diff] [blame] | 104 | {}, {kUseMultiPlaneFormatForHardwareVideo}); |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | protected: |
| 109 | void VerifySharedImageCreation(FakeContext* context) { |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 110 | if (GetParam()) { |
| 111 | EXPECT_CALL(*context, DoCreateSharedImage(viz::MultiPlaneFormat::kNV12, _, |
| 112 | _, _, _, _, _)); |
| 113 | return; |
| 114 | } |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 115 | EXPECT_CALL(*context, |
| 116 | DoCreateSharedImage(_, gfx::BufferPlane::Y, _, _, _, _)); |
| 117 | EXPECT_CALL(*context, |
| 118 | DoCreateSharedImage(_, gfx::BufferPlane::UV, _, _, _, _)); |
| 119 | } |
| 120 | |
| 121 | int NumSharedImagesPerFrame() { |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 122 | if (GetParam()) { |
| 123 | return 1; |
| 124 | } |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 125 | return 2; |
| 126 | } |
| 127 | |
| 128 | base::test::ScopedFeatureList scoped_feature_list_; |
| 129 | }; |
| 130 | |
| 131 | TEST_P(RenderableGpuMemoryBufferVideoFramePoolTest, SimpleLifetimes) { |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 132 | base::test::SingleThreadTaskEnvironment task_environment; |
| 133 | const gfx::BufferFormat format = gfx::BufferFormat::YUV_420_BIPLANAR; |
| 134 | const gfx::Size size0(128, 256); |
Christopher Cameron | c59fe51 | 2021-10-19 23:06:54 | [diff] [blame] | 135 | const gfx::ColorSpace color_space0 = gfx::ColorSpace::CreateREC709(); |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 136 | |
| 137 | base::WeakPtr<FakeContext> context; |
| 138 | std::unique_ptr<RenderableGpuMemoryBufferVideoFramePool> pool; |
| 139 | { |
| 140 | auto context_strong = std::make_unique<FakeContext>(); |
| 141 | context = context_strong->GetWeakPtr(); |
| 142 | pool = RenderableGpuMemoryBufferVideoFramePool::Create( |
| 143 | std::move(context_strong)); |
| 144 | } |
| 145 | |
| 146 | // Create a new frame. |
| 147 | EXPECT_CALL(*context, DoCreateGpuMemoryBuffer(size0, format)); |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 148 | VerifySharedImageCreation(context.get()); |
Christopher Cameron | c59fe51 | 2021-10-19 23:06:54 | [diff] [blame] | 149 | auto video_frame0 = pool->MaybeCreateVideoFrame(size0, color_space0); |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 150 | video_frame0 = nullptr; |
| 151 | task_environment.RunUntilIdle(); |
| 152 | |
| 153 | // Expect the frame to be reused. |
| 154 | EXPECT_CALL(*context, DoCreateGpuMemoryBuffer(size0, format)).Times(0); |
| 155 | EXPECT_CALL(*context, DoCreateSharedImage(_, _, _, _, _, _)).Times(0); |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 156 | EXPECT_CALL(*context, DoCreateSharedImage(_, _, _, _, _, _, _)).Times(0); |
Christopher Cameron | c59fe51 | 2021-10-19 23:06:54 | [diff] [blame] | 157 | auto video_frame1 = pool->MaybeCreateVideoFrame(size0, color_space0); |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 158 | |
| 159 | // Expect a new frame to be created. |
| 160 | EXPECT_CALL(*context, DoCreateGpuMemoryBuffer(size0, format)); |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 161 | VerifySharedImageCreation(context.get()); |
Christopher Cameron | c59fe51 | 2021-10-19 23:06:54 | [diff] [blame] | 162 | auto video_frame2 = pool->MaybeCreateVideoFrame(size0, color_space0); |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 163 | |
| 164 | // Expect a new frame to be created. |
| 165 | EXPECT_CALL(*context, DoCreateGpuMemoryBuffer(size0, format)); |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 166 | VerifySharedImageCreation(context.get()); |
Christopher Cameron | c59fe51 | 2021-10-19 23:06:54 | [diff] [blame] | 167 | auto video_frame3 = pool->MaybeCreateVideoFrame(size0, color_space0); |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 168 | |
| 169 | // Freeing two frames will not result in any frames being destroyed, because |
| 170 | // we allow unused 2 frames to exist. |
| 171 | video_frame1 = nullptr; |
| 172 | video_frame2 = nullptr; |
| 173 | task_environment.RunUntilIdle(); |
| 174 | |
| 175 | // Freeing the third frame will result in one of the frames being destroyed. |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 176 | EXPECT_CALL(*context, DestroySharedImage(_, _)) |
| 177 | .Times(NumSharedImagesPerFrame()); |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 178 | video_frame3 = nullptr; |
| 179 | task_environment.RunUntilIdle(); |
| 180 | |
| 181 | // Destroying the pool will result in the remaining two frames being |
| 182 | // destroyed. |
| 183 | EXPECT_TRUE(!!context); |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 184 | EXPECT_CALL(*context, DestroySharedImage(_, _)) |
| 185 | .Times(NumSharedImagesPerFrame() * 2); |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 186 | pool.reset(); |
| 187 | task_environment.RunUntilIdle(); |
| 188 | EXPECT_FALSE(!!context); |
| 189 | } |
| 190 | |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 191 | TEST_P(RenderableGpuMemoryBufferVideoFramePoolTest, FrameFreedAfterPool) { |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 192 | base::test::SingleThreadTaskEnvironment task_environment; |
| 193 | const gfx::BufferFormat format = gfx::BufferFormat::YUV_420_BIPLANAR; |
| 194 | const gfx::Size size0(128, 256); |
Christopher Cameron | c59fe51 | 2021-10-19 23:06:54 | [diff] [blame] | 195 | const gfx::ColorSpace color_space0 = gfx::ColorSpace::CreateREC709(); |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 196 | |
| 197 | base::WeakPtr<FakeContext> context; |
| 198 | std::unique_ptr<RenderableGpuMemoryBufferVideoFramePool> pool; |
| 199 | { |
| 200 | auto context_strong = std::make_unique<FakeContext>(); |
| 201 | context = context_strong->GetWeakPtr(); |
| 202 | pool = RenderableGpuMemoryBufferVideoFramePool::Create( |
| 203 | std::move(context_strong)); |
| 204 | } |
| 205 | |
| 206 | // Create a new frame. |
| 207 | EXPECT_CALL(*context, DoCreateGpuMemoryBuffer(size0, format)); |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 208 | VerifySharedImageCreation(context.get()); |
Christopher Cameron | c59fe51 | 2021-10-19 23:06:54 | [diff] [blame] | 209 | auto video_frame0 = pool->MaybeCreateVideoFrame(size0, color_space0); |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 210 | task_environment.RunUntilIdle(); |
| 211 | |
| 212 | // If the pool is destroyed, but a frame still exists, the context will not |
| 213 | // be destroyed. |
| 214 | pool.reset(); |
| 215 | task_environment.RunUntilIdle(); |
| 216 | EXPECT_TRUE(context); |
| 217 | |
| 218 | // Destroy the frame. Still nothing will happen, because its destruction will |
| 219 | // happen after a posted task is run. |
| 220 | video_frame0 = nullptr; |
| 221 | |
| 222 | // The shared images will be destroyed once the posted task is run. |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 223 | EXPECT_CALL(*context, DestroySharedImage(_, _)) |
| 224 | .Times(NumSharedImagesPerFrame()); |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 225 | task_environment.RunUntilIdle(); |
| 226 | EXPECT_FALSE(!!context); |
| 227 | } |
| 228 | |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 229 | TEST_P(RenderableGpuMemoryBufferVideoFramePoolTest, CrossThread) { |
Ben Wagner | b114256 | 2021-12-15 10:44:12 | [diff] [blame] | 230 | base::test::TaskEnvironment task_environment{ |
| 231 | base::test::TaskEnvironment::TimeSource::MOCK_TIME}; |
| 232 | const gfx::Size size0(128, 256); |
| 233 | const gfx::ColorSpace color_space0 = gfx::ColorSpace::CreateREC709(); |
| 234 | |
| 235 | // Create a pool on the main thread. |
| 236 | auto pool = RenderableGpuMemoryBufferVideoFramePool::Create( |
| 237 | std::make_unique<FakeContext>()); |
| 238 | |
| 239 | base::ThreadPool::CreateSequencedTaskRunner({})->PostTaskAndReplyWithResult( |
| 240 | FROM_HERE, |
| 241 | // Create a frame on another thread. |
| 242 | base::BindLambdaForTesting( |
| 243 | [&]() { return pool->MaybeCreateVideoFrame(size0, color_space0); }), |
| 244 | // Destroy the video frame on the main thread. |
| 245 | base::BindLambdaForTesting( |
| 246 | [&](scoped_refptr<VideoFrame> video_frame0) {})); |
| 247 | task_environment.RunUntilIdle(); |
| 248 | |
| 249 | // Destroy the pool. |
| 250 | pool = nullptr; |
| 251 | task_environment.RunUntilIdle(); |
| 252 | } |
| 253 | |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 254 | TEST_P(RenderableGpuMemoryBufferVideoFramePoolTest, |
| 255 | VideoFramesDestroyedConcurrently) { |
Ben Wagner | be1f047 | 2021-12-15 10:52:08 | [diff] [blame] | 256 | base::test::TaskEnvironment task_environment{ |
| 257 | base::test::TaskEnvironment::TimeSource::MOCK_TIME}; |
| 258 | const gfx::BufferFormat format = gfx::BufferFormat::YUV_420_BIPLANAR; |
| 259 | const gfx::Size size0(128, 256); |
| 260 | const gfx::ColorSpace color_space0 = gfx::ColorSpace::CreateREC709(); |
| 261 | |
| 262 | // Create a pool and several frames on the main thread. |
| 263 | base::WeakPtr<FakeContext> context; |
| 264 | std::unique_ptr<RenderableGpuMemoryBufferVideoFramePool> pool; |
| 265 | { |
| 266 | auto context_strong = std::make_unique<FakeContext>(); |
| 267 | context = context_strong->GetWeakPtr(); |
| 268 | pool = RenderableGpuMemoryBufferVideoFramePool::Create( |
| 269 | std::move(context_strong)); |
| 270 | } |
| 271 | |
| 272 | std::vector<scoped_refptr<VideoFrame>> frames; |
| 273 | static constexpr int kNumFrames = 3; |
| 274 | for (int i = 0; i < kNumFrames; i++) { |
| 275 | EXPECT_CALL(*context, DoCreateGpuMemoryBuffer(size0, format)); |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 276 | VerifySharedImageCreation(context.get()); |
Ben Wagner | be1f047 | 2021-12-15 10:52:08 | [diff] [blame] | 277 | frames.emplace_back(pool->MaybeCreateVideoFrame(size0, color_space0)); |
| 278 | } |
| 279 | task_environment.RunUntilIdle(); |
| 280 | |
| 281 | // Expect all frames to be destroyed eventually. |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 282 | EXPECT_CALL(*context, DestroySharedImage(_, _)) |
| 283 | .Times(kNumFrames * NumSharedImagesPerFrame()); |
Ben Wagner | be1f047 | 2021-12-15 10:52:08 | [diff] [blame] | 284 | |
| 285 | // Destroy frames on separate threads. TSAN will tell us if there's a problem. |
| 286 | for (int i = 0; i < kNumFrames; i++) { |
| 287 | base::ThreadPool::CreateSequencedTaskRunner({})->PostTask( |
Paul Semel | 4c1e3e8 | 2022-10-24 09:20:54 | [diff] [blame] | 288 | FROM_HERE, base::DoNothingWithBoundArgs(std::move(frames[i]))); |
Ben Wagner | be1f047 | 2021-12-15 10:52:08 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | pool.reset(); |
| 292 | task_environment.RunUntilIdle(); |
| 293 | EXPECT_FALSE(!!context); |
| 294 | } |
| 295 | |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 296 | TEST_P(RenderableGpuMemoryBufferVideoFramePoolTest, ConcurrentCreateDestroy) { |
Ben Wagner | be1f047 | 2021-12-15 10:52:08 | [diff] [blame] | 297 | base::test::TaskEnvironment task_environment{ |
| 298 | base::test::TaskEnvironment::TimeSource::MOCK_TIME}; |
| 299 | const gfx::Size size0(128, 256); |
| 300 | const gfx::ColorSpace color_space0 = gfx::ColorSpace::CreateREC709(); |
| 301 | |
| 302 | // Create a pool on the main thread. |
| 303 | auto pool = RenderableGpuMemoryBufferVideoFramePool::Create( |
| 304 | std::make_unique<FakeContext>()); |
| 305 | |
| 306 | // Create a frame on the main thread. |
| 307 | auto video_frame0 = pool->MaybeCreateVideoFrame(size0, color_space0); |
| 308 | task_environment.RunUntilIdle(); |
| 309 | |
| 310 | // Destroy the frame on another thread. TSAN will tell us if there's a |
| 311 | // problem. |
| 312 | base::ThreadPool::CreateSequencedTaskRunner({})->PostTask( |
Paul Semel | 4c1e3e8 | 2022-10-24 09:20:54 | [diff] [blame] | 313 | FROM_HERE, base::DoNothingWithBoundArgs(std::move(video_frame0))); |
Ben Wagner | be1f047 | 2021-12-15 10:52:08 | [diff] [blame] | 314 | |
| 315 | // Create another frame on the main thread. |
| 316 | auto video_frame1 = pool->MaybeCreateVideoFrame(size0, color_space0); |
| 317 | task_environment.RunUntilIdle(); |
| 318 | |
| 319 | video_frame1 = nullptr; |
| 320 | pool.reset(); |
| 321 | task_environment.RunUntilIdle(); |
| 322 | } |
| 323 | |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 324 | TEST_P(RenderableGpuMemoryBufferVideoFramePoolTest, RespectSizeAndColorSpace) { |
Christopher Cameron | c59fe51 | 2021-10-19 23:06:54 | [diff] [blame] | 325 | base::test::SingleThreadTaskEnvironment task_environment; |
| 326 | const gfx::BufferFormat format = gfx::BufferFormat::YUV_420_BIPLANAR; |
| 327 | const gfx::Size size0(128, 256); |
| 328 | const gfx::ColorSpace color_space0 = gfx::ColorSpace::CreateREC709(); |
| 329 | const gfx::Size size1(256, 256); |
| 330 | const gfx::ColorSpace color_space1 = gfx::ColorSpace::CreateREC601(); |
| 331 | |
| 332 | base::WeakPtr<FakeContext> context; |
| 333 | std::unique_ptr<RenderableGpuMemoryBufferVideoFramePool> pool; |
| 334 | { |
| 335 | auto context_strong = std::make_unique<FakeContext>(); |
| 336 | context = context_strong->GetWeakPtr(); |
| 337 | pool = RenderableGpuMemoryBufferVideoFramePool::Create( |
| 338 | std::move(context_strong)); |
| 339 | } |
| 340 | |
| 341 | // Create a new frame. |
| 342 | EXPECT_CALL(*context, DoCreateGpuMemoryBuffer(size0, format)).Times(1); |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 343 | VerifySharedImageCreation(context.get()); |
Christopher Cameron | c59fe51 | 2021-10-19 23:06:54 | [diff] [blame] | 344 | auto video_frame0 = pool->MaybeCreateVideoFrame(size0, color_space0); |
| 345 | video_frame0 = nullptr; |
| 346 | task_environment.RunUntilIdle(); |
| 347 | |
| 348 | // Expect the frame to be reused. |
| 349 | EXPECT_CALL(*context, DoCreateGpuMemoryBuffer(_, _)).Times(0); |
| 350 | EXPECT_CALL(*context, DoCreateSharedImage(_, _, _, _, _, _)).Times(0); |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 351 | EXPECT_CALL(*context, DoCreateSharedImage(_, _, _, _, _, _, _)).Times(0); |
Christopher Cameron | c59fe51 | 2021-10-19 23:06:54 | [diff] [blame] | 352 | video_frame0 = pool->MaybeCreateVideoFrame(size0, color_space0); |
| 353 | video_frame0 = nullptr; |
| 354 | task_environment.RunUntilIdle(); |
| 355 | |
| 356 | // Change the size, expect a new frame to be created (and the previous frame |
| 357 | // to be destroyed). |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 358 | EXPECT_CALL(*context, DestroySharedImage(_, _)) |
| 359 | .Times(NumSharedImagesPerFrame()); |
Christopher Cameron | c59fe51 | 2021-10-19 23:06:54 | [diff] [blame] | 360 | EXPECT_CALL(*context, DoCreateGpuMemoryBuffer(size1, format)); |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 361 | VerifySharedImageCreation(context.get()); |
Christopher Cameron | c59fe51 | 2021-10-19 23:06:54 | [diff] [blame] | 362 | video_frame0 = pool->MaybeCreateVideoFrame(size1, color_space0); |
| 363 | video_frame0 = nullptr; |
| 364 | task_environment.RunUntilIdle(); |
| 365 | |
| 366 | // Expect that frame to be reused. |
| 367 | EXPECT_CALL(*context, DoCreateGpuMemoryBuffer(_, _)).Times(0); |
| 368 | EXPECT_CALL(*context, DoCreateSharedImage(_, _, _, _, _, _)).Times(0); |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 369 | EXPECT_CALL(*context, DoCreateSharedImage(_, _, _, _, _, _, _)).Times(0); |
Christopher Cameron | c59fe51 | 2021-10-19 23:06:54 | [diff] [blame] | 370 | video_frame0 = pool->MaybeCreateVideoFrame(size1, color_space0); |
| 371 | video_frame0 = nullptr; |
| 372 | task_environment.RunUntilIdle(); |
| 373 | |
| 374 | // Change the color space, expect a new frame to be created (and the previous |
| 375 | // frame to be destroyed). |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 376 | EXPECT_CALL(*context, DestroySharedImage(_, _)) |
| 377 | .Times(NumSharedImagesPerFrame()); |
Christopher Cameron | c59fe51 | 2021-10-19 23:06:54 | [diff] [blame] | 378 | EXPECT_CALL(*context, DoCreateGpuMemoryBuffer(size1, format)); |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 379 | VerifySharedImageCreation(context.get()); |
Christopher Cameron | c59fe51 | 2021-10-19 23:06:54 | [diff] [blame] | 380 | video_frame0 = pool->MaybeCreateVideoFrame(size1, color_space1); |
| 381 | video_frame0 = nullptr; |
| 382 | task_environment.RunUntilIdle(); |
| 383 | |
| 384 | // Expect that frame to be reused. |
| 385 | EXPECT_CALL(*context, DoCreateGpuMemoryBuffer(_, _)).Times(0); |
| 386 | EXPECT_CALL(*context, DoCreateSharedImage(_, _, _, _, _, _)).Times(0); |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 387 | EXPECT_CALL(*context, DoCreateSharedImage(_, _, _, _, _, _, _)).Times(0); |
Christopher Cameron | c59fe51 | 2021-10-19 23:06:54 | [diff] [blame] | 388 | video_frame0 = pool->MaybeCreateVideoFrame(size1, color_space1); |
| 389 | video_frame0 = nullptr; |
| 390 | task_environment.RunUntilIdle(); |
| 391 | |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 392 | EXPECT_CALL(*context, DestroySharedImage(_, _)) |
| 393 | .Times(NumSharedImagesPerFrame()); |
Christopher Cameron | c59fe51 | 2021-10-19 23:06:54 | [diff] [blame] | 394 | pool.reset(); |
| 395 | task_environment.RunUntilIdle(); |
| 396 | EXPECT_FALSE(!!context); |
| 397 | } |
| 398 | |
Colin Blundell | e62aefe | 2023-06-09 12:32:48 | [diff] [blame] | 399 | INSTANTIATE_TEST_SUITE_P(All, |
| 400 | RenderableGpuMemoryBufferVideoFramePoolTest, |
| 401 | testing::Bool()); |
| 402 | |
Christopher Cameron | 746d2cd | 2021-06-25 04:42:13 | [diff] [blame] | 403 | } // namespace |
| 404 | |
| 405 | } // namespace media |