• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "../AndroidPipe.h"
16 
17 #include "base/Lock.h"
18 #include "../opengles.h"
19 #include <assert.h>
20 #include <atomic>
21 #include <memory>
22 #include <unordered_set>
23 
24 #include <string.h>
25 
26 using android::base::AutoLock;
27 using android::base::Lock;
28 
29 namespace android {
30 namespace opengl {
31 
32 struct ProcessPipeIdRegistry {
33     Lock lock;
34     std::unordered_set<uint64_t> ids;
35 };
36 
37 static ProcessPipeIdRegistry sRegistry;
38 
sIdExistsInRegistry(uint64_t id)39 static bool sIdExistsInRegistry(uint64_t id) {
40     AutoLock lock(sRegistry.lock);
41     auto it = sRegistry.ids.find(id);
42     return it != sRegistry.ids.end();
43 }
44 
45 namespace {
46 
47 // GLProcessPipe is a pipe service that is used for releasing graphics resources
48 // per guest process. At the time being, guest processes can acquire host color
49 // buffer handles / EGLImage handles and they need to be properly released when
50 // guest process exits unexpectedly. This class is used to detect if guest
51 // process exits, so that a proper cleanup function can be called.
52 
53 // It is done by setting up a pipe per guest process before acquiring color
54 // buffer handles. When guest process exits, the pipe will be closed, and
55 // onGuestClose() will trigger the cleanup path.
56 
57 class GLProcessPipe : public AndroidPipe {
58 public:
59     //////////////////////////////////////////////////////////////////////////
60     // The pipe service class for this implementation.
61     class Service : public AndroidPipe::Service {
62     public:
Service()63         Service() : AndroidPipe::Service("GLProcessPipe") {}
64 
canLoad() const65         bool canLoad() const override { return true; }
66 
create(void * hwPipe,const char * args)67         AndroidPipe* create(void* hwPipe, const char* args) override {
68             return new GLProcessPipe(hwPipe, this);
69         }
70 
load(void * hwPipe,const char * args,base::Stream * stream)71         AndroidPipe* load(void* hwPipe, const char* args,
72                          base::Stream* stream) override {
73             return new GLProcessPipe(hwPipe, this, stream);
74         }
75 
preLoad(base::Stream * stream)76         void preLoad(base::Stream* stream) override {
77             GLProcessPipe::s_headId.store(stream->getBe64());
78         }
79 
preSave(base::Stream * stream)80         void preSave(base::Stream* stream) override {
81             stream->putBe64(GLProcessPipe::s_headId.load());
82         }
83     };
84 
GLProcessPipe(void * hwPipe,Service * service,base::Stream * loadStream=nullptr)85     GLProcessPipe(void* hwPipe, Service* service,
86                   base::Stream* loadStream = nullptr)
87         : AndroidPipe(hwPipe, service) {
88         if (loadStream) {
89             m_uniqueId = loadStream->getBe64();
90             m_hasData = (loadStream->getByte() != 0);
91         } else {
92             m_uniqueId = ++s_headId;
93         }
94         AutoLock lock(sRegistry.lock);
95         sRegistry.ids.insert(m_uniqueId);
96     }
97 
~GLProcessPipe()98     ~GLProcessPipe() {
99         AutoLock lock(sRegistry.lock);
100         sRegistry.ids.erase(m_uniqueId);
101     }
102 
onSave(base::Stream * stream)103     void onSave(base::Stream* stream) override {
104         stream->putBe64(m_uniqueId);
105         stream->putByte(m_hasData ? 1 : 0);
106     }
107 
onGuestClose(PipeCloseReason reason)108     void onGuestClose(PipeCloseReason reason) override {
109         if (sIdExistsInRegistry(m_uniqueId)) {
110             android_cleanupProcGLObjects(m_uniqueId);
111         }
112         delete this;
113     }
114 
onGuestPoll() const115     unsigned onGuestPoll() const override {
116         return PIPE_POLL_IN | PIPE_POLL_OUT;
117     }
118 
onGuestRecv(AndroidPipeBuffer * buffers,int numBuffers)119     int onGuestRecv(AndroidPipeBuffer* buffers, int numBuffers) override {
120         assert(buffers[0].size >= 8);
121         if (m_hasData) {
122             m_hasData = false;
123             memcpy(buffers[0].data, (const char*)&m_uniqueId, sizeof(m_uniqueId));
124             return sizeof(m_uniqueId);
125         } else {
126             return 0;
127         }
128     }
129 
onGuestSend(const AndroidPipeBuffer * buffers,int numBuffers,void ** newPipePtr)130     int onGuestSend(const AndroidPipeBuffer* buffers,
131                             int numBuffers,
132                             void** newPipePtr) override {
133         // The guest is supposed to send us a confirm code first. The code is
134         // 100 (4 byte integer).
135         assert(buffers[0].size >= 4);
136         int32_t confirmInt = *((int32_t*)buffers[0].data);
137         assert(confirmInt == 100);
138         (void)confirmInt;
139         m_hasData = true;
140         return buffers[0].size;
141     }
142 
onGuestWantWakeOn(int flags)143     void onGuestWantWakeOn(int flags) override {}
144 
145 private:
146     // An identifier for the guest process corresponding to this pipe.
147     // With very high probability, all currently-active processes have unique
148     // identifiers, since the IDs are assigned sequentially from a 64-bit ID
149     // space.
150     // Please change it if you ever have a use case that exhausts them
151     uint64_t m_uniqueId;
152     bool m_hasData = false;
153     static std::atomic<uint64_t> s_headId;
154 
155 };
156 
157 std::atomic<uint64_t> GLProcessPipe::s_headId {0};
158 
159 }
160 
registerGLProcessPipeService()161 void registerGLProcessPipeService() {
162     AndroidPipe::Service::add(std::make_unique<GLProcessPipe::Service>());
163 }
164 
forEachProcessPipeId(std::function<void (uint64_t)> f)165 void forEachProcessPipeId(std::function<void(uint64_t)> f) {
166     AutoLock lock(sRegistry.lock);
167     for (auto id: sRegistry.ids) {
168         f(id);
169     }
170 }
171 
forEachProcessPipeIdRunAndErase(std::function<void (uint64_t)> f)172 void forEachProcessPipeIdRunAndErase(std::function<void(uint64_t)> f) {
173     AutoLock lock(sRegistry.lock);
174     auto it = sRegistry.ids.begin();
175     while (it != sRegistry.ids.end()) {
176         f(*it);
177         it = sRegistry.ids.erase(it);
178     }
179 }
180 
181 }
182 }
183