1 /*
2  * Copyright (C) 2022 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 #ifndef ANDROID_HWC_HOSTUTILS_H
18 #define ANDROID_HWC_HOSTUTILS_H
19 
20 #include "Common.h"
21 #include "HostConnection.h"
22 
23 namespace aidl::android::hardware::graphics::composer3::impl {
24 
25 HostConnection* createOrGetHostConnection();
26 
getAndValidateHostConnection(HostConnection ** ppHostCon,ExtendedRCEncoderContext ** ppRcEnc)27 inline HWC3::Error getAndValidateHostConnection(HostConnection** ppHostCon,
28                                                 ExtendedRCEncoderContext** ppRcEnc) {
29     *ppHostCon = nullptr;
30     *ppRcEnc = nullptr;
31 
32     HostConnection* hostCon = createOrGetHostConnection();
33     if (!hostCon) {
34         ALOGE("%s: Failed to get host connection\n", __FUNCTION__);
35         return HWC3::Error::NoResources;
36     }
37     ExtendedRCEncoderContext* rcEnc = hostCon->rcEncoder();
38     if (!rcEnc) {
39         ALOGE("%s: Failed to get renderControl encoder context\n", __FUNCTION__);
40         return HWC3::Error::NoResources;
41     }
42 
43     *ppHostCon = hostCon;
44     *ppRcEnc = rcEnc;
45     return HWC3::Error::None;
46 }
47 
48 #define DEFINE_AND_VALIDATE_HOST_CONNECTION                               \
49     HostConnection* hostCon;                                              \
50     ExtendedRCEncoderContext* rcEnc;                                      \
51     {                                                                     \
52         HWC3::Error res = getAndValidateHostConnection(&hostCon, &rcEnc); \
53         if (res != HWC3::Error::None) {                                   \
54             return res;                                                   \
55         }                                                                 \
56     }
57 }  // namespace aidl::android::hardware::graphics::composer3::impl
58 
59 #endif
60