From 9c4c7f6e4907d3ce234db348652c4f412ec546d8 Mon Sep 17 00:00:00 2001 From: Hongjin Li Date: Tue, 18 Mar 2025 14:11:04 +0800 Subject: [PATCH] refactor[mpp_runtime]: Refactor C++ mpp_runtime to C Signed-off-by: Chandler Chen Signed-off-by: Hongjin Li Signed-off-by: Herman Chen Change-Id: I0b68072abb1d4132b22a3817f4ab09dafecc7dee --- osal/CMakeLists.txt | 2 +- osal/inc/mpp_runtime.h | 17 +-- osal/{mpp_runtime.cpp => mpp_runtime.c} | 146 +++++++++++++----------- 3 files changed, 86 insertions(+), 79 deletions(-) rename osal/{mpp_runtime.cpp => mpp_runtime.c} (56%) diff --git a/osal/CMakeLists.txt b/osal/CMakeLists.txt index 65d23577..4fbced8d 100644 --- a/osal/CMakeLists.txt +++ b/osal/CMakeLists.txt @@ -30,7 +30,7 @@ add_library(osal OBJECT mpp_singleton.c mpp_soc.c mpp_platform.c - mpp_runtime.cpp + mpp_runtime.c mpp_allocator.cpp mpp_mem_pool.c mpp_callback.cpp diff --git a/osal/inc/mpp_runtime.h b/osal/inc/mpp_runtime.h index 908d31c9..193ec970 100644 --- a/osal/inc/mpp_runtime.h +++ b/osal/inc/mpp_runtime.h @@ -1,17 +1,6 @@ +/* SPDX-License-Identifier: Apache-2.0 OR MIT */ /* - * Copyright 2015 Rockchip Electronics 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright (c) 2015 Rockchip Electronics Co., Ltd. */ #ifndef __MPP_RUNTIME__ @@ -27,7 +16,7 @@ extern "C" { * Runtime function detection is to support different binary on different * runtime environment. This is usefull on product environemnt. */ -RK_U32 mpp_rt_allcator_is_valid(MppBufferType type); +rk_u32 mpp_rt_allcator_is_valid(MppBufferType type); #ifdef __cplusplus } diff --git a/osal/mpp_runtime.cpp b/osal/mpp_runtime.c similarity index 56% rename from osal/mpp_runtime.cpp rename to osal/mpp_runtime.c index 079463e8..ce088626 100644 --- a/osal/mpp_runtime.cpp +++ b/osal/mpp_runtime.c @@ -1,17 +1,6 @@ +/* SPDX-License-Identifier: Apache-2.0 OR MIT */ /* - * Copyright 2015 Rockchip Electronics 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright (c) 2015 Rockchip Electronics Co., Ltd. */ #define MODULE_TAG "mpp_rt" @@ -20,13 +9,31 @@ #include #include "mpp_env.h" -#include "mpp_log.h" +#include "mpp_mem.h" +#include "mpp_debug.h" #include "mpp_common.h" +#include "mpp_singleton.h" + #include "mpp_runtime.h" #define MAX_DTS_PATH_LEN 256 -static RK_U32 mpp_rt_debug = 0; +#define get_srv_runtime() \ + ({ \ + MppRuntimeSrv *__tmp; \ + if (!srv_runtime) { \ + mpp_rt_srv_init(); \ + } \ + if (srv_runtime) { \ + __tmp = srv_runtime; \ + } else { \ + mpp_err("mpp mem pool srv not init at %s\n", __FUNCTION__); \ + __tmp = NULL; \ + } \ + __tmp; \ + }) + +static rk_u32 mpp_rt_debug = 0; static const char *mpp_dts_base = "/proc/device-tree/"; @@ -53,67 +60,60 @@ static const char *mpp_vpu_address[] = { "@ff650000", /* rk3399 */ }; -class MppRuntimeService +typedef struct MppRuntimeService_t { + rk_u32 allocator_valid[MPP_BUFFER_TYPE_BUTT]; +} MppRuntimeSrv; + +static MppRuntimeSrv *srv_runtime; + +static void mpp_rt_srv_init() { -private: - // avoid any unwanted function - MppRuntimeService(); - ~MppRuntimeService() {}; - MppRuntimeService(const MppRuntimeService &); - MppRuntimeService &operator=(const MppRuntimeService &); + MppRuntimeSrv *srv = srv_runtime; - RK_U32 allocator_valid[MPP_BUFFER_TYPE_BUTT]; - -public: - static MppRuntimeService *get_instance() { - static MppRuntimeService instance; - return &instance; - } - - RK_U32 get_allocator_valid(MppBufferType type); -}; - -RK_U32 MppRuntimeService::get_allocator_valid(MppBufferType type) -{ - MppBufferType buffer_type = (MppBufferType)(type & MPP_BUFFER_TYPE_MASK); - return (buffer_type < MPP_BUFFER_TYPE_BUTT) ? allocator_valid[buffer_type] : (0); -}; - -MppRuntimeService::MppRuntimeService() -{ mpp_env_get_u32("mpp_rt_debug", &mpp_rt_debug, 0); - allocator_valid[MPP_BUFFER_TYPE_NORMAL] = 1; - allocator_valid[MPP_BUFFER_TYPE_ION] = !access("/dev/ion", F_OK | R_OK | W_OK); - allocator_valid[MPP_BUFFER_TYPE_DRM] = + if (srv) + return; + + srv = mpp_calloc(MppRuntimeSrv, 1); + if (!srv) { + mpp_err_f("failed to allocate runtime service\n"); + return; + } + + srv_runtime = srv; + + srv->allocator_valid[MPP_BUFFER_TYPE_NORMAL] = 1; + srv->allocator_valid[MPP_BUFFER_TYPE_ION] = !access("/dev/ion", F_OK | R_OK | W_OK); + srv->allocator_valid[MPP_BUFFER_TYPE_DRM] = !access("/dev/dri/renderD128", F_OK | R_OK | W_OK) || !access("/dev/dri/card0", F_OK | R_OK | W_OK); - allocator_valid[MPP_BUFFER_TYPE_DMA_HEAP] = !access("/dev/dma_heap", F_OK | R_OK); + srv->allocator_valid[MPP_BUFFER_TYPE_DMA_HEAP] = !access("/dev/dma_heap", F_OK | R_OK); - if (!allocator_valid[MPP_BUFFER_TYPE_ION] && - !allocator_valid[MPP_BUFFER_TYPE_DRM] && - !allocator_valid[MPP_BUFFER_TYPE_DMA_HEAP]) { + if (!srv->allocator_valid[MPP_BUFFER_TYPE_ION] && + !srv->allocator_valid[MPP_BUFFER_TYPE_DRM] && + !srv->allocator_valid[MPP_BUFFER_TYPE_DMA_HEAP]) { mpp_err("can NOT found any allocator\n"); return; } - if (allocator_valid[MPP_BUFFER_TYPE_DMA_HEAP]) { + if (srv->allocator_valid[MPP_BUFFER_TYPE_DMA_HEAP]) { mpp_rt_dbg("use dma heap allocator\n"); return; } - if (allocator_valid[MPP_BUFFER_TYPE_ION] && !allocator_valid[MPP_BUFFER_TYPE_DRM]) { + if (srv->allocator_valid[MPP_BUFFER_TYPE_ION] && !srv->allocator_valid[MPP_BUFFER_TYPE_DRM]) { mpp_rt_dbg("use ion allocator\n"); return; } - if (!allocator_valid[MPP_BUFFER_TYPE_ION] && allocator_valid[MPP_BUFFER_TYPE_DRM]) { + if (!srv->allocator_valid[MPP_BUFFER_TYPE_ION] && srv->allocator_valid[MPP_BUFFER_TYPE_DRM]) { mpp_rt_dbg("use drm allocator\n"); return; } if (!access("/dev/mpp_service", F_OK | R_OK | W_OK)) { - allocator_valid[MPP_BUFFER_TYPE_ION] = 0; + srv->allocator_valid[MPP_BUFFER_TYPE_ION] = 0; mpp_rt_dbg("use drm allocator for mpp_service\n"); return; @@ -121,28 +121,28 @@ MppRuntimeService::MppRuntimeService() // If both ion and drm is enabled detect allocator in dts to choose one // TODO: When unify dma fd kernel is completed this part will be removed. - if (allocator_valid[MPP_BUFFER_TYPE_ION] && - allocator_valid[MPP_BUFFER_TYPE_DRM]) { + if (srv->allocator_valid[MPP_BUFFER_TYPE_ION] && + srv->allocator_valid[MPP_BUFFER_TYPE_DRM]) { /* Detect hardware buffer type is ion or drm */ - RK_U32 i, j; + rk_u32 i, j; char path[MAX_DTS_PATH_LEN]; - RK_U32 path_len = MAX_DTS_PATH_LEN - 1; - RK_U32 dts_path_len = snprintf(path, path_len, "%s", mpp_dts_base); + rk_u32 path_len = MAX_DTS_PATH_LEN - 1; + rk_u32 dts_path_len = snprintf(path, path_len, "%s", mpp_dts_base); char *p = path + dts_path_len; - RK_U32 allocator_found = 0; + rk_u32 allocator_found = 0; path_len -= dts_path_len; for (i = 0; i < MPP_ARRAY_ELEMS(mpp_vpu_names); i++) { for (j = 0; j < MPP_ARRAY_ELEMS(mpp_vpu_address); j++) { - RK_U32 dev_path_len = snprintf(p, path_len, "%s%s", + rk_u32 dev_path_len = snprintf(p, path_len, "%s%s", mpp_vpu_names[i], mpp_vpu_address[j]); int f_ok = access(path, F_OK); if (f_ok == 0) { snprintf(p + dev_path_len, path_len - dev_path_len, "/%s", "allocator"); f_ok = access(path, F_OK); if (f_ok == 0) { - RK_S32 val = 0; + rk_s32 val = 0; FILE *fp = fopen(path, "rb"); if (fp) { size_t len = fread(&val, 1, 4, fp); @@ -155,10 +155,10 @@ MppRuntimeService::MppRuntimeService() } if (val == 0) { - allocator_valid[MPP_BUFFER_TYPE_DRM] = 0; + srv->allocator_valid[MPP_BUFFER_TYPE_DRM] = 0; mpp_rt_dbg("found ion allocator in dts\n"); } else { - allocator_valid[MPP_BUFFER_TYPE_ION] = 0; + srv->allocator_valid[MPP_BUFFER_TYPE_ION] = 0; mpp_rt_dbg("found drm allocator in dts\n"); } allocator_found = 1; @@ -175,9 +175,27 @@ MppRuntimeService::MppRuntimeService() if (!allocator_found) mpp_log("Can NOT found allocator in dts, enable both ion and drm\n"); } + return; } -RK_U32 mpp_rt_allcator_is_valid(MppBufferType type) +static void mpp_rt_srv_deinit() { - return MppRuntimeService::get_instance()->get_allocator_valid(type); + MPP_FREE(srv_runtime); } + +rk_u32 mpp_rt_allcator_is_valid(MppBufferType type) +{ + MppBufferType buffer_type = (MppBufferType)(type & MPP_BUFFER_TYPE_MASK); + rk_u32 valid = 0; + + if (buffer_type < MPP_BUFFER_TYPE_BUTT) { + MppRuntimeSrv *srv = get_srv_runtime(); + + if (srv) + valid = srv->allocator_valid[buffer_type]; + } + + return valid; +} + +MPP_SINGLETON(MPP_SGLN_RUNTIME, mpp_runtime, mpp_rt_srv_init, mpp_rt_srv_deinit);