OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 "content/common/gpu/media/android_copying_backing_strategy.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/metrics/histogram.h" |
| 11 #include "content/common/gpu/gpu_channel.h" |
| 12 #include "content/common/gpu/media/avda_return_on_failure.h" |
| 13 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" |
| 14 #include "media/base/bitstream_buffer.h" |
| 15 #include "media/base/limits.h" |
| 16 #include "media/base/video_decoder_config.h" |
| 17 #include "media/video/picture.h" |
| 18 #include "ui/gl/android/surface_texture.h" |
| 19 #include "ui/gl/gl_bindings.h" |
| 20 |
| 21 namespace content { |
| 22 |
| 23 // TODO(dwkang): We only need kMaxVideoFrames to pass media stack's prerolling |
| 24 // phase, but 1 is added due to crbug.com/176036. This should be tuned when we |
| 25 // have actual use case. |
| 26 enum { kNumPictureBuffers = media::limits::kMaxVideoFrames + 1 }; |
| 27 |
| 28 AndroidCopyingBackingStrategy::AndroidCopyingBackingStrategy() |
| 29 : state_provider_(nullptr) { |
| 30 } |
| 31 |
| 32 AndroidCopyingBackingStrategy::~AndroidCopyingBackingStrategy() { |
| 33 } |
| 34 |
| 35 void AndroidCopyingBackingStrategy::SetStateProvider( |
| 36 AndroidVideoDecodeAccelerator::StateProvider* state_provider) |
| 37 { |
| 38 state_provider_ = state_provider; |
| 39 } |
| 40 |
| 41 void AndroidCopyingBackingStrategy::Cleanup() { |
| 42 DCHECK(state_provider_->ThreadChecker().CalledOnValidThread()); |
| 43 if (copier_) |
| 44 copier_->Destroy(); |
| 45 } |
| 46 |
| 47 uint32 AndroidCopyingBackingStrategy::GetNumPictureBuffers() const { |
| 48 return kNumPictureBuffers; |
| 49 } |
| 50 |
| 51 uint32 AndroidCopyingBackingStrategy::GetTextureTarget() const { |
| 52 return GL_TEXTURE_2D; |
| 53 } |
| 54 |
| 55 void AndroidCopyingBackingStrategy::AssignCurrentSurfaceToPictureBuffer( |
| 56 int32 codec_buf_index, const media::PictureBuffer& picture_buffer) { |
| 57 |
| 58 // Make sure that the decoder is available. |
| 59 RETURN_ON_FAILURE(state_provider_, |
| 60 state_provider_->GetGlDecoder(), |
| 61 "Failed to get gles2 decoder instance.", |
| 62 ILLEGAL_STATE); |
| 63 |
| 64 // Render the codec buffer into |surface_texture_|, and switch it to be |
| 65 // the front buffer. |
| 66 // This ignores the emitted ByteBuffer and instead relies on rendering to |
| 67 // the codec's SurfaceTexture and then copying from that texture to the |
| 68 // client's PictureBuffer's texture. This means that each picture's data |
| 69 // is written three times: once to the ByteBuffer, once to the |
| 70 // SurfaceTexture, and once to the client's texture. It would be nicer to |
| 71 // either: |
| 72 // 1) Render directly to the client's texture from MediaCodec (one write); |
| 73 // or |
| 74 // 2) Upload the ByteBuffer to the client's texture (two writes). |
| 75 // Unfortunately neither is possible: |
| 76 // 1) MediaCodec's use of SurfaceTexture is a singleton, and the texture |
| 77 // written to can't change during the codec's lifetime. b/11990461 |
| 78 // 2) The ByteBuffer is likely to contain the pixels in a vendor-specific, |
| 79 // opaque/non-standard format. It's not possible to negotiate the |
| 80 // decoder to emit a specific colorspace, even using HW CSC. b/10706245 |
| 81 // So, we live with these two extra copies per picture :( |
| 82 state_provider_->GetMediaCodec()->ReleaseOutputBuffer(codec_buf_index, |
| 83 true); |
| 84 |
| 85 gfx::SurfaceTexture* surface_texture = state_provider_->GetSurfaceTexture(); |
| 86 surface_texture->UpdateTexImage(); |
| 87 |
| 88 float transfrom_matrix[16]; |
| 89 surface_texture->GetTransformMatrix(transfrom_matrix); |
| 90 |
| 91 uint32 picture_buffer_texture_id = picture_buffer.texture_id(); |
| 92 |
| 93 // Defer initializing the CopyTextureCHROMIUMResourceManager until it is |
| 94 // needed because it takes 10s of milliseconds to initialize. |
| 95 if (!copier_) { |
| 96 copier_.reset(new gpu::CopyTextureCHROMIUMResourceManager()); |
| 97 copier_->Initialize(state_provider_->GetGlDecoder()); |
| 98 } |
| 99 |
| 100 // Here, we copy |surface_texture_id_| to the picture buffer instead of |
| 101 // setting new texture to |surface_texture_| by calling attachToGLContext() |
| 102 // because: |
| 103 // 1. Once we call detachFrameGLContext(), it deletes the texture previous |
| 104 // attached. |
| 105 // 2. SurfaceTexture requires us to apply a transform matrix when we show |
| 106 // the texture. |
| 107 // TODO(hkuang): get the StreamTexture transform matrix in GPU process |
| 108 // instead of using default matrix crbug.com/226218. |
| 109 const static GLfloat default_matrix[16] = {1.0f, 0.0f, 0.0f, 0.0f, |
| 110 0.0f, 1.0f, 0.0f, 0.0f, |
| 111 0.0f, 0.0f, 1.0f, 0.0f, |
| 112 0.0f, 0.0f, 0.0f, 1.0f}; |
| 113 copier_->DoCopyTextureWithTransform(state_provider_->GetGlDecoder(), |
| 114 GL_TEXTURE_EXTERNAL_OES, state_provider_->GetSurfaceTextureId(), |
| 115 GetTextureTarget(), picture_buffer_texture_id, |
| 116 GL_RGBA, GL_UNSIGNED_BYTE, |
| 117 state_provider_->GetSize().width(), |
| 118 state_provider_->GetSize().height(), |
| 119 false, false, false, nullptr, default_matrix); |
| 120 } |
| 121 |
| 122 } // namespace content |
OLD | NEW |