• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CONTENT_RENDERER_GPU_FRAME_SWAP_MESSAGE_QUEUE_H_
6 #define CONTENT_RENDERER_GPU_FRAME_SWAP_MESSAGE_QUEUE_H_
7 
8 #include <map>
9 #include <vector>
10 
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/base/swap_promise.h"
17 #include "content/common/content_export.h"
18 #include "content/renderer/message_delivery_policy.h"
19 
20 namespace IPC {
21 class Message;
22 };
23 
24 namespace content {
25 
26 class FrameSwapMessageSubQueue;
27 
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:
~SendMessageScope()35     virtual ~SendMessageScope() {}
36   };
37 
38   FrameSwapMessageQueue();
39 
40   // Queues message to be returned on a matching DrainMessages call.
41   //
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);
51 
52   // Returns true if there are no messages in the queue.
53   bool Empty() const;
54 
55   // Should be called when a successful swap occurs. The messages for that swap
56   // can be obtained by calling DrainMessages.
57   //
58   // |source_frame_number| frame number for which the swap occurred.
59   void DidSwap(int source_frame_number);
60 
61   // Should be called when we know a swap will not occur. This also means we
62   // won't be expecting a DrainMessages call.
63   //
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);
72 
73   // A SendMessageScope object must be held by the caller when this method is
74   // called.
75   //
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);
80 
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.
83   //
84   // Returns an object that must be kept in scope till an IPC message containing
85   // |messages| is sent.
86   scoped_ptr<SendMessageScope> AcquireSendMessageScope();
87 
88   static void TransferMessages(ScopedVector<IPC::Message>& source,
89                                std::vector<IPC::Message>* dest);
90 
91  private:
92   friend class base::RefCountedThreadSafe<FrameSwapMessageQueue>;
93 
94   FrameSwapMessageSubQueue* GetSubQueue(MessageDeliveryPolicy policy);
95 
96   ~FrameSwapMessageQueue();
97 
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_;
102 
103   DISALLOW_COPY_AND_ASSIGN(FrameSwapMessageQueue);
104 };
105 
106 }  // namespace content
107 
108 #endif  // CONTENT_RENDERER_GPU_FRAME_SWAP_MESSAGE_QUEUE_H_
109