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