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