1 /*
2  * Copyright (c) 2014-2020, 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 <deque>
38 #include <map>
39 #include <set>
40 #include "core/buffer_allocator.h"
41 #include "hwc_buffer_allocator.h"
42 
43 using PerFrameMetadataKey =
44     android::hardware::graphics::composer::V2_3::IComposerClient::PerFrameMetadataKey;
45 
46 namespace sdm {
47 
48 DisplayError SetCSC(const private_handle_t *pvt_handle, ColorMetaData *color_metadata);
49 bool GetColorPrimary(const int32_t &dataspace, ColorPrimaries *color_primary);
50 bool GetTransfer(const int32_t &dataspace, GammaTransfer *gamma_transfer);
51 bool GetRange(const int32_t &dataspace, ColorRange *color_range);
52 bool GetSDMColorSpace(const int32_t &dataspace, ColorMetaData *color_metadata);
53 bool IsBT2020(const ColorPrimaries &color_primary);
54 int32_t TranslateFromLegacyDataspace(const int32_t &legacy_ds);
55 
56 enum GeometryChanges {
57   kNone         = 0x000,
58   kBlendMode    = 0x001,
59   kDataspace    = 0x002,
60   kDisplayFrame = 0x004,
61   kPlaneAlpha   = 0x008,
62   kSourceCrop   = 0x010,
63   kTransform    = 0x020,
64   kZOrder       = 0x040,
65   kAdded        = 0x080,
66   kRemoved      = 0x100,
67   kBufferGeometry = 0x200,
68   kConfigChanged = 0x400,
69 };
70 
71 class HWCLayer {
72  public:
73   explicit HWCLayer(hwc2_display_t display_id, HWCBufferAllocator *buf_allocator);
74   ~HWCLayer();
GetZ()75   uint32_t GetZ() const { return z_; }
GetId()76   hwc2_layer_t GetId() const { return id_; }
GetSDMLayer()77   Layer *GetSDMLayer() { return layer_; }
78   void ResetPerFrameData();
79 
80   HWC2::Error SetLayerBlendMode(HWC2::BlendMode mode);
81   HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence);
82   HWC2::Error SetLayerColor(hwc_color_t color);
83   HWC2::Error SetLayerCompositionType(HWC2::Composition type);
84   HWC2::Error SetLayerDataspace(int32_t dataspace);
85   HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
86   HWC2::Error SetCursorPosition(int32_t x, int32_t y);
87   HWC2::Error SetLayerPlaneAlpha(float alpha);
88   HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
89   HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
90   HWC2::Error SetLayerTransform(HWC2::Transform transform);
91   HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
92   HWC2::Error SetLayerPerFrameMetadata(uint32_t num_elements, const PerFrameMetadataKey *keys,
93                                        const float *metadata);
94   HWC2::Error SetLayerPerFrameMetadataBlobs(uint32_t num_elements, const PerFrameMetadataKey *keys,
95                                             const uint32_t *sizes, const uint8_t* metadata);
96   HWC2::Error SetLayerZOrder(uint32_t z);
97   HWC2::Error SetLayerColorTransform(const float *matrix);
98   void SetComposition(const LayerComposition &sdm_composition);
GetClientRequestedCompositionType()99   HWC2::Composition GetClientRequestedCompositionType() { return client_requested_; }
GetOrigClientRequestedCompositionType()100   HWC2::Composition GetOrigClientRequestedCompositionType() { return client_requested_orig_; }
UpdateClientCompositionType(HWC2::Composition type)101   void UpdateClientCompositionType(HWC2::Composition type) { client_requested_ = type; }
GetDeviceSelectedCompositionType()102   HWC2::Composition GetDeviceSelectedCompositionType() { return device_selected_; }
GetLayerDataspace()103   int32_t GetLayerDataspace() { return dataspace_; }
GetGeometryChanges()104   uint32_t GetGeometryChanges() { return geometry_changes_; }
ResetGeometryChanges()105   void ResetGeometryChanges() { geometry_changes_ = GeometryChanges::kNone; }
106   void PushBackReleaseFence(int32_t fence);
107   int32_t PopBackReleaseFence(void);
108   int32_t PopFrontReleaseFence(void);
ResetValidation()109   void ResetValidation() { layer_->update_mask.reset(); }
NeedsValidation()110   bool NeedsValidation() { return (geometry_changes_ || layer_->update_mask.any()); }
IsSingleBuffered()111   bool IsSingleBuffered() { return single_buffer_; }
112   bool IsScalingPresent();
113   bool IsRotationPresent();
114   bool IsDataSpaceSupported();
115   static LayerBufferFormat GetSDMFormat(const int32_t &source, const int flags);
IsSurfaceUpdated()116   bool IsSurfaceUpdated() { return surface_updated_; }
SetPartialUpdate(bool enabled)117   void SetPartialUpdate(bool enabled) { partial_update_enabled_ = enabled; }
IsNonIntegralSourceCrop()118   bool IsNonIntegralSourceCrop() { return non_integral_source_crop_; }
HasMetaDataRefreshRate()119   bool HasMetaDataRefreshRate() { return has_metadata_refresh_rate_; }
IsColorTransformSet()120   bool IsColorTransformSet() const { return color_transform_matrix_set_; }
121   void SetLayerAsMask();
BufferLatched()122   bool BufferLatched() { return buffer_flipped_; }
ResetBufferFlip()123   void ResetBufferFlip() { buffer_flipped_ = false; }
124 
125  private:
126   Layer *layer_ = nullptr;
127   uint32_t z_ = 0;
128   const hwc2_layer_t id_;
129   const hwc2_display_t display_id_;
130   static std::atomic<hwc2_layer_t> next_id_;
131   std::deque<int32_t> release_fences_;
132   HWCBufferAllocator *buffer_allocator_ = NULL;
133   int32_t dataspace_ =  HAL_DATASPACE_UNKNOWN;
134   LayerTransform layer_transform_ = {};
135   LayerRect dst_rect_ = {};
136   bool single_buffer_ = false;
137   int buffer_fd_ = -1;
138   bool dataspace_supported_ = false;
139   bool partial_update_enabled_ = false;
140   bool surface_updated_ = true;
141   bool non_integral_source_crop_ = false;
142   bool has_metadata_refresh_rate_ = false;
143   bool color_transform_matrix_set_ = false;
144   bool buffer_flipped_ = false;
145 
146   // Composition requested by client(SF) Original
147   HWC2::Composition client_requested_orig_ = HWC2::Composition::Device;
148   // Composition requested by client(SF) Modified for internel use
149   HWC2::Composition client_requested_ = HWC2::Composition::Device;
150   // Composition selected by SDM
151   HWC2::Composition device_selected_ = HWC2::Composition::Device;
152   uint32_t geometry_changes_ = GeometryChanges::kNone;
153 
154   void SetRect(const hwc_rect_t &source, LayerRect *target);
155   void SetRect(const hwc_frect_t &source, LayerRect *target);
156   uint32_t GetUint32Color(const hwc_color_t &source);
157   LayerBufferS3DFormat GetS3DFormat(uint32_t s3d_format);
158   void GetUBWCStatsFromMetaData(UBWCStats *cr_stats, UbwcCrStatsVector *cr_vec);
159   DisplayError SetMetaData(const private_handle_t *pvt_handle, Layer *layer);
160   uint32_t RoundToStandardFPS(float fps);
161   void ValidateAndSetCSC(const private_handle_t *handle);
162   void SetDirtyRegions(hwc_region_t surface_damage);
163 };
164 
165 struct SortLayersByZ {
operatorSortLayersByZ166   bool operator()(const HWCLayer *lhs, const HWCLayer *rhs) const {
167     return lhs->GetZ() < rhs->GetZ();
168   }
169 };
170 
171 }  // namespace sdm
172 #endif  // __HWC_LAYERS_H__
173