[M108 Aura Migration][NaCl][PPFwk] Add error logs + SVACE/DLOG/Static analysis fix
[platform/framework/web/chromium-efl.git] / content / renderer / pepper / pepper_video_decoder_host.h
1 // Copyright 2014 The Chromium Authors
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_PEPPER_VIDEO_DECODER_HOST_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_VIDEO_DECODER_HOST_H_
7
8 #include <stdint.h>
9
10 #include <list>
11 #include <map>
12 #include <memory>
13 #include <set>
14 #include <vector>
15
16 #include "gpu/command_buffer/common/mailbox.h"
17 #include "media/video/video_decode_accelerator.h"
18 #include "ppapi/c/pp_codecs.h"
19 #include "ppapi/host/host_message_context.h"
20 #include "ppapi/host/resource_host.h"
21 #include "ppapi/proxy/resource_message_params.h"
22
23 namespace content {
24
25 class RendererPpapiHost;
26 class VideoDecoderShim;
27
28 class PepperVideoDecoderHost : public ppapi::host::ResourceHost,
29                                public media::VideoDecodeAccelerator::Client {
30  public:
31   PepperVideoDecoderHost(RendererPpapiHost* host,
32                          PP_Instance instance,
33                          PP_Resource resource);
34
35   PepperVideoDecoderHost(const PepperVideoDecoderHost&) = delete;
36   PepperVideoDecoderHost& operator=(const PepperVideoDecoderHost&) = delete;
37
38   ~PepperVideoDecoderHost() override;
39
40  private:
41   enum class PictureBufferState {
42     ASSIGNED,
43     IN_USE,
44     DISMISSED,
45   };
46
47   struct PendingDecode {
48     PendingDecode(int32_t decode_id,
49                   uint32_t shm_id,
50                   uint32_t size,
51                   const ppapi::host::ReplyMessageContext& reply_context);
52     ~PendingDecode();
53
54     const int32_t decode_id;
55     const uint32_t shm_id;
56     const uint32_t size;
57     const ppapi::host::ReplyMessageContext reply_context;
58   };
59   typedef std::list<PendingDecode> PendingDecodeList;
60
61   struct MappedBuffer {
62     MappedBuffer(base::UnsafeSharedMemoryRegion region,
63                  base::WritableSharedMemoryMapping mapping);
64     ~MappedBuffer();
65
66     MappedBuffer(MappedBuffer&&);
67     MappedBuffer& operator=(MappedBuffer&&);
68
69     base::UnsafeSharedMemoryRegion region;
70     base::WritableSharedMemoryMapping mapping;
71     bool busy = false;
72   };
73
74   friend class VideoDecoderShim;
75
76   // ResourceHost implementation.
77   int32_t OnResourceMessageReceived(
78       const IPC::Message& msg,
79       ppapi::host::HostMessageContext* context) override;
80
81   // media::VideoDecodeAccelerator::Client implementation.
82   void ProvidePictureBuffers(uint32_t requested_num_of_buffers,
83                              media::VideoPixelFormat format,
84                              uint32_t textures_per_buffer,
85                              const gfx::Size& dimensions,
86                              uint32_t texture_target) override;
87   void DismissPictureBuffer(int32_t picture_buffer_id) override;
88   void PictureReady(const media::Picture& picture) override;
89   void NotifyEndOfBitstreamBuffer(int32_t bitstream_buffer_id) override;
90   void NotifyFlushDone() override;
91   void NotifyResetDone() override;
92   void NotifyError(media::VideoDecodeAccelerator::Error error) override;
93
94   int32_t OnHostMsgInitialize(ppapi::host::HostMessageContext* context,
95                               const ppapi::HostResource& graphics_context,
96                               PP_VideoProfile profile,
97                               PP_HardwareAcceleration acceleration,
98                               uint32_t min_picture_count);
99   int32_t OnHostMsgGetShm(ppapi::host::HostMessageContext* context,
100                           uint32_t shm_id,
101                           uint32_t shm_size);
102   int32_t OnHostMsgDecode(ppapi::host::HostMessageContext* context,
103                           uint32_t shm_id,
104                           uint32_t size,
105                           int32_t decode_id);
106   int32_t OnHostMsgAssignTextures(ppapi::host::HostMessageContext* context,
107                                   const PP_Size& size,
108                                   const std::vector<uint32_t>& texture_ids,
109                                   const std::vector<gpu::Mailbox>& mailboxes);
110   int32_t OnHostMsgRecyclePicture(ppapi::host::HostMessageContext* context,
111                                   uint32_t picture_id);
112   int32_t OnHostMsgFlush(ppapi::host::HostMessageContext* context);
113   int32_t OnHostMsgReset(ppapi::host::HostMessageContext* context);
114
115   // These methods are needed by VideoDecodeShim, to look like a
116   // VideoDecodeAccelerator.
117   const uint8_t* DecodeIdToAddress(uint32_t decode_id);
118   std::vector<gpu::Mailbox> TakeMailboxes() {
119     return std::move(texture_mailboxes_);
120   }
121
122   // Tries to initialize software decoder. Returns true on success.
123   bool TryFallbackToSoftwareDecoder();
124
125   PendingDecodeList::iterator GetPendingDecodeById(int32_t decode_id);
126
127   // Non-owning pointer.
128   RendererPpapiHost* renderer_ppapi_host_;
129
130   media::VideoCodecProfile profile_ = media::VIDEO_CODEC_PROFILE_UNKNOWN;
131
132   std::unique_ptr<media::VideoDecodeAccelerator> decoder_;
133
134   bool software_fallback_allowed_ = false;
135   bool software_fallback_used_ = false;
136
137   // Used for UMA stats; not frame-accurate.
138   gfx::Size coded_size_;
139
140   int pending_texture_requests_ = 0;
141
142   // Set after software decoder fallback to dismiss all outstanding texture
143   // requests.
144   int assign_textures_messages_to_dismiss_ = 0;
145
146   // A vector holding our shm buffers, in sync with a similar vector in the
147   // resource. We use a buffer's index in these vectors as its id on both sides
148   // of the proxy. Only add buffers or update them in place so as not to
149   // invalidate these ids.
150   //
151   // These regions are created here, in the host, and shared with the other side
152   // of the proxy who will write into them. While they are only used in a
153   // read-only way in the host, using a ReadOnlySharedMemoryRegion would involve
154   // an extra round-trip to allow the other side of the proxy to map the region
155   // writable before sending a read-only region back to the host.
156   std::vector<MappedBuffer> shm_buffers_;
157
158   uint32_t min_picture_count_ = 0;
159   typedef std::map<uint32_t, PictureBufferState> PictureBufferMap;
160   PictureBufferMap picture_buffer_map_;
161
162   // Mailboxes corresponding to textures given to AssignPictureBuffers, to allow
163   // VideoDecoderShim to use them from another context.
164   std::vector<gpu::Mailbox> texture_mailboxes_;
165
166   // Keeps list of pending decodes.
167   PendingDecodeList pending_decodes_;
168
169   ppapi::host::ReplyMessageContext flush_reply_context_;
170   ppapi::host::ReplyMessageContext reset_reply_context_;
171
172   bool initialized_ = false;
173 };
174
175 }  // namespace content
176
177 #endif  // CONTENT_RENDERER_PEPPER_PEPPER_VIDEO_DECODER_HOST_H_