From 16f9acba839eaf44ae43d08096960d2a2a678524 Mon Sep 17 00:00:00 2001 From: pwx1285814 Date: Thu, 26 Sep 2024 14:17:33 +0800 Subject: [PATCH] =?UTF-8?q?CleanCode=E5=91=8A=E8=AD=A6=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: pwx1285814 Change-Id: I2d4311217971965271920f5ac79a44c75924d9cf Signed-off-by: pwx1285814 --- common/include/dscreen_constants.h | 12 +++-- .../imageJpeg/include/jpeg_image_processor.h | 3 +- .../imageJpeg/src/jpeg_image_processor.cpp | 21 +++++---- .../common/utils/src/dscreen_maprelation.cpp | 47 ++++++++++++++----- services/common/utils/src/video_param.cpp | 33 +++++++++---- .../dscreenmgr/1.0/src/dscreen.cpp | 6 +++ .../src/screen_data_channel_impl.cpp | 4 +- 7 files changed, 89 insertions(+), 37 deletions(-) diff --git a/common/include/dscreen_constants.h b/common/include/dscreen_constants.h index 5afa2ae6..57226598 100644 --- a/common/include/dscreen_constants.h +++ b/common/include/dscreen_constants.h @@ -140,6 +140,12 @@ constexpr uint32_t DSCREEN_MAX_SCREEN_DATA_WIDTH = 2560; /* Screen max screen data height */ constexpr uint32_t DSCREEN_MAX_SCREEN_DATA_HEIGHT = 2772; +/* Screen max display data width */ +constexpr uint32_t DSCREEN_MAX_DISPLAY_DATA_WIDTH = 2560; + +/* Screen max display data height */ +constexpr uint32_t DSCREEN_MAX_DISPLAY_DATA_HEIGHT = 2772; + /* Screen dump data max file size */ constexpr uint32_t DUMP_FILE_MAX_SIZE = 295 * 1024 * 1024; @@ -185,7 +191,7 @@ const std::string KEY_ERR_CODE = "errCode"; const std::string KEY_ERR_CONTENT = "errContent"; const std::string KEY_VIDEO_PARAM = "videoParam"; const std::string KEY_MAPRELATION = "mapRelation"; -const std::string KEY_PARTIALREFREAH = "PartialRefresh"; +const std::string KEY_PARTIALREFRESH = "PartialRefresh"; const std::string CODEC_NAME_H264 = "OMX_hisi_video_encoder_avc"; const std::string CODEC_NAME_H265 = "OMX_hisi_video_encoder_hevc"; const std::string CODEC_NAME_MPEG4 = "avenc_mpeg4"; @@ -209,8 +215,8 @@ constexpr uint32_t MAX_MESSAGES_LEN = 40 * 1024 * 1024; constexpr float DEFAULT_DENSITY = 2.0; constexpr int32_t DEFAULT_SCREEN_FLAGS = 0; constexpr double DEFAULT_FPS = 60.0; -constexpr int32_t DIRTY_MAX_SIZE = 10; -constexpr uint32_t DIRTY_MAX_BUF_SIZE = 4096; +constexpr uint32_t DIRTY_MAX_SIZE = 10; +constexpr uint32_t DIRTY_MAX_BUF_SIZE = 1560000; constexpr uint32_t DIRTY_MAX_IMAGE_DATA_SIZE = 780000; constexpr uint8_t DEFAULT_CODECTYPE = VIDEO_CODEC_TYPE_VIDEO_H264; constexpr uint8_t DEFAULT_VIDEO_FORMAT = VIDEO_DATA_FORMAT_NV12; diff --git a/services/common/imageJpeg/include/jpeg_image_processor.h b/services/common/imageJpeg/include/jpeg_image_processor.h index 6027a64a..6402f950 100644 --- a/services/common/imageJpeg/include/jpeg_image_processor.h +++ b/services/common/imageJpeg/include/jpeg_image_processor.h @@ -43,7 +43,8 @@ public: private: uint32_t CompressRgbaToJpeg(const OHOS::Rect &damage, uint8_t *inputData, uint32_t inputDataSize, std::shared_ptr &data); - void DecompressJpegToNV12(size_t jpegSize, uint8_t *inputData, uint8_t *outputData, const uint32_t &outputDataSize); + void DecompressJpegToNV12(size_t jpegSize, uint8_t *inputData, uint8_t *outputData, const uint32_t &outputDataSize, + const DirtyRect &rect); static const constexpr char *DSCREEN_LOG_TAG = "JpegImageProcessor"; sptr imageSurface_; diff --git a/services/common/imageJpeg/src/jpeg_image_processor.cpp b/services/common/imageJpeg/src/jpeg_image_processor.cpp index 17de8114..294180d0 100644 --- a/services/common/imageJpeg/src/jpeg_image_processor.cpp +++ b/services/common/imageJpeg/src/jpeg_image_processor.cpp @@ -136,7 +136,7 @@ void JpegImageProcessor::EncodeDamageData(sptr &surfaceBuffer, surfaceAddrIdx += configParam_.GetScreenWidth() * RGBA_CHROMA; } uint32_t jpegSize = CompressRgbaToJpeg(damage, partialBuffer, partialSize, data); - DHLOGI("CompressRgbaToJpeg end, jpegSize %{public}" PRId32, jpegSize); + DHLOGI("CompressRgbaToJpeg end, jpegSize %{public}" PRIu32, jpegSize); delete [] partialBuffer; } @@ -163,7 +163,7 @@ int32_t JpegImageProcessor::DecodeDamageData(const std::shared_ptr & delete [] jpegData; return ret; } - offset += item.dirtySize; + offset += static_cast(item.dirtySize); uint32_t dirtyImageDataSize = item.width * item.height * RGB_CHROMA; if (dirtyImageDataSize > DIRTY_MAX_IMAGE_DATA_SIZE) { DHLOGE("%{public}s: The dirtyImageDataSize is out of range. Expected max: %{public}" PRIu32 @@ -173,7 +173,7 @@ int32_t JpegImageProcessor::DecodeDamageData(const std::shared_ptr & } uint8_t *dirtyImageData = new uint8_t[dirtyImageDataSize] {0}; DHLOGI("%{public}s: DecompressJpegToNV12.", DSCREEN_LOG_TAG); - DecompressJpegToNV12(item.dirtySize, jpegData, dirtyImageData, dirtyImageDataSize); + DecompressJpegToNV12(item.dirtySize, jpegData, dirtyImageData, dirtyImageDataSize, item); DHLOGI("%{public}s: DecompressJpegToNV12 success.", DSCREEN_LOG_TAG); ret = ReplaceDamage2LastFrame(lastFrame, dirtyImageData, item); if (ret != DH_SUCCESS) { @@ -193,9 +193,9 @@ int32_t JpegImageProcessor::ReplaceDamage2LastFrame(uint8_t *lastFrame, uint8_t { DHLOGI("%{public}s: ReplaceDamage2LastFrame.", DSCREEN_LOG_TAG); uint8_t *lastFrameIdx = lastFrame; - uint8_t *yData = lastFrameIdx + configParam_.GetScreenWidth() * rect.yPos + rect.xPos; + uint8_t *yData = lastFrameIdx + (configParam_.GetScreenWidth() * rect.yPos + rect.xPos); uint8_t *uData = lastFrameIdx + configParam_.GetScreenWidth() * configParam_.GetScreenHeight() + - configParam_.GetScreenWidth() * rect.yPos / TWO + rect.xPos; + (configParam_.GetScreenWidth() * (rect.yPos / TWO) + rect.xPos); uint8_t *yDirtyData = dirtyImageData; uint8_t *uDirtyData = dirtyImageData + rect.width * rect.height; uint8_t *yTempData = nullptr; @@ -209,7 +209,7 @@ int32_t JpegImageProcessor::ReplaceDamage2LastFrame(uint8_t *lastFrame, uint8_t } yDirtyData += rect.width; if (i % TWO) { - uTempData = uData + configParam_.GetScreenWidth() * i / TWO; + uTempData = uData + configParam_.GetScreenWidth() * (i / TWO); ret = memcpy_s(uTempData, rect.width, uDirtyData, rect.width); if (ret != EOK) { DHLOGE("%{public}s: memcpy uData failed.", DSCREEN_LOG_TAG); @@ -262,7 +262,8 @@ uint32_t JpegImageProcessor::CompressRgbaToJpeg(const OHOS::Rect &damage, (void)jpeg_write_scanlines(&cinfo, row_pointer, 1); } jpeg_finish_compress(&cinfo); - DirtyRect rect = {damage.x, damage.y, damage.w, damage.h, outSize}; + DirtyRect rect = { static_cast(damage.x), static_cast(damage.y), damage.w, damage.h, + static_cast(outSize) }; data->AddData(static_cast(outSize), outBuffer); data->AddDirtyRect(rect); jpeg_destroy_compress(&cinfo); @@ -274,7 +275,7 @@ uint32_t JpegImageProcessor::CompressRgbaToJpeg(const OHOS::Rect &damage, } void JpegImageProcessor::DecompressJpegToNV12( - size_t jpegSize, uint8_t *inputData, uint8_t *outputData, const uint32_t &outputDataSize) + size_t jpegSize, uint8_t *inputData, uint8_t *outputData, const uint32_t &outputDataSize, const DirtyRect &rect) { jpeg_decompress_struct cinfo; jpeg_error_mgr jerr; @@ -283,7 +284,7 @@ void JpegImageProcessor::DecompressJpegToNV12( jpeg_mem_src(&cinfo, inputData, jpegSize); (void)jpeg_read_header(&cinfo, TRUE); (void)jpeg_start_decompress(&cinfo); - if ((cinfo.output_width > DSCREEN_MAX_LEN) || (cinfo.output_height > DSCREEN_MAX_LEN)) { + if ((cinfo.output_width != rect.width) || (cinfo.output_height != rect.height)) { DHLOGE("%{public}s: JPEG image dimensions exceed DSCREEN_MAX_LEN: width = %{public}" PRIu32 ", height = %{public}" PRIu32, DSCREEN_LOG_TAG, cinfo.output_width, cinfo.output_height); jpeg_destroy_decompress(&cinfo); @@ -315,7 +316,7 @@ void JpegImageProcessor::DecompressJpegToNV12( int32_t v = ((UB_PARAM * buffer[0][j * RGB_CHROMA] - VG_PARAM * buffer[0][j * RGB_CHROMA + 1] - VB_PARAM * buffer[0][j * RGB_CHROMA + TWO] + UA_PARAM) >> MOVEBITS) + UA_PARAM; outputData[yIndex++] = static_cast((y < 0) ? 0 : (y > YUV_PARAM) ? YUV_PARAM : y); - if ((i % TWO == 0) && (j % TWO == 0) && (uvIndex < outputDataSize)) { + if ((i % TWO == 0) && (j % TWO == 0) && (uvIndex < outputDataSize - 1)) { outputData[uvIndex++] = static_cast((u < 0) ? 0 : (u > YUV_PARAM) ? YUV_PARAM : u); outputData[uvIndex++] = static_cast((v < 0) ? 0 : (v > YUV_PARAM) ? YUV_PARAM : v); } diff --git a/services/common/utils/src/dscreen_maprelation.cpp b/services/common/utils/src/dscreen_maprelation.cpp index 0572b157..f921be55 100644 --- a/services/common/utils/src/dscreen_maprelation.cpp +++ b/services/common/utils/src/dscreen_maprelation.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -18,6 +18,7 @@ #include "dscreen_constants.h" #include "dscreen_errcode.h" #include "dscreen_json_util.h" +#include "dscreen_log.h" using json = nlohmann::json; @@ -80,11 +81,13 @@ void to_json(json &j, const DScreenMapRelation &dScreenMapRelation) void from_json(const json &j, DScreenMapRelation &dScreenMapRelation) { if (!IsUInt64(j, KEY_DISPLAY_ID) || !IsUInt64(j, KEY_SCREEN_ID)) { + DHLOGE("Invalid display or screen ID."); return; } dScreenMapRelation.displayId_ = j[KEY_DISPLAY_ID].get(); dScreenMapRelation.screenId_ = j[KEY_SCREEN_ID].get(); if (!j.contains(KEY_DISPLAY_RECT) || !j.contains(KEY_SCREEN_RECT)) { + DHLOGE("Missing display or screen rect."); return; } from_json(j.at(KEY_DISPLAY_RECT), dScreenMapRelation.displayRect_); @@ -103,14 +106,23 @@ void to_json(json &j, const DisplayRect &rect) void from_json(const json &j, DisplayRect &rect) { - if (!IsInt32(j, KEY_POINT_START_X) || !IsInt32(j, KEY_POINT_START_Y) || - !IsInt32(j, KEY_WIDTH) || !IsInt32(j, KEY_HEIGHT)) { + if (!IsInt32(j, KEY_POINT_START_X) || !IsInt32(j, KEY_POINT_START_Y) || !IsInt32(j, KEY_WIDTH) || + !IsInt32(j, KEY_HEIGHT)) { + DHLOGE("Invalid display rect parameters."); return; } - rect.startX = j[KEY_POINT_START_X].get(); - rect.startY = j[KEY_POINT_START_Y].get(); - rect.width = j[KEY_WIDTH].get(); - rect.height = j[KEY_HEIGHT].get(); + int32_t startX = j[KEY_POINT_START_X].get(); + int32_t startY = j[KEY_POINT_START_Y].get(); + int32_t width = j[KEY_WIDTH].get(); + int32_t height = j[KEY_HEIGHT].get(); + if ((width > DSCREEN_MAX_VIDEO_DATA_WIDTH) || (height > DSCREEN_MAX_VIDEO_DATA_HEIGHT)) { + DHLOGE("Screen video width or height exceeds the maximum limit."); + return; + } + rect.startX = startX; + rect.startY = startY; + rect.width = width; + rect.height = height; } void to_json(json &j, const ScreenRect &rect) @@ -125,14 +137,23 @@ void to_json(json &j, const ScreenRect &rect) void from_json(const json &j, ScreenRect &rect) { - if (!IsInt32(j, KEY_POINT_START_X) || !IsInt32(j, KEY_POINT_START_Y) || - !IsUInt32(j, KEY_WIDTH) || !IsUInt32(j, KEY_HEIGHT)) { + if (!IsInt32(j, KEY_POINT_START_X) || !IsInt32(j, KEY_POINT_START_Y) || !IsUInt32(j, KEY_WIDTH) || + !IsUInt32(j, KEY_HEIGHT)) { + DHLOGE("Invalid screen rect parameters."); + return; + } + int32_t startX = j[KEY_POINT_START_X].get(); + int32_t startY = j[KEY_POINT_START_Y].get(); + uint32_t width = j[KEY_WIDTH].get(); + uint32_t height = j[KEY_HEIGHT].get(); + if ((width > DSCREEN_MAX_SCREEN_DATA_WIDTH) || (height > DSCREEN_MAX_SCREEN_DATA_HEIGHT)) { + DHLOGE("Screen width or height exceeds the maximum limit."); return; } - rect.startX = j[KEY_POINT_START_X].get(); - rect.startY = j[KEY_POINT_START_Y].get(); - rect.width = j[KEY_WIDTH].get(); - rect.height = j[KEY_HEIGHT].get(); + rect.startX = startX; + rect.startY = startY; + rect.width = width; + rect.height = height; } } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/common/utils/src/video_param.cpp b/services/common/utils/src/video_param.cpp index 69e3ce99..9909ebc8 100644 --- a/services/common/utils/src/video_param.cpp +++ b/services/common/utils/src/video_param.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,6 +16,7 @@ #include "dscreen_constants.h" #include "dscreen_json_util.h" +#include "dscreen_log.h" using json = nlohmann::json; @@ -111,7 +112,7 @@ void to_json(json &j, const DistributedHardware::VideoParam &videoParam) {KEY_FPS, videoParam.fps_}, {KEY_CODECTYPE, videoParam.codecType_}, {KEY_COLOR_FORMAT, videoParam.videoFormat_}, - {KEY_PARTIALREFREAH, videoParam.isPartialRefresh_} + {KEY_PARTIALREFRESH, videoParam.isPartialRefresh_} }; } @@ -121,19 +122,35 @@ void from_json(const json &j, DistributedHardware::VideoParam &videoParam) !IsUInt32(j, KEY_VIDEO_WIDTH) || !IsUInt32(j, KEY_VIDEO_HEIGHT) || !IsFloat(j, KEY_FPS) || !IsUInt8(j, KEY_CODECTYPE) || !IsUInt8(j, KEY_COLOR_FORMAT)) { + DHLOGE("Invalid JSON value for one or more keys."); return; } - videoParam.screenWidth_ = j[KEY_SCREEN_WIDTH].get(); - videoParam.screenHeight_ = j[KEY_SCREEN_HEIGHT].get(); - videoParam.videoWidth_ = j[KEY_VIDEO_WIDTH].get(); - videoParam.videoHeight_ = j[KEY_VIDEO_HEIGHT].get(); + const uint32_t screenWidth = j[KEY_SCREEN_WIDTH].get(); + const uint32_t screenHeight = j[KEY_SCREEN_HEIGHT].get(); + const uint32_t videoWidth = j[KEY_VIDEO_WIDTH].get(); + const uint32_t videoHeight = j[KEY_VIDEO_HEIGHT].get(); + + if ((screenWidth > DSCREEN_MAX_SCREEN_DATA_WIDTH) || (screenHeight > DSCREEN_MAX_SCREEN_DATA_HEIGHT)) { + DHLOGE("Screen width or height exceeds the maximum limit."); + return; + } + + if ((videoWidth > DSCREEN_MAX_VIDEO_DATA_WIDTH) || (videoHeight > DSCREEN_MAX_VIDEO_DATA_HEIGHT)) { + DHLOGE("Video width or height exceeds the maximum limit."); + return; + } + + videoParam.screenWidth_ = screenWidth; + videoParam.screenHeight_ = screenHeight; + videoParam.videoWidth_ = videoWidth; + videoParam.videoHeight_ = videoHeight; videoParam.fps_ = j[KEY_FPS].get(); videoParam.codecType_ = j[KEY_CODECTYPE].get(); videoParam.videoFormat_ = j[KEY_COLOR_FORMAT].get(); videoParam.isPartialRefresh_ = false; - if (IsBool(j, KEY_PARTIALREFREAH)) { - videoParam.isPartialRefresh_ = j[KEY_PARTIALREFREAH].get(); + if (IsBool(j, KEY_PARTIALREFRESH)) { + videoParam.isPartialRefresh_ = j[KEY_PARTIALREFRESH].get(); } } } // namespace DistributedHardware diff --git a/services/screenservice/sourceservice/dscreenmgr/1.0/src/dscreen.cpp b/services/screenservice/sourceservice/dscreenmgr/1.0/src/dscreen.cpp index 5c4951f7..e150f872 100644 --- a/services/screenservice/sourceservice/dscreenmgr/1.0/src/dscreen.cpp +++ b/services/screenservice/sourceservice/dscreenmgr/1.0/src/dscreen.cpp @@ -273,6 +273,12 @@ int32_t DScreen::CheckJsonData(json &attrJson) DHLOGE("enable param is invalid."); return ERR_DH_SCREEN_SA_ENABLE_JSON_ERROR; } + + if ((attrJson[KEY_SCREEN_WIDTH].get() > DSCREEN_MAX_SCREEN_DATA_WIDTH) || + (attrJson[KEY_SCREEN_HEIGHT].get() > DSCREEN_MAX_SCREEN_DATA_HEIGHT)) { + DHLOGE("Screen width or height is out of range."); + return ERR_DH_SCREEN_SA_ENABLE_JSON_ERROR; + } return DH_SUCCESS; } diff --git a/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp b/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp index c2c5151a..145e1de6 100644 --- a/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp +++ b/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp @@ -372,9 +372,9 @@ void ScreenDataChannelImpl::JsonToDirtyJson(nlohmann::json &rectJson, std::share if (!IsInt32(rectJson, "dirtySize") || !IsInt32(rectJson, "dataType")) { return; } - int32_t dirtySize = rectJson["dirtySize"].get(); + uint32_t dirtySize = rectJson["dirtySize"].get(); int32_t dataType = rectJson["dataType"].get(); - int32_t num = 0; + uint32_t num = 0; if (dirtySize >= DIRTY_MAX_SIZE) { return; } -- Gitee