| 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/android_deferred_rendering_backing_strategy.h
" | 5 #include "content/common/gpu/media/android_deferred_rendering_backing_strategy.h
" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "content/common/gpu/gpu_channel.h" | 11 #include "content/common/gpu/gpu_channel.h" |
| 12 #include "content/common/gpu/gpu_surface_lookup.h" |
| 12 #include "content/common/gpu/media/avda_codec_image.h" | 13 #include "content/common/gpu/media/avda_codec_image.h" |
| 13 #include "content/common/gpu/media/avda_return_on_failure.h" | 14 #include "content/common/gpu/media/avda_return_on_failure.h" |
| 14 #include "content/common/gpu/media/avda_shared_state.h" | 15 #include "content/common/gpu/media/avda_shared_state.h" |
| 15 #include "gpu/command_buffer/service/texture_manager.h" | 16 #include "gpu/command_buffer/service/texture_manager.h" |
| 16 #include "ui/gl/android/surface_texture.h" | 17 #include "ui/gl/android/surface_texture.h" |
| 17 #include "ui/gl/gl_bindings.h" | 18 #include "ui/gl/gl_bindings.h" |
| 18 | 19 |
| 19 namespace content { | 20 namespace content { |
| 20 | 21 |
| 21 AndroidDeferredRenderingBackingStrategy:: | 22 AndroidDeferredRenderingBackingStrategy:: |
| 22 AndroidDeferredRenderingBackingStrategy() | 23 AndroidDeferredRenderingBackingStrategy() |
| 23 : state_provider_(nullptr), media_codec_(nullptr) {} | 24 : state_provider_(nullptr), media_codec_(nullptr) {} |
| 24 | 25 |
| 25 AndroidDeferredRenderingBackingStrategy:: | 26 AndroidDeferredRenderingBackingStrategy:: |
| 26 ~AndroidDeferredRenderingBackingStrategy() {} | 27 ~AndroidDeferredRenderingBackingStrategy() {} |
| 27 | 28 |
| 28 void AndroidDeferredRenderingBackingStrategy::Initialize( | 29 gfx::ScopedJavaSurface AndroidDeferredRenderingBackingStrategy::Initialize( |
| 29 AVDAStateProvider* state_provider) { | 30 AVDAStateProvider* state_provider, |
| 31 int surface_view_id) { |
| 30 state_provider_ = state_provider; | 32 state_provider_ = state_provider; |
| 31 shared_state_ = new AVDASharedState(); | 33 shared_state_ = new AVDASharedState(); |
| 32 | 34 |
| 35 gfx::ScopedJavaSurface surface; |
| 36 if (surface_view_id != media::VideoDecodeAccelerator::Config::kNoSurfaceID) { |
| 37 surface = |
| 38 GpuSurfaceLookup::GetInstance()->AcquireJavaSurface(surface_view_id); |
| 39 } else { |
| 40 // Create a detached SurfaceTexture. Detaching it will silently fail to |
| 41 // delete texture 0. |
| 42 surface_texture_ = gfx::SurfaceTexture::Create(0); |
| 43 surface_texture_->DetachFromGLContext(); |
| 44 surface = gfx::ScopedJavaSurface(surface_texture_.get()); |
| 45 } |
| 46 |
| 33 // Create a texture for the SurfaceTexture to use. We don't attach it here | 47 // Create a texture for the SurfaceTexture to use. We don't attach it here |
| 34 // so that it gets attached in the compositor gl context in the common case. | 48 // so that it gets attached in the compositor gl context in the common case. |
| 35 GLuint service_id = 0; | 49 GLuint service_id = 0; |
| 36 glGenTextures(1, &service_id); | 50 glGenTextures(1, &service_id); |
| 37 DCHECK(service_id); | 51 DCHECK(service_id); |
| 38 shared_state_->set_surface_texture_service_id(service_id); | 52 shared_state_->set_surface_texture_service_id(service_id); |
| 53 |
| 54 return surface; |
| 39 } | 55 } |
| 40 | 56 |
| 41 void AndroidDeferredRenderingBackingStrategy::Cleanup( | 57 void AndroidDeferredRenderingBackingStrategy::Cleanup( |
| 42 bool have_context, | 58 bool have_context, |
| 43 const AndroidVideoDecodeAccelerator::OutputBufferMap& buffers) { | 59 const AndroidVideoDecodeAccelerator::OutputBufferMap& buffers) { |
| 44 // Make sure that no PictureBuffer textures refer to the SurfaceTexture or to | 60 // Make sure that no PictureBuffer textures refer to the SurfaceTexture or to |
| 45 // the service_id that we created for it. | 61 // the service_id that we created for it. |
| 46 for (const std::pair<int, media::PictureBuffer>& entry : buffers) | 62 for (const std::pair<int, media::PictureBuffer>& entry : buffers) |
| 47 SetImageForPicture(entry.second, nullptr); | 63 SetImageForPicture(entry.second, nullptr); |
| 48 | 64 |
| 49 // Now that no AVDACodecImages refer to the SurfaceTexture's texture, delete | 65 // Now that no AVDACodecImages refer to the SurfaceTexture's texture, delete |
| 50 // the texture name. | 66 // the texture name. |
| 51 GLuint service_id = shared_state_->surface_texture_service_id(); | 67 GLuint service_id = shared_state_->surface_texture_service_id(); |
| 52 if (service_id > 0 && have_context) | 68 if (service_id > 0 && have_context) |
| 53 glDeleteTextures(1, &service_id); | 69 glDeleteTextures(1, &service_id); |
| 54 } | 70 } |
| 55 | 71 |
| 72 scoped_refptr<gfx::SurfaceTexture> |
| 73 AndroidDeferredRenderingBackingStrategy::GetSurfaceTexture() const { |
| 74 return surface_texture_; |
| 75 } |
| 76 |
| 56 uint32_t AndroidDeferredRenderingBackingStrategy::GetTextureTarget() const { | 77 uint32_t AndroidDeferredRenderingBackingStrategy::GetTextureTarget() const { |
| 57 return GL_TEXTURE_EXTERNAL_OES; | 78 return GL_TEXTURE_EXTERNAL_OES; |
| 58 } | 79 } |
| 59 | 80 |
| 60 scoped_refptr<gfx::SurfaceTexture> | |
| 61 AndroidDeferredRenderingBackingStrategy::CreateSurfaceTexture() { | |
| 62 // AVDACodecImage will handle attaching this to a texture later. | |
| 63 surface_texture_ = gfx::SurfaceTexture::Create(0); | |
| 64 // Detach from our GL context so that the GLImages can attach. It will | |
| 65 // silently fail to delete texture 0. | |
| 66 surface_texture_->DetachFromGLContext(); | |
| 67 | |
| 68 return surface_texture_; | |
| 69 } | |
| 70 | |
| 71 gpu::gles2::TextureRef* | 81 gpu::gles2::TextureRef* |
| 72 AndroidDeferredRenderingBackingStrategy::GetTextureForPicture( | 82 AndroidDeferredRenderingBackingStrategy::GetTextureForPicture( |
| 73 const media::PictureBuffer& picture_buffer) { | 83 const media::PictureBuffer& picture_buffer) { |
| 74 RETURN_NULL_IF_NULL(state_provider_->GetGlDecoder()); | 84 RETURN_NULL_IF_NULL(state_provider_->GetGlDecoder()); |
| 75 gpu::gles2::TextureManager* texture_manager = | 85 gpu::gles2::TextureManager* texture_manager = |
| 76 state_provider_->GetGlDecoder()->GetContextGroup()->texture_manager(); | 86 state_provider_->GetGlDecoder()->GetContextGroup()->texture_manager(); |
| 77 RETURN_NULL_IF_NULL(texture_manager); | 87 RETURN_NULL_IF_NULL(texture_manager); |
| 78 gpu::gles2::TextureRef* texture_ref = | 88 gpu::gles2::TextureRef* texture_ref = |
| 79 texture_manager->GetTexture(picture_buffer.internal_texture_id()); | 89 texture_manager->GetTexture(picture_buffer.internal_texture_id()); |
| 80 RETURN_NULL_IF_NULL(texture_ref); | 90 RETURN_NULL_IF_NULL(texture_ref); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 109 size.width(), size.height(), 1, 0, GL_RGBA, | 119 size.width(), size.height(), 1, 0, GL_RGBA, |
| 110 GL_UNSIGNED_BYTE, gfx::Rect()); | 120 GL_UNSIGNED_BYTE, gfx::Rect()); |
| 111 | 121 |
| 112 // Override the texture's service_id, so that it will use the one that | 122 // Override the texture's service_id, so that it will use the one that |
| 113 // will be / is attached to the SurfaceTexture. | 123 // will be / is attached to the SurfaceTexture. |
| 114 DCHECK(shared_state_->surface_texture_service_id()); | 124 DCHECK(shared_state_->surface_texture_service_id()); |
| 115 texture_ref->texture()->SetUnownedServiceId( | 125 texture_ref->texture()->SetUnownedServiceId( |
| 116 shared_state_->surface_texture_service_id()); | 126 shared_state_->surface_texture_service_id()); |
| 117 | 127 |
| 118 static_cast<AVDACodecImage*>(image.get()) | 128 static_cast<AVDACodecImage*>(image.get()) |
| 119 ->setTexture(texture_ref->texture()); | 129 ->SetTexture(texture_ref->texture()); |
| 120 } else { | 130 } else { |
| 121 // Clear the unowned service_id, so that this texture is no longer going | 131 // Clear the unowned service_id, so that this texture is no longer going |
| 122 // to depend on the surface texture at all. | 132 // to depend on the surface texture at all. |
| 123 texture_ref->texture()->SetUnownedServiceId(0); | 133 texture_ref->texture()->SetUnownedServiceId(0); |
| 124 } | 134 } |
| 125 | 135 |
| 136 // For SurfaceTexture we set the image to UNBOUND so that the implementation |
| 137 // will call CopyTexImage, which is where AVDACodecImage updates the |
| 138 // SurfaceTexture to the right frame. |
| 139 // For SurfaceView we set the image to be BOUND because ScheduleOverlayPlane |
| 140 // expects it. If something tries to sample from this texture it won't work, |
| 141 // but there's no way to sample from a SurfaceView anyway, so it doesn't |
| 142 // matter. The only way to use this texture is to schedule it as an overlay. |
| 143 const gpu::gles2::Texture::ImageState image_state = |
| 144 surface_texture_ ? gpu::gles2::Texture::UNBOUND |
| 145 : gpu::gles2::Texture::BOUND; |
| 126 texture_manager->SetLevelImage(texture_ref, GetTextureTarget(), 0, | 146 texture_manager->SetLevelImage(texture_ref, GetTextureTarget(), 0, |
| 127 image.get(), gpu::gles2::Texture::UNBOUND); | 147 image.get(), image_state); |
| 128 } | 148 } |
| 129 | 149 |
| 130 void AndroidDeferredRenderingBackingStrategy::UseCodecBufferForPictureBuffer( | 150 void AndroidDeferredRenderingBackingStrategy::UseCodecBufferForPictureBuffer( |
| 131 int32_t codec_buf_index, | 151 int32_t codec_buf_index, |
| 132 const media::PictureBuffer& picture_buffer) { | 152 const media::PictureBuffer& picture_buffer) { |
| 133 // Make sure that the decoder is available. | 153 // Make sure that the decoder is available. |
| 134 RETURN_IF_NULL(state_provider_->GetGlDecoder()); | 154 RETURN_IF_NULL(state_provider_->GetGlDecoder()); |
| 135 | 155 |
| 136 // Notify the AVDACodecImage for picture_buffer that it should use the | 156 // Notify the AVDACodecImage for picture_buffer that it should use the |
| 137 // decoded buffer codec_buf_index to render this frame. | 157 // decoded buffer codec_buf_index to render this frame. |
| 138 AVDACodecImage* avImage = GetImageForPicture(picture_buffer); | 158 AVDACodecImage* avda_image = GetImageForPicture(picture_buffer); |
| 139 RETURN_IF_NULL(avImage); | 159 RETURN_IF_NULL(avda_image); |
| 140 DCHECK_EQ(avImage->GetMediaCodecBufferIndex(), -1); | 160 DCHECK_EQ(avda_image->GetMediaCodecBufferIndex(), -1); |
| 141 // Note that this is not a race, since we do not re-use a PictureBuffer | 161 // Note that this is not a race, since we do not re-use a PictureBuffer |
| 142 // until after the CC is done drawing it. | 162 // until after the CC is done drawing it. |
| 143 avImage->SetMediaCodecBufferIndex(codec_buf_index); | 163 avda_image->SetMediaCodecBufferIndex(codec_buf_index); |
| 144 avImage->SetSize(state_provider_->GetSize()); | 164 avda_image->SetSize(state_provider_->GetSize()); |
| 145 } | 165 } |
| 146 | 166 |
| 147 void AndroidDeferredRenderingBackingStrategy::AssignOnePictureBuffer( | 167 void AndroidDeferredRenderingBackingStrategy::AssignOnePictureBuffer( |
| 148 const media::PictureBuffer& picture_buffer) { | 168 const media::PictureBuffer& picture_buffer) { |
| 149 // Attach a GLImage to each texture that will use the surface texture. | 169 // Attach a GLImage to each texture that will use the surface texture. |
| 150 // We use a refptr here in case SetImageForPicture fails. | 170 // We use a refptr here in case SetImageForPicture fails. |
| 151 scoped_refptr<gl::GLImage> gl_image( | 171 scoped_refptr<gl::GLImage> gl_image = |
| 152 new AVDACodecImage(shared_state_, media_codec_, | 172 new AVDACodecImage(shared_state_, media_codec_, |
| 153 state_provider_->GetGlDecoder(), surface_texture_)); | 173 state_provider_->GetGlDecoder(), surface_texture_); |
| 154 SetImageForPicture(picture_buffer, gl_image); | 174 SetImageForPicture(picture_buffer, gl_image); |
| 155 } | 175 } |
| 156 | 176 |
| 157 void AndroidDeferredRenderingBackingStrategy::ReleaseCodecBufferForPicture( | 177 void AndroidDeferredRenderingBackingStrategy::ReleaseCodecBufferForPicture( |
| 158 const media::PictureBuffer& picture_buffer) { | 178 const media::PictureBuffer& picture_buffer) { |
| 159 AVDACodecImage* avImage = GetImageForPicture(picture_buffer); | 179 AVDACodecImage* avda_image = GetImageForPicture(picture_buffer); |
| 160 | 180 |
| 161 // See if there is a media codec buffer still attached to this image. | 181 // See if there is a media codec buffer still attached to this image. |
| 162 const int32_t codec_buffer = avImage->GetMediaCodecBufferIndex(); | 182 const int32_t codec_buffer = avda_image->GetMediaCodecBufferIndex(); |
| 163 | 183 |
| 164 if (codec_buffer >= 0) { | 184 if (codec_buffer >= 0) { |
| 165 // PictureBuffer wasn't displayed, so release the buffer. | 185 // PictureBuffer wasn't displayed, so release the buffer. |
| 166 media_codec_->ReleaseOutputBuffer(codec_buffer, false); | 186 media_codec_->ReleaseOutputBuffer(codec_buffer, false); |
| 167 avImage->SetMediaCodecBufferIndex(-1); | 187 avda_image->SetMediaCodecBufferIndex(-1); |
| 168 } | 188 } |
| 169 } | 189 } |
| 170 | 190 |
| 171 void AndroidDeferredRenderingBackingStrategy::ReuseOnePictureBuffer( | 191 void AndroidDeferredRenderingBackingStrategy::ReuseOnePictureBuffer( |
| 172 const media::PictureBuffer& picture_buffer) { | 192 const media::PictureBuffer& picture_buffer) { |
| 173 // At this point, the CC must be done with the picture. We can't really | 193 // At this point, the CC must be done with the picture. We can't really |
| 174 // check for that here directly. it's guaranteed in gpu_video_decoder.cc, | 194 // check for that here directly. it's guaranteed in gpu_video_decoder.cc, |
| 175 // when it waits on the sync point before releasing the mailbox. That sync | 195 // when it waits on the sync point before releasing the mailbox. That sync |
| 176 // point is inserted by destroying the resource in VideoLayerImpl::DidDraw. | 196 // point is inserted by destroying the resource in VideoLayerImpl::DidDraw. |
| 177 ReleaseCodecBufferForPicture(picture_buffer); | 197 ReleaseCodecBufferForPicture(picture_buffer); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 189 SetImageForPicture(picture_buffer, nullptr); | 209 SetImageForPicture(picture_buffer, nullptr); |
| 190 } | 210 } |
| 191 | 211 |
| 192 void AndroidDeferredRenderingBackingStrategy::CodecChanged( | 212 void AndroidDeferredRenderingBackingStrategy::CodecChanged( |
| 193 media::VideoCodecBridge* codec, | 213 media::VideoCodecBridge* codec, |
| 194 const AndroidVideoDecodeAccelerator::OutputBufferMap& buffers) { | 214 const AndroidVideoDecodeAccelerator::OutputBufferMap& buffers) { |
| 195 // Clear any outstanding codec buffer indices, since the new codec (if any) | 215 // Clear any outstanding codec buffer indices, since the new codec (if any) |
| 196 // doesn't know about them. | 216 // doesn't know about them. |
| 197 media_codec_ = codec; | 217 media_codec_ = codec; |
| 198 for (const std::pair<int, media::PictureBuffer>& entry : buffers) { | 218 for (const std::pair<int, media::PictureBuffer>& entry : buffers) { |
| 199 AVDACodecImage* avImage = GetImageForPicture(entry.second); | 219 AVDACodecImage* avda_image = GetImageForPicture(entry.second); |
| 200 avImage->SetMediaCodec(codec); | 220 avda_image->SetMediaCodec(codec); |
| 201 avImage->SetMediaCodecBufferIndex(-1); | 221 avda_image->SetMediaCodecBufferIndex(-1); |
| 202 } | 222 } |
| 203 } | 223 } |
| 204 | 224 |
| 205 void AndroidDeferredRenderingBackingStrategy::OnFrameAvailable() { | 225 void AndroidDeferredRenderingBackingStrategy::OnFrameAvailable() { |
| 206 shared_state_->SignalFrameAvailable(); | 226 shared_state_->SignalFrameAvailable(); |
| 207 } | 227 } |
| 208 | 228 |
| 229 bool AndroidDeferredRenderingBackingStrategy::PicturesAreOverlayable() { |
| 230 // SurfaceView frames are always overlayable because that's the only way to |
| 231 // display them. |
| 232 return !surface_texture_; |
| 233 } |
| 234 |
| 209 } // namespace content | 235 } // namespace content |
| OLD | NEW |