Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 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 | |
Yahan Zhou | e8cf63d | 2016-09-22 12:33:50 -0700 | [diff] [blame] | 17 | #include "renderControl_enc.h" |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 18 | #include "qemu_pipe.h" |
Yahan Zhou | e8cf63d | 2016-09-22 12:33:50 -0700 | [diff] [blame] | 19 | |
Luca Stefani | 1cb647a | 2019-03-07 21:58:17 +0100 | [diff] [blame] | 20 | #if PLATFORM_SDK_VERSION < 26 |
Logan Chien | d7bf00d | 2018-09-21 06:30:09 +0000 | [diff] [blame] | 21 | #include <cutils/log.h> |
Luca Stefani | 1cb647a | 2019-03-07 21:58:17 +0100 | [diff] [blame] | 22 | #else |
| 23 | #include <log/log.h> |
| 24 | #endif |
Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 25 | #include <pthread.h> |
Lingfeng Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 26 | #include <errno.h> |
Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 27 | |
Lingfeng Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 28 | static QEMU_PIPE_HANDLE sProcPipe = 0; |
Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 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 | |
Yahan Zhou | e8cf63d | 2016-09-22 12:33:50 -0700 | [diff] [blame] | 34 | // processPipeInitOnce is used to generate a process unique ID (puid). |
Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 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. |
| 41 | static void processPipeInitOnce() { |
| 42 | sProcPipe = qemu_pipe_open("GLProcessPipe"); |
Lingfeng Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 43 | if (!qemu_pipe_valid(sProcPipe)) { |
Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 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 { |
Lingfeng Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 52 | stat = |
| 53 | qemu_pipe_write(sProcPipe, (const char*)&confirmInt, |
Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 54 | sizeof(confirmInt)); |
| 55 | } while (stat < 0 && errno == EINTR); |
| 56 | |
| 57 | if (stat != sizeof(confirmInt)) { // failed |
Lingfeng Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 58 | qemu_pipe_close(sProcPipe); |
Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 59 | sProcPipe = 0; |
| 60 | ALOGW("Process pipe failed"); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | // Ask the host for per-process unique ID |
| 65 | do { |
Lingfeng Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 66 | stat = |
| 67 | qemu_pipe_read(sProcPipe, (char*)&sProcUID, |
| 68 | sizeof(sProcUID)); |
Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 69 | } while (stat < 0 && errno == EINTR); |
| 70 | |
| 71 | if (stat != sizeof(sProcUID)) { |
Lingfeng Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 72 | qemu_pipe_close(sProcPipe); |
Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 73 | sProcPipe = 0; |
| 74 | sProcUID = 0; |
| 75 | ALOGW("Process pipe failed"); |
| 76 | return; |
| 77 | } |
| 78 | } |
| 79 | |
Yahan Zhou | e8cf63d | 2016-09-22 12:33:50 -0700 | [diff] [blame] | 80 | bool processPipeInit(renderControl_encoder_context_t *rcEnc) { |
Yahan Zhou | 4105059 | 2016-07-28 16:31:59 -0700 | [diff] [blame] | 81 | pthread_once(&sProcPipeOnce, processPipeInitOnce); |
Yahan Zhou | e8cf63d | 2016-09-22 12:33:50 -0700 | [diff] [blame] | 82 | if (!sProcPipe) return false; |
| 83 | rcEnc->rcSetPuid(rcEnc, sProcUID); |
| 84 | return true; |
Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 85 | } |