blob: b46556d43d5df5c7a0dfef734712e94685fab4fc [file] [log] [blame]
Dan Stozac6998d22015-09-24 17:03:36 -07001/*
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
37struct hwc_composer_device_1;
38struct hwc_display_contents_1;
39struct hwc_layer_1;
40
41namespace android {
42
43class HWC2On1Adapter : public hwc2_device_t
44{
45public:
46 HWC2On1Adapter(struct hwc_composer_device_1* hwc1Device);
47 ~HWC2On1Adapter();
48
49 struct hwc_composer_device_1* getHwc1Device() const { return mHwc1Device; }
50 uint8_t getHwc1MinorVersion() const { return mHwc1MinorVersion; }
51
52private:
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);
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);
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);
79 static int32_t createVirtualDisplayHook(hwc2_device_t* device,
Dan Stoza5cf424b2016-05-20 14:02:39 -070080 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
Dan Stozac6998d22015-09-24 17:03:36 -070084 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);
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);
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();
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);
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:
136 DeferredFence()
137 : mMutex(),
138 mFences({Fence::NO_FENCE, Fence::NO_FENCE}) {}
139
140 void add(int32_t fenceFd) {
141 mFences.emplace(new Fence(fenceFd));
142 mFences.pop();
143 }
144
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:
156 FencedBuffer() : mBuffer(nullptr), mFence(Fence::NO_FENCE) {}
157
158 void setBuffer(buffer_handle_t buffer) { mBuffer = buffer; }
159 void setFence(int fenceFd) { mFence = new Fence(fenceFd); }
160
161 buffer_handle_t getBuffer() const { return mBuffer; }
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
176 hwc2_display_t getId() const { return mId; }
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
181 void setHwc1Id(int32_t id) { mHwc1Id = id; }
182 int32_t getHwc1Id() const { return mHwc1Id; }
183
184 void incDirty() { ++mDirtyCount; }
185 void decDirty() { --mDirtyCount; }
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);
Dan Stoza076ac672016-03-14 10:47:53 -0700197 HWC2::Error getColorModes(uint32_t* outNumModes, int32_t* outModes);
Dan Stozac6998d22015-09-24 17:03:36 -0700198 HWC2::Error getConfigs(uint32_t* outNumConfigs,
199 hwc2_config_t* outConfigIds);
200 HWC2::Error getDozeSupport(int32_t* outSupport);
Dan Stozaed40eba2016-03-16 12:33:52 -0700201 HWC2::Error getHdrCapabilities(uint32_t* outNumTypes,
202 int32_t* outTypes, float* outMaxLuminance,
203 float* outMaxAverageLuminance, float* outMinLuminance);
Dan Stozac6998d22015-09-24 17:03:36 -0700204 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,
Dan Stoza5cf424b2016-05-20 14:02:39 -0700214 int32_t acquireFence, int32_t dataspace,
215 hwc_region_t damage);
Dan Stoza076ac672016-03-14 10:47:53 -0700216 HWC2::Error setColorMode(int32_t mode);
Dan Stoza5df2a862016-03-24 16:19:37 -0700217 HWC2::Error setColorTransform(android_color_transform_t hint);
Dan Stozac6998d22015-09-24 17:03:36 -0700218 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
Dan Stoza5df2a862016-03-24 16:19:37 -0700241 bool hasColorTransform() const;
242
Dan Stozac6998d22015-09-24 17:03:36 -0700243 std::string dump() const;
244
245 private:
246 class Config {
247 public:
Dan Stoza076ac672016-03-14 10:47:53 -0700248 Config(Display& display)
Dan Stozac6998d22015-09-24 17:03:36 -0700249 : mDisplay(display),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -0700250 mId(0),
Dan Stozafc4e2022016-02-23 11:43:19 -0800251 mAttributes() {}
Dan Stozac6998d22015-09-24 17:03:36 -0700252
253 bool isOnDisplay(const Display& display) const {
254 return display.getId() == mDisplay.getId();
255 }
256
Dan Stozac6998d22015-09-24 17:03:36 -0700257 void setAttribute(HWC2::Attribute attribute, int32_t value);
258 int32_t getAttribute(HWC2::Attribute attribute) const;
259
Dan Stoza076ac672016-03-14 10:47:53 -0700260 void setHwc1Id(uint32_t id);
261 bool hasHwc1Id(uint32_t id) const;
262 int32_t getColorModeForHwc1Id(uint32_t id) const;
263 HWC2::Error getHwc1IdForColorMode(int32_t mode,
264 uint32_t* outId) const;
265
266 void setId(hwc2_config_t id) { mId = id; }
267 hwc2_config_t getId() const { return mId; }
268
269 // Attempts to merge two configs that differ only in color
270 // mode. Returns whether the merge was successful
271 bool merge(const Config& other);
272
273 std::set<int32_t> getColorTransforms() const;
274
275 // splitLine divides the output into two lines suitable for
276 // dumpsys SurfaceFlinger
277 std::string toString(bool splitLine = false) const;
Dan Stozac6998d22015-09-24 17:03:36 -0700278
279 private:
280 Display& mDisplay;
Dan Stoza076ac672016-03-14 10:47:53 -0700281 hwc2_config_t mId;
Dan Stozac6998d22015-09-24 17:03:36 -0700282 std::unordered_map<HWC2::Attribute, int32_t> mAttributes;
Dan Stoza076ac672016-03-14 10:47:53 -0700283
284 // Maps from color transform to HWC1 config ID
285 std::unordered_map<int32_t, uint32_t> mHwc1Ids;
Dan Stozac6998d22015-09-24 17:03:36 -0700286 };
287
288 class Changes {
289 public:
290 uint32_t getNumTypes() const {
291 return static_cast<uint32_t>(mTypeChanges.size());
292 }
293
294 uint32_t getNumLayerRequests() const {
295 return static_cast<uint32_t>(mLayerRequests.size());
296 }
297
298 const std::unordered_map<hwc2_layer_t, HWC2::Composition>&
299 getTypeChanges() const {
300 return mTypeChanges;
301 }
302
303 const std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>&
304 getLayerRequests() const {
305 return mLayerRequests;
306 }
307
308 int32_t getDisplayRequests() const {
309 int32_t requests = 0;
310 for (auto request : mDisplayRequests) {
311 requests |= static_cast<int32_t>(request);
312 }
313 return requests;
314 }
315
316 void addTypeChange(hwc2_layer_t layerId,
317 HWC2::Composition type) {
318 mTypeChanges.insert({layerId, type});
319 }
320
321 void clearTypeChanges() { mTypeChanges.clear(); }
322
323 void addLayerRequest(hwc2_layer_t layerId,
324 HWC2::LayerRequest request) {
325 mLayerRequests.insert({layerId, request});
326 }
327
328 private:
329 std::unordered_map<hwc2_layer_t, HWC2::Composition>
330 mTypeChanges;
331 std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>
332 mLayerRequests;
333 std::unordered_set<HWC2::DisplayRequest> mDisplayRequests;
334 };
335
336 std::shared_ptr<const Config>
337 getConfig(hwc2_config_t configId) const;
338
Dan Stoza076ac672016-03-14 10:47:53 -0700339 void populateColorModes();
340 void initializeActiveConfig();
341
Dan Stozac6998d22015-09-24 17:03:36 -0700342 void reallocateHwc1Contents();
343 void assignHwc1LayerIds();
344
345 void updateTypeChanges(const struct hwc_layer_1& hwc1Layer,
346 const Layer& layer);
347 void updateLayerRequests(const struct hwc_layer_1& hwc1Layer,
348 const Layer& layer);
349
350 void prepareFramebufferTarget();
351
352 static std::atomic<hwc2_display_t> sNextId;
353 const hwc2_display_t mId;
354 HWC2On1Adapter& mDevice;
355
356 std::atomic<size_t> mDirtyCount;
357
358 // The state of this display should only be modified from
359 // SurfaceFlinger's main loop, with the exception of when dump is
360 // called. To prevent a bad state from crashing us during a dump
361 // call, all public calls into Display must acquire this mutex.
362 //
363 // It is recursive because we don't want to deadlock in validate
364 // (or present) when we call HWC2On1Adapter::prepareAllDisplays
365 // (or setAllDisplays), which calls back into Display functions
366 // which require locking.
367 mutable std::recursive_mutex mStateMutex;
368
369 bool mZIsDirty;
370 HWC1Contents mHwc1RequestedContents;
371 HWC1Contents mHwc1ReceivedContents;
372 DeferredFence mRetireFence;
373
374 // Will only be non-null after the layer has been validated but
375 // before it has been presented
376 std::unique_ptr<Changes> mChanges;
377
378 int32_t mHwc1Id;
Dan Stoza076ac672016-03-14 10:47:53 -0700379
Dan Stozac6998d22015-09-24 17:03:36 -0700380 std::vector<std::shared_ptr<Config>> mConfigs;
381 std::shared_ptr<const Config> mActiveConfig;
Dan Stoza076ac672016-03-14 10:47:53 -0700382 std::set<int32_t> mColorModes;
383 int32_t mActiveColorMode;
Dan Stozac6998d22015-09-24 17:03:36 -0700384 std::string mName;
385 HWC2::DisplayType mType;
386 HWC2::PowerMode mPowerMode;
387 HWC2::Vsync mVsyncEnabled;
388
389 FencedBuffer mClientTarget;
390 FencedBuffer mOutputBuffer;
391
Dan Stoza5df2a862016-03-24 16:19:37 -0700392 bool mHasColorTransform;
393
Dan Stozac6998d22015-09-24 17:03:36 -0700394 std::multiset<std::shared_ptr<Layer>, SortLayersByZ> mLayers;
395 std::unordered_map<size_t, std::shared_ptr<Layer>> mHwc1LayerMap;
396 };
397
398 template <typename ...Args>
399 static int32_t callDisplayFunction(hwc2_device_t* device,
400 hwc2_display_t displayId, HWC2::Error (Display::*member)(Args...),
401 Args... args) {
402 auto display = getAdapter(device)->getDisplay(displayId);
403 if (!display) {
404 return static_cast<int32_t>(HWC2::Error::BadDisplay);
405 }
406 auto error = ((*display).*member)(std::forward<Args>(args)...);
407 return static_cast<int32_t>(error);
408 }
409
410 template <typename MF, MF memFunc, typename ...Args>
411 static int32_t displayHook(hwc2_device_t* device, hwc2_display_t displayId,
412 Args... args) {
413 return HWC2On1Adapter::callDisplayFunction(device, displayId, memFunc,
414 std::forward<Args>(args)...);
415 }
416
417 static int32_t getDisplayAttributeHook(hwc2_device_t* device,
418 hwc2_display_t display, hwc2_config_t config,
419 int32_t intAttribute, int32_t* outValue) {
420 auto attribute = static_cast<HWC2::Attribute>(intAttribute);
421 return callDisplayFunction(device, display, &Display::getAttribute,
422 config, attribute, outValue);
423 }
424
Dan Stoza5df2a862016-03-24 16:19:37 -0700425 static int32_t setColorTransformHook(hwc2_device_t* device,
426 hwc2_display_t display, const float* /*matrix*/,
427 int32_t /*android_color_transform_t*/ intHint) {
428 // We intentionally throw away the matrix, because if the hint is
429 // anything other than IDENTITY, we have to fall back to client
430 // composition anyway
431 auto hint = static_cast<android_color_transform_t>(intHint);
432 return callDisplayFunction(device, display, &Display::setColorTransform,
433 hint);
434 }
435
Dan Stozac6998d22015-09-24 17:03:36 -0700436 static int32_t setPowerModeHook(hwc2_device_t* device,
437 hwc2_display_t display, int32_t intMode) {
438 auto mode = static_cast<HWC2::PowerMode>(intMode);
439 return callDisplayFunction(device, display, &Display::setPowerMode,
440 mode);
441 }
442
443 static int32_t setVsyncEnabledHook(hwc2_device_t* device,
444 hwc2_display_t display, int32_t intEnabled) {
445 auto enabled = static_cast<HWC2::Vsync>(intEnabled);
446 return callDisplayFunction(device, display, &Display::setVsyncEnabled,
447 enabled);
448 }
449
450 // Layer functions
451
452 template <typename T>
453 class LatchedState {
454 public:
455 LatchedState(Layer& parent, T initialValue)
456 : mParent(parent),
457 mPendingValue(initialValue),
458 mValue(initialValue) {}
459
460 void setPending(T value) {
461 if (value == mPendingValue) {
462 return;
463 }
464 if (mPendingValue == mValue) {
465 mParent.incDirty();
466 } else if (value == mValue) {
467 mParent.decDirty();
468 }
469 mPendingValue = value;
470 }
471
472 T getValue() const { return mValue; }
473 T getPendingValue() const { return mPendingValue; }
474
475 bool isDirty() const { return mPendingValue != mValue; }
476
477 void latch() {
478 if (isDirty()) {
479 mValue = mPendingValue;
480 mParent.decDirty();
481 }
482 }
483
484 private:
485 Layer& mParent;
486 T mPendingValue;
487 T mValue;
488 };
489
490 class Layer {
491 public:
492 Layer(Display& display);
493
494 bool operator==(const Layer& other) { return mId == other.mId; }
495 bool operator!=(const Layer& other) { return !(*this == other); }
496
497 hwc2_layer_t getId() const { return mId; }
498 Display& getDisplay() const { return mDisplay; }
499
500 void incDirty() { if (mDirtyCount++ == 0) mDisplay.incDirty(); }
501 void decDirty() { if (--mDirtyCount == 0) mDisplay.decDirty(); }
502 bool isDirty() const { return mDirtyCount > 0; }
503
504 // HWC2 Layer functions
505 HWC2::Error setBuffer(buffer_handle_t buffer, int32_t acquireFence);
506 HWC2::Error setCursorPosition(int32_t x, int32_t y);
507 HWC2::Error setSurfaceDamage(hwc_region_t damage);
508
509 // HWC2 Layer state functions
510 HWC2::Error setBlendMode(HWC2::BlendMode mode);
511 HWC2::Error setColor(hwc_color_t color);
512 HWC2::Error setCompositionType(HWC2::Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700513 HWC2::Error setDataspace(android_dataspace_t dataspace);
Dan Stozac6998d22015-09-24 17:03:36 -0700514 HWC2::Error setDisplayFrame(hwc_rect_t frame);
515 HWC2::Error setPlaneAlpha(float alpha);
516 HWC2::Error setSidebandStream(const native_handle_t* stream);
517 HWC2::Error setSourceCrop(hwc_frect_t crop);
518 HWC2::Error setTransform(HWC2::Transform transform);
519 HWC2::Error setVisibleRegion(hwc_region_t visible);
520 HWC2::Error setZ(uint32_t z);
521
522 HWC2::Composition getCompositionType() const {
523 return mCompositionType.getValue();
524 }
525 uint32_t getZ() const { return mZ; }
526
527 void addReleaseFence(int fenceFd);
528 const sp<Fence>& getReleaseFence() const;
529
530 void setHwc1Id(size_t id) { mHwc1Id = id; }
531 size_t getHwc1Id() const { return mHwc1Id; }
532
533 void applyState(struct hwc_layer_1& hwc1Layer, bool applyAllState);
534
535 std::string dump() const;
536
537 private:
538 void applyCommonState(struct hwc_layer_1& hwc1Layer,
539 bool applyAllState);
540 void applySolidColorState(struct hwc_layer_1& hwc1Layer,
541 bool applyAllState);
542 void applySidebandState(struct hwc_layer_1& hwc1Layer,
543 bool applyAllState);
544 void applyBufferState(struct hwc_layer_1& hwc1Layer);
545 void applyCompositionType(struct hwc_layer_1& hwc1Layer,
546 bool applyAllState);
547
548 static std::atomic<hwc2_layer_t> sNextId;
549 const hwc2_layer_t mId;
550 Display& mDisplay;
551 size_t mDirtyCount;
552
553 FencedBuffer mBuffer;
554 std::vector<hwc_rect_t> mSurfaceDamage;
555
556 LatchedState<HWC2::BlendMode> mBlendMode;
557 LatchedState<hwc_color_t> mColor;
558 LatchedState<HWC2::Composition> mCompositionType;
559 LatchedState<hwc_rect_t> mDisplayFrame;
560 LatchedState<float> mPlaneAlpha;
561 LatchedState<const native_handle_t*> mSidebandStream;
562 LatchedState<hwc_frect_t> mSourceCrop;
563 LatchedState<HWC2::Transform> mTransform;
564 LatchedState<std::vector<hwc_rect_t>> mVisibleRegion;
565 uint32_t mZ;
566
567 DeferredFence mReleaseFence;
568
569 size_t mHwc1Id;
Dan Stoza5df2a862016-03-24 16:19:37 -0700570 bool mHasUnsupportedDataspace;
Dan Stozac6998d22015-09-24 17:03:36 -0700571 bool mHasUnsupportedPlaneAlpha;
572 };
573
574 template <typename ...Args>
575 static int32_t callLayerFunction(hwc2_device_t* device,
576 hwc2_display_t displayId, hwc2_layer_t layerId,
577 HWC2::Error (Layer::*member)(Args...), Args... args) {
578 auto result = getAdapter(device)->getLayer(displayId, layerId);
579 auto error = std::get<HWC2::Error>(result);
580 if (error == HWC2::Error::None) {
581 auto layer = std::get<Layer*>(result);
582 error = ((*layer).*member)(std::forward<Args>(args)...);
583 }
584 return static_cast<int32_t>(error);
585 }
586
587 template <typename MF, MF memFunc, typename ...Args>
588 static int32_t layerHook(hwc2_device_t* device, hwc2_display_t displayId,
589 hwc2_layer_t layerId, Args... args) {
590 return HWC2On1Adapter::callLayerFunction(device, displayId, layerId,
591 memFunc, std::forward<Args>(args)...);
592 }
593
594 // Layer state functions
595
596 static int32_t setLayerBlendModeHook(hwc2_device_t* device,
597 hwc2_display_t display, hwc2_layer_t layer, int32_t intMode) {
598 auto mode = static_cast<HWC2::BlendMode>(intMode);
599 return callLayerFunction(device, display, layer,
600 &Layer::setBlendMode, mode);
601 }
602
603 static int32_t setLayerCompositionTypeHook(hwc2_device_t* device,
604 hwc2_display_t display, hwc2_layer_t layer, int32_t intType) {
605 auto type = static_cast<HWC2::Composition>(intType);
606 return callLayerFunction(device, display, layer,
607 &Layer::setCompositionType, type);
608 }
609
Dan Stoza5df2a862016-03-24 16:19:37 -0700610 static int32_t setLayerDataspaceHook(hwc2_device_t* device,
611 hwc2_display_t display, hwc2_layer_t layer, int32_t intDataspace) {
612 auto dataspace = static_cast<android_dataspace_t>(intDataspace);
613 return callLayerFunction(device, display, layer, &Layer::setDataspace,
614 dataspace);
615 }
616
Dan Stozac6998d22015-09-24 17:03:36 -0700617 static int32_t setLayerTransformHook(hwc2_device_t* device,
618 hwc2_display_t display, hwc2_layer_t layer, int32_t intTransform) {
619 auto transform = static_cast<HWC2::Transform>(intTransform);
620 return callLayerFunction(device, display, layer, &Layer::setTransform,
621 transform);
622 }
623
624 static int32_t setLayerZOrderHook(hwc2_device_t* device,
625 hwc2_display_t display, hwc2_layer_t layer, uint32_t z) {
626 return callDisplayFunction(device, display, &Display::updateLayerZ,
627 layer, z);
628 }
629
630 // Adapter internals
631
632 void populateCapabilities();
633 Display* getDisplay(hwc2_display_t id);
634 std::tuple<Layer*, HWC2::Error> getLayer(hwc2_display_t displayId,
635 hwc2_layer_t layerId);
636 void populatePrimary();
637
638 bool prepareAllDisplays();
639 std::vector<struct hwc_display_contents_1*> mHwc1Contents;
640 HWC2::Error setAllDisplays();
641
642 void hwc1Invalidate();
643 void hwc1Vsync(int hwc1DisplayId, int64_t timestamp);
644 void hwc1Hotplug(int hwc1DisplayId, int connected);
645
646 // These are set in the constructor and before any asynchronous events are
647 // possible
648
649 struct hwc_composer_device_1* const mHwc1Device;
650 const uint8_t mHwc1MinorVersion;
651 bool mHwc1SupportsVirtualDisplays;
652
653 class Callbacks;
654 const std::unique_ptr<Callbacks> mHwc1Callbacks;
655
656 std::unordered_set<HWC2::Capability> mCapabilities;
657
658 // These are only accessed from the main SurfaceFlinger thread (not from
659 // callbacks or dump
660
661 std::map<hwc2_layer_t, std::shared_ptr<Layer>> mLayers;
662 std::shared_ptr<Display> mHwc1VirtualDisplay;
663
664 // These are potentially accessed from multiple threads, and are protected
Dan Stozafc4e2022016-02-23 11:43:19 -0800665 // by this mutex. This needs to be recursive, since the HWC1 implementation
666 // can call back into the invalidate callback on the same thread that is
667 // calling prepare.
668 std::recursive_timed_mutex mStateMutex;
Dan Stozac6998d22015-09-24 17:03:36 -0700669
670 struct CallbackInfo {
671 hwc2_callback_data_t data;
672 hwc2_function_pointer_t pointer;
673 };
674 std::unordered_map<HWC2::Callback, CallbackInfo> mCallbacks;
675 bool mHasPendingInvalidate;
676 std::vector<std::pair<int, int64_t>> mPendingVsyncs;
677 std::vector<std::pair<int, int>> mPendingHotplugs;
678
679 std::map<hwc2_display_t, std::shared_ptr<Display>> mDisplays;
680 std::unordered_map<int, hwc2_display_t> mHwc1DisplayMap;
681};
682
683} // namespace android
684
685#endif