1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
media / gpu / android / codec_image_unittest.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "gpu/command_buffer/service/mock_texture_owner.h"
#include "gpu/command_buffer/service/ref_counted_lock_for_test.h"
#include "gpu/command_buffer/service/texture_manager.h"
#include "gpu/config/gpu_finch_features.h"
#include "media/base/android/media_codec_bridge.h"
#include "media/base/android/mock_media_codec_bridge.h"
#include "media/gpu/android/codec_image.h"
#include "media/gpu/android/mock_codec_buffer_wait_coordinator.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_context_egl.h"
#include "ui/gl/gl_implementation.h"
#include "ui/gl/gl_share_group.h"
#include "ui/gl/gl_surface_egl.h"
#include "ui/gl/init/gl_factory.h"
using testing::_;
using testing::Eq;
using testing::InSequence;
using testing::Invoke;
using testing::NiceMock;
using testing::Return;
namespace media {
constexpr gfx::Size kFrameSize(640, 480);
class CodecImageTest : public testing::Test {
public:
CodecImageTest() = default;
void SetUp() override {
auto codec = std::make_unique<NiceMock<MockMediaCodecBridge>>();
codec_ = codec.get();
wrapper_ = std::make_unique<CodecWrapper>(
CodecSurfacePair(std::move(codec), new CodecSurfaceBundle()),
base::DoNothing(), kFrameSize, gfx::ColorSpace::CreateREC709(),
std::nullopt);
ON_CALL(*codec_, DequeueOutputBuffer(_, _, _, _, _, _, _))
.WillByDefault(Return(OkStatus()));
gl::init::InitializeStaticGLBindingsImplementation(
gl::GLImplementationParts(gl::kGLImplementationEGLGLES2));
display_ = gl::init::InitializeGLOneOffPlatformImplementation(
/*disable_gl_drawing=*/false,
/*init_extensions=*/false,
/*gpu_preference=*/gl::GpuPreference::kDefault);
scoped_refptr<gl::GLSurface> surface(new gl::PbufferGLSurfaceEGL(
gl::GLSurfaceEGL::GetGLDisplayEGL(), gfx::Size(320, 240)));
surface->Initialize();
share_group_ = new gl::GLShareGroup();
context_ = new gl::GLContextEGL(share_group_.get());
context_->Initialize(surface.get(), gl::GLContextAttribs());
ASSERT_TRUE(context_->default_surface());
ASSERT_TRUE(context_->MakeCurrentDefault());
glGenTextures(1, &texture_id_);
// The tests rely on this texture being bound.
glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_id_);
auto texture_owner = base::MakeRefCounted<NiceMock<gpu::MockTextureOwner>>(
texture_id_, context_.get(), context_->default_surface(),
BindsTextureOnUpdate());
ON_CALL(*texture_owner, GetCodedSizeAndVisibleRect(_, _, _))
.WillByDefault(
Invoke([](gfx::Size rotated_visible_size, gfx::Size* coded_size,
gfx::Rect* visible_rect) {
*visible_rect = gfx::Rect(rotated_visible_size);
*coded_size = visible_rect->size();
return true;
}));
codec_buffer_wait_coordinator_ =
base::MakeRefCounted<NiceMock<MockCodecBufferWaitCoordinator>>(
std::move(texture_owner));
}
void TearDown() override {
if (texture_id_ && context_->MakeCurrentDefault()) {
glDeleteTextures(1, &texture_id_);
}
context_ = nullptr;
share_group_ = nullptr;
gl::init::ShutdownGL(display_, false);
wrapper_->TakeCodecSurfacePair();
}
enum ImageKind { kOverlay, kTextureOwner };
scoped_refptr<CodecImage> NewImage(
ImageKind kind,
CodecImage::UnusedCB unused_cb = base::DoNothing()) {
std::unique_ptr<CodecOutputBuffer> buffer;
wrapper_->DequeueOutputBuffer(nullptr, nullptr, &buffer);
auto codec_buffer_wait_coordinator =
kind == kTextureOwner ? codec_buffer_wait_coordinator_ : nullptr;
auto buffer_renderer = std::make_unique<CodecOutputBufferRenderer>(
std::move(buffer), codec_buffer_wait_coordinator,
features::NeedThreadSafeAndroidMedia()
? base::MakeRefCounted<gpu::RefCountedLockForTest>()
: nullptr);
buffer_renderer->set_frame_info_callback(base::BindOnce(
&CodecImageTest::OnFrameInfoReady, base::Unretained(this)));
scoped_refptr<CodecImage> image =
new CodecImage(buffer_renderer->size(),
features::NeedThreadSafeAndroidMedia()
? base::MakeRefCounted<gpu::RefCountedLockForTest>()
: nullptr);
image->Initialize(
std::move(buffer_renderer), kind == kTextureOwner,
base::BindRepeating(&PromotionHintReceiver::OnPromotionHint,
base::Unretained(&promotion_hint_receiver_)));
image->AddUnusedCB(std::move(unused_cb));
return image;
}
MOCK_METHOD(void,
OnFrameInfoReady,
(std::optional<gfx::Size> coded_size,
std::optional<gfx::Rect> visible_rect),
());
virtual bool BindsTextureOnUpdate() { return true; }
base::test::TaskEnvironment task_environment_;
raw_ptr<NiceMock<MockMediaCodecBridge>> codec_;
std::unique_ptr<CodecWrapper> wrapper_;
scoped_refptr<NiceMock<MockCodecBufferWaitCoordinator>>
codec_buffer_wait_coordinator_;
scoped_refptr<gl::GLContext> context_;
scoped_refptr<gl::GLShareGroup> share_group_;
GLuint texture_id_ = 0;
raw_ptr<gl::GLDisplay> display_ = nullptr;
class PromotionHintReceiver {
public:
MOCK_METHOD1(OnPromotionHint, void(PromotionHintAggregator::Hint));
};
PromotionHintReceiver promotion_hint_receiver_;
};
class CodecImageTestExplicitBind : public CodecImageTest {
bool BindsTextureOnUpdate() override { return false; }
};
TEST_F(CodecImageTest, UnusedCBRunsOnDestruction) {
// Add multiple UnusedCBs and verify that they are all run when the CodecImage
// is destroyed.
base::MockCallback<CodecImage::UnusedCB> cb_1;
base::MockCallback<CodecImage::UnusedCB> cb_2;
auto i = NewImage(kOverlay);
i->AddUnusedCB(cb_1.Get());
i->AddUnusedCB(cb_2.Get());
EXPECT_CALL(cb_1, Run(i.get()));
EXPECT_CALL(cb_2, Run(i.get()));
EXPECT_CALL(*this, OnFrameInfoReady(Eq(std::nullopt), Eq(std::nullopt)));
i = nullptr;
}
TEST_F(CodecImageTest, UnusedCBRunsOnNotifyUnused) {
base::MockCallback<CodecImage::UnusedCB> cb_1;
base::MockCallback<CodecImage::UnusedCB> cb_2;
auto i = NewImage(kTextureOwner);
ASSERT_TRUE(i->get_codec_output_buffer_for_testing());
ASSERT_TRUE(i->HasTextureOwner());
i->AddUnusedCB(cb_1.Get());
i->AddUnusedCB(cb_2.Get());
EXPECT_CALL(cb_1, Run(i.get()));
EXPECT_CALL(cb_2, Run(i.get()));
EXPECT_CALL(*this, OnFrameInfoReady(Eq(std::nullopt), Eq(std::nullopt)));
// Also verify that the output buffer and texture owner are released.
i->NotifyUnused();
EXPECT_FALSE(i->get_codec_output_buffer_for_testing());
EXPECT_FALSE(i->HasTextureOwner());
// Verify that an additional call doesn't crash. It should do nothing.
i->NotifyUnused();
}
TEST_F(CodecImageTest, ImageStartsUnrendered) {
auto i = NewImage(kTextureOwner);
ASSERT_FALSE(i->was_rendered_to_front_buffer());
EXPECT_CALL(*this, OnFrameInfoReady(Eq(std::nullopt), Eq(std::nullopt)));
}
TEST_F(CodecImageTest, CanRenderTextureOwnerImageToBackBuffer) {
auto i = NewImage(kTextureOwner);
ASSERT_TRUE(i->RenderToTextureOwnerBackBuffer());
ASSERT_FALSE(i->was_rendered_to_front_buffer());
EXPECT_CALL(*this, OnFrameInfoReady(Eq(std::nullopt), Eq(std::nullopt)));
}
TEST_F(CodecImageTest, CodecBufferInvalidationResultsInRenderingFailure) {
auto i = NewImage(kTextureOwner);
// Invalidate the backing codec buffer.
wrapper_->TakeCodecSurfacePair();
EXPECT_CALL(*this, OnFrameInfoReady(Eq(std::nullopt), Eq(std::nullopt)));
ASSERT_FALSE(i->RenderToTextureOwnerBackBuffer());
}
TEST_F(CodecImageTest, RenderToBackBufferDoesntWait) {
auto i = NewImage(kTextureOwner);
InSequence s;
EXPECT_CALL(*codec_, ReleaseOutputBuffer(_, true));
EXPECT_CALL(*codec_buffer_wait_coordinator_, SetReleaseTimeToNow());
EXPECT_CALL(*codec_buffer_wait_coordinator_, WaitForFrameAvailable())
.Times(0);
ASSERT_TRUE(i->RenderToTextureOwnerBackBuffer());
EXPECT_CALL(*this, OnFrameInfoReady(Eq(std::nullopt), Eq(std::nullopt)));
}
TEST_F(CodecImageTest, PromotingTheBackBufferWaits) {
auto i = NewImage(kTextureOwner);
EXPECT_CALL(*codec_buffer_wait_coordinator_, SetReleaseTimeToNow()).Times(1);
i->RenderToTextureOwnerBackBuffer();
EXPECT_CALL(*codec_buffer_wait_coordinator_, WaitForFrameAvailable());
EXPECT_CALL(*this,
OnFrameInfoReady(Eq(kFrameSize), Eq(gfx::Rect(kFrameSize))));
ASSERT_TRUE(i->RenderToFrontBuffer());
}
TEST_F(CodecImageTest, PromotingTheBackBufferAlwaysSucceeds) {
auto i = NewImage(kTextureOwner);
i->RenderToTextureOwnerBackBuffer();
// Invalidating the codec buffer doesn't matter after it's rendered to the
// back buffer.
wrapper_->TakeCodecSurfacePair();
EXPECT_CALL(*this,
OnFrameInfoReady(Eq(kFrameSize), Eq(gfx::Rect(kFrameSize))));
ASSERT_TRUE(i->RenderToFrontBuffer());
}
TEST_F(CodecImageTest, FrontBufferRenderingFailsIfBackBufferRenderingFailed) {
auto i = NewImage(kTextureOwner);
wrapper_->TakeCodecSurfacePair();
EXPECT_CALL(*this, OnFrameInfoReady(Eq(std::nullopt), Eq(std::nullopt)));
i->RenderToTextureOwnerBackBuffer();
ASSERT_FALSE(i->RenderToFrontBuffer());
}
TEST_F(CodecImageTest, RenderToFrontBufferRestoresTextureBindings) {
GLuint pre_bound_texture = 0;
glGenTextures(1, &pre_bound_texture);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, pre_bound_texture);
auto i = NewImage(kTextureOwner);
EXPECT_CALL(*codec_buffer_wait_coordinator_->texture_owner(),
UpdateTexImage());
EXPECT_CALL(*this,
OnFrameInfoReady(Eq(kFrameSize), Eq(gfx::Rect(kFrameSize))));
i->RenderToFrontBuffer();
GLint post_bound_texture = 0;
glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &post_bound_texture);
ASSERT_EQ(pre_bound_texture, static_cast<GLuint>(post_bound_texture));
}
TEST_F(CodecImageTestExplicitBind, RenderToFrontBufferDoesNotBindTexture) {
GLuint pre_bound_texture = 0;
glGenTextures(1, &pre_bound_texture);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, pre_bound_texture);
auto i = NewImage(kTextureOwner);
EXPECT_CALL(*codec_buffer_wait_coordinator_->texture_owner(),
UpdateTexImage());
EXPECT_CALL(*this,
OnFrameInfoReady(Eq(kFrameSize), Eq(gfx::Rect(kFrameSize))));
i->RenderToFrontBuffer();
GLint post_bound_texture = 0;
glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &post_bound_texture);
ASSERT_EQ(pre_bound_texture, static_cast<GLuint>(post_bound_texture));
}
TEST_F(CodecImageTest, RenderToFrontBufferRestoresGLContext) {
// Make a new context current.
scoped_refptr<gl::GLSurface> surface(new gl::PbufferGLSurfaceEGL(
gl::GLSurfaceEGL::GetGLDisplayEGL(), gfx::Size(320, 240)));
surface->Initialize();
scoped_refptr<gl::GLShareGroup> share_group(new gl::GLShareGroup());
scoped_refptr<gl::GLContext> context(new gl::GLContextEGL(share_group.get()));
context->Initialize(surface.get(), gl::GLContextAttribs());
ASSERT_TRUE(context->default_surface());
ASSERT_TRUE(context->MakeCurrentDefault());
auto i = NewImage(kTextureOwner);
// UpdateTexImage sets it's own context.
EXPECT_CALL(*codec_buffer_wait_coordinator_->texture_owner(),
UpdateTexImage());
EXPECT_CALL(*this,
OnFrameInfoReady(Eq(kFrameSize), Eq(gfx::Rect(kFrameSize))));
i->RenderToFrontBuffer();
// Our context should have been restored.
ASSERT_TRUE(context->IsCurrent(surface.get()));
context = nullptr;
share_group = nullptr;
}
TEST_F(CodecImageTest, GetAHardwareBuffer) {
auto i = NewImage(kTextureOwner);
EXPECT_EQ(codec_buffer_wait_coordinator_->texture_owner()
->get_a_hardware_buffer_count,
0);
EXPECT_FALSE(i->was_rendered_to_front_buffer());
EXPECT_CALL(*codec_buffer_wait_coordinator_->texture_owner(),
UpdateTexImage());
EXPECT_CALL(*this,
OnFrameInfoReady(Eq(kFrameSize), Eq(gfx::Rect(kFrameSize))));
i->GetAHardwareBuffer();
EXPECT_EQ(codec_buffer_wait_coordinator_->texture_owner()
->get_a_hardware_buffer_count,
1);
EXPECT_TRUE(i->was_rendered_to_front_buffer());
}
TEST_F(CodecImageTest, GetAHardwareBufferAfterRelease) {
// Make sure that we get a nullptr AHB once we've marked the image as unused.
auto i = NewImage(kTextureOwner);
EXPECT_CALL(*this, OnFrameInfoReady(Eq(std::nullopt), Eq(std::nullopt)));
i->NotifyUnused();
EXPECT_FALSE(i->GetAHardwareBuffer());
}
TEST_F(CodecImageTest, RenderAfterUnusedDoesntCrash) {
auto i = NewImage(kTextureOwner);
EXPECT_CALL(*this, OnFrameInfoReady(Eq(std::nullopt), Eq(std::nullopt)));
i->NotifyUnused();
EXPECT_FALSE(i->RenderToTextureOwnerBackBuffer());
EXPECT_FALSE(i->RenderToTextureOwnerFrontBuffer());
}
} // namespace media