Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / gpu / frame_swap_message_queue.h
blob51eb229ed7871ed8367a2b39c19c741d868ee28b
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_
8 #include <map>
9 #include <vector>
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"
20 namespace IPC {
21 class Message;
24 namespace content {
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> {
32 public:
33 class CONTENT_EXPORT SendMessageScope {
34 public:
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,
50 bool* is_first);
52 // Returns true if there are no messages in the queue.
53 bool Empty() const;
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
68 // messages.
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
74 // called.
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);
91 private:
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_