1 // Copyright (c) 2012 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 #include "ppapi/proxy/ppb_video_decoder_proxy.h"
7 #include "base/logging.h"
8 #include "base/numerics/safe_conversions.h"
9 #include "gpu/command_buffer/client/gles2_implementation.h"
10 #include "ppapi/proxy/enter_proxy.h"
11 #include "ppapi/proxy/plugin_dispatcher.h"
12 #include "ppapi/proxy/ppapi_messages.h"
13 #include "ppapi/proxy/ppb_buffer_proxy.h"
14 #include "ppapi/proxy/ppb_graphics_3d_proxy.h"
15 #include "ppapi/thunk/enter.h"
16 #include "ppapi/thunk/resource_creation_api.h"
17 #include "ppapi/thunk/thunk.h"
19 using ppapi::thunk::EnterResourceNoLock
;
20 using ppapi::thunk::PPB_Buffer_API
;
21 using ppapi::thunk::PPB_Graphics3D_API
;
22 using ppapi::thunk::PPB_VideoDecoder_Dev_API
;
27 class VideoDecoder
: public PPB_VideoDecoder_Shared
{
29 // You must call Init() before using this class.
30 explicit VideoDecoder(const HostResource
& resource
);
31 virtual ~VideoDecoder();
33 static VideoDecoder
* Create(const HostResource
& resource
,
34 PP_Resource graphics_context
,
35 PP_VideoDecoder_Profile profile
);
37 // PPB_VideoDecoder_Dev_API implementation.
38 virtual int32_t Decode(const PP_VideoBitstreamBuffer_Dev
* bitstream_buffer
,
39 scoped_refptr
<TrackedCallback
> callback
) override
;
40 virtual void AssignPictureBuffers(
41 uint32_t no_of_buffers
, const PP_PictureBuffer_Dev
* buffers
) override
;
42 virtual void ReusePictureBuffer(int32_t picture_buffer_id
) override
;
43 virtual int32_t Flush(scoped_refptr
<TrackedCallback
> callback
) override
;
44 virtual int32_t Reset(scoped_refptr
<TrackedCallback
> callback
) override
;
45 virtual void Destroy() override
;
48 friend class PPB_VideoDecoder_Proxy
;
50 PluginDispatcher
* GetDispatcher() const;
52 // Run the callbacks that were passed into the plugin interface.
53 void FlushACK(int32_t result
);
54 void ResetACK(int32_t result
);
55 void EndOfBitstreamACK(int32_t buffer_id
, int32_t result
);
57 DISALLOW_COPY_AND_ASSIGN(VideoDecoder
);
60 VideoDecoder::VideoDecoder(const HostResource
& decoder
)
61 : PPB_VideoDecoder_Shared(decoder
) {
64 VideoDecoder::~VideoDecoder() {
66 PPB_VideoDecoder_Shared::Destroy();
69 int32_t VideoDecoder::Decode(
70 const PP_VideoBitstreamBuffer_Dev
* bitstream_buffer
,
71 scoped_refptr
<TrackedCallback
> callback
) {
72 EnterResourceNoLock
<PPB_Buffer_API
>
73 enter_buffer(bitstream_buffer
->data
, true);
74 if (enter_buffer
.failed())
75 return PP_ERROR_BADRESOURCE
;
77 if (!SetBitstreamBufferCallback(bitstream_buffer
->id
, callback
))
78 return PP_ERROR_BADARGUMENT
;
81 static_cast<Buffer
*>(enter_buffer
.object());
82 HostResource host_buffer
= ppb_buffer
->host_resource();
85 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Decode(
86 API_ID_PPB_VIDEO_DECODER_DEV
, host_resource(),
87 host_buffer
, bitstream_buffer
->id
,
88 bitstream_buffer
->size
));
89 return PP_OK_COMPLETIONPENDING
;
92 void VideoDecoder::AssignPictureBuffers(uint32_t no_of_buffers
,
93 const PP_PictureBuffer_Dev
* buffers
) {
94 std::vector
<PP_PictureBuffer_Dev
> buffer_list(
95 buffers
, buffers
+ no_of_buffers
);
97 GetDispatcher()->Send(
98 new PpapiHostMsg_PPBVideoDecoder_AssignPictureBuffers(
99 API_ID_PPB_VIDEO_DECODER_DEV
, host_resource(), buffer_list
));
102 void VideoDecoder::ReusePictureBuffer(int32_t picture_buffer_id
) {
103 FlushCommandBuffer();
104 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_ReusePictureBuffer(
105 API_ID_PPB_VIDEO_DECODER_DEV
, host_resource(), picture_buffer_id
));
108 int32_t VideoDecoder::Flush(scoped_refptr
<TrackedCallback
> callback
) {
109 if (!SetFlushCallback(callback
))
110 return PP_ERROR_INPROGRESS
;
112 FlushCommandBuffer();
113 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Flush(
114 API_ID_PPB_VIDEO_DECODER_DEV
, host_resource()));
115 return PP_OK_COMPLETIONPENDING
;
118 int32_t VideoDecoder::Reset(scoped_refptr
<TrackedCallback
> callback
) {
119 if (!SetResetCallback(callback
))
120 return PP_ERROR_INPROGRESS
;
122 FlushCommandBuffer();
123 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Reset(
124 API_ID_PPB_VIDEO_DECODER_DEV
, host_resource()));
125 return PP_OK_COMPLETIONPENDING
;
128 void VideoDecoder::Destroy() {
129 FlushCommandBuffer();
130 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Destroy(
131 API_ID_PPB_VIDEO_DECODER_DEV
, host_resource()));
132 PPB_VideoDecoder_Shared::Destroy();
135 PluginDispatcher
* VideoDecoder::GetDispatcher() const {
136 return PluginDispatcher::GetForResource(this);
139 void VideoDecoder::ResetACK(int32_t result
) {
140 RunResetCallback(result
);
143 void VideoDecoder::FlushACK(int32_t result
) {
144 RunFlushCallback(result
);
147 void VideoDecoder::EndOfBitstreamACK(
148 int32_t bitstream_buffer_id
, int32_t result
) {
149 RunBitstreamBufferCallback(bitstream_buffer_id
, result
);
152 PPB_VideoDecoder_Proxy::PPB_VideoDecoder_Proxy(Dispatcher
* dispatcher
)
153 : InterfaceProxy(dispatcher
),
154 callback_factory_(this) {
157 PPB_VideoDecoder_Proxy::~PPB_VideoDecoder_Proxy() {
160 bool PPB_VideoDecoder_Proxy::OnMessageReceived(const IPC::Message
& msg
) {
161 if (!dispatcher()->permissions().HasPermission(PERMISSION_DEV
))
165 IPC_BEGIN_MESSAGE_MAP(PPB_VideoDecoder_Proxy
, msg
)
166 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Create
,
168 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Decode
, OnMsgDecode
)
169 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_AssignPictureBuffers
,
170 OnMsgAssignPictureBuffers
)
171 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_ReusePictureBuffer
,
172 OnMsgReusePictureBuffer
)
173 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Flush
, OnMsgFlush
)
174 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Reset
, OnMsgReset
)
175 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Destroy
, OnMsgDestroy
)
176 IPC_MESSAGE_HANDLER(PpapiMsg_PPBVideoDecoder_ResetACK
, OnMsgResetACK
)
177 IPC_MESSAGE_HANDLER(PpapiMsg_PPBVideoDecoder_EndOfBitstreamACK
,
178 OnMsgEndOfBitstreamACK
)
179 IPC_MESSAGE_HANDLER(PpapiMsg_PPBVideoDecoder_FlushACK
, OnMsgFlushACK
)
180 IPC_MESSAGE_UNHANDLED(handled
= false)
181 IPC_END_MESSAGE_MAP()
186 PP_Resource
PPB_VideoDecoder_Proxy::CreateProxyResource(
187 PP_Instance instance
,
188 PP_Resource graphics_context
,
189 PP_VideoDecoder_Profile profile
) {
190 PluginDispatcher
* dispatcher
= PluginDispatcher::GetForInstance(instance
);
191 // Dispatcher is null if it cannot find the instance passed to it (i.e. if the
192 // client passes in an invalid instance).
196 if (!dispatcher
->preferences().is_accelerated_video_decode_enabled
)
199 EnterResourceNoLock
<PPB_Graphics3D_API
> enter_context(graphics_context
,
201 if (enter_context
.failed())
204 Graphics3D
* context
= static_cast<Graphics3D
*>(enter_context
.object());
207 dispatcher
->Send(new PpapiHostMsg_PPBVideoDecoder_Create(
208 API_ID_PPB_VIDEO_DECODER_DEV
, instance
,
209 context
->host_resource(), profile
, &result
));
210 if (result
.is_null())
213 // Need a scoped_refptr to keep the object alive during the Init call.
214 scoped_refptr
<VideoDecoder
> decoder(new VideoDecoder(result
));
215 decoder
->InitCommon(graphics_context
, context
->gles2_impl());
216 return decoder
->GetReference();
219 void PPB_VideoDecoder_Proxy::OnMsgCreate(
220 PP_Instance instance
, const HostResource
& graphics_context
,
221 PP_VideoDecoder_Profile profile
,
222 HostResource
* result
) {
223 thunk::EnterResourceCreation
resource_creation(instance
);
224 if (resource_creation
.failed())
227 // Make the resource and get the API pointer to its interface.
228 result
->SetHostResource(
229 instance
, resource_creation
.functions()->CreateVideoDecoderDev(
230 instance
, graphics_context
.host_resource(), profile
));
233 void PPB_VideoDecoder_Proxy::OnMsgDecode(
234 const HostResource
& decoder
,
235 const HostResource
& buffer
, int32 id
, uint32 size
) {
236 EnterHostFromHostResourceForceCallback
<PPB_VideoDecoder_Dev_API
> enter(
237 decoder
, callback_factory_
,
238 &PPB_VideoDecoder_Proxy::SendMsgEndOfBitstreamACKToPlugin
, decoder
, id
);
241 PP_VideoBitstreamBuffer_Dev bitstream
= { id
, buffer
.host_resource(), size
};
242 enter
.SetResult(enter
.object()->Decode(&bitstream
, enter
.callback()));
245 void PPB_VideoDecoder_Proxy::OnMsgAssignPictureBuffers(
246 const HostResource
& decoder
,
247 const std::vector
<PP_PictureBuffer_Dev
>& buffers
) {
248 EnterHostFromHostResource
<PPB_VideoDecoder_Dev_API
> enter(decoder
);
249 if (enter
.succeeded() && !buffers
.empty()) {
250 const PP_PictureBuffer_Dev
* buffer_array
= &buffers
.front();
251 enter
.object()->AssignPictureBuffers(
252 base::checked_cast
<uint32_t>(buffers
.size()), buffer_array
);
256 void PPB_VideoDecoder_Proxy::OnMsgReusePictureBuffer(
257 const HostResource
& decoder
, int32 picture_buffer_id
) {
258 EnterHostFromHostResource
<PPB_VideoDecoder_Dev_API
> enter(decoder
);
259 if (enter
.succeeded())
260 enter
.object()->ReusePictureBuffer(picture_buffer_id
);
263 void PPB_VideoDecoder_Proxy::OnMsgFlush(const HostResource
& decoder
) {
264 EnterHostFromHostResourceForceCallback
<PPB_VideoDecoder_Dev_API
> enter(
265 decoder
, callback_factory_
,
266 &PPB_VideoDecoder_Proxy::SendMsgFlushACKToPlugin
, decoder
);
267 if (enter
.succeeded())
268 enter
.SetResult(enter
.object()->Flush(enter
.callback()));
271 void PPB_VideoDecoder_Proxy::OnMsgReset(const HostResource
& decoder
) {
272 EnterHostFromHostResourceForceCallback
<PPB_VideoDecoder_Dev_API
> enter(
273 decoder
, callback_factory_
,
274 &PPB_VideoDecoder_Proxy::SendMsgResetACKToPlugin
, decoder
);
275 if (enter
.succeeded())
276 enter
.SetResult(enter
.object()->Reset(enter
.callback()));
279 void PPB_VideoDecoder_Proxy::OnMsgDestroy(const HostResource
& decoder
) {
280 EnterHostFromHostResource
<PPB_VideoDecoder_Dev_API
> enter(decoder
);
281 if (enter
.succeeded())
282 enter
.object()->Destroy();
285 void PPB_VideoDecoder_Proxy::SendMsgEndOfBitstreamACKToPlugin(
286 int32_t result
, const HostResource
& decoder
, int32 id
) {
287 dispatcher()->Send(new PpapiMsg_PPBVideoDecoder_EndOfBitstreamACK(
288 API_ID_PPB_VIDEO_DECODER_DEV
, decoder
, id
, result
));
291 void PPB_VideoDecoder_Proxy::SendMsgFlushACKToPlugin(
292 int32_t result
, const HostResource
& decoder
) {
293 dispatcher()->Send(new PpapiMsg_PPBVideoDecoder_FlushACK(
294 API_ID_PPB_VIDEO_DECODER_DEV
, decoder
, result
));
297 void PPB_VideoDecoder_Proxy::SendMsgResetACKToPlugin(
298 int32_t result
, const HostResource
& decoder
) {
299 dispatcher()->Send(new PpapiMsg_PPBVideoDecoder_ResetACK(
300 API_ID_PPB_VIDEO_DECODER_DEV
, decoder
, result
));
303 void PPB_VideoDecoder_Proxy::OnMsgEndOfBitstreamACK(
304 const HostResource
& decoder
, int32_t id
, int32_t result
) {
305 EnterPluginFromHostResource
<PPB_VideoDecoder_Dev_API
> enter(decoder
);
306 if (enter
.succeeded())
307 static_cast<VideoDecoder
*>(enter
.object())->EndOfBitstreamACK(id
, result
);
310 void PPB_VideoDecoder_Proxy::OnMsgFlushACK(
311 const HostResource
& decoder
, int32_t result
) {
312 EnterPluginFromHostResource
<PPB_VideoDecoder_Dev_API
> enter(decoder
);
313 if (enter
.succeeded())
314 static_cast<VideoDecoder
*>(enter
.object())->FlushACK(result
);
317 void PPB_VideoDecoder_Proxy::OnMsgResetACK(
318 const HostResource
& decoder
, int32_t result
) {
319 EnterPluginFromHostResource
<PPB_VideoDecoder_Dev_API
> enter(decoder
);
320 if (enter
.succeeded())
321 static_cast<VideoDecoder
*>(enter
.object())->ResetACK(result
);