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 #ifndef MEDIA_GPU_AVDA_SHARED_STATE_H_ | 5 #ifndef MEDIA_GPU_AVDA_SHARED_STATE_H_ |
6 #define MEDIA_GPU_AVDA_SHARED_STATE_H_ | 6 #define MEDIA_GPU_AVDA_SHARED_STATE_H_ |
7 | 7 |
8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
9 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" | 9 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" |
10 #include "media/base/android/media_codec_bridge.h" | 10 #include "media/base/android/media_codec_bridge.h" |
11 #include "media/base/android/sdk_media_codec_bridge.h" | 11 #include "ui/gl/gl_bindings.h" |
12 #include "ui/gl/gl_context.h" | 12 #include "ui/gl/gl_context.h" |
13 #include "ui/gl/gl_image.h" | 13 #include "ui/gl/gl_image.h" |
14 #include "ui/gl/gl_surface.h" | 14 #include "ui/gl/gl_surface.h" |
15 | 15 |
16 namespace gl { | 16 namespace gl { |
17 class SurfaceTexture; | 17 class SurfaceTexture; |
18 } | 18 } |
19 | 19 |
20 namespace media { | 20 namespace media { |
21 | 21 |
22 class AVDACodecImage; | 22 class AVDACodecImage; |
23 class MediaCodecBridge; | |
24 | 23 |
25 // Shared state to allow communication between the AVDA and the | 24 // Shared state to allow communication between the AVDA and the |
26 // GLImages that configure GL for drawing the frames. | 25 // GLImages that configure GL for drawing the frames. |
27 class AVDASharedState : public base::RefCounted<AVDASharedState> { | 26 class AVDASharedState : public base::RefCounted<AVDASharedState> { |
28 public: | 27 public: |
29 AVDASharedState(); | 28 AVDASharedState(); |
30 | 29 |
31 GLint surface_texture_service_id() const { | 30 GLuint surface_texture_service_id() const { |
32 return surface_texture_service_id_; | 31 return surface_texture_service_id_; |
33 } | 32 } |
34 | 33 |
35 // Set the SurfaceTexture's client texture name, which the SurfaceTexture | |
36 // might not know about yet (see surface_texture_is_attached()). | |
37 void set_surface_texture_service_id(GLint id) { | |
38 surface_texture_service_id_ = id; | |
39 } | |
40 | |
41 // Signal the "frame available" event. This may be called from any thread. | 34 // Signal the "frame available" event. This may be called from any thread. |
42 void SignalFrameAvailable(); | 35 void SignalFrameAvailable(); |
43 | 36 |
44 void WaitForFrameAvailable(); | 37 void WaitForFrameAvailable(); |
45 | 38 |
46 // Context that the surface texture is bound to, or nullptr if it is not in | 39 void SetSurfaceTexture(scoped_refptr<gl::SurfaceTexture> surface_texture, |
47 // the attached state. | 40 GLuint attached_service_id); |
| 41 |
| 42 // Context and surface that |surface_texture_| is bound to, if |
| 43 // |surface_texture_| is not null. |
48 gl::GLContext* context() const { return context_.get(); } | 44 gl::GLContext* context() const { return context_.get(); } |
49 | 45 |
50 gl::GLSurface* surface() const { return surface_.get(); } | 46 gl::GLSurface* surface() const { return surface_.get(); } |
51 | 47 |
52 bool surface_texture_is_attached() const { | |
53 return surface_texture_is_attached_; | |
54 } | |
55 | |
56 // Iterates over all known codec images and updates the MediaCodec attached to | 48 // Iterates over all known codec images and updates the MediaCodec attached to |
57 // each one. | 49 // each one. |
58 void CodecChanged(media::MediaCodecBridge* codec); | 50 void CodecChanged(media::MediaCodecBridge* codec); |
59 | 51 |
60 // Methods for finding and updating the AVDACodecImage associated with a given | 52 // Methods for finding and updating the AVDACodecImage associated with a given |
61 // picture buffer id. GetImageForPicture() will return null for unknown ids. | 53 // picture buffer id. GetImageForPicture() will return null for unknown ids. |
62 // Calling SetImageForPicture() with a nullptr will erase the entry. | 54 // Calling SetImageForPicture() with a nullptr will erase the entry. |
63 void SetImageForPicture(int picture_buffer_id, AVDACodecImage* image); | 55 void SetImageForPicture(int picture_buffer_id, AVDACodecImage* image); |
64 AVDACodecImage* GetImageForPicture(int picture_buffer_id) const; | 56 AVDACodecImage* GetImageForPicture(int picture_buffer_id) const; |
65 | 57 |
66 // TODO(liberato): move the surface texture here and make these calls | |
67 // attach / detach it also. There are several changes going on in avda | |
68 // concurrently, so I don't want to change that until the dust settles. | |
69 // AVDACodecImage would no longer hold the surface texture. | |
70 | |
71 // Call this when the SurfaceTexture is attached to a GL context. This will | |
72 // update surface_texture_is_attached(), and set the context() and surface() | |
73 // to match. | |
74 void DidAttachSurfaceTexture(); | |
75 | |
76 // Helper method for coordinating the interactions between | 58 // Helper method for coordinating the interactions between |
77 // MediaCodec::ReleaseOutputBuffer() and WaitForFrameAvailable() when | 59 // MediaCodec::ReleaseOutputBuffer() and WaitForFrameAvailable() when |
78 // rendering to a SurfaceTexture; this method should never be called when | 60 // rendering to a SurfaceTexture; this method should never be called when |
79 // rendering to a SurfaceView. | 61 // rendering to a SurfaceView. |
80 // | 62 // |
81 // The release of the codec buffer to the surface texture is asynchronous, by | 63 // The release of the codec buffer to the surface texture is asynchronous, by |
82 // using this helper we can attempt to let this process complete in a non | 64 // using this helper we can attempt to let this process complete in a non |
83 // blocking fashion before the SurfaceTexture is used. | 65 // blocking fashion before the SurfaceTexture is used. |
84 // | 66 // |
85 // Clients should call this method to release the codec buffer for rendering | 67 // Clients should call this method to release the codec buffer for rendering |
86 // and then call WaitForFrameAvailable() before using the SurfaceTexture. In | 68 // and then call WaitForFrameAvailable() before using the SurfaceTexture. In |
87 // the ideal case the SurfaceTexture has already been updated, otherwise the | 69 // the ideal case the SurfaceTexture has already been updated, otherwise the |
88 // method will wait for a pro-rated amount of time based on elapsed time up | 70 // method will wait for a pro-rated amount of time based on elapsed time up |
89 // to a short deadline. | 71 // to a short deadline. |
90 // | 72 // |
91 // Some devices do not reliably notify frame availability, so we use a very | 73 // Some devices do not reliably notify frame availability, so we use a very |
92 // short deadline of only a few milliseconds to avoid indefinite stalls. | 74 // short deadline of only a few milliseconds to avoid indefinite stalls. |
93 void RenderCodecBufferToSurfaceTexture(media::MediaCodecBridge* codec, | 75 void RenderCodecBufferToSurfaceTexture(media::MediaCodecBridge* codec, |
94 int codec_buffer_index); | 76 int codec_buffer_index); |
95 | 77 |
96 protected: | 78 protected: |
97 virtual ~AVDASharedState(); | 79 virtual ~AVDASharedState(); |
98 | 80 |
99 private: | 81 private: |
100 friend class base::RefCounted<AVDASharedState>; | 82 friend class base::RefCounted<AVDASharedState>; |
101 | 83 |
102 // Platform gl texture Id for |surface_texture_|. This will be zero if | 84 scoped_refptr<gl::SurfaceTexture> surface_texture_; |
103 // and only if |texture_owner_| is null. | 85 |
104 // TODO(liberato): This should be GLuint, but we don't seem to have the type. | 86 // Platform gl texture id for |surface_texture_|. |
105 GLint surface_texture_service_id_; | 87 GLuint surface_texture_service_id_; |
106 | 88 |
107 // For signalling OnFrameAvailable(). | 89 // For signalling OnFrameAvailable(). |
108 base::WaitableEvent frame_available_event_; | 90 base::WaitableEvent frame_available_event_; |
109 | 91 |
110 // True if and only if the surface texture is currently attached. | 92 // Context and surface that |surface_texture_| is bound to, if |
111 bool surface_texture_is_attached_; | 93 // |surface_texture_| is not null. |
112 | |
113 // Context and surface that the surface texture is attached to, if it is | |
114 // currently attached. | |
115 scoped_refptr<gl::GLContext> context_; | 94 scoped_refptr<gl::GLContext> context_; |
116 scoped_refptr<gl::GLSurface> surface_; | 95 scoped_refptr<gl::GLSurface> surface_; |
117 | 96 |
118 // Maps a picture buffer id to a AVDACodecImage. | 97 // Maps a picture buffer id to a AVDACodecImage. |
119 std::map<int, AVDACodecImage*> codec_images_; | 98 std::map<int, AVDACodecImage*> codec_images_; |
120 | 99 |
121 // The time of the last call to RenderCodecBufferToSurfaceTexture(), null if | 100 // The time of the last call to RenderCodecBufferToSurfaceTexture(), null if |
122 // if there has been no last call or WaitForFrameAvailable() has been called | 101 // if there has been no last call or WaitForFrameAvailable() has been called |
123 // since the last call. | 102 // since the last call. |
124 base::TimeTicks release_time_; | 103 base::TimeTicks release_time_; |
125 | 104 |
126 DISALLOW_COPY_AND_ASSIGN(AVDASharedState); | 105 DISALLOW_COPY_AND_ASSIGN(AVDASharedState); |
127 }; | 106 }; |
128 | 107 |
129 } // namespace media | 108 } // namespace media |
130 | 109 |
131 #endif // MEDIA_GPU_AVDA_SHARED_STATE_H_ | 110 #endif // MEDIA_GPU_AVDA_SHARED_STATE_H_ |
OLD | NEW |