1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef RENDER_SERVICE_CORE_PIPELINE_RS_BASE_RENDER_UTIL_H
17 #define RENDER_SERVICE_CORE_PIPELINE_RS_BASE_RENDER_UTIL_H
18 
19 #include <vector>
20 #include <atomic>
21 #include "image/bitmap.h"
22 #include "params/rs_surface_render_params.h"
23 #include "utils/matrix.h"
24 #include "utils/rect.h"
25 #include "draw/pen.h"
26 
27 #include "screen_manager/rs_screen_manager.h"
28 #include "pipeline/rs_paint_filter_canvas.h"
29 #include "pipeline/rs_surface_handler.h"
30 #include "pipeline/rs_surface_render_node.h"
31 #include "pixel_map.h"
32 #include "sync_fence.h"
33 
34 namespace OHOS {
35 namespace Rosen {
36 struct ComposeInfo {
37     GraphicIRect srcRect;
38     GraphicIRect dstRect;
39     GraphicIRect boundRect;
40     GraphicIRect visibleRect;
41     std::vector<GraphicIRect> dirtyRects;
42     GraphicMatrix matrix;
43     int32_t gravity { 0 };
44     int32_t zOrder { 0 };
45     GraphicLayerAlpha alpha;
46     sptr<SurfaceBuffer> buffer = nullptr;
47     sptr<SurfaceBuffer> preBuffer = nullptr;
48     sptr<SyncFence> fence = SyncFence::InvalidFence();
49     GraphicBlendType blendType = GraphicBlendType::GRAPHIC_BLEND_NONE;
50     bool needClient = false;
51     int32_t sdrNit { 0 };
52     int32_t displayNit { 0 };
53     float brightnessRatio { 0.0 };
54 };
55 
56 class RSSurfaceRenderParams;
57 class RSTransactionData;
58 #ifdef USE_VIDEO_PROCESSING_ENGINE
59 constexpr float DEFAULT_SCREEN_LIGHT_NITS = 500;
60 constexpr float DEFAULT_BRIGHTNESS_RATIO = 1.0f;
61 #endif
62 constexpr uint64_t CONSUME_DIRECTLY = 0;
63 struct BufferDrawParam {
64     sptr<OHOS::SurfaceBuffer> buffer;
65     sptr<SyncFence> acquireFence = SyncFence::InvalidFence();
66 
67     Drawing::Matrix matrix; // for moving canvas to layer(surface)'s leftTop point.
68     Drawing::Rect srcRect; // surface's bufferSize
69     Drawing::Rect dstRect; // surface's boundsSize
70 
71     Vector4f cornerRadius;
72     RRect clipRRect;
73 
74     Drawing::Rect clipRect;
75 
76     bool isNeedClip = true;
77     Drawing::Brush paint;
78     Drawing::ColorQuad backgroundColor = Drawing::Color::COLOR_TRANSPARENT;
79     GraphicColorGamut targetColorGamut = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB;
80 
81     bool useCPU = false;
82     bool isMirror = false;
83     bool setColorFilter = true;
84     bool useBilinearInterpolation = false;
85     ScreenId screenId = INVALID_SCREEN_ID;
86     std::vector<GraphicHDRMetaData> metaDatas = {}; // static meta datas for HDR10
87     GraphicHDRMetaDataSet metaDataSet; // dynamic meta datas for HDR10+, HDR VIVID
88     uint32_t threadIndex = UNI_MAIN_THREAD_INDEX; // use to decide eglimage unmap thread index
89 #ifdef USE_VIDEO_PROCESSING_ENGINE
90     float sdrNits = DEFAULT_SCREEN_LIGHT_NITS;
91     float tmoNits = DEFAULT_SCREEN_LIGHT_NITS;
92     float displayNits = DEFAULT_SCREEN_LIGHT_NITS;
93     float brightnessRatio = DEFAULT_BRIGHTNESS_RATIO;
94     bool isHdrRedraw = false;
95 #endif
96 };
97 
98 using WriteToPngParam = struct {
99     uint32_t width;
100     uint32_t height;
101     uint32_t stride;
102     uint32_t bitDepth;
103     const uint8_t *data;
104 };
105 
106 enum class ColorFilterMode {
107     INVERT_COLOR_DISABLE_MODE = 0,
108     INVERT_COLOR_ENABLE_MODE = 1,
109     DALTONIZATION_PROTANOMALY_MODE = 2,
110     DALTONIZATION_DEUTERANOMALY_MODE = 4,
111     DALTONIZATION_TRITANOMALY_MODE = 8,
112     INVERT_DALTONIZATION_PROTANOMALY_MODE = 3,
113     INVERT_DALTONIZATION_DEUTERANOMALY_MODE = 5,
114     INVERT_DALTONIZATION_TRITANOMALY_MODE = 9,
115     DALTONIZATION_NORMAL_MODE = 16,
116     COLOR_FILTER_END = 32,
117 };
118 
119 class RSBaseRenderUtil {
120 public:
121     static bool IsNeedClient(RSRenderNode& node, const ComposeInfo& info);
122     static void SetNeedClient(bool flag);
123     static bool IsBufferValid(const sptr<SurfaceBuffer>& buffer);
124     static BufferRequestConfig GetFrameBufferRequestConfig(const ScreenInfo& screenInfo, bool isPhysical = true,
125         bool isProtected = false,
126         GraphicColorGamut colorGamut = GRAPHIC_COLOR_GAMUT_SRGB,
127         GraphicPixelFormat pixelFormat = GRAPHIC_PIXEL_FMT_RGBA_8888);
128 
129     static GraphicTransformType GetSurfaceBufferTransformType(
130         const sptr<IConsumerSurface>& consumer, const sptr<SurfaceBuffer>& buffer);
131     static Drawing::Matrix GetSurfaceTransformMatrix(GraphicTransformType rotationTransform, const RectF &bounds,
132         const RectF &bufferBounds = {0.0f, 0.0f, 0.0f, 0.0f}, Gravity gravity = Gravity::RESIZE);
133     static Drawing::Matrix GetSurfaceTransformMatrixForRotationFixed(GraphicTransformType rotationTransform,
134         const RectF &bounds, const RectF &bufferBounds = {0.0f, 0.0f, 0.0f, 0.0f}, Gravity gravity = Gravity::RESIZE);
135     static Drawing::Matrix GetGravityMatrix(Gravity gravity, const RectF& bufferSize, const RectF& bounds);
136 
137     static void SetPropertiesForCanvas(RSPaintFilterCanvas& canvas, const BufferDrawParam& params);
138     static Drawing::ColorType GetColorTypeFromBufferFormat(int32_t pixelFmt);
139     static Drawing::BitmapFormat GenerateDrawingBitmapFormat(const sptr<OHOS::SurfaceBuffer>& buffer);
140 
141     static GSError DropFrameProcess(RSSurfaceHandler& surfaceHandler, uint64_t presentWhen = 0);
142     static bool ConsumeAndUpdateBuffer(RSSurfaceHandler& surfaceHandler,
143         uint64_t presentWhen = CONSUME_DIRECTLY, bool dropFrameByPidEnable = false);
144     static bool ReleaseBuffer(RSSurfaceHandler& surfaceHandler);
145 
146     static std::unique_ptr<RSTransactionData> ParseTransactionData(MessageParcel& parcel);
147 
148     static bool ConvertBufferToBitmap(sptr<SurfaceBuffer> buffer, std::vector<uint8_t>& newBuffer,
149         GraphicColorGamut dstGamut, Drawing::Bitmap& bitmap, const std::vector<GraphicHDRMetaData>& metaDatas = {});
150     /**
151      * @brief Set the Color Filter Mode To Paint object
152      *
153      * @param colorFilterMode SkBlendMode applied to SKPaint
154      * @param paint color matrix applied to SKPaint
155      * @param brightnessRatio hdr brightness ratio
156      */
157     static void SetColorFilterModeToPaint(ColorFilterMode colorFilterMode, Drawing::Brush& paint,
158         float hdrBrightnessRatio = 1.f);
159     static bool IsColorFilterModeValid(ColorFilterMode mode);
160 
161     static bool WriteSurfaceRenderNodeToPng(const RSSurfaceRenderNode& node);
162     static bool WriteCacheRenderNodeToPng(const RSRenderNode& node);
163     static bool WriteSurfaceBufferToPng(sptr<SurfaceBuffer>& buffer, uint64_t id = 0);
164 
165     static bool WritePixelMapToPng(Media::PixelMap& pixelMap);
166     static int32_t GetScreenRotationOffset(RSSurfaceRenderParams* nodeParams);
167     static void DealWithSurfaceRotationAndGravity(GraphicTransformType transform, Gravity gravity,
168         RectF& localBounds, BufferDrawParam& params, RSSurfaceRenderParams* nodeParams = nullptr);
169     static void FlipMatrix(GraphicTransformType transform, BufferDrawParam& params);
170 
171     // GraphicTransformType has two attributes: rotation and flip, it take out one of the attributes separately
172     static GraphicTransformType GetRotateTransform(GraphicTransformType transform);
173     static GraphicTransformType GetFlipTransform(GraphicTransformType transform);
174 
175     // GraphicTransformType from hdi layer info is clockwise, for surface and surface node is anti-clockwise
176     // need conversion here
177     static GraphicTransformType ClockwiseToAntiClockwiseTransform(GraphicTransformType transform);
178     static int RotateEnumToInt(ScreenRotation rotation);
179     static int RotateEnumToInt(GraphicTransformType rotation);
180     static GraphicTransformType RotateEnumToInt(int angle,
181         GraphicTransformType flip = GraphicTransformType::GRAPHIC_ROTATE_NONE);
182     static Rect MergeBufferDamages(const std::vector<Rect>& damages);
183     static bool WriteCacheImageRenderNodeToPng(std::shared_ptr<Drawing::Surface> surface, std::string debugInfo);
184     static bool WriteCacheImageRenderNodeToPng(std::shared_ptr<Drawing::Image> image, std::string debugInfo);
185 
186     static int GetAccumulatedBufferCount();
187     static void IncAcquiredBufferCount();
188     static void DecAcquiredBufferCount();
189     static pid_t GetLastSendingPid();
190 private:
191     static bool CreateYuvToRGBABitMap(sptr<OHOS::SurfaceBuffer> buffer, std::vector<uint8_t>& newBuffer,
192         Drawing::Bitmap& bitmap);
193     static bool CreateNewColorGamutBitmap(sptr<OHOS::SurfaceBuffer> buffer, std::vector<uint8_t>& newBuffer,
194         Drawing::Bitmap& bitmap, GraphicColorGamut srcGamut, GraphicColorGamut dstGamut,
195         const std::vector<GraphicHDRMetaData>& metaDatas = {});
196     static bool CreateBitmap(sptr<OHOS::SurfaceBuffer> buffer, Drawing::Bitmap& bitmap);
197     static bool WriteToPng(const std::string &filename, const WriteToPngParam &param);
198     static ScreenId GetScreenIdFromSurfaceRenderParams(RSSurfaceRenderParams* nodeParams);
199 
200     static bool enableClient;
201 
202     static inline std::atomic<int> acquiredBufferCount_ = 0;
203     static pid_t lastSendingPid_;
204 };
205 } // namespace Rosen
206 } // namespace OHOS
207 #endif // RENDER_SERVICE_CORE_PIPELINE_RS_BASE_RENDER_UTIL_H
208