OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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/command_buffer_helper.h" | |
6 | |
7 #include "base/macros.h" | |
8 #include "base/memory/ptr_util.h" | |
9 #include "base/threading/thread_checker.h" | |
10 #include "gpu/command_buffer/common/mailbox.h" | |
11 #include "gpu/command_buffer/common/sync_token.h" | |
12 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" | |
13 #include "gpu/command_buffer/service/mailbox_manager.h" | |
14 #include "gpu/command_buffer/service/texture_manager.h" | |
15 #include "gpu/ipc/service/gpu_command_buffer_stub.h" | |
16 | |
17 namespace media { | |
18 | |
19 class CommandBufferHelperImpl | |
20 : public CommandBufferHelper, | |
21 private gpu::GpuCommandBufferStub::DestructionObserver { | |
22 public: | |
23 explicit CommandBufferHelperImpl(gpu::GpuCommandBufferStub* stub) | |
24 : stub_(stub) { | |
25 stub_->AddDestructionObserver(this); | |
26 } | |
27 | |
28 ~CommandBufferHelperImpl() override { | |
29 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
30 if (stub_) | |
31 stub_->RemoveDestructionObserver(this); | |
32 } | |
33 | |
34 bool MakeContextCurrent() override { | |
35 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
36 if (!stub_) | |
37 return false; | |
38 gpu::gles2::GLES2Decoder* decoder = stub_->decoder(); | |
39 if (!decoder) | |
40 return false; | |
41 return decoder->MakeCurrent(); | |
42 } | |
43 | |
44 scoped_refptr<gpu::gles2::TextureRef> CreateTexture(GLenum target, | |
45 GLenum internal_format, | |
46 GLsizei width, | |
47 GLsizei height, | |
48 GLenum format, | |
49 GLenum type) override { | |
50 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
51 if (!stub_) | |
52 return nullptr; | |
53 gpu::gles2::GLES2Decoder* decoder = stub_->decoder(); | |
54 if (!decoder) | |
55 return nullptr; | |
56 gpu::gles2::ContextGroup* group = decoder->GetContextGroup(); | |
57 if (!group) | |
58 return nullptr; | |
59 gpu::gles2::TextureManager* texture_manager = group->texture_manager(); | |
60 if (!texture_manager) | |
61 return nullptr; | |
62 | |
63 // We can't use texture_manager->CreateTexture(), since it requires a unique | |
64 // |client_id|. Instead we create the texture directly, and create our own | |
65 // TextureRef for it. | |
66 // TODO(sandersd): Perhaps it was a mistake to share the command buffer? | |
67 GLuint texture_id; | |
68 glGenTextures(1, &texture_id); | |
69 glBindTexture(target, texture_id); | |
70 | |
71 scoped_refptr<gpu::gles2::TextureRef> texture_ref = | |
72 gpu::gles2::TextureRef::Create(texture_manager, 0, texture_id); | |
73 texture_manager->SetTarget(texture_ref.get(), target); | |
74 texture_manager->SetLevelInfo(texture_ref.get(), // ref | |
75 target, // target | |
76 0, // level | |
77 internal_format, // internal_format | |
78 width, // width | |
79 height, // height | |
80 1, // depth | |
81 0, // border | |
82 format, // format | |
83 type, // type | |
84 gfx::Rect()); // cleared_rect | |
85 | |
86 texture_manager->SetParameteri(__func__, decoder->GetErrorState(), | |
87 texture_ref.get(), GL_TEXTURE_MAG_FILTER, | |
88 GL_LINEAR); | |
89 texture_manager->SetParameteri(__func__, decoder->GetErrorState(), | |
90 texture_ref.get(), GL_TEXTURE_MIN_FILTER, | |
91 GL_LINEAR); | |
92 | |
93 texture_manager->SetParameteri(__func__, decoder->GetErrorState(), | |
94 texture_ref.get(), GL_TEXTURE_WRAP_S, | |
95 GL_CLAMP_TO_EDGE); | |
96 texture_manager->SetParameteri(__func__, decoder->GetErrorState(), | |
97 texture_ref.get(), GL_TEXTURE_WRAP_T, | |
98 GL_CLAMP_TO_EDGE); | |
99 | |
100 texture_manager->SetParameteri(__func__, decoder->GetErrorState(), | |
101 texture_ref.get(), GL_TEXTURE_BASE_LEVEL, 0); | |
102 texture_manager->SetParameteri(__func__, decoder->GetErrorState(), | |
103 texture_ref.get(), GL_TEXTURE_MAX_LEVEL, 0); | |
104 | |
105 // TODO(sandersd): Do we always want to allocate for GL_TEXTURE_2D? What | |
106 // about GL_TEXTURE_RECTANGLE? | |
107 if (target == GL_TEXTURE_2D) { | |
108 glTexImage2D(target, // target | |
109 0, // level | |
110 internal_format, // internal_format | |
111 width, // width | |
112 height, // height | |
113 0, // border | |
114 format, // format | |
115 type, // type | |
116 nullptr); // data | |
117 } | |
118 | |
119 decoder->RestoreActiveTextureUnitBinding(target); | |
120 return texture_ref; | |
121 } | |
122 | |
123 base::Optional<gpu::SyncToken> CreateSyncToken() override { | |
124 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
125 | |
126 if (!stub_) | |
127 return base::nullopt; | |
128 | |
129 // TODO(sandersd): Is there a way to get the correct namespace? Should we be | |
130 // setting the verified flag? Is 0 correct? Can we insert our own sync | |
131 // point? | |
132 return gpu::SyncToken(gpu::CommandBufferNamespace::GPU_IO, | |
133 stub_->stream_id(), stub_->command_buffer_id(), 0); | |
watk
2017/06/14 00:01:38
Are you intending this TODO to be of the kind "let
sandersd (OOO until July 31)
2017/06/14 00:03:02
This is in the category of 'I don't understand thi
| |
134 } | |
135 | |
136 base::Optional<gpu::Mailbox> CreateMailbox( | |
137 gpu::gles2::TextureRef* texture_ref) override { | |
138 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
139 if (!stub_) | |
140 return base::nullopt; | |
141 gpu::gles2::GLES2Decoder* decoder = stub_->decoder(); | |
142 if (!decoder) | |
143 return base::nullopt; | |
144 gpu::gles2::ContextGroup* group = decoder->GetContextGroup(); | |
145 if (!group) | |
146 return base::nullopt; | |
147 gpu::gles2::MailboxManager* mailbox_manager = group->mailbox_manager(); | |
148 if (!mailbox_manager) | |
149 return base::nullopt; | |
150 | |
151 gpu::Mailbox mailbox = gpu::Mailbox::Generate(); | |
152 mailbox_manager->ProduceTexture(mailbox, texture_ref->texture()); | |
153 return mailbox; | |
154 } | |
155 | |
156 private: | |
157 void OnWillDestroyStub() override { | |
158 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
159 stub_ = nullptr; | |
160 } | |
161 | |
162 gpu::GpuCommandBufferStub* stub_ = nullptr; | |
watk
2017/06/14 00:01:38
Unconventional to have an initializer here and in
sandersd (OOO until July 31)
2017/06/16 22:31:38
Done.
| |
163 THREAD_CHECKER(thread_checker_); | |
164 | |
165 DISALLOW_COPY_AND_ASSIGN(CommandBufferHelperImpl); | |
166 }; | |
167 | |
168 // static | |
169 std::unique_ptr<CommandBufferHelper> CommandBufferHelper::Create( | |
170 gpu::GpuCommandBufferStub* stub) { | |
171 if (!stub) | |
172 return nullptr; | |
173 return base::MakeUnique<CommandBufferHelperImpl>(stub); | |
174 } | |
175 | |
176 } // namespace media | |
OLD | NEW |