Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / android / in_process / synchronous_compositor_registry.cc
blobc37331073a9b5bc70ec89a8c242e22caa01318f2
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::CheckIsReady(int routing_id) {
109 DCHECK(entry_map_.find(routing_id) != entry_map_.end());
110 Entry& entry = entry_map_[routing_id];
111 if (entry.IsReady()) {
112 entry.compositor->DidInitializeRendererObjects(entry.output_surface,
113 entry.begin_frame_source);
117 void SynchronousCompositorRegistry::UnregisterObjects(int routing_id) {
118 DCHECK(entry_map_.find(routing_id) != entry_map_.end());
119 Entry& entry = entry_map_[routing_id];
120 DCHECK(entry.IsReady());
121 entry.compositor->DidDestroyRendererObjects();
124 void SynchronousCompositorRegistry::RemoveEntryIfNeeded(int routing_id) {
125 DCHECK(entry_map_.find(routing_id) != entry_map_.end());
126 Entry& entry = entry_map_[routing_id];
127 if (!entry.compositor && !entry.begin_frame_source && !entry.output_surface) {
128 entry_map_.erase(routing_id);
132 bool SynchronousCompositorRegistry::CalledOnValidThread() const {
133 return BrowserThread::CurrentlyOn(BrowserThread::UI);
136 SynchronousCompositorRegistry::Entry::Entry()
137 : compositor(nullptr),
138 begin_frame_source(nullptr),
139 output_surface(nullptr) {
142 bool SynchronousCompositorRegistry::Entry::IsReady() {
143 return compositor && begin_frame_source && output_surface;
146 } // namespace content