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