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