• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "native_pixel_map_manager.h"
17 
18 #ifdef OHOS_PLATFORM
19 #include "pixelmap_native_impl.h"
20 #include "native_pixel_map.h"
21 #endif
22 
GetInstance()23 NativePixelMapManager& NativePixelMapManager::GetInstance()
24 {
25     static NativePixelMapManager instance;
26     return instance;
27 }
28 
Register(void * handle,NativePixelMapType type)29 void NativePixelMapManager::Register(void* handle, NativePixelMapType type)
30 {
31     std::lock_guard<std::mutex> lck(mutex_);
32     mapper_.emplace(handle, type);
33 }
34 
Unregister(void * handle)35 void NativePixelMapManager::Unregister(void* handle)
36 {
37     std::lock_guard<std::mutex> lck(mutex_);
38     mapper_.erase(handle);
39 }
40 
GetNativePixelMapType(void * handle)41 NativePixelMapType NativePixelMapManager::GetNativePixelMapType(void* handle)
42 {
43     std::lock_guard<std::mutex> lck(mutex_);
44     auto iter = mapper_.find(handle);
45     if (iter == mapper_.end()) {
46         return NativePixelMapType::OBJECT_UNKNOWN;
47     }
48     return iter->second;
49 }
50 
51 namespace OHOS {
52 namespace Rosen {
53 #ifdef OHOS_PLATFORM
GetPixelMapFromNativePixelMap(OH_Drawing_PixelMap * pixelMap)54 std::shared_ptr<OHOS::Media::PixelMap> GetPixelMapFromNativePixelMap(OH_Drawing_PixelMap* pixelMap)
55 {
56     if (pixelMap == nullptr) {
57         return nullptr;
58     }
59 
60     std::shared_ptr<OHOS::Media::PixelMap> pixelMapPtr = nullptr;
61     switch (NativePixelMapManager::GetInstance().GetNativePixelMapType(pixelMap)) {
62         case NativePixelMapType::OBJECT_FROM_C:
63             if (pixelMap) {
64                 pixelMapPtr = reinterpret_cast<OH_PixelmapNative*>(pixelMap)->GetInnerPixelmap();
65             }
66             break;
67         case NativePixelMapType::OBJECT_FROM_JS:
68             pixelMapPtr = OHOS::Media::PixelMapNative_GetPixelMap(reinterpret_cast<NativePixelMap_*>(pixelMap));
69             break;
70         default:
71             break;
72     }
73 
74     return pixelMapPtr;
75 }
76 #endif
77 } // namespace Rosen
78 } // namespace OHOS