1 // Copyright 2018 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.
4
5 #include "media/gpu/test/fake_command_buffer_helper.h"
6 #include "gpu/command_buffer/service/shared_image_backing.h"
7 #include "gpu/command_buffer/service/shared_image_representation.h"
8
9 #include "base/logging.h"
10
11 namespace media {
12
FakeCommandBufferHelper(scoped_refptr<base::SingleThreadTaskRunner> task_runner)13 FakeCommandBufferHelper::FakeCommandBufferHelper(
14 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
15 : CommandBufferHelper(task_runner), task_runner_(std::move(task_runner)) {
16 DVLOG(1) << __func__;
17 }
18
~FakeCommandBufferHelper()19 FakeCommandBufferHelper::~FakeCommandBufferHelper() {
20 DVLOG(1) << __func__;
21 }
22
StubLost()23 void FakeCommandBufferHelper::StubLost() {
24 DVLOG(1) << __func__;
25 DCHECK(task_runner_->BelongsToCurrentThread());
26 // Keep a reference to |this| in case the destruction cb drops the last one.
27 scoped_refptr<CommandBufferHelper> thiz(this);
28 if (will_destroy_stub_cb_)
29 std::move(will_destroy_stub_cb_).Run(!is_context_lost_);
30 has_stub_ = false;
31 is_context_lost_ = true;
32 is_context_current_ = false;
33 waits_.clear();
34 }
35
ContextLost()36 void FakeCommandBufferHelper::ContextLost() {
37 DVLOG(1) << __func__;
38 DCHECK(task_runner_->BelongsToCurrentThread());
39 is_context_lost_ = true;
40 is_context_current_ = false;
41 }
42
CurrentContextLost()43 void FakeCommandBufferHelper::CurrentContextLost() {
44 DVLOG(2) << __func__;
45 DCHECK(task_runner_->BelongsToCurrentThread());
46 is_context_current_ = false;
47 }
48
HasTexture(GLuint service_id)49 bool FakeCommandBufferHelper::HasTexture(GLuint service_id) {
50 DVLOG(4) << __func__ << "(" << service_id << ")";
51 DCHECK(task_runner_->BelongsToCurrentThread());
52 return service_ids_.count(service_id);
53 }
54
ReleaseSyncToken(gpu::SyncToken sync_token)55 void FakeCommandBufferHelper::ReleaseSyncToken(gpu::SyncToken sync_token) {
56 DVLOG(3) << __func__;
57 DCHECK(task_runner_->BelongsToCurrentThread());
58 DCHECK(waits_.count(sync_token));
59 task_runner_->PostTask(FROM_HERE, std::move(waits_[sync_token]));
60 waits_.erase(sync_token);
61 }
62
GetGLContext()63 gl::GLContext* FakeCommandBufferHelper::GetGLContext() {
64 DVLOG(4) << __func__;
65 DCHECK(task_runner_->BelongsToCurrentThread());
66 return nullptr;
67 }
68
GetSharedImageStub()69 gpu::SharedImageStub* FakeCommandBufferHelper::GetSharedImageStub() {
70 return nullptr;
71 }
72
HasStub()73 bool FakeCommandBufferHelper::HasStub() {
74 return has_stub_;
75 }
76
MakeContextCurrent()77 bool FakeCommandBufferHelper::MakeContextCurrent() {
78 DVLOG(3) << __func__;
79 DCHECK(task_runner_->BelongsToCurrentThread());
80 is_context_current_ = !is_context_lost_;
81 return is_context_current_;
82 }
83
84 std::unique_ptr<gpu::SharedImageRepresentationFactoryRef>
Register(std::unique_ptr<gpu::SharedImageBacking> backing)85 FakeCommandBufferHelper::Register(
86 std::unique_ptr<gpu::SharedImageBacking> backing) {
87 DVLOG(2) << __func__;
88 DCHECK(task_runner_->BelongsToCurrentThread());
89 return nullptr;
90 }
91
GetTexture(GLuint service_id) const92 gpu::TextureBase* FakeCommandBufferHelper::GetTexture(GLuint service_id) const {
93 DVLOG(2) << __func__ << "(" << service_id << ")";
94 DCHECK(task_runner_->BelongsToCurrentThread());
95 DCHECK(service_ids_.count(service_id));
96 return nullptr;
97 }
98
CreateTexture(GLenum target,GLenum internal_format,GLsizei width,GLsizei height,GLenum format,GLenum type)99 GLuint FakeCommandBufferHelper::CreateTexture(GLenum target,
100 GLenum internal_format,
101 GLsizei width,
102 GLsizei height,
103 GLenum format,
104 GLenum type) {
105 DVLOG(2) << __func__;
106 DCHECK(task_runner_->BelongsToCurrentThread());
107 DCHECK(is_context_current_);
108 GLuint service_id = next_service_id_++;
109 service_ids_.insert(service_id);
110 return service_id;
111 }
112
DestroyTexture(GLuint service_id)113 void FakeCommandBufferHelper::DestroyTexture(GLuint service_id) {
114 DVLOG(2) << __func__ << "(" << service_id << ")";
115 DCHECK(task_runner_->BelongsToCurrentThread());
116 DCHECK(is_context_current_);
117 DCHECK(service_ids_.count(service_id));
118 service_ids_.erase(service_id);
119 }
120
SetCleared(GLuint service_id)121 void FakeCommandBufferHelper::SetCleared(GLuint service_id) {
122 DVLOG(2) << __func__;
123 DCHECK(task_runner_->BelongsToCurrentThread());
124 DCHECK(service_ids_.count(service_id));
125 }
126
BindImage(GLuint service_id,gl::GLImage * image,bool client_managed)127 bool FakeCommandBufferHelper::BindImage(GLuint service_id,
128 gl::GLImage* image,
129 bool client_managed) {
130 DVLOG(2) << __func__ << "(" << service_id << ")";
131 DCHECK(task_runner_->BelongsToCurrentThread());
132 DCHECK(service_ids_.count(service_id));
133 return has_stub_;
134 }
135
CreateMailbox(GLuint service_id)136 gpu::Mailbox FakeCommandBufferHelper::CreateMailbox(GLuint service_id) {
137 DVLOG(2) << __func__ << "(" << service_id << ")";
138 DCHECK(task_runner_->BelongsToCurrentThread());
139 DCHECK(service_ids_.count(service_id));
140 if (!has_stub_)
141 return gpu::Mailbox();
142 return gpu::Mailbox::Generate();
143 }
144
ProduceTexture(const gpu::Mailbox & mailbox,GLuint service_id)145 void FakeCommandBufferHelper::ProduceTexture(const gpu::Mailbox& mailbox,
146 GLuint service_id) {
147 DVLOG(2) << __func__ << "(" << service_id << ")";
148 DCHECK(task_runner_->BelongsToCurrentThread());
149 DCHECK(service_ids_.count(service_id));
150 }
151
WaitForSyncToken(gpu::SyncToken sync_token,base::OnceClosure done_cb)152 void FakeCommandBufferHelper::WaitForSyncToken(gpu::SyncToken sync_token,
153 base::OnceClosure done_cb) {
154 DVLOG(2) << __func__;
155 DCHECK(task_runner_->BelongsToCurrentThread());
156 DCHECK(!waits_.count(sync_token));
157 if (has_stub_)
158 waits_.emplace(sync_token, std::move(done_cb));
159 }
160
SetWillDestroyStubCB(WillDestroyStubCB will_destroy_stub_cb)161 void FakeCommandBufferHelper::SetWillDestroyStubCB(
162 WillDestroyStubCB will_destroy_stub_cb) {
163 DCHECK(!will_destroy_stub_cb_);
164 will_destroy_stub_cb_ = std::move(will_destroy_stub_cb);
165 }
166
IsPassthrough() const167 bool FakeCommandBufferHelper::IsPassthrough() const {
168 return false;
169 }
170
SupportsTextureRectangle() const171 bool FakeCommandBufferHelper::SupportsTextureRectangle() const {
172 return false;
173 }
174
175 } // namespace media
176