blob: a1d2c884cb35d8d0d0fe3c291fb86c31063aa252 [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
Fabien Sanglardc8591472017-03-07 10:10:03 -080026#include "MiniFence.h"
Dan Stozac6998d22015-09-24 17:03:36 -070027
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
Fabien Sanglard33960702016-11-29 17:58:21 -0800137 // The semantics of the fences returned by the device differ between
138 // hwc1.set() and hwc2.present(). Read hwcomposer.h and hwcomposer2.h
139 // for more information.
140 //
141 // Release fences in hwc1 are obtained on set() for a frame n and signaled
142 // when the layer buffer is not needed for read operations anymore
143 // (typically on frame n+1). In HWC2, release fences are obtained with a
144 // special call after present() for frame n. These fences signal
145 // on frame n: More specifically, the fence for a given buffer provided in
146 // frame n will signal when the prior buffer is no longer required.
147 //
148 // A retire fence (HWC1) is signaled when a composition is replaced
149 // on the panel whereas a present fence (HWC2) is signaled when a
150 // composition starts to be displayed on a panel.
151 //
152 // The HWC2to1Adapter emulates the new fence semantics for a frame
153 // n by returning the fence from frame n-1. For frame 0, the adapter
154 // returns NO_FENCE.
Dan Stozac6998d22015-09-24 17:03:36 -0700155 class DeferredFence {
156 public:
157 DeferredFence()
Fabien Sanglardc8591472017-03-07 10:10:03 -0800158 : mFences({MiniFence::NO_FENCE, MiniFence::NO_FENCE}) {}
Dan Stozac6998d22015-09-24 17:03:36 -0700159
160 void add(int32_t fenceFd) {
Fabien Sanglardc8591472017-03-07 10:10:03 -0800161 mFences.emplace(new MiniFence(fenceFd));
Dan Stozac6998d22015-09-24 17:03:36 -0700162 mFences.pop();
163 }
164
Fabien Sanglardc8591472017-03-07 10:10:03 -0800165 const sp<MiniFence>& get() const {
Dan Stozac6998d22015-09-24 17:03:36 -0700166 return mFences.front();
167 }
168
169 private:
Fabien Sanglard33960702016-11-29 17:58:21 -0800170 // There are always two fences in this queue.
Fabien Sanglardc8591472017-03-07 10:10:03 -0800171 std::queue<sp<MiniFence>> mFences;
Dan Stozac6998d22015-09-24 17:03:36 -0700172 };
173
174 class FencedBuffer {
175 public:
Fabien Sanglardc8591472017-03-07 10:10:03 -0800176 FencedBuffer() : mBuffer(nullptr), mFence(MiniFence::NO_FENCE) {}
Dan Stozac6998d22015-09-24 17:03:36 -0700177
178 void setBuffer(buffer_handle_t buffer) { mBuffer = buffer; }
Fabien Sanglardc8591472017-03-07 10:10:03 -0800179 void setFence(int fenceFd) { mFence = new MiniFence(fenceFd); }
Dan Stozac6998d22015-09-24 17:03:36 -0700180
181 buffer_handle_t getBuffer() const { return mBuffer; }
182 int getFence() const { return mFence->dup(); }
183
184 private:
185 buffer_handle_t mBuffer;
Fabien Sanglardc8591472017-03-07 10:10:03 -0800186 sp<MiniFence> mFence;
Dan Stozac6998d22015-09-24 17:03:36 -0700187 };
188
189 class Display {
190 public:
Dan Stozac6998d22015-09-24 17:03:36 -0700191 Display(HWC2On1Adapter& device, HWC2::DisplayType type);
192
193 hwc2_display_t getId() const { return mId; }
194 HWC2On1Adapter& getDevice() const { return mDevice; }
195
196 // Does not require locking because it is set before adding the
197 // Displays to the Adapter's list of displays
198 void setHwc1Id(int32_t id) { mHwc1Id = id; }
199 int32_t getHwc1Id() const { return mHwc1Id; }
200
Dan Stozac6998d22015-09-24 17:03:36 -0700201 // HWC2 Display functions
202 HWC2::Error acceptChanges();
203 HWC2::Error createLayer(hwc2_layer_t* outLayerId);
204 HWC2::Error destroyLayer(hwc2_layer_t layerId);
205 HWC2::Error getActiveConfig(hwc2_config_t* outConfigId);
206 HWC2::Error getAttribute(hwc2_config_t configId,
207 HWC2::Attribute attribute, int32_t* outValue);
208 HWC2::Error getChangedCompositionTypes(uint32_t* outNumElements,
209 hwc2_layer_t* outLayers, int32_t* outTypes);
Dan Stoza076ac672016-03-14 10:47:53 -0700210 HWC2::Error getColorModes(uint32_t* outNumModes, int32_t* outModes);
Dan Stozac6998d22015-09-24 17:03:36 -0700211 HWC2::Error getConfigs(uint32_t* outNumConfigs,
212 hwc2_config_t* outConfigIds);
213 HWC2::Error getDozeSupport(int32_t* outSupport);
Dan Stozaed40eba2016-03-16 12:33:52 -0700214 HWC2::Error getHdrCapabilities(uint32_t* outNumTypes,
215 int32_t* outTypes, float* outMaxLuminance,
216 float* outMaxAverageLuminance, float* outMinLuminance);
Dan Stozac6998d22015-09-24 17:03:36 -0700217 HWC2::Error getName(uint32_t* outSize, char* outName);
218 HWC2::Error getReleaseFences(uint32_t* outNumElements,
219 hwc2_layer_t* outLayers, int32_t* outFences);
220 HWC2::Error getRequests(int32_t* outDisplayRequests,
221 uint32_t* outNumElements, hwc2_layer_t* outLayers,
222 int32_t* outLayerRequests);
223 HWC2::Error getType(int32_t* outType);
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800224
225 // Since HWC1 "presents" (called "set" in HWC1) all Displays
226 // at once, the first call to any Display::present will trigger
227 // present() on all Displays in the Device. Subsequent calls without
228 // first calling validate() are noop (except for duping/returning
229 // the retire fence).
Dan Stozac6998d22015-09-24 17:03:36 -0700230 HWC2::Error present(int32_t* outRetireFence);
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800231
Dan Stozac6998d22015-09-24 17:03:36 -0700232 HWC2::Error setActiveConfig(hwc2_config_t configId);
233 HWC2::Error setClientTarget(buffer_handle_t target,
Dan Stoza5cf424b2016-05-20 14:02:39 -0700234 int32_t acquireFence, int32_t dataspace,
235 hwc_region_t damage);
Michael Wright28f24d02016-07-12 13:30:53 -0700236 HWC2::Error setColorMode(android_color_mode_t mode);
Dan Stoza5df2a862016-03-24 16:19:37 -0700237 HWC2::Error setColorTransform(android_color_transform_t hint);
Dan Stozac6998d22015-09-24 17:03:36 -0700238 HWC2::Error setOutputBuffer(buffer_handle_t buffer,
239 int32_t releaseFence);
240 HWC2::Error setPowerMode(HWC2::PowerMode mode);
241 HWC2::Error setVsyncEnabled(HWC2::Vsync enabled);
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800242
243 // Since HWC1 "validates" (called "prepare" in HWC1) all Displays
244 // at once, the first call to any Display::validate() will trigger
245 // validate() on all other Displays in the Device.
Dan Stozac6998d22015-09-24 17:03:36 -0700246 HWC2::Error validate(uint32_t* outNumTypes,
247 uint32_t* outNumRequests);
248
249 HWC2::Error updateLayerZ(hwc2_layer_t layerId, uint32_t z);
250
251 // Read configs from HWC1 device
252 void populateConfigs();
253
254 // Set configs for a virtual display
255 void populateConfigs(uint32_t width, uint32_t height);
256
257 bool prepare();
Fabien Sanglard33960702016-11-29 17:58:21 -0800258
259 // Called after hwc.prepare() with responses from the device.
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800260 void generateChanges();
Fabien Sanglard33960702016-11-29 17:58:21 -0800261
Dan Stozac6998d22015-09-24 17:03:36 -0700262 bool hasChanges() const;
263 HWC2::Error set(hwc_display_contents_1& hwcContents);
264 void addRetireFence(int fenceFd);
265 void addReleaseFences(const hwc_display_contents_1& hwcContents);
266
Dan Stoza5df2a862016-03-24 16:19:37 -0700267 bool hasColorTransform() const;
268
Dan Stozac6998d22015-09-24 17:03:36 -0700269 std::string dump() const;
270
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800271 // Return a rect from the pool allocated during validate()
272 hwc_rect_t* GetRects(size_t numRects);
273
274 hwc_display_contents_1* getDisplayContents();
275
276 void markGeometryChanged() { mGeometryChanged = true; }
277 void resetGeometryMarker() { mGeometryChanged = false;}
Dan Stozac6998d22015-09-24 17:03:36 -0700278 private:
279 class Config {
280 public:
Dan Stoza076ac672016-03-14 10:47:53 -0700281 Config(Display& display)
Dan Stozac6998d22015-09-24 17:03:36 -0700282 : mDisplay(display),
Pablo Ceballosbd3577e2016-06-20 17:40:34 -0700283 mId(0),
Dan Stozafc4e2022016-02-23 11:43:19 -0800284 mAttributes() {}
Dan Stozac6998d22015-09-24 17:03:36 -0700285
286 bool isOnDisplay(const Display& display) const {
287 return display.getId() == mDisplay.getId();
288 }
289
Dan Stozac6998d22015-09-24 17:03:36 -0700290 void setAttribute(HWC2::Attribute attribute, int32_t value);
291 int32_t getAttribute(HWC2::Attribute attribute) const;
292
Dan Stoza076ac672016-03-14 10:47:53 -0700293 void setHwc1Id(uint32_t id);
294 bool hasHwc1Id(uint32_t id) const;
Michael Wright28f24d02016-07-12 13:30:53 -0700295 HWC2::Error getColorModeForHwc1Id(uint32_t id,
296 android_color_mode_t *outMode) const;
297 HWC2::Error getHwc1IdForColorMode(android_color_mode_t mode,
Dan Stoza076ac672016-03-14 10:47:53 -0700298 uint32_t* outId) const;
299
300 void setId(hwc2_config_t id) { mId = id; }
301 hwc2_config_t getId() const { return mId; }
302
303 // Attempts to merge two configs that differ only in color
304 // mode. Returns whether the merge was successful
305 bool merge(const Config& other);
306
Michael Wright28f24d02016-07-12 13:30:53 -0700307 std::set<android_color_mode_t> getColorModes() const;
Dan Stoza076ac672016-03-14 10:47:53 -0700308
309 // splitLine divides the output into two lines suitable for
310 // dumpsys SurfaceFlinger
311 std::string toString(bool splitLine = false) const;
Dan Stozac6998d22015-09-24 17:03:36 -0700312
313 private:
314 Display& mDisplay;
Dan Stoza076ac672016-03-14 10:47:53 -0700315 hwc2_config_t mId;
Dan Stozac6998d22015-09-24 17:03:36 -0700316 std::unordered_map<HWC2::Attribute, int32_t> mAttributes;
Dan Stoza076ac672016-03-14 10:47:53 -0700317
318 // Maps from color transform to HWC1 config ID
Michael Wright28f24d02016-07-12 13:30:53 -0700319 std::unordered_map<android_color_mode_t, uint32_t> mHwc1Ids;
Dan Stozac6998d22015-09-24 17:03:36 -0700320 };
321
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800322 // Stores changes requested from the device upon calling prepare().
Fabien Sanglard33960702016-11-29 17:58:21 -0800323 // Handles change request to:
324 // - Layer composition type.
325 // - Layer hints.
Dan Stozac6998d22015-09-24 17:03:36 -0700326 class Changes {
327 public:
328 uint32_t getNumTypes() const {
329 return static_cast<uint32_t>(mTypeChanges.size());
330 }
331
332 uint32_t getNumLayerRequests() const {
333 return static_cast<uint32_t>(mLayerRequests.size());
334 }
335
336 const std::unordered_map<hwc2_layer_t, HWC2::Composition>&
337 getTypeChanges() const {
338 return mTypeChanges;
339 }
340
341 const std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>&
342 getLayerRequests() const {
343 return mLayerRequests;
344 }
345
Dan Stozac6998d22015-09-24 17:03:36 -0700346 void addTypeChange(hwc2_layer_t layerId,
347 HWC2::Composition type) {
348 mTypeChanges.insert({layerId, type});
349 }
350
351 void clearTypeChanges() { mTypeChanges.clear(); }
352
353 void addLayerRequest(hwc2_layer_t layerId,
354 HWC2::LayerRequest request) {
355 mLayerRequests.insert({layerId, request});
356 }
357
358 private:
359 std::unordered_map<hwc2_layer_t, HWC2::Composition>
360 mTypeChanges;
361 std::unordered_map<hwc2_layer_t, HWC2::LayerRequest>
362 mLayerRequests;
Dan Stozac6998d22015-09-24 17:03:36 -0700363 };
364
365 std::shared_ptr<const Config>
366 getConfig(hwc2_config_t configId) const;
367
Dan Stoza076ac672016-03-14 10:47:53 -0700368 void populateColorModes();
369 void initializeActiveConfig();
370
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800371 // Creates a bi-directional mapping between index in HWC1
372 // prepare/set array and Layer object. Stores mapping in
373 // mHwc1LayerMap and also updates Layer's attribute mHwc1Id.
Dan Stozac6998d22015-09-24 17:03:36 -0700374 void assignHwc1LayerIds();
375
Fabien Sanglard33960702016-11-29 17:58:21 -0800376 // Called after a response to prepare() has been received:
377 // Ingest composition type changes requested by the device.
Dan Stozac6998d22015-09-24 17:03:36 -0700378 void updateTypeChanges(const struct hwc_layer_1& hwc1Layer,
379 const Layer& layer);
Fabien Sanglard33960702016-11-29 17:58:21 -0800380
381 // Called after a response to prepare() has been received:
382 // Ingest layer hint changes requested by the device.
Dan Stozac6998d22015-09-24 17:03:36 -0700383 void updateLayerRequests(const struct hwc_layer_1& hwc1Layer,
384 const Layer& layer);
385
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800386 // Set all fields in HWC1 comm array for layer containing the
387 // HWC_FRAMEBUFFER_TARGET (always the last layer).
Dan Stozac6998d22015-09-24 17:03:36 -0700388 void prepareFramebufferTarget();
389
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800390 // Display ID generator.
Dan Stozac6998d22015-09-24 17:03:36 -0700391 static std::atomic<hwc2_display_t> sNextId;
392 const hwc2_display_t mId;
Dan Stozac6998d22015-09-24 17:03:36 -0700393
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800394
395 HWC2On1Adapter& mDevice;
Dan Stozac6998d22015-09-24 17:03:36 -0700396
397 // The state of this display should only be modified from
398 // SurfaceFlinger's main loop, with the exception of when dump is
399 // called. To prevent a bad state from crashing us during a dump
400 // call, all public calls into Display must acquire this mutex.
401 //
402 // It is recursive because we don't want to deadlock in validate
403 // (or present) when we call HWC2On1Adapter::prepareAllDisplays
404 // (or setAllDisplays), which calls back into Display functions
405 // which require locking.
406 mutable std::recursive_mutex mStateMutex;
407
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800408 // Allocate RAM able to store all layers and rects used for
409 // communication with HWC1. Place allocated RAM in variable
410 // mHwc1RequestedContents.
411 void allocateRequestedContents();
Fabien Sanglard33960702016-11-29 17:58:21 -0800412
413 // Array of structs exchanged between client and hwc1 device.
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800414 // Sent to device upon calling prepare().
415 std::unique_ptr<hwc_display_contents_1> mHwc1RequestedContents;
416 private:
Dan Stozac6998d22015-09-24 17:03:36 -0700417 DeferredFence mRetireFence;
418
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800419 // Will only be non-null after the Display has been validated and
Dan Stozac6998d22015-09-24 17:03:36 -0700420 // before it has been presented
421 std::unique_ptr<Changes> mChanges;
422
423 int32_t mHwc1Id;
Dan Stoza076ac672016-03-14 10:47:53 -0700424
Dan Stozac6998d22015-09-24 17:03:36 -0700425 std::vector<std::shared_ptr<Config>> mConfigs;
426 std::shared_ptr<const Config> mActiveConfig;
Michael Wright28f24d02016-07-12 13:30:53 -0700427 std::set<android_color_mode_t> mColorModes;
428 android_color_mode_t mActiveColorMode;
Dan Stozac6998d22015-09-24 17:03:36 -0700429 std::string mName;
430 HWC2::DisplayType mType;
431 HWC2::PowerMode mPowerMode;
432 HWC2::Vsync mVsyncEnabled;
433
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800434 // Used to populate HWC1 HWC_FRAMEBUFFER_TARGET layer
Dan Stozac6998d22015-09-24 17:03:36 -0700435 FencedBuffer mClientTarget;
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800436
437
Dan Stozac6998d22015-09-24 17:03:36 -0700438 FencedBuffer mOutputBuffer;
439
Dan Stoza5df2a862016-03-24 16:19:37 -0700440 bool mHasColorTransform;
441
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800442 // All layers this Display is aware of.
Dan Stozac6998d22015-09-24 17:03:36 -0700443 std::multiset<std::shared_ptr<Layer>, SortLayersByZ> mLayers;
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800444
445 // Mapping between layer index in array of hwc_display_contents_1*
446 // passed to HWC1 during validate/set and Layer object.
Dan Stozac6998d22015-09-24 17:03:36 -0700447 std::unordered_map<size_t, std::shared_ptr<Layer>> mHwc1LayerMap;
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800448
449 // All communication with HWC1 via prepare/set is done with one
450 // alloc. This pointer is pointing to a pool of hwc_rect_t.
451 size_t mNumAvailableRects;
452 hwc_rect_t* mNextAvailableRect;
453
454 // True if any of the Layers contained in this Display have been
455 // updated with anything other than a buffer since last call to
456 // Display::set()
457 bool mGeometryChanged;
Dan Stozac6998d22015-09-24 17:03:36 -0700458 };
459
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800460 // Utility template calling a Display object method directly based on the
461 // hwc2_display_t displayId parameter.
Dan Stozac6998d22015-09-24 17:03:36 -0700462 template <typename ...Args>
463 static int32_t callDisplayFunction(hwc2_device_t* device,
464 hwc2_display_t displayId, HWC2::Error (Display::*member)(Args...),
465 Args... args) {
466 auto display = getAdapter(device)->getDisplay(displayId);
467 if (!display) {
468 return static_cast<int32_t>(HWC2::Error::BadDisplay);
469 }
470 auto error = ((*display).*member)(std::forward<Args>(args)...);
471 return static_cast<int32_t>(error);
472 }
473
474 template <typename MF, MF memFunc, typename ...Args>
475 static int32_t displayHook(hwc2_device_t* device, hwc2_display_t displayId,
476 Args... args) {
477 return HWC2On1Adapter::callDisplayFunction(device, displayId, memFunc,
478 std::forward<Args>(args)...);
479 }
480
481 static int32_t getDisplayAttributeHook(hwc2_device_t* device,
482 hwc2_display_t display, hwc2_config_t config,
483 int32_t intAttribute, int32_t* outValue) {
484 auto attribute = static_cast<HWC2::Attribute>(intAttribute);
485 return callDisplayFunction(device, display, &Display::getAttribute,
486 config, attribute, outValue);
487 }
488
Dan Stoza5df2a862016-03-24 16:19:37 -0700489 static int32_t setColorTransformHook(hwc2_device_t* device,
490 hwc2_display_t display, const float* /*matrix*/,
491 int32_t /*android_color_transform_t*/ intHint) {
492 // We intentionally throw away the matrix, because if the hint is
493 // anything other than IDENTITY, we have to fall back to client
494 // composition anyway
495 auto hint = static_cast<android_color_transform_t>(intHint);
496 return callDisplayFunction(device, display, &Display::setColorTransform,
497 hint);
498 }
499
Michael Wright28f24d02016-07-12 13:30:53 -0700500 static int32_t setColorModeHook(hwc2_device_t* device,
501 hwc2_display_t display, int32_t /*android_color_mode_t*/ intMode) {
502 auto mode = static_cast<android_color_mode_t>(intMode);
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800503 return callDisplayFunction(device, display, &Display::setColorMode,
504 mode);
Michael Wright28f24d02016-07-12 13:30:53 -0700505 }
506
Dan Stozac6998d22015-09-24 17:03:36 -0700507 static int32_t setPowerModeHook(hwc2_device_t* device,
508 hwc2_display_t display, int32_t intMode) {
509 auto mode = static_cast<HWC2::PowerMode>(intMode);
510 return callDisplayFunction(device, display, &Display::setPowerMode,
511 mode);
512 }
513
514 static int32_t setVsyncEnabledHook(hwc2_device_t* device,
515 hwc2_display_t display, int32_t intEnabled) {
516 auto enabled = static_cast<HWC2::Vsync>(intEnabled);
517 return callDisplayFunction(device, display, &Display::setVsyncEnabled,
518 enabled);
519 }
520
Dan Stozac6998d22015-09-24 17:03:36 -0700521 class Layer {
522 public:
Chih-Hung Hsieh342b7602016-09-01 11:34:16 -0700523 explicit Layer(Display& display);
Dan Stozac6998d22015-09-24 17:03:36 -0700524
525 bool operator==(const Layer& other) { return mId == other.mId; }
526 bool operator!=(const Layer& other) { return !(*this == other); }
527
528 hwc2_layer_t getId() const { return mId; }
529 Display& getDisplay() const { return mDisplay; }
530
Dan Stozac6998d22015-09-24 17:03:36 -0700531 // HWC2 Layer functions
532 HWC2::Error setBuffer(buffer_handle_t buffer, int32_t acquireFence);
533 HWC2::Error setCursorPosition(int32_t x, int32_t y);
534 HWC2::Error setSurfaceDamage(hwc_region_t damage);
535
536 // HWC2 Layer state functions
537 HWC2::Error setBlendMode(HWC2::BlendMode mode);
538 HWC2::Error setColor(hwc_color_t color);
539 HWC2::Error setCompositionType(HWC2::Composition type);
Dan Stoza5df2a862016-03-24 16:19:37 -0700540 HWC2::Error setDataspace(android_dataspace_t dataspace);
Dan Stozac6998d22015-09-24 17:03:36 -0700541 HWC2::Error setDisplayFrame(hwc_rect_t frame);
542 HWC2::Error setPlaneAlpha(float alpha);
543 HWC2::Error setSidebandStream(const native_handle_t* stream);
544 HWC2::Error setSourceCrop(hwc_frect_t crop);
545 HWC2::Error setTransform(HWC2::Transform transform);
546 HWC2::Error setVisibleRegion(hwc_region_t visible);
547 HWC2::Error setZ(uint32_t z);
548
549 HWC2::Composition getCompositionType() const {
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800550 return mCompositionType;
Dan Stozac6998d22015-09-24 17:03:36 -0700551 }
552 uint32_t getZ() const { return mZ; }
553
554 void addReleaseFence(int fenceFd);
Fabien Sanglardc8591472017-03-07 10:10:03 -0800555 const sp<MiniFence>& getReleaseFence() const;
Dan Stozac6998d22015-09-24 17:03:36 -0700556
557 void setHwc1Id(size_t id) { mHwc1Id = id; }
558 size_t getHwc1Id() const { return mHwc1Id; }
559
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800560 // Write state to HWC1 communication struct.
561 void applyState(struct hwc_layer_1& hwc1Layer);
Dan Stozac6998d22015-09-24 17:03:36 -0700562
563 std::string dump() const;
564
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800565 std::size_t getNumVisibleRegions() { return mVisibleRegion.size(); }
566
567 std::size_t getNumSurfaceDamages() { return mSurfaceDamage.size(); }
568
569 // True if a layer cannot be properly rendered by the device due
570 // to usage of SolidColor (a.k.a BackgroundColor in HWC1).
571 bool hasUnsupportedBackgroundColor() {
572 return (mCompositionType == HWC2::Composition::SolidColor &&
573 !mDisplay.getDevice().supportsBackgroundColor());
574 }
Dan Stozac6998d22015-09-24 17:03:36 -0700575 private:
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800576 void applyCommonState(struct hwc_layer_1& hwc1Layer);
577 void applySolidColorState(struct hwc_layer_1& hwc1Layer);
578 void applySidebandState(struct hwc_layer_1& hwc1Layer);
Dan Stozac6998d22015-09-24 17:03:36 -0700579 void applyBufferState(struct hwc_layer_1& hwc1Layer);
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800580 void applyCompositionType(struct hwc_layer_1& hwc1Layer);
Dan Stozac6998d22015-09-24 17:03:36 -0700581
582 static std::atomic<hwc2_layer_t> sNextId;
583 const hwc2_layer_t mId;
584 Display& mDisplay;
Dan Stozac6998d22015-09-24 17:03:36 -0700585
586 FencedBuffer mBuffer;
587 std::vector<hwc_rect_t> mSurfaceDamage;
588
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800589 HWC2::BlendMode mBlendMode;
590 hwc_color_t mColor;
591 HWC2::Composition mCompositionType;
592 hwc_rect_t mDisplayFrame;
593 float mPlaneAlpha;
594 const native_handle_t* mSidebandStream;
595 hwc_frect_t mSourceCrop;
596 HWC2::Transform mTransform;
597 std::vector<hwc_rect_t> mVisibleRegion;
598
Dan Stozac6998d22015-09-24 17:03:36 -0700599 uint32_t mZ;
600
601 DeferredFence mReleaseFence;
602
603 size_t mHwc1Id;
604 bool mHasUnsupportedPlaneAlpha;
605 };
606
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800607 // Utility tempate calling a Layer object method based on ID parameters:
608 // hwc2_display_t displayId
609 // and
610 // hwc2_layer_t layerId
Dan Stozac6998d22015-09-24 17:03:36 -0700611 template <typename ...Args>
612 static int32_t callLayerFunction(hwc2_device_t* device,
613 hwc2_display_t displayId, hwc2_layer_t layerId,
614 HWC2::Error (Layer::*member)(Args...), Args... args) {
615 auto result = getAdapter(device)->getLayer(displayId, layerId);
616 auto error = std::get<HWC2::Error>(result);
617 if (error == HWC2::Error::None) {
618 auto layer = std::get<Layer*>(result);
619 error = ((*layer).*member)(std::forward<Args>(args)...);
620 }
621 return static_cast<int32_t>(error);
622 }
623
624 template <typename MF, MF memFunc, typename ...Args>
625 static int32_t layerHook(hwc2_device_t* device, hwc2_display_t displayId,
626 hwc2_layer_t layerId, Args... args) {
627 return HWC2On1Adapter::callLayerFunction(device, displayId, layerId,
628 memFunc, std::forward<Args>(args)...);
629 }
630
631 // Layer state functions
632
633 static int32_t setLayerBlendModeHook(hwc2_device_t* device,
634 hwc2_display_t display, hwc2_layer_t layer, int32_t intMode) {
635 auto mode = static_cast<HWC2::BlendMode>(intMode);
636 return callLayerFunction(device, display, layer,
637 &Layer::setBlendMode, mode);
638 }
639
640 static int32_t setLayerCompositionTypeHook(hwc2_device_t* device,
641 hwc2_display_t display, hwc2_layer_t layer, int32_t intType) {
642 auto type = static_cast<HWC2::Composition>(intType);
643 return callLayerFunction(device, display, layer,
644 &Layer::setCompositionType, type);
645 }
646
Dan Stoza5df2a862016-03-24 16:19:37 -0700647 static int32_t setLayerDataspaceHook(hwc2_device_t* device,
648 hwc2_display_t display, hwc2_layer_t layer, int32_t intDataspace) {
649 auto dataspace = static_cast<android_dataspace_t>(intDataspace);
650 return callLayerFunction(device, display, layer, &Layer::setDataspace,
651 dataspace);
652 }
653
Dan Stozac6998d22015-09-24 17:03:36 -0700654 static int32_t setLayerTransformHook(hwc2_device_t* device,
655 hwc2_display_t display, hwc2_layer_t layer, int32_t intTransform) {
656 auto transform = static_cast<HWC2::Transform>(intTransform);
657 return callLayerFunction(device, display, layer, &Layer::setTransform,
658 transform);
659 }
660
661 static int32_t setLayerZOrderHook(hwc2_device_t* device,
662 hwc2_display_t display, hwc2_layer_t layer, uint32_t z) {
663 return callDisplayFunction(device, display, &Display::updateLayerZ,
664 layer, z);
665 }
666
667 // Adapter internals
668
669 void populateCapabilities();
670 Display* getDisplay(hwc2_display_t id);
671 std::tuple<Layer*, HWC2::Error> getLayer(hwc2_display_t displayId,
672 hwc2_layer_t layerId);
673 void populatePrimary();
674
675 bool prepareAllDisplays();
676 std::vector<struct hwc_display_contents_1*> mHwc1Contents;
677 HWC2::Error setAllDisplays();
678
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800679 // Callbacks
Dan Stozac6998d22015-09-24 17:03:36 -0700680 void hwc1Invalidate();
681 void hwc1Vsync(int hwc1DisplayId, int64_t timestamp);
682 void hwc1Hotplug(int hwc1DisplayId, int connected);
683
684 // These are set in the constructor and before any asynchronous events are
685 // possible
686
687 struct hwc_composer_device_1* const mHwc1Device;
688 const uint8_t mHwc1MinorVersion;
689 bool mHwc1SupportsVirtualDisplays;
Fabien Sanglardeb3db612016-11-18 16:12:31 -0800690 bool mHwc1SupportsBackgroundColor;
Dan Stozac6998d22015-09-24 17:03:36 -0700691
692 class Callbacks;
693 const std::unique_ptr<Callbacks> mHwc1Callbacks;
694
695 std::unordered_set<HWC2::Capability> mCapabilities;
696
697 // These are only accessed from the main SurfaceFlinger thread (not from
698 // callbacks or dump
699
700 std::map<hwc2_layer_t, std::shared_ptr<Layer>> mLayers;
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800701
702 // A HWC1 supports only one virtual display.
Dan Stozac6998d22015-09-24 17:03:36 -0700703 std::shared_ptr<Display> mHwc1VirtualDisplay;
704
705 // These are potentially accessed from multiple threads, and are protected
Dan Stozafc4e2022016-02-23 11:43:19 -0800706 // by this mutex. This needs to be recursive, since the HWC1 implementation
707 // can call back into the invalidate callback on the same thread that is
708 // calling prepare.
709 std::recursive_timed_mutex mStateMutex;
Dan Stozac6998d22015-09-24 17:03:36 -0700710
711 struct CallbackInfo {
712 hwc2_callback_data_t data;
713 hwc2_function_pointer_t pointer;
714 };
715 std::unordered_map<HWC2::Callback, CallbackInfo> mCallbacks;
716 bool mHasPendingInvalidate;
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800717
718 // There is a small gap between the time the HWC1 module is started and
719 // when the callbacks for vsync and hotplugs are registered by the
720 // HWC2on1Adapter. To prevent losing events they are stored in these arrays
721 // and fed to the callback as soon as possible.
Dan Stozac6998d22015-09-24 17:03:36 -0700722 std::vector<std::pair<int, int64_t>> mPendingVsyncs;
723 std::vector<std::pair<int, int>> mPendingHotplugs;
724
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800725 // Mapping between HWC1 display id and Display objects.
Dan Stozac6998d22015-09-24 17:03:36 -0700726 std::map<hwc2_display_t, std::shared_ptr<Display>> mDisplays;
Fabien Sanglard06e908a2017-02-12 00:08:14 -0800727
728 // Map HWC1 display type (HWC_DISPLAY_PRIMARY, HWC_DISPLAY_EXTERNAL,
729 // HWC_DISPLAY_VIRTUAL) to Display IDs generated by HWC2on1Adapter objects.
Dan Stozac6998d22015-09-24 17:03:36 -0700730 std::unordered_map<int, hwc2_display_t> mHwc1DisplayMap;
731};
732
733} // namespace android
734
735#endif