• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 #include "RenderThreadInfo.h"
18 
19 #include "base/Lookup.h"
20 #include "base/StreamSerializing.h"
21 #include "base/Lock.h"
22 
23 #include "FrameBuffer.h"
24 
25 #include <unordered_map>
26 #include <unordered_set>
27 
28 using android::base::AutoLock;
29 using android::base::Stream;
30 using android::base::Lock;
31 
32 static thread_local RenderThreadInfo* s_threadInfoPtr;
33 
34 struct RenderThreadRegistry {
35     Lock lock;
36     std::unordered_set<RenderThreadInfo*> threadInfos;
37 };
38 
39 static RenderThreadRegistry sRegistry;
40 
RenderThreadInfo()41 RenderThreadInfo::RenderThreadInfo() {
42     s_threadInfoPtr = this;
43     AutoLock lock(sRegistry.lock);
44     sRegistry.threadInfos.insert(this);
45 }
46 
~RenderThreadInfo()47 RenderThreadInfo::~RenderThreadInfo() {
48     s_threadInfoPtr = nullptr;
49     AutoLock lock(sRegistry.lock);
50     sRegistry.threadInfos.erase(this);
51 }
52 
get()53 RenderThreadInfo* RenderThreadInfo::get() {
54     return s_threadInfoPtr;
55 }
56 
57 // Loop over all active render thread infos. Takes the global render thread info lock.
forAllRenderThreadInfos(std::function<void (RenderThreadInfo *)> f)58 void RenderThreadInfo::forAllRenderThreadInfos(std::function<void(RenderThreadInfo*)> f) {
59     AutoLock lock(sRegistry.lock);
60     for (auto info: sRegistry.threadInfos) {
61         f(info);
62     }
63 }
64 
onSave(Stream * stream)65 void RenderThreadInfo::onSave(Stream* stream) {
66     if (currContext) {
67         stream->putBe32(currContext->getHndl());
68     } else {
69         stream->putBe32(0);
70     }
71     if (currDrawSurf) {
72         stream->putBe32(currDrawSurf->getHndl());
73     } else {
74         stream->putBe32(0);
75     }
76     if (currReadSurf) {
77         stream->putBe32(currReadSurf->getHndl());
78     } else {
79         stream->putBe32(0);
80     }
81 
82     saveCollection(stream, m_contextSet, [](Stream* stream, HandleType val) {
83         stream->putBe32(val);
84     });
85     saveCollection(stream, m_windowSet, [](Stream* stream, HandleType val) {
86         stream->putBe32(val);
87     });
88 
89     stream->putBe64(m_puid);
90 
91     // No need to associate render threads with sync threads
92     // if there is a global sync thread. This is only needed
93     // to maintain backward compatibility with snapshot file format.
94     // (Used to be: stream->putBe64(syncThreadAlias))
95     stream->putBe64(0);
96 }
97 
onLoad(Stream * stream)98 bool RenderThreadInfo::onLoad(Stream* stream) {
99     FrameBuffer* fb = FrameBuffer::getFB();
100     assert(fb);
101     HandleType ctxHndl = stream->getBe32();
102     HandleType drawSurf = stream->getBe32();
103     HandleType readSurf = stream->getBe32();
104     currContextHandleFromLoad = ctxHndl;
105     currDrawSurfHandleFromLoad = drawSurf;
106     currReadSurfHandleFromLoad = readSurf;
107 
108     fb->lock();
109     currContext = fb->getContext_locked(ctxHndl);
110     currDrawSurf = fb->getWindowSurface_locked(drawSurf);
111     currReadSurf = fb->getWindowSurface_locked(readSurf);
112     fb->unlock();
113 
114     loadCollection(stream, &m_contextSet, [](Stream* stream) {
115         return stream->getBe32();
116     });
117     loadCollection(stream, &m_windowSet, [](Stream* stream) {
118         return stream->getBe32();
119     });
120 
121     m_puid = stream->getBe64();
122 
123     // (Used to be: syncThreadAlias = stream->getBe64())
124     stream->getBe64();
125 
126     return true;
127 }
128 
postLoadRefreshCurrentContextSurfacePtrs()129 void RenderThreadInfo::postLoadRefreshCurrentContextSurfacePtrs() {
130     FrameBuffer* fb = FrameBuffer::getFB();
131     assert(fb);
132     fb->lock();
133     currContext = fb->getContext_locked(currContextHandleFromLoad);
134     currDrawSurf = fb->getWindowSurface_locked(currDrawSurfHandleFromLoad);
135     currReadSurf = fb->getWindowSurface_locked(currReadSurfHandleFromLoad);
136     fb->unlock();
137 }
138