Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(750)

Side by Side Diff: content/common/gpu/media/android_deferred_rendering_backing_strategy.cc

Issue 1639963002: AndroidVideoDecodeAccelerator can now render to a SurfaceView (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/media/avda_codec_image.h" 12 #include "content/common/gpu/media/avda_codec_image.h"
13 #include "content/common/gpu/media/avda_return_on_failure.h" 13 #include "content/common/gpu/media/avda_return_on_failure.h"
14 #include "content/common/gpu/media/avda_shared_state.h" 14 #include "content/common/gpu/media/avda_shared_state.h"
15 #include "gpu/command_buffer/service/texture_manager.h" 15 #include "gpu/command_buffer/service/texture_manager.h"
16 #include "ui/gl/android/surface_texture.h" 16 #include "ui/gl/android/surface_texture.h"
17 #include "ui/gl/gl_bindings.h" 17 #include "ui/gl/gl_bindings.h"
18 18
19 namespace content { 19 namespace content {
20 20
21 AndroidDeferredRenderingBackingStrategy:: 21 AndroidDeferredRenderingBackingStrategy::
22 AndroidDeferredRenderingBackingStrategy() 22 AndroidDeferredRenderingBackingStrategy()
23 : state_provider_(nullptr), media_codec_(nullptr) {} 23 : state_provider_(nullptr), surface_kind_(UNKNOWN), media_codec_(nullptr) {}
24 24
25 AndroidDeferredRenderingBackingStrategy:: 25 AndroidDeferredRenderingBackingStrategy::
26 ~AndroidDeferredRenderingBackingStrategy() {} 26 ~AndroidDeferredRenderingBackingStrategy() {}
27 27
28 void AndroidDeferredRenderingBackingStrategy::Initialize( 28 void AndroidDeferredRenderingBackingStrategy::Initialize(
liberato (no reviews please) 2016/01/26 21:48:45 Initialize(state_provider, surface_id (which may b
29 AVDAStateProvider* state_provider) { 29 AVDAStateProvider* state_provider,
30 scoped_refptr<gfx::SurfaceTexture> surface_texture) {
30 state_provider_ = state_provider; 31 state_provider_ = state_provider;
32 surface_texture_ = surface_texture;
31 shared_state_ = new AVDASharedState(); 33 shared_state_ = new AVDASharedState();
34 surface_kind_ = surface_texture ? SURFACE_TEXTURE : SURFACE_VIEW;
32 35
33 // Create a texture for the SurfaceTexture to use. We don't attach it here 36 // 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. 37 // so that it gets attached in the compositor gl context in the common case.
35 GLuint service_id = 0; 38 GLuint service_id = 0;
36 glGenTextures(1, &service_id); 39 glGenTextures(1, &service_id);
37 DCHECK(service_id); 40 DCHECK(service_id);
38 shared_state_->set_surface_texture_service_id(service_id); 41 shared_state_->set_surface_texture_service_id(service_id);
39 } 42 }
40 43
41 void AndroidDeferredRenderingBackingStrategy::Cleanup( 44 void AndroidDeferredRenderingBackingStrategy::Cleanup(
42 bool have_context, 45 bool have_context,
43 const AndroidVideoDecodeAccelerator::OutputBufferMap& buffers) { 46 const AndroidVideoDecodeAccelerator::OutputBufferMap& buffers) {
44 // Make sure that no PictureBuffer textures refer to the SurfaceTexture or to 47 // Make sure that no PictureBuffer textures refer to the SurfaceTexture or to
45 // the service_id that we created for it. 48 // the service_id that we created for it.
46 for (const std::pair<int, media::PictureBuffer>& entry : buffers) 49 for (const std::pair<int, media::PictureBuffer>& entry : buffers)
47 SetImageForPicture(entry.second, nullptr); 50 SetImageForPicture(entry.second, nullptr);
48 51
49 // Now that no AVDACodecImages refer to the SurfaceTexture's texture, delete 52 // Now that no AVDACodecImages refer to the SurfaceTexture's texture, delete
50 // the texture name. 53 // the texture name.
51 GLuint service_id = shared_state_->surface_texture_service_id(); 54 GLuint service_id = shared_state_->surface_texture_service_id();
52 if (service_id > 0 && have_context) 55 if (service_id > 0 && have_context)
53 glDeleteTextures(1, &service_id); 56 glDeleteTextures(1, &service_id);
54 } 57 }
55 58
56 uint32_t AndroidDeferredRenderingBackingStrategy::GetTextureTarget() const { 59 uint32_t AndroidDeferredRenderingBackingStrategy::GetTextureTarget() const {
57 return GL_TEXTURE_EXTERNAL_OES; 60 return GL_TEXTURE_EXTERNAL_OES;
58 } 61 }
59 62
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* 63 gpu::gles2::TextureRef*
72 AndroidDeferredRenderingBackingStrategy::GetTextureForPicture( 64 AndroidDeferredRenderingBackingStrategy::GetTextureForPicture(
73 const media::PictureBuffer& picture_buffer) { 65 const media::PictureBuffer& picture_buffer) {
74 RETURN_NULL_IF_NULL(state_provider_->GetGlDecoder()); 66 RETURN_NULL_IF_NULL(state_provider_->GetGlDecoder());
75 gpu::gles2::TextureManager* texture_manager = 67 gpu::gles2::TextureManager* texture_manager =
76 state_provider_->GetGlDecoder()->GetContextGroup()->texture_manager(); 68 state_provider_->GetGlDecoder()->GetContextGroup()->texture_manager();
77 RETURN_NULL_IF_NULL(texture_manager); 69 RETURN_NULL_IF_NULL(texture_manager);
78 gpu::gles2::TextureRef* texture_ref = 70 gpu::gles2::TextureRef* texture_ref =
79 texture_manager->GetTexture(picture_buffer.internal_texture_id()); 71 texture_manager->GetTexture(picture_buffer.internal_texture_id());
80 RETURN_NULL_IF_NULL(texture_ref); 72 RETURN_NULL_IF_NULL(texture_ref);
(...skipping 28 matching lines...) Expand all
109 size.width(), size.height(), 1, 0, GL_RGBA, 101 size.width(), size.height(), 1, 0, GL_RGBA,
110 GL_UNSIGNED_BYTE, gfx::Rect()); 102 GL_UNSIGNED_BYTE, gfx::Rect());
111 103
112 // Override the texture's service_id, so that it will use the one that 104 // Override the texture's service_id, so that it will use the one that
113 // will be / is attached to the SurfaceTexture. 105 // will be / is attached to the SurfaceTexture.
114 DCHECK(shared_state_->surface_texture_service_id()); 106 DCHECK(shared_state_->surface_texture_service_id());
115 texture_ref->texture()->SetUnownedServiceId( 107 texture_ref->texture()->SetUnownedServiceId(
116 shared_state_->surface_texture_service_id()); 108 shared_state_->surface_texture_service_id());
117 109
118 static_cast<AVDACodecImage*>(image.get()) 110 static_cast<AVDACodecImage*>(image.get())
119 ->setTexture(texture_ref->texture()); 111 ->SetTexture(texture_ref->texture());
120 } else { 112 } else {
121 // Clear the unowned service_id, so that this texture is no longer going 113 // Clear the unowned service_id, so that this texture is no longer going
122 // to depend on the surface texture at all. 114 // to depend on the surface texture at all.
123 texture_ref->texture()->SetUnownedServiceId(0); 115 texture_ref->texture()->SetUnownedServiceId(0);
124 } 116 }
125 117
118 // For SurfaceTexture we set the image to UNBOUND so that the implementation
119 // will call CopyTexImage, which is where AVDACodecImage updates the
120 // SurfaceTexture to the right frame.
121 // For SurfaceView we set the image to be BOUND because ScheduleOverlayPlane
122 // expects it. If something tries to sample from this texture it won't work,
123 // but there's no way to sample from a SurfaceView anyway, so it doesn't
124 // matter. The only way to use this texture is to schedule it as an overlay.
125 const gpu::gles2::Texture::ImageState image_state =
126 (surface_kind_ == SURFACE_VIEW) ? gpu::gles2::Texture::BOUND
127 : gpu::gles2::Texture::UNBOUND;
126 texture_manager->SetLevelImage(texture_ref, GetTextureTarget(), 0, 128 texture_manager->SetLevelImage(texture_ref, GetTextureTarget(), 0,
127 image.get(), gpu::gles2::Texture::UNBOUND); 129 image.get(), image_state);
128 } 130 }
129 131
130 void AndroidDeferredRenderingBackingStrategy::UseCodecBufferForPictureBuffer( 132 void AndroidDeferredRenderingBackingStrategy::UseCodecBufferForPictureBuffer(
131 int32_t codec_buf_index, 133 int32_t codec_buf_index,
132 const media::PictureBuffer& picture_buffer) { 134 const media::PictureBuffer& picture_buffer) {
133 // Make sure that the decoder is available. 135 // Make sure that the decoder is available.
134 RETURN_IF_NULL(state_provider_->GetGlDecoder()); 136 RETURN_IF_NULL(state_provider_->GetGlDecoder());
135 137
136 // Notify the AVDACodecImage for picture_buffer that it should use the 138 // Notify the AVDACodecImage for picture_buffer that it should use the
137 // decoded buffer codec_buf_index to render this frame. 139 // decoded buffer codec_buf_index to render this frame.
138 AVDACodecImage* avImage = GetImageForPicture(picture_buffer); 140 AVDACodecImage* avda_image = GetImageForPicture(picture_buffer);
139 RETURN_IF_NULL(avImage); 141 RETURN_IF_NULL(avda_image);
140 DCHECK_EQ(avImage->GetMediaCodecBufferIndex(), -1); 142 DCHECK_EQ(avda_image->GetMediaCodecBufferIndex(), -1);
141 // Note that this is not a race, since we do not re-use a PictureBuffer 143 // Note that this is not a race, since we do not re-use a PictureBuffer
142 // until after the CC is done drawing it. 144 // until after the CC is done drawing it.
143 avImage->SetMediaCodecBufferIndex(codec_buf_index); 145 avda_image->SetMediaCodecBufferIndex(codec_buf_index);
144 avImage->SetSize(state_provider_->GetSize()); 146 avda_image->SetSize(state_provider_->GetSize());
145 } 147 }
146 148
147 void AndroidDeferredRenderingBackingStrategy::AssignOnePictureBuffer( 149 void AndroidDeferredRenderingBackingStrategy::AssignOnePictureBuffer(
148 const media::PictureBuffer& picture_buffer) { 150 const media::PictureBuffer& picture_buffer) {
149 // Attach a GLImage to each texture that will use the surface texture. 151 // Attach a GLImage to each texture that will use the surface texture.
150 // We use a refptr here in case SetImageForPicture fails. 152 // We use a refptr here in case SetImageForPicture fails.
151 scoped_refptr<gl::GLImage> gl_image( 153 scoped_refptr<gl::GLImage> gl_image =
152 new AVDACodecImage(shared_state_, media_codec_, 154 new AVDACodecImage(shared_state_, media_codec_,
153 state_provider_->GetGlDecoder(), surface_texture_)); 155 state_provider_->GetGlDecoder(), surface_texture_);
154 SetImageForPicture(picture_buffer, gl_image); 156 SetImageForPicture(picture_buffer, gl_image);
155 } 157 }
156 158
157 void AndroidDeferredRenderingBackingStrategy::ReleaseCodecBufferForPicture( 159 void AndroidDeferredRenderingBackingStrategy::ReleaseCodecBufferForPicture(
158 const media::PictureBuffer& picture_buffer) { 160 const media::PictureBuffer& picture_buffer) {
159 AVDACodecImage* avImage = GetImageForPicture(picture_buffer); 161 AVDACodecImage* avda_image = GetImageForPicture(picture_buffer);
160 162
161 // See if there is a media codec buffer still attached to this image. 163 // See if there is a media codec buffer still attached to this image.
162 const int32_t codec_buffer = avImage->GetMediaCodecBufferIndex(); 164 const int32_t codec_buffer = avda_image->GetMediaCodecBufferIndex();
163 165
164 if (codec_buffer >= 0) { 166 if (codec_buffer >= 0) {
165 // PictureBuffer wasn't displayed, so release the buffer. 167 // PictureBuffer wasn't displayed, so release the buffer.
166 media_codec_->ReleaseOutputBuffer(codec_buffer, false); 168 media_codec_->ReleaseOutputBuffer(codec_buffer, false);
167 avImage->SetMediaCodecBufferIndex(-1); 169 avda_image->SetMediaCodecBufferIndex(-1);
168 } 170 }
169 } 171 }
170 172
171 void AndroidDeferredRenderingBackingStrategy::ReuseOnePictureBuffer( 173 void AndroidDeferredRenderingBackingStrategy::ReuseOnePictureBuffer(
172 const media::PictureBuffer& picture_buffer) { 174 const media::PictureBuffer& picture_buffer) {
173 // At this point, the CC must be done with the picture. We can't really 175 // 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, 176 // 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 177 // when it waits on the sync point before releasing the mailbox. That sync
176 // point is inserted by destroying the resource in VideoLayerImpl::DidDraw. 178 // point is inserted by destroying the resource in VideoLayerImpl::DidDraw.
177 ReleaseCodecBufferForPicture(picture_buffer); 179 ReleaseCodecBufferForPicture(picture_buffer);
(...skipping 11 matching lines...) Expand all
189 SetImageForPicture(picture_buffer, nullptr); 191 SetImageForPicture(picture_buffer, nullptr);
190 } 192 }
191 193
192 void AndroidDeferredRenderingBackingStrategy::CodecChanged( 194 void AndroidDeferredRenderingBackingStrategy::CodecChanged(
193 media::VideoCodecBridge* codec, 195 media::VideoCodecBridge* codec,
194 const AndroidVideoDecodeAccelerator::OutputBufferMap& buffers) { 196 const AndroidVideoDecodeAccelerator::OutputBufferMap& buffers) {
195 // Clear any outstanding codec buffer indices, since the new codec (if any) 197 // Clear any outstanding codec buffer indices, since the new codec (if any)
196 // doesn't know about them. 198 // doesn't know about them.
197 media_codec_ = codec; 199 media_codec_ = codec;
198 for (const std::pair<int, media::PictureBuffer>& entry : buffers) { 200 for (const std::pair<int, media::PictureBuffer>& entry : buffers) {
199 AVDACodecImage* avImage = GetImageForPicture(entry.second); 201 AVDACodecImage* avda_image = GetImageForPicture(entry.second);
200 avImage->SetMediaCodec(codec); 202 avda_image->SetMediaCodec(codec);
201 avImage->SetMediaCodecBufferIndex(-1); 203 avda_image->SetMediaCodecBufferIndex(-1);
202 } 204 }
203 } 205 }
204 206
205 void AndroidDeferredRenderingBackingStrategy::OnFrameAvailable() { 207 void AndroidDeferredRenderingBackingStrategy::OnFrameAvailable() {
206 shared_state_->SignalFrameAvailable(); 208 shared_state_->SignalFrameAvailable();
207 } 209 }
208 210
209 } // namespace content 211 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698