blob: 500c48f6ef2fd72527e952c29080d51de726a287 [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),
239 mHwcId(hwcId) {}
240
241 bool isOnDisplay(const Display& display) const {
242 return display.getId() == mDisplay.getId();
243 }
244
245 hwc2_config_t getId() const { return mId; }
246 uint32_t getHwcId() const { return mHwcId; }
247
248 void setAttribute(HWC2::Attribute attribute, int32_t value);
249 int32_t getAttribute(HWC2::Attribute attribute) const;
250
251 std::string toString() const;
252
253 private:
254 Display& mDisplay;
255 const hwc2_config_t mId;
256 const uint32_t mHwcId;
257 std::unordered_map<HWC2::Attribute, int32_t> mAttributes;
258 };
259
260 class Changes {
261 public:
262 uint32_t getNumTypes() const {
263 return static_cast<uint32_t>(mTypeChanges.size());
264 }
265
266 uint32_t getNumLayerRequests() const {
267 return static_cast<uint32_t>(mLayerRequests.size());
268 }
269
270 const std::unordered_map<hwc2_layer_t, HWC2::Composition>&
271 getTypeChanges() const {
272 return mTypeChanges;
273 }
274
275 const std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>&
276 getLayerRequests() const {
277 return mLayerRequests;
278 }
279
280 int32_t getDisplayRequests() const {
281 int32_t requests = 0;
282 for (auto request : mDisplayRequests) {
283 requests |= static_cast<int32_t>(request);
284 }
285 return requests;
286 }
287
288 void addTypeChange(hwc2_layer_t layerId,
289 HWC2::Composition type) {
290 mTypeChanges.insert({layerId, type});
291 }
292
293 void clearTypeChanges() { mTypeChanges.clear(); }
294
295 void addLayerRequest(hwc2_layer_t layerId,
296 HWC2::LayerRequest request) {
297 mLayerRequests.insert({layerId, request});
298 }
299
300 private:
301 std::unordered_map<hwc2_layer_t, HWC2::Composition>
302 mTypeChanges;
303 std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>
304 mLayerRequests;
305 std::unordered_set<HWC2::DisplayRequest> mDisplayRequests;
306 };
307
308 std::shared_ptr<const Config>
309 getConfig(hwc2_config_t configId) const;
310
311 void reallocateHwc1Contents();
312 void assignHwc1LayerIds();
313
314 void updateTypeChanges(const struct hwc_layer_1& hwc1Layer,
315 const Layer& layer);
316 void updateLayerRequests(const struct hwc_layer_1& hwc1Layer,
317 const Layer& layer);
318
319 void prepareFramebufferTarget();
320
321 static std::atomic<hwc2_display_t> sNextId;
322 const hwc2_display_t mId;
323 HWC2On1Adapter& mDevice;
324
325 std::atomic<size_t> mDirtyCount;
326
327 // The state of this display should only be modified from
328 // SurfaceFlinger's main loop, with the exception of when dump is
329 // called. To prevent a bad state from crashing us during a dump
330 // call, all public calls into Display must acquire this mutex.
331 //
332 // It is recursive because we don't want to deadlock in validate
333 // (or present) when we call HWC2On1Adapter::prepareAllDisplays
334 // (or setAllDisplays), which calls back into Display functions
335 // which require locking.
336 mutable std::recursive_mutex mStateMutex;
337
338 bool mZIsDirty;
339 HWC1Contents mHwc1RequestedContents;
340 HWC1Contents mHwc1ReceivedContents;
341 DeferredFence mRetireFence;
342
343 // Will only be non-null after the layer has been validated but
344 // before it has been presented
345 std::unique_ptr<Changes> mChanges;
346
347 int32_t mHwc1Id;
348 std::vector<std::shared_ptr<Config>> mConfigs;
349 std::shared_ptr<const Config> mActiveConfig;
350 std::string mName;
351 HWC2::DisplayType mType;
352 HWC2::PowerMode mPowerMode;
353 HWC2::Vsync mVsyncEnabled;
354
355 FencedBuffer mClientTarget;
356 FencedBuffer mOutputBuffer;
357
358 std::multiset<std::shared_ptr<Layer>, SortLayersByZ> mLayers;
359 std::unordered_map<size_t, std::shared_ptr<Layer>> mHwc1LayerMap;
360 };
361
362 template <typename ...Args>
363 static int32_t callDisplayFunction(hwc2_device_t* device,
364 hwc2_display_t displayId, HWC2::Error (Display::*member)(Args...),
365 Args... args) {
366 auto display = getAdapter(device)->getDisplay(displayId);
367 if (!display) {
368 return static_cast<int32_t>(HWC2::Error::BadDisplay);
369 }
370 auto error = ((*display).*member)(std::forward<Args>(args)...);
371 return static_cast<int32_t>(error);
372 }
373
374 template <typename MF, MF memFunc, typename ...Args>
375 static int32_t displayHook(hwc2_device_t* device, hwc2_display_t displayId,
376 Args... args) {
377 return HWC2On1Adapter::callDisplayFunction(device, displayId, memFunc,
378 std::forward<Args>(args)...);
379 }
380
381 static int32_t getDisplayAttributeHook(hwc2_device_t* device,
382 hwc2_display_t display, hwc2_config_t config,
383 int32_t intAttribute, int32_t* outValue) {
384 auto attribute = static_cast<HWC2::Attribute>(intAttribute);
385 return callDisplayFunction(device, display, &Display::getAttribute,
386 config, attribute, outValue);
387 }
388
389 static int32_t setPowerModeHook(hwc2_device_t* device,
390 hwc2_display_t display, int32_t intMode) {
391 auto mode = static_cast<HWC2::PowerMode>(intMode);
392 return callDisplayFunction(device, display, &Display::setPowerMode,
393 mode);
394 }
395
396 static int32_t setVsyncEnabledHook(hwc2_device_t* device,
397 hwc2_display_t display, int32_t intEnabled) {
398 auto enabled = static_cast<HWC2::Vsync>(intEnabled);
399 return callDisplayFunction(device, display, &Display::setVsyncEnabled,
400 enabled);
401 }
402
403 // Layer functions
404
405 template <typename T>
406 class LatchedState {
407 public:
408 LatchedState(Layer& parent, T initialValue)
409 : mParent(parent),
410 mPendingValue(initialValue),
411 mValue(initialValue) {}
412
413 void setPending(T value) {
414 if (value == mPendingValue) {
415 return;
416 }
417 if (mPendingValue == mValue) {
418 mParent.incDirty();
419 } else if (value == mValue) {
420 mParent.decDirty();
421 }
422 mPendingValue = value;
423 }
424
425 T getValue() const { return mValue; }
426 T getPendingValue() const { return mPendingValue; }
427
428 bool isDirty() const { return mPendingValue != mValue; }
429
430 void latch() {
431 if (isDirty()) {
432 mValue = mPendingValue;
433 mParent.decDirty();
434 }
435 }
436
437 private:
438 Layer& mParent;
439 T mPendingValue;
440 T mValue;
441 };
442
443 class Layer {
444 public:
445 Layer(Display& display);
446
447 bool operator==(const Layer& other) { return mId == other.mId; }
448 bool operator!=(const Layer& other) { return !(*this == other); }
449
450 hwc2_layer_t getId() const { return mId; }
451 Display& getDisplay() const { return mDisplay; }
452
453 void incDirty() { if (mDirtyCount++ == 0) mDisplay.incDirty(); }
454 void decDirty() { if (--mDirtyCount == 0) mDisplay.decDirty(); }
455 bool isDirty() const { return mDirtyCount > 0; }
456
457 // HWC2 Layer functions
458 HWC2::Error setBuffer(buffer_handle_t buffer, int32_t acquireFence);
459 HWC2::Error setCursorPosition(int32_t x, int32_t y);
460 HWC2::Error setSurfaceDamage(hwc_region_t damage);
461
462 // HWC2 Layer state functions
463 HWC2::Error setBlendMode(HWC2::BlendMode mode);
464 HWC2::Error setColor(hwc_color_t color);
465 HWC2::Error setCompositionType(HWC2::Composition type);
466 HWC2::Error setDisplayFrame(hwc_rect_t frame);
467 HWC2::Error setPlaneAlpha(float alpha);
468 HWC2::Error setSidebandStream(const native_handle_t* stream);
469 HWC2::Error setSourceCrop(hwc_frect_t crop);
470 HWC2::Error setTransform(HWC2::Transform transform);
471 HWC2::Error setVisibleRegion(hwc_region_t visible);
472 HWC2::Error setZ(uint32_t z);
473
474 HWC2::Composition getCompositionType() const {
475 return mCompositionType.getValue();
476 }
477 uint32_t getZ() const { return mZ; }
478
479 void addReleaseFence(int fenceFd);
480 const sp<Fence>& getReleaseFence() const;
481
482 void setHwc1Id(size_t id) { mHwc1Id = id; }
483 size_t getHwc1Id() const { return mHwc1Id; }
484
485 void applyState(struct hwc_layer_1& hwc1Layer, bool applyAllState);
486
487 std::string dump() const;
488
489 private:
490 void applyCommonState(struct hwc_layer_1& hwc1Layer,
491 bool applyAllState);
492 void applySolidColorState(struct hwc_layer_1& hwc1Layer,
493 bool applyAllState);
494 void applySidebandState(struct hwc_layer_1& hwc1Layer,
495 bool applyAllState);
496 void applyBufferState(struct hwc_layer_1& hwc1Layer);
497 void applyCompositionType(struct hwc_layer_1& hwc1Layer,
498 bool applyAllState);
499
500 static std::atomic<hwc2_layer_t> sNextId;
501 const hwc2_layer_t mId;
502 Display& mDisplay;
503 size_t mDirtyCount;
504
505 FencedBuffer mBuffer;
506 std::vector<hwc_rect_t> mSurfaceDamage;
507
508 LatchedState<HWC2::BlendMode> mBlendMode;
509 LatchedState<hwc_color_t> mColor;
510 LatchedState<HWC2::Composition> mCompositionType;
511 LatchedState<hwc_rect_t> mDisplayFrame;
512 LatchedState<float> mPlaneAlpha;
513 LatchedState<const native_handle_t*> mSidebandStream;
514 LatchedState<hwc_frect_t> mSourceCrop;
515 LatchedState<HWC2::Transform> mTransform;
516 LatchedState<std::vector<hwc_rect_t>> mVisibleRegion;
517 uint32_t mZ;
518
519 DeferredFence mReleaseFence;
520
521 size_t mHwc1Id;
522 bool mHasUnsupportedPlaneAlpha;
523 };
524
525 template <typename ...Args>
526 static int32_t callLayerFunction(hwc2_device_t* device,
527 hwc2_display_t displayId, hwc2_layer_t layerId,
528 HWC2::Error (Layer::*member)(Args...), Args... args) {
529 auto result = getAdapter(device)->getLayer(displayId, layerId);
530 auto error = std::get<HWC2::Error>(result);
531 if (error == HWC2::Error::None) {
532 auto layer = std::get<Layer*>(result);
533 error = ((*layer).*member)(std::forward<Args>(args)...);
534 }
535 return static_cast<int32_t>(error);
536 }
537
538 template <typename MF, MF memFunc, typename ...Args>
539 static int32_t layerHook(hwc2_device_t* device, hwc2_display_t displayId,
540 hwc2_layer_t layerId, Args... args) {
541 return HWC2On1Adapter::callLayerFunction(device, displayId, layerId,
542 memFunc, std::forward<Args>(args)...);
543 }
544
545 // Layer state functions
546
547 static int32_t setLayerBlendModeHook(hwc2_device_t* device,
548 hwc2_display_t display, hwc2_layer_t layer, int32_t intMode) {
549 auto mode = static_cast<HWC2::BlendMode>(intMode);
550 return callLayerFunction(device, display, layer,
551 &Layer::setBlendMode, mode);
552 }
553
554 static int32_t setLayerCompositionTypeHook(hwc2_device_t* device,
555 hwc2_display_t display, hwc2_layer_t layer, int32_t intType) {
556 auto type = static_cast<HWC2::Composition>(intType);
557 return callLayerFunction(device, display, layer,
558 &Layer::setCompositionType, type);
559 }
560
561 static int32_t setLayerTransformHook(hwc2_device_t* device,
562 hwc2_display_t display, hwc2_layer_t layer, int32_t intTransform) {
563 auto transform = static_cast<HWC2::Transform>(intTransform);
564 return callLayerFunction(device, display, layer, &Layer::setTransform,
565 transform);
566 }
567
568 static int32_t setLayerZOrderHook(hwc2_device_t* device,
569 hwc2_display_t display, hwc2_layer_t layer, uint32_t z) {
570 return callDisplayFunction(device, display, &Display::updateLayerZ,
571 layer, z);
572 }
573
574 // Adapter internals
575
576 void populateCapabilities();
577 Display* getDisplay(hwc2_display_t id);
578 std::tuple<Layer*, HWC2::Error> getLayer(hwc2_display_t displayId,
579 hwc2_layer_t layerId);
580 void populatePrimary();
581
582 bool prepareAllDisplays();
583 std::vector<struct hwc_display_contents_1*> mHwc1Contents;
584 HWC2::Error setAllDisplays();
585
586 void hwc1Invalidate();
587 void hwc1Vsync(int hwc1DisplayId, int64_t timestamp);
588 void hwc1Hotplug(int hwc1DisplayId, int connected);
589
590 // These are set in the constructor and before any asynchronous events are
591 // possible
592
593 struct hwc_composer_device_1* const mHwc1Device;
594 const uint8_t mHwc1MinorVersion;
595 bool mHwc1SupportsVirtualDisplays;
596
597 class Callbacks;
598 const std::unique_ptr<Callbacks> mHwc1Callbacks;
599
600 std::unordered_set<HWC2::Capability> mCapabilities;
601
602 // These are only accessed from the main SurfaceFlinger thread (not from
603 // callbacks or dump
604
605 std::map<hwc2_layer_t, std::shared_ptr<Layer>> mLayers;
606 std::shared_ptr<Display> mHwc1VirtualDisplay;
607
608 // These are potentially accessed from multiple threads, and are protected
609 // by this mutex
610 std::timed_mutex mStateMutex;
611
612 struct CallbackInfo {
613 hwc2_callback_data_t data;
614 hwc2_function_pointer_t pointer;
615 };
616 std::unordered_map<HWC2::Callback, CallbackInfo> mCallbacks;
617 bool mHasPendingInvalidate;
618 std::vector<std::pair<int, int64_t>> mPendingVsyncs;
619 std::vector<std::pair<int, int>> mPendingHotplugs;
620
621 std::map<hwc2_display_t, std::shared_ptr<Display>> mDisplays;
622 std::unordered_map<int, hwc2_display_t> mHwc1DisplayMap;
623};
624
625} // namespace android
626
627#endif