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/scoped_vector.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.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/gpu_memory_buffer.h"
28 #include "ui/gfx/native_widget_types.h"
29 #include "ui/gfx/size.h"
30 #include "ui/gl/gl_surface.h"
31 #include "ui/gl/gpu_preference.h"
44 class GpuVideoDecodeAccelerator
;
45 class GpuVideoEncodeAccelerator
;
47 struct WaitForCommandState
;
49 class GpuCommandBufferStub
50 : public GpuMemoryManagerClient
,
53 public base::SupportsWeakPtr
<GpuCommandBufferStub
> {
55 class DestructionObserver
{
57 // Called in Destroy(), before the context/surface are released.
58 virtual void OnWillDestroyStub() = 0;
61 virtual ~DestructionObserver() {}
64 typedef base::Callback
<void(const std::vector
<ui::LatencyInfo
>&)>
69 GpuCommandBufferStub
* share_group
,
70 const gfx::GLSurfaceHandle
& handle
,
71 gpu::gles2::MailboxManager
* mailbox_manager
,
72 const gfx::Size
& size
,
73 const gpu::gles2::DisallowedFeatures
& disallowed_features
,
74 const std::vector
<int32
>& attribs
,
75 gfx::GpuPreference gpu_preference
,
76 bool use_virtualized_gl_context
,
79 GpuWatchdog
* watchdog
,
81 const GURL
& active_url
);
83 ~GpuCommandBufferStub() override
;
85 // IPC::Listener implementation:
86 bool OnMessageReceived(const IPC::Message
& message
) override
;
88 // IPC::Sender implementation:
89 bool Send(IPC::Message
* msg
) override
;
91 // GpuMemoryManagerClient implementation:
92 gfx::Size
GetSurfaceSize() const override
;
93 gpu::gles2::MemoryTracker
* GetMemoryTracker() const override
;
94 void SetMemoryAllocation(const gpu::MemoryAllocation
& allocation
) override
;
95 void SuggestHaveFrontBuffer(bool suggest_have_frontbuffer
) override
;
96 bool GetTotalGpuMemory(uint64
* bytes
) override
;
98 // Whether this command buffer can currently handle IPC messages.
101 // If the command buffer is pre-empted and cannot process commands.
102 bool IsPreempted() const {
103 return scheduler_
.get() && scheduler_
->IsPreempted();
106 // Whether there are commands in the buffer that haven't been processed.
107 bool HasUnprocessedCommands();
109 gpu::gles2::GLES2Decoder
* decoder() const { return decoder_
.get(); }
110 gpu::GpuScheduler
* scheduler() const { return scheduler_
.get(); }
111 GpuChannel
* channel() const { return channel_
; }
113 // Identifies the target surface.
114 int32
surface_id() const { return surface_id_
; }
116 // Identifies the various GpuCommandBufferStubs in the GPU process belonging
117 // to the same renderer process.
118 int32
route_id() const { return route_id_
; }
120 gfx::GpuPreference
gpu_preference() { return gpu_preference_
; }
122 int32
GetRequestedAttribute(int attr
) const;
124 // Sends a message to the console.
125 void SendConsoleMessage(int32 id
, const std::string
& message
);
127 void SendCachedShader(const std::string
& key
, const std::string
& shader
);
129 gfx::GLSurface
* surface() const { return surface_
.get(); }
131 void AddDestructionObserver(DestructionObserver
* observer
);
132 void RemoveDestructionObserver(DestructionObserver
* observer
);
134 // Associates a sync point to this stub. When the stub is destroyed, it will
135 // retire all sync points that haven't been previously retired.
136 void AddSyncPoint(uint32 sync_point
);
138 void SetPreemptByFlag(scoped_refptr
<gpu::PreemptionFlag
> flag
);
140 void SetLatencyInfoCallback(const LatencyInfoCallback
& callback
);
142 void MarkContextLost();
144 uint64
GetMemoryUsage() const;
146 void SwapBuffersCompleted(const std::vector
<ui::LatencyInfo
>& latency_info
);
149 GpuMemoryManager
* GetMemoryManager() const;
153 // Cleans up and sends reply if OnInitialize failed.
154 void OnInitializeFailed(IPC::Message
* reply_message
);
157 void OnInitialize(base::SharedMemoryHandle shared_state_shm
,
158 IPC::Message
* reply_message
);
159 void OnSetGetBuffer(int32 shm_id
, IPC::Message
* reply_message
);
160 void OnProduceFrontBuffer(const gpu::Mailbox
& mailbox
);
161 void OnGetState(IPC::Message
* reply_message
);
162 void OnWaitForTokenInRange(int32 start
,
164 IPC::Message
* reply_message
);
165 void OnWaitForGetOffsetInRange(int32 start
,
167 IPC::Message
* reply_message
);
168 void OnAsyncFlush(int32 put_offset
, uint32 flush_count
,
169 const std::vector
<ui::LatencyInfo
>& latency_info
);
170 void OnRescheduled();
171 void OnRegisterTransferBuffer(int32 id
,
172 base::SharedMemoryHandle transfer_buffer
,
174 void OnDestroyTransferBuffer(int32 id
);
175 void OnGetTransferBuffer(int32 id
, IPC::Message
* reply_message
);
177 void OnCreateVideoDecoder(media::VideoCodecProfile profile
,
179 IPC::Message
* reply_message
);
180 void OnCreateVideoEncoder(media::VideoFrame::Format input_format
,
181 const gfx::Size
& input_visible_size
,
182 media::VideoCodecProfile output_profile
,
183 uint32 initial_bitrate
,
185 IPC::Message
* reply_message
);
187 void OnSetSurfaceVisible(bool visible
);
189 void OnEnsureBackbuffer();
191 void OnRetireSyncPoint(uint32 sync_point
);
192 bool OnWaitSyncPoint(uint32 sync_point
);
193 void OnSyncPointRetired();
194 void OnSignalSyncPoint(uint32 sync_point
, uint32 id
);
195 void OnSignalSyncPointAck(uint32 id
);
196 void OnSignalQuery(uint32 query
, uint32 id
);
198 void OnSetClientHasMemoryAllocationChangedCallback(bool has_callback
);
200 void OnCreateImage(int32 id
,
201 gfx::GpuMemoryBufferHandle handle
,
203 gfx::GpuMemoryBuffer::Format format
,
204 uint32 internalformat
);
205 void OnDestroyImage(int32 id
);
207 void OnCommandProcessed();
209 void OnCreateStreamTexture(
210 uint32 texture_id
, int32 stream_id
, bool* succeeded
);
214 // Wrapper for GpuScheduler::PutChanged that sets the crash report URL.
217 // Poll the command buffer to execute work.
220 // Whether this command buffer needs to be polled again in the future.
223 void ScheduleDelayedWork(int64 delay
);
225 bool CheckContextLost();
226 void CheckCompleteWaits();
228 // The lifetime of objects of this class is managed by a GpuChannel. The
229 // GpuChannels destroy all the GpuCommandBufferStubs that they own when they
230 // are destroyed. So a raw pointer is safe.
231 GpuChannel
* channel_
;
233 // The group of contexts that share namespaces with this context.
234 scoped_refptr
<gpu::gles2::ContextGroup
> context_group_
;
236 gfx::GLSurfaceHandle handle_
;
237 gfx::Size initial_size_
;
238 gpu::gles2::DisallowedFeatures disallowed_features_
;
239 std::vector
<int32
> requested_attribs_
;
240 gfx::GpuPreference gpu_preference_
;
241 bool use_virtualized_gl_context_
;
245 uint32 last_flush_count_
;
247 scoped_ptr
<gpu::CommandBufferService
> command_buffer_
;
248 scoped_ptr
<gpu::gles2::GLES2Decoder
> decoder_
;
249 scoped_ptr
<gpu::GpuScheduler
> scheduler_
;
250 scoped_refptr
<gfx::GLSurface
> surface_
;
252 scoped_ptr
<GpuMemoryManagerClientState
> memory_manager_client_state_
;
253 // The last memory allocation received from the GpuMemoryManager (used to
254 // elide redundant work).
255 bool last_memory_allocation_valid_
;
256 gpu::MemoryAllocation last_memory_allocation_
;
258 GpuWatchdog
* watchdog_
;
260 ObserverList
<DestructionObserver
> destruction_observers_
;
262 // A queue of sync points associated with this stub.
263 std::deque
<uint32
> sync_points_
;
264 int sync_point_wait_count_
;
266 bool delayed_work_scheduled_
;
267 uint64 previous_messages_processed_
;
268 base::TimeTicks last_idle_time_
;
270 scoped_refptr
<gpu::PreemptionFlag
> preemption_flag_
;
272 LatencyInfoCallback latency_info_callback_
;
275 size_t active_url_hash_
;
277 size_t total_gpu_memory_
;
278 scoped_ptr
<WaitForCommandState
> wait_for_token_
;
279 scoped_ptr
<WaitForCommandState
> wait_for_get_offset_
;
281 DISALLOW_COPY_AND_ASSIGN(GpuCommandBufferStub
);
284 } // namespace content
286 #endif // CONTENT_COMMON_GPU_GPU_COMMAND_BUFFER_STUB_H_