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_ON_1_ADAPTER_H
18 #define ANDROID_SF_HWC2_ON_1_ADAPTER_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/Fence.h>
27 
28 #include <atomic>
29 #include <map>
30 #include <mutex>
31 #include <queue>
32 #include <set>
33 #include <unordered_map>
34 #include <unordered_set>
35 #include <vector>
36 
37 struct hwc_composer_device_1;
38 struct hwc_display_contents_1;
39 struct hwc_layer_1;
40 
41 namespace android {
42 
43 class HWC2On1Adapter : public hwc2_device_t
44 {
45 public:
46     explicit HWC2On1Adapter(struct hwc_composer_device_1* hwc1Device);
47     ~HWC2On1Adapter();
48 
getHwc1Device()49     struct hwc_composer_device_1* getHwc1Device() const { return mHwc1Device; }
getHwc1MinorVersion()50     uint8_t getHwc1MinorVersion() const { return mHwc1MinorVersion; }
51 
52 private:
getAdapter(hwc2_device_t * device)53     static inline HWC2On1Adapter* getAdapter(hwc2_device_t* device) {
54         return static_cast<HWC2On1Adapter*>(device);
55     }
56 
57     // getCapabilities
58 
59     void doGetCapabilities(uint32_t* outCount,
60             int32_t* /*hwc2_capability_t*/ outCapabilities);
getCapabilitiesHook(hwc2_device_t * device,uint32_t * outCount,int32_t * outCapabilities)61     static void getCapabilitiesHook(hwc2_device_t* device, uint32_t* outCount,
62             int32_t* /*hwc2_capability_t*/ outCapabilities) {
63         getAdapter(device)->doGetCapabilities(outCount, outCapabilities);
64     }
65 
66     // getFunction
67 
68     hwc2_function_pointer_t doGetFunction(HWC2::FunctionDescriptor descriptor);
getFunctionHook(hwc2_device_t * device,int32_t intDesc)69     static hwc2_function_pointer_t getFunctionHook(hwc2_device_t* device,
70             int32_t intDesc) {
71         auto descriptor = static_cast<HWC2::FunctionDescriptor>(intDesc);
72         return getAdapter(device)->doGetFunction(descriptor);
73     }
74 
75     // Device functions
76 
77     HWC2::Error createVirtualDisplay(uint32_t width, uint32_t height,
78             hwc2_display_t* outDisplay);
createVirtualDisplayHook(hwc2_device_t * device,uint32_t width,uint32_t height,int32_t *,hwc2_display_t * outDisplay)79     static int32_t createVirtualDisplayHook(hwc2_device_t* device,
80             uint32_t width, uint32_t height, int32_t* /*format*/,
81             hwc2_display_t* outDisplay) {
82         // HWC1 implementations cannot override the buffer format requested by
83         // the consumer
84         auto error = getAdapter(device)->createVirtualDisplay(width, height,
85                 outDisplay);
86         return static_cast<int32_t>(error);
87     }
88 
89     HWC2::Error destroyVirtualDisplay(hwc2_display_t display);
destroyVirtualDisplayHook(hwc2_device_t * device,hwc2_display_t display)90     static int32_t destroyVirtualDisplayHook(hwc2_device_t* device,
91             hwc2_display_t display) {
92         auto error = getAdapter(device)->destroyVirtualDisplay(display);
93         return static_cast<int32_t>(error);
94     }
95 
96     std::string mDumpString;
97     void dump(uint32_t* outSize, char* outBuffer);
dumpHook(hwc2_device_t * device,uint32_t * outSize,char * outBuffer)98     static void dumpHook(hwc2_device_t* device, uint32_t* outSize,
99             char* outBuffer) {
100         getAdapter(device)->dump(outSize, outBuffer);
101     }
102 
103     uint32_t getMaxVirtualDisplayCount();
getMaxVirtualDisplayCountHook(hwc2_device_t * device)104     static uint32_t getMaxVirtualDisplayCountHook(hwc2_device_t* device) {
105         return getAdapter(device)->getMaxVirtualDisplayCount();
106     }
107 
108     HWC2::Error registerCallback(HWC2::Callback descriptor,
109             hwc2_callback_data_t callbackData, hwc2_function_pointer_t pointer);
registerCallbackHook(hwc2_device_t * device,int32_t intDesc,hwc2_callback_data_t callbackData,hwc2_function_pointer_t pointer)110     static int32_t registerCallbackHook(hwc2_device_t* device,
111             int32_t intDesc, hwc2_callback_data_t callbackData,
112             hwc2_function_pointer_t pointer) {
113         auto descriptor = static_cast<HWC2::Callback>(intDesc);
114         auto error = getAdapter(device)->registerCallback(descriptor,
115                 callbackData, pointer);
116         return static_cast<int32_t>(error);
117     }
118 
119     // Display functions
120 
121     class Layer;
122 
123     class SortLayersByZ {
124         public:
125             bool operator()(const std::shared_ptr<Layer>& lhs,
126                     const std::shared_ptr<Layer>& rhs);
127     };
128 
129     class DisplayContentsDeleter {
130         public:
131             void operator()(struct hwc_display_contents_1* contents);
132     };
133 
134     class DeferredFence {
135         public:
DeferredFence()136             DeferredFence()
137               : mMutex(),
138                 mFences({Fence::NO_FENCE, Fence::NO_FENCE}) {}
139 
add(int32_t fenceFd)140             void add(int32_t fenceFd) {
141                 mFences.emplace(new Fence(fenceFd));
142                 mFences.pop();
143             }
144 
get()145             const sp<Fence>& get() const {
146                 return mFences.front();
147             }
148 
149         private:
150             mutable std::mutex mMutex;
151             std::queue<sp<Fence>> mFences;
152     };
153 
154     class FencedBuffer {
155         public:
FencedBuffer()156             FencedBuffer() : mBuffer(nullptr), mFence(Fence::NO_FENCE) {}
157 
setBuffer(buffer_handle_t buffer)158             void setBuffer(buffer_handle_t buffer) { mBuffer = buffer; }
setFence(int fenceFd)159             void setFence(int fenceFd) { mFence = new Fence(fenceFd); }
160 
getBuffer()161             buffer_handle_t getBuffer() const { return mBuffer; }
getFence()162             int getFence() const { return mFence->dup(); }
163 
164         private:
165             buffer_handle_t mBuffer;
166             sp<Fence> mFence;
167     };
168 
169     class Display {
170         public:
171             typedef std::unique_ptr<hwc_display_contents_1,
172                     DisplayContentsDeleter> HWC1Contents;
173 
174             Display(HWC2On1Adapter& device, HWC2::DisplayType type);
175 
getId()176             hwc2_display_t getId() const { return mId; }
getDevice()177             HWC2On1Adapter& getDevice() const { return mDevice; }
178 
179             // Does not require locking because it is set before adding the
180             // Displays to the Adapter's list of displays
setHwc1Id(int32_t id)181             void setHwc1Id(int32_t id) { mHwc1Id = id; }
getHwc1Id()182             int32_t getHwc1Id() const { return mHwc1Id; }
183 
incDirty()184             void incDirty() { ++mDirtyCount; }
decDirty()185             void decDirty() { --mDirtyCount; }
isDirty()186             bool isDirty() const { return mDirtyCount > 0 || mZIsDirty; }
187 
188             // HWC2 Display functions
189             HWC2::Error acceptChanges();
190             HWC2::Error createLayer(hwc2_layer_t* outLayerId);
191             HWC2::Error destroyLayer(hwc2_layer_t layerId);
192             HWC2::Error getActiveConfig(hwc2_config_t* outConfigId);
193             HWC2::Error getAttribute(hwc2_config_t configId,
194                     HWC2::Attribute attribute, int32_t* outValue);
195             HWC2::Error getChangedCompositionTypes(uint32_t* outNumElements,
196                     hwc2_layer_t* outLayers, int32_t* outTypes);
197             HWC2::Error getColorModes(uint32_t* outNumModes, int32_t* outModes);
198             HWC2::Error getConfigs(uint32_t* outNumConfigs,
199                     hwc2_config_t* outConfigIds);
200             HWC2::Error getDozeSupport(int32_t* outSupport);
201             HWC2::Error getHdrCapabilities(uint32_t* outNumTypes,
202                     int32_t* outTypes, float* outMaxLuminance,
203                     float* outMaxAverageLuminance, float* outMinLuminance);
204             HWC2::Error getName(uint32_t* outSize, char* outName);
205             HWC2::Error getReleaseFences(uint32_t* outNumElements,
206                     hwc2_layer_t* outLayers, int32_t* outFences);
207             HWC2::Error getRequests(int32_t* outDisplayRequests,
208                     uint32_t* outNumElements, hwc2_layer_t* outLayers,
209                     int32_t* outLayerRequests);
210             HWC2::Error getType(int32_t* outType);
211             HWC2::Error present(int32_t* outRetireFence);
212             HWC2::Error setActiveConfig(hwc2_config_t configId);
213             HWC2::Error setClientTarget(buffer_handle_t target,
214                     int32_t acquireFence, int32_t dataspace,
215                     hwc_region_t damage);
216             HWC2::Error setColorMode(android_color_mode_t mode);
217             HWC2::Error setColorTransform(android_color_transform_t hint);
218             HWC2::Error setOutputBuffer(buffer_handle_t buffer,
219                     int32_t releaseFence);
220             HWC2::Error setPowerMode(HWC2::PowerMode mode);
221             HWC2::Error setVsyncEnabled(HWC2::Vsync enabled);
222             HWC2::Error validate(uint32_t* outNumTypes,
223                     uint32_t* outNumRequests);
224 
225             HWC2::Error updateLayerZ(hwc2_layer_t layerId, uint32_t z);
226 
227             // Read configs from HWC1 device
228             void populateConfigs();
229 
230             // Set configs for a virtual display
231             void populateConfigs(uint32_t width, uint32_t height);
232 
233             bool prepare();
234             HWC1Contents cloneRequestedContents() const;
235             void setReceivedContents(HWC1Contents contents);
236             bool hasChanges() const;
237             HWC2::Error set(hwc_display_contents_1& hwcContents);
238             void addRetireFence(int fenceFd);
239             void addReleaseFences(const hwc_display_contents_1& hwcContents);
240 
241             bool hasColorTransform() const;
242 
243             std::string dump() const;
244 
245         private:
246             class Config {
247                 public:
Config(Display & display)248                     Config(Display& display)
249                       : mDisplay(display),
250                         mId(0),
251                         mAttributes() {}
252 
isOnDisplay(const Display & display)253                     bool isOnDisplay(const Display& display) const {
254                         return display.getId() == mDisplay.getId();
255                     }
256 
257                     void setAttribute(HWC2::Attribute attribute, int32_t value);
258                     int32_t getAttribute(HWC2::Attribute attribute) const;
259 
260                     void setHwc1Id(uint32_t id);
261                     bool hasHwc1Id(uint32_t id) const;
262                     HWC2::Error getColorModeForHwc1Id(uint32_t id,
263                             android_color_mode_t *outMode) const;
264                     HWC2::Error getHwc1IdForColorMode(android_color_mode_t mode,
265                             uint32_t* outId) const;
266 
setId(hwc2_config_t id)267                     void setId(hwc2_config_t id) { mId = id; }
getId()268                     hwc2_config_t getId() const { return mId; }
269 
270                     // Attempts to merge two configs that differ only in color
271                     // mode. Returns whether the merge was successful
272                     bool merge(const Config& other);
273 
274                     std::set<android_color_mode_t> getColorModes() const;
275 
276                     // splitLine divides the output into two lines suitable for
277                     // dumpsys SurfaceFlinger
278                     std::string toString(bool splitLine = false) const;
279 
280                 private:
281                     Display& mDisplay;
282                     hwc2_config_t mId;
283                     std::unordered_map<HWC2::Attribute, int32_t> mAttributes;
284 
285                     // Maps from color transform to HWC1 config ID
286                     std::unordered_map<android_color_mode_t, uint32_t> mHwc1Ids;
287             };
288 
289             class Changes {
290                 public:
getNumTypes()291                     uint32_t getNumTypes() const {
292                         return static_cast<uint32_t>(mTypeChanges.size());
293                     }
294 
getNumLayerRequests()295                     uint32_t getNumLayerRequests() const {
296                         return static_cast<uint32_t>(mLayerRequests.size());
297                     }
298 
299                     const std::unordered_map<hwc2_layer_t, HWC2::Composition>&
getTypeChanges()300                             getTypeChanges() const {
301                         return mTypeChanges;
302                     }
303 
304                     const std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>&
getLayerRequests()305                             getLayerRequests() const {
306                         return mLayerRequests;
307                     }
308 
getDisplayRequests()309                     int32_t getDisplayRequests() const {
310                         int32_t requests = 0;
311                         for (auto request : mDisplayRequests) {
312                             requests |= static_cast<int32_t>(request);
313                         }
314                         return requests;
315                     }
316 
addTypeChange(hwc2_layer_t layerId,HWC2::Composition type)317                     void addTypeChange(hwc2_layer_t layerId,
318                             HWC2::Composition type) {
319                         mTypeChanges.insert({layerId, type});
320                     }
321 
clearTypeChanges()322                     void clearTypeChanges() { mTypeChanges.clear(); }
323 
addLayerRequest(hwc2_layer_t layerId,HWC2::LayerRequest request)324                     void addLayerRequest(hwc2_layer_t layerId,
325                             HWC2::LayerRequest request) {
326                         mLayerRequests.insert({layerId, request});
327                     }
328 
329                 private:
330                     std::unordered_map<hwc2_layer_t, HWC2::Composition>
331                             mTypeChanges;
332                     std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>
333                             mLayerRequests;
334                     std::unordered_set<HWC2::DisplayRequest> mDisplayRequests;
335             };
336 
337             std::shared_ptr<const Config>
338                     getConfig(hwc2_config_t configId) const;
339 
340             void populateColorModes();
341             void initializeActiveConfig();
342 
343             void reallocateHwc1Contents();
344             void assignHwc1LayerIds();
345 
346             void updateTypeChanges(const struct hwc_layer_1& hwc1Layer,
347                     const Layer& layer);
348             void updateLayerRequests(const struct hwc_layer_1& hwc1Layer,
349                     const Layer& layer);
350 
351             void prepareFramebufferTarget();
352 
353             static std::atomic<hwc2_display_t> sNextId;
354             const hwc2_display_t mId;
355             HWC2On1Adapter& mDevice;
356 
357             std::atomic<size_t> mDirtyCount;
358 
359             // The state of this display should only be modified from
360             // SurfaceFlinger's main loop, with the exception of when dump is
361             // called. To prevent a bad state from crashing us during a dump
362             // call, all public calls into Display must acquire this mutex.
363             //
364             // It is recursive because we don't want to deadlock in validate
365             // (or present) when we call HWC2On1Adapter::prepareAllDisplays
366             // (or setAllDisplays), which calls back into Display functions
367             // which require locking.
368             mutable std::recursive_mutex mStateMutex;
369 
370             bool mZIsDirty;
371             HWC1Contents mHwc1RequestedContents;
372             HWC1Contents mHwc1ReceivedContents;
373             DeferredFence mRetireFence;
374 
375             // Will only be non-null after the layer has been validated but
376             // before it has been presented
377             std::unique_ptr<Changes> mChanges;
378 
379             int32_t mHwc1Id;
380 
381             std::vector<std::shared_ptr<Config>> mConfigs;
382             std::shared_ptr<const Config> mActiveConfig;
383             std::set<android_color_mode_t> mColorModes;
384             android_color_mode_t mActiveColorMode;
385             std::string mName;
386             HWC2::DisplayType mType;
387             HWC2::PowerMode mPowerMode;
388             HWC2::Vsync mVsyncEnabled;
389 
390             FencedBuffer mClientTarget;
391             FencedBuffer mOutputBuffer;
392 
393             bool mHasColorTransform;
394 
395             std::multiset<std::shared_ptr<Layer>, SortLayersByZ> mLayers;
396             std::unordered_map<size_t, std::shared_ptr<Layer>> mHwc1LayerMap;
397     };
398 
399     template <typename ...Args>
callDisplayFunction(hwc2_device_t * device,hwc2_display_t displayId,HWC2::Error (Display::* member)(Args...),Args...args)400     static int32_t callDisplayFunction(hwc2_device_t* device,
401             hwc2_display_t displayId, HWC2::Error (Display::*member)(Args...),
402             Args... args) {
403         auto display = getAdapter(device)->getDisplay(displayId);
404         if (!display) {
405             return static_cast<int32_t>(HWC2::Error::BadDisplay);
406         }
407         auto error = ((*display).*member)(std::forward<Args>(args)...);
408         return static_cast<int32_t>(error);
409     }
410 
411     template <typename MF, MF memFunc, typename ...Args>
displayHook(hwc2_device_t * device,hwc2_display_t displayId,Args...args)412     static int32_t displayHook(hwc2_device_t* device, hwc2_display_t displayId,
413             Args... args) {
414         return HWC2On1Adapter::callDisplayFunction(device, displayId, memFunc,
415                 std::forward<Args>(args)...);
416     }
417 
getDisplayAttributeHook(hwc2_device_t * device,hwc2_display_t display,hwc2_config_t config,int32_t intAttribute,int32_t * outValue)418     static int32_t getDisplayAttributeHook(hwc2_device_t* device,
419             hwc2_display_t display, hwc2_config_t config,
420             int32_t intAttribute, int32_t* outValue) {
421         auto attribute = static_cast<HWC2::Attribute>(intAttribute);
422         return callDisplayFunction(device, display, &Display::getAttribute,
423                 config, attribute, outValue);
424     }
425 
setColorTransformHook(hwc2_device_t * device,hwc2_display_t display,const float *,int32_t intHint)426     static int32_t setColorTransformHook(hwc2_device_t* device,
427             hwc2_display_t display, const float* /*matrix*/,
428             int32_t /*android_color_transform_t*/ intHint) {
429         // We intentionally throw away the matrix, because if the hint is
430         // anything other than IDENTITY, we have to fall back to client
431         // composition anyway
432         auto hint = static_cast<android_color_transform_t>(intHint);
433         return callDisplayFunction(device, display, &Display::setColorTransform,
434                 hint);
435     }
436 
setColorModeHook(hwc2_device_t * device,hwc2_display_t display,int32_t intMode)437     static int32_t setColorModeHook(hwc2_device_t* device,
438             hwc2_display_t display, int32_t /*android_color_mode_t*/ intMode) {
439         auto mode = static_cast<android_color_mode_t>(intMode);
440         return callDisplayFunction(device, display, &Display::setColorMode, mode);
441     }
442 
setPowerModeHook(hwc2_device_t * device,hwc2_display_t display,int32_t intMode)443     static int32_t setPowerModeHook(hwc2_device_t* device,
444             hwc2_display_t display, int32_t intMode) {
445         auto mode = static_cast<HWC2::PowerMode>(intMode);
446         return callDisplayFunction(device, display, &Display::setPowerMode,
447                 mode);
448     }
449 
setVsyncEnabledHook(hwc2_device_t * device,hwc2_display_t display,int32_t intEnabled)450     static int32_t setVsyncEnabledHook(hwc2_device_t* device,
451             hwc2_display_t display, int32_t intEnabled) {
452         auto enabled = static_cast<HWC2::Vsync>(intEnabled);
453         return callDisplayFunction(device, display, &Display::setVsyncEnabled,
454                 enabled);
455     }
456 
457     // Layer functions
458 
459     template <typename T>
460     class LatchedState {
461         public:
LatchedState(Layer & parent,T initialValue)462             LatchedState(Layer& parent, T initialValue)
463               : mParent(parent),
464                 mPendingValue(initialValue),
465                 mValue(initialValue) {}
466 
setPending(T value)467             void setPending(T value) {
468                 if (value == mPendingValue) {
469                     return;
470                 }
471                 if (mPendingValue == mValue) {
472                     mParent.incDirty();
473                 } else if (value == mValue) {
474                     mParent.decDirty();
475                 }
476                 mPendingValue = value;
477             }
478 
getValue()479             T getValue() const { return mValue; }
getPendingValue()480             T getPendingValue() const { return mPendingValue; }
481 
isDirty()482             bool isDirty() const { return mPendingValue != mValue; }
483 
latch()484             void latch() {
485                 if (isDirty()) {
486                     mValue = mPendingValue;
487                     mParent.decDirty();
488                 }
489             }
490 
491         private:
492             Layer& mParent;
493             T mPendingValue;
494             T mValue;
495     };
496 
497     class Layer {
498         public:
499             explicit Layer(Display& display);
500 
501             bool operator==(const Layer& other) { return mId == other.mId; }
502             bool operator!=(const Layer& other) { return !(*this == other); }
503 
getId()504             hwc2_layer_t getId() const { return mId; }
getDisplay()505             Display& getDisplay() const { return mDisplay; }
506 
incDirty()507             void incDirty() { if (mDirtyCount++ == 0) mDisplay.incDirty(); }
decDirty()508             void decDirty() { if (--mDirtyCount == 0) mDisplay.decDirty(); }
isDirty()509             bool isDirty() const { return mDirtyCount > 0; }
510 
511             // HWC2 Layer functions
512             HWC2::Error setBuffer(buffer_handle_t buffer, int32_t acquireFence);
513             HWC2::Error setCursorPosition(int32_t x, int32_t y);
514             HWC2::Error setSurfaceDamage(hwc_region_t damage);
515 
516             // HWC2 Layer state functions
517             HWC2::Error setBlendMode(HWC2::BlendMode mode);
518             HWC2::Error setColor(hwc_color_t color);
519             HWC2::Error setCompositionType(HWC2::Composition type);
520             HWC2::Error setDataspace(android_dataspace_t dataspace);
521             HWC2::Error setDisplayFrame(hwc_rect_t frame);
522             HWC2::Error setPlaneAlpha(float alpha);
523             HWC2::Error setSidebandStream(const native_handle_t* stream);
524             HWC2::Error setSourceCrop(hwc_frect_t crop);
525             HWC2::Error setTransform(HWC2::Transform transform);
526             HWC2::Error setVisibleRegion(hwc_region_t visible);
527             HWC2::Error setZ(uint32_t z);
528 
getCompositionType()529             HWC2::Composition getCompositionType() const {
530                 return mCompositionType.getValue();
531             }
getZ()532             uint32_t getZ() const { return mZ; }
533 
534             void addReleaseFence(int fenceFd);
535             const sp<Fence>& getReleaseFence() const;
536 
setHwc1Id(size_t id)537             void setHwc1Id(size_t id) { mHwc1Id = id; }
getHwc1Id()538             size_t getHwc1Id() const { return mHwc1Id; }
539 
540             void applyState(struct hwc_layer_1& hwc1Layer, bool applyAllState);
541 
542             std::string dump() const;
543 
544         private:
545             void applyCommonState(struct hwc_layer_1& hwc1Layer,
546                     bool applyAllState);
547             void applySolidColorState(struct hwc_layer_1& hwc1Layer,
548                     bool applyAllState);
549             void applySidebandState(struct hwc_layer_1& hwc1Layer,
550                     bool applyAllState);
551             void applyBufferState(struct hwc_layer_1& hwc1Layer);
552             void applyCompositionType(struct hwc_layer_1& hwc1Layer,
553                     bool applyAllState);
554 
555             static std::atomic<hwc2_layer_t> sNextId;
556             const hwc2_layer_t mId;
557             Display& mDisplay;
558             size_t mDirtyCount;
559 
560             FencedBuffer mBuffer;
561             std::vector<hwc_rect_t> mSurfaceDamage;
562 
563             LatchedState<HWC2::BlendMode> mBlendMode;
564             LatchedState<hwc_color_t> mColor;
565             LatchedState<HWC2::Composition> mCompositionType;
566             LatchedState<hwc_rect_t> mDisplayFrame;
567             LatchedState<float> mPlaneAlpha;
568             LatchedState<const native_handle_t*> mSidebandStream;
569             LatchedState<hwc_frect_t> mSourceCrop;
570             LatchedState<HWC2::Transform> mTransform;
571             LatchedState<std::vector<hwc_rect_t>> mVisibleRegion;
572             uint32_t mZ;
573 
574             DeferredFence mReleaseFence;
575 
576             size_t mHwc1Id;
577             bool mHasUnsupportedDataspace;
578             bool mHasUnsupportedPlaneAlpha;
579     };
580 
581     template <typename ...Args>
callLayerFunction(hwc2_device_t * device,hwc2_display_t displayId,hwc2_layer_t layerId,HWC2::Error (Layer::* member)(Args...),Args...args)582     static int32_t callLayerFunction(hwc2_device_t* device,
583             hwc2_display_t displayId, hwc2_layer_t layerId,
584             HWC2::Error (Layer::*member)(Args...), Args... args) {
585         auto result = getAdapter(device)->getLayer(displayId, layerId);
586         auto error = std::get<HWC2::Error>(result);
587         if (error == HWC2::Error::None) {
588             auto layer = std::get<Layer*>(result);
589             error = ((*layer).*member)(std::forward<Args>(args)...);
590         }
591         return static_cast<int32_t>(error);
592     }
593 
594     template <typename MF, MF memFunc, typename ...Args>
layerHook(hwc2_device_t * device,hwc2_display_t displayId,hwc2_layer_t layerId,Args...args)595     static int32_t layerHook(hwc2_device_t* device, hwc2_display_t displayId,
596             hwc2_layer_t layerId, Args... args) {
597         return HWC2On1Adapter::callLayerFunction(device, displayId, layerId,
598                 memFunc, std::forward<Args>(args)...);
599     }
600 
601     // Layer state functions
602 
setLayerBlendModeHook(hwc2_device_t * device,hwc2_display_t display,hwc2_layer_t layer,int32_t intMode)603     static int32_t setLayerBlendModeHook(hwc2_device_t* device,
604             hwc2_display_t display, hwc2_layer_t layer, int32_t intMode) {
605         auto mode = static_cast<HWC2::BlendMode>(intMode);
606         return callLayerFunction(device, display, layer,
607                 &Layer::setBlendMode, mode);
608     }
609 
setLayerCompositionTypeHook(hwc2_device_t * device,hwc2_display_t display,hwc2_layer_t layer,int32_t intType)610     static int32_t setLayerCompositionTypeHook(hwc2_device_t* device,
611             hwc2_display_t display, hwc2_layer_t layer, int32_t intType) {
612         auto type = static_cast<HWC2::Composition>(intType);
613         return callLayerFunction(device, display, layer,
614                 &Layer::setCompositionType, type);
615     }
616 
setLayerDataspaceHook(hwc2_device_t * device,hwc2_display_t display,hwc2_layer_t layer,int32_t intDataspace)617     static int32_t setLayerDataspaceHook(hwc2_device_t* device,
618             hwc2_display_t display, hwc2_layer_t layer, int32_t intDataspace) {
619         auto dataspace = static_cast<android_dataspace_t>(intDataspace);
620         return callLayerFunction(device, display, layer, &Layer::setDataspace,
621                 dataspace);
622     }
623 
setLayerTransformHook(hwc2_device_t * device,hwc2_display_t display,hwc2_layer_t layer,int32_t intTransform)624     static int32_t setLayerTransformHook(hwc2_device_t* device,
625             hwc2_display_t display, hwc2_layer_t layer, int32_t intTransform) {
626         auto transform = static_cast<HWC2::Transform>(intTransform);
627         return callLayerFunction(device, display, layer, &Layer::setTransform,
628                 transform);
629     }
630 
setLayerZOrderHook(hwc2_device_t * device,hwc2_display_t display,hwc2_layer_t layer,uint32_t z)631     static int32_t setLayerZOrderHook(hwc2_device_t* device,
632             hwc2_display_t display, hwc2_layer_t layer, uint32_t z) {
633         return callDisplayFunction(device, display, &Display::updateLayerZ,
634                 layer, z);
635     }
636 
637     // Adapter internals
638 
639     void populateCapabilities();
640     Display* getDisplay(hwc2_display_t id);
641     std::tuple<Layer*, HWC2::Error> getLayer(hwc2_display_t displayId,
642             hwc2_layer_t layerId);
643     void populatePrimary();
644 
645     bool prepareAllDisplays();
646     std::vector<struct hwc_display_contents_1*> mHwc1Contents;
647     HWC2::Error setAllDisplays();
648 
649     void hwc1Invalidate();
650     void hwc1Vsync(int hwc1DisplayId, int64_t timestamp);
651     void hwc1Hotplug(int hwc1DisplayId, int connected);
652 
653     // These are set in the constructor and before any asynchronous events are
654     // possible
655 
656     struct hwc_composer_device_1* const mHwc1Device;
657     const uint8_t mHwc1MinorVersion;
658     bool mHwc1SupportsVirtualDisplays;
659 
660     class Callbacks;
661     const std::unique_ptr<Callbacks> mHwc1Callbacks;
662 
663     std::unordered_set<HWC2::Capability> mCapabilities;
664 
665     // These are only accessed from the main SurfaceFlinger thread (not from
666     // callbacks or dump
667 
668     std::map<hwc2_layer_t, std::shared_ptr<Layer>> mLayers;
669     std::shared_ptr<Display> mHwc1VirtualDisplay;
670 
671     // These are potentially accessed from multiple threads, and are protected
672     // by this mutex. This needs to be recursive, since the HWC1 implementation
673     // can call back into the invalidate callback on the same thread that is
674     // calling prepare.
675     std::recursive_timed_mutex mStateMutex;
676 
677     struct CallbackInfo {
678         hwc2_callback_data_t data;
679         hwc2_function_pointer_t pointer;
680     };
681     std::unordered_map<HWC2::Callback, CallbackInfo> mCallbacks;
682     bool mHasPendingInvalidate;
683     std::vector<std::pair<int, int64_t>> mPendingVsyncs;
684     std::vector<std::pair<int, int>> mPendingHotplugs;
685 
686     std::map<hwc2_display_t, std::shared_ptr<Display>> mDisplays;
687     std::unordered_map<int, hwc2_display_t> mHwc1DisplayMap;
688 };
689 
690 } // namespace android
691 
692 #endif
693