1 /*
2  * Copyright (c) 2021-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 "rs_render_thread_util.h"
17 
18 #include "platform/common/rs_log.h"
19 #ifdef ROSEN_OHOS
20 #include "bundle_mgr_interface.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 #endif
24 
25 namespace OHOS {
26 namespace Rosen {
SrcRectScaleDown(Drawing::Rect & srcRect,const Drawing::Rect & localBounds)27 void RSRenderThreadUtil::SrcRectScaleDown(Drawing::Rect& srcRect, const Drawing::Rect& localBounds)
28 {
29     uint32_t newWidth = static_cast<uint32_t>(srcRect.GetWidth());
30     uint32_t newHeight = static_cast<uint32_t>(srcRect.GetHeight());
31     // Canvas is able to handle the situation when the window is out of screen, using bounds instead of dst.
32     uint32_t boundsWidth = static_cast<uint32_t>(localBounds.GetWidth());
33     uint32_t boundsHeight = static_cast<uint32_t>(localBounds.GetHeight());
34 
35     uint32_t newWidthBoundsHeight = newWidth * boundsHeight;
36     uint32_t newHeightBoundsWidth = newHeight * boundsWidth;
37 
38     if (newWidthBoundsHeight > newHeightBoundsWidth) {
39         newWidth = boundsWidth * newHeight / boundsHeight;
40     } else if (newWidthBoundsHeight < newHeightBoundsWidth) {
41         newHeight = boundsHeight * newWidth / boundsWidth;
42     } else {
43         return;
44     }
45 
46     uint32_t currentWidth = static_cast<uint32_t>(srcRect.GetWidth());
47     uint32_t currentHeight = static_cast<uint32_t>(srcRect.GetHeight());
48     if (newWidth < currentWidth) {
49         // the crop is too wide
50         uint32_t dw = currentWidth - newWidth;
51         auto halfdw = dw / 2;
52         srcRect = Drawing::Rect(srcRect.GetLeft() + static_cast<int32_t>(halfdw), srcRect.GetTop(),
53             srcRect.GetLeft() + static_cast<int32_t>(halfdw) + static_cast<int32_t>(newWidth),
54             srcRect.GetTop() + srcRect.GetHeight());
55     } else {
56         // thr crop is too tall
57         uint32_t dh = currentHeight - newHeight;
58         auto halfdh = dh / 2;
59         srcRect = Drawing::Rect(srcRect.GetLeft(), srcRect.GetTop() + static_cast<int32_t>(halfdh),
60             srcRect.GetLeft() + srcRect.GetWidth(),
61             srcRect.GetTop() + static_cast<int32_t>(halfdh) + static_cast<int32_t>(newHeight));
62     }
63 }
64 
SrcRectScaleFit(const Drawing::Rect & srcRect,Drawing::Rect & dstRect,const Drawing::Rect & localBounds)65 void RSRenderThreadUtil::SrcRectScaleFit(
66     const Drawing::Rect& srcRect, Drawing::Rect& dstRect, const Drawing::Rect& localBounds)
67 {
68     uint32_t srcWidth = static_cast<uint32_t>(srcRect.GetWidth());
69     uint32_t srcHeight = static_cast<uint32_t>(srcRect.GetHeight());
70     float newWidth = 0.0f;
71     float newHeight = 0.0f;
72     // Canvas is able to handle the situation when the window is out of screen, using bounds instead of dst.
73     uint32_t boundsWidth = static_cast<uint32_t>(localBounds.GetWidth());
74     uint32_t boundsHeight = static_cast<uint32_t>(localBounds.GetHeight());
75     if (boundsWidth == 0 || boundsHeight == 0 || srcWidth == 0 || srcHeight == 0) {
76         return;
77     }
78 
79     if (srcWidth * boundsHeight > srcHeight * boundsWidth) {
80         newWidth = boundsWidth;
81         newHeight = srcHeight * newWidth / srcWidth;
82     } else if (srcWidth * boundsHeight < srcHeight * boundsWidth) {
83         newHeight = boundsHeight;
84         newWidth = newHeight * srcWidth / srcHeight;
85     } else {
86         newWidth = boundsWidth;
87         newHeight = boundsHeight;
88     }
89     newHeight = newHeight * srcHeight / boundsHeight;
90     newWidth = newWidth * srcWidth / boundsWidth;
91     if (newWidth < srcWidth) {
92         float halfdw = (srcWidth - newWidth) / 2;
93         dstRect = Drawing::Rect(srcRect.GetLeft() + halfdw, srcRect.GetTop(), srcRect.GetLeft() + halfdw + newWidth,
94             srcRect.GetTop() + srcRect.GetHeight());
95     } else if (newHeight < srcHeight) {
96         float halfdh = (srcHeight - newHeight) / 2;
97         dstRect = Drawing::Rect(srcRect.GetLeft(), srcRect.GetTop() + halfdh, srcRect.GetLeft() + srcRect.GetWidth(),
98             srcRect.GetTop() + halfdh + newHeight);
99     }
100 }
101 
102 #ifdef ROSEN_OHOS
GetApiCompatibleVersion()103 uint32_t RSRenderThreadUtil::GetApiCompatibleVersion()
104 {
105     uint32_t apiCompatibleVersion = INVALID_API_COMPATIBLE_VERSION;
106     OHOS::sptr<OHOS::ISystemAbilityManager> systemAbilityManager =
107         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
108     OHOS::sptr<OHOS::IRemoteObject> remoteObject =
109         systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
110     sptr<AppExecFwk::IBundleMgr> iBundleMgr = OHOS::iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
111     if (iBundleMgr == nullptr) {
112         RS_LOGE("RSRenderThreadUtil::GetApiCompatibleVersion iBundleMgr is nullptr");
113         return apiCompatibleVersion;
114     }
115     AppExecFwk::BundleInfo bundleInfo;
116     if (iBundleMgr->GetBundleInfoForSelf(0, bundleInfo) == ERR_OK) {
117         apiCompatibleVersion = bundleInfo.targetVersion % API_VERSION_MOD;
118         RS_LOGD("RSRenderThreadUtil::GetApiCompatibleVersion targetVersion: [%{public}u], apiCompatibleVersion: "
119                 "[%{public}u]",
120             bundleInfo.targetVersion, apiCompatibleVersion);
121     } else {
122         RS_LOGE("RSRenderThreadUtil::GetApiCompatibleVersion failed");
123     }
124     return apiCompatibleVersion;
125 }
126 #endif
127 } // namespace Rosen
128 } // namespace OHOS