blob: bffeefe75d4c80c49cf04d472be2c9eb6e0d0d6c [file] [log] [blame]
Dan Stozac6998d22015-09-24 17:03:36 -07001/*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_SF_HWC2_ON_1_ADAPTER_H
18#define ANDROID_SF_HWC2_ON_1_ADAPTER_H
19
20#define HWC2_INCLUDE_STRINGIFICATION
21#define HWC2_USE_CPP11
22#include <hardware/hwcomposer2.h>
23#undef HWC2_INCLUDE_STRINGIFICATION
24#undef HWC2_USE_CPP11
25
26#include <ui/Fence.h>
27
28#include <atomic>
29#include <map>
30#include <mutex>
31#include <queue>
32#include <set>
33#include <unordered_map>
34#include <unordered_set>
35#include <vector>
36
37struct hwc_composer_device_1;
38struct hwc_display_contents_1;
39struct hwc_layer_1;
40
41namespace android {
42
43class HWC2On1Adapter : public hwc2_device_t
44{
45public:
46 HWC2On1Adapter(struct hwc_composer_device_1* hwc1Device);
47 ~HWC2On1Adapter();
48
49 struct hwc_composer_device_1* getHwc1Device() const { return mHwc1Device; }
50 uint8_t getHwc1MinorVersion() const { return mHwc1MinorVersion; }
51
52private:
53 static inline HWC2On1Adapter* getAdapter(hwc2_device_t* device) {
54 return static_cast<HWC2On1Adapter*>(device);
55 }
56
57 // getCapabilities
58
59 void doGetCapabilities(uint32_t* outCount,
60 int32_t* /*hwc2_capability_t*/ outCapabilities);
61 static void getCapabilitiesHook(hwc2_device_t* device, uint32_t* outCount,
62 int32_t* /*hwc2_capability_t*/ outCapabilities) {
63 getAdapter(device)->doGetCapabilities(outCount, outCapabilities);
64 }
65
66 // getFunction
67
68 hwc2_function_pointer_t doGetFunction(HWC2::FunctionDescriptor descriptor);
69 static hwc2_function_pointer_t getFunctionHook(hwc2_device_t* device,
70 int32_t intDesc) {
71 auto descriptor = static_cast<HWC2::FunctionDescriptor>(intDesc);
72 return getAdapter(device)->doGetFunction(descriptor);
73 }
74
75 // Device functions
76
77 HWC2::Error createVirtualDisplay(uint32_t width, uint32_t height,
78 hwc2_display_t* outDisplay);
79 static int32_t createVirtualDisplayHook(hwc2_device_t* device,
80 uint32_t width, uint32_t height, hwc2_display_t* outDisplay) {
81 auto error = getAdapter(device)->createVirtualDisplay(width, height,
82 outDisplay);
83 return static_cast<int32_t>(error);
84 }
85
86 HWC2::Error destroyVirtualDisplay(hwc2_display_t display);
87 static int32_t destroyVirtualDisplayHook(hwc2_device_t* device,
88 hwc2_display_t display) {
89 auto error = getAdapter(device)->destroyVirtualDisplay(display);
90 return static_cast<int32_t>(error);
91 }
92
93 std::string mDumpString;
94 void dump(uint32_t* outSize, char* outBuffer);
95 static void dumpHook(hwc2_device_t* device, uint32_t* outSize,
96 char* outBuffer) {
97 getAdapter(device)->dump(outSize, outBuffer);
98 }
99
100 uint32_t getMaxVirtualDisplayCount();
101 static uint32_t getMaxVirtualDisplayCountHook(hwc2_device_t* device) {
102 return getAdapter(device)->getMaxVirtualDisplayCount();
103 }
104
105 HWC2::Error registerCallback(HWC2::Callback descriptor,
106 hwc2_callback_data_t callbackData, hwc2_function_pointer_t pointer);
107 static int32_t registerCallbackHook(hwc2_device_t* device,
108 int32_t intDesc, hwc2_callback_data_t callbackData,
109 hwc2_function_pointer_t pointer) {
110 auto descriptor = static_cast<HWC2::Callback>(intDesc);
111 auto error = getAdapter(device)->registerCallback(descriptor,
112 callbackData, pointer);
113 return static_cast<int32_t>(error);
114 }
115
116 // Display functions
117
118 class Layer;
119
120 class SortLayersByZ {
121 public:
122 bool operator()(const std::shared_ptr<Layer>& lhs,
123 const std::shared_ptr<Layer>& rhs);
124 };
125
126 class DisplayContentsDeleter {
127 public:
128 void operator()(struct hwc_display_contents_1* contents);
129 };
130
131 class DeferredFence {
132 public:
133 DeferredFence()
134 : mMutex(),
135 mFences({Fence::NO_FENCE, Fence::NO_FENCE}) {}
136
137 void add(int32_t fenceFd) {
138 mFences.emplace(new Fence(fenceFd));
139 mFences.pop();
140 }
141
142 const sp<Fence>& get() const {
143 return mFences.front();
144 }
145
146 private:
147 mutable std::mutex mMutex;
148 std::queue<sp<Fence>> mFences;
149 };
150
151 class FencedBuffer {
152 public:
153 FencedBuffer() : mBuffer(nullptr), mFence(Fence::NO_FENCE) {}
154
155 void setBuffer(buffer_handle_t buffer) { mBuffer = buffer; }
156 void setFence(int fenceFd) { mFence = new Fence(fenceFd); }
157
158 buffer_handle_t getBuffer() const { return mBuffer; }
159 int getFence() const { return mFence->dup(); }
160
161 private:
162 buffer_handle_t mBuffer;
163 sp<Fence> mFence;
164 };
165
166 class Display {
167 public:
168 typedef std::unique_ptr<hwc_display_contents_1,
169 DisplayContentsDeleter> HWC1Contents;
170
171 Display(HWC2On1Adapter& device, HWC2::DisplayType type);
172
173 hwc2_display_t getId() const { return mId; }
174 HWC2On1Adapter& getDevice() const { return mDevice; }
175
176 // Does not require locking because it is set before adding the
177 // Displays to the Adapter's list of displays
178 void setHwc1Id(int32_t id) { mHwc1Id = id; }
179 int32_t getHwc1Id() const { return mHwc1Id; }
180
181 void incDirty() { ++mDirtyCount; }
182 void decDirty() { --mDirtyCount; }
183 bool isDirty() const { return mDirtyCount > 0 || mZIsDirty; }
184
185 // HWC2 Display functions
186 HWC2::Error acceptChanges();
187 HWC2::Error createLayer(hwc2_layer_t* outLayerId);
188 HWC2::Error destroyLayer(hwc2_layer_t layerId);
189 HWC2::Error getActiveConfig(hwc2_config_t* outConfigId);
190 HWC2::Error getAttribute(hwc2_config_t configId,
191 HWC2::Attribute attribute, int32_t* outValue);
192 HWC2::Error getChangedCompositionTypes(uint32_t* outNumElements,
193 hwc2_layer_t* outLayers, int32_t* outTypes);
194 HWC2::Error getConfigs(uint32_t* outNumConfigs,
195 hwc2_config_t* outConfigIds);
196 HWC2::Error getDozeSupport(int32_t* outSupport);
197 HWC2::Error getName(uint32_t* outSize, char* outName);
198 HWC2::Error getReleaseFences(uint32_t* outNumElements,
199 hwc2_layer_t* outLayers, int32_t* outFences);
200 HWC2::Error getRequests(int32_t* outDisplayRequests,
201 uint32_t* outNumElements, hwc2_layer_t* outLayers,
202 int32_t* outLayerRequests);
203 HWC2::Error getType(int32_t* outType);
204 HWC2::Error present(int32_t* outRetireFence);
205 HWC2::Error setActiveConfig(hwc2_config_t configId);
206 HWC2::Error setClientTarget(buffer_handle_t target,
207 int32_t acquireFence, int32_t dataspace);
208 HWC2::Error setOutputBuffer(buffer_handle_t buffer,
209 int32_t releaseFence);
210 HWC2::Error setPowerMode(HWC2::PowerMode mode);
211 HWC2::Error setVsyncEnabled(HWC2::Vsync enabled);
212 HWC2::Error validate(uint32_t* outNumTypes,
213 uint32_t* outNumRequests);
214
215 HWC2::Error updateLayerZ(hwc2_layer_t layerId, uint32_t z);
216
217 // Read configs from HWC1 device
218 void populateConfigs();
219
220 // Set configs for a virtual display
221 void populateConfigs(uint32_t width, uint32_t height);
222
223 bool prepare();
224 HWC1Contents cloneRequestedContents() const;
225 void setReceivedContents(HWC1Contents contents);
226 bool hasChanges() const;
227 HWC2::Error set(hwc_display_contents_1& hwcContents);
228 void addRetireFence(int fenceFd);
229 void addReleaseFences(const hwc_display_contents_1& hwcContents);
230
231 std::string dump() const;
232
233 private:
234 class Config {
235 public:
236 Config(Display& display, hwc2_config_t id, uint32_t hwcId)
237 : mDisplay(display),
238 mId(id),
Dan Stozafc4e2022016-02-23 11:43:19 -0800239 mHwcId(hwcId),
240 mAttributes() {}
Dan Stozac6998d22015-09-24 17:03:36 -0700241
242 bool isOnDisplay(const Display& display) const {
243 return display.getId() == mDisplay.getId();
244 }
245
246 hwc2_config_t getId() const { return mId; }
247 uint32_t getHwcId() const { return mHwcId; }
248
249 void setAttribute(HWC2::Attribute attribute, int32_t value);
250 int32_t getAttribute(HWC2::Attribute attribute) const;
251
252 std::string toString() const;
253
254 private:
255 Display& mDisplay;
256 const hwc2_config_t mId;
257 const uint32_t mHwcId;
258 std::unordered_map<HWC2::Attribute, int32_t> mAttributes;
259 };
260
261 class Changes {
262 public:
263 uint32_t getNumTypes() const {
264 return static_cast<uint32_t>(mTypeChanges.size());
265 }
266
267 uint32_t getNumLayerRequests() const {
268 return static_cast<uint32_t>(mLayerRequests.size());
269 }
270
271 const std::unordered_map<hwc2_layer_t, HWC2::Composition>&
272 getTypeChanges() const {
273 return mTypeChanges;
274 }
275
276 const std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>&
277 getLayerRequests() const {
278 return mLayerRequests;
279 }
280
281 int32_t getDisplayRequests() const {
282 int32_t requests = 0;
283 for (auto request : mDisplayRequests) {
284 requests |= static_cast<int32_t>(request);
285 }
286 return requests;
287 }
288
289 void addTypeChange(hwc2_layer_t layerId,
290 HWC2::Composition type) {
291 mTypeChanges.insert({layerId, type});
292 }
293
294 void clearTypeChanges() { mTypeChanges.clear(); }
295
296 void addLayerRequest(hwc2_layer_t layerId,
297 HWC2::LayerRequest request) {
298 mLayerRequests.insert({layerId, request});
299 }
300
301 private:
302 std::unordered_map<hwc2_layer_t, HWC2::Composition>
303 mTypeChanges;
304 std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>
305 mLayerRequests;
306 std::unordered_set<HWC2::DisplayRequest> mDisplayRequests;
307 };
308
309 std::shared_ptr<const Config>
310 getConfig(hwc2_config_t configId) const;
311
312 void reallocateHwc1Contents();
313 void assignHwc1LayerIds();
314
315 void updateTypeChanges(const struct hwc_layer_1& hwc1Layer,
316 const Layer& layer);
317 void updateLayerRequests(const struct hwc_layer_1& hwc1Layer,
318 const Layer& layer);
319
320 void prepareFramebufferTarget();
321
322 static std::atomic<hwc2_display_t> sNextId;
323 const hwc2_display_t mId;
324 HWC2On1Adapter& mDevice;
325
326 std::atomic<size_t> mDirtyCount;
327
328 // The state of this display should only be modified from
329 // SurfaceFlinger's main loop, with the exception of when dump is
330 // called. To prevent a bad state from crashing us during a dump
331 // call, all public calls into Display must acquire this mutex.
332 //
333 // It is recursive because we don't want to deadlock in validate
334 // (or present) when we call HWC2On1Adapter::prepareAllDisplays
335 // (or setAllDisplays), which calls back into Display functions
336 // which require locking.
337 mutable std::recursive_mutex mStateMutex;
338
339 bool mZIsDirty;
340 HWC1Contents mHwc1RequestedContents;
341 HWC1Contents mHwc1ReceivedContents;
342 DeferredFence mRetireFence;
343
344 // Will only be non-null after the layer has been validated but
345 // before it has been presented
346 std::unique_ptr<Changes> mChanges;
347
348 int32_t mHwc1Id;
349 std::vector<std::shared_ptr<Config>> mConfigs;
350 std::shared_ptr<const Config> mActiveConfig;
351 std::string mName;
352 HWC2::DisplayType mType;
353 HWC2::PowerMode mPowerMode;
354 HWC2::Vsync mVsyncEnabled;
355
356 FencedBuffer mClientTarget;
357 FencedBuffer mOutputBuffer;
358
359 std::multiset<std::shared_ptr<Layer>, SortLayersByZ> mLayers;
360 std::unordered_map<size_t, std::shared_ptr<Layer>> mHwc1LayerMap;
361 };
362
363 template <typename ...Args>
364 static int32_t callDisplayFunction(hwc2_device_t* device,
365 hwc2_display_t displayId, HWC2::Error (Display::*member)(Args...),
366 Args... args) {
367 auto display = getAdapter(device)->getDisplay(displayId);
368 if (!display) {
369 return static_cast<int32_t>(HWC2::Error::BadDisplay);
370 }
371 auto error = ((*display).*member)(std::forward<Args>(args)...);
372 return static_cast<int32_t>(error);
373 }
374
375 template <typename MF, MF memFunc, typename ...Args>
376 static int32_t displayHook(hwc2_device_t* device, hwc2_display_t displayId,
377 Args... args) {
378 return HWC2On1Adapter::callDisplayFunction(device, displayId, memFunc,
379 std::forward<Args>(args)...);
380 }
381
382 static int32_t getDisplayAttributeHook(hwc2_device_t* device,
383 hwc2_display_t display, hwc2_config_t config,
384 int32_t intAttribute, int32_t* outValue) {
385 auto attribute = static_cast<HWC2::Attribute>(intAttribute);
386 return callDisplayFunction(device, display, &Display::getAttribute,
387 config, attribute, outValue);
388 }
389
390 static int32_t setPowerModeHook(hwc2_device_t* device,
391 hwc2_display_t display, int32_t intMode) {
392 auto mode = static_cast<HWC2::PowerMode>(intMode);
393 return callDisplayFunction(device, display, &Display::setPowerMode,
394 mode);
395 }
396
397 static int32_t setVsyncEnabledHook(hwc2_device_t* device,
398 hwc2_display_t display, int32_t intEnabled) {
399 auto enabled = static_cast<HWC2::Vsync>(intEnabled);
400 return callDisplayFunction(device, display, &Display::setVsyncEnabled,
401 enabled);
402 }
403
404 // Layer functions
405
406 template <typename T>
407 class LatchedState {
408 public:
409 LatchedState(Layer& parent, T initialValue)
410 : mParent(parent),
411 mPendingValue(initialValue),
412 mValue(initialValue) {}
413
414 void setPending(T value) {
415 if (value == mPendingValue) {
416 return;
417 }
418 if (mPendingValue == mValue) {
419 mParent.incDirty();
420 } else if (value == mValue) {
421 mParent.decDirty();
422 }
423 mPendingValue = value;
424 }
425
426 T getValue() const { return mValue; }
427 T getPendingValue() const { return mPendingValue; }
428
429 bool isDirty() const { return mPendingValue != mValue; }
430
431 void latch() {
432 if (isDirty()) {
433 mValue = mPendingValue;
434 mParent.decDirty();
435 }
436 }
437
438 private:
439 Layer& mParent;
440 T mPendingValue;
441 T mValue;
442 };
443
444 class Layer {
445 public:
446 Layer(Display& display);
447
448 bool operator==(const Layer& other) { return mId == other.mId; }
449 bool operator!=(const Layer& other) { return !(*this == other); }
450
451 hwc2_layer_t getId() const { return mId; }
452 Display& getDisplay() const { return mDisplay; }
453
454 void incDirty() { if (mDirtyCount++ == 0) mDisplay.incDirty(); }
455 void decDirty() { if (--mDirtyCount == 0) mDisplay.decDirty(); }
456 bool isDirty() const { return mDirtyCount > 0; }
457
458 // HWC2 Layer functions
459 HWC2::Error setBuffer(buffer_handle_t buffer, int32_t acquireFence);
460 HWC2::Error setCursorPosition(int32_t x, int32_t y);
461 HWC2::Error setSurfaceDamage(hwc_region_t damage);
462
463 // HWC2 Layer state functions
464 HWC2::Error setBlendMode(HWC2::BlendMode mode);
465 HWC2::Error setColor(hwc_color_t color);
466 HWC2::Error setCompositionType(HWC2::Composition type);
467 HWC2::Error setDisplayFrame(hwc_rect_t frame);
468 HWC2::Error setPlaneAlpha(float alpha);
469 HWC2::Error setSidebandStream(const native_handle_t* stream);
470 HWC2::Error setSourceCrop(hwc_frect_t crop);
471 HWC2::Error setTransform(HWC2::Transform transform);
472 HWC2::Error setVisibleRegion(hwc_region_t visible);
473 HWC2::Error setZ(uint32_t z);
474
475 HWC2::Composition getCompositionType() const {
476 return mCompositionType.getValue();
477 }
478 uint32_t getZ() const { return mZ; }
479
480 void addReleaseFence(int fenceFd);
481 const sp<Fence>& getReleaseFence() const;
482
483 void setHwc1Id(size_t id) { mHwc1Id = id; }
484 size_t getHwc1Id() const { return mHwc1Id; }
485
486 void applyState(struct hwc_layer_1& hwc1Layer, bool applyAllState);
487
488 std::string dump() const;
489
490 private:
491 void applyCommonState(struct hwc_layer_1& hwc1Layer,
492 bool applyAllState);
493 void applySolidColorState(struct hwc_layer_1& hwc1Layer,
494 bool applyAllState);
495 void applySidebandState(struct hwc_layer_1& hwc1Layer,
496 bool applyAllState);
497 void applyBufferState(struct hwc_layer_1& hwc1Layer);
498 void applyCompositionType(struct hwc_layer_1& hwc1Layer,
499 bool applyAllState);
500
501 static std::atomic<hwc2_layer_t> sNextId;
502 const hwc2_layer_t mId;
503 Display& mDisplay;
504 size_t mDirtyCount;
505
506 FencedBuffer mBuffer;
507 std::vector<hwc_rect_t> mSurfaceDamage;
508
509 LatchedState<HWC2::BlendMode> mBlendMode;
510 LatchedState<hwc_color_t> mColor;
511 LatchedState<HWC2::Composition> mCompositionType;
512 LatchedState<hwc_rect_t> mDisplayFrame;
513 LatchedState<float> mPlaneAlpha;
514 LatchedState<const native_handle_t*> mSidebandStream;
515 LatchedState<hwc_frect_t> mSourceCrop;
516 LatchedState<HWC2::Transform> mTransform;
517 LatchedState<std::vector<hwc_rect_t>> mVisibleRegion;
518 uint32_t mZ;
519
520 DeferredFence mReleaseFence;
521
522 size_t mHwc1Id;
523 bool mHasUnsupportedPlaneAlpha;
524 };
525
526 template <typename ...Args>
527 static int32_t callLayerFunction(hwc2_device_t* device,
528 hwc2_display_t displayId, hwc2_layer_t layerId,
529 HWC2::Error (Layer::*member)(Args...), Args... args) {
530 auto result = getAdapter(device)->getLayer(displayId, layerId);
531 auto error = std::get<HWC2::Error>(result);
532 if (error == HWC2::Error::None) {
533 auto layer = std::get<Layer*>(result);
534 error = ((*layer).*member)(std::forward<Args>(args)...);
535 }
536 return static_cast<int32_t>(error);
537 }
538
539 template <typename MF, MF memFunc, typename ...Args>
540 static int32_t layerHook(hwc2_device_t* device, hwc2_display_t displayId,
541 hwc2_layer_t layerId, Args... args) {
542 return HWC2On1Adapter::callLayerFunction(device, displayId, layerId,
543 memFunc, std::forward<Args>(args)...);
544 }
545
546 // Layer state functions
547
548 static int32_t setLayerBlendModeHook(hwc2_device_t* device,
549 hwc2_display_t display, hwc2_layer_t layer, int32_t intMode) {
550 auto mode = static_cast<HWC2::BlendMode>(intMode);
551 return callLayerFunction(device, display, layer,
552 &Layer::setBlendMode, mode);
553 }
554
555 static int32_t setLayerCompositionTypeHook(hwc2_device_t* device,
556 hwc2_display_t display, hwc2_layer_t layer, int32_t intType) {
557 auto type = static_cast<HWC2::Composition>(intType);
558 return callLayerFunction(device, display, layer,
559 &Layer::setCompositionType, type);
560 }
561
562 static int32_t setLayerTransformHook(hwc2_device_t* device,
563 hwc2_display_t display, hwc2_layer_t layer, int32_t intTransform) {
564 auto transform = static_cast<HWC2::Transform>(intTransform);
565 return callLayerFunction(device, display, layer, &Layer::setTransform,
566 transform);
567 }
568
569 static int32_t setLayerZOrderHook(hwc2_device_t* device,
570 hwc2_display_t display, hwc2_layer_t layer, uint32_t z) {
571 return callDisplayFunction(device, display, &Display::updateLayerZ,
572 layer, z);
573 }
574
575 // Adapter internals
576
577 void populateCapabilities();
578 Display* getDisplay(hwc2_display_t id);
579 std::tuple<Layer*, HWC2::Error> getLayer(hwc2_display_t displayId,
580 hwc2_layer_t layerId);
581 void populatePrimary();
582
583 bool prepareAllDisplays();
584 std::vector<struct hwc_display_contents_1*> mHwc1Contents;
585 HWC2::Error setAllDisplays();
586
587 void hwc1Invalidate();
588 void hwc1Vsync(int hwc1DisplayId, int64_t timestamp);
589 void hwc1Hotplug(int hwc1DisplayId, int connected);
590
591 // These are set in the constructor and before any asynchronous events are
592 // possible
593
594 struct hwc_composer_device_1* const mHwc1Device;
595 const uint8_t mHwc1MinorVersion;
596 bool mHwc1SupportsVirtualDisplays;
597
598 class Callbacks;
599 const std::unique_ptr<Callbacks> mHwc1Callbacks;
600
601 std::unordered_set<HWC2::Capability> mCapabilities;
602
603 // These are only accessed from the main SurfaceFlinger thread (not from
604 // callbacks or dump
605
606 std::map<hwc2_layer_t, std::shared_ptr<Layer>> mLayers;
607 std::shared_ptr<Display> mHwc1VirtualDisplay;
608
609 // These are potentially accessed from multiple threads, and are protected
Dan Stozafc4e2022016-02-23 11:43:19 -0800610 // by this mutex. This needs to be recursive, since the HWC1 implementation
611 // can call back into the invalidate callback on the same thread that is
612 // calling prepare.
613 std::recursive_timed_mutex mStateMutex;
Dan Stozac6998d22015-09-24 17:03:36 -0700614
615 struct CallbackInfo {
616 hwc2_callback_data_t data;
617 hwc2_function_pointer_t pointer;
618 };
619 std::unordered_map<HWC2::Callback, CallbackInfo> mCallbacks;
620 bool mHasPendingInvalidate;
621 std::vector<std::pair<int, int64_t>> mPendingVsyncs;
622 std::vector<std::pair<int, int>> mPendingHotplugs;
623
624 std::map<hwc2_display_t, std::shared_ptr<Display>> mDisplays;
625 std::unordered_map<int, hwc2_display_t> mHwc1DisplayMap;
626};
627
628} // namespace android
629
630#endif