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 activation occurs. The messages for
56 // that activation can be obtained by calling DrainMessages.
58 // |source_frame_number| frame number for which the activate occurred.
59 void DidActivate(int source_frame_number
);
61 // Should be called when a successful swap occurs. The messages for that
62 // swap can be obtained by calling DrainMessages.
64 // |source_frame_number| frame number for which the swap occurred.
65 void DidSwap(int source_frame_number
);
67 // Should be called when we know a swap will not occur.
69 // |source_frame_number| frame number for which the swap will not occur.
70 // |reason| reason for the which the swap will not occur.
71 // |messages| depending on |reason| it may make sense to deliver certain
72 // messages asynchronously. This vector will contain those
74 void DidNotSwap(int source_frame_number
,
75 cc::SwapPromise::DidNotSwapReason reason
,
76 ScopedVector
<IPC::Message
>* messages
);
78 // A SendMessageScope object must be held by the caller when this method is
81 // |messages| vector to store messages, it's not cleared, only appended to.
82 // The method will append messages queued for frame numbers lower
83 // or equal to |source_frame_number|
84 void DrainMessages(ScopedVector
<IPC::Message
>* messages
);
86 // SendMessageScope is used to make sure that messages sent from different
87 // threads (impl/main) are scheduled in the right order on the IO threads.
89 // Returns an object that must be kept in scope till an IPC message containing
90 // |messages| is sent.
91 scoped_ptr
<SendMessageScope
> AcquireSendMessageScope();
93 static void TransferMessages(ScopedVector
<IPC::Message
>& source
,
94 std::vector
<IPC::Message
>* dest
);
97 friend class base::RefCountedThreadSafe
<FrameSwapMessageQueue
>;
99 FrameSwapMessageSubQueue
* GetSubQueue(MessageDeliveryPolicy policy
);
101 ~FrameSwapMessageQueue();
103 mutable base::Lock lock_
;
104 scoped_ptr
<FrameSwapMessageSubQueue
> visual_state_queue_
;
105 scoped_ptr
<FrameSwapMessageSubQueue
> swap_queue_
;
106 ScopedVector
<IPC::Message
> next_drain_messages_
;
108 DISALLOW_COPY_AND_ASSIGN(FrameSwapMessageQueue
);
111 } // namespace content
113 #endif // CONTENT_RENDERER_GPU_FRAME_SWAP_MESSAGE_QUEUE_H_