Revert of cc: Implement shared worker contexts. (patchset #9 id:160001 of https:...
[chromium-blink-merge.git] / content / browser / android / in_process / synchronous_compositor_registry.cc
blob84c45a24d49197dcf6ef92442ac5da42d3bd76b3
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 cc::InputHandler* input_handler,
111 SynchronousInputHandlerProxy* synchronous_input_handler_proxy) {
112 DCHECK(CalledOnValidThread());
113 DCHECK(input_handler);
114 Entry& entry = entry_map_[routing_id];
115 DCHECK(!entry.input_handler);
116 entry.input_handler = input_handler;
117 entry.synchronous_input_handler_proxy = synchronous_input_handler_proxy;
118 CheckIsReady(routing_id);
121 void SynchronousCompositorRegistry::UnregisterInputHandler(int routing_id) {
122 DCHECK(CalledOnValidThread());
123 DCHECK(entry_map_.find(routing_id) != entry_map_.end());
124 Entry& entry = entry_map_[routing_id];
126 if (entry.IsReady())
127 UnregisterObjects(routing_id);
128 entry.input_handler = nullptr;
129 RemoveEntryIfNeeded(routing_id);
132 void SynchronousCompositorRegistry::CheckIsReady(int routing_id) {
133 DCHECK(entry_map_.find(routing_id) != entry_map_.end());
134 Entry& entry = entry_map_[routing_id];
135 if (entry.IsReady()) {
136 entry.compositor->DidInitializeRendererObjects(
137 entry.output_surface, entry.begin_frame_source, entry.input_handler,
138 entry.synchronous_input_handler_proxy);
142 void SynchronousCompositorRegistry::UnregisterObjects(int routing_id) {
143 DCHECK(entry_map_.find(routing_id) != entry_map_.end());
144 Entry& entry = entry_map_[routing_id];
145 DCHECK(entry.IsReady());
146 entry.compositor->DidDestroyRendererObjects();
149 void SynchronousCompositorRegistry::RemoveEntryIfNeeded(int routing_id) {
150 DCHECK(entry_map_.find(routing_id) != entry_map_.end());
151 Entry& entry = entry_map_[routing_id];
152 if (!entry.compositor && !entry.begin_frame_source && !entry.output_surface &&
153 !entry.input_handler) {
154 entry_map_.erase(routing_id);
158 bool SynchronousCompositorRegistry::CalledOnValidThread() const {
159 return BrowserThread::CurrentlyOn(BrowserThread::UI);
162 SynchronousCompositorRegistry::Entry::Entry()
163 : compositor(nullptr),
164 begin_frame_source(nullptr),
165 output_surface(nullptr),
166 input_handler(nullptr),
167 synchronous_input_handler_proxy(nullptr) {}
169 bool SynchronousCompositorRegistry::Entry::IsReady() {
170 return compositor && begin_frame_source && output_surface && input_handler &&
171 synchronous_input_handler_proxy;
174 } // namespace content