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 "content/common/gpu/media/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 "content/common/gpu/media/avda_return_on_failure.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/video/picture.h" | |
15 #include "ui/gl/android/surface_texture.h" | |
16 #include "ui/gl/gl_bindings.h" | |
17 | |
18 namespace content { | |
19 | |
20 // TODO(liberato): It is unclear if we have an issue with deadlock during | |
21 // playback if we lower this. Previously (crbug.com/176036), a deadlock | |
22 // could occur during preroll. More recent tests have shown some | |
23 // instability with kNumPictureBuffers==2 with similar symptoms | |
24 // during playback. crbug.com/:531588 . | |
25 enum { kNumPictureBuffers = media::limits::kMaxVideoFrames + 1 }; | |
26 | |
27 AndroidCopyingBackingStrategy::AndroidCopyingBackingStrategy() | |
28 : state_provider_(nullptr) {} | |
29 | |
30 AndroidCopyingBackingStrategy::~AndroidCopyingBackingStrategy() {} | |
31 | |
32 void AndroidCopyingBackingStrategy::SetStateProvider( | |
33 AndroidVideoDecodeAcceleratorStateProvider* state_provider) { | |
34 state_provider_ = state_provider; | |
35 } | |
36 | |
37 void AndroidCopyingBackingStrategy::Cleanup() { | |
38 DCHECK(state_provider_->ThreadChecker().CalledOnValidThread()); | |
39 if (copier_) | |
40 copier_->Destroy(); | |
41 } | |
42 | |
43 uint32 AndroidCopyingBackingStrategy::GetNumPictureBuffers() const { | |
44 return kNumPictureBuffers; | |
45 } | |
46 | |
47 uint32 AndroidCopyingBackingStrategy::GetTextureTarget() const { | |
48 return GL_TEXTURE_2D; | |
49 } | |
50 | |
51 void AndroidCopyingBackingStrategy::AssignCurrentSurfaceToPictureBuffer( | |
52 int32 codec_buf_index, | |
53 const media::PictureBuffer& picture_buffer) { | |
54 // Make sure that the decoder is available. | |
55 RETURN_ON_FAILURE(state_provider_, state_provider_->GetGlDecoder(), | |
56 "Failed to get gles2 decoder instance.", ILLEGAL_STATE); | |
57 | |
58 // Render the codec buffer into |surface_texture_|, and switch it to be | |
59 // the front buffer. | |
60 // This ignores the emitted ByteBuffer and instead relies on rendering to | |
61 // the codec's SurfaceTexture and then copying from that texture to the | |
62 // client's PictureBuffer's texture. This means that each picture's data | |
63 // is written three times: once to the ByteBuffer, once to the | |
64 // SurfaceTexture, and once to the client's texture. It would be nicer to | |
65 // either: | |
66 // 1) Render directly to the client's texture from MediaCodec (one write); | |
67 // or | |
68 // 2) Upload the ByteBuffer to the client's texture (two writes). | |
69 // Unfortunately neither is possible: | |
70 // 1) MediaCodec's use of SurfaceTexture is a singleton, and the texture | |
71 // written to can't change during the codec's lifetime. b/11990461 | |
72 // 2) The ByteBuffer is likely to contain the pixels in a vendor-specific, | |
73 // opaque/non-standard format. It's not possible to negotiate the | |
74 // decoder to emit a specific colorspace, even using HW CSC. b/10706245 | |
75 // So, we live with these two extra copies per picture :( | |
76 { | |
77 TRACE_EVENT0("media", "AVDA::ReleaseOutputBuffer"); | |
78 state_provider_->GetMediaCodec()->ReleaseOutputBuffer(codec_buf_index, | |
79 true); | |
80 } | |
81 | |
82 gfx::SurfaceTexture* surface_texture = state_provider_->GetSurfaceTexture(); | |
83 { | |
84 TRACE_EVENT0("media", "AVDA::UpdateTexImage"); | |
85 surface_texture->UpdateTexImage(); | |
86 } | |
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, 0.0f, 1.0f, | |
DaleCurtis
2015/09/14 18:46:20
static const? Rename to kDefaultMatrix?
liberato (no reviews please)
2015/09/14 22:34:52
done, as kIdentityMatrix.
| |
110 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, | |
111 0.0f, 0.0f, 0.0f, 1.0f}; | |
112 copier_->DoCopyTextureWithTransform( | |
113 state_provider_->GetGlDecoder(), GL_TEXTURE_EXTERNAL_OES, | |
114 state_provider_->GetSurfaceTextureId(), picture_buffer_texture_id, | |
115 state_provider_->GetSize().width(), state_provider_->GetSize().height(), | |
116 false, false, false, default_matrix); | |
117 } | |
118 | |
119 } // namespace content | |
OLD | NEW |