Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / android / in_process / synchronous_compositor_registry.cc
blobd1017977ba55107fe3727910280a3b7859cae9a7
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/browser/android/in_process/synchronous_compositor_registry.h"
7 #include "content/browser/android/in_process/synchronous_compositor_impl.h"
8 #include "content/public/browser/browser_thread.h"
10 namespace content {
12 namespace {
13 base::LazyInstance<SynchronousCompositorRegistry> g_compositor_registry =
14 LAZY_INSTANCE_INITIALIZER;
17 // static
18 SynchronousCompositorRegistry* SynchronousCompositorRegistry::GetInstance() {
19 return g_compositor_registry.Pointer();
22 SynchronousCompositorRegistry::SynchronousCompositorRegistry() {
23 DCHECK(CalledOnValidThread());
26 SynchronousCompositorRegistry::~SynchronousCompositorRegistry() {
27 DCHECK(CalledOnValidThread());
30 void SynchronousCompositorRegistry::RegisterCompositor(
31 int routing_id,
32 SynchronousCompositorImpl* compositor) {
33 DCHECK(CalledOnValidThread());
34 DCHECK(compositor);
35 Entry& entry = entry_map_[routing_id];
36 DCHECK(!entry.compositor);
37 entry.compositor = compositor;
38 CheckIsReady(routing_id);
41 void SynchronousCompositorRegistry::UnregisterCompositor(
42 int routing_id,
43 SynchronousCompositorImpl* compositor) {
44 DCHECK(CalledOnValidThread());
45 DCHECK(compositor);
46 DCHECK(entry_map_.find(routing_id) != entry_map_.end());
47 Entry& entry = entry_map_[routing_id];
48 DCHECK_EQ(compositor, entry.compositor);
50 if (entry.IsReady())
51 UnregisterObjects(routing_id);
52 entry.compositor = nullptr;
53 RemoveEntryIfNeeded(routing_id);
56 void SynchronousCompositorRegistry::RegisterBeginFrameSource(
57 int routing_id,
58 SynchronousCompositorExternalBeginFrameSource* begin_frame_source) {
59 DCHECK(CalledOnValidThread());
60 DCHECK(begin_frame_source);
61 Entry& entry = entry_map_[routing_id];
62 DCHECK(!entry.begin_frame_source);
63 entry.begin_frame_source = begin_frame_source;
64 CheckIsReady(routing_id);
67 void SynchronousCompositorRegistry::UnregisterBeginFrameSource(
68 int routing_id,
69 SynchronousCompositorExternalBeginFrameSource* begin_frame_source) {
70 DCHECK(CalledOnValidThread());
71 DCHECK(begin_frame_source);
72 DCHECK(entry_map_.find(routing_id) != entry_map_.end());
73 Entry& entry = entry_map_[routing_id];
74 DCHECK_EQ(begin_frame_source, entry.begin_frame_source);
76 if (entry.IsReady())
77 UnregisterObjects(routing_id);
78 entry.begin_frame_source = nullptr;
79 RemoveEntryIfNeeded(routing_id);
82 void SynchronousCompositorRegistry::RegisterOutputSurface(
83 int routing_id,
84 SynchronousCompositorOutputSurface* output_surface) {
85 DCHECK(CalledOnValidThread());
86 DCHECK(output_surface);
87 Entry& entry = entry_map_[routing_id];
88 DCHECK(!entry.output_surface);
89 entry.output_surface = output_surface;
90 CheckIsReady(routing_id);
93 void SynchronousCompositorRegistry::UnregisterOutputSurface(
94 int routing_id,
95 SynchronousCompositorOutputSurface* output_surface) {
96 DCHECK(CalledOnValidThread());
97 DCHECK(output_surface);
98 DCHECK(entry_map_.find(routing_id) != entry_map_.end());
99 Entry& entry = entry_map_[routing_id];
100 DCHECK_EQ(output_surface, entry.output_surface);
102 if (entry.IsReady())
103 UnregisterObjects(routing_id);
104 entry.output_surface = nullptr;
105 RemoveEntryIfNeeded(routing_id);
108 void SynchronousCompositorRegistry::RegisterInputHandler(
109 int routing_id,
110 SynchronousInputHandlerProxy* synchronous_input_handler_proxy) {
111 DCHECK(CalledOnValidThread());
112 DCHECK(synchronous_input_handler_proxy);
113 Entry& entry = entry_map_[routing_id];
114 DCHECK(!entry.synchronous_input_handler_proxy);
115 entry.synchronous_input_handler_proxy = synchronous_input_handler_proxy;
116 CheckIsReady(routing_id);
119 void SynchronousCompositorRegistry::UnregisterInputHandler(int routing_id) {
120 DCHECK(CalledOnValidThread());
121 DCHECK(entry_map_.find(routing_id) != entry_map_.end());
122 Entry& entry = entry_map_[routing_id];
124 if (entry.IsReady())
125 UnregisterObjects(routing_id);
126 entry.synchronous_input_handler_proxy = nullptr;
127 RemoveEntryIfNeeded(routing_id);
130 void SynchronousCompositorRegistry::CheckIsReady(int routing_id) {
131 DCHECK(entry_map_.find(routing_id) != entry_map_.end());
132 Entry& entry = entry_map_[routing_id];
133 if (entry.IsReady()) {
134 entry.compositor->DidInitializeRendererObjects(
135 entry.output_surface, entry.begin_frame_source,
136 entry.synchronous_input_handler_proxy);
140 void SynchronousCompositorRegistry::UnregisterObjects(int routing_id) {
141 DCHECK(entry_map_.find(routing_id) != entry_map_.end());
142 Entry& entry = entry_map_[routing_id];
143 DCHECK(entry.IsReady());
144 entry.compositor->DidDestroyRendererObjects();
147 void SynchronousCompositorRegistry::RemoveEntryIfNeeded(int routing_id) {
148 DCHECK(entry_map_.find(routing_id) != entry_map_.end());
149 Entry& entry = entry_map_[routing_id];
150 if (!entry.compositor && !entry.begin_frame_source && !entry.output_surface &&
151 !entry.synchronous_input_handler_proxy) {
152 entry_map_.erase(routing_id);
156 bool SynchronousCompositorRegistry::CalledOnValidThread() const {
157 return BrowserThread::CurrentlyOn(BrowserThread::UI);
160 SynchronousCompositorRegistry::Entry::Entry()
161 : compositor(nullptr),
162 begin_frame_source(nullptr),
163 output_surface(nullptr),
164 synchronous_input_handler_proxy(nullptr) {}
166 bool SynchronousCompositorRegistry::Entry::IsReady() {
167 return compositor && begin_frame_source && output_surface &&
168 synchronous_input_handler_proxy;
171 } // namespace content