• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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 ANDROID_SF_HWC2_H
18 #define ANDROID_SF_HWC2_H
19 
20 #define HWC2_INCLUDE_STRINGIFICATION
21 #define HWC2_USE_CPP11
22 #include <hardware/hwcomposer2.h>
23 #undef HWC2_INCLUDE_STRINGIFICATION
24 #undef HWC2_USE_CPP11
25 
26 #include <ui/HdrCapabilities.h>
27 #include <math/mat4.h>
28 
29 #include <utils/Log.h>
30 #include <utils/StrongPointer.h>
31 #include <utils/Timers.h>
32 
33 #include <functional>
34 #include <string>
35 #include <unordered_map>
36 #include <unordered_set>
37 #include <vector>
38 #include <map>
39 
40 namespace android {
41     class Fence;
42     class FloatRect;
43     class GraphicBuffer;
44     class Rect;
45     class Region;
46     namespace Hwc2 {
47         class Composer;
48     }
49 }
50 
51 namespace HWC2 {
52 
53 class Display;
54 class Layer;
55 
56 // Implement this interface to receive hardware composer events.
57 //
58 // These callback functions will generally be called on a hwbinder thread, but
59 // when first registering the callback the onHotplugReceived() function will
60 // immediately be called on the thread calling registerCallback().
61 //
62 // All calls receive a sequenceId, which will be the value that was supplied to
63 // HWC2::Device::registerCallback(). It's used to help differentiate callbacks
64 // from different hardware composer instances.
65 class ComposerCallback {
66  public:
67     virtual void onHotplugReceived(int32_t sequenceId, hwc2_display_t display,
68                                    Connection connection,
69                                    bool primaryDisplay) = 0;
70     virtual void onRefreshReceived(int32_t sequenceId,
71                                    hwc2_display_t display) = 0;
72     virtual void onVsyncReceived(int32_t sequenceId, hwc2_display_t display,
73                                  int64_t timestamp) = 0;
74     virtual ~ComposerCallback() = default;
75 };
76 
77 // C++ Wrapper around hwc2_device_t. Load all functions pointers
78 // and handle callback registration.
79 class Device
80 {
81 public:
82     // useVrComposer is passed to the composer HAL. When true, the composer HAL
83     // will use the vr composer service, otherwise it uses the real hardware
84     // composer.
85     Device(bool useVrComposer);
86 
87     void registerCallback(ComposerCallback* callback, int32_t sequenceId);
88 
89     // Required by HWC2
90 
91     std::string dump() const;
92 
getCapabilities()93     const std::unordered_set<Capability>& getCapabilities() const {
94         return mCapabilities;
95     };
96 
97     uint32_t getMaxVirtualDisplayCount() const;
98     Error createVirtualDisplay(uint32_t width, uint32_t height,
99             android_pixel_format_t* format, Display** outDisplay);
100     void destroyDisplay(hwc2_display_t displayId);
101 
102     void onHotplug(hwc2_display_t displayId, Connection connection);
103 
104     // Other Device methods
105 
106     Display* getDisplayById(hwc2_display_t id);
107 
getComposer()108     android::Hwc2::Composer* getComposer() { return mComposer.get(); }
109 
110 private:
111     // Initialization methods
112 
113     void loadCapabilities();
114 
115     // Member variables
116     std::unique_ptr<android::Hwc2::Composer> mComposer;
117     std::unordered_set<Capability> mCapabilities;
118     std::unordered_map<hwc2_display_t, std::unique_ptr<Display>> mDisplays;
119     bool mRegisteredCallback;
120 };
121 
122 // Convenience C++ class to access hwc2_device_t Display functions directly.
123 class Display
124 {
125 public:
126     Display(android::Hwc2::Composer& composer,
127             const std::unordered_set<Capability>& capabilities,
128             hwc2_display_t id, DisplayType type);
129     ~Display();
130 
131     class Config
132     {
133     public:
134         class Builder
135         {
136         public:
137             Builder(Display& display, hwc2_config_t id);
138 
build()139             std::shared_ptr<const Config> build() {
140                 return std::const_pointer_cast<const Config>(
141                         std::move(mConfig));
142             }
143 
setWidth(int32_t width)144             Builder& setWidth(int32_t width) {
145                 mConfig->mWidth = width;
146                 return *this;
147             }
setHeight(int32_t height)148             Builder& setHeight(int32_t height) {
149                 mConfig->mHeight = height;
150                 return *this;
151             }
setVsyncPeriod(int32_t vsyncPeriod)152             Builder& setVsyncPeriod(int32_t vsyncPeriod) {
153                 mConfig->mVsyncPeriod = vsyncPeriod;
154                 return *this;
155             }
setDpiX(int32_t dpiX)156             Builder& setDpiX(int32_t dpiX) {
157                 if (dpiX == -1) {
158                     mConfig->mDpiX = getDefaultDensity();
159                 } else {
160                     mConfig->mDpiX = dpiX / 1000.0f;
161                 }
162                 return *this;
163             }
setDpiY(int32_t dpiY)164             Builder& setDpiY(int32_t dpiY) {
165                 if (dpiY == -1) {
166                     mConfig->mDpiY = getDefaultDensity();
167                 } else {
168                     mConfig->mDpiY = dpiY / 1000.0f;
169                 }
170                 return *this;
171             }
172 
173         private:
174             float getDefaultDensity();
175             std::shared_ptr<Config> mConfig;
176         };
177 
getDisplayId()178         hwc2_display_t getDisplayId() const { return mDisplay.getId(); }
getId()179         hwc2_config_t getId() const { return mId; }
180 
getWidth()181         int32_t getWidth() const { return mWidth; }
getHeight()182         int32_t getHeight() const { return mHeight; }
getVsyncPeriod()183         nsecs_t getVsyncPeriod() const { return mVsyncPeriod; }
getDpiX()184         float getDpiX() const { return mDpiX; }
getDpiY()185         float getDpiY() const { return mDpiY; }
186 
187     private:
188         Config(Display& display, hwc2_config_t id);
189 
190         Display& mDisplay;
191         hwc2_config_t mId;
192 
193         int32_t mWidth;
194         int32_t mHeight;
195         nsecs_t mVsyncPeriod;
196         float mDpiX;
197         float mDpiY;
198     };
199 
200     // Required by HWC2
201 
202     [[clang::warn_unused_result]] Error acceptChanges();
203     [[clang::warn_unused_result]] Error createLayer(Layer** outLayer);
204     [[clang::warn_unused_result]] Error destroyLayer(Layer* layer);
205     [[clang::warn_unused_result]] Error getActiveConfig(
206             std::shared_ptr<const Config>* outConfig) const;
207     [[clang::warn_unused_result]] Error getChangedCompositionTypes(
208             std::unordered_map<Layer*, Composition>* outTypes);
209     [[clang::warn_unused_result]] Error getColorModes(
210             std::vector<android_color_mode_t>* outModes) const;
211 
212     // Doesn't call into the HWC2 device, so no errors are possible
213     std::vector<std::shared_ptr<const Config>> getConfigs() const;
214 
215     [[clang::warn_unused_result]] Error getName(std::string* outName) const;
216     [[clang::warn_unused_result]] Error getRequests(
217             DisplayRequest* outDisplayRequests,
218             std::unordered_map<Layer*, LayerRequest>* outLayerRequests);
219     [[clang::warn_unused_result]] Error getType(DisplayType* outType) const;
220     [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const;
221     [[clang::warn_unused_result]] Error getHdrCapabilities(
222             std::unique_ptr<android::HdrCapabilities>* outCapabilities) const;
223     [[clang::warn_unused_result]] Error getReleaseFences(
224             std::unordered_map<Layer*,
225                     android::sp<android::Fence>>* outFences) const;
226     [[clang::warn_unused_result]] Error present(
227             android::sp<android::Fence>* outPresentFence);
228     [[clang::warn_unused_result]] Error setActiveConfig(
229             const std::shared_ptr<const Config>& config);
230     [[clang::warn_unused_result]] Error setClientTarget(
231             uint32_t slot, const android::sp<android::GraphicBuffer>& target,
232             const android::sp<android::Fence>& acquireFence,
233             android_dataspace_t dataspace);
234     [[clang::warn_unused_result]] Error setColorMode(android_color_mode_t mode);
235     [[clang::warn_unused_result]] Error setColorTransform(
236             const android::mat4& matrix, android_color_transform_t hint);
237     [[clang::warn_unused_result]] Error setOutputBuffer(
238             const android::sp<android::GraphicBuffer>& buffer,
239             const android::sp<android::Fence>& releaseFence);
240     [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode);
241     [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled);
242     [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes,
243             uint32_t* outNumRequests);
244     [[clang::warn_unused_result]] Error presentOrValidate(uint32_t* outNumTypes,
245                                                  uint32_t* outNumRequests,
246                                                           android::sp<android::Fence>* outPresentFence, uint32_t* state);
247 
248     // Most methods in this class write a command to a command buffer.  The
249     // command buffer is implicitly submitted in validate, present, and
250     // presentOrValidate.  This method provides a way to discard the commands,
251     // which can be used to discard stale commands.
252     void discardCommands();
253 
254     // Other Display methods
255 
getId()256     hwc2_display_t getId() const { return mId; }
isConnected()257     bool isConnected() const { return mIsConnected; }
258     void setConnected(bool connected);  // For use by Device only
259 
260 private:
261     int32_t getAttribute(hwc2_config_t configId, Attribute attribute);
262     void loadConfig(hwc2_config_t configId);
263     void loadConfigs();
264 
265     // This may fail (and return a null pointer) if no layer with this ID exists
266     // on this display
267     Layer* getLayerById(hwc2_layer_t id) const;
268 
269     // Member variables
270 
271     // These are references to data owned by HWC2::Device, which will outlive
272     // this HWC2::Display, so these references are guaranteed to be valid for
273     // the lifetime of this object.
274     android::Hwc2::Composer& mComposer;
275     const std::unordered_set<Capability>& mCapabilities;
276 
277     hwc2_display_t mId;
278     bool mIsConnected;
279     DisplayType mType;
280     std::unordered_map<hwc2_layer_t, std::unique_ptr<Layer>> mLayers;
281     // The ordering in this map matters, for getConfigs(), when it is
282     // converted to a vector
283     std::map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs;
284 };
285 
286 // Convenience C++ class to access hwc2_device_t Layer functions directly.
287 class Layer
288 {
289 public:
290     Layer(android::Hwc2::Composer& composer,
291           const std::unordered_set<Capability>& capabilities,
292           hwc2_display_t displayId, hwc2_layer_t layerId);
293     ~Layer();
294 
getId()295     hwc2_layer_t getId() const { return mId; }
296 
297     // Register a listener to be notified when the layer is destroyed. When the
298     // listener function is called, the Layer will be in the process of being
299     // destroyed, so it's not safe to call methods on it.
300     void setLayerDestroyedListener(std::function<void(Layer*)> listener);
301 
302     [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y);
303     [[clang::warn_unused_result]] Error setBuffer(uint32_t slot,
304             const android::sp<android::GraphicBuffer>& buffer,
305             const android::sp<android::Fence>& acquireFence);
306     [[clang::warn_unused_result]] Error setSurfaceDamage(
307             const android::Region& damage);
308 
309     [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode);
310     [[clang::warn_unused_result]] Error setColor(hwc_color_t color);
311     [[clang::warn_unused_result]] Error setCompositionType(Composition type);
312     [[clang::warn_unused_result]] Error setDataspace(
313             android_dataspace_t dataspace);
314     [[clang::warn_unused_result]] Error setDisplayFrame(
315             const android::Rect& frame);
316     [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha);
317     [[clang::warn_unused_result]] Error setSidebandStream(
318             const native_handle_t* stream);
319     [[clang::warn_unused_result]] Error setSourceCrop(
320             const android::FloatRect& crop);
321     [[clang::warn_unused_result]] Error setTransform(Transform transform);
322     [[clang::warn_unused_result]] Error setVisibleRegion(
323             const android::Region& region);
324     [[clang::warn_unused_result]] Error setZOrder(uint32_t z);
325     [[clang::warn_unused_result]] Error setInfo(uint32_t type, uint32_t appId);
326 
327 private:
328     // These are references to data owned by HWC2::Device, which will outlive
329     // this HWC2::Layer, so these references are guaranteed to be valid for
330     // the lifetime of this object.
331     android::Hwc2::Composer& mComposer;
332     const std::unordered_set<Capability>& mCapabilities;
333 
334     hwc2_display_t mDisplayId;
335     hwc2_layer_t mId;
336     android_dataspace mDataSpace = HAL_DATASPACE_UNKNOWN;
337     std::function<void(Layer*)> mLayerDestroyedListener;
338 };
339 
340 } // namespace HWC2
341 
342 #endif // ANDROID_SF_HWC2_H
343