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