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 "foundation/graphic/graphic_surface/interfaces/inner_api/surface/window.h"
21 #include "foundation/graphic/graphic_surface/interfaces/inner_api/surface/surface.h"
22 #include "foundation/graphic/graphic_surface/surface/include/native_window.h"
23 #include "nweb_log.h"
24 
25 namespace OHOS::NWeb {
26 
GetInstance()27 WindowAdapterImpl& WindowAdapterImpl::GetInstance()
28 {
29     static WindowAdapterImpl instance;
30     return instance;
31 }
32 
CreateNativeWindowFromSurface(void * pSurface)33 NWebNativeWindow WindowAdapterImpl::CreateNativeWindowFromSurface(void* pSurface)
34 {
35     OHNativeWindow* window = ::CreateNativeWindowFromSurface(pSurface);
36     if (!window) {
37         return nullptr;
38     }
39     int32_t usage = BUFFER_USAGE_MEM_DMA;
40     NativeWindowHandleOpt(window, SET_USAGE, usage);
41     return reinterpret_cast<NWebNativeWindow>(window);
42 }
43 
DestroyNativeWindow(NWebNativeWindow window)44 void WindowAdapterImpl::DestroyNativeWindow(NWebNativeWindow window)
45 {
46     ::DestoryNativeWindow(reinterpret_cast<OHNativeWindow*>(window));
47 }
48 
NativeWindowSetBufferGeometry(NWebNativeWindow window,int32_t width,int32_t height)49 int32_t WindowAdapterImpl::NativeWindowSetBufferGeometry(NWebNativeWindow window, int32_t width, int32_t height)
50 {
51     return ::NativeWindowHandleOpt(reinterpret_cast<OHNativeWindow*>(window), SET_BUFFER_GEOMETRY, width, height);
52 }
53 
NativeWindowSurfaceCleanCache(NWebNativeWindow window)54 void WindowAdapterImpl::NativeWindowSurfaceCleanCache(NWebNativeWindow window)
55 {
56     WVLOG_D("WindowAdapterImpl::NativeWindowSurfaceCleanCache");
57     reinterpret_cast<OHNativeWindow*>(window)->surface->CleanCache();
58 }
59 
NativeWindowSurfaceCleanCacheWithPara(NWebNativeWindow window,bool cleanAll)60 void WindowAdapterImpl::NativeWindowSurfaceCleanCacheWithPara(NWebNativeWindow window, bool cleanAll)
61 {
62     WVLOG_D("WindowAdapterImpl::NativeWindowSurfaceCleanCacheWithPara");
63     auto nativeWindow = reinterpret_cast<OHNativeWindow*>(window);
64     if (!nativeWindow || !nativeWindow->surface) {
65         WVLOG_D("window or surface is null, no need to clean surface cache");
66         return;
67     }
68 
69     // eglDestroySurface has disconnected the surface link
70     GSError ret = nativeWindow->surface->Connect();
71     if (ret == (int32_t)GSERROR_OK) {
72         nativeWindow->surface->CleanCache(cleanAll);
73         nativeWindow->surface->Disconnect();
74     }
75 }
76 } // namespace OHOS::NWeb
77