Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / video_decoder_shim.h
1 // Copyright 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 CONTENT_RENDERER_PEPPER_VIDEO_DECODER_SHIM_H_
6 #define CONTENT_RENDERER_PEPPER_VIDEO_DECODER_SHIM_H_
7
8 #include <queue>
9 #include <vector>
10
11 #include "base/basictypes.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/linked_ptr.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "gpu/command_buffer/common/mailbox.h"
17 #include "media/base/video_decoder_config.h"
18 #include "media/video/video_decode_accelerator.h"
19
20 #include "ppapi/c/pp_codecs.h"
21
22 namespace base {
23 class SingleThreadTaskRunner;
24 }
25
26 namespace gpu {
27 namespace gles2 {
28 class GLES2Interface;
29 }
30 }
31
32 namespace media {
33 class DecoderBuffer;
34 }
35
36 namespace webkit {
37 namespace gpu {
38 class ContextProviderWebContext;
39 }
40 }
41
42 namespace content {
43
44 class PepperVideoDecoderHost;
45
46 // This class is a shim to wrap a media::VideoDecoder so that it can be used
47 // by PepperVideoDecoderHost in place of a media::VideoDecodeAccelerator.
48 // This class should be constructed, used, and destructed on the main (render)
49 // thread.
50 class VideoDecoderShim : public media::VideoDecodeAccelerator {
51  public:
52   explicit VideoDecoderShim(PepperVideoDecoderHost* host);
53   virtual ~VideoDecoderShim();
54
55   // media::VideoDecodeAccelerator implementation.
56   virtual bool Initialize(
57       media::VideoCodecProfile profile,
58       media::VideoDecodeAccelerator::Client* client) OVERRIDE;
59   virtual void Decode(const media::BitstreamBuffer& bitstream_buffer) OVERRIDE;
60   virtual void AssignPictureBuffers(
61       const std::vector<media::PictureBuffer>& buffers) OVERRIDE;
62   virtual void ReusePictureBuffer(int32 picture_buffer_id) OVERRIDE;
63   virtual void Flush() OVERRIDE;
64   virtual void Reset() OVERRIDE;
65   virtual void Destroy() OVERRIDE;
66
67  private:
68   enum State {
69     UNINITIALIZED,
70     DECODING,
71     FLUSHING,
72     RESETTING,
73   };
74
75   struct PendingDecode;
76   struct PendingFrame;
77   class DecoderImpl;
78
79   void OnInitializeComplete(int32_t result, uint32_t texture_pool_size);
80   void OnDecodeComplete(int32_t result, uint32_t decode_id);
81   void OnOutputComplete(scoped_ptr<PendingFrame> frame);
82   void SendPictures();
83   void OnResetComplete();
84   void NotifyCompletedDecodes();
85   void DismissTexture(uint32_t texture_id);
86   void DeleteTexture(uint32_t texture_id);
87   // Call this whenever we change GL state that the plugin relies on, such as
88   // creating picture textures.
89   void FlushCommandBuffer();
90
91   scoped_ptr<DecoderImpl> decoder_impl_;
92   State state_;
93
94   PepperVideoDecoderHost* host_;
95   scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_;
96   scoped_refptr<webkit::gpu::ContextProviderWebContext> context_provider_;
97
98   // The current decoded frame size.
99   gfx::Size texture_size_;
100   // Map that takes the plugin's GL texture id to the renderer's GL texture id.
101   typedef base::hash_map<uint32_t, uint32_t> TextureIdMap;
102   TextureIdMap texture_id_map_;
103   // Available textures (these are plugin ids.)
104   typedef base::hash_set<uint32_t> TextureIdSet;
105   TextureIdSet available_textures_;
106   // Track textures that are no longer needed (these are plugin ids.)
107   TextureIdSet textures_to_dismiss_;
108   // Mailboxes for pending texture requests, to write to plugin's textures.
109   std::vector<gpu::Mailbox> pending_texture_mailboxes_;
110
111   // Queue of completed decode ids, for notifying the host.
112   typedef std::queue<uint32_t> CompletedDecodeQueue;
113   CompletedDecodeQueue completed_decodes_;
114
115   // Queue of decoded frames that have been converted to RGB and await upload to
116   // a GL texture.
117   typedef std::queue<linked_ptr<PendingFrame> > PendingFrameQueue;
118   PendingFrameQueue pending_frames_;
119
120   // The optimal number of textures to allocate for decoder_impl_.
121   uint32_t texture_pool_size_;
122
123   uint32_t num_pending_decodes_;
124
125   base::WeakPtrFactory<VideoDecoderShim> weak_ptr_factory_;
126
127   DISALLOW_COPY_AND_ASSIGN(VideoDecoderShim);
128 };
129
130 }  // namespace content
131
132 #endif  // CONTENT_RENDERER_PEPPER_VIDEO_DECODER_SHIM_H_