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