blob: aa960047fb32bdae06aab41797c15ec67c29487b [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()
Fabien Sanglard27efbdb2016-11-29 11:14:54 -0800141 : mFences({Fence::NO_FENCE, Fence::NO_FENCE}) {}
Dan Stozac6998d22015-09-24 17:03:36 -0700142
143 void add(int32_t fenceFd) {
144 mFences.emplace(new Fence(fenceFd));
145 mFences.pop();
146 }
147
148 const sp<Fence>& get() const {
149 return mFences.front();
150 }
151
152 private:
Dan Stozac6998d22015-09-24 17:03:36 -0700153 std::queue<sp<Fence>> mFences;
154 };
155
156 class FencedBuffer {
157 public:
158 FencedBuffer() : mBuffer(nullptr), mFence(Fence::NO_FENCE) {}
159
160 void setBuffer(buffer_handle_t buffer) { mBuffer = buffer; }
161 void setFence(int fenceFd) { mFence = new Fence(fenceFd); }
162
163 buffer_handle_t getBuffer() const { return mBuffer; }
164 int getFence() const { return mFence->dup(); }
165
166 private:
167 buffer_handle_t mBuffer;
168 sp<Fence> mFence;
169 };
170
171 class Display {
172 public:
173 typedef std::unique_ptr<hwc_display_contents_1,
174 DisplayContentsDeleter> HWC1Contents;
175
176 Display(HWC2On1Adapter& device, HWC2::DisplayType type);
177
178 hwc2_display_t getId() const { return mId; }
179 HWC2On1Adapter& getDevice() const { return mDevice; }
180
181 // Does not require locking because it is set before adding the
182 // Displays to the Adapter's list of displays
183 void setHwc1Id(int32_t id) { mHwc1Id = id; }
184 int32_t getHwc1Id() const { return mHwc1Id; }
185
186 void incDirty() { ++mDirtyCount; }
187 void decDirty() { --mDirtyCount; }
188 bool isDirty() const { return mDirtyCount > 0 || mZIsDirty; }
189
190 // HWC2 Display functions
191 HWC2::Error acceptChanges();
192 HWC2::Error createLayer(hwc2_layer_t* outLayerId);
193 HWC2::Error destroyLayer(hwc2_layer_t layerId);
194 HWC2::Error getActiveConfig(hwc2_config_t* outConfigId);
195 HWC2::Error getAttribute(hwc2_config_t configId,
196 HWC2::Attribute attribute, int32_t* outValue);
197 HWC2::Error getChangedCompositionTypes(uint32_t* outNumElements,
198 hwc2_layer_t* outLayers, int32_t* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700199 HWC2::Error getColorModes(uint32_t* outNumModes, int32_t* outModes);
Dan Stozac6998d22015-09-24 17:03:36 -0700200 HWC2::Error getConfigs(uint32_t* outNumConfigs,
201 hwc2_config_t* outConfigIds);
202 HWC2::Error getDozeSupport(int32_t* outSupport);
Dan Stozaed40eba2016-03-16 12:33:52 -0700203 HWC2::Error getHdrCapabilities(uint32_t* outNumTypes,
204 int32_t* outTypes, float* outMaxLuminance,
205 float* outMaxAverageLuminance, float* outMinLuminance);
Dan Stozac6998d22015-09-24 17:03:36 -0700206 HWC2::Error getName(uint32_t* outSize, char* outName);
207 HWC2::Error getReleaseFences(uint32_t* outNumElements,
208 hwc2_layer_t* outLayers, int32_t* outFences);
209 HWC2::Error getRequests(int32_t* outDisplayRequests,
210 uint32_t* outNumElements, hwc2_layer_t* outLayers,
211 int32_t* outLayerRequests);
212 HWC2::Error getType(int32_t* outType);
213 HWC2::Error present(int32_t* outRetireFence);
214 HWC2::Error setActiveConfig(hwc2_config_t configId);
215 HWC2::Error setClientTarget(buffer_handle_t target,
Dan Stoza5cf424b2016-05-20 14:02:39 -0700216 int32_t acquireFence, int32_t dataspace,
217 hwc_region_t damage);
Michael Wright28f24d02016-07-12 13:30:53 -0700218 HWC2::Error setColorMode(android_color_mode_t mode);
Dan Stoza5df2a862016-03-24 16:19:37 -0700219 HWC2::Error setColorTransform(android_color_transform_t hint);
Dan Stozac6998d22015-09-24 17:03:36 -0700220 HWC2::Error setOutputBuffer(buffer_handle_t buffer,
221 int32_t releaseFence);
222 HWC2::Error setPowerMode(HWC2::PowerMode mode);
223 HWC2::Error setVsyncEnabled(HWC2::Vsync enabled);
224 HWC2::Error validate(uint32_t* outNumTypes,
225 uint32_t* outNumRequests);
226
227 HWC2::Error updateLayerZ(hwc2_layer_t layerId, uint32_t z);
228
229 // Read configs from HWC1 device
230 void populateConfigs();
231
232 // Set configs for a virtual display
233 void populateConfigs(uint32_t width, uint32_t height);
234
235 bool prepare();
236 HWC1Contents cloneRequestedContents() const;
237 void setReceivedContents(HWC1Contents contents);
238 bool hasChanges() const;
239 HWC2::Error set(hwc_display_contents_1& hwcContents);
240 void addRetireFence(int fenceFd);
241 void addReleaseFences(const hwc_display_contents_1& hwcContents);
242
Dan Stoza5df2a862016-03-24 16:19:37 -0700243 bool hasColorTransform() const;
244
Dan Stozac6998d22015-09-24 17:03:36 -0700245 std::string dump() const;
246
247 private:
248 class Config {
249 public:
Dan Stoza076ac672016-03-14 10:47:53 -0700250 Config(Display& display)
Dan Stozac6998d22015-09-24 17:03:36 -0700251 : mDisplay(display),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -0700252 mId(0),
Dan Stozafc4e2022016-02-23 11:43:19 -0800253 mAttributes() {}
Dan Stozac6998d22015-09-24 17:03:36 -0700254
255 bool isOnDisplay(const Display& display) const {
256 return display.getId() == mDisplay.getId();
257 }
258
Dan Stozac6998d22015-09-24 17:03:36 -0700259 void setAttribute(HWC2::Attribute attribute, int32_t value);
260 int32_t getAttribute(HWC2::Attribute attribute) const;
261
Dan Stoza076ac672016-03-14 10:47:53 -0700262 void setHwc1Id(uint32_t id);
263 bool hasHwc1Id(uint32_t id) const;
Michael Wright28f24d02016-07-12 13:30:53 -0700264 HWC2::Error getColorModeForHwc1Id(uint32_t id,
265 android_color_mode_t *outMode) const;
266 HWC2::Error getHwc1IdForColorMode(android_color_mode_t mode,
Dan Stoza076ac672016-03-14 10:47:53 -0700267 uint32_t* outId) const;
268
269 void setId(hwc2_config_t id) { mId = id; }
270 hwc2_config_t getId() const { return mId; }
271
272 // Attempts to merge two configs that differ only in color
273 // mode. Returns whether the merge was successful
274 bool merge(const Config& other);
275
Michael Wright28f24d02016-07-12 13:30:53 -0700276 std::set<android_color_mode_t> getColorModes() const;
Dan Stoza076ac672016-03-14 10:47:53 -0700277
278 // splitLine divides the output into two lines suitable for
279 // dumpsys SurfaceFlinger
280 std::string toString(bool splitLine = false) const;
Dan Stozac6998d22015-09-24 17:03:36 -0700281
282 private:
283 Display& mDisplay;
Dan Stoza076ac672016-03-14 10:47:53 -0700284 hwc2_config_t mId;
Dan Stozac6998d22015-09-24 17:03:36 -0700285 std::unordered_map<HWC2::Attribute, int32_t> mAttributes;
Dan Stoza076ac672016-03-14 10:47:53 -0700286
287 // Maps from color transform to HWC1 config ID
Michael Wright28f24d02016-07-12 13:30:53 -0700288 std::unordered_map<android_color_mode_t, uint32_t> mHwc1Ids;
Dan Stozac6998d22015-09-24 17:03:36 -0700289 };
290
291 class Changes {
292 public:
293 uint32_t getNumTypes() const {
294 return static_cast<uint32_t>(mTypeChanges.size());
295 }
296
297 uint32_t getNumLayerRequests() const {
298 return static_cast<uint32_t>(mLayerRequests.size());
299 }
300
301 const std::unordered_map<hwc2_layer_t, HWC2::Composition>&
302 getTypeChanges() const {
303 return mTypeChanges;
304 }
305
306 const std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>&
307 getLayerRequests() const {
308 return mLayerRequests;
309 }
310
Dan Stozac6998d22015-09-24 17:03:36 -0700311 void addTypeChange(hwc2_layer_t layerId,
312 HWC2::Composition type) {
313 mTypeChanges.insert({layerId, type});
314 }
315
316 void clearTypeChanges() { mTypeChanges.clear(); }
317
318 void addLayerRequest(hwc2_layer_t layerId,
319 HWC2::LayerRequest request) {
320 mLayerRequests.insert({layerId, request});
321 }
322
323 private:
324 std::unordered_map<hwc2_layer_t, HWC2::Composition>
325 mTypeChanges;
326 std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>
327 mLayerRequests;
Dan Stozac6998d22015-09-24 17:03:36 -0700328 };
329
330 std::shared_ptr<const Config>
331 getConfig(hwc2_config_t configId) const;
332
Dan Stoza076ac672016-03-14 10:47:53 -0700333 void populateColorModes();
334 void initializeActiveConfig();
335
Dan Stozac6998d22015-09-24 17:03:36 -0700336 void reallocateHwc1Contents();
337 void assignHwc1LayerIds();
338
339 void updateTypeChanges(const struct hwc_layer_1& hwc1Layer,
340 const Layer& layer);
341 void updateLayerRequests(const struct hwc_layer_1& hwc1Layer,
342 const Layer& layer);
343
344 void prepareFramebufferTarget();
345
346 static std::atomic<hwc2_display_t> sNextId;
347 const hwc2_display_t mId;
348 HWC2On1Adapter& mDevice;
349
350 std::atomic<size_t> mDirtyCount;
351
352 // The state of this display should only be modified from
353 // SurfaceFlinger's main loop, with the exception of when dump is
354 // called. To prevent a bad state from crashing us during a dump
355 // call, all public calls into Display must acquire this mutex.
356 //
357 // It is recursive because we don't want to deadlock in validate
358 // (or present) when we call HWC2On1Adapter::prepareAllDisplays
359 // (or setAllDisplays), which calls back into Display functions
360 // which require locking.
361 mutable std::recursive_mutex mStateMutex;
362
363 bool mZIsDirty;
364 HWC1Contents mHwc1RequestedContents;
365 HWC1Contents mHwc1ReceivedContents;
366 DeferredFence mRetireFence;
367
368 // Will only be non-null after the layer has been validated but
369 // before it has been presented
370 std::unique_ptr<Changes> mChanges;
371
372 int32_t mHwc1Id;
Dan Stoza076ac672016-03-14 10:47:53 -0700373
Dan Stozac6998d22015-09-24 17:03:36 -0700374 std::vector<std::shared_ptr<Config>> mConfigs;
375 std::shared_ptr<const Config> mActiveConfig;
Michael Wright28f24d02016-07-12 13:30:53 -0700376 std::set<android_color_mode_t> mColorModes;
377 android_color_mode_t mActiveColorMode;
Dan Stozac6998d22015-09-24 17:03:36 -0700378 std::string mName;
379 HWC2::DisplayType mType;
380 HWC2::PowerMode mPowerMode;
381 HWC2::Vsync mVsyncEnabled;
382
383 FencedBuffer mClientTarget;
384 FencedBuffer mOutputBuffer;
385
Dan Stoza5df2a862016-03-24 16:19:37 -0700386 bool mHasColorTransform;
387
Dan Stozac6998d22015-09-24 17:03:36 -0700388 std::multiset<std::shared_ptr<Layer>, SortLayersByZ> mLayers;
389 std::unordered_map<size_t, std::shared_ptr<Layer>> mHwc1LayerMap;
390 };
391
392 template <typename ...Args>
393 static int32_t callDisplayFunction(hwc2_device_t* device,
394 hwc2_display_t displayId, HWC2::Error (Display::*member)(Args...),
395 Args... args) {
396 auto display = getAdapter(device)->getDisplay(displayId);
397 if (!display) {
398 return static_cast<int32_t>(HWC2::Error::BadDisplay);
399 }
400 auto error = ((*display).*member)(std::forward<Args>(args)...);
401 return static_cast<int32_t>(error);
402 }
403
404 template <typename MF, MF memFunc, typename ...Args>
405 static int32_t displayHook(hwc2_device_t* device, hwc2_display_t displayId,
406 Args... args) {
407 return HWC2On1Adapter::callDisplayFunction(device, displayId, memFunc,
408 std::forward<Args>(args)...);
409 }
410
411 static int32_t getDisplayAttributeHook(hwc2_device_t* device,
412 hwc2_display_t display, hwc2_config_t config,
413 int32_t intAttribute, int32_t* outValue) {
414 auto attribute = static_cast<HWC2::Attribute>(intAttribute);
415 return callDisplayFunction(device, display, &Display::getAttribute,
416 config, attribute, outValue);
417 }
418
Dan Stoza5df2a862016-03-24 16:19:37 -0700419 static int32_t setColorTransformHook(hwc2_device_t* device,
420 hwc2_display_t display, const float* /*matrix*/,
421 int32_t /*android_color_transform_t*/ intHint) {
422 // We intentionally throw away the matrix, because if the hint is
423 // anything other than IDENTITY, we have to fall back to client
424 // composition anyway
425 auto hint = static_cast<android_color_transform_t>(intHint);
426 return callDisplayFunction(device, display, &Display::setColorTransform,
427 hint);
428 }
429
Michael Wright28f24d02016-07-12 13:30:53 -0700430 static int32_t setColorModeHook(hwc2_device_t* device,
431 hwc2_display_t display, int32_t /*android_color_mode_t*/ intMode) {
432 auto mode = static_cast<android_color_mode_t>(intMode);
433 return callDisplayFunction(device, display, &Display::setColorMode, mode);
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:
Chih-Hung Hsieh342b7602016-09-01 11:34:16 -0700492 explicit Layer(Display& display);
Dan Stozac6998d22015-09-24 17:03:36 -0700493
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;
Fabien Sanglardeb3db612016-11-18 16:12:31 -0800572 bool mHasUnsupportedBackgroundColor;
Dan Stozac6998d22015-09-24 17:03:36 -0700573 };
574
575 template <typename ...Args>
576 static int32_t callLayerFunction(hwc2_device_t* device,
577 hwc2_display_t displayId, hwc2_layer_t layerId,
578 HWC2::Error (Layer::*member)(Args...), Args... args) {
579 auto result = getAdapter(device)->getLayer(displayId, layerId);
580 auto error = std::get<HWC2::Error>(result);
581 if (error == HWC2::Error::None) {
582 auto layer = std::get<Layer*>(result);
583 error = ((*layer).*member)(std::forward<Args>(args)...);
584 }
585 return static_cast<int32_t>(error);
586 }
587
588 template <typename MF, MF memFunc, typename ...Args>
589 static int32_t layerHook(hwc2_device_t* device, hwc2_display_t displayId,
590 hwc2_layer_t layerId, Args... args) {
591 return HWC2On1Adapter::callLayerFunction(device, displayId, layerId,
592 memFunc, std::forward<Args>(args)...);
593 }
594
595 // Layer state functions
596
597 static int32_t setLayerBlendModeHook(hwc2_device_t* device,
598 hwc2_display_t display, hwc2_layer_t layer, int32_t intMode) {
599 auto mode = static_cast<HWC2::BlendMode>(intMode);
600 return callLayerFunction(device, display, layer,
601 &Layer::setBlendMode, mode);
602 }
603
604 static int32_t setLayerCompositionTypeHook(hwc2_device_t* device,
605 hwc2_display_t display, hwc2_layer_t layer, int32_t intType) {
606 auto type = static_cast<HWC2::Composition>(intType);
607 return callLayerFunction(device, display, layer,
608 &Layer::setCompositionType, type);
609 }
610
Dan Stoza5df2a862016-03-24 16:19:37 -0700611 static int32_t setLayerDataspaceHook(hwc2_device_t* device,
612 hwc2_display_t display, hwc2_layer_t layer, int32_t intDataspace) {
613 auto dataspace = static_cast<android_dataspace_t>(intDataspace);
614 return callLayerFunction(device, display, layer, &Layer::setDataspace,
615 dataspace);
616 }
617
Dan Stozac6998d22015-09-24 17:03:36 -0700618 static int32_t setLayerTransformHook(hwc2_device_t* device,
619 hwc2_display_t display, hwc2_layer_t layer, int32_t intTransform) {
620 auto transform = static_cast<HWC2::Transform>(intTransform);
621 return callLayerFunction(device, display, layer, &Layer::setTransform,
622 transform);
623 }
624
625 static int32_t setLayerZOrderHook(hwc2_device_t* device,
626 hwc2_display_t display, hwc2_layer_t layer, uint32_t z) {
627 return callDisplayFunction(device, display, &Display::updateLayerZ,
628 layer, z);
629 }
630
631 // Adapter internals
632
633 void populateCapabilities();
634 Display* getDisplay(hwc2_display_t id);
635 std::tuple<Layer*, HWC2::Error> getLayer(hwc2_display_t displayId,
636 hwc2_layer_t layerId);
637 void populatePrimary();
638
639 bool prepareAllDisplays();
640 std::vector<struct hwc_display_contents_1*> mHwc1Contents;
641 HWC2::Error setAllDisplays();
642
643 void hwc1Invalidate();
644 void hwc1Vsync(int hwc1DisplayId, int64_t timestamp);
645 void hwc1Hotplug(int hwc1DisplayId, int connected);
646
647 // These are set in the constructor and before any asynchronous events are
648 // possible
649
650 struct hwc_composer_device_1* const mHwc1Device;
651 const uint8_t mHwc1MinorVersion;
652 bool mHwc1SupportsVirtualDisplays;
Fabien Sanglardeb3db612016-11-18 16:12:31 -0800653 bool mHwc1SupportsBackgroundColor;
Dan Stozac6998d22015-09-24 17:03:36 -0700654
655 class Callbacks;
656 const std::unique_ptr<Callbacks> mHwc1Callbacks;
657
658 std::unordered_set<HWC2::Capability> mCapabilities;
659
660 // These are only accessed from the main SurfaceFlinger thread (not from
661 // callbacks or dump
662
663 std::map<hwc2_layer_t, std::shared_ptr<Layer>> mLayers;
664 std::shared_ptr<Display> mHwc1VirtualDisplay;
665
666 // These are potentially accessed from multiple threads, and are protected
Dan Stozafc4e2022016-02-23 11:43:19 -0800667 // by this mutex. This needs to be recursive, since the HWC1 implementation
668 // can call back into the invalidate callback on the same thread that is
669 // calling prepare.
670 std::recursive_timed_mutex mStateMutex;
Dan Stozac6998d22015-09-24 17:03:36 -0700671
672 struct CallbackInfo {
673 hwc2_callback_data_t data;
674 hwc2_function_pointer_t pointer;
675 };
676 std::unordered_map<HWC2::Callback, CallbackInfo> mCallbacks;
677 bool mHasPendingInvalidate;
678 std::vector<std::pair<int, int64_t>> mPendingVsyncs;
679 std::vector<std::pair<int, int>> mPendingHotplugs;
680
681 std::map<hwc2_display_t, std::shared_ptr<Display>> mDisplays;
682 std::unordered_map<int, hwc2_display_t> mHwc1DisplayMap;
683};
684
685} // namespace android
686
687#endif