• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <atomic>
20 #include "draw/pen.h"
21 #include "image/bitmap.h"
22 #include "metadata_helper.h"
23 #include "params/rs_surface_render_params.h"
24 #include "pipeline/rs_paint_filter_canvas.h"
25 #include "pipeline/rs_surface_handler.h"
26 #include "pipeline/rs_surface_render_node.h"
27 #include "pixel_map.h"
28 #include "screen_manager/rs_screen_manager.h"
29 #include "sync_fence.h"
30 #include "utils/matrix.h"
31 #include "utils/rect.h"
32 #include <vector>
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     float sdrNit { 0.0f };
52     float displayNit { 0.0f };
53     float brightnessRatio { 0.0f };
54     std::vector<float> layerLinearMatrix;
55 };
56 #ifdef RS_ENABLE_GPU
57 class RSSurfaceRenderParams;
58 #endif
59 class RSTransactionData;
60 #ifdef USE_VIDEO_PROCESSING_ENGINE
61 constexpr float DEFAULT_SCREEN_LIGHT_NITS = 500;
62 constexpr float DEFAULT_BRIGHTNESS_RATIO = 1.0f;
63 #endif
64 constexpr uint64_t CONSUME_DIRECTLY = 0;
65 struct BufferDrawParam {
66     sptr<OHOS::SurfaceBuffer> buffer;
67     sptr<SyncFence> acquireFence = SyncFence::InvalidFence();
68 
69     Drawing::Matrix matrix; // for moving canvas to layer(surface)'s leftTop point.
70     Drawing::Rect srcRect; // surface's bufferSize
71     Drawing::Rect dstRect; // surface's boundsSize
72     bool hasCropMetadata = false;
73     HDI::Display::Graphic::Common::V1_0::BufferHandleMetaRegion metaRegion;
74 
75     Vector4f cornerRadius;
76     RRect clipRRect;
77 
78     Drawing::Rect clipRect;
79 
80     bool isNeedClip = true;
81     Drawing::Brush paint;
82     Drawing::ColorQuad backgroundColor = Drawing::Color::COLOR_TRANSPARENT;
83     GraphicColorGamut targetColorGamut = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB;
84 
85     bool useCPU = false;
86     bool isMirror = false;
87     bool setColorFilter = true;
88     bool useBilinearInterpolation = false;
89     ScreenId screenId = INVALID_SCREEN_ID;
90     std::vector<GraphicHDRMetaData> metaDatas = {}; // static meta datas for HDR10
91     GraphicHDRMetaDataSet metaDataSet; // dynamic meta datas for HDR10+, HDR VIVID
92     uint32_t threadIndex = UNI_MAIN_THREAD_INDEX; // use to decide eglimage unmap thread index
93 #ifdef USE_VIDEO_PROCESSING_ENGINE
94     float sdrNits = DEFAULT_SCREEN_LIGHT_NITS;
95     float tmoNits = DEFAULT_SCREEN_LIGHT_NITS;
96     float displayNits = DEFAULT_SCREEN_LIGHT_NITS;
97     float brightnessRatio = DEFAULT_BRIGHTNESS_RATIO;
98     std::vector<float> layerLinearMatrix;
99     bool isHdrRedraw = false;
100     bool isTmoNitsFixed = false;
101     bool hasMetadata = false; // SDR has metadata
102 #endif
103     bool isHeterog = false;
104     bool colorFollow = false;
105     bool preRotation = false;
106     Drawing::AlphaType alphaType = Drawing::AlphaType::ALPHATYPE_PREMUL;
107 };
108 
109 using WriteToPngParam = struct {
110     uint32_t width;
111     uint32_t height;
112     uint32_t stride;
113     uint32_t bitDepth;
114     const uint8_t *data;
115 };
116 
117 enum class ColorFilterMode {
118     INVERT_COLOR_DISABLE_MODE = 0,
119     INVERT_COLOR_ENABLE_MODE = 1,
120     DALTONIZATION_PROTANOMALY_MODE = 2,
121     DALTONIZATION_DEUTERANOMALY_MODE = 4,
122     DALTONIZATION_TRITANOMALY_MODE = 8,
123     INVERT_DALTONIZATION_PROTANOMALY_MODE = 3,
124     INVERT_DALTONIZATION_DEUTERANOMALY_MODE = 5,
125     INVERT_DALTONIZATION_TRITANOMALY_MODE = 9,
126     DALTONIZATION_NORMAL_MODE = 16,
127     COLOR_FILTER_END = 32,
128 };
129 
130 constexpr int32_t ROUND_ANGLE = 360;
131 
132 class RSBaseRenderUtil {
133 public:
134     static bool IsNeedClient(RSRenderNode& node, const ComposeInfo& info);
135     static void SetNeedClient(bool flag);
136     static bool IsBufferValid(const sptr<SurfaceBuffer>& buffer);
137     static BufferRequestConfig GetFrameBufferRequestConfig(const ScreenInfo& screenInfo, bool isProtected = false,
138         GraphicColorGamut colorGamut = GRAPHIC_COLOR_GAMUT_SRGB,
139         GraphicPixelFormat pixelFormat = GRAPHIC_PIXEL_FMT_RGBA_8888);
140 
141     static GraphicTransformType GetSurfaceBufferTransformType(
142         const sptr<IConsumerSurface>& consumer, const sptr<SurfaceBuffer>& buffer);
143     static Drawing::Matrix GetSurfaceTransformMatrix(GraphicTransformType rotationTransform, const RectF &bounds,
144         const RectF &bufferBounds = {0.0f, 0.0f, 0.0f, 0.0f});
145     static Drawing::Matrix GetSurfaceTransformMatrixForRotationFixed(GraphicTransformType rotationTransform,
146         const RectF &bounds, const RectF &bufferBounds = {0.0f, 0.0f, 0.0f, 0.0f}, Gravity gravity = Gravity::RESIZE);
147     static Drawing::Matrix GetGravityMatrix(Gravity gravity, const RectF& bufferSize, const RectF& bounds);
148 
149     static void SetPropertiesForCanvas(RSPaintFilterCanvas& canvas, const BufferDrawParam& params);
150     static Drawing::ColorType GetColorTypeFromBufferFormat(int32_t pixelFmt);
151     static Drawing::BitmapFormat GenerateDrawingBitmapFormat(const sptr<OHOS::SurfaceBuffer>& buffer,
152         const Drawing::AlphaType alphaType = Drawing::AlphaType::ALPHATYPE_PREMUL);
153 
154     static GSError DropFrameProcess(RSSurfaceHandler& surfaceHandler, uint64_t presentWhen = 0);
155     static bool ConsumeAndUpdateBuffer(RSSurfaceHandler& surfaceHandler, uint64_t presentWhen = CONSUME_DIRECTLY,
156         bool dropFrameByPidEnable = false, uint64_t parentNodeId = 0, bool deleteCacheDisable = false);
157     static bool ReleaseBuffer(RSSurfaceHandler& surfaceHandler);
158 
159     static std::unique_ptr<RSTransactionData> ParseTransactionData(MessageParcel& parcel, uint32_t parcelNumber);
160 
161     static bool ConvertBufferToBitmap(sptr<SurfaceBuffer> buffer, std::vector<uint8_t>& newBuffer,
162         GraphicColorGamut dstGamut, Drawing::Bitmap& bitmap, const std::vector<GraphicHDRMetaData>& metaDatas = {});
163     /**
164      * @brief Set the Color Filter Mode To Paint object
165      *
166      * @param colorFilterMode SkBlendMode applied to SKPaint
167      * @param paint color matrix applied to SKPaint
168      * @param brightnessRatio hdr brightness ratio
169      */
170     static void SetColorFilterModeToPaint(ColorFilterMode colorFilterMode, Drawing::Brush& paint,
171         float hdrBrightnessRatio = 1.f);
172     static bool IsColorFilterModeValid(ColorFilterMode mode);
173 
174     static bool WriteSurfaceRenderNodeToPng(const RSSurfaceRenderNode& node);
175     static bool WriteSurfaceBufferToPng(sptr<SurfaceBuffer>& buffer, uint64_t id = 0);
176 
177     static bool WritePixelMapToPng(Media::PixelMap& pixelMap);
178     static int32_t GetScreenRotationOffset(RSSurfaceRenderParams* nodeParams);
179 #ifdef RS_ENABLE_GPU
180     static void DealWithSurfaceRotationAndGravity(GraphicTransformType transform, Gravity gravity,
181         RectF& localBounds, BufferDrawParam& params, RSSurfaceRenderParams* nodeParams = nullptr);
182 #endif
183     static void FlipMatrix(GraphicTransformType transform, BufferDrawParam& params);
184 
185     // GraphicTransformType has two attributes: rotation and flip, it take out one of the attributes separately
186     static GraphicTransformType GetRotateTransform(GraphicTransformType transform);
187     static GraphicTransformType GetFlipTransform(GraphicTransformType transform);
188 
189     static int RotateEnumToInt(ScreenRotation rotation);
190     static int RotateEnumToInt(GraphicTransformType rotation);
191     static GraphicTransformType RotateEnumToInt(int angle,
192         GraphicTransformType flip = GraphicTransformType::GRAPHIC_ROTATE_NONE);
193     static Rect MergeBufferDamages(const std::vector<Rect>& damages);
194     static void MergeBufferDamages(Rect& surfaceDamage, const std::vector<Rect>& damages);
195     static bool WriteCacheImageRenderNodeToPng(std::shared_ptr<Drawing::Surface> surface, std::string debugInfo);
196     static bool WriteCacheImageRenderNodeToPng(std::shared_ptr<Drawing::Image> image, std::string debugInfo);
197     static bool WriteCacheImageRenderNodeToPng(std::shared_ptr<Drawing::Bitmap> bitmap, std::string debugInfo);
198 
199     static int GetAccumulatedBufferCount();
200     static void IncAcquiredBufferCount();
201     static void DecAcquiredBufferCount();
202     static pid_t GetLastSendingPid();
203 
204 private:
205     static bool CreateYuvToRGBABitMap(sptr<OHOS::SurfaceBuffer> buffer, std::vector<uint8_t>& newBuffer,
206         Drawing::Bitmap& bitmap);
207     static bool CreateNewColorGamutBitmap(sptr<OHOS::SurfaceBuffer> buffer, std::vector<uint8_t>& newBuffer,
208         Drawing::Bitmap& bitmap, GraphicColorGamut srcGamut, GraphicColorGamut dstGamut,
209         const std::vector<GraphicHDRMetaData>& metaDatas = {});
210     static bool CreateBitmap(sptr<OHOS::SurfaceBuffer> buffer, Drawing::Bitmap& bitmap);
211     static bool WriteToPng(const std::string &filename, const WriteToPngParam &param);
212     static ScreenId GetScreenIdFromSurfaceRenderParams(RSSurfaceRenderParams* nodeParams);
213 
214     static bool enableClient;
215 
216     static inline std::atomic<int> acquiredBufferCount_ = 0;
217     static pid_t lastSendingPid_;
218 };
219 } // namespace Rosen
220 } // namespace OHOS
221 #endif // RENDER_SERVICE_CORE_PIPELINE_RS_BASE_RENDER_UTIL_H
222