• 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 "surface.h"
22 #include "surface/window.h"
23 #include "native_window.h"
24 #include "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 
NativeWindowSurfaceCleanCacheWithPara(NWebNativeWindow window,bool cleanAll)83 void WindowAdapterImpl::NativeWindowSurfaceCleanCacheWithPara(NWebNativeWindow window, bool cleanAll)
84 {
85     auto nativeWindow = reinterpret_cast<OHNativeWindow*>(window);
86     if (!nativeWindow || !nativeWindow->surface) {
87         WVLOG_D("window or surface is null, no need to clean surface cache");
88         return;
89     }
90     WVLOG_D("NativeWindowSurfaceCleanCacheWithPara");
91 
92     // eglDestroySurface has disconnected the surface link
93     GSError ret = nativeWindow->surface->Connect();
94     if (ret == (int32_t)GSERROR_OK) {
95         nativeWindow->surface->CleanCache(cleanAll);
96         nativeWindow->surface->Disconnect();
97     }
98 }
99 
SetTransformHint(uint32_t rotation,NWebNativeWindow window)100 void WindowAdapterImpl::SetTransformHint(uint32_t rotation, NWebNativeWindow window)
101 {
102     auto nativeWindow = reinterpret_cast<OHNativeWindow*>(window);
103     if (!nativeWindow || !nativeWindow->surface) {
104         WVLOG_E("window or buffer is null, fail to set buffer rotation");
105         return;
106     }
107 
108     auto transform = ConvertRotation(rotation);
109     nativeWindow->surface->SetTransformHint(transform);
110 }
111 
AddNativeWindowRef(NWebNativeWindow window)112 void WindowAdapterImpl::AddNativeWindowRef(NWebNativeWindow window)
113 {
114     int32_t ret = OH_NativeWindow_NativeObjectReference(reinterpret_cast<OHNativeWindow*>(window));
115     if (ret != 0) {
116         WVLOG_E("add window reference failed.");
117     }
118 }
119 
NativeWindowUnRef(NWebNativeWindow window)120 void WindowAdapterImpl::NativeWindowUnRef(NWebNativeWindow window)
121 {
122     int32_t ret = OH_NativeWindow_NativeObjectUnreference(reinterpret_cast<OHNativeWindow*>(window));
123     if (ret != 0) {
124         WVLOG_E("cancel window reference failed.");
125     }
126 }
127 } // namespace OHOS::NWeb
128