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 CPP_EVS_SAMPLEDRIVER_AIDL_INCLUDE_EVSGLDISPLAY_H
18 #define CPP_EVS_SAMPLEDRIVER_AIDL_INCLUDE_EVSGLDISPLAY_H
19 
20 #include "GlWrapper.h"
21 
22 #include <aidl/android/frameworks/automotive/display/ICarDisplayProxy.h>
23 #include <aidl/android/hardware/automotive/evs/BnEvsDisplay.h>
24 #include <aidl/android/hardware/automotive/evs/BufferDesc.h>
25 #include <aidl/android/hardware/automotive/evs/DisplayDesc.h>
26 #include <aidl/android/hardware/automotive/evs/DisplayState.h>
27 
28 #include <condition_variable>
29 #include <thread>
30 
31 namespace aidl::android::hardware::automotive::evs::implementation {
32 
33 namespace automotivedisplay = ::aidl::android::frameworks::automotive::display;
34 namespace aidlevs = ::aidl::android::hardware::automotive::evs;
35 
36 class EvsGlDisplay final : public ::aidl::android::hardware::automotive::evs::BnEvsDisplay {
37 public:
38     // Methods from ::aidl::android::hardware::automotive::evs::IEvsDisplay follow.
39     ::ndk::ScopedAStatus getDisplayInfo(aidlevs::DisplayDesc* _aidl_return) override;
40     ::ndk::ScopedAStatus getDisplayState(aidlevs::DisplayState* _aidl_return) override;
41     ::ndk::ScopedAStatus getTargetBuffer(aidlevs::BufferDesc* _aidl_return) override;
42     ::ndk::ScopedAStatus returnTargetBufferForDisplay(const evs::BufferDesc& buffer) override;
43     ::ndk::ScopedAStatus setDisplayState(aidlevs::DisplayState state) override;
44 
45     // Implementation details
46     EvsGlDisplay(const std::shared_ptr<automotivedisplay::ICarDisplayProxy>& service,
47                  uint64_t displayId);
48     virtual ~EvsGlDisplay() override;
49 
50     // This gets called if another caller "steals" ownership of the display
51     void forceShutdown();
52 
53 private:
54     // A graphics buffer into which we'll store images.  This member variable
55     // will be protected by semaphores.
56     struct BufferRecord {
57         ::aidl::android::hardware::graphics::common::HardwareBufferDescription description;
58         buffer_handle_t handle;
59         int fingerprint;
60     } mBuffer;
61 
62     // State of a rendering thread
63     enum RenderThreadStates {
64         STOPPED = 0,
65         STOPPING = 1,
66         RUN = 2,
67     };
68 
69     uint64_t mDisplayId;
70     aidlevs::DisplayDesc mInfo;
71     aidlevs::DisplayState mRequestedState GUARDED_BY(mLock) = aidlevs::DisplayState::NOT_VISIBLE;
72     std::shared_ptr<automotivedisplay::ICarDisplayProxy> mDisplayProxy;
73 
74     GlWrapper mGlWrapper;
75     mutable std::mutex mLock;
76 
77     // This tells us whether or not our buffer is in use.  Protected by
78     // semaphores.
79     bool mBufferBusy = false;
80 
81     // Variables to synchronize a rendering thread w/ main and binder threads
82     std::thread mRenderThread;
83     RenderThreadStates mState GUARDED_BY(mLock) = STOPPED;
84     bool mBufferReady = false;
85     void renderFrames();
86     bool initializeGlContextLocked() REQUIRES(mLock);
87 
88     std::condition_variable mBufferReadyToUse;
89     std::condition_variable mBufferReadyToRender;
90     std::condition_variable mBufferDone;
91 };
92 
93 }  // namespace aidl::android::hardware::automotive::evs::implementation
94 
95 #endif  // CPP_EVS_SAMPLEDRIVER_AIDL_INCLUDE_EVSGLDISPLAY_H
96