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 #include "HidlDisplay.h"
18 
19 #include "utils/include/Utils.h"
20 
21 #include <aidl/android/hardware/automotive/evs/BufferDesc.h>
22 #include <aidl/android/hardware/automotive/evs/DisplayDesc.h>
23 #include <aidl/android/hardware/automotive/evs/DisplayState.h>
24 #include <aidl/android/hardware/automotive/evs/EvsResult.h>
25 #include <android-base/logging.h>
26 #include <ui/DisplayMode.h>
27 #include <ui/DisplayState.h>
28 
29 namespace aidl::android::automotive::evs::implementation {
30 
31 namespace hidlevs = ::android::hardware::automotive::evs;
32 
33 using ::aidl::android::hardware::automotive::evs::BufferDesc;
34 using ::aidl::android::hardware::automotive::evs::DisplayDesc;
35 using ::aidl::android::hardware::automotive::evs::DisplayState;
36 using ::aidl::android::hardware::automotive::evs::EvsResult;
37 using ::android::hardware::Return;
38 
~HidlDisplay()39 HidlDisplay::~HidlDisplay() {
40     // simply release a shared pointer to remote display object.
41     mAidlDisplay = nullptr;
42 }
43 
getDisplayInfo(getDisplayInfo_cb _hidl_cb)44 Return<void> HidlDisplay::getDisplayInfo(getDisplayInfo_cb _hidl_cb) {
45     if (!mAidlDisplay) {
46         LOG(ERROR) << "A reference to AIDL IEvsDisplay is invalid.";
47         _hidl_cb({});
48         return {};
49     }
50 
51     DisplayDesc aidlDesc;
52     if (auto status = mAidlDisplay->getDisplayInfo(&aidlDesc); !status.isOk()) {
53         LOG(ERROR) << "Failed to read a display information";
54         _hidl_cb({});
55         return {};
56     }
57 
58     hidlevs::V1_0::DisplayDesc hidlDesc = {
59             .displayId = aidlDesc.id,
60             .vendorFlags = static_cast<uint32_t>(aidlDesc.vendorFlags),
61     };
62     _hidl_cb(std::move(hidlDesc));
63     return {};
64 }
65 
setDisplayState(hidlevs::V1_0::DisplayState state)66 Return<hidlevs::V1_0::EvsResult> HidlDisplay::setDisplayState(hidlevs::V1_0::DisplayState state) {
67     if (!mAidlDisplay) {
68         LOG(ERROR) << "A reference to AIDL IEvsDisplay is invalid.";
69         return hidlevs::V1_0::EvsResult::UNDERLYING_SERVICE_ERROR;
70     }
71 
72     if (auto status = mAidlDisplay->setDisplayState(Utils::makeFromHidl(state)); !status.isOk()) {
73         return Utils::makeToHidl(static_cast<EvsResult>(status.getServiceSpecificError()));
74     }
75     return hidlevs::V1_0::EvsResult::OK;
76 }
77 
getDisplayState()78 Return<hidlevs::V1_0::DisplayState> HidlDisplay::getDisplayState() {
79     if (!mAidlDisplay) {
80         LOG(ERROR) << "A reference to AIDL IEvsDisplay is invalid.";
81         return Utils::makeToHidl(DisplayState::DEAD);
82     }
83 
84     DisplayState aidlState;
85     if (auto status = mAidlDisplay->getDisplayState(&aidlState); !status.isOk()) {
86         return Utils::makeToHidl(DisplayState::DEAD);
87     }
88 
89     return Utils::makeToHidl(aidlState);
90 }
91 
getTargetBuffer(getTargetBuffer_cb _hidl_cb)92 Return<void> HidlDisplay::getTargetBuffer(getTargetBuffer_cb _hidl_cb) {
93     if (!mAidlDisplay) {
94         LOG(ERROR) << "A reference to AIDL IEvsDisplay is invalid.";
95         _hidl_cb({});
96         return {};
97     }
98 
99     BufferDesc aidlBuffer;
100     auto status = mAidlDisplay->getTargetBuffer(&aidlBuffer);
101     if (!status.isOk()) {
102         LOG(ERROR) << "Failed to get a target buffer";
103         _hidl_cb({});
104         return {};
105     }
106 
107     // We already own a copy of a buffer handle so do not need to duplicate it
108     // again.
109     hidlevs::V1_0::BufferDesc hidlBuffer = Utils::makeToHidlV1_0(aidlBuffer, /* doDup = */ false);
110     mHeldBuffer = std::move(aidlBuffer);
111     _hidl_cb(hidlBuffer);
112     return {};
113 }
114 
returnTargetBufferForDisplay(const hidlevs::V1_0::BufferDesc & buffer)115 Return<hidlevs::V1_0::EvsResult> HidlDisplay::returnTargetBufferForDisplay(
116         const hidlevs::V1_0::BufferDesc& buffer) {
117     if (!mAidlDisplay) {
118         LOG(ERROR) << "A reference to AIDL IEvsDisplay is invalid.";
119         return hidlevs::V1_0::EvsResult::UNDERLYING_SERVICE_ERROR;
120     }
121 
122     if (buffer.bufferId != mHeldBuffer.bufferId) {
123         LOG(WARNING) << "Ignores a request to return a buffer " << buffer.bufferId << "; a buffer "
124                      << mHeldBuffer.bufferId << " is held.";
125     } else {
126         auto status = mAidlDisplay->returnTargetBufferForDisplay(std::move(mHeldBuffer));
127         if (!status.isOk()) {
128             LOG(WARNING) << "Failed to return a buffer " << mHeldBuffer.bufferId;
129         }
130     }
131 
132     return hidlevs::V1_0::EvsResult::OK;
133 }
134 
getDisplayInfo_1_1(getDisplayInfo_1_1_cb _hidl_cb)135 Return<void> HidlDisplay::getDisplayInfo_1_1(getDisplayInfo_1_1_cb _hidl_cb) {
136     ::android::hardware::hidl_vec<uint8_t> hidlMode(sizeof(::android::ui::DisplayMode));
137     ::android::hardware::hidl_vec<uint8_t> hidlState(sizeof(::android::ui::DisplayState));
138 
139     if (!mAidlDisplay) {
140         LOG(ERROR) << "A reference to AIDL IEvsDisplay is invalid.";
141         _hidl_cb(hidlMode, hidlState);
142         return {};
143     }
144 
145     DisplayDesc aidlDesc;
146     if (auto status = mAidlDisplay->getDisplayInfo(&aidlDesc); !status.isOk()) {
147         LOG(ERROR) << "Failed to read a display information";
148         _hidl_cb(hidlMode, hidlState);
149         return {};
150     }
151 
152     ::android::ui::DisplayMode* pMode =
153             reinterpret_cast<::android::ui::DisplayMode*>(hidlMode.data());
154     ::android::ui::DisplayState* pState =
155             reinterpret_cast<::android::ui::DisplayState*>(hidlState.data());
156     pMode->resolution.width = aidlDesc.width;
157     pMode->resolution.height = aidlDesc.height;
158     pState->orientation = static_cast<::android::ui::Rotation>(aidlDesc.orientation);
159 
160     _hidl_cb(hidlMode, hidlState);
161     return {};
162 }
163 
164 }  // namespace aidl::android::automotive::evs::implementation
165