1 // Copyright 2014 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 #include "content/renderer/gpu/frame_swap_message_queue.h"
9 #include "base/containers/hash_tables.h"
10 #include "base/logging.h"
11 #include "base/stl_util.h"
12 #include "ipc/ipc_message.h"
18 class FrameSwapMessageSubQueue
{
20 FrameSwapMessageSubQueue() {}
21 virtual ~FrameSwapMessageSubQueue() {}
22 virtual bool Empty() const = 0;
23 virtual void QueueMessage(int source_frame_number
,
24 scoped_ptr
<IPC::Message
> msg
,
26 virtual void DrainMessages(int source_frame_number
,
27 ScopedVector
<IPC::Message
>* messages
) = 0;
30 DISALLOW_COPY_AND_ASSIGN(FrameSwapMessageSubQueue
);
35 // Queue specific to MESSAGE_DELIVERY_POLICY_WITH_VISUAL_STATE.
36 class SendMessageScopeImpl
: public FrameSwapMessageQueue::SendMessageScope
{
38 SendMessageScopeImpl(base::Lock
* lock
) : auto_lock_(*lock
) {}
39 ~SendMessageScopeImpl() override
{}
42 base::AutoLock auto_lock_
;
45 class VisualStateQueue
: public FrameSwapMessageSubQueue
{
49 ~VisualStateQueue() override
{
50 for (VisualStateQueueMap::iterator i
= queue_
.begin(); i
!= queue_
.end();
52 STLDeleteElements(&i
->second
);
56 bool Empty() const override
{ return queue_
.empty(); }
58 void QueueMessage(int source_frame_number
,
59 scoped_ptr
<IPC::Message
> msg
,
60 bool* is_first
) override
{
62 *is_first
= (queue_
.count(source_frame_number
) == 0);
64 queue_
[source_frame_number
].push_back(msg
.release());
67 void DrainMessages(int source_frame_number
,
68 ScopedVector
<IPC::Message
>* messages
) override
{
69 VisualStateQueueMap::iterator end
= queue_
.upper_bound(source_frame_number
);
70 for (VisualStateQueueMap::iterator i
= queue_
.begin(); i
!= end
; i
++) {
71 DCHECK(i
->first
<= source_frame_number
);
72 messages
->insert(messages
->end(), i
->second
.begin(), i
->second
.end());
75 queue_
.erase(queue_
.begin(), end
);
79 typedef std::map
<int, std::vector
<IPC::Message
*> > VisualStateQueueMap
;
80 VisualStateQueueMap queue_
;
82 DISALLOW_COPY_AND_ASSIGN(VisualStateQueue
);
85 // Queue specific to MESSAGE_DELIVERY_POLICY_WITH_NEXT_SWAP.
86 class SwapQueue
: public FrameSwapMessageSubQueue
{
89 bool Empty() const override
{ return queue_
.empty(); }
91 void QueueMessage(int source_frame_number
,
92 scoped_ptr
<IPC::Message
> msg
,
93 bool* is_first
) override
{
96 queue_
.push_back(msg
.release());
99 void DrainMessages(int source_frame_number
,
100 ScopedVector
<IPC::Message
>* messages
) override
{
101 messages
->insert(messages
->end(), queue_
.begin(), queue_
.end());
106 ScopedVector
<IPC::Message
> queue_
;
108 DISALLOW_COPY_AND_ASSIGN(SwapQueue
);
113 FrameSwapMessageQueue::FrameSwapMessageQueue()
114 : visual_state_queue_(new VisualStateQueue()),
115 swap_queue_(new SwapQueue()) {
118 FrameSwapMessageQueue::~FrameSwapMessageQueue() {
121 bool FrameSwapMessageQueue::Empty() const {
122 base::AutoLock
lock(lock_
);
123 return next_drain_messages_
.empty() && visual_state_queue_
->Empty() &&
124 swap_queue_
->Empty();
127 FrameSwapMessageSubQueue
* FrameSwapMessageQueue::GetSubQueue(
128 MessageDeliveryPolicy policy
) {
130 case MESSAGE_DELIVERY_POLICY_WITH_NEXT_SWAP
:
131 return swap_queue_
.get();
133 case MESSAGE_DELIVERY_POLICY_WITH_VISUAL_STATE
:
134 return visual_state_queue_
.get();
141 void FrameSwapMessageQueue::QueueMessageForFrame(MessageDeliveryPolicy policy
,
142 int source_frame_number
,
143 scoped_ptr
<IPC::Message
> msg
,
145 base::AutoLock
lock(lock_
);
146 GetSubQueue(policy
)->QueueMessage(source_frame_number
, msg
.Pass(), is_first
);
149 void FrameSwapMessageQueue::DidActivate(int source_frame_number
) {
150 base::AutoLock
lock(lock_
);
151 visual_state_queue_
->DrainMessages(source_frame_number
,
152 &next_drain_messages_
);
155 void FrameSwapMessageQueue::DidSwap(int source_frame_number
) {
156 base::AutoLock
lock(lock_
);
157 swap_queue_
->DrainMessages(0, &next_drain_messages_
);
160 void FrameSwapMessageQueue::DidNotSwap(int source_frame_number
,
161 cc::SwapPromise::DidNotSwapReason reason
,
162 ScopedVector
<IPC::Message
>* messages
) {
163 base::AutoLock
lock(lock_
);
165 case cc::SwapPromise::SWAP_FAILS
:
166 case cc::SwapPromise::COMMIT_NO_UPDATE
:
167 swap_queue_
->DrainMessages(source_frame_number
, messages
);
168 visual_state_queue_
->DrainMessages(source_frame_number
, messages
);
170 case cc::SwapPromise::COMMIT_FAILS
:
171 case cc::SwapPromise::ACTIVATION_FAILS
:
172 // Do not queue any responses here. If ACTIVATION_FAILS or
173 // COMMIT_FAILS the renderer is shutting down, which will result
174 // in the RenderFrameHostImpl destructor firing the remaining
175 // response callbacks itself.
180 void FrameSwapMessageQueue::DrainMessages(
181 ScopedVector
<IPC::Message
>* messages
) {
182 lock_
.AssertAcquired();
183 messages
->insert(messages
->end(),
184 next_drain_messages_
.begin(),
185 next_drain_messages_
.end());
186 next_drain_messages_
.weak_clear();
189 scoped_ptr
<FrameSwapMessageQueue::SendMessageScope
>
190 FrameSwapMessageQueue::AcquireSendMessageScope() {
191 return make_scoped_ptr(new SendMessageScopeImpl(&lock_
));
195 void FrameSwapMessageQueue::TransferMessages(ScopedVector
<IPC::Message
>& source
,
196 vector
<IPC::Message
>* dest
) {
197 for (vector
<IPC::Message
*>::iterator i
= source
.begin(); i
!= source
.end();
206 } // namespace content