• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "window_adapter_impl.h"
17 
18 #include <cstdarg>
19 
20 #include "nweb_log.h"
21 #include "foundation/graphic/graphic_surface/interfaces/inner_api/surface/surface.h"
22 #include "foundation/graphic/graphic_surface/interfaces/inner_api/surface/window.h"
23 #include "foundation/graphic/graphic_surface/surface/include/native_window.h"
24 #include "foundation/graphic/graphic_surface/interfaces/inner_api/surface/external_window.h"
25 
26 namespace OHOS::NWeb {
27 constexpr uint32_t ROTATE_NONE = 0;
28 constexpr uint32_t ROTATE_90 = 90;
29 constexpr uint32_t ROTATE_180 = 180;
30 constexpr uint32_t ROTATE_270 = 270;
ConvertRotation(uint32_t rotation)31 GraphicTransformType ConvertRotation(uint32_t rotation)
32 {
33     GraphicTransformType transform = GraphicTransformType::GRAPHIC_ROTATE_BUTT;
34     switch (rotation) {
35         case ROTATE_NONE:
36             transform = GraphicTransformType::GRAPHIC_ROTATE_NONE;
37             break;
38         case ROTATE_90:
39             transform = GraphicTransformType::GRAPHIC_ROTATE_90;
40             break;
41         case ROTATE_180:
42             transform = GraphicTransformType::GRAPHIC_ROTATE_180;
43             break;
44         case ROTATE_270:
45             transform = GraphicTransformType::GRAPHIC_ROTATE_270;
46             break;
47         default:
48             transform = GraphicTransformType::GRAPHIC_ROTATE_NONE;
49             break;
50     }
51     return transform;
52 }
53 
GetInstance()54 WindowAdapterImpl& WindowAdapterImpl::GetInstance()
55 {
56     static WindowAdapterImpl instance;
57     return instance;
58 }
59 
CreateNativeWindowFromSurface(void * pSurface)60 NWebNativeWindow WindowAdapterImpl::CreateNativeWindowFromSurface(void* pSurface)
61 {
62     OHNativeWindow* window = ::CreateNativeWindowFromSurface(pSurface);
63     if (!window) {
64         return nullptr;
65     }
66     uint64_t usage = BUFFER_USAGE_MEM_DMA;
67     NativeWindowHandleOpt(window, SET_USAGE, usage);
68     return reinterpret_cast<NWebNativeWindow>(window);
69 }
70 
DestroyNativeWindow(NWebNativeWindow window)71 void WindowAdapterImpl::DestroyNativeWindow(NWebNativeWindow window)
72 {
73     ::DestoryNativeWindow(reinterpret_cast<OHNativeWindow*>(window));
74 }
75 
NativeWindowSetBufferGeometry(NWebNativeWindow window,int32_t width,int32_t height)76 int32_t WindowAdapterImpl::NativeWindowSetBufferGeometry(NWebNativeWindow window, int32_t width, int32_t height)
77 {
78     return ::NativeWindowHandleOpt(reinterpret_cast<OHNativeWindow*>(window), SET_BUFFER_GEOMETRY, width, height);
79 }
80 
NativeWindowSurfaceCleanCache(NWebNativeWindow window)81 void WindowAdapterImpl::NativeWindowSurfaceCleanCache(NWebNativeWindow window)
82 {
83     WVLOG_D("WindowAdapterImpl::NativeWindowSurfaceCleanCache");
84     auto nativeWindow = reinterpret_cast<OHNativeWindow *>(window);
85     if (!nativeWindow || !nativeWindow->surface) {
86         WVLOG_D("window or surface is null, no need to clean surface cache");
87         return;
88     }
89     nativeWindow->surface->CleanCache();
90 }
91 
NativeWindowSurfaceCleanCacheWithPara(NWebNativeWindow window,bool cleanAll)92 void WindowAdapterImpl::NativeWindowSurfaceCleanCacheWithPara(NWebNativeWindow window, bool cleanAll)
93 {
94     auto nativeWindow = reinterpret_cast<OHNativeWindow*>(window);
95     if (!nativeWindow || !nativeWindow->surface) {
96         WVLOG_D("window or surface is null, no need to clean surface cache");
97         return;
98     }
99     WVLOG_D("NativeWindowSurfaceCleanCacheWithPara");
100 
101     // eglDestroySurface has disconnected the surface link
102     GSError ret = nativeWindow->surface->Connect();
103     if (ret == (int32_t)GSERROR_OK) {
104         nativeWindow->surface->CleanCache(cleanAll);
105         nativeWindow->surface->Disconnect();
106     }
107 }
108 
SetTransformHint(uint32_t rotation,NWebNativeWindow window)109 void WindowAdapterImpl::SetTransformHint(uint32_t rotation, NWebNativeWindow window)
110 {
111     auto nativeWindow = reinterpret_cast<OHNativeWindow*>(window);
112     if (!nativeWindow || !nativeWindow->surface) {
113         WVLOG_E("window or buffer is null, fail to set buffer rotation");
114         return;
115     }
116 
117     auto transform = ConvertRotation(rotation);
118     nativeWindow->surface->SetTransformHint(transform);
119 }
120 
AddNativeWindowRef(NWebNativeWindow window)121 void WindowAdapterImpl::AddNativeWindowRef(NWebNativeWindow window)
122 {
123     int32_t ret = OH_NativeWindow_NativeObjectReference(reinterpret_cast<OHNativeWindow*>(window));
124     if (ret != 0) {
125         WVLOG_E("add window reference failed.");
126     }
127 }
128 
NativeWindowUnRef(NWebNativeWindow window)129 void WindowAdapterImpl::NativeWindowUnRef(NWebNativeWindow window)
130 {
131     int32_t ret = OH_NativeWindow_NativeObjectUnreference(reinterpret_cast<OHNativeWindow*>(window));
132     if (ret != 0) {
133         WVLOG_E("cancel window reference failed.");
134     }
135 }
136 } // namespace OHOS::NWeb
137