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 "include/core/SkBitmap.h" 21 #include "include/core/SkCanvas.h" 22 #include "include/core/SkColor.h" 23 #include "include/core/SkMatrix.h" 24 #include "include/core/SkRect.h" 25 #include "include/core/SkColorFilter.h" 26 27 #include "screen_manager/rs_screen_manager.h" 28 #include "pipeline/rs_paint_filter_canvas.h" 29 #include "pipeline/rs_surface_render_node.h" 30 #include "pixel_map.h" 31 #include "sync_fence.h" 32 33 namespace OHOS { 34 namespace Rosen { 35 class RSTransactionData; 36 struct BufferDrawParam { 37 sptr<OHOS::SurfaceBuffer> buffer; 38 sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE; 39 40 SkMatrix matrix; // for moving canvas to layer(surface)'s leftTop point. 41 SkRect srcRect; // surface's bufferSize 42 SkRect dstRect; // surface's boundsSize 43 44 Vector4f cornerRadius; 45 RRect clipRRect; 46 SkRect clipRect; 47 48 bool isNeedClip = true; 49 SkPaint paint; 50 SkColor backgroundColor = SK_ColorTRANSPARENT; 51 ColorGamut targetColorGamut = ColorGamut::COLOR_GAMUT_SRGB; 52 53 bool useCPU = false; 54 bool setColorFilter = true; 55 std::vector<GraphicHDRMetaData> metaDatas = {}; // static meta datas for HDR10 56 GraphicHDRMetaDataSet metaDataSet; // dynamic meta datas for HDR10+, HDR VIVID 57 }; 58 59 using WriteToPngParam = struct { 60 uint32_t width; 61 uint32_t height; 62 uint32_t stride; 63 uint32_t bitDepth; 64 const uint8_t *data; 65 }; 66 67 enum class ColorFilterMode { 68 INVERT_COLOR_DISABLE_MODE = 0, 69 INVERT_COLOR_ENABLE_MODE = 1, 70 DALTONIZATION_PROTANOMALY_MODE = 2, 71 DALTONIZATION_DEUTERANOMALY_MODE = 4, 72 DALTONIZATION_TRITANOMALY_MODE = 8, 73 INVERT_DALTONIZATION_PROTANOMALY_MODE = 3, 74 INVERT_DALTONIZATION_DEUTERANOMALY_MODE = 5, 75 INVERT_DALTONIZATION_TRITANOMALY_MODE = 9, 76 DALTONIZATION_NORMAL_MODE = 16, 77 COLOR_FILTER_END = 32, 78 }; 79 80 class RSBaseRenderUtil { 81 public: 82 static bool IsBufferValid(const sptr<SurfaceBuffer>& buffer); 83 static BufferRequestConfig GetFrameBufferRequestConfig(const ScreenInfo& screenInfo, bool isPhysical = true); 84 85 static SkMatrix GetSurfaceTransformMatrix(const RSSurfaceRenderNode& node, const RectF& bounds); 86 static SkMatrix GetNodeGravityMatrix( 87 const RSSurfaceRenderNode& node, const sptr<SurfaceBuffer>& buffer, const RectF& bounds); 88 static void SetPropertiesForCanvas(RSPaintFilterCanvas& canvas, const BufferDrawParam& params); 89 90 static GSError DropFrameProcess(RSSurfaceHandler& node); 91 static bool ConsumeAndUpdateBuffer(RSSurfaceHandler& surfaceHandler); 92 static bool ReleaseBuffer(RSSurfaceHandler& surfaceHandler); 93 94 static std::unique_ptr<RSTransactionData> ParseTransactionData(MessageParcel& parcel); 95 96 static bool ConvertBufferToBitmap(sptr<SurfaceBuffer> buffer, std::vector<uint8_t>& newBuffer, 97 ColorGamut dstGamut, SkBitmap& bitmap, const std::vector<GraphicHDRMetaData>& metaDatas = {}); 98 /** 99 * @brief Set the Color Filter Mode To Paint object 100 * 101 * @param colorFilterMode SkBlendMode applied to SKPaint 102 * @param paint color matrix applied to SKPaint 103 */ 104 static void SetColorFilterModeToPaint(ColorFilterMode colorFilterMode, SkPaint& paint); 105 static bool IsColorFilterModeValid(ColorFilterMode mode); 106 107 static bool WriteSurfaceRenderNodeToPng(const RSSurfaceRenderNode& node); 108 109 static bool WritePixelMapToPng(Media::PixelMap& pixelMap); 110 static void DealWithSurfaceRotationAndGravity( 111 const RSSurfaceRenderNode& node, RectF& localBounds, BufferDrawParam& params); 112 static void FlipMatrix(const RSSurfaceRenderNode& node, BufferDrawParam& params); 113 114 // GraphicTransformType has two attributes: rotation and flip, it take out one of the attributes separately 115 static GraphicTransformType GetRotateTransform(GraphicTransformType transform); 116 static GraphicTransformType GetFlipTransform(GraphicTransformType transform); 117 private: 118 static bool CreateYuvToRGBABitMap(sptr<OHOS::SurfaceBuffer> buffer, std::vector<uint8_t>& newBuffer, 119 SkBitmap& bitmap); 120 static bool CreateNewColorGamutBitmap(sptr<OHOS::SurfaceBuffer> buffer, std::vector<uint8_t>& newBuffer, 121 SkBitmap& bitmap, ColorGamut srcGamut, ColorGamut dstGamut, const std::vector<GraphicHDRMetaData>& metaDatas = {}); 122 static bool CreateBitmap(sptr<OHOS::SurfaceBuffer> buffer, SkBitmap& bitmap); 123 static bool WriteToPng(const std::string &filename, const WriteToPngParam ¶m); 124 private: 125 }; 126 } // namespace Rosen 127 } // namespace OHOS 128 #endif // RENDER_SERVICE_CORE_PIPELINE_RS_BASE_RENDER_UTIL_H 129