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