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 Initialize(PP_Resource graphics_context
,
52 PP_VideoProfile profile
,
53 PP_HardwareAcceleration acceleration
,
54 scoped_refptr
<TrackedCallback
> callback
) override
;
55 int32_t Decode(uint32_t decode_id
,
58 scoped_refptr
<TrackedCallback
> callback
) override
;
59 int32_t GetPicture0_1(
60 PP_VideoPicture_0_1
* picture
,
61 scoped_refptr
<TrackedCallback
> callback
) override
;
62 int32_t GetPicture(PP_VideoPicture
* picture
,
63 scoped_refptr
<TrackedCallback
> callback
) override
;
64 void RecyclePicture(const PP_VideoPicture
* picture
) override
;
65 int32_t Flush(scoped_refptr
<TrackedCallback
> callback
) override
;
66 int32_t Reset(scoped_refptr
<TrackedCallback
> callback
) override
;
68 // PluginResource implementation.
69 void OnReplyReceived(const ResourceMessageReplyParams
& params
,
70 const IPC::Message
& msg
) override
;
72 // Called only by unit tests. This bypasses Graphics3D setup, which doesn't
73 // work in ppapi::proxy::PluginProxyTest.
77 // Struct to hold a shared memory buffer.
79 ShmBuffer(scoped_ptr
<base::SharedMemory
> shm
,
84 const scoped_ptr
<base::SharedMemory
> shm
;
86 // Index into shm_buffers_ vector, used as an id. This should map 1:1 to
87 // the index on the host side of the proxy.
88 const uint32_t shm_id
;
91 // Struct to hold texture information.
93 Texture(uint32_t texture_target
, const PP_Size
& size
);
96 const uint32_t texture_target
;
100 // Struct to hold a picture received from the decoder.
102 Picture(int32_t decode_id
,
104 const PP_Rect
& visible_rect
);
109 PP_Rect visible_rect
;
112 // Unsolicited reply message handlers.
113 void OnPluginMsgRequestTextures(const ResourceMessageReplyParams
& params
,
114 uint32_t num_textures
,
116 uint32_t texture_target
,
117 const std::vector
<gpu::Mailbox
>& mailboxes
);
118 void OnPluginMsgPictureReady(const ResourceMessageReplyParams
& params
,
121 const PP_Rect
& visible_rect
);
122 void OnPluginMsgDismissPicture(const ResourceMessageReplyParams
& params
,
123 uint32_t texture_id
);
124 void OnPluginMsgNotifyError(const ResourceMessageReplyParams
& params
,
127 // Reply message handlers for operations that are done in the host.
128 void OnPluginMsgInitializeComplete(const ResourceMessageReplyParams
& params
);
129 void OnPluginMsgDecodeComplete(const ResourceMessageReplyParams
& params
,
131 void OnPluginMsgFlushComplete(const ResourceMessageReplyParams
& params
);
132 void OnPluginMsgResetComplete(const ResourceMessageReplyParams
& params
);
134 void RunCallbackWithError(scoped_refptr
<TrackedCallback
>* callback
);
135 void DeleteGLTexture(uint32_t texture_id
);
136 void WriteNextPicture();
138 // ScopedVector to own the shared memory buffers.
139 ScopedVector
<ShmBuffer
> shm_buffers_
;
141 // List of available shared memory buffers.
142 typedef std::vector
<ShmBuffer
*> ShmBufferList
;
143 ShmBufferList available_shm_buffers_
;
145 // Map of GL texture id to texture info.
146 typedef base::hash_map
<uint32_t, Texture
> TextureMap
;
147 TextureMap textures_
;
149 // Queue of received pictures.
150 typedef std::queue
<Picture
> PictureQueue
;
151 PictureQueue received_pictures_
;
153 // Pending callbacks.
154 scoped_refptr
<TrackedCallback
> initialize_callback_
;
155 scoped_refptr
<TrackedCallback
> decode_callback_
;
156 scoped_refptr
<TrackedCallback
> get_picture_callback_
;
157 scoped_refptr
<TrackedCallback
> flush_callback_
;
158 scoped_refptr
<TrackedCallback
> reset_callback_
;
160 // Number of Decode calls made, mod 2^31, to serve as a uid for each decode.
161 int32_t num_decodes_
;
162 // The maximum delay (in Decode calls) before we receive a picture. If we
163 // haven't received a picture from a Decode call after this many successive
164 // calls to Decode, then we will never receive a picture from the call.
165 // Note that this isn't guaranteed by H264 or other codecs. In practice, this
166 // number is less than 16. Make it much larger just to be safe.
167 // NOTE: because we count decodes mod 2^31, this value must be a power of 2.
168 static const int kMaximumPictureDelay
= 128;
169 uint32_t decode_ids_
[kMaximumPictureDelay
];
171 // State for pending get_picture_callback_.
172 PP_VideoPicture
* get_picture_
;
173 PP_VideoPicture_0_1
* get_picture_0_1_
;
175 ScopedPPResource graphics3d_
;
176 gpu::gles2::GLES2Implementation
* gles2_impl_
;
180 int32_t decoder_last_error_
;
182 DISALLOW_COPY_AND_ASSIGN(VideoDecoderResource
);
188 #endif // PPAPI_PROXY_VIDEO_DECODER_RESOURCE_H_