1 // Copyright (c) 2012 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_GPU_COMMAND_BUFFER_STUB_H_
6 #define CONTENT_COMMON_GPU_GPU_COMMAND_BUFFER_STUB_H_
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "base/time/time.h"
15 #include "content/common/content_export.h"
16 #include "content/common/gpu/gpu_memory_manager.h"
17 #include "content/common/gpu/gpu_memory_manager_client.h"
18 #include "gpu/command_buffer/common/constants.h"
19 #include "gpu/command_buffer/common/gpu_memory_allocation.h"
20 #include "gpu/command_buffer/service/command_buffer_service.h"
21 #include "gpu/command_buffer/service/context_group.h"
22 #include "gpu/command_buffer/service/gpu_scheduler.h"
23 #include "ipc/ipc_listener.h"
24 #include "ipc/ipc_sender.h"
25 #include "media/base/video_decoder_config.h"
26 #include "ui/events/latency_info.h"
27 #include "ui/gfx/geometry/size.h"
28 #include "ui/gfx/gpu_memory_buffer.h"
29 #include "ui/gfx/native_widget_types.h"
30 #include "ui/gfx/swap_result.h"
31 #include "ui/gl/gl_surface.h"
32 #include "ui/gl/gpu_preference.h"
40 class SubscriptionRefSet
;
47 class GpuVideoDecodeAccelerator
;
48 class GpuVideoEncodeAccelerator
;
50 struct WaitForCommandState
;
52 class GpuCommandBufferStub
53 : public GpuMemoryManagerClient
,
56 public base::SupportsWeakPtr
<GpuCommandBufferStub
> {
58 class DestructionObserver
{
60 // Called in Destroy(), before the context/surface are released.
61 virtual void OnWillDestroyStub() = 0;
64 virtual ~DestructionObserver() {}
67 typedef base::Callback
<void(const std::vector
<ui::LatencyInfo
>&)>
72 base::SingleThreadTaskRunner
* task_runner
,
73 GpuCommandBufferStub
* share_group
,
74 const gfx::GLSurfaceHandle
& handle
,
75 gpu::gles2::MailboxManager
* mailbox_manager
,
76 gpu::gles2::SubscriptionRefSet
* subscription_ref_set
,
77 gpu::ValueStateMap
* pending_valuebuffer_state
,
78 const gfx::Size
& size
,
79 const gpu::gles2::DisallowedFeatures
& disallowed_features
,
80 const std::vector
<int32
>& attribs
,
81 gfx::GpuPreference gpu_preference
,
82 bool use_virtualized_gl_context
,
86 GpuWatchdog
* watchdog
,
88 const GURL
& active_url
);
90 ~GpuCommandBufferStub() override
;
92 // IPC::Listener implementation:
93 bool OnMessageReceived(const IPC::Message
& message
) override
;
95 // IPC::Sender implementation:
96 bool Send(IPC::Message
* msg
) override
;
98 // GpuMemoryManagerClient implementation:
99 gfx::Size
GetSurfaceSize() const override
;
100 gpu::gles2::MemoryTracker
* GetMemoryTracker() const override
;
101 void SetMemoryAllocation(const gpu::MemoryAllocation
& allocation
) override
;
102 void SuggestHaveFrontBuffer(bool suggest_have_frontbuffer
) override
;
103 bool GetTotalGpuMemory(uint64
* bytes
) override
;
105 // Whether this command buffer can currently handle IPC messages.
108 // Whether there are commands in the buffer that haven't been processed.
109 bool HasUnprocessedCommands();
111 gpu::gles2::GLES2Decoder
* decoder() const { return decoder_
.get(); }
112 gpu::GpuScheduler
* scheduler() const { return scheduler_
.get(); }
113 GpuChannel
* channel() const { return channel_
; }
115 // Unique command buffer ID for this command buffer stub.
116 uint64_t command_buffer_id() const { return command_buffer_id_
; }
118 // Identifies the target surface.
119 int32
surface_id() const { return surface_id_
; }
121 // Identifies the various GpuCommandBufferStubs in the GPU process belonging
122 // to the same renderer process.
123 int32
route_id() const { return route_id_
; }
125 // Identifies the stream for this command buffer.
126 int32
stream_id() const { return stream_id_
; }
128 gfx::GpuPreference
gpu_preference() { return gpu_preference_
; }
130 int32
GetRequestedAttribute(int attr
) const;
132 // Sends a message to the console.
133 void SendConsoleMessage(int32 id
, const std::string
& message
);
135 void SendCachedShader(const std::string
& key
, const std::string
& shader
);
137 gfx::GLSurface
* surface() const { return surface_
.get(); }
139 void AddDestructionObserver(DestructionObserver
* observer
);
140 void RemoveDestructionObserver(DestructionObserver
* observer
);
142 // Associates a sync point to this stub. When the stub is destroyed, it will
143 // retire all sync points that haven't been previously retired.
144 void AddSyncPoint(uint32 sync_point
, bool retire
);
146 void SetPreemptByFlag(scoped_refptr
<gpu::PreemptionFlag
> flag
);
148 void SetLatencyInfoCallback(const LatencyInfoCallback
& callback
);
150 void MarkContextLost();
152 const gpu::gles2::FeatureInfo
* GetFeatureInfo() const;
154 void SendSwapBuffersCompleted(
155 const std::vector
<ui::LatencyInfo
>& latency_info
,
156 gfx::SwapResult result
);
157 void SendUpdateVSyncParameters(base::TimeTicks timebase
,
158 base::TimeDelta interval
);
161 GpuMemoryManager
* GetMemoryManager() const;
167 // Cleans up and sends reply if OnInitialize failed.
168 void OnInitializeFailed(IPC::Message
* reply_message
);
171 void OnInitialize(base::SharedMemoryHandle shared_state_shm
,
172 IPC::Message
* reply_message
);
173 void OnSetGetBuffer(int32 shm_id
, IPC::Message
* reply_message
);
174 void OnProduceFrontBuffer(const gpu::Mailbox
& mailbox
);
175 void OnGetState(IPC::Message
* reply_message
);
176 void OnWaitForTokenInRange(int32 start
,
178 IPC::Message
* reply_message
);
179 void OnWaitForGetOffsetInRange(int32 start
,
181 IPC::Message
* reply_message
);
182 void OnAsyncFlush(int32 put_offset
, uint32 flush_count
,
183 const std::vector
<ui::LatencyInfo
>& latency_info
);
184 void OnRegisterTransferBuffer(int32 id
,
185 base::SharedMemoryHandle transfer_buffer
,
187 void OnDestroyTransferBuffer(int32 id
);
188 void OnGetTransferBuffer(int32 id
, IPC::Message
* reply_message
);
190 void OnCreateVideoDecoder(media::VideoCodecProfile profile
,
192 IPC::Message
* reply_message
);
193 void OnCreateVideoEncoder(media::VideoPixelFormat input_format
,
194 const gfx::Size
& input_visible_size
,
195 media::VideoCodecProfile output_profile
,
196 uint32 initial_bitrate
,
198 IPC::Message
* reply_message
);
200 void OnSetSurfaceVisible(bool visible
);
202 void OnEnsureBackbuffer();
204 void OnRetireSyncPoint(uint32 sync_point
);
205 bool OnWaitSyncPoint(uint32 sync_point
);
206 void OnWaitSyncPointCompleted(uint32 sync_point
);
207 void OnSignalSyncPoint(uint32 sync_point
, uint32 id
);
208 void OnSignalSyncPointAck(uint32 id
);
209 void OnSignalQuery(uint32 query
, uint32 id
);
211 void OnSetClientHasMemoryAllocationChangedCallback(bool has_callback
);
213 void OnCreateImage(int32 id
,
214 gfx::GpuMemoryBufferHandle handle
,
216 gfx::BufferFormat format
,
217 uint32 internalformat
);
218 void OnDestroyImage(int32 id
);
219 void OnCreateStreamTexture(uint32 texture_id
,
223 void OnCommandProcessed();
225 void OnSchedulingChanged(bool scheduled
);
229 // Wrapper for GpuScheduler::PutChanged that sets the crash report URL.
232 // Poll the command buffer to execute work.
236 // Schedule processing of delayed work. This updates the time at which
237 // delayed work should be processed. |process_delayed_work_time_| is
238 // updated to current time + delay. Call this after processing some amount
240 void ScheduleDelayedWork(base::TimeDelta delay
);
242 bool CheckContextLost();
243 void CheckCompleteWaits();
244 void PullTextureUpdates(uint32 sync_point
);
246 // The lifetime of objects of this class is managed by a GpuChannel. The
247 // GpuChannels destroy all the GpuCommandBufferStubs that they own when they
248 // are destroyed. So a raw pointer is safe.
249 GpuChannel
* channel_
;
251 // Task runner for main thread.
252 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
254 // The group of contexts that share namespaces with this context.
255 scoped_refptr
<gpu::gles2::ContextGroup
> context_group_
;
258 gfx::GLSurfaceHandle handle_
;
259 gfx::Size initial_size_
;
260 gpu::gles2::DisallowedFeatures disallowed_features_
;
261 std::vector
<int32
> requested_attribs_
;
262 gfx::GpuPreference gpu_preference_
;
263 bool use_virtualized_gl_context_
;
264 const uint64_t command_buffer_id_
;
265 const int32 stream_id_
;
266 const int32 route_id_
;
267 const int32 surface_id_
;
269 uint32 last_flush_count_
;
271 scoped_ptr
<gpu::CommandBufferService
> command_buffer_
;
272 scoped_ptr
<gpu::gles2::GLES2Decoder
> decoder_
;
273 scoped_ptr
<gpu::GpuScheduler
> scheduler_
;
274 scoped_refptr
<gfx::GLSurface
> surface_
;
276 scoped_ptr
<GpuMemoryManagerClientState
> memory_manager_client_state_
;
277 // The last memory allocation received from the GpuMemoryManager (used to
278 // elide redundant work).
279 bool last_memory_allocation_valid_
;
280 gpu::MemoryAllocation last_memory_allocation_
;
282 GpuWatchdog
* watchdog_
;
284 base::ObserverList
<DestructionObserver
> destruction_observers_
;
286 // A queue of sync points associated with this stub.
287 std::deque
<uint32
> sync_points_
;
288 bool waiting_for_sync_point_
;
290 base::TimeTicks process_delayed_work_time_
;
291 uint32_t previous_processed_num_
;
292 base::TimeTicks last_idle_time_
;
294 scoped_refptr
<gpu::PreemptionFlag
> preemption_flag_
;
296 LatencyInfoCallback latency_info_callback_
;
299 size_t active_url_hash_
;
301 size_t total_gpu_memory_
;
302 scoped_ptr
<WaitForCommandState
> wait_for_token_
;
303 scoped_ptr
<WaitForCommandState
> wait_for_get_offset_
;
305 DISALLOW_COPY_AND_ASSIGN(GpuCommandBufferStub
);
308 } // namespace content
310 #endif // CONTENT_COMMON_GPU_GPU_COMMAND_BUFFER_STUB_H_