GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / content / common / gpu / client / gpu_video_encode_accelerator_host.h
blob021eff46ec160a51f3784b2f43bbc8f2477b8e78
1 // Copyright 2013 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.
5 #ifndef CONTENT_COMMON_GPU_CLIENT_GPU_VIDEO_ENCODE_ACCELERATOR_HOST_H_
6 #define CONTENT_COMMON_GPU_CLIENT_GPU_VIDEO_ENCODE_ACCELERATOR_HOST_H_
8 #include <vector>
10 #include "base/containers/hash_tables.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
15 #include "gpu/config/gpu_info.h"
16 #include "ipc/ipc_listener.h"
17 #include "media/video/video_encode_accelerator.h"
19 namespace gfx {
21 class Size;
23 } // namespace gfx
25 namespace media {
27 class VideoFrame;
29 } // namespace media
31 namespace content {
33 class GpuChannelHost;
35 // This class is the renderer-side host for the VideoEncodeAccelerator in the
36 // GPU process, coordinated over IPC.
37 class GpuVideoEncodeAcceleratorHost
38 : public IPC::Listener,
39 public media::VideoEncodeAccelerator,
40 public CommandBufferProxyImpl::DeletionObserver,
41 public base::NonThreadSafe {
42 public:
43 // |this| is guaranteed not to outlive |channel| and |impl|. (See comments
44 // for |channel_| and |impl_|.)
45 GpuVideoEncodeAcceleratorHost(GpuChannelHost* channel,
46 CommandBufferProxyImpl* impl);
48 static std::vector<media::VideoEncodeAccelerator::SupportedProfile>
49 ConvertGpuToMediaProfiles(const std::vector<
50 gpu::VideoEncodeAcceleratorSupportedProfile>& gpu_profiles);
52 // IPC::Listener implementation.
53 bool OnMessageReceived(const IPC::Message& message) override;
54 void OnChannelError() override;
56 // media::VideoEncodeAccelerator implementation.
57 std::vector<SupportedProfile> GetSupportedProfiles() override;
58 bool Initialize(media::VideoFrame::Format input_format,
59 const gfx::Size& input_visible_size,
60 media::VideoCodecProfile output_profile,
61 uint32 initial_bitrate,
62 Client* client) override;
63 void Encode(const scoped_refptr<media::VideoFrame>& frame,
64 bool force_keyframe) override;
65 void UseOutputBitstreamBuffer(const media::BitstreamBuffer& buffer) override;
66 void RequestEncodingParametersChange(uint32 bitrate,
67 uint32 framerate_num) override;
68 void Destroy() override;
70 // CommandBufferProxyImpl::DeletionObserver implemetnation.
71 void OnWillDeleteImpl() override;
73 private:
74 // Only Destroy() should be deleting |this|.
75 ~GpuVideoEncodeAcceleratorHost() override;
77 // Notify |client_| of an error. Posts a task to avoid re-entrancy.
78 void PostNotifyError(Error);
80 void Send(IPC::Message* message);
82 // IPC handlers, proxying media::VideoEncodeAccelerator::Client for the GPU
83 // process. Should not be called directly.
84 void OnRequireBitstreamBuffers(uint32 input_count,
85 const gfx::Size& input_coded_size,
86 uint32 output_buffer_size);
87 void OnNotifyInputDone(int32 frame_id);
88 void OnBitstreamBufferReady(int32 bitstream_buffer_id,
89 uint32 payload_size,
90 bool key_frame);
91 void OnNotifyError(Error error);
93 // Unowned reference to the GpuChannelHost to send IPC messages to the GPU
94 // process. |channel_| outlives |impl_|, so the reference is always valid as
95 // long as it is not NULL.
96 GpuChannelHost* channel_;
98 // Route ID for the associated encoder in the GPU process.
99 int32 encoder_route_id_;
101 // The client that will receive callbacks from the encoder.
102 Client* client_;
104 // Unowned reference to the CommandBufferProxyImpl that created us. |this|
105 // registers as a DeletionObserver of |impl_|, so the reference is always
106 // valid as long as it is not NULL.
107 CommandBufferProxyImpl* impl_;
109 // media::VideoFrames sent to the encoder.
110 // base::IDMap not used here, since that takes pointers, not scoped_refptr.
111 typedef base::hash_map<int32, scoped_refptr<media::VideoFrame> > FrameMap;
112 FrameMap frame_map_;
114 // ID serial number for the next frame to send to the GPU process.
115 int32 next_frame_id_;
117 // WeakPtr factory for posting tasks back to itself.
118 base::WeakPtrFactory<GpuVideoEncodeAcceleratorHost> weak_this_factory_;
120 DISALLOW_COPY_AND_ASSIGN(GpuVideoEncodeAcceleratorHost);
123 } // namespace content
125 #endif // CONTENT_COMMON_GPU_CLIENT_GPU_VIDEO_ENCODE_ACCELERATOR_HOST_H_