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 "HalDisplay.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/DisplayState.h>
23 #include <aidl/android/hardware/automotive/evs/EvsResult.h>
24 #include <android-base/logging.h>
25 #include <android-base/stringprintf.h>
26 
27 #include <cinttypes>
28 
29 namespace aidl::android::automotive::evs::implementation {
30 
31 using ::aidl::android::hardware::automotive::evs::BufferDesc;
32 using ::aidl::android::hardware::automotive::evs::DisplayDesc;
33 using ::aidl::android::hardware::automotive::evs::DisplayState;
34 using ::aidl::android::hardware::automotive::evs::EvsResult;
35 using ::aidl::android::hardware::automotive::evs::IEvsDisplay;
36 using ::android::base::StringAppendF;
37 using ::ndk::ScopedAStatus;
38 
HalDisplay(std::shared_ptr<IEvsDisplay> display,int32_t id)39 HalDisplay::HalDisplay(std::shared_ptr<IEvsDisplay> display, int32_t id) :
40       mHwDisplay(display), mId(id) {
41     // nothing to do.
42 }
43 
~HalDisplay()44 HalDisplay::~HalDisplay() {
45     shutdown();
46 }
47 
shutdown()48 void HalDisplay::shutdown() {
49     // simply release a shared pointer to remote display object.
50     mHwDisplay.reset();
51 }
52 
53 /**
54  * Returns a shared pointer to remote display object.
55  */
getHwDisplay()56 std::shared_ptr<IEvsDisplay> HalDisplay::getHwDisplay() {
57     return mHwDisplay;
58 }
59 
60 /**
61  * Gets basic display information from a hardware display object
62  * and returns.
63  */
getDisplayInfo(DisplayDesc * _aidl_return)64 ScopedAStatus HalDisplay::getDisplayInfo(DisplayDesc* _aidl_return) {
65     if (!mHwDisplay) {
66         return Utils::buildScopedAStatusFromEvsResult(EvsResult::RESOURCE_NOT_AVAILABLE);
67     }
68 
69     return mHwDisplay->getDisplayInfo(_aidl_return);
70 }
71 
72 /**
73  * Gets current display state from a hardware display object and return.
74  */
getDisplayState(DisplayState * _aidl_return)75 ScopedAStatus HalDisplay::getDisplayState(DisplayState* _aidl_return) {
76     if (!mHwDisplay) {
77         return Utils::buildScopedAStatusFromEvsResult(EvsResult::RESOURCE_NOT_AVAILABLE);
78     }
79 
80     return mHwDisplay->getDisplayState(_aidl_return);
81 }
82 
83 /**
84  * Returns a handle to a frame buffer associated with the display.
85  */
getTargetBuffer(BufferDesc * _aidl_return)86 ScopedAStatus HalDisplay::getTargetBuffer(BufferDesc* _aidl_return) {
87     if (!mHwDisplay) {
88         return Utils::buildScopedAStatusFromEvsResult(EvsResult::RESOURCE_NOT_AVAILABLE);
89     }
90 
91     return mHwDisplay->getTargetBuffer(_aidl_return);
92 }
93 
94 /**
95  * Notifies the display that the buffer is ready to be used.
96  */
returnTargetBufferForDisplay(const BufferDesc & buffer)97 ScopedAStatus HalDisplay::returnTargetBufferForDisplay(const BufferDesc& buffer) {
98     if (!mHwDisplay) {
99         return Utils::buildScopedAStatusFromEvsResult(EvsResult::RESOURCE_NOT_AVAILABLE);
100     }
101 
102     return mHwDisplay->returnTargetBufferForDisplay(buffer);
103 }
104 
105 /**
106  * Sets the display state as what the clients wants.
107  */
setDisplayState(DisplayState state)108 ScopedAStatus HalDisplay::setDisplayState(DisplayState state) {
109     if (!mHwDisplay) {
110         return Utils::buildScopedAStatusFromEvsResult(EvsResult::RESOURCE_NOT_AVAILABLE);
111     }
112 
113     return mHwDisplay->setDisplayState(state);
114 }
115 
toString(const char * indent)116 std::string HalDisplay::toString(const char* indent) {
117     std::string buffer;
118     if (mId == kDisplayIdUnavailable) {
119         // Display identifier has not set
120         StringAppendF(&buffer, "HalDisplay: Display port is unknown.\n");
121     } else {
122         StringAppendF(&buffer, "HalDisplay: Display port %" PRId32 "\n", mId);
123     }
124 
125     DisplayDesc displayDesc;
126     auto status = getDisplayInfo(&displayDesc);
127     if (status.isOk()) {
128         StringAppendF(&buffer, "%sWidth: %" PRId32 "\n", indent, displayDesc.width);
129         StringAppendF(&buffer, "%sHeight: %" PRId32 "\n", indent, displayDesc.height);
130         StringAppendF(&buffer, "%sRotation: %" PRId32 "\n", indent,
131                       static_cast<int32_t>(displayDesc.orientation));
132     }
133 
134     return buffer;
135 }
136 
137 }  // namespace aidl::android::automotive::evs::implementation
138