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.
5 #ifndef CONTENT_RENDERER_PEPPER_PEPPER_VIDEO_DECODER_HOST_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_VIDEO_DECODER_HOST_H_
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"
25 class RendererPpapiHost;
26 class VideoDecoderShim;
28 class PepperVideoDecoderHost : public ppapi::host::ResourceHost,
29 public media::VideoDecodeAccelerator::Client {
31 PepperVideoDecoderHost(RendererPpapiHost* host,
33 PP_Resource resource);
35 PepperVideoDecoderHost(const PepperVideoDecoderHost&) = delete;
36 PepperVideoDecoderHost& operator=(const PepperVideoDecoderHost&) = delete;
38 ~PepperVideoDecoderHost() override;
41 enum class PictureBufferState {
47 struct PendingDecode {
48 PendingDecode(int32_t decode_id,
51 const ppapi::host::ReplyMessageContext& reply_context);
54 const int32_t decode_id;
55 const uint32_t shm_id;
57 const ppapi::host::ReplyMessageContext reply_context;
59 typedef std::list<PendingDecode> PendingDecodeList;
62 MappedBuffer(base::UnsafeSharedMemoryRegion region,
63 base::WritableSharedMemoryMapping mapping);
66 MappedBuffer(MappedBuffer&&);
67 MappedBuffer& operator=(MappedBuffer&&);
69 base::UnsafeSharedMemoryRegion region;
70 base::WritableSharedMemoryMapping mapping;
74 friend class VideoDecoderShim;
76 // ResourceHost implementation.
77 int32_t OnResourceMessageReceived(
78 const IPC::Message& msg,
79 ppapi::host::HostMessageContext* context) override;
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;
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,
102 int32_t OnHostMsgDecode(ppapi::host::HostMessageContext* context,
106 int32_t OnHostMsgAssignTextures(ppapi::host::HostMessageContext* context,
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);
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_);
122 // Tries to initialize software decoder. Returns true on success.
123 bool TryFallbackToSoftwareDecoder();
125 PendingDecodeList::iterator GetPendingDecodeById(int32_t decode_id);
127 // Non-owning pointer.
128 RendererPpapiHost* renderer_ppapi_host_;
130 media::VideoCodecProfile profile_ = media::VIDEO_CODEC_PROFILE_UNKNOWN;
132 std::unique_ptr<media::VideoDecodeAccelerator> decoder_;
134 bool software_fallback_allowed_ = false;
135 bool software_fallback_used_ = false;
137 // Used for UMA stats; not frame-accurate.
138 gfx::Size coded_size_;
140 int pending_texture_requests_ = 0;
142 // Set after software decoder fallback to dismiss all outstanding texture
144 int assign_textures_messages_to_dismiss_ = 0;
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.
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_;
158 uint32_t min_picture_count_ = 0;
159 typedef std::map<uint32_t, PictureBufferState> PictureBufferMap;
160 PictureBufferMap picture_buffer_map_;
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_;
166 // Keeps list of pending decodes.
167 PendingDecodeList pending_decodes_;
169 ppapi::host::ReplyMessageContext flush_reply_context_;
170 ppapi::host::ReplyMessageContext reset_reply_context_;
172 bool initialized_ = false;
175 } // namespace content
177 #endif // CONTENT_RENDERER_PEPPER_PEPPER_VIDEO_DECODER_HOST_H_