diff --git a/surface/BUILD.gn b/surface/BUILD.gn index 6cae8e965610aee01f43bfd9801e26aa0d1a7d09..9fad7dfb219c13765d8605775ad38cf82ac27bc9 100644 --- a/surface/BUILD.gn +++ b/surface/BUILD.gn @@ -139,3 +139,16 @@ ohos_shared_library("surface") { ## Build surface.so }}} +ohos_source_set("arkuix_surface") { + sources = [ + "src/cross_platform/native_window.cpp", + "src/cross_platform/surface_utils.cpp", + ] + + configs = [ ":surface_config" ] + + public_configs = [ ":surface_public_config" ] + + part_name = "graphic_surface" + subsystem_name = "graphic" +} diff --git a/surface/include/cross_platform/external_window.h b/surface/include/cross_platform/external_window.h new file mode 100644 index 0000000000000000000000000000000000000000..f0e361c08d4ca2b71e5d82a35c35bef48e8a1988 --- /dev/null +++ b/surface/include/cross_platform/external_window.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 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 + * + * 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. + */ + +/** + * @addtogroup NativeWindow + * + * @brief Provides the native window capability for connection to the EGL. + * + * @syscap SystemCapability.Graphic.Graphic2D.NativeWindow + */ + +/** + * @file external_window.h + * + * @brief Defines the functions for obtaining and using a native window. + * + */ + +#ifndef NDK_INCLUDE_EXTERNAL_NATIVE_WINDOW_H_ +#define NDK_INCLUDE_EXTERNAL_NATIVE_WINDOW_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif +typedef void OHNativeWindow; +/** + * @brief Creates an OHNativeWindow instance.\n + * + * @syscap SystemCapability.Graphic.Graphic2D.NativeWindow + * @param surfaceId Indicates the surfaceId to a surface. + * @param window indicates the pointer to an OHNativeWindow instance. + * @return Returns an error code, 0 is Success, otherwise, failed. + */ +int32_t OH_NativeWindow_CreateNativeWindowFromSurfaceId(uint64_t surfaceId, OHNativeWindow **window); +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/surface/include/cross_platform/surface_utils.h b/surface/include/cross_platform/surface_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..c6a718c3af6cc4cb7afc322546266eee5262932d --- /dev/null +++ b/surface/include/cross_platform/surface_utils.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2025 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 + * + * 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. + */ + +#ifndef FRAMEWORKS_SURFACE_INCLUDE_CROSS_PLATFORM_SURFACE_UTILS_H +#define FRAMEWORKS_SURFACE_INCLUDE_CROSS_PLATFORM_SURFACE_UTILS_H + +#include +#include +#include "graphic_common.h" + +namespace OHOS { +class SurfaceUtils { +public: + SurfaceUtils(const SurfaceUtils&) = delete; + SurfaceUtils& operator=(const SurfaceUtils&) = delete; + static SurfaceUtils& GetInstance(); + void* GetNativeWindow(uint64_t uniqueId); + SurfaceError AddNativeWindow(uint64_t uniqueId, void *nativeWidow); + SurfaceError RemoveNativeWindow(uint64_t uniqueId); + +private: + SurfaceUtils() = default; + ~SurfaceUtils(); + std::mutex mutex_; + std::unordered_map nativeWindowCache_; +}; +} // namespace OHOS +#endif // FRAMEWORKS_SURFACE_INCLUDE_CROSS_PLATFORM_SURFACE_UTILS_H diff --git a/surface/src/cross_platform/native_window.cpp b/surface/src/cross_platform/native_window.cpp new file mode 100644 index 0000000000000000000000000000000000000000..001915a2512c9fe561c29683c5ebb9a234724edd --- /dev/null +++ b/surface/src/cross_platform/native_window.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2025 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 + * + * 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. + */ + +#include "cross_platform/external_window.h" +#include "cross_platform/surface_utils.h" + +using namespace OHOS; +int32_t OH_NativeWindow_CreateNativeWindowFromSurfaceId(uint64_t surfaceId, OHNativeWindow **window) +{ + if (window == nullptr) { + return OHOS::SURFACE_ERROR_INVALID_PARAM; + } + + auto &utils = SurfaceUtils::GetInstance(); + *window = reinterpret_cast(utils.GetNativeWindow(surfaceId)); + if (*window != nullptr) { + return OHOS::GSERROR_OK; + } + return OHOS::SURFACE_ERROR_INVALID_PARAM; +} \ No newline at end of file diff --git a/surface/src/cross_platform/surface_utils.cpp b/surface/src/cross_platform/surface_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd205413aaf5e1d54077df97a3cc410960cbbc22 --- /dev/null +++ b/surface/src/cross_platform/surface_utils.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2025 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 + * + * 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. + */ + +#include +#include "cross_platform/surface_utils.h" + +namespace OHOS { +SurfaceUtils& SurfaceUtils::GetInstance() +{ + static SurfaceUtils instance; + return instance; +} + +SurfaceUtils::~SurfaceUtils() +{ + nativeWindowCache_.clear(); +} + +void* SurfaceUtils::GetNativeWindow(uint64_t uniqueId) +{ + std::lock_guard lockGuard(mutex_); + auto iter = nativeWindowCache_.find(uniqueId); + if (iter == nativeWindowCache_.end()) { + return nullptr; + } + return iter->second; +} + +SurfaceError SurfaceUtils::AddNativeWindow(uint64_t uniqueId, void *nativeWidow) +{ + if (nativeWidow == nullptr) { + return GSERROR_INVALID_ARGUMENTS; + } + std::lock_guard lockGuard(mutex_); + if (nativeWindowCache_.count(uniqueId) == 0) { + nativeWindowCache_[uniqueId] = nativeWidow; + return GSERROR_OK; + } + return GSERROR_OK; +} + +SurfaceError SurfaceUtils::RemoveNativeWindow(uint64_t uniqueId) +{ + std::lock_guard lockGuard(mutex_); + nativeWindowCache_.erase(uniqueId); + return GSERROR_OK; +} +} // namespace OHOS