blob: 627d5ac0800ce6773f984cdfbb2d24a169a0444e [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
19#include <hardware/hwcomposer2.h>
20
Roman Stratiienkoa32f9072022-05-13 12:12:20 +030021#include "bufferinfo/BufferInfoGetter.h"
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020022#include "compositor/LayerData.h"
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020023
24namespace android {
25
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020026class HwcDisplay;
27
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020028class HwcLayer {
29 public:
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020030 explicit HwcLayer(HwcDisplay *parent_display) : parent_(parent_display){};
31
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020032 HWC2::Composition GetSfType() const {
33 return sf_type_;
34 }
35 HWC2::Composition GetValidatedType() const {
36 return validated_type_;
37 }
38 void AcceptTypeChange() {
39 sf_type_ = validated_type_;
40 }
41 void SetValidatedType(HWC2::Composition type) {
42 validated_type_ = type;
43 }
44 bool IsTypeChanged() const {
45 return sf_type_ != validated_type_;
46 }
47
Roman Stratiienkodd214942022-05-03 18:24:49 +030048 bool GetPriorBufferScanOutFlag() const {
49 return prior_buffer_scanout_flag_;
50 }
51
52 void SetPriorBufferScanOutFlag(bool state) {
53 prior_buffer_scanout_flag_ = state;
54 }
55
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020056 uint32_t GetZOrder() const {
57 return z_order_;
58 }
59
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020060 auto &GetLayerData() {
61 return layer_data_;
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020062 }
63
64 // Layer hooks
65 HWC2::Error SetCursorPosition(int32_t /*x*/, int32_t /*y*/);
66 HWC2::Error SetLayerBlendMode(int32_t mode);
67 HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence);
68 HWC2::Error SetLayerColor(hwc_color_t /*color*/);
69 HWC2::Error SetLayerCompositionType(int32_t type);
70 HWC2::Error SetLayerDataspace(int32_t dataspace);
71 HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
72 HWC2::Error SetLayerPlaneAlpha(float alpha);
73 HWC2::Error SetLayerSidebandStream(const native_handle_t *stream);
74 HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
75 HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
76 HWC2::Error SetLayerTransform(int32_t transform);
77 HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
78 HWC2::Error SetLayerZOrder(uint32_t order);
79
80 private:
81 // sf_type_ stores the initial type given to us by surfaceflinger,
82 // validated_type_ stores the type after running ValidateDisplay
83 HWC2::Composition sf_type_ = HWC2::Composition::Invalid;
84 HWC2::Composition validated_type_ = HWC2::Composition::Invalid;
85
Roman Stratiienko03fd35c2022-01-04 14:30:37 +020086 uint32_t z_order_ = 0;
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020087 LayerData layer_data_;
88
89 /* Should be populated to layer_data_.acquire_fence only before presenting */
90 UniqueFd acquire_fence_;
91
92 /* The following buffer data can have 2 sources:
93 * 1 - Mapper@4 metadata API
94 * 2 - HWC@2 API
95 * We keep ability to have 2 sources in drm_hwc. It may be useful for CLIENT
96 * layer, at this moment HWC@2 API can't specify blending mode for this layer,
97 * but Mapper@4 can do that
98 */
99 BufferColorSpace color_space_{};
100 BufferSampleRange sample_range_{};
101 BufferBlendMode blend_mode_{};
102 buffer_handle_t buffer_handle_{};
103 bool buffer_handle_updated_{};
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200104
Roman Stratiienkodd214942022-05-03 18:24:49 +0300105 bool prior_buffer_scanout_flag_{};
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200106
Roman Stratiienko4b2cc482022-02-21 14:53:58 +0200107 HwcDisplay *const parent_;
108
109 /* Layer state */
110 public:
111 void PopulateLayerData(bool test);
112
113 bool IsLayerUsableAsDevice() const {
114 return !bi_get_failed_ && !fb_import_failed_ && buffer_handle_ != nullptr;
115 }
116
117 private:
118 void ImportFb();
119 bool bi_get_failed_{};
120 bool fb_import_failed_{};
Roman Stratiienkoa32f9072022-05-13 12:12:20 +0300121
122 /* SwapChain Cache */
123 public:
124 void SwChainClearCache();
125
126 private:
127 struct SwapChainElement {
128 std::optional<BufferInfo> bi;
129 std::shared_ptr<DrmFbIdHandle> fb;
130 };
131
132 bool SwChainGetBufferFromCache(BufferUniqueId unique_id);
133 void SwChainReassemble(BufferUniqueId unique_id);
134 void SwChainAddCurrentBuffer(BufferUniqueId unique_id);
135
136 std::map<int /*seq_no*/, SwapChainElement> swchain_cache_;
137 std::map<BufferUniqueId, int /*seq_no*/> swchain_lookup_table_;
138 bool swchain_reassembled_{};
Roman Stratiienko03fd35c2022-01-04 14:30:37 +0200139};
140
141} // namespace android