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 #ifndef CONTENT_RENDERER_GPU_FRAME_SWAP_MESSAGE_QUEUE_H_
6 #define CONTENT_RENDERER_GPU_FRAME_SWAP_MESSAGE_QUEUE_H_
11 #include "base/auto_reset.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/synchronization/lock.h"
16 #include "cc/output/swap_promise.h"
17 #include "content/common/content_export.h"
18 #include "content/renderer/message_delivery_policy.h"
26 class FrameSwapMessageSubQueue
;
28 // Queue used to keep track of which IPC::Messages should be sent along with a
29 // particular compositor frame swap.
30 class CONTENT_EXPORT FrameSwapMessageQueue
31 : public base::RefCountedThreadSafe
<FrameSwapMessageQueue
> {
33 class CONTENT_EXPORT SendMessageScope
{
35 virtual ~SendMessageScope() {}
38 FrameSwapMessageQueue();
40 // Queues message to be returned on a matching DrainMessages call.
42 // |policy| determines how messages are matched with DrainMessages calls.
43 // |source_frame_number| frame number to queue |msg| for.
44 // |msg| - message to queue. The method takes ownership of |msg|.
45 // |is_first| - output parameter. Set to true if this was the first message
46 // enqueued for the given source_frame_number.
47 void QueueMessageForFrame(MessageDeliveryPolicy policy
,
48 int source_frame_number
,
49 scoped_ptr
<IPC::Message
> msg
,
52 // Returns true if there are no messages in the queue.
55 // Should be called when a successful swap occurs. The messages for that swap
56 // can be obtained by calling DrainMessages.
58 // |source_frame_number| frame number for which the swap occurred.
59 void DidSwap(int source_frame_number
);
61 // Should be called when we know a swap will not occur. This also means we
62 // won't be expecting a DrainMessages call.
64 // |source_frame_number| frame number for which the swap will not occur.
65 // |reason| reason for the which the swap will not occur.
66 // |messages| depending on |reason| it may make sense to deliver certain
67 // messages asynchronously. This vector will contain those
69 void DidNotSwap(int source_frame_number
,
70 cc::SwapPromise::DidNotSwapReason reason
,
71 ScopedVector
<IPC::Message
>* messages
);
73 // A SendMessageScope object must be held by the caller when this method is
76 // |messages| vector to store messages, it's not cleared, only appended to.
77 // The method will append messages queued for frame numbers lower
78 // or equal to |source_frame_number|
79 void DrainMessages(ScopedVector
<IPC::Message
>* messages
);
81 // SendMessageScope is used to make sure that messages sent from different
82 // threads (impl/main) are scheduled in the right order on the IO threads.
84 // Returns an object that must be kept in scope till an IPC message containing
85 // |messages| is sent.
86 scoped_ptr
<SendMessageScope
> AcquireSendMessageScope();
88 static void TransferMessages(ScopedVector
<IPC::Message
>& source
,
89 std::vector
<IPC::Message
>* dest
);
92 friend class base::RefCountedThreadSafe
<FrameSwapMessageQueue
>;
94 FrameSwapMessageSubQueue
* GetSubQueue(MessageDeliveryPolicy policy
);
96 ~FrameSwapMessageQueue();
98 mutable base::Lock lock_
;
99 scoped_ptr
<FrameSwapMessageSubQueue
> visual_state_queue_
;
100 scoped_ptr
<FrameSwapMessageSubQueue
> swap_queue_
;
101 ScopedVector
<IPC::Message
> next_drain_messages_
;
103 DISALLOW_COPY_AND_ASSIGN(FrameSwapMessageQueue
);
106 } // namespace content
108 #endif // CONTENT_RENDERER_GPU_FRAME_SWAP_MESSAGE_QUEUE_H_