1 /*
2  * Copyright (c) 2014-2021, The Linux Foundation. All rights reserved.
3  * Not a Contribution.
4  *
5  * Copyright 2015 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef __HWC_LAYERS_H__
21 #define __HWC_LAYERS_H__
22 
23 /* This class translates HWC2 Layer functions to the SDM LayerStack
24  */
25 
26 #include <gralloc_priv.h>
27 #include <qdMetaData.h>
28 #include <core/layer_stack.h>
29 #include <core/layer_buffer.h>
30 #include <utils/utils.h>
31 #define HWC2_INCLUDE_STRINGIFICATION
32 #define HWC2_USE_CPP11
33 #include <hardware/hwcomposer2.h>
34 #undef HWC2_INCLUDE_STRINGIFICATION
35 #undef HWC2_USE_CPP11
36 #include <android/hardware/graphics/composer/2.3/IComposerClient.h>
37 #include <vendor/qti/hardware/display/composer/3.0/IQtiComposerClient.h>
38 
39 #include <deque>
40 #include <map>
41 #include <set>
42 
43 #include "core/buffer_allocator.h"
44 #include "hwc_buffer_allocator.h"
45 
46 using PerFrameMetadataKey =
47     android::hardware::graphics::composer::V2_3::IComposerClient::PerFrameMetadataKey;
48 using vendor::qti::hardware::display::composer::V3_0::IQtiComposerClient;
49 using android::hardware::graphics::common::V1_2::Dataspace;
50 
51 namespace sdm {
52 
53 DisplayError SetCSC(const private_handle_t *pvt_handle, ColorMetaData *color_metadata);
54 bool GetColorPrimary(const int32_t &dataspace, ColorPrimaries *color_primary);
55 bool GetTransfer(const int32_t &dataspace, GammaTransfer *gamma_transfer);
56 bool GetRange(const int32_t &dataspace, ColorRange *color_range);
57 bool GetSDMColorSpace(const int32_t &dataspace, ColorMetaData *color_metadata);
58 bool IsBT2020(const ColorPrimaries &color_primary);
59 int32_t TranslateFromLegacyDataspace(const int32_t &legacy_ds);
60 DisplayError ColorMetadataToDataspace(ColorMetaData color_metadata, Dataspace *dataspace);
61 
62 enum GeometryChanges {
63   kNone         = 0x000,
64   kBlendMode    = 0x001,
65   kDataspace    = 0x002,
66   kDisplayFrame = 0x004,
67   kPlaneAlpha   = 0x008,
68   kSourceCrop   = 0x010,
69   kTransform    = 0x020,
70   kZOrder       = 0x040,
71   kAdded        = 0x080,
72   kRemoved      = 0x100,
73   kBufferGeometry = 0x200,
74 };
75 
76 enum LayerTypes {
77   kLayerUnknown = 0,
78   kLayerApp = 1,
79   kLayerGame = 2,
80   kLayerBrowser = 3,
81 };
82 
83 class HWCLayer {
84  public:
85   explicit HWCLayer(hwc2_display_t display_id, HWCBufferAllocator *buf_allocator);
86   ~HWCLayer();
GetZ()87   uint32_t GetZ() const { return z_; }
GetId()88   hwc2_layer_t GetId() const { return id_; }
GetType()89   LayerTypes GetType() const { return type_; }
GetSDMLayer()90   Layer *GetSDMLayer() { return layer_; }
91   void ResetPerFrameData();
92 
93   HWC2::Error SetLayerBlendMode(HWC2::BlendMode mode);
94   HWC2::Error SetLayerBuffer(buffer_handle_t buffer, shared_ptr<Fence> acquire_fence);
95   HWC2::Error SetLayerColor(hwc_color_t color);
96   HWC2::Error SetLayerCompositionType(HWC2::Composition type);
97   HWC2::Error SetLayerDataspace(int32_t dataspace);
98   HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
99   HWC2::Error SetCursorPosition(int32_t x, int32_t y);
100   HWC2::Error SetLayerPlaneAlpha(float alpha);
101   HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
102   HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
103   HWC2::Error SetLayerTransform(HWC2::Transform transform);
104   HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
105   HWC2::Error SetLayerPerFrameMetadata(uint32_t num_elements, const PerFrameMetadataKey *keys,
106                                        const float *metadata);
107   HWC2::Error SetLayerPerFrameMetadataBlobs(uint32_t num_elements, const PerFrameMetadataKey *keys,
108                                             const uint32_t *sizes, const uint8_t* metadata);
109   HWC2::Error SetLayerZOrder(uint32_t z);
110   HWC2::Error SetLayerType(IQtiComposerClient::LayerType type);
111   HWC2::Error SetLayerColorTransform(const float *matrix);
112   void SetComposition(const LayerComposition &sdm_composition);
GetClientRequestedCompositionType()113   HWC2::Composition GetClientRequestedCompositionType() { return client_requested_; }
UpdateClientCompositionType(HWC2::Composition type)114   void UpdateClientCompositionType(HWC2::Composition type) { client_requested_ = type; }
GetDeviceSelectedCompositionType()115   HWC2::Composition GetDeviceSelectedCompositionType() { return device_selected_; }
GetLayerDataspace()116   int32_t GetLayerDataspace() { return dataspace_; }
GetGeometryChanges()117   uint32_t GetGeometryChanges() { return geometry_changes_; }
ResetGeometryChanges()118   void ResetGeometryChanges() { geometry_changes_ = GeometryChanges::kNone; }
119   void PushBackReleaseFence(const shared_ptr<Fence> &fence);
120   void PopBackReleaseFence(shared_ptr<Fence> *fence);
121   void PopFrontReleaseFence(shared_ptr<Fence> *fence);
ResetValidation()122   void ResetValidation() { layer_->update_mask.reset(); }
NeedsValidation()123   bool NeedsValidation() { return (geometry_changes_ || layer_->update_mask.any()); }
IsSingleBuffered()124   bool IsSingleBuffered() { return single_buffer_; }
125   bool IsScalingPresent();
126   bool IsRotationPresent();
127   bool IsDataSpaceSupported();
IsProtected()128   bool IsProtected() { return secure_; }
129   static LayerBufferFormat GetSDMFormat(const int32_t &source, const int flags);
IsSurfaceUpdated()130   bool IsSurfaceUpdated() { return surface_updated_; }
SetPartialUpdate(bool enabled)131   void SetPartialUpdate(bool enabled) { partial_update_enabled_ = enabled; }
IsNonIntegralSourceCrop()132   bool IsNonIntegralSourceCrop() { return non_integral_source_crop_; }
HasMetaDataRefreshRate()133   bool HasMetaDataRefreshRate() { return has_metadata_refresh_rate_; }
IsColorTransformSet()134   bool IsColorTransformSet() { return color_transform_matrix_set_; }
135   void SetLayerAsMask();
BufferLatched()136   bool BufferLatched() { return buffer_flipped_; }
ResetBufferFlip()137   void ResetBufferFlip() { buffer_flipped_ = false; }
138 
139  private:
140   Layer *layer_ = nullptr;
141   LayerTypes type_ = kLayerUnknown;
142   uint32_t z_ = 0;
143   const hwc2_layer_t id_;
144   const hwc2_display_t display_id_;
145   static std::atomic<hwc2_layer_t> next_id_;
146   std::deque<shared_ptr<Fence>> release_fences_;
147   HWCBufferAllocator *buffer_allocator_ = NULL;
148   int32_t dataspace_ =  HAL_DATASPACE_UNKNOWN;
149   LayerTransform layer_transform_ = {};
150   LayerRect dst_rect_ = {};
151   bool single_buffer_ = false;
152   int buffer_fd_ = -1;
153   bool dataspace_supported_ = false;
154   bool partial_update_enabled_ = false;
155   bool surface_updated_ = true;
156   bool non_integral_source_crop_ = false;
157   bool has_metadata_refresh_rate_ = false;
158   bool color_transform_matrix_set_ = false;
159   bool buffer_flipped_ = false;
160   bool secure_ = false;
161 
162   // Composition requested by client(SF)
163   HWC2::Composition client_requested_ = HWC2::Composition::Device;
164   // Composition selected by SDM
165   HWC2::Composition device_selected_ = HWC2::Composition::Device;
166   uint32_t geometry_changes_ = GeometryChanges::kNone;
167 
168   void SetRect(const hwc_rect_t &source, LayerRect *target);
169   void SetRect(const hwc_frect_t &source, LayerRect *target);
170   uint32_t GetUint32Color(const hwc_color_t &source);
171   void GetUBWCStatsFromMetaData(UBWCStats *cr_stats, UbwcCrStatsVector *cr_vec);
172   DisplayError SetMetaData(const private_handle_t *pvt_handle, Layer *layer);
173   uint32_t RoundToStandardFPS(float fps);
174   void ValidateAndSetCSC(const private_handle_t *handle);
175   void SetDirtyRegions(hwc_region_t surface_damage);
176 };
177 
178 struct SortLayersByZ {
operatorSortLayersByZ179   bool operator()(const HWCLayer *lhs, const HWCLayer *rhs) const {
180     return lhs->GetZ() < rhs->GetZ();
181   }
182 };
183 
184 }  // namespace sdm
185 #endif  // __HWC_LAYERS_H__
186