| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/gpu/android_copying_backing_strategy.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/trace_event/trace_event.h" | |
| 10 #include "gpu/command_buffer/service/context_group.h" | |
| 11 #include "gpu/command_buffer/service/gles2_cmd_copy_texture_chromium.h" | |
| 12 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" | |
| 13 #include "media/base/limits.h" | |
| 14 #include "media/gpu/avda_return_on_failure.h" | |
| 15 #include "media/video/picture.h" | |
| 16 #include "ui/gl/android/surface_texture.h" | |
| 17 #include "ui/gl/gl_bindings.h" | |
| 18 | |
| 19 namespace media { | |
| 20 | |
| 21 AndroidCopyingBackingStrategy::AndroidCopyingBackingStrategy( | |
| 22 AVDAStateProvider* state_provider) | |
| 23 : state_provider_(state_provider), | |
| 24 surface_texture_id_(0), | |
| 25 media_codec_(nullptr) {} | |
| 26 | |
| 27 AndroidCopyingBackingStrategy::~AndroidCopyingBackingStrategy() {} | |
| 28 | |
| 29 gl::ScopedJavaSurface AndroidCopyingBackingStrategy::Initialize( | |
| 30 int surface_view_id) { | |
| 31 if (surface_view_id != VideoDecodeAccelerator::Config::kNoSurfaceID) { | |
| 32 LOG(ERROR) << "The copying strategy should not be initialized with a " | |
| 33 "surface id."; | |
| 34 return gl::ScopedJavaSurface(); | |
| 35 } | |
| 36 | |
| 37 surface_texture_ = | |
| 38 state_provider_->CreateAttachedSurfaceTexture(&surface_texture_id_); | |
| 39 return gl::ScopedJavaSurface(surface_texture_.get()); | |
| 40 } | |
| 41 | |
| 42 void AndroidCopyingBackingStrategy::BeginCleanup( | |
| 43 bool have_context, | |
| 44 const AndroidVideoDecodeAccelerator::OutputBufferMap& buffers) { | |
| 45 DCHECK(state_provider_->ThreadChecker().CalledOnValidThread()); | |
| 46 | |
| 47 if (copier_) | |
| 48 copier_->Destroy(); | |
| 49 | |
| 50 if (surface_texture_id_ && have_context) | |
| 51 glDeleteTextures(1, &surface_texture_id_); | |
| 52 } | |
| 53 | |
| 54 void AndroidCopyingBackingStrategy::EndCleanup() {} | |
| 55 | |
| 56 scoped_refptr<gl::SurfaceTexture> | |
| 57 AndroidCopyingBackingStrategy::GetSurfaceTexture() const { | |
| 58 return surface_texture_; | |
| 59 } | |
| 60 | |
| 61 uint32_t AndroidCopyingBackingStrategy::GetTextureTarget() const { | |
| 62 return GL_TEXTURE_2D; | |
| 63 } | |
| 64 | |
| 65 gfx::Size AndroidCopyingBackingStrategy::GetPictureBufferSize() const { | |
| 66 return state_provider_->GetSize(); | |
| 67 } | |
| 68 | |
| 69 void AndroidCopyingBackingStrategy::UseCodecBufferForPictureBuffer( | |
| 70 int32_t codec_buf_index, | |
| 71 const PictureBuffer& picture_buffer) { | |
| 72 // Make sure that the decoder is available. | |
| 73 RETURN_ON_FAILURE(state_provider_, state_provider_->GetGlDecoder().get(), | |
| 74 "Failed to get gles2 decoder instance.", ILLEGAL_STATE); | |
| 75 | |
| 76 // Render the codec buffer into |surface_texture_|, and switch it to be | |
| 77 // the front buffer. | |
| 78 // This ignores the emitted ByteBuffer and instead relies on rendering to | |
| 79 // the codec's SurfaceTexture and then copying from that texture to the | |
| 80 // client's PictureBuffer's texture. This means that each picture's data | |
| 81 // is written three times: once to the ByteBuffer, once to the | |
| 82 // SurfaceTexture, and once to the client's texture. It would be nicer to | |
| 83 // either: | |
| 84 // 1) Render directly to the client's texture from MediaCodec (one write); | |
| 85 // or | |
| 86 // 2) Upload the ByteBuffer to the client's texture (two writes). | |
| 87 // Unfortunately neither is possible: | |
| 88 // 1) MediaCodec's use of SurfaceTexture is a singleton, and the texture | |
| 89 // written to can't change during the codec's lifetime. b/11990461 | |
| 90 // 2) The ByteBuffer is likely to contain the pixels in a vendor-specific, | |
| 91 // opaque/non-standard format. It's not possible to negotiate the | |
| 92 // decoder to emit a specific colorspace, even using HW CSC. b/10706245 | |
| 93 // So, we live with these two extra copies per picture :( | |
| 94 { | |
| 95 TRACE_EVENT0("media", "AVDA::ReleaseOutputBuffer"); | |
| 96 media_codec_->ReleaseOutputBuffer(codec_buf_index, true); | |
| 97 } | |
| 98 | |
| 99 { | |
| 100 TRACE_EVENT0("media", "AVDA::UpdateTexImage"); | |
| 101 surface_texture_->UpdateTexImage(); | |
| 102 } | |
| 103 | |
| 104 float transform_matrix[16]; | |
| 105 surface_texture_->GetTransformMatrix(transform_matrix); | |
| 106 | |
| 107 DCHECK_LE(1u, picture_buffer.texture_ids().size()); | |
| 108 uint32_t picture_buffer_texture_id = picture_buffer.texture_ids()[0]; | |
| 109 | |
| 110 // Defer initializing the CopyTextureCHROMIUMResourceManager until it is | |
| 111 // needed because it takes 10s of milliseconds to initialize. | |
| 112 if (!copier_) { | |
| 113 copier_.reset(new gpu::CopyTextureCHROMIUMResourceManager()); | |
| 114 copier_->Initialize(state_provider_->GetGlDecoder().get(), | |
| 115 state_provider_->GetGlDecoder() | |
| 116 ->GetContextGroup() | |
| 117 ->feature_info() | |
| 118 ->feature_flags()); | |
| 119 } | |
| 120 | |
| 121 // Here, we copy |surface_texture_id_| to the picture buffer instead of | |
| 122 // setting new texture to |surface_texture_| by calling attachToGLContext() | |
| 123 // because: | |
| 124 // 1. Once we call detachFrameGLContext(), it deletes the texture previously | |
| 125 // attached. | |
| 126 // 2. SurfaceTexture requires us to apply a transform matrix when we show | |
| 127 // the texture. | |
| 128 copier_->DoCopyTextureWithTransform( | |
| 129 state_provider_->GetGlDecoder().get(), GL_TEXTURE_EXTERNAL_OES, | |
| 130 surface_texture_id_, GL_TEXTURE_2D, picture_buffer_texture_id, | |
| 131 state_provider_->GetSize().width(), state_provider_->GetSize().height(), | |
| 132 true, false, false, transform_matrix); | |
| 133 } | |
| 134 | |
| 135 void AndroidCopyingBackingStrategy::CodecChanged(VideoCodecBridge* codec) { | |
| 136 media_codec_ = codec; | |
| 137 } | |
| 138 | |
| 139 void AndroidCopyingBackingStrategy::OnFrameAvailable() { | |
| 140 // TODO(liberato): crbug.com/574948 . The OnFrameAvailable logic can be | |
| 141 // moved into AVDA, and we should wait for it before doing the copy. | |
| 142 // Because there were some test failures, we don't do this now but | |
| 143 // instead preserve the old behavior. | |
| 144 } | |
| 145 | |
| 146 bool AndroidCopyingBackingStrategy::ArePicturesOverlayable() { | |
| 147 return false; | |
| 148 } | |
| 149 | |
| 150 void AndroidCopyingBackingStrategy::UpdatePictureBufferSize( | |
| 151 PictureBuffer* picture_buffer, | |
| 152 const gfx::Size& new_size) { | |
| 153 // This strategy uses 2D textures who's allocated memory is dependent on the | |
| 154 // size. To update size in all places, we must: | |
| 155 // 1) Update the PictureBuffer meta-data | |
| 156 picture_buffer->set_size(new_size); | |
| 157 | |
| 158 // 2) Update the GL texture via glTexImage2D. This step assumes the caller | |
| 159 // has made our GL context current. | |
| 160 DCHECK_LE(1u, picture_buffer->texture_ids().size()); | |
| 161 glBindTexture(GL_TEXTURE_2D, picture_buffer->texture_ids()[0]); | |
| 162 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, new_size.width(), new_size.height(), | |
| 163 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); | |
| 164 state_provider_->GetGlDecoder()->RestoreActiveTextureUnitBinding( | |
| 165 GL_TEXTURE_2D); | |
| 166 | |
| 167 // 3) Update the CHROMIUM Texture's size. | |
| 168 gpu::gles2::TextureRef* texture_ref = | |
| 169 state_provider_->GetTextureForPicture(*picture_buffer); | |
| 170 RETURN_IF_NULL(texture_ref); | |
| 171 gpu::gles2::TextureManager* texture_manager = | |
| 172 state_provider_->GetGlDecoder()->GetContextGroup()->texture_manager(); | |
| 173 RETURN_IF_NULL(texture_manager); | |
| 174 texture_manager->SetLevelInfo(texture_ref, GetTextureTarget(), 0, GL_RGBA, | |
| 175 new_size.width(), new_size.height(), 1, 0, | |
| 176 GL_RGBA, GL_UNSIGNED_BYTE, gfx::Rect(new_size)); | |
| 177 } | |
| 178 | |
| 179 } // namespace media | |
| OLD | NEW |