blob: 21e6b58c6be6ef4962e75127db3264cd9363931f [file] [log] [blame]
Roman Stratiienko03fd35c2022-01-04 14:30:37 +02001/*
2 * Copyright (C) 2022 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
Roman Stratiienkobde95662022-12-10 20:27:58 +020017#pragma once
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020018
Drew Davenporta241a772024-09-24 11:26:30 -060019#include <aidl/android/hardware/graphics/common/Transform.h>
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020020#include <hardware/hwcomposer2.h>
21
Roman Stratiienkoa32f9072022-05-13 12:12:20 +030022#include "bufferinfo/BufferInfoGetter.h"
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020023#include "compositor/LayerData.h"
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020024
25namespace android {
26
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020027class HwcDisplay;
28
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020029class HwcLayer {
30 public:
Drew Davenporta241a772024-09-24 11:26:30 -060031 // A set of properties to be validated.
32 struct LayerProperties {
33 std::optional<BufferBlendMode> blend_mode;
34 };
35
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020036 explicit HwcLayer(HwcDisplay *parent_display) : parent_(parent_display){};
37
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020038 HWC2::Composition GetSfType() const {
39 return sf_type_;
40 }
41 HWC2::Composition GetValidatedType() const {
42 return validated_type_;
43 }
44 void AcceptTypeChange() {
45 sf_type_ = validated_type_;
46 }
47 void SetValidatedType(HWC2::Composition type) {
48 validated_type_ = type;
49 }
50 bool IsTypeChanged() const {
51 return sf_type_ != validated_type_;
52 }
53
Roman Stratiienkodd214942022-05-03 18:24:49 +030054 bool GetPriorBufferScanOutFlag() const {
55 return prior_buffer_scanout_flag_;
56 }
57
58 void SetPriorBufferScanOutFlag(bool state) {
59 prior_buffer_scanout_flag_ = state;
60 }
61
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020062 uint32_t GetZOrder() const {
63 return z_order_;
64 }
65
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020066 auto &GetLayerData() {
67 return layer_data_;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020068 }
69
Drew Davenporta241a772024-09-24 11:26:30 -060070 void SetLayerProperties(const LayerProperties &layer_properties);
71
72 // HWC2 Layer hooks
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020073 HWC2::Error SetCursorPosition(int32_t /*x*/, int32_t /*y*/);
74 HWC2::Error SetLayerBlendMode(int32_t mode);
75 HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence);
76 HWC2::Error SetLayerColor(hwc_color_t /*color*/);
77 HWC2::Error SetLayerCompositionType(int32_t type);
78 HWC2::Error SetLayerDataspace(int32_t dataspace);
79 HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
80 HWC2::Error SetLayerPlaneAlpha(float alpha);
81 HWC2::Error SetLayerSidebandStream(const native_handle_t *stream);
82 HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
83 HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
84 HWC2::Error SetLayerTransform(int32_t transform);
85 HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
86 HWC2::Error SetLayerZOrder(uint32_t order);
87
88 private:
89 // sf_type_ stores the initial type given to us by surfaceflinger,
90 // validated_type_ stores the type after running ValidateDisplay
91 HWC2::Composition sf_type_ = HWC2::Composition::Invalid;
92 HWC2::Composition validated_type_ = HWC2::Composition::Invalid;
93
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020094 uint32_t z_order_ = 0;
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020095 LayerData layer_data_;
96
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020097 /* The following buffer data can have 2 sources:
98 * 1 - Mapper@4 metadata API
99 * 2 - HWC@2 API
100 * We keep ability to have 2 sources in drm_hwc. It may be useful for CLIENT
101 * layer, at this moment HWC@2 API can't specify blending mode for this layer,
102 * but Mapper@4 can do that
103 */
104 BufferColorSpace color_space_{};
105 BufferSampleRange sample_range_{};
106 BufferBlendMode blend_mode_{};
107 buffer_handle_t buffer_handle_{};
108 bool buffer_handle_updated_{};
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200109
Roman Stratiienkodd214942022-05-03 18:24:49 +0300110 bool prior_buffer_scanout_flag_{};
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200111
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200112 HwcDisplay *const parent_;
113
114 /* Layer state */
115 public:
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200116 void PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200117
118 bool IsLayerUsableAsDevice() const {
119 return !bi_get_failed_ && !fb_import_failed_ && buffer_handle_ != nullptr;
120 }
121
122 private:
123 void ImportFb();
124 bool bi_get_failed_{};
125 bool fb_import_failed_{};
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300126
127 /* SwapChain Cache */
128 public:
129 void SwChainClearCache();
130
131 private:
132 struct SwapChainElement {
133 std::optional<BufferInfo> bi;
134 std::shared_ptr<DrmFbIdHandle> fb;
135 };
136
137 bool SwChainGetBufferFromCache(BufferUniqueId unique_id);
138 void SwChainReassemble(BufferUniqueId unique_id);
139 void SwChainAddCurrentBuffer(BufferUniqueId unique_id);
140
141 std::map<int /*seq_no*/, SwapChainElement> swchain_cache_;
142 std::map<BufferUniqueId, int /*seq_no*/> swchain_lookup_table_;
143 bool swchain_reassembled_{};
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200144};
145
146} // namespace android