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