1 /*
2  * Copyright (C) 2023 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 "EvsGlDisplay.h"
18 
19 #include <aidl/android/hardware/automotive/evs/EvsResult.h>
20 #include <aidl/android/hardware/graphics/common/BufferUsage.h>
21 #include <aidl/android/hardware/graphics/common/PixelFormat.h>
22 #include <aidlcommonsupport/NativeHandle.h>
23 #include <android-base/thread_annotations.h>
24 #include <linux/time.h>
25 #include <ui/DisplayMode.h>
26 #include <ui/DisplayState.h>
27 #include <ui/GraphicBufferAllocator.h>
28 #include <ui/GraphicBufferMapper.h>
29 #include <utils/SystemClock.h>
30 
31 #include <chrono>
32 
33 namespace {
34 
35 using ::aidl::android::frameworks::automotive::display::ICarDisplayProxy;
36 using ::aidl::android::hardware::graphics::common::BufferUsage;
37 using ::aidl::android::hardware::graphics::common::PixelFormat;
38 using ::android::base::ScopedLockAssertion;
39 using ::ndk::ScopedAStatus;
40 
41 constexpr auto kTimeout = std::chrono::seconds(1);
42 
43 bool debugFirstFrameDisplayed = false;
44 
generateFingerPrint(buffer_handle_t handle)45 int generateFingerPrint(buffer_handle_t handle) {
46     return static_cast<int>(reinterpret_cast<long>(handle) & 0xFFFFFFFF);
47 }
48 
49 }  // namespace
50 
51 namespace aidl::android::hardware::automotive::evs::implementation {
52 
EvsGlDisplay(const std::shared_ptr<ICarDisplayProxy> & pDisplayProxy,uint64_t displayId)53 EvsGlDisplay::EvsGlDisplay(const std::shared_ptr<ICarDisplayProxy>& pDisplayProxy,
54                            uint64_t displayId)
55     : mDisplayId(displayId), mDisplayProxy(pDisplayProxy) {
56     LOG(DEBUG) << "EvsGlDisplay instantiated";
57 
58     // Set up our self description
59     // NOTE:  These are arbitrary values chosen for testing
60     mInfo.id = std::to_string(displayId);
61     mInfo.vendorFlags = 3870;
62 
63     // Start a thread to render images on this display
64     {
65         std::lock_guard lock(mLock);
66         mState = RUN;
67     }
68     mRenderThread = std::thread([this]() { renderFrames(); });
69 }
70 
~EvsGlDisplay()71 EvsGlDisplay::~EvsGlDisplay() {
72     LOG(DEBUG) << "EvsGlDisplay being destroyed";
73     forceShutdown();
74 }
75 
76 /**
77  * This gets called if another caller "steals" ownership of the display
78  */
forceShutdown()79 void EvsGlDisplay::forceShutdown() {
80     LOG(DEBUG) << "EvsGlDisplay forceShutdown";
81     {
82         std::lock_guard lock(mLock);
83 
84         // If the buffer isn't being held by a remote client, release it now as an
85         // optimization to release the resources more quickly than the destructor might
86         // get called.
87         if (mBuffer.handle != nullptr) {
88             // Report if we're going away while a buffer is outstanding
89             if (mBufferBusy || mState == RUN) {
90                 LOG(ERROR) << "EvsGlDisplay going down while client is holding a buffer";
91             }
92             mState = STOPPING;
93         }
94 
95         // Put this object into an unrecoverable error state since somebody else
96         // is going to own the display now.
97         mRequestedState = DisplayState::DEAD;
98     }
99     mBufferReadyToRender.notify_all();
100 
101     if (mRenderThread.joinable()) {
102         mRenderThread.join();
103     }
104 }
105 
106 /**
107  * Initialize GL in the context of a caller's thread and prepare a graphic
108  * buffer to use.
109  */
initializeGlContextLocked()110 bool EvsGlDisplay::initializeGlContextLocked() {
111     // Initialize our display window
112     // NOTE:  This will cause the display to become "VISIBLE" before a frame is actually
113     // returned, which is contrary to the spec and will likely result in a black frame being
114     // (briefly) shown.
115     if (!mGlWrapper.initialize(mDisplayProxy, mDisplayId)) {
116         // Report the failure
117         LOG(ERROR) << "Failed to initialize GL display";
118         return false;
119     }
120 
121     // Assemble the buffer description we'll use for our render target
122     static_assert(::aidl::android::hardware::graphics::common::PixelFormat::RGBA_8888 ==
123                   static_cast<::aidl::android::hardware::graphics::common::PixelFormat>(
124                           HAL_PIXEL_FORMAT_RGBA_8888));
125     mBuffer.description = {
126             .width = static_cast<int>(mGlWrapper.getWidth()),
127             .height = static_cast<int>(mGlWrapper.getHeight()),
128             .layers = 1,
129             .format = PixelFormat::RGBA_8888,
130             // FIXME: Below line is not using
131             // ::aidl::android::hardware::graphics::common::BufferUsage because
132             // BufferUsage enum does not support a bitwise-OR operation; they
133             // should be BufferUsage::GPU_RENDER_TARGET |
134             // BufferUsage::COMPOSER_OVERLAY
135             .usage = static_cast<BufferUsage>(GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER),
136     };
137 
138     ::android::GraphicBufferAllocator& alloc(::android::GraphicBufferAllocator::get());
139     uint32_t stride = static_cast<uint32_t>(mBuffer.description.stride);
140     buffer_handle_t handle = nullptr;
141     const ::android::status_t result =
142             alloc.allocate(mBuffer.description.width, mBuffer.description.height,
143                            static_cast<::android::PixelFormat>(mBuffer.description.format),
144                            mBuffer.description.layers,
145                            static_cast<uint64_t>(mBuffer.description.usage), &handle, &stride,
146                            /* requestorName= */ "EvsGlDisplay");
147     mBuffer.description.stride = stride;
148     mBuffer.fingerprint = generateFingerPrint(mBuffer.handle);
149     if (result != ::android::NO_ERROR) {
150         LOG(ERROR) << "Error " << result << " allocating " << mBuffer.description.width << " x "
151                    << mBuffer.description.height << " graphics buffer.";
152         mGlWrapper.shutdown();
153         return false;
154     }
155 
156     mBuffer.handle = handle;
157     if (mBuffer.handle == nullptr) {
158         LOG(ERROR) << "We didn't get a buffer handle back from the allocator";
159         mGlWrapper.shutdown();
160         return false;
161     }
162 
163     LOG(DEBUG) << "Allocated new buffer " << mBuffer.handle << " with stride "
164                << mBuffer.description.stride;
165     return true;
166 }
167 
168 /**
169  * This method runs in a separate thread and renders the contents of the buffer.
170  */
renderFrames()171 void EvsGlDisplay::renderFrames() {
172     {
173         std::lock_guard lock(mLock);
174 
175         if (!initializeGlContextLocked()) {
176             LOG(ERROR) << "Failed to initialize GL context";
177             return;
178         }
179 
180         // Display buffer is ready.
181         mBufferBusy = false;
182     }
183     mBufferReadyToUse.notify_all();
184 
185     while (true) {
186         {
187             std::unique_lock lock(mLock);
188             ScopedLockAssertion lock_assertion(mLock);
189             mBufferReadyToRender.wait(
190                     lock, [this]() REQUIRES(mLock) { return mBufferReady || mState != RUN; });
191             if (mState != RUN) {
192                 LOG(DEBUG) << "A rendering thread is stopping";
193                 break;
194             }
195             mBufferReady = false;
196         }
197 
198         // Update the texture contents with the provided data
199         if (!mGlWrapper.updateImageTexture(mBuffer.handle, mBuffer.description)) {
200             LOG(WARNING) << "Failed to update the image texture";
201             continue;
202         }
203 
204         // Put the image on the screen
205         mGlWrapper.renderImageToScreen();
206         if (!debugFirstFrameDisplayed) {
207             LOG(DEBUG) << "EvsFirstFrameDisplayTiming start time: " << ::android::elapsedRealtime()
208                        << " ms.";
209             debugFirstFrameDisplayed = true;
210         }
211 
212         // Mark current frame is consumed.
213         {
214             std::lock_guard lock(mLock);
215             mBufferBusy = false;
216         }
217         mBufferDone.notify_all();
218     }
219 
220     LOG(DEBUG) << "A rendering thread is stopped.";
221 
222     // Drop the graphics buffer we've been using
223     ::android::GraphicBufferAllocator& alloc(::android::GraphicBufferAllocator::get());
224     alloc.free(mBuffer.handle);
225     mBuffer.handle = nullptr;
226 
227     mGlWrapper.hideWindow(mDisplayProxy, mDisplayId);
228     mGlWrapper.shutdown();
229 
230     std::lock_guard lock(mLock);
231     mState = STOPPED;
232 }
233 
234 /**
235  * Returns basic information about the EVS display provided by the system.
236  * See the description of the DisplayDesc structure for details.
237  */
getDisplayInfo(DisplayDesc * _aidl_return)238 ScopedAStatus EvsGlDisplay::getDisplayInfo(DisplayDesc* _aidl_return) {
239     if (!mDisplayProxy) {
240         return ::ndk::ScopedAStatus::fromServiceSpecificError(
241                 static_cast<int>(EvsResult::UNDERLYING_SERVICE_ERROR));
242     }
243 
244     ::aidl::android::frameworks::automotive::display::DisplayDesc proxyDisplay;
245     auto status = mDisplayProxy->getDisplayInfo(mDisplayId, &proxyDisplay);
246     if (!status.isOk()) {
247         return ::ndk::ScopedAStatus::fromServiceSpecificError(
248                 static_cast<int>(EvsResult::UNDERLYING_SERVICE_ERROR));
249     }
250 
251     _aidl_return->width = proxyDisplay.width;
252     _aidl_return->height = proxyDisplay.height;
253     _aidl_return->orientation = static_cast<Rotation>(proxyDisplay.orientation);
254     _aidl_return->id = mInfo.id;  // FIXME: what should be ID here?
255     _aidl_return->vendorFlags = mInfo.vendorFlags;
256     return ::ndk::ScopedAStatus::ok();
257 }
258 
259 /**
260  * Clients may set the display state to express their desired state.
261  * The HAL implementation must gracefully accept a request for any state
262  * while in any other state, although the response may be to ignore the request.
263  * The display is defined to start in the NOT_VISIBLE state upon initialization.
264  * The client is then expected to request the VISIBLE_ON_NEXT_FRAME state, and
265  * then begin providing video.  When the display is no longer required, the client
266  * is expected to request the NOT_VISIBLE state after passing the last video frame.
267  */
setDisplayState(DisplayState state)268 ScopedAStatus EvsGlDisplay::setDisplayState(DisplayState state) {
269     LOG(DEBUG) << __FUNCTION__;
270     std::lock_guard lock(mLock);
271 
272     if (mRequestedState == DisplayState::DEAD) {
273         // This object no longer owns the display -- it's been superceeded!
274         return ScopedAStatus::fromServiceSpecificError(static_cast<int>(EvsResult::OWNERSHIP_LOST));
275     }
276 
277     // Ensure we recognize the requested state so we don't go off the rails
278     static constexpr ::ndk::enum_range<DisplayState> kDisplayStateRange;
279     if (std::find(kDisplayStateRange.begin(), kDisplayStateRange.end(), state) ==
280         kDisplayStateRange.end()) {
281         return ScopedAStatus::fromServiceSpecificError(static_cast<int>(EvsResult::INVALID_ARG));
282     }
283 
284     switch (state) {
285         case DisplayState::NOT_VISIBLE:
286             mGlWrapper.hideWindow(mDisplayProxy, mDisplayId);
287             break;
288         case DisplayState::VISIBLE:
289             mGlWrapper.showWindow(mDisplayProxy, mDisplayId);
290             break;
291         default:
292             break;
293     }
294 
295     // Record the requested state
296     mRequestedState = state;
297 
298     return ScopedAStatus::ok();
299 }
300 
301 /**
302  * The HAL implementation should report the actual current state, which might
303  * transiently differ from the most recently requested state.  Note, however, that
304  * the logic responsible for changing display states should generally live above
305  * the device layer, making it undesirable for the HAL implementation to
306  * spontaneously change display states.
307  */
getDisplayState(DisplayState * _aidl_return)308 ScopedAStatus EvsGlDisplay::getDisplayState(DisplayState* _aidl_return) {
309     LOG(DEBUG) << __FUNCTION__;
310     std::lock_guard lock(mLock);
311     *_aidl_return = mRequestedState;
312     return ScopedAStatus::ok();
313 }
314 
315 /**
316  * This call returns a handle to a frame buffer associated with the display.
317  * This buffer may be locked and written to by software and/or GL.  This buffer
318  * must be returned via a call to returnTargetBufferForDisplay() even if the
319  * display is no longer visible.
320  */
getTargetBuffer(BufferDesc * _aidl_return)321 ScopedAStatus EvsGlDisplay::getTargetBuffer(BufferDesc* _aidl_return) {
322     LOG(DEBUG) << __FUNCTION__;
323     std::unique_lock lock(mLock);
324     ScopedLockAssertion lock_assertion(mLock);
325     if (mRequestedState == DisplayState::DEAD) {
326         LOG(ERROR) << "Rejecting buffer request from object that lost ownership of the display.";
327         return ScopedAStatus::fromServiceSpecificError(static_cast<int>(EvsResult::OWNERSHIP_LOST));
328     }
329 
330     // If we don't already have a buffer, allocate one now
331     // mBuffer.memHandle is a type of buffer_handle_t, which is equal to
332     // native_handle_t*.
333     mBufferReadyToUse.wait(lock, [this]() REQUIRES(mLock) { return !mBufferBusy; });
334 
335     // Do we have a frame available?
336     if (mBufferBusy) {
337         // This means either we have a 2nd client trying to compete for buffers
338         // (an unsupported mode of operation) or else the client hasn't returned
339         // a previously issued buffer yet (they're behaving badly).
340         // NOTE:  We have to make the callback even if we have nothing to provide
341         LOG(ERROR) << "getTargetBuffer called while no buffers available.";
342         return ScopedAStatus::fromServiceSpecificError(
343                 static_cast<int>(EvsResult::BUFFER_NOT_AVAILABLE));
344     }
345 
346     // Mark our buffer as busy
347     mBufferBusy = true;
348 
349     // Send the buffer to the client
350     LOG(VERBOSE) << "Providing display buffer handle " << mBuffer.handle;
351 
352     BufferDesc bufferDescToSend = {
353             .buffer =
354                     {
355                             .description = mBuffer.description,
356                             .handle = std::move(::android::dupToAidl(mBuffer.handle)),
357                     },
358             .pixelSizeBytes = 4,  // RGBA_8888 is 4-byte-per-pixel format
359             .bufferId = mBuffer.fingerprint,
360     };
361     *_aidl_return = std::move(bufferDescToSend);
362 
363     return ScopedAStatus::ok();
364 }
365 
366 /**
367  * This call tells the display that the buffer is ready for display.
368  * The buffer is no longer valid for use by the client after this call.
369  */
returnTargetBufferForDisplay(const BufferDesc & buffer)370 ScopedAStatus EvsGlDisplay::returnTargetBufferForDisplay(const BufferDesc& buffer) {
371     LOG(VERBOSE) << __FUNCTION__;
372     std::unique_lock lock(mLock);
373     ScopedLockAssertion lock_assertion(mLock);
374 
375     // Nobody should call us with a null handle
376     if (buffer.buffer.handle.fds.size() < 1) {
377         LOG(ERROR) << __FUNCTION__ << " called without a valid buffer handle.";
378         return ScopedAStatus::fromServiceSpecificError(static_cast<int>(EvsResult::INVALID_ARG));
379     }
380     if (buffer.bufferId != mBuffer.fingerprint) {
381         LOG(ERROR) << "Got an unrecognized frame returned.";
382         return ScopedAStatus::fromServiceSpecificError(static_cast<int>(EvsResult::INVALID_ARG));
383     }
384     if (!mBufferBusy) {
385         LOG(ERROR) << "A frame was returned with no outstanding frames.";
386         return ScopedAStatus::fromServiceSpecificError(static_cast<int>(EvsResult::INVALID_ARG));
387     }
388 
389     // If we've been displaced by another owner of the display, then we can't do anything else
390     if (mRequestedState == DisplayState::DEAD) {
391         return ScopedAStatus::fromServiceSpecificError(static_cast<int>(EvsResult::OWNERSHIP_LOST));
392     }
393 
394     // If we were waiting for a new frame, this is it!
395     if (mRequestedState == DisplayState::VISIBLE_ON_NEXT_FRAME) {
396         mRequestedState = DisplayState::VISIBLE;
397         mGlWrapper.showWindow(mDisplayProxy, mDisplayId);
398     }
399 
400     // Validate we're in an expected state
401     if (mRequestedState != DisplayState::VISIBLE) {
402         // Not sure why a client would send frames back when we're not visible.
403         LOG(WARNING) << "Got a frame returned while not visible - ignoring.";
404         return ScopedAStatus::ok();
405     }
406     mBufferReady = true;
407     mBufferReadyToRender.notify_all();
408 
409     if (!mBufferDone.wait_for(lock, kTimeout, [this]() REQUIRES(mLock) { return !mBufferBusy; })) {
410         return ScopedAStatus::fromServiceSpecificError(
411                 static_cast<int>(EvsResult::UNDERLYING_SERVICE_ERROR));
412     }
413 
414     return ScopedAStatus::ok();
415 }
416 
417 }  // namespace aidl::android::hardware::automotive::evs::implementation
418