OSDN Git Service

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