1 /*
2  * Copyright 2019 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 #ifndef LOG_TAG
20 #warning "ComposerClient.h included without LOG_TAG"
21 #endif
22 
23 #include <android/hardware/graphics/composer/2.4/IComposerCallback.h>
24 #include <android/hardware/graphics/composer/2.4/IComposerClient.h>
25 #include <composer-hal/2.4/ComposerCommandEngine.h>
26 #include <composer-hal/2.4/ComposerHal.h>
27 #include <composer-resources/2.1/ComposerResources.h>
28 
29 namespace android {
30 namespace hardware {
31 namespace graphics {
32 namespace composer {
33 namespace V2_4 {
34 namespace hal {
35 
36 namespace detail {
37 
38 // ComposerClientImpl implements V2_*::IComposerClient on top of V2_*::ComposerHal
39 template <typename Interface, typename Hal>
40 class ComposerClientImpl : public V2_3::hal::detail::ComposerClientImpl<Interface, Hal> {
41   public:
ComposerClientImpl(Hal * hal)42     ComposerClientImpl(Hal* hal) : BaseType2_3(hal) {}
43 
~ComposerClientImpl()44     ~ComposerClientImpl() override { mHal->unregisterEventCallback_2_4(); }
45 
46     class HalEventCallback : public Hal::EventCallback_2_4 {
47       public:
HalEventCallback(Hal * hal,const sp<IComposerCallback> callback,V2_1::hal::ComposerResources * resources)48         HalEventCallback(Hal* hal, const sp<IComposerCallback> callback,
49                          V2_1::hal::ComposerResources* resources)
50             : mHal(hal), mCallback(callback), mResources(resources) {}
51 
onHotplug(Display display,IComposerCallback::Connection connected)52         void onHotplug(Display display, IComposerCallback::Connection connected) override {
53             if (connected == IComposerCallback::Connection::CONNECTED) {
54                 if (mResources->hasDisplay(display)) {
55                     // This is a subsequent hotplug "connected" for a display. This signals a
56                     // display change and thus the framework may want to reallocate buffers. We
57                     // need to free all cached handles, since they are holding a strong reference
58                     // to the underlying buffers.
59                     V2_1::hal::detail::ComposerClientImpl<Interface, Hal>::cleanDisplayResources(
60                             display, mResources, mHal);
61                     mResources->removeDisplay(display);
62                 }
63                 mResources->addPhysicalDisplay(display);
64             } else if (connected == IComposerCallback::Connection::DISCONNECTED) {
65                 mResources->removeDisplay(display);
66             }
67 
68             auto ret = mCallback->onHotplug(display, connected);
69             ALOGE_IF(!ret.isOk(), "failed to send onHotplug: %s", ret.description().c_str());
70         }
71 
onRefresh(Display display)72         void onRefresh(Display display) override {
73             mResources->setDisplayMustValidateState(display, true);
74             auto ret = mCallback->onRefresh(display);
75             ALOGE_IF(!ret.isOk(), "failed to send onRefresh: %s", ret.description().c_str());
76         }
77 
onVsync(Display display,int64_t timestamp)78         void onVsync(Display display, int64_t timestamp) override {
79             auto ret = mCallback->onVsync(display, timestamp);
80             ALOGE_IF(!ret.isOk(), "failed to send onVsync: %s", ret.description().c_str());
81         }
82 
onVsync_2_4(Display display,int64_t timestamp,VsyncPeriodNanos vsyncPeriodNanos)83         void onVsync_2_4(Display display, int64_t timestamp,
84                          VsyncPeriodNanos vsyncPeriodNanos) override {
85             auto ret = mCallback->onVsync_2_4(display, timestamp, vsyncPeriodNanos);
86             ALOGE_IF(!ret.isOk(), "failed to send onVsync_2_4: %s", ret.description().c_str());
87         }
88 
onVsyncPeriodTimingChanged(Display display,const VsyncPeriodChangeTimeline & updatedTimeline)89         void onVsyncPeriodTimingChanged(Display display,
90                                         const VsyncPeriodChangeTimeline& updatedTimeline) override {
91             auto ret = mCallback->onVsyncPeriodTimingChanged(display, updatedTimeline);
92             ALOGE_IF(!ret.isOk(), "failed to send onVsyncPeriodTimingChanged: %s",
93                      ret.description().c_str());
94         }
95 
onSeamlessPossible(Display display)96         void onSeamlessPossible(Display display) override {
97             auto ret = mCallback->onSeamlessPossible(display);
98             ALOGE_IF(!ret.isOk(), "failed to send onSealmessPossible: %s",
99                      ret.description().c_str());
100         }
101 
102       protected:
103         Hal* const mHal;
104         const sp<IComposerCallback> mCallback;
105         V2_1::hal::ComposerResources* const mResources;
106     };
107 
registerCallback_2_4(const sp<IComposerCallback> & callback)108     Return<void> registerCallback_2_4(const sp<IComposerCallback>& callback) override {
109         // no locking as we require this function to be called only once
110         mHalEventCallback_2_4 =
111                 std::make_unique<HalEventCallback>(mHal, callback, mResources.get());
112         mHal->registerEventCallback_2_4(mHalEventCallback_2_4.get());
113         return Void();
114     }
115 
getDisplayCapabilities_2_4(Display display,IComposerClient::getDisplayCapabilities_2_4_cb hidl_cb)116     Return<void> getDisplayCapabilities_2_4(
117             Display display, IComposerClient::getDisplayCapabilities_2_4_cb hidl_cb) override {
118         std::vector<IComposerClient::DisplayCapability> capabilities;
119         Error error = mHal->getDisplayCapabilities_2_4(display, &capabilities);
120         hidl_cb(error, capabilities);
121         return Void();
122     }
123 
getDisplayConnectionType(Display display,IComposerClient::getDisplayConnectionType_cb hidl_cb)124     Return<void> getDisplayConnectionType(
125             Display display, IComposerClient::getDisplayConnectionType_cb hidl_cb) override {
126         IComposerClient::DisplayConnectionType type;
127         Error error = mHal->getDisplayConnectionType(display, &type);
128         hidl_cb(error, type);
129         return Void();
130     }
131 
getDisplayAttribute_2_4(Display display,Config config,IComposerClient::Attribute attribute,IComposerClient::getDisplayAttribute_2_4_cb hidl_cb)132     Return<void> getDisplayAttribute_2_4(
133             Display display, Config config, IComposerClient::Attribute attribute,
134             IComposerClient::getDisplayAttribute_2_4_cb hidl_cb) override {
135         int32_t value = 0;
136         Error error = mHal->getDisplayAttribute_2_4(display, config, attribute, &value);
137         hidl_cb(error, value);
138         return Void();
139     }
140 
getDisplayVsyncPeriod(Display display,IComposerClient::getDisplayVsyncPeriod_cb hidl_cb)141     Return<void> getDisplayVsyncPeriod(Display display,
142                                        IComposerClient::getDisplayVsyncPeriod_cb hidl_cb) override {
143         VsyncPeriodNanos vsyncPeriods;
144         Error error = mHal->getDisplayVsyncPeriod(display, &vsyncPeriods);
145         hidl_cb(error, vsyncPeriods);
146         return Void();
147     }
148 
setActiveConfigWithConstraints(Display display,Config config,const IComposerClient::VsyncPeriodChangeConstraints & vsyncPeriodChangeConstraints,IComposerClient::setActiveConfigWithConstraints_cb hidl_cb)149     Return<void> setActiveConfigWithConstraints(
150             Display display, Config config,
151             const IComposerClient::VsyncPeriodChangeConstraints& vsyncPeriodChangeConstraints,
152             IComposerClient::setActiveConfigWithConstraints_cb hidl_cb) override {
153         VsyncPeriodChangeTimeline timeline = {};
154         Error error = mHal->setActiveConfigWithConstraints(display, config,
155                                                            vsyncPeriodChangeConstraints, &timeline);
156         hidl_cb(error, timeline);
157         return Void();
158     }
159 
setAutoLowLatencyMode(Display display,bool on)160     Return<Error> setAutoLowLatencyMode(Display display, bool on) override {
161         return mHal->setAutoLowLatencyMode(display, on);
162     }
163 
getSupportedContentTypes(Display display,IComposerClient::getSupportedContentTypes_cb hidl_cb)164     Return<void> getSupportedContentTypes(
165             Display display, IComposerClient::getSupportedContentTypes_cb hidl_cb) override {
166         std::vector<IComposerClient::ContentType> supportedContentTypes;
167         Error error = mHal->getSupportedContentTypes(display, &supportedContentTypes);
168 
169         hidl_cb(error, supportedContentTypes);
170         return Void();
171     }
172 
setContentType(Display display,IComposerClient::ContentType contentType)173     Return<Error> setContentType(Display display,
174                                  IComposerClient::ContentType contentType) override {
175         return mHal->setContentType(display, contentType);
176     }
177 
getLayerGenericMetadataKeys(IComposerClient::getLayerGenericMetadataKeys_cb hidl_cb)178     Return<void> getLayerGenericMetadataKeys(
179             IComposerClient::getLayerGenericMetadataKeys_cb hidl_cb) override {
180         std::vector<IComposerClient::LayerGenericMetadataKey> keys;
181         Error error = mHal->getLayerGenericMetadataKeys(&keys);
182         hidl_cb(error, keys);
183         return Void();
184     }
185 
create(Hal * hal)186     static std::unique_ptr<ComposerClientImpl> create(Hal* hal) {
187         auto client = std::make_unique<ComposerClientImpl>(hal);
188         return client->init() ? std::move(client) : nullptr;
189     }
190 
191   protected:
createCommandEngine()192     std::unique_ptr<V2_1::hal::ComposerCommandEngine> createCommandEngine() override {
193         return std::make_unique<ComposerCommandEngine>(
194                 mHal, static_cast<V2_2::hal::ComposerResources*>(mResources.get()));
195     }
196 
197   private:
198     using BaseType2_3 = V2_3::hal::detail::ComposerClientImpl<Interface, Hal>;
199     using BaseType2_1 = V2_1::hal::detail::ComposerClientImpl<Interface, Hal>;
200     using BaseType2_1::mHal;
201     using BaseType2_1::mResources;
202     std::unique_ptr<HalEventCallback> mHalEventCallback_2_4;
203 };
204 
205 }  // namespace detail
206 
207 using ComposerClient = detail::ComposerClientImpl<IComposerClient, ComposerHal>;
208 
209 }  // namespace hal
210 }  // namespace V2_4
211 }  // namespace composer
212 }  // namespace graphics
213 }  // namespace hardware
214 }  // namespace android
215