| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/common/gpu/media/avda_codec_image.h" | 5 #include "content/common/gpu/media/avda_codec_image.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "content/common/gpu/media/avda_shared_state.h" | 10 #include "content/common/gpu/media/avda_shared_state.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 const scoped_refptr<AVDASharedState>& shared_state, | 22 const scoped_refptr<AVDASharedState>& shared_state, |
| 23 media::VideoCodecBridge* codec, | 23 media::VideoCodecBridge* codec, |
| 24 const base::WeakPtr<gpu::gles2::GLES2Decoder>& decoder, | 24 const base::WeakPtr<gpu::gles2::GLES2Decoder>& decoder, |
| 25 const scoped_refptr<gfx::SurfaceTexture>& surface_texture) | 25 const scoped_refptr<gfx::SurfaceTexture>& surface_texture) |
| 26 : shared_state_(shared_state), | 26 : shared_state_(shared_state), |
| 27 codec_buffer_index_(kInvalidCodecBufferIndex), | 27 codec_buffer_index_(kInvalidCodecBufferIndex), |
| 28 media_codec_(codec), | 28 media_codec_(codec), |
| 29 decoder_(decoder), | 29 decoder_(decoder), |
| 30 surface_texture_(surface_texture), | 30 surface_texture_(surface_texture), |
| 31 detach_surface_texture_on_destruction_(false), | 31 detach_surface_texture_on_destruction_(false), |
| 32 texture_(0), | 32 texture_(0) { |
| 33 need_shader_info_(true), | 33 // Default to a sane guess of "flip Y", just in case we can't get |
| 34 texmatrix_uniform_location_(-1) { | 34 // the matrix on the first call. |
| 35 memset(gl_matrix_, 0, sizeof(gl_matrix_)); | 35 memset(gl_matrix_, 0, sizeof(gl_matrix_)); |
| 36 gl_matrix_[0] = gl_matrix_[5] = gl_matrix_[10] = gl_matrix_[15] = 1.0f; | 36 gl_matrix_[0] = gl_matrix_[10] = gl_matrix_[15] = 1.0f; |
| 37 gl_matrix_[5] = -1.0f; |
| 37 } | 38 } |
| 38 | 39 |
| 39 AVDACodecImage::~AVDACodecImage() {} | 40 AVDACodecImage::~AVDACodecImage() {} |
| 40 | 41 |
| 41 void AVDACodecImage::Destroy(bool have_context) {} | 42 void AVDACodecImage::Destroy(bool have_context) {} |
| 42 | 43 |
| 43 gfx::Size AVDACodecImage::GetSize() { | 44 gfx::Size AVDACodecImage::GetSize() { |
| 44 return size_; | 45 return size_; |
| 45 } | 46 } |
| 46 | 47 |
| 47 unsigned AVDACodecImage::GetInternalFormat() { | 48 unsigned AVDACodecImage::GetInternalFormat() { |
| 48 return GL_RGBA; | 49 return GL_RGBA; |
| 49 } | 50 } |
| 50 | 51 |
| 51 bool AVDACodecImage::BindTexImage(unsigned target) { | 52 bool AVDACodecImage::BindTexImage(unsigned target) { |
| 52 return false; | 53 return false; |
| 53 } | 54 } |
| 54 | 55 |
| 55 void AVDACodecImage::ReleaseTexImage(unsigned target) {} | 56 void AVDACodecImage::ReleaseTexImage(unsigned target) {} |
| 56 | 57 |
| 57 bool AVDACodecImage::CopyTexImage(unsigned target) { | 58 bool AVDACodecImage::CopyTexImage(unsigned target) { |
| 58 if (!surface_texture_) | 59 if (!surface_texture_) |
| 59 return false; | 60 return false; |
| 60 | 61 |
| 61 if (target != GL_TEXTURE_EXTERNAL_OES) | 62 if (target != GL_TEXTURE_EXTERNAL_OES) |
| 62 return false; | 63 return false; |
| 63 | 64 |
| 64 // Verify that the currently bound texture is the right one. If we're not | 65 // Insist that the current context is in the same share group, since we will |
| 65 // copying to a Texture that shares our service_id, then we can't do much. | 66 // only work if we're using the surface texture's client texture. |
| 66 // This will force a copy. | 67 if (!IsCorrectShareGroup()) |
| 67 // TODO(liberato): Fall back to a copy that uses the texture matrix. | 68 return false; |
| 69 |
| 68 GLint bound_service_id = 0; | 70 GLint bound_service_id = 0; |
| 69 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &bound_service_id); | 71 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &bound_service_id); |
| 72 // We insist that the currently bound texture is the right one. We could |
| 73 // make a new glimage from a 2D image. |
| 70 if (bound_service_id != shared_state_->surface_texture_service_id()) | 74 if (bound_service_id != shared_state_->surface_texture_service_id()) |
| 71 return false; | 75 return false; |
| 72 | 76 |
| 73 // Attach the surface texture to our GL context if needed. | 77 // If the surface texture isn't attached yet, then attach it. Note that this |
| 78 // will be to the texture in |shared_state_|, because of the checks above. |
| 74 if (!shared_state_->surface_texture_is_attached()) | 79 if (!shared_state_->surface_texture_is_attached()) |
| 75 AttachSurfaceTextureToContext(); | 80 AttachSurfaceTextureToContext(); |
| 76 | 81 |
| 77 // Make sure that we have the right image in the front buffer. | 82 // Make sure that we have the right image in the front buffer. Note that the |
| 78 UpdateSurfaceTexture(); | 83 // bound_service_id is guaranteed to be equal to the surface texture's client |
| 79 | 84 // texture id, so we can skip preserving it if the right context is current. |
| 80 InstallTextureMatrix(); | 85 UpdateSurfaceTexture(kDontRestoreBindings); |
| 81 | |
| 82 // TODO(liberato): Handle the texture matrix properly. | |
| 83 // Either we can update the shader with it or we can move all of the logic | |
| 84 // to updateTexImage() to the right place in the cc to send it to the shader. | |
| 85 // For now, we just skip it. crbug.com/530681 | |
| 86 | 86 |
| 87 // By setting image state to UNBOUND instead of COPIED we ensure that | 87 // By setting image state to UNBOUND instead of COPIED we ensure that |
| 88 // CopyTexImage() is called each time the surface texture is used for drawing. | 88 // CopyTexImage() is called each time the surface texture is used for drawing. |
| 89 // It would be nice if we could do this via asking for the currently bound | 89 // It would be nice if we could do this via asking for the currently bound |
| 90 // Texture, but the active unit never seems to change. | 90 // Texture, but the active unit never seems to change. |
| 91 texture_->SetLevelImage(GL_TEXTURE_EXTERNAL_OES, 0, this, | 91 texture_->SetLevelImage(GL_TEXTURE_EXTERNAL_OES, 0, this, |
| 92 gpu::gles2::Texture::UNBOUND); | 92 gpu::gles2::Texture::UNBOUND); |
| 93 | 93 |
| 94 return true; | 94 return true; |
| 95 } | 95 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 116 media_codec_->ReleaseOutputBuffer(codec_buffer_index_, true); | 116 media_codec_->ReleaseOutputBuffer(codec_buffer_index_, true); |
| 117 codec_buffer_index_ = kInvalidCodecBufferIndex; | 117 codec_buffer_index_ = kInvalidCodecBufferIndex; |
| 118 } | 118 } |
| 119 return true; | 119 return true; |
| 120 } | 120 } |
| 121 | 121 |
| 122 void AVDACodecImage::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, | 122 void AVDACodecImage::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, |
| 123 uint64_t process_tracing_id, | 123 uint64_t process_tracing_id, |
| 124 const std::string& dump_name) {} | 124 const std::string& dump_name) {} |
| 125 | 125 |
| 126 void AVDACodecImage::UpdateSurfaceTexture() { | 126 void AVDACodecImage::UpdateSurfaceTexture(RestoreBindingsMode mode) { |
| 127 DCHECK(surface_texture_); | 127 DCHECK(surface_texture_); |
| 128 | 128 |
| 129 // Render via the media codec if needed. | 129 // Render via the media codec if needed. |
| 130 if (codec_buffer_index_ == kInvalidCodecBufferIndex || !media_codec_) | 130 if (!IsCodecBufferOutstanding()) |
| 131 return; | 131 return; |
| 132 | 132 |
| 133 // The decoder buffer is still pending. | 133 // The decoder buffer is still pending. |
| 134 // This must be synchronous, so wait for OnFrameAvailable. | 134 // This must be synchronous, so wait for OnFrameAvailable. |
| 135 media_codec_->ReleaseOutputBuffer(codec_buffer_index_, true); | 135 media_codec_->ReleaseOutputBuffer(codec_buffer_index_, true); |
| 136 { | 136 { |
| 137 SCOPED_UMA_HISTOGRAM_TIMER("Media.AvdaCodecImage.WaitTimeForFrame"); | 137 SCOPED_UMA_HISTOGRAM_TIMER("Media.AvdaCodecImage.WaitTimeForFrame"); |
| 138 shared_state_->WaitForFrameAvailable(); | 138 shared_state_->WaitForFrameAvailable(); |
| 139 } | 139 } |
| 140 | 140 |
| 141 // Don't bother to check if we're rendered again. | 141 // Don't bother to check if we're rendered again. |
| 142 codec_buffer_index_ = kInvalidCodecBufferIndex; | 142 codec_buffer_index_ = kInvalidCodecBufferIndex; |
| 143 | 143 |
| 144 // Swap the rendered image to the front. | 144 // Swap the rendered image to the front. |
| 145 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current; | 145 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current = MakeCurrentIfNeeded(); |
| 146 if (!shared_state_->context()->IsCurrent(NULL)) { | 146 |
| 147 scoped_make_current.reset(new ui::ScopedMakeCurrent( | 147 // If we changed contexts, then we always want to restore it, since the caller |
| 148 shared_state_->context(), shared_state_->surface())); | 148 // doesn't know that we're switching contexts. |
| 149 } | 149 if (scoped_make_current) |
| 150 mode = kDoRestoreBindings; |
| 151 |
| 152 // Save the current binding if requested. |
| 153 GLint bound_service_id = 0; |
| 154 if (mode == kDoRestoreBindings) |
| 155 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &bound_service_id); |
| 156 |
| 150 surface_texture_->UpdateTexImage(); | 157 surface_texture_->UpdateTexImage(); |
| 158 if (mode == kDoRestoreBindings) |
| 159 glBindTexture(GL_TEXTURE_EXTERNAL_OES, bound_service_id); |
| 151 | 160 |
| 152 // Helpfully, this is already column major. | 161 // Helpfully, this is already column major. |
| 153 surface_texture_->GetTransformMatrix(gl_matrix_); | 162 surface_texture_->GetTransformMatrix(gl_matrix_); |
| 154 } | 163 } |
| 155 | 164 |
| 156 void AVDACodecImage::SetMediaCodecBufferIndex(int buffer_index) { | 165 void AVDACodecImage::SetMediaCodecBufferIndex(int buffer_index) { |
| 157 codec_buffer_index_ = buffer_index; | 166 codec_buffer_index_ = buffer_index; |
| 158 } | 167 } |
| 159 | 168 |
| 160 int AVDACodecImage::GetMediaCodecBufferIndex() const { | 169 int AVDACodecImage::GetMediaCodecBufferIndex() const { |
| 161 return codec_buffer_index_; | 170 return codec_buffer_index_; |
| 162 } | 171 } |
| 163 | 172 |
| 164 void AVDACodecImage::SetSize(const gfx::Size& size) { | 173 void AVDACodecImage::SetSize(const gfx::Size& size) { |
| 165 size_ = size; | 174 size_ = size; |
| 166 } | 175 } |
| 167 | 176 |
| 168 void AVDACodecImage::SetMediaCodec(media::MediaCodecBridge* codec) { | 177 void AVDACodecImage::SetMediaCodec(media::MediaCodecBridge* codec) { |
| 169 media_codec_ = codec; | 178 media_codec_ = codec; |
| 170 } | 179 } |
| 171 | 180 |
| 172 void AVDACodecImage::SetTexture(gpu::gles2::Texture* texture) { | 181 void AVDACodecImage::SetTexture(gpu::gles2::Texture* texture) { |
| 173 texture_ = texture; | 182 texture_ = texture; |
| 174 } | 183 } |
| 175 | 184 |
| 176 void AVDACodecImage::AttachSurfaceTextureToContext() { | 185 void AVDACodecImage::AttachSurfaceTextureToContext() { |
| 177 DCHECK(surface_texture_); | 186 DCHECK(surface_texture_); |
| 178 | 187 |
| 188 // We assume that the currently bound texture is the intended one. |
| 189 |
| 179 // Attach the surface texture to the first context we're bound on, so that | 190 // Attach the surface texture to the first context we're bound on, so that |
| 180 // no context switch is needed later. | 191 // no context switch is needed later. |
| 181 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 192 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 182 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 193 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 183 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 194 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 184 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 195 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 185 | 196 |
| 186 // The surface texture is already detached, so just attach it. | 197 // The surface texture is already detached, so just attach it. |
| 187 // We could do this earlier, but SurfaceTexture has context affinity, and we | 198 // We could do this earlier, but SurfaceTexture has context affinity, and we |
| 188 // don't want to require a context switch. | 199 // don't want to require a context switch. |
| 189 surface_texture_->AttachToGLContext(); | 200 surface_texture_->AttachToGLContext(); |
| 190 shared_state_->did_attach_surface_texture(); | 201 shared_state_->DidAttachSurfaceTexture(); |
| 191 } | 202 } |
| 192 | 203 |
| 193 void AVDACodecImage::InstallTextureMatrix() { | 204 scoped_ptr<ui::ScopedMakeCurrent> AVDACodecImage::MakeCurrentIfNeeded() { |
| 194 DCHECK(surface_texture_); | 205 DCHECK(shared_state_->context()); |
| 195 | 206 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current; |
| 196 // glUseProgram() has been run already -- just modify the uniform. | 207 if (!shared_state_->context()->IsCurrent(NULL)) { |
| 197 // Updating this via VideoFrameProvider::Client::DidUpdateMatrix() would | 208 scoped_make_current.reset(new ui::ScopedMakeCurrent( |
| 198 // be a better solution, except that we'd definitely miss a frame at this | 209 shared_state_->context(), shared_state_->surface())); |
| 199 // point in drawing. | |
| 200 // Our current method assumes that we'll end up being a stream resource, | |
| 201 // and that the program has a texMatrix uniform that does what we want. | |
| 202 if (need_shader_info_) { | |
| 203 GLint program_id = -1; | |
| 204 glGetIntegerv(GL_CURRENT_PROGRAM, &program_id); | |
| 205 | |
| 206 if (program_id >= 0) { | |
| 207 // This is memorized from cc/output/shader.cc . | |
| 208 const char* uniformName = "texMatrix"; | |
| 209 | |
| 210 // Within unittests this value may be -1. | |
| 211 texmatrix_uniform_location_ = | |
| 212 glGetUniformLocation(program_id, uniformName); | |
| 213 } | |
| 214 | |
| 215 // Only try once. | |
| 216 need_shader_info_ = false; | |
| 217 } | 210 } |
| 218 | 211 |
| 219 if (texmatrix_uniform_location_ >= 0) { | 212 return scoped_make_current; |
| 220 glUniformMatrix4fv(texmatrix_uniform_location_, 1, false, gl_matrix_); | 213 } |
| 214 |
| 215 bool AVDACodecImage::GetCustomMatrix(int matrixId, float matrix[16]) { |
| 216 if (matrixId != 12345) // TODO(liberato): enum |
| 217 return false; |
| 218 |
| 219 if (IsCodecBufferOutstanding() && shared_state_ && surface_texture_) { |
| 220 // Our current matrix may be stale. Update it if possible. |
| 221 if (!shared_state_->surface_texture_is_attached()) { |
| 222 // Don't attach the surface texture permanently. Perhaps we should |
| 223 // just attach the surface texture in avda and be done with it. |
| 224 GLuint service_id = 0; |
| 225 glGenTextures(1, &service_id); |
| 226 GLint bound_service_id = 0; |
| 227 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &bound_service_id); |
| 228 glBindTexture(GL_TEXTURE_EXTERNAL_OES, service_id); |
| 229 AttachSurfaceTextureToContext(); |
| 230 UpdateSurfaceTexture(kDontRestoreBindings); |
| 231 // Detach the surface texture, which deletes the generated texture. |
| 232 surface_texture_->DetachFromGLContext(); |
| 233 shared_state_->DidDetachSurfaceTexture(); |
| 234 glBindTexture(GL_TEXTURE_EXTERNAL_OES, bound_service_id); |
| 235 } else { |
| 236 // Surface texture is already attached, so just update it. |
| 237 UpdateSurfaceTexture(kDoRestoreBindings); |
| 238 } |
| 221 } | 239 } |
| 240 |
| 241 memcpy(matrix, gl_matrix_, sizeof(gl_matrix_)); |
| 242 return true; |
| 243 } |
| 244 |
| 245 bool AVDACodecImage::IsCorrectShareGroup() const { |
| 246 if (!shared_state_) |
| 247 return false; |
| 248 |
| 249 gfx::GLContext* current_context = gfx::GLContext::GetCurrent(); |
| 250 if (!current_context) |
| 251 return false; |
| 252 return current_context->share_group() == shared_state_->share_group(); |
| 253 } |
| 254 |
| 255 bool AVDACodecImage::IsCodecBufferOutstanding() const { |
| 256 return codec_buffer_index_ != kInvalidCodecBufferIndex && media_codec_; |
| 222 } | 257 } |
| 223 | 258 |
| 224 } // namespace content | 259 } // namespace content |
| OLD | NEW |