blob: ad0527ac866031586bfd98a33a19673b3827a228 [file] [log] [blame]
Yahan Zhou41050592016-07-28 16:31:59 -07001/*
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
Yahan Zhoue8cf63d2016-09-22 12:33:50 -070017#include "renderControl_enc.h"
Lingfeng Yang88c170c2016-11-30 00:52:35 +000018#include "qemu_pipe.h"
Yahan Zhoue8cf63d2016-09-22 12:33:50 -070019
Luca Stefani1cb647a2019-03-07 21:58:17 +010020#if PLATFORM_SDK_VERSION < 26
Logan Chiend7bf00d2018-09-21 06:30:09 +000021#include <cutils/log.h>
Luca Stefani1cb647a2019-03-07 21:58:17 +010022#else
23#include <log/log.h>
24#endif
Yahan Zhou41050592016-07-28 16:31:59 -070025#include <pthread.h>
Lingfeng Yange38d15c2018-09-24 16:24:01 -070026#include <errno.h>
Yahan Zhou41050592016-07-28 16:31:59 -070027
Lingfeng Yange38d15c2018-09-24 16:24:01 -070028static QEMU_PIPE_HANDLE sProcPipe = 0;
Yahan Zhou41050592016-07-28 16:31:59 -070029static 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().
32static uint64_t sProcUID = 0;
33
Yahan Zhoue8cf63d2016-09-22 12:33:50 -070034// processPipeInitOnce is used to generate a process unique ID (puid).
Yahan Zhou41050592016-07-28 16:31:59 -070035// 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.
41static void processPipeInitOnce() {
42 sProcPipe = qemu_pipe_open("GLProcessPipe");
Lingfeng Yange38d15c2018-09-24 16:24:01 -070043 if (!qemu_pipe_valid(sProcPipe)) {
Yahan Zhou41050592016-07-28 16:31:59 -070044 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 {
Lingfeng Yange38d15c2018-09-24 16:24:01 -070052 stat =
53 qemu_pipe_write(sProcPipe, (const char*)&confirmInt,
Yahan Zhou41050592016-07-28 16:31:59 -070054 sizeof(confirmInt));
55 } while (stat < 0 && errno == EINTR);
56
57 if (stat != sizeof(confirmInt)) { // failed
Lingfeng Yange38d15c2018-09-24 16:24:01 -070058 qemu_pipe_close(sProcPipe);
Yahan Zhou41050592016-07-28 16:31:59 -070059 sProcPipe = 0;
60 ALOGW("Process pipe failed");
61 return;
62 }
63
64 // Ask the host for per-process unique ID
65 do {
Lingfeng Yange38d15c2018-09-24 16:24:01 -070066 stat =
67 qemu_pipe_read(sProcPipe, (char*)&sProcUID,
68 sizeof(sProcUID));
Yahan Zhou41050592016-07-28 16:31:59 -070069 } while (stat < 0 && errno == EINTR);
70
71 if (stat != sizeof(sProcUID)) {
Lingfeng Yange38d15c2018-09-24 16:24:01 -070072 qemu_pipe_close(sProcPipe);
Yahan Zhou41050592016-07-28 16:31:59 -070073 sProcPipe = 0;
74 sProcUID = 0;
75 ALOGW("Process pipe failed");
76 return;
77 }
78}
79
Yahan Zhoue8cf63d2016-09-22 12:33:50 -070080bool processPipeInit(renderControl_encoder_context_t *rcEnc) {
Yahan Zhou41050592016-07-28 16:31:59 -070081 pthread_once(&sProcPipeOnce, processPipeInitOnce);
Yahan Zhoue8cf63d2016-09-22 12:33:50 -070082 if (!sProcPipe) return false;
83 rcEnc->rcSetPuid(rcEnc, sProcUID);
84 return true;
Lingfeng Yang88c170c2016-11-30 00:52:35 +000085}