1 // Copyright (c) 2014 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.
5 #ifndef PPAPI_PROXY_VIDEO_DECODER_RESOURCE_H_
6 #define PPAPI_PROXY_VIDEO_DECODER_RESOURCE_H_
10 #include "base/containers/hash_tables.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "ppapi/proxy/connection.h"
15 #include "ppapi/proxy/plugin_resource.h"
16 #include "ppapi/proxy/ppapi_proxy_export.h"
17 #include "ppapi/shared_impl/resource.h"
18 #include "ppapi/shared_impl/scoped_pp_resource.h"
19 #include "ppapi/thunk/ppb_video_decoder_api.h"
24 class GLES2Implementation
;
30 class PPB_Graphics3D_Shared
;
31 class TrackedCallback
;
35 class PPAPI_PROXY_EXPORT VideoDecoderResource
36 : public PluginResource
,
37 public thunk::PPB_VideoDecoder_API
{
39 VideoDecoderResource(Connection connection
, PP_Instance instance
);
40 ~VideoDecoderResource() override
;
42 // Resource overrides.
43 thunk::PPB_VideoDecoder_API
* AsPPB_VideoDecoder_API() override
;
45 // PPB_VideoDecoder_API implementation.
46 int32_t Initialize0_1(
47 PP_Resource graphics_context
,
48 PP_VideoProfile profile
,
49 PP_Bool allow_software_fallback
,
50 scoped_refptr
<TrackedCallback
> callback
) override
;
51 int32_t Initialize0_2(
52 PP_Resource graphics_context
,
53 PP_VideoProfile profile
,
54 PP_HardwareAcceleration acceleration
,
55 scoped_refptr
<TrackedCallback
> callback
) override
;
56 int32_t Initialize(PP_Resource graphics_context
,
57 PP_VideoProfile profile
,
58 PP_HardwareAcceleration acceleration
,
59 uint32_t min_picture_count
,
60 scoped_refptr
<TrackedCallback
> callback
) override
;
61 int32_t Decode(uint32_t decode_id
,
64 scoped_refptr
<TrackedCallback
> callback
) override
;
65 int32_t GetPicture0_1(
66 PP_VideoPicture_0_1
* picture
,
67 scoped_refptr
<TrackedCallback
> callback
) override
;
68 int32_t GetPicture(PP_VideoPicture
* picture
,
69 scoped_refptr
<TrackedCallback
> callback
) override
;
70 void RecyclePicture(const PP_VideoPicture
* picture
) override
;
71 int32_t Flush(scoped_refptr
<TrackedCallback
> callback
) override
;
72 int32_t Reset(scoped_refptr
<TrackedCallback
> callback
) override
;
74 // PluginResource implementation.
75 void OnReplyReceived(const ResourceMessageReplyParams
& params
,
76 const IPC::Message
& msg
) override
;
78 // Called only by unit tests. This bypasses Graphics3D setup, which doesn't
79 // work in ppapi::proxy::PluginProxyTest.
83 // Struct to hold a shared memory buffer.
85 ShmBuffer(scoped_ptr
<base::SharedMemory
> shm
,
90 const scoped_ptr
<base::SharedMemory
> shm
;
92 // Index into shm_buffers_ vector, used as an id. This should map 1:1 to
93 // the index on the host side of the proxy.
94 const uint32_t shm_id
;
97 // Struct to hold texture information.
99 Texture(uint32_t texture_target
, const PP_Size
& size
);
102 const uint32_t texture_target
;
106 // Struct to hold a picture received from the decoder.
108 Picture(int32_t decode_id
,
110 const PP_Rect
& visible_rect
);
115 PP_Rect visible_rect
;
118 // Unsolicited reply message handlers.
119 void OnPluginMsgRequestTextures(const ResourceMessageReplyParams
& params
,
120 uint32_t num_textures
,
122 uint32_t texture_target
,
123 const std::vector
<gpu::Mailbox
>& mailboxes
);
124 void OnPluginMsgPictureReady(const ResourceMessageReplyParams
& params
,
127 const PP_Rect
& visible_rect
);
128 void OnPluginMsgDismissPicture(const ResourceMessageReplyParams
& params
,
129 uint32_t texture_id
);
130 void OnPluginMsgNotifyError(const ResourceMessageReplyParams
& params
,
133 // Reply message handlers for operations that are done in the host.
134 void OnPluginMsgInitializeComplete(const ResourceMessageReplyParams
& params
);
135 void OnPluginMsgDecodeComplete(const ResourceMessageReplyParams
& params
,
137 void OnPluginMsgFlushComplete(const ResourceMessageReplyParams
& params
);
138 void OnPluginMsgResetComplete(const ResourceMessageReplyParams
& params
);
140 void RunCallbackWithError(scoped_refptr
<TrackedCallback
>* callback
);
141 void DeleteGLTexture(uint32_t texture_id
);
142 void WriteNextPicture();
144 // ScopedVector to own the shared memory buffers.
145 ScopedVector
<ShmBuffer
> shm_buffers_
;
147 // List of available shared memory buffers.
148 typedef std::vector
<ShmBuffer
*> ShmBufferList
;
149 ShmBufferList available_shm_buffers_
;
151 // Map of GL texture id to texture info.
152 typedef base::hash_map
<uint32_t, Texture
> TextureMap
;
153 TextureMap textures_
;
155 // Queue of received pictures.
156 typedef std::queue
<Picture
> PictureQueue
;
157 PictureQueue received_pictures_
;
159 // Pending callbacks.
160 scoped_refptr
<TrackedCallback
> initialize_callback_
;
161 scoped_refptr
<TrackedCallback
> decode_callback_
;
162 scoped_refptr
<TrackedCallback
> get_picture_callback_
;
163 scoped_refptr
<TrackedCallback
> flush_callback_
;
164 scoped_refptr
<TrackedCallback
> reset_callback_
;
166 // Number of Decode calls made, mod 2^31, to serve as a uid for each decode.
167 int32_t num_decodes_
;
168 // The maximum delay (in Decode calls) before we receive a picture. If we
169 // haven't received a picture from a Decode call after this many successive
170 // calls to Decode, then we will never receive a picture from the call.
171 // Note that this isn't guaranteed by H264 or other codecs. In practice, this
172 // number is less than 16. Make it much larger just to be safe.
173 // NOTE: because we count decodes mod 2^31, this value must be a power of 2.
174 static const int kMaximumPictureDelay
= 128;
175 uint32_t decode_ids_
[kMaximumPictureDelay
];
177 uint32_t min_picture_count_
;
179 // State for pending get_picture_callback_.
180 PP_VideoPicture
* get_picture_
;
181 PP_VideoPicture_0_1
* get_picture_0_1_
;
183 ScopedPPResource graphics3d_
;
184 gpu::gles2::GLES2Implementation
* gles2_impl_
;
188 int32_t decoder_last_error_
;
190 DISALLOW_COPY_AND_ASSIGN(VideoDecoderResource
);
196 #endif // PPAPI_PROXY_VIDEO_DECODER_RESOURCE_H_