blob: 1ec9605a86058b0575fb253bccd656562074bd56 [file] [log] [blame]
[email protected]aed96532012-06-23 14:27:421// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]7ace8ad2011-08-06 03:23:582// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ppapi/proxy/ppb_video_decoder_proxy.h"
6
7#include "base/logging.h"
avie029c4132015-12-23 06:45:228#include "base/macros.h"
brettw669d47b12015-02-13 21:17:389#include "base/numerics/safe_conversions.h"
[email protected]7ace8ad2011-08-06 03:23:5810#include "gpu/command_buffer/client/gles2_implementation.h"
11#include "ppapi/proxy/enter_proxy.h"
12#include "ppapi/proxy/plugin_dispatcher.h"
13#include "ppapi/proxy/ppapi_messages.h"
14#include "ppapi/proxy/ppb_buffer_proxy.h"
[email protected]1b2ec22e2011-08-30 00:48:3315#include "ppapi/proxy/ppb_graphics_3d_proxy.h"
[email protected]7ace8ad2011-08-06 03:23:5816#include "ppapi/thunk/enter.h"
17#include "ppapi/thunk/resource_creation_api.h"
18#include "ppapi/thunk/thunk.h"
19
20using ppapi::thunk::EnterResourceNoLock;
21using ppapi::thunk::PPB_Buffer_API;
[email protected]1b2ec22e2011-08-30 00:48:3322using ppapi::thunk::PPB_Graphics3D_API;
[email protected]4ba60db2014-05-06 07:08:1923using ppapi::thunk::PPB_VideoDecoder_Dev_API;
[email protected]7ace8ad2011-08-06 03:23:5824
[email protected]4d2efd22011-08-18 21:58:0225namespace ppapi {
[email protected]7ace8ad2011-08-06 03:23:5826namespace proxy {
27
[email protected]614888b2012-01-05 06:18:1228class VideoDecoder : public PPB_VideoDecoder_Shared {
[email protected]7ace8ad2011-08-06 03:23:5829 public:
[email protected]7f8b26b2011-08-18 15:41:0130 // You must call Init() before using this class.
31 explicit VideoDecoder(const HostResource& resource);
nicke4784432015-04-23 14:01:4832 ~VideoDecoder() override;
[email protected]7ace8ad2011-08-06 03:23:5833
34 static VideoDecoder* Create(const HostResource& resource,
[email protected]1b2ec22e2011-08-30 00:48:3335 PP_Resource graphics_context,
[email protected]2ffc31a2011-09-01 03:18:2836 PP_VideoDecoder_Profile profile);
[email protected]7ace8ad2011-08-06 03:23:5837
[email protected]4ba60db2014-05-06 07:08:1938 // PPB_VideoDecoder_Dev_API implementation.
nicke4784432015-04-23 14:01:4839 int32_t Decode(const PP_VideoBitstreamBuffer_Dev* bitstream_buffer,
40 scoped_refptr<TrackedCallback> callback) override;
41 void AssignPictureBuffers(uint32_t no_of_buffers,
42 const PP_PictureBuffer_Dev* buffers) override;
43 void ReusePictureBuffer(int32_t picture_buffer_id) override;
44 int32_t Flush(scoped_refptr<TrackedCallback> callback) override;
45 int32_t Reset(scoped_refptr<TrackedCallback> callback) override;
46 void Destroy() override;
[email protected]7ace8ad2011-08-06 03:23:5847
[email protected]7ace8ad2011-08-06 03:23:5848 private:
49 friend class PPB_VideoDecoder_Proxy;
[email protected]7f8b26b2011-08-18 15:41:0150
51 PluginDispatcher* GetDispatcher() const;
[email protected]7ace8ad2011-08-06 03:23:5852
53 // Run the callbacks that were passed into the plugin interface.
54 void FlushACK(int32_t result);
55 void ResetACK(int32_t result);
56 void EndOfBitstreamACK(int32_t buffer_id, int32_t result);
57
58 DISALLOW_COPY_AND_ASSIGN(VideoDecoder);
59};
60
[email protected]614888b2012-01-05 06:18:1261VideoDecoder::VideoDecoder(const HostResource& decoder)
62 : PPB_VideoDecoder_Shared(decoder) {
[email protected]7ace8ad2011-08-06 03:23:5863}
64
65VideoDecoder::~VideoDecoder() {
[email protected]c1e77f1a2013-03-28 18:12:0466 FlushCommandBuffer();
67 PPB_VideoDecoder_Shared::Destroy();
[email protected]7ace8ad2011-08-06 03:23:5868}
69
[email protected]7ace8ad2011-08-06 03:23:5870int32_t VideoDecoder::Decode(
71 const PP_VideoBitstreamBuffer_Dev* bitstream_buffer,
[email protected]aed96532012-06-23 14:27:4272 scoped_refptr<TrackedCallback> callback) {
[email protected]4d2efd22011-08-18 21:58:0273 EnterResourceNoLock<PPB_Buffer_API>
[email protected]7ace8ad2011-08-06 03:23:5874 enter_buffer(bitstream_buffer->data, true);
75 if (enter_buffer.failed())
76 return PP_ERROR_BADRESOURCE;
77
[email protected]d527cdd82011-08-12 22:50:1978 if (!SetBitstreamBufferCallback(bitstream_buffer->id, callback))
79 return PP_ERROR_BADARGUMENT;
[email protected]7ace8ad2011-08-06 03:23:5880
81 Buffer* ppb_buffer =
82 static_cast<Buffer*>(enter_buffer.object());
83 HostResource host_buffer = ppb_buffer->host_resource();
84
85 FlushCommandBuffer();
86 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Decode(
[email protected]ac4b54d2011-10-20 23:09:2887 API_ID_PPB_VIDEO_DECODER_DEV, host_resource(),
[email protected]7ace8ad2011-08-06 03:23:5888 host_buffer, bitstream_buffer->id,
89 bitstream_buffer->size));
90 return PP_OK_COMPLETIONPENDING;
91}
92
93void VideoDecoder::AssignPictureBuffers(uint32_t no_of_buffers,
94 const PP_PictureBuffer_Dev* buffers) {
95 std::vector<PP_PictureBuffer_Dev> buffer_list(
96 buffers, buffers + no_of_buffers);
97 FlushCommandBuffer();
98 GetDispatcher()->Send(
99 new PpapiHostMsg_PPBVideoDecoder_AssignPictureBuffers(
[email protected]ac4b54d2011-10-20 23:09:28100 API_ID_PPB_VIDEO_DECODER_DEV, host_resource(), buffer_list));
[email protected]7ace8ad2011-08-06 03:23:58101}
102
103void VideoDecoder::ReusePictureBuffer(int32_t picture_buffer_id) {
104 FlushCommandBuffer();
105 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_ReusePictureBuffer(
[email protected]ac4b54d2011-10-20 23:09:28106 API_ID_PPB_VIDEO_DECODER_DEV, host_resource(), picture_buffer_id));
[email protected]7ace8ad2011-08-06 03:23:58107}
108
[email protected]aed96532012-06-23 14:27:42109int32_t VideoDecoder::Flush(scoped_refptr<TrackedCallback> callback) {
[email protected]d527cdd82011-08-12 22:50:19110 if (!SetFlushCallback(callback))
111 return PP_ERROR_INPROGRESS;
[email protected]7ace8ad2011-08-06 03:23:58112
113 FlushCommandBuffer();
114 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Flush(
[email protected]ac4b54d2011-10-20 23:09:28115 API_ID_PPB_VIDEO_DECODER_DEV, host_resource()));
[email protected]7ace8ad2011-08-06 03:23:58116 return PP_OK_COMPLETIONPENDING;
117}
118
[email protected]aed96532012-06-23 14:27:42119int32_t VideoDecoder::Reset(scoped_refptr<TrackedCallback> callback) {
[email protected]d527cdd82011-08-12 22:50:19120 if (!SetResetCallback(callback))
121 return PP_ERROR_INPROGRESS;
[email protected]7ace8ad2011-08-06 03:23:58122
123 FlushCommandBuffer();
124 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Reset(
[email protected]ac4b54d2011-10-20 23:09:28125 API_ID_PPB_VIDEO_DECODER_DEV, host_resource()));
[email protected]7ace8ad2011-08-06 03:23:58126 return PP_OK_COMPLETIONPENDING;
127}
128
129void VideoDecoder::Destroy() {
130 FlushCommandBuffer();
131 GetDispatcher()->Send(new PpapiHostMsg_PPBVideoDecoder_Destroy(
[email protected]ac4b54d2011-10-20 23:09:28132 API_ID_PPB_VIDEO_DECODER_DEV, host_resource()));
[email protected]9a578392011-12-07 18:59:27133 PPB_VideoDecoder_Shared::Destroy();
[email protected]7ace8ad2011-08-06 03:23:58134}
135
[email protected]7f8b26b2011-08-18 15:41:01136PluginDispatcher* VideoDecoder::GetDispatcher() const {
137 return PluginDispatcher::GetForResource(this);
[email protected]7ace8ad2011-08-06 03:23:58138}
139
140void VideoDecoder::ResetACK(int32_t result) {
141 RunResetCallback(result);
142}
143
144void VideoDecoder::FlushACK(int32_t result) {
145 RunFlushCallback(result);
146}
147
148void VideoDecoder::EndOfBitstreamACK(
149 int32_t bitstream_buffer_id, int32_t result) {
150 RunBitstreamBufferCallback(bitstream_buffer_id, result);
151}
152
[email protected]5c966022011-09-13 18:09:37153PPB_VideoDecoder_Proxy::PPB_VideoDecoder_Proxy(Dispatcher* dispatcher)
154 : InterfaceProxy(dispatcher),
[email protected]a2f53dc2013-04-30 01:06:35155 callback_factory_(this) {
[email protected]7ace8ad2011-08-06 03:23:58156}
157
158PPB_VideoDecoder_Proxy::~PPB_VideoDecoder_Proxy() {
159}
160
[email protected]7ace8ad2011-08-06 03:23:58161bool PPB_VideoDecoder_Proxy::OnMessageReceived(const IPC::Message& msg) {
[email protected]17cd29a2012-11-15 20:43:23162 if (!dispatcher()->permissions().HasPermission(PERMISSION_DEV))
163 return false;
164
[email protected]7ace8ad2011-08-06 03:23:58165 bool handled = true;
166 IPC_BEGIN_MESSAGE_MAP(PPB_VideoDecoder_Proxy, msg)
167 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Create,
168 OnMsgCreate)
169 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Decode, OnMsgDecode)
170 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_AssignPictureBuffers,
171 OnMsgAssignPictureBuffers)
172 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_ReusePictureBuffer,
173 OnMsgReusePictureBuffer)
174 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Flush, OnMsgFlush)
175 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Reset, OnMsgReset)
176 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBVideoDecoder_Destroy, OnMsgDestroy)
177 IPC_MESSAGE_HANDLER(PpapiMsg_PPBVideoDecoder_ResetACK, OnMsgResetACK)
178 IPC_MESSAGE_HANDLER(PpapiMsg_PPBVideoDecoder_EndOfBitstreamACK,
179 OnMsgEndOfBitstreamACK)
180 IPC_MESSAGE_HANDLER(PpapiMsg_PPBVideoDecoder_FlushACK, OnMsgFlushACK)
181 IPC_MESSAGE_UNHANDLED(handled = false)
182 IPC_END_MESSAGE_MAP()
183 DCHECK(handled);
184 return handled;
185}
186
187PP_Resource PPB_VideoDecoder_Proxy::CreateProxyResource(
[email protected]1b2ec22e2011-08-30 00:48:33188 PP_Instance instance,
189 PP_Resource graphics_context,
[email protected]2ffc31a2011-09-01 03:18:28190 PP_VideoDecoder_Profile profile) {
[email protected]7ace8ad2011-08-06 03:23:58191 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
192 // Dispatcher is null if it cannot find the instance passed to it (i.e. if the
193 // client passes in an invalid instance).
194 if (!dispatcher)
195 return 0;
196
[email protected]6013c792014-03-07 00:18:33197 if (!dispatcher->preferences().is_accelerated_video_decode_enabled)
198 return 0;
199
[email protected]88cc78182011-12-01 06:47:09200 EnterResourceNoLock<PPB_Graphics3D_API> enter_context(graphics_context,
201 true);
202 if (enter_context.failed())
203 return 0;
[email protected]1b2ec22e2011-08-30 00:48:33204
[email protected]88cc78182011-12-01 06:47:09205 Graphics3D* context = static_cast<Graphics3D*>(enter_context.object());
[email protected]7ace8ad2011-08-06 03:23:58206
207 HostResource result;
208 dispatcher->Send(new PpapiHostMsg_PPBVideoDecoder_Create(
[email protected]ac4b54d2011-10-20 23:09:28209 API_ID_PPB_VIDEO_DECODER_DEV, instance,
[email protected]88cc78182011-12-01 06:47:09210 context->host_resource(), profile, &result));
[email protected]7ace8ad2011-08-06 03:23:58211 if (result.is_null())
212 return 0;
213
[email protected]7f8b26b2011-08-18 15:41:01214 // Need a scoped_refptr to keep the object alive during the Init call.
215 scoped_refptr<VideoDecoder> decoder(new VideoDecoder(result));
[email protected]88cc78182011-12-01 06:47:09216 decoder->InitCommon(graphics_context, context->gles2_impl());
[email protected]7f8b26b2011-08-18 15:41:01217 return decoder->GetReference();
[email protected]7ace8ad2011-08-06 03:23:58218}
219
220void PPB_VideoDecoder_Proxy::OnMsgCreate(
[email protected]1b2ec22e2011-08-30 00:48:33221 PP_Instance instance, const HostResource& graphics_context,
[email protected]2ffc31a2011-09-01 03:18:28222 PP_VideoDecoder_Profile profile,
[email protected]7ace8ad2011-08-06 03:23:58223 HostResource* result) {
[email protected]5c966022011-09-13 18:09:37224 thunk::EnterResourceCreation resource_creation(instance);
[email protected]7ace8ad2011-08-06 03:23:58225 if (resource_creation.failed())
226 return;
227
[email protected]7ace8ad2011-08-06 03:23:58228 // Make the resource and get the API pointer to its interface.
229 result->SetHostResource(
[email protected]4ba60db2014-05-06 07:08:19230 instance, resource_creation.functions()->CreateVideoDecoderDev(
[email protected]2ffc31a2011-09-01 03:18:28231 instance, graphics_context.host_resource(), profile));
[email protected]7ace8ad2011-08-06 03:23:58232}
233
avie029c4132015-12-23 06:45:22234void PPB_VideoDecoder_Proxy::OnMsgDecode(const HostResource& decoder,
235 const HostResource& buffer,
236 int32_t id,
237 uint32_t size) {
[email protected]4ba60db2014-05-06 07:08:19238 EnterHostFromHostResourceForceCallback<PPB_VideoDecoder_Dev_API> enter(
[email protected]5c966022011-09-13 18:09:37239 decoder, callback_factory_,
[email protected]7ace8ad2011-08-06 03:23:58240 &PPB_VideoDecoder_Proxy::SendMsgEndOfBitstreamACKToPlugin, decoder, id);
[email protected]5c966022011-09-13 18:09:37241 if (enter.failed())
242 return;
[email protected]7ace8ad2011-08-06 03:23:58243 PP_VideoBitstreamBuffer_Dev bitstream = { id, buffer.host_resource(), size };
[email protected]5c966022011-09-13 18:09:37244 enter.SetResult(enter.object()->Decode(&bitstream, enter.callback()));
[email protected]7ace8ad2011-08-06 03:23:58245}
246
247void PPB_VideoDecoder_Proxy::OnMsgAssignPictureBuffers(
248 const HostResource& decoder,
249 const std::vector<PP_PictureBuffer_Dev>& buffers) {
[email protected]4ba60db2014-05-06 07:08:19250 EnterHostFromHostResource<PPB_VideoDecoder_Dev_API> enter(decoder);
[email protected]5c966022011-09-13 18:09:37251 if (enter.succeeded() && !buffers.empty()) {
252 const PP_PictureBuffer_Dev* buffer_array = &buffers.front();
brettw669d47b12015-02-13 21:17:38253 enter.object()->AssignPictureBuffers(
254 base::checked_cast<uint32_t>(buffers.size()), buffer_array);
[email protected]5c966022011-09-13 18:09:37255 }
[email protected]7ace8ad2011-08-06 03:23:58256}
257
258void PPB_VideoDecoder_Proxy::OnMsgReusePictureBuffer(
avie029c4132015-12-23 06:45:22259 const HostResource& decoder,
260 int32_t picture_buffer_id) {
[email protected]4ba60db2014-05-06 07:08:19261 EnterHostFromHostResource<PPB_VideoDecoder_Dev_API> enter(decoder);
[email protected]5c966022011-09-13 18:09:37262 if (enter.succeeded())
263 enter.object()->ReusePictureBuffer(picture_buffer_id);
[email protected]7ace8ad2011-08-06 03:23:58264}
265
266void PPB_VideoDecoder_Proxy::OnMsgFlush(const HostResource& decoder) {
[email protected]4ba60db2014-05-06 07:08:19267 EnterHostFromHostResourceForceCallback<PPB_VideoDecoder_Dev_API> enter(
[email protected]5c966022011-09-13 18:09:37268 decoder, callback_factory_,
[email protected]7ace8ad2011-08-06 03:23:58269 &PPB_VideoDecoder_Proxy::SendMsgFlushACKToPlugin, decoder);
[email protected]5c966022011-09-13 18:09:37270 if (enter.succeeded())
271 enter.SetResult(enter.object()->Flush(enter.callback()));
[email protected]7ace8ad2011-08-06 03:23:58272}
273
274void PPB_VideoDecoder_Proxy::OnMsgReset(const HostResource& decoder) {
[email protected]4ba60db2014-05-06 07:08:19275 EnterHostFromHostResourceForceCallback<PPB_VideoDecoder_Dev_API> enter(
[email protected]5c966022011-09-13 18:09:37276 decoder, callback_factory_,
[email protected]7ace8ad2011-08-06 03:23:58277 &PPB_VideoDecoder_Proxy::SendMsgResetACKToPlugin, decoder);
[email protected]5c966022011-09-13 18:09:37278 if (enter.succeeded())
279 enter.SetResult(enter.object()->Reset(enter.callback()));
[email protected]7ace8ad2011-08-06 03:23:58280}
281
282void PPB_VideoDecoder_Proxy::OnMsgDestroy(const HostResource& decoder) {
[email protected]4ba60db2014-05-06 07:08:19283 EnterHostFromHostResource<PPB_VideoDecoder_Dev_API> enter(decoder);
[email protected]5c966022011-09-13 18:09:37284 if (enter.succeeded())
285 enter.object()->Destroy();
[email protected]7ace8ad2011-08-06 03:23:58286}
287
288void PPB_VideoDecoder_Proxy::SendMsgEndOfBitstreamACKToPlugin(
avie029c4132015-12-23 06:45:22289 int32_t result,
290 const HostResource& decoder,
291 int32_t id) {
[email protected]7ace8ad2011-08-06 03:23:58292 dispatcher()->Send(new PpapiMsg_PPBVideoDecoder_EndOfBitstreamACK(
[email protected]ac4b54d2011-10-20 23:09:28293 API_ID_PPB_VIDEO_DECODER_DEV, decoder, id, result));
[email protected]7ace8ad2011-08-06 03:23:58294}
295
296void PPB_VideoDecoder_Proxy::SendMsgFlushACKToPlugin(
297 int32_t result, const HostResource& decoder) {
298 dispatcher()->Send(new PpapiMsg_PPBVideoDecoder_FlushACK(
[email protected]ac4b54d2011-10-20 23:09:28299 API_ID_PPB_VIDEO_DECODER_DEV, decoder, result));
[email protected]7ace8ad2011-08-06 03:23:58300}
301
302void PPB_VideoDecoder_Proxy::SendMsgResetACKToPlugin(
303 int32_t result, const HostResource& decoder) {
304 dispatcher()->Send(new PpapiMsg_PPBVideoDecoder_ResetACK(
[email protected]ac4b54d2011-10-20 23:09:28305 API_ID_PPB_VIDEO_DECODER_DEV, decoder, result));
[email protected]7ace8ad2011-08-06 03:23:58306}
307
308void PPB_VideoDecoder_Proxy::OnMsgEndOfBitstreamACK(
309 const HostResource& decoder, int32_t id, int32_t result) {
[email protected]4ba60db2014-05-06 07:08:19310 EnterPluginFromHostResource<PPB_VideoDecoder_Dev_API> enter(decoder);
[email protected]7ace8ad2011-08-06 03:23:58311 if (enter.succeeded())
312 static_cast<VideoDecoder*>(enter.object())->EndOfBitstreamACK(id, result);
313}
314
315void PPB_VideoDecoder_Proxy::OnMsgFlushACK(
316 const HostResource& decoder, int32_t result) {
[email protected]4ba60db2014-05-06 07:08:19317 EnterPluginFromHostResource<PPB_VideoDecoder_Dev_API> enter(decoder);
[email protected]7ace8ad2011-08-06 03:23:58318 if (enter.succeeded())
319 static_cast<VideoDecoder*>(enter.object())->FlushACK(result);
320}
321
322void PPB_VideoDecoder_Proxy::OnMsgResetACK(
323 const HostResource& decoder, int32_t result) {
[email protected]4ba60db2014-05-06 07:08:19324 EnterPluginFromHostResource<PPB_VideoDecoder_Dev_API> enter(decoder);
[email protected]7ace8ad2011-08-06 03:23:58325 if (enter.succeeded())
326 static_cast<VideoDecoder*>(enter.object())->ResetACK(result);
327}
328
329} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02330} // namespace ppapi