1 /*
2 * Copyright (C) 2016 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 "renderControl_enc.h"
18 #include "qemu_pipe.h"
19
20 #include <cutils/log.h>
21 #include <pthread.h>
22
23 static int sProcPipe = 0;
24 static pthread_once_t sProcPipeOnce = PTHREAD_ONCE_INIT;
25 // sProcUID is a unique ID per process assigned by the host.
26 // It is different from getpid().
27 static uint64_t sProcUID = 0;
28
29 // processPipeInitOnce is used to generate a process unique ID (puid).
30 // processPipeInitOnce will only be called at most once per process.
31 // Use it with pthread_once for thread safety.
32 // The host associates resources with process unique ID (puid) for memory cleanup.
33 // It will fallback to the default path if the host does not support it.
34 // Processes are identified by acquiring a per-process 64bit unique ID from the
35 // host.
processPipeInitOnce()36 static void processPipeInitOnce() {
37 sProcPipe = qemu_pipe_open("GLProcessPipe");
38 if (sProcPipe < 0) {
39 sProcPipe = 0;
40 ALOGW("Process pipe failed");
41 return;
42 }
43 // Send a confirmation int to the host
44 int32_t confirmInt = 100;
45 ssize_t stat = 0;
46 do {
47 stat = ::write(sProcPipe, (const char*)&confirmInt,
48 sizeof(confirmInt));
49 } while (stat < 0 && errno == EINTR);
50
51 if (stat != sizeof(confirmInt)) { // failed
52 close(sProcPipe);
53 sProcPipe = 0;
54 ALOGW("Process pipe failed");
55 return;
56 }
57
58 // Ask the host for per-process unique ID
59 do {
60 stat = ::read(sProcPipe, (char*)&sProcUID,
61 sizeof(sProcUID));
62 } while (stat < 0 && errno == EINTR);
63
64 if (stat != sizeof(sProcUID)) {
65 close(sProcPipe);
66 sProcPipe = 0;
67 sProcUID = 0;
68 ALOGW("Process pipe failed");
69 return;
70 }
71 }
72
processPipeInit(renderControl_encoder_context_t * rcEnc)73 bool processPipeInit(renderControl_encoder_context_t *rcEnc) {
74 pthread_once(&sProcPipeOnce, processPipeInitOnce);
75 if (!sProcPipe) return false;
76 rcEnc->rcSetPuid(rcEnc, sProcUID);
77 return true;
78 }
79