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