• 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 #ifndef USE_ROSEN_DRAWING
21 #include "include/core/SkBitmap.h"
22 #include "include/core/SkCanvas.h"
23 #include "include/core/SkColor.h"
24 #include "include/core/SkMatrix.h"
25 #include "include/core/SkRect.h"
26 #include "include/core/SkColorFilter.h"
27 #else
28 #include "image/bitmap.h"
29 #include "utils/matrix.h"
30 #include "utils/rect.h"
31 #include "draw/pen.h"
32 #endif
33 
34 #include "screen_manager/rs_screen_manager.h"
35 #include "pipeline/rs_paint_filter_canvas.h"
36 #include "pipeline/rs_surface_render_node.h"
37 #include "pipeline/rs_composer_adapter.h"
38 #include "pixel_map.h"
39 #include "sync_fence.h"
40 
41 namespace OHOS {
42 namespace Rosen {
43 class RSTransactionData;
44 struct BufferDrawParam {
45     sptr<OHOS::SurfaceBuffer> buffer;
46     sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
47 
48 #ifndef USE_ROSEN_DRAWING
49     SkMatrix matrix; // for moving canvas to layer(surface)'s leftTop point.
50     SkRect srcRect; // surface's bufferSize
51     SkRect dstRect; // surface's boundsSize
52 #else
53     Drawing::Matrix matrix; // for moving canvas to layer(surface)'s leftTop point.
54     Drawing::Rect srcRect; // surface's bufferSize
55     Drawing::Rect dstRect; // surface's boundsSize
56 #endif
57 
58     Vector4f cornerRadius;
59     RRect clipRRect;
60 
61 #ifndef USE_ROSEN_DRAWING
62     SkRect clipRect;
63 #else
64     Drawing::Rect clipRect;
65 #endif
66 
67     bool isNeedClip = true;
68 #ifndef USE_ROSEN_DRAWING
69     SkPaint paint;
70     SkColor backgroundColor = SK_ColorTRANSPARENT;
71 #else
72     Drawing::Brush paint;
73     Drawing::ColorQuad backgroundColor = Drawing::Color::COLOR_TRANSPARENT;
74 #endif
75     GraphicColorGamut targetColorGamut = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB;
76 
77     bool useCPU = false;
78     bool setColorFilter = true;
79     std::vector<GraphicHDRMetaData> metaDatas = {}; // static meta datas for HDR10
80     GraphicHDRMetaDataSet metaDataSet; // dynamic meta datas for HDR10+, HDR VIVID
81     uint32_t threadIndex = UNI_MAIN_THREAD_INDEX; // use to decide eglimage unmap thread index
82 };
83 
84 using WriteToPngParam = struct {
85     uint32_t width;
86     uint32_t height;
87     uint32_t stride;
88     uint32_t bitDepth;
89     const uint8_t *data;
90 };
91 
92 enum class ColorFilterMode {
93     INVERT_COLOR_DISABLE_MODE = 0,
94     INVERT_COLOR_ENABLE_MODE = 1,
95     DALTONIZATION_PROTANOMALY_MODE = 2,
96     DALTONIZATION_DEUTERANOMALY_MODE = 4,
97     DALTONIZATION_TRITANOMALY_MODE = 8,
98     INVERT_DALTONIZATION_PROTANOMALY_MODE = 3,
99     INVERT_DALTONIZATION_DEUTERANOMALY_MODE = 5,
100     INVERT_DALTONIZATION_TRITANOMALY_MODE = 9,
101     DALTONIZATION_NORMAL_MODE = 16,
102     COLOR_FILTER_END = 32,
103 };
104 
105 class RSBaseRenderUtil {
106 public:
107     static bool IsNeedClient(RSRenderNode& node, const ComposeInfo& info);
108     static void SetNeedClient(bool flag);
109     static bool IsBufferValid(const sptr<SurfaceBuffer>& buffer);
110     static BufferRequestConfig GetFrameBufferRequestConfig(const ScreenInfo& screenInfo, bool isPhysical = true);
111 
112 #ifndef USE_ROSEN_DRAWING
113     static SkMatrix GetSurfaceTransformMatrix(GraphicTransformType rotationTransform, const RectF& bounds);
114     static SkMatrix GetGravityMatrix(Gravity gravity, const sptr<SurfaceBuffer>& buffer, const RectF& bounds);
115 #else
116     static Drawing::Matrix GetSurfaceTransformMatrix(GraphicTransformType rotationTransform, const RectF& bounds);
117     static Drawing::Matrix GetGravityMatrix(Gravity gravity, const sptr<SurfaceBuffer>& buffer, const RectF& bounds);
118 #endif
119     static void SetPropertiesForCanvas(RSPaintFilterCanvas& canvas, const BufferDrawParam& params);
120 
121     static GSError DropFrameProcess(RSSurfaceHandler& node);
122     static bool ConsumeAndUpdateBuffer(RSSurfaceHandler& surfaceHandler);
123     static bool ReleaseBuffer(RSSurfaceHandler& surfaceHandler);
124 
125     static std::unique_ptr<RSTransactionData> ParseTransactionData(MessageParcel& parcel);
126 
127 #ifndef USE_ROSEN_DRAWING
128     static bool ConvertBufferToBitmap(sptr<SurfaceBuffer> buffer, std::vector<uint8_t>& newBuffer,
129         GraphicColorGamut dstGamut, SkBitmap& bitmap, const std::vector<GraphicHDRMetaData>& metaDatas = {});
130 #else
131     static bool ConvertBufferToBitmap(sptr<SurfaceBuffer> buffer, std::vector<uint8_t>& newBuffer,
132         GraphicColorGamut dstGamut, Drawing::Bitmap& bitmap, const std::vector<GraphicHDRMetaData>& metaDatas = {});
133 #endif
134     /**
135      * @brief Set the Color Filter Mode To Paint object
136      *
137      * @param colorFilterMode SkBlendMode applied to SKPaint
138      * @param paint color matrix applied to SKPaint
139      */
140 #ifndef USE_ROSEN_DRAWING
141     static void SetColorFilterModeToPaint(ColorFilterMode colorFilterMode, SkPaint& paint);
142 #else
143     static void SetColorFilterModeToPaint(ColorFilterMode colorFilterMode, Drawing::Brush& paint);
144 #endif
145     static bool IsColorFilterModeValid(ColorFilterMode mode);
146 
147     static bool WriteSurfaceRenderNodeToPng(const RSSurfaceRenderNode& node);
148     static bool WriteCacheRenderNodeToPng(const RSRenderNode& node);
149 
150     static bool WritePixelMapToPng(Media::PixelMap& pixelMap);
151     static void DealWithSurfaceRotationAndGravity(GraphicTransformType transform, Gravity gravity,
152         RectF& localBounds, BufferDrawParam& params);
153     static void FlipMatrix(GraphicTransformType transform, BufferDrawParam& params);
154 
155     // GraphicTransformType has two attributes: rotation and flip, it take out one of the attributes separately
156     static GraphicTransformType GetRotateTransform(GraphicTransformType transform);
157     static GraphicTransformType GetFlipTransform(GraphicTransformType transform);
158 
159     // GraphicTransformType from hdi layer info is clockwise, for surface and surface node is anti-clockwise
160     // need conversion here
161     static GraphicTransformType ClockwiseToAntiClockwiseTransform(GraphicTransformType transform);
162     static int RotateEnumToInt(GraphicTransformType rotation);
163     static GraphicTransformType RotateEnumToInt(int angle,
164         GraphicTransformType flip = GraphicTransformType::GRAPHIC_ROTATE_NONE);
165 
166 private:
167 #ifndef USE_ROSEN_DRAWING
168     static bool CreateYuvToRGBABitMap(sptr<OHOS::SurfaceBuffer> buffer, std::vector<uint8_t>& newBuffer,
169         SkBitmap& bitmap);
170     static bool CreateNewColorGamutBitmap(sptr<OHOS::SurfaceBuffer> buffer, std::vector<uint8_t>& newBuffer,
171                                           SkBitmap& bitmap, GraphicColorGamut srcGamut, GraphicColorGamut dstGamut,
172                                           const std::vector<GraphicHDRMetaData>& metaDatas = {});
173     static bool CreateBitmap(sptr<OHOS::SurfaceBuffer> buffer, SkBitmap& bitmap);
174 #else
175     static bool CreateYuvToRGBABitMap(sptr<OHOS::SurfaceBuffer> buffer, std::vector<uint8_t>& newBuffer,
176         Drawing::Bitmap& bitmap);
177     static bool CreateNewColorGamutBitmap(sptr<OHOS::SurfaceBuffer> buffer, std::vector<uint8_t>& newBuffer,
178         Drawing::Bitmap& bitmap, GraphicColorGamut srcGamut, GraphicColorGamut dstGamut,
179         const std::vector<GraphicHDRMetaData>& metaDatas = {});
180     static bool CreateBitmap(sptr<OHOS::SurfaceBuffer> buffer, Drawing::Bitmap& bitmap);
181 #endif
182     static bool WriteToPng(const std::string &filename, const WriteToPngParam &param);
183     static bool IsForceClient();
184 
185     static bool enableClient;
186 };
187 } // namespace Rosen
188 } // namespace OHOS
189 #endif // RENDER_SERVICE_CORE_PIPELINE_RS_BASE_RENDER_UTIL_H
190