blob: b8fb959ad382de72d6dd0852a10187ca07d74c57 [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;
Drew Davenportac9681e2024-09-24 12:17:34 -060034 std::optional<BufferColorSpace> color_space;
35 std::optional<BufferSampleRange> sample_range;
Drew Davenport1b0d8b72024-09-24 15:31:38 -060036 std::optional<HWC2::Composition> composition_type;
Drew Davenport22d66b42024-09-24 15:34:57 -060037 std::optional<hwc_rect_t> display_frame;
Drew Davenport07b96f02024-09-24 15:37:12 -060038 std::optional<float> alpha;
Drew Davenport7ab8c182024-09-24 17:04:26 -060039 std::optional<hwc_frect_t> source_crop;
Drew Davenport51c61e42024-09-24 17:13:39 -060040 std::optional<LayerTransform> transform;
Drew Davenport5d679b02024-09-25 10:15:58 -060041 std::optional<uint32_t> z_order;
Drew Davenporta241a772024-09-24 11:26:30 -060042 };
43
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020044 explicit HwcLayer(HwcDisplay *parent_display) : parent_(parent_display){};
45
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020046 HWC2::Composition GetSfType() const {
47 return sf_type_;
48 }
49 HWC2::Composition GetValidatedType() const {
50 return validated_type_;
51 }
52 void AcceptTypeChange() {
53 sf_type_ = validated_type_;
54 }
55 void SetValidatedType(HWC2::Composition type) {
56 validated_type_ = type;
57 }
58 bool IsTypeChanged() const {
59 return sf_type_ != validated_type_;
60 }
61
Roman Stratiienkodd214942022-05-03 18:24:49 +030062 bool GetPriorBufferScanOutFlag() const {
63 return prior_buffer_scanout_flag_;
64 }
65
66 void SetPriorBufferScanOutFlag(bool state) {
67 prior_buffer_scanout_flag_ = state;
68 }
69
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020070 uint32_t GetZOrder() const {
71 return z_order_;
72 }
73
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020074 auto &GetLayerData() {
75 return layer_data_;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020076 }
77
Drew Davenporta241a772024-09-24 11:26:30 -060078 void SetLayerProperties(const LayerProperties &layer_properties);
79
80 // HWC2 Layer hooks
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020081 HWC2::Error SetCursorPosition(int32_t /*x*/, int32_t /*y*/);
82 HWC2::Error SetLayerBlendMode(int32_t mode);
83 HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence);
84 HWC2::Error SetLayerColor(hwc_color_t /*color*/);
85 HWC2::Error SetLayerCompositionType(int32_t type);
86 HWC2::Error SetLayerDataspace(int32_t dataspace);
87 HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
88 HWC2::Error SetLayerPlaneAlpha(float alpha);
89 HWC2::Error SetLayerSidebandStream(const native_handle_t *stream);
90 HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
91 HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
92 HWC2::Error SetLayerTransform(int32_t transform);
93 HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
94 HWC2::Error SetLayerZOrder(uint32_t order);
95
96 private:
97 // sf_type_ stores the initial type given to us by surfaceflinger,
98 // validated_type_ stores the type after running ValidateDisplay
99 HWC2::Composition sf_type_ = HWC2::Composition::Invalid;
100 HWC2::Composition validated_type_ = HWC2::Composition::Invalid;
101
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200102 uint32_t z_order_ = 0;
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200103 LayerData layer_data_;
104
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200105 /* The following buffer data can have 2 sources:
106 * 1 - Mapper@4 metadata API
107 * 2 - HWC@2 API
108 * We keep ability to have 2 sources in drm_hwc. It may be useful for CLIENT
109 * layer, at this moment HWC@2 API can't specify blending mode for this layer,
110 * but Mapper@4 can do that
111 */
112 BufferColorSpace color_space_{};
113 BufferSampleRange sample_range_{};
114 BufferBlendMode blend_mode_{};
115 buffer_handle_t buffer_handle_{};
116 bool buffer_handle_updated_{};
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200117
Roman Stratiienkodd214942022-05-03 18:24:49 +0300118 bool prior_buffer_scanout_flag_{};
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200119
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200120 HwcDisplay *const parent_;
121
122 /* Layer state */
123 public:
Roman Stratiienko359a9d32023-01-16 17:41:07 +0200124 void PopulateLayerData();
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200125
126 bool IsLayerUsableAsDevice() const {
127 return !bi_get_failed_ && !fb_import_failed_ && buffer_handle_ != nullptr;
128 }
129
130 private:
131 void ImportFb();
132 bool bi_get_failed_{};
133 bool fb_import_failed_{};
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300134
135 /* SwapChain Cache */
136 public:
137 void SwChainClearCache();
138
139 private:
140 struct SwapChainElement {
141 std::optional<BufferInfo> bi;
142 std::shared_ptr<DrmFbIdHandle> fb;
143 };
144
145 bool SwChainGetBufferFromCache(BufferUniqueId unique_id);
146 void SwChainReassemble(BufferUniqueId unique_id);
147 void SwChainAddCurrentBuffer(BufferUniqueId unique_id);
148
149 std::map<int /*seq_no*/, SwapChainElement> swchain_cache_;
150 std::map<BufferUniqueId, int /*seq_no*/> swchain_lookup_table_;
151 bool swchain_reassembled_{};
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200152};
153
154} // namespace android