blob: 9abdc38ff2de2ff70f1d075fbf14b34ca55d5dec [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
Fabien Sanglard33960702016-11-29 17:58:21 -080043// For devices unable to provide an implementation of HWC2 (see hwcomposer2.h),
44// we provide an adapter able to talk to HWC1 (see hwcomposer.h). It translates
45// streamed function calls ala HWC2 model to batched array of structs calls ala
46// HWC1 model.
Dan Stozac6998d22015-09-24 17:03:36 -070047class HWC2On1Adapter : public hwc2_device_t
48{
49public:
Chih-Hung Hsieh342b7602016-09-01 11:34:16 -070050 explicit HWC2On1Adapter(struct hwc_composer_device_1* hwc1Device);
Dan Stozac6998d22015-09-24 17:03:36 -070051 ~HWC2On1Adapter();
52
53 struct hwc_composer_device_1* getHwc1Device() const { return mHwc1Device; }
54 uint8_t getHwc1MinorVersion() const { return mHwc1MinorVersion; }
55
56private:
57 static inline HWC2On1Adapter* getAdapter(hwc2_device_t* device) {
58 return static_cast<HWC2On1Adapter*>(device);
59 }
60
61 // getCapabilities
62
63 void doGetCapabilities(uint32_t* outCount,
64 int32_t* /*hwc2_capability_t*/ outCapabilities);
65 static void getCapabilitiesHook(hwc2_device_t* device, uint32_t* outCount,
66 int32_t* /*hwc2_capability_t*/ outCapabilities) {
67 getAdapter(device)->doGetCapabilities(outCount, outCapabilities);
68 }
69
Fabien Sanglardeb3db612016-11-18 16:12:31 -080070 bool supportsBackgroundColor() {
71 return mHwc1SupportsBackgroundColor;
72 }
73
Dan Stozac6998d22015-09-24 17:03:36 -070074 // getFunction
75
76 hwc2_function_pointer_t doGetFunction(HWC2::FunctionDescriptor descriptor);
77 static hwc2_function_pointer_t getFunctionHook(hwc2_device_t* device,
78 int32_t intDesc) {
79 auto descriptor = static_cast<HWC2::FunctionDescriptor>(intDesc);
80 return getAdapter(device)->doGetFunction(descriptor);
81 }
82
83 // Device functions
84
85 HWC2::Error createVirtualDisplay(uint32_t width, uint32_t height,
86 hwc2_display_t* outDisplay);
87 static int32_t createVirtualDisplayHook(hwc2_device_t* device,
Dan Stoza5cf424b2016-05-20 14:02:39 -070088 uint32_t width, uint32_t height, int32_t* /*format*/,
89 hwc2_display_t* outDisplay) {
90 // HWC1 implementations cannot override the buffer format requested by
91 // the consumer
Dan Stozac6998d22015-09-24 17:03:36 -070092 auto error = getAdapter(device)->createVirtualDisplay(width, height,
93 outDisplay);
94 return static_cast<int32_t>(error);
95 }
96
97 HWC2::Error destroyVirtualDisplay(hwc2_display_t display);
98 static int32_t destroyVirtualDisplayHook(hwc2_device_t* device,
99 hwc2_display_t display) {
100 auto error = getAdapter(device)->destroyVirtualDisplay(display);
101 return static_cast<int32_t>(error);
102 }
103
104 std::string mDumpString;
105 void dump(uint32_t* outSize, char* outBuffer);
106 static void dumpHook(hwc2_device_t* device, uint32_t* outSize,
107 char* outBuffer) {
108 getAdapter(device)->dump(outSize, outBuffer);
109 }
110
111 uint32_t getMaxVirtualDisplayCount();
112 static uint32_t getMaxVirtualDisplayCountHook(hwc2_device_t* device) {
113 return getAdapter(device)->getMaxVirtualDisplayCount();
114 }
115
116 HWC2::Error registerCallback(HWC2::Callback descriptor,
117 hwc2_callback_data_t callbackData, hwc2_function_pointer_t pointer);
118 static int32_t registerCallbackHook(hwc2_device_t* device,
119 int32_t intDesc, hwc2_callback_data_t callbackData,
120 hwc2_function_pointer_t pointer) {
121 auto descriptor = static_cast<HWC2::Callback>(intDesc);
122 auto error = getAdapter(device)->registerCallback(descriptor,
123 callbackData, pointer);
124 return static_cast<int32_t>(error);
125 }
126
127 // Display functions
128
129 class Layer;
130
131 class SortLayersByZ {
132 public:
133 bool operator()(const std::shared_ptr<Layer>& lhs,
134 const std::shared_ptr<Layer>& rhs);
135 };
136
137 class DisplayContentsDeleter {
138 public:
139 void operator()(struct hwc_display_contents_1* contents);
140 };
141
Fabien Sanglard33960702016-11-29 17:58:21 -0800142 // The semantics of the fences returned by the device differ between
143 // hwc1.set() and hwc2.present(). Read hwcomposer.h and hwcomposer2.h
144 // for more information.
145 //
146 // Release fences in hwc1 are obtained on set() for a frame n and signaled
147 // when the layer buffer is not needed for read operations anymore
148 // (typically on frame n+1). In HWC2, release fences are obtained with a
149 // special call after present() for frame n. These fences signal
150 // on frame n: More specifically, the fence for a given buffer provided in
151 // frame n will signal when the prior buffer is no longer required.
152 //
153 // A retire fence (HWC1) is signaled when a composition is replaced
154 // on the panel whereas a present fence (HWC2) is signaled when a
155 // composition starts to be displayed on a panel.
156 //
157 // The HWC2to1Adapter emulates the new fence semantics for a frame
158 // n by returning the fence from frame n-1. For frame 0, the adapter
159 // returns NO_FENCE.
Dan Stozac6998d22015-09-24 17:03:36 -0700160 class DeferredFence {
161 public:
162 DeferredFence()
Fabien Sanglard27efbdb2016-11-29 11:14:54 -0800163 : mFences({Fence::NO_FENCE, Fence::NO_FENCE}) {}
Dan Stozac6998d22015-09-24 17:03:36 -0700164
165 void add(int32_t fenceFd) {
166 mFences.emplace(new Fence(fenceFd));
167 mFences.pop();
168 }
169
170 const sp<Fence>& get() const {
171 return mFences.front();
172 }
173
174 private:
Fabien Sanglard33960702016-11-29 17:58:21 -0800175 // There are always two fences in this queue.
Dan Stozac6998d22015-09-24 17:03:36 -0700176 std::queue<sp<Fence>> mFences;
177 };
178
179 class FencedBuffer {
180 public:
181 FencedBuffer() : mBuffer(nullptr), mFence(Fence::NO_FENCE) {}
182
183 void setBuffer(buffer_handle_t buffer) { mBuffer = buffer; }
184 void setFence(int fenceFd) { mFence = new Fence(fenceFd); }
185
186 buffer_handle_t getBuffer() const { return mBuffer; }
187 int getFence() const { return mFence->dup(); }
188
189 private:
190 buffer_handle_t mBuffer;
191 sp<Fence> mFence;
192 };
193
194 class Display {
195 public:
196 typedef std::unique_ptr<hwc_display_contents_1,
197 DisplayContentsDeleter> HWC1Contents;
198
199 Display(HWC2On1Adapter& device, HWC2::DisplayType type);
200
201 hwc2_display_t getId() const { return mId; }
202 HWC2On1Adapter& getDevice() const { return mDevice; }
203
204 // Does not require locking because it is set before adding the
205 // Displays to the Adapter's list of displays
206 void setHwc1Id(int32_t id) { mHwc1Id = id; }
207 int32_t getHwc1Id() const { return mHwc1Id; }
208
209 void incDirty() { ++mDirtyCount; }
210 void decDirty() { --mDirtyCount; }
211 bool isDirty() const { return mDirtyCount > 0 || mZIsDirty; }
212
213 // HWC2 Display functions
214 HWC2::Error acceptChanges();
215 HWC2::Error createLayer(hwc2_layer_t* outLayerId);
216 HWC2::Error destroyLayer(hwc2_layer_t layerId);
217 HWC2::Error getActiveConfig(hwc2_config_t* outConfigId);
218 HWC2::Error getAttribute(hwc2_config_t configId,
219 HWC2::Attribute attribute, int32_t* outValue);
220 HWC2::Error getChangedCompositionTypes(uint32_t* outNumElements,
221 hwc2_layer_t* outLayers, int32_t* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700222 HWC2::Error getColorModes(uint32_t* outNumModes, int32_t* outModes);
Dan Stozac6998d22015-09-24 17:03:36 -0700223 HWC2::Error getConfigs(uint32_t* outNumConfigs,
224 hwc2_config_t* outConfigIds);
225 HWC2::Error getDozeSupport(int32_t* outSupport);
Dan Stozaed40eba2016-03-16 12:33:52 -0700226 HWC2::Error getHdrCapabilities(uint32_t* outNumTypes,
227 int32_t* outTypes, float* outMaxLuminance,
228 float* outMaxAverageLuminance, float* outMinLuminance);
Dan Stozac6998d22015-09-24 17:03:36 -0700229 HWC2::Error getName(uint32_t* outSize, char* outName);
230 HWC2::Error getReleaseFences(uint32_t* outNumElements,
231 hwc2_layer_t* outLayers, int32_t* outFences);
232 HWC2::Error getRequests(int32_t* outDisplayRequests,
233 uint32_t* outNumElements, hwc2_layer_t* outLayers,
234 int32_t* outLayerRequests);
235 HWC2::Error getType(int32_t* outType);
236 HWC2::Error present(int32_t* outRetireFence);
237 HWC2::Error setActiveConfig(hwc2_config_t configId);
238 HWC2::Error setClientTarget(buffer_handle_t target,
Dan Stoza5cf424b2016-05-20 14:02:39 -0700239 int32_t acquireFence, int32_t dataspace,
240 hwc_region_t damage);
Michael Wright28f24d02016-07-12 13:30:53 -0700241 HWC2::Error setColorMode(android_color_mode_t mode);
Dan Stoza5df2a862016-03-24 16:19:37 -0700242 HWC2::Error setColorTransform(android_color_transform_t hint);
Dan Stozac6998d22015-09-24 17:03:36 -0700243 HWC2::Error setOutputBuffer(buffer_handle_t buffer,
244 int32_t releaseFence);
245 HWC2::Error setPowerMode(HWC2::PowerMode mode);
246 HWC2::Error setVsyncEnabled(HWC2::Vsync enabled);
247 HWC2::Error validate(uint32_t* outNumTypes,
248 uint32_t* outNumRequests);
249
250 HWC2::Error updateLayerZ(hwc2_layer_t layerId, uint32_t z);
251
252 // Read configs from HWC1 device
253 void populateConfigs();
254
255 // Set configs for a virtual display
256 void populateConfigs(uint32_t width, uint32_t height);
257
258 bool prepare();
259 HWC1Contents cloneRequestedContents() const;
Fabien Sanglard33960702016-11-29 17:58:21 -0800260
261 // Called after hwc.prepare() with responses from the device.
Dan Stozac6998d22015-09-24 17:03:36 -0700262 void setReceivedContents(HWC1Contents contents);
Fabien Sanglard33960702016-11-29 17:58:21 -0800263
Dan Stozac6998d22015-09-24 17:03:36 -0700264 bool hasChanges() const;
265 HWC2::Error set(hwc_display_contents_1& hwcContents);
266 void addRetireFence(int fenceFd);
267 void addReleaseFences(const hwc_display_contents_1& hwcContents);
268
Dan Stoza5df2a862016-03-24 16:19:37 -0700269 bool hasColorTransform() const;
270
Dan Stozac6998d22015-09-24 17:03:36 -0700271 std::string dump() const;
272
273 private:
274 class Config {
275 public:
Dan Stoza076ac672016-03-14 10:47:53 -0700276 Config(Display& display)
Dan Stozac6998d22015-09-24 17:03:36 -0700277 : mDisplay(display),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -0700278 mId(0),
Dan Stozafc4e2022016-02-23 11:43:19 -0800279 mAttributes() {}
Dan Stozac6998d22015-09-24 17:03:36 -0700280
281 bool isOnDisplay(const Display& display) const {
282 return display.getId() == mDisplay.getId();
283 }
284
Dan Stozac6998d22015-09-24 17:03:36 -0700285 void setAttribute(HWC2::Attribute attribute, int32_t value);
286 int32_t getAttribute(HWC2::Attribute attribute) const;
287
Dan Stoza076ac672016-03-14 10:47:53 -0700288 void setHwc1Id(uint32_t id);
289 bool hasHwc1Id(uint32_t id) const;
Michael Wright28f24d02016-07-12 13:30:53 -0700290 HWC2::Error getColorModeForHwc1Id(uint32_t id,
291 android_color_mode_t *outMode) const;
292 HWC2::Error getHwc1IdForColorMode(android_color_mode_t mode,
Dan Stoza076ac672016-03-14 10:47:53 -0700293 uint32_t* outId) const;
294
295 void setId(hwc2_config_t id) { mId = id; }
296 hwc2_config_t getId() const { return mId; }
297
298 // Attempts to merge two configs that differ only in color
299 // mode. Returns whether the merge was successful
300 bool merge(const Config& other);
301
Michael Wright28f24d02016-07-12 13:30:53 -0700302 std::set<android_color_mode_t> getColorModes() const;
Dan Stoza076ac672016-03-14 10:47:53 -0700303
304 // splitLine divides the output into two lines suitable for
305 // dumpsys SurfaceFlinger
306 std::string toString(bool splitLine = false) const;
Dan Stozac6998d22015-09-24 17:03:36 -0700307
308 private:
309 Display& mDisplay;
Dan Stoza076ac672016-03-14 10:47:53 -0700310 hwc2_config_t mId;
Dan Stozac6998d22015-09-24 17:03:36 -0700311 std::unordered_map<HWC2::Attribute, int32_t> mAttributes;
Dan Stoza076ac672016-03-14 10:47:53 -0700312
313 // Maps from color transform to HWC1 config ID
Michael Wright28f24d02016-07-12 13:30:53 -0700314 std::unordered_map<android_color_mode_t, uint32_t> mHwc1Ids;
Dan Stozac6998d22015-09-24 17:03:36 -0700315 };
316
Fabien Sanglard33960702016-11-29 17:58:21 -0800317 // Store changes requested from the device upon calling prepare().
318 // Handles change request to:
319 // - Layer composition type.
320 // - Layer hints.
Dan Stozac6998d22015-09-24 17:03:36 -0700321 class Changes {
322 public:
323 uint32_t getNumTypes() const {
324 return static_cast<uint32_t>(mTypeChanges.size());
325 }
326
327 uint32_t getNumLayerRequests() const {
328 return static_cast<uint32_t>(mLayerRequests.size());
329 }
330
331 const std::unordered_map<hwc2_layer_t, HWC2::Composition>&
332 getTypeChanges() const {
333 return mTypeChanges;
334 }
335
336 const std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>&
337 getLayerRequests() const {
338 return mLayerRequests;
339 }
340
Dan Stozac6998d22015-09-24 17:03:36 -0700341 void addTypeChange(hwc2_layer_t layerId,
342 HWC2::Composition type) {
343 mTypeChanges.insert({layerId, type});
344 }
345
346 void clearTypeChanges() { mTypeChanges.clear(); }
347
348 void addLayerRequest(hwc2_layer_t layerId,
349 HWC2::LayerRequest request) {
350 mLayerRequests.insert({layerId, request});
351 }
352
353 private:
354 std::unordered_map<hwc2_layer_t, HWC2::Composition>
355 mTypeChanges;
356 std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>
357 mLayerRequests;
Dan Stozac6998d22015-09-24 17:03:36 -0700358 };
359
360 std::shared_ptr<const Config>
361 getConfig(hwc2_config_t configId) const;
362
Dan Stoza076ac672016-03-14 10:47:53 -0700363 void populateColorModes();
364 void initializeActiveConfig();
365
Dan Stozac6998d22015-09-24 17:03:36 -0700366 void reallocateHwc1Contents();
367 void assignHwc1LayerIds();
368
Fabien Sanglard33960702016-11-29 17:58:21 -0800369 // Called after a response to prepare() has been received:
370 // Ingest composition type changes requested by the device.
Dan Stozac6998d22015-09-24 17:03:36 -0700371 void updateTypeChanges(const struct hwc_layer_1& hwc1Layer,
372 const Layer& layer);
Fabien Sanglard33960702016-11-29 17:58:21 -0800373
374 // Called after a response to prepare() has been received:
375 // Ingest layer hint changes requested by the device.
Dan Stozac6998d22015-09-24 17:03:36 -0700376 void updateLayerRequests(const struct hwc_layer_1& hwc1Layer,
377 const Layer& layer);
378
379 void prepareFramebufferTarget();
380
381 static std::atomic<hwc2_display_t> sNextId;
382 const hwc2_display_t mId;
383 HWC2On1Adapter& mDevice;
384
385 std::atomic<size_t> mDirtyCount;
386
387 // The state of this display should only be modified from
388 // SurfaceFlinger's main loop, with the exception of when dump is
389 // called. To prevent a bad state from crashing us during a dump
390 // call, all public calls into Display must acquire this mutex.
391 //
392 // It is recursive because we don't want to deadlock in validate
393 // (or present) when we call HWC2On1Adapter::prepareAllDisplays
394 // (or setAllDisplays), which calls back into Display functions
395 // which require locking.
396 mutable std::recursive_mutex mStateMutex;
397
398 bool mZIsDirty;
Fabien Sanglard33960702016-11-29 17:58:21 -0800399
400 // Array of structs exchanged between client and hwc1 device.
401 HWC1Contents mHwc1RequestedContents; // Sent to device upon calling prepare().
402 HWC1Contents mHwc1ReceivedContents; // Returned by device after prepare().
403
Dan Stozac6998d22015-09-24 17:03:36 -0700404 DeferredFence mRetireFence;
405
406 // Will only be non-null after the layer has been validated but
407 // before it has been presented
408 std::unique_ptr<Changes> mChanges;
409
410 int32_t mHwc1Id;
Dan Stoza076ac672016-03-14 10:47:53 -0700411
Dan Stozac6998d22015-09-24 17:03:36 -0700412 std::vector<std::shared_ptr<Config>> mConfigs;
413 std::shared_ptr<const Config> mActiveConfig;
Michael Wright28f24d02016-07-12 13:30:53 -0700414 std::set<android_color_mode_t> mColorModes;
415 android_color_mode_t mActiveColorMode;
Dan Stozac6998d22015-09-24 17:03:36 -0700416 std::string mName;
417 HWC2::DisplayType mType;
418 HWC2::PowerMode mPowerMode;
419 HWC2::Vsync mVsyncEnabled;
420
421 FencedBuffer mClientTarget;
422 FencedBuffer mOutputBuffer;
423
Dan Stoza5df2a862016-03-24 16:19:37 -0700424 bool mHasColorTransform;
425
Dan Stozac6998d22015-09-24 17:03:36 -0700426 std::multiset<std::shared_ptr<Layer>, SortLayersByZ> mLayers;
427 std::unordered_map<size_t, std::shared_ptr<Layer>> mHwc1LayerMap;
428 };
429
430 template <typename ...Args>
431 static int32_t callDisplayFunction(hwc2_device_t* device,
432 hwc2_display_t displayId, HWC2::Error (Display::*member)(Args...),
433 Args... args) {
434 auto display = getAdapter(device)->getDisplay(displayId);
435 if (!display) {
436 return static_cast<int32_t>(HWC2::Error::BadDisplay);
437 }
438 auto error = ((*display).*member)(std::forward<Args>(args)...);
439 return static_cast<int32_t>(error);
440 }
441
442 template <typename MF, MF memFunc, typename ...Args>
443 static int32_t displayHook(hwc2_device_t* device, hwc2_display_t displayId,
444 Args... args) {
445 return HWC2On1Adapter::callDisplayFunction(device, displayId, memFunc,
446 std::forward<Args>(args)...);
447 }
448
449 static int32_t getDisplayAttributeHook(hwc2_device_t* device,
450 hwc2_display_t display, hwc2_config_t config,
451 int32_t intAttribute, int32_t* outValue) {
452 auto attribute = static_cast<HWC2::Attribute>(intAttribute);
453 return callDisplayFunction(device, display, &Display::getAttribute,
454 config, attribute, outValue);
455 }
456
Dan Stoza5df2a862016-03-24 16:19:37 -0700457 static int32_t setColorTransformHook(hwc2_device_t* device,
458 hwc2_display_t display, const float* /*matrix*/,
459 int32_t /*android_color_transform_t*/ intHint) {
460 // We intentionally throw away the matrix, because if the hint is
461 // anything other than IDENTITY, we have to fall back to client
462 // composition anyway
463 auto hint = static_cast<android_color_transform_t>(intHint);
464 return callDisplayFunction(device, display, &Display::setColorTransform,
465 hint);
466 }
467
Michael Wright28f24d02016-07-12 13:30:53 -0700468 static int32_t setColorModeHook(hwc2_device_t* device,
469 hwc2_display_t display, int32_t /*android_color_mode_t*/ intMode) {
470 auto mode = static_cast<android_color_mode_t>(intMode);
471 return callDisplayFunction(device, display, &Display::setColorMode, mode);
472 }
473
Dan Stozac6998d22015-09-24 17:03:36 -0700474 static int32_t setPowerModeHook(hwc2_device_t* device,
475 hwc2_display_t display, int32_t intMode) {
476 auto mode = static_cast<HWC2::PowerMode>(intMode);
477 return callDisplayFunction(device, display, &Display::setPowerMode,
478 mode);
479 }
480
481 static int32_t setVsyncEnabledHook(hwc2_device_t* device,
482 hwc2_display_t display, int32_t intEnabled) {
483 auto enabled = static_cast<HWC2::Vsync>(intEnabled);
484 return callDisplayFunction(device, display, &Display::setVsyncEnabled,
485 enabled);
486 }
487
488 // Layer functions
489
490 template <typename T>
491 class LatchedState {
492 public:
493 LatchedState(Layer& parent, T initialValue)
494 : mParent(parent),
495 mPendingValue(initialValue),
496 mValue(initialValue) {}
497
498 void setPending(T value) {
499 if (value == mPendingValue) {
500 return;
501 }
502 if (mPendingValue == mValue) {
503 mParent.incDirty();
504 } else if (value == mValue) {
505 mParent.decDirty();
506 }
507 mPendingValue = value;
508 }
509
510 T getValue() const { return mValue; }
511 T getPendingValue() const { return mPendingValue; }
512
513 bool isDirty() const { return mPendingValue != mValue; }
514
515 void latch() {
516 if (isDirty()) {
517 mValue = mPendingValue;
518 mParent.decDirty();
519 }
520 }
521
522 private:
523 Layer& mParent;
524 T mPendingValue;
525 T mValue;
526 };
527
528 class Layer {
529 public:
Chih-Hung Hsieh342b7602016-09-01 11:34:16 -0700530 explicit Layer(Display& display);
Dan Stozac6998d22015-09-24 17:03:36 -0700531
532 bool operator==(const Layer& other) { return mId == other.mId; }
533 bool operator!=(const Layer& other) { return !(*this == other); }
534
535 hwc2_layer_t getId() const { return mId; }
536 Display& getDisplay() const { return mDisplay; }
537
538 void incDirty() { if (mDirtyCount++ == 0) mDisplay.incDirty(); }
539 void decDirty() { if (--mDirtyCount == 0) mDisplay.decDirty(); }
540 bool isDirty() const { return mDirtyCount > 0; }
541
542 // HWC2 Layer functions
543 HWC2::Error setBuffer(buffer_handle_t buffer, int32_t acquireFence);
544 HWC2::Error setCursorPosition(int32_t x, int32_t y);
545 HWC2::Error setSurfaceDamage(hwc_region_t damage);
546
547 // HWC2 Layer state functions
548 HWC2::Error setBlendMode(HWC2::BlendMode mode);
549 HWC2::Error setColor(hwc_color_t color);
550 HWC2::Error setCompositionType(HWC2::Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700551 HWC2::Error setDataspace(android_dataspace_t dataspace);
Dan Stozac6998d22015-09-24 17:03:36 -0700552 HWC2::Error setDisplayFrame(hwc_rect_t frame);
553 HWC2::Error setPlaneAlpha(float alpha);
554 HWC2::Error setSidebandStream(const native_handle_t* stream);
555 HWC2::Error setSourceCrop(hwc_frect_t crop);
556 HWC2::Error setTransform(HWC2::Transform transform);
557 HWC2::Error setVisibleRegion(hwc_region_t visible);
558 HWC2::Error setZ(uint32_t z);
559
560 HWC2::Composition getCompositionType() const {
561 return mCompositionType.getValue();
562 }
563 uint32_t getZ() const { return mZ; }
564
565 void addReleaseFence(int fenceFd);
566 const sp<Fence>& getReleaseFence() const;
567
568 void setHwc1Id(size_t id) { mHwc1Id = id; }
569 size_t getHwc1Id() const { return mHwc1Id; }
570
571 void applyState(struct hwc_layer_1& hwc1Layer, bool applyAllState);
572
573 std::string dump() const;
574
575 private:
576 void applyCommonState(struct hwc_layer_1& hwc1Layer,
577 bool applyAllState);
578 void applySolidColorState(struct hwc_layer_1& hwc1Layer,
579 bool applyAllState);
580 void applySidebandState(struct hwc_layer_1& hwc1Layer,
581 bool applyAllState);
582 void applyBufferState(struct hwc_layer_1& hwc1Layer);
583 void applyCompositionType(struct hwc_layer_1& hwc1Layer,
584 bool applyAllState);
585
586 static std::atomic<hwc2_layer_t> sNextId;
587 const hwc2_layer_t mId;
588 Display& mDisplay;
589 size_t mDirtyCount;
590
591 FencedBuffer mBuffer;
592 std::vector<hwc_rect_t> mSurfaceDamage;
593
594 LatchedState<HWC2::BlendMode> mBlendMode;
595 LatchedState<hwc_color_t> mColor;
596 LatchedState<HWC2::Composition> mCompositionType;
597 LatchedState<hwc_rect_t> mDisplayFrame;
598 LatchedState<float> mPlaneAlpha;
599 LatchedState<const native_handle_t*> mSidebandStream;
600 LatchedState<hwc_frect_t> mSourceCrop;
601 LatchedState<HWC2::Transform> mTransform;
602 LatchedState<std::vector<hwc_rect_t>> mVisibleRegion;
603 uint32_t mZ;
604
605 DeferredFence mReleaseFence;
606
607 size_t mHwc1Id;
Dan Stoza5df2a862016-03-24 16:19:37 -0700608 bool mHasUnsupportedDataspace;
Dan Stozac6998d22015-09-24 17:03:36 -0700609 bool mHasUnsupportedPlaneAlpha;
Fabien Sanglardeb3db612016-11-18 16:12:31 -0800610 bool mHasUnsupportedBackgroundColor;
Dan Stozac6998d22015-09-24 17:03:36 -0700611 };
612
613 template <typename ...Args>
614 static int32_t callLayerFunction(hwc2_device_t* device,
615 hwc2_display_t displayId, hwc2_layer_t layerId,
616 HWC2::Error (Layer::*member)(Args...), Args... args) {
617 auto result = getAdapter(device)->getLayer(displayId, layerId);
618 auto error = std::get<HWC2::Error>(result);
619 if (error == HWC2::Error::None) {
620 auto layer = std::get<Layer*>(result);
621 error = ((*layer).*member)(std::forward<Args>(args)...);
622 }
623 return static_cast<int32_t>(error);
624 }
625
626 template <typename MF, MF memFunc, typename ...Args>
627 static int32_t layerHook(hwc2_device_t* device, hwc2_display_t displayId,
628 hwc2_layer_t layerId, Args... args) {
629 return HWC2On1Adapter::callLayerFunction(device, displayId, layerId,
630 memFunc, std::forward<Args>(args)...);
631 }
632
633 // Layer state functions
634
635 static int32_t setLayerBlendModeHook(hwc2_device_t* device,
636 hwc2_display_t display, hwc2_layer_t layer, int32_t intMode) {
637 auto mode = static_cast<HWC2::BlendMode>(intMode);
638 return callLayerFunction(device, display, layer,
639 &Layer::setBlendMode, mode);
640 }
641
642 static int32_t setLayerCompositionTypeHook(hwc2_device_t* device,
643 hwc2_display_t display, hwc2_layer_t layer, int32_t intType) {
644 auto type = static_cast<HWC2::Composition>(intType);
645 return callLayerFunction(device, display, layer,
646 &Layer::setCompositionType, type);
647 }
648
Dan Stoza5df2a862016-03-24 16:19:37 -0700649 static int32_t setLayerDataspaceHook(hwc2_device_t* device,
650 hwc2_display_t display, hwc2_layer_t layer, int32_t intDataspace) {
651 auto dataspace = static_cast<android_dataspace_t>(intDataspace);
652 return callLayerFunction(device, display, layer, &Layer::setDataspace,
653 dataspace);
654 }
655
Dan Stozac6998d22015-09-24 17:03:36 -0700656 static int32_t setLayerTransformHook(hwc2_device_t* device,
657 hwc2_display_t display, hwc2_layer_t layer, int32_t intTransform) {
658 auto transform = static_cast<HWC2::Transform>(intTransform);
659 return callLayerFunction(device, display, layer, &Layer::setTransform,
660 transform);
661 }
662
663 static int32_t setLayerZOrderHook(hwc2_device_t* device,
664 hwc2_display_t display, hwc2_layer_t layer, uint32_t z) {
665 return callDisplayFunction(device, display, &Display::updateLayerZ,
666 layer, z);
667 }
668
669 // Adapter internals
670
671 void populateCapabilities();
672 Display* getDisplay(hwc2_display_t id);
673 std::tuple<Layer*, HWC2::Error> getLayer(hwc2_display_t displayId,
674 hwc2_layer_t layerId);
675 void populatePrimary();
676
677 bool prepareAllDisplays();
678 std::vector<struct hwc_display_contents_1*> mHwc1Contents;
679 HWC2::Error setAllDisplays();
680
681 void hwc1Invalidate();
682 void hwc1Vsync(int hwc1DisplayId, int64_t timestamp);
683 void hwc1Hotplug(int hwc1DisplayId, int connected);
684
685 // These are set in the constructor and before any asynchronous events are
686 // possible
687
688 struct hwc_composer_device_1* const mHwc1Device;
689 const uint8_t mHwc1MinorVersion;
690 bool mHwc1SupportsVirtualDisplays;
Fabien Sanglardeb3db612016-11-18 16:12:31 -0800691 bool mHwc1SupportsBackgroundColor;
Dan Stozac6998d22015-09-24 17:03:36 -0700692
693 class Callbacks;
694 const std::unique_ptr<Callbacks> mHwc1Callbacks;
695
696 std::unordered_set<HWC2::Capability> mCapabilities;
697
698 // These are only accessed from the main SurfaceFlinger thread (not from
699 // callbacks or dump
700
701 std::map<hwc2_layer_t, std::shared_ptr<Layer>> mLayers;
702 std::shared_ptr<Display> mHwc1VirtualDisplay;
703
704 // These are potentially accessed from multiple threads, and are protected
Dan Stozafc4e2022016-02-23 11:43:19 -0800705 // by this mutex. This needs to be recursive, since the HWC1 implementation
706 // can call back into the invalidate callback on the same thread that is
707 // calling prepare.
708 std::recursive_timed_mutex mStateMutex;
Dan Stozac6998d22015-09-24 17:03:36 -0700709
710 struct CallbackInfo {
711 hwc2_callback_data_t data;
712 hwc2_function_pointer_t pointer;
713 };
714 std::unordered_map<HWC2::Callback, CallbackInfo> mCallbacks;
715 bool mHasPendingInvalidate;
716 std::vector<std::pair<int, int64_t>> mPendingVsyncs;
717 std::vector<std::pair<int, int>> mPendingHotplugs;
718
719 std::map<hwc2_display_t, std::shared_ptr<Display>> mDisplays;
720 std::unordered_map<int, hwc2_display_t> mHwc1DisplayMap;
721};
722
723} // namespace android
724
725#endif