1 /*
2 * Copyright (c) 2021 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 "wl_subsurface_factory.h"
17
18 #include <mutex>
19
20 namespace OHOS {
GetInstance()21 sptr<WlSubsurfaceFactory> WlSubsurfaceFactory::GetInstance()
22 {
23 if (instance == nullptr) {
24 static std::mutex mutex;
25 std::lock_guard<std::mutex> lock(mutex);
26 if (instance == nullptr) {
27 instance = new WlSubsurfaceFactory();
28 }
29 }
30 return instance;
31 }
32
Init()33 void WlSubsurfaceFactory::Init()
34 {
35 delegator.Dep<WaylandService>()->OnAppear(&WlSubsurfaceFactory::OnAppear);
36 }
37
Deinit()38 void WlSubsurfaceFactory::Deinit()
39 {
40 if (subcompositor != nullptr) {
41 wl_subcompositor_destroy(subcompositor);
42 subcompositor = nullptr;
43 }
44 }
45
OnAppear(const GetServiceFunc get,const std::string & iname,uint32_t ver)46 void WlSubsurfaceFactory::OnAppear(const GetServiceFunc get, const std::string &iname, uint32_t ver)
47 {
48 constexpr uint32_t wlSubcompositorVersion = 1;
49 if (iname == "wl_subcompositor") {
50 auto ret = get(&wl_subcompositor_interface, wlSubcompositorVersion);
51 subcompositor = static_cast<struct wl_subcompositor *>(ret);
52 }
53 }
54
Create(const sptr<WlSurface> & surf,const sptr<WlSurface> & parent)55 sptr<WlSubsurface> WlSubsurfaceFactory::Create(const sptr<WlSurface> &surf, const sptr<WlSurface> &parent)
56 {
57 struct wl_subsurface *subsurf = nullptr;
58 if (subcompositor != nullptr &&
59 surf != nullptr && surf->GetRawPtr() != nullptr &&
60 parent != nullptr && parent->GetRawPtr() != nullptr) {
61 subsurf = wl_subcompositor_get_subsurface(subcompositor, surf->GetRawPtr(), parent->GetRawPtr());
62 }
63
64 if (subsurf) {
65 sptr<WlSubsurface> wlSubsurf = new WlSubsurface(subsurf);
66 return wlSubsurf;
67 }
68 return nullptr;
69 }
70 } // namespace OHOS
71