Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ppapi / proxy / video_decoder_resource.h
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.
4
5 #ifndef PPAPI_PROXY_VIDEO_DECODER_RESOURCE_H_
6 #define PPAPI_PROXY_VIDEO_DECODER_RESOURCE_H_
7
8 #include <queue>
9
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"
20
21 namespace gpu {
22 struct Mailbox;
23 namespace gles2 {
24 class GLES2Implementation;
25 }
26 }
27
28 namespace ppapi {
29
30 class PPB_Graphics3D_Shared;
31 class TrackedCallback;
32
33 namespace proxy {
34
35 class PPAPI_PROXY_EXPORT VideoDecoderResource
36     : public PluginResource,
37       public thunk::PPB_VideoDecoder_API {
38  public:
39   VideoDecoderResource(Connection connection, PP_Instance instance);
40   virtual ~VideoDecoderResource();
41
42   // Resource overrides.
43   virtual thunk::PPB_VideoDecoder_API* AsPPB_VideoDecoder_API() override;
44
45   // PPB_VideoDecoder_API implementation.
46   virtual 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   virtual int32_t Initialize(PP_Resource graphics_context,
52                              PP_VideoProfile profile,
53                              PP_HardwareAcceleration acceleration,
54                              scoped_refptr<TrackedCallback> callback) override;
55   virtual int32_t Decode(uint32_t decode_id,
56                          uint32_t size,
57                          const void* buffer,
58                          scoped_refptr<TrackedCallback> callback) override;
59   virtual int32_t GetPicture0_1(
60       PP_VideoPicture_0_1* picture,
61       scoped_refptr<TrackedCallback> callback) override;
62   virtual int32_t GetPicture(PP_VideoPicture* picture,
63                              scoped_refptr<TrackedCallback> callback) override;
64   virtual void RecyclePicture(const PP_VideoPicture* picture) override;
65   virtual int32_t Flush(scoped_refptr<TrackedCallback> callback) override;
66   virtual int32_t Reset(scoped_refptr<TrackedCallback> callback) override;
67
68   // PluginResource implementation.
69   virtual void OnReplyReceived(const ResourceMessageReplyParams& params,
70                                const IPC::Message& msg) override;
71
72   // Called only by unit tests. This bypasses Graphics3D setup, which doesn't
73   // work in ppapi::proxy::PluginProxyTest.
74   void SetForTest();
75
76  private:
77   // Struct to hold a shared memory buffer.
78   struct ShmBuffer {
79     ShmBuffer(scoped_ptr<base::SharedMemory> shm,
80               uint32_t size,
81               uint32_t shm_id);
82     ~ShmBuffer();
83
84     const scoped_ptr<base::SharedMemory> shm;
85     void* addr;
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;
89   };
90
91   // Struct to hold texture information.
92   struct Texture {
93     Texture(uint32_t texture_target, const PP_Size& size);
94     ~Texture();
95
96     const uint32_t texture_target;
97     const PP_Size size;
98   };
99
100   // Struct to hold a picture received from the decoder.
101   struct Picture {
102     Picture(int32_t decode_id,
103             uint32_t texture_id,
104             const PP_Rect& visible_rect);
105     ~Picture();
106
107     int32_t decode_id;
108     uint32_t texture_id;
109     PP_Rect visible_rect;
110   };
111
112   // Unsolicited reply message handlers.
113   void OnPluginMsgRequestTextures(const ResourceMessageReplyParams& params,
114                                   uint32_t num_textures,
115                                   const PP_Size& size,
116                                   uint32_t texture_target,
117                                   const std::vector<gpu::Mailbox>& mailboxes);
118   void OnPluginMsgPictureReady(const ResourceMessageReplyParams& params,
119                                int32_t decode_id,
120                                uint32_t texture_id,
121                                const PP_Rect& visible_rect);
122   void OnPluginMsgDismissPicture(const ResourceMessageReplyParams& params,
123                                  uint32_t texture_id);
124   void OnPluginMsgNotifyError(const ResourceMessageReplyParams& params,
125                               int32_t error);
126
127   // Reply message handlers for operations that are done in the host.
128   void OnPluginMsgInitializeComplete(const ResourceMessageReplyParams& params);
129   void OnPluginMsgDecodeComplete(const ResourceMessageReplyParams& params,
130                                  uint32_t shm_id);
131   void OnPluginMsgFlushComplete(const ResourceMessageReplyParams& params);
132   void OnPluginMsgResetComplete(const ResourceMessageReplyParams& params);
133
134   void RunCallbackWithError(scoped_refptr<TrackedCallback>* callback);
135   void DeleteGLTexture(uint32_t texture_id);
136   void WriteNextPicture();
137
138   // ScopedVector to own the shared memory buffers.
139   ScopedVector<ShmBuffer> shm_buffers_;
140
141   // List of available shared memory buffers.
142   typedef std::vector<ShmBuffer*> ShmBufferList;
143   ShmBufferList available_shm_buffers_;
144
145   // Map of GL texture id to texture info.
146   typedef base::hash_map<uint32_t, Texture> TextureMap;
147   TextureMap textures_;
148
149   // Queue of received pictures.
150   typedef std::queue<Picture> PictureQueue;
151   PictureQueue received_pictures_;
152
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_;
159
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];
170
171   // State for pending get_picture_callback_.
172   PP_VideoPicture* get_picture_;
173   PP_VideoPicture_0_1* get_picture_0_1_;
174
175   ScopedPPResource graphics3d_;
176   gpu::gles2::GLES2Implementation* gles2_impl_;
177
178   bool initialized_;
179   bool testing_;
180   int32_t decoder_last_error_;
181
182   DISALLOW_COPY_AND_ASSIGN(VideoDecoderResource);
183 };
184
185 }  // namespace proxy
186 }  // namespace ppapi
187
188 #endif  // PPAPI_PROXY_VIDEO_DECODER_RESOURCE_H_