1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 * Copyright (C) 2023 BlackBerry Limited
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 #include <pthread.h>
18 #include <screen/screen.h>
19
20 #include "NativeSubWindow.h"
21
22 namespace {
23
24 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
25 static screen_context_t g_screen_ctx;
26
screen_init(void)27 static void screen_init(void) {
28 /* initialize the global screen context */
29 screen_create_context(&g_screen_ctx, SCREEN_APPLICATION_CONTEXT);
30 }
31
get_screen_context()32 static screen_context_t get_screen_context() {
33 pthread_once(&once_control, screen_init);
34 return g_screen_ctx;
35 }
36
37 } // namespace
38
createSubWindow(FBNativeWindowType p_window,int x,int y,int width,int height,float dpr,SubWindowRepaintCallback repaint_callback,void * repaint_callback_param,int hideWindow)39 EGLNativeWindowType createSubWindow(FBNativeWindowType p_window, int x, int y, int width,
40 int height, float dpr,
41 SubWindowRepaintCallback repaint_callback,
42 void* repaint_callback_param, int hideWindow) {
43 screen_context_t screen_ctx;
44 screen_window_t screen_window;
45 int rc;
46
47 screen_ctx = get_screen_context();
48 if (screen_ctx == nullptr) {
49 perror("No screen context");
50 return nullptr;
51 }
52
53 int type = p_window ? SCREEN_EMBEDDED_WINDOW : SCREEN_APPLICATION_WINDOW;
54 rc = screen_create_window_type(&screen_window, screen_ctx, type);
55 if (rc) {
56 perror("screen_create_window");
57 return nullptr;
58 }
59
60 int alpha_mode = SCREEN_NON_PRE_MULTIPLIED_ALPHA;
61 screen_set_window_property_iv(screen_window, SCREEN_PROPERTY_ALPHA_MODE, &alpha_mode);
62
63 int usage = SCREEN_USAGE_NATIVE | SCREEN_USAGE_OPENGL_ES2;
64 screen_set_window_property_iv(screen_window, SCREEN_PROPERTY_USAGE, &usage);
65
66 int interval = 1;
67 screen_set_window_property_iv(screen_window, SCREEN_PROPERTY_SWAP_INTERVAL, &interval);
68
69 int format = SCREEN_FORMAT_RGBA8888;
70 screen_set_window_property_iv(screen_window, SCREEN_PROPERTY_FORMAT, &format);
71
72 int transp = SCREEN_TRANSPARENCY_NONE;
73 screen_set_window_property_iv(screen_window, SCREEN_PROPERTY_TRANSPARENCY, &transp);
74
75 int pos[2] = {x, y};
76 screen_set_window_property_iv(screen_window, SCREEN_PROPERTY_POSITION, pos);
77
78 int size[2] = {width, height};
79 screen_set_window_property_iv(screen_window, SCREEN_PROPERTY_SIZE, size);
80 screen_set_window_property_iv(screen_window, SCREEN_PROPERTY_BUFFER_SIZE, size);
81
82 rc = screen_create_window_buffers(screen_window, 2);
83 if (rc) {
84 perror("screen_create_window_buffers");
85 screen_destroy_window(screen_window);
86 return nullptr;
87 }
88
89 if (p_window) {
90 char group[64] = {0};
91 rc = screen_get_window_property_cv(p_window, SCREEN_PROPERTY_ID, sizeof(group), group);
92 if (!rc && group[0]) {
93 rc = screen_join_window_group(screen_window, group);
94 }
95 if (rc || !group[0]) {
96 perror("group");
97 }
98 } else {
99 // TODO: yodai create window group?
100 }
101
102 int sensitivity = SCREEN_SENSITIVITY_TEST;
103 screen_set_window_property_iv(screen_window, SCREEN_PROPERTY_SENSITIVITY, &sensitivity);
104
105 int visible = hideWindow ? 0 : 1;
106 screen_set_window_property_iv(screen_window, SCREEN_PROPERTY_VISIBLE, &visible);
107
108 rc = screen_flush_context(screen_ctx, SCREEN_WAIT_IDLE);
109 if (rc) {
110 perror("flush");
111 }
112
113 return screen_window;
114 }
115
destroySubWindow(EGLNativeWindowType win)116 void destroySubWindow(EGLNativeWindowType win) { screen_destroy_window(win); }
117
moveSubWindow(FBNativeWindowType p_parent_window,EGLNativeWindowType p_sub_window,int x,int y,int width,int height,float dpr)118 int moveSubWindow(FBNativeWindowType p_parent_window, EGLNativeWindowType p_sub_window, int x,
119 int y, int width, int height, float dpr) {
120 int pos[2] = {x, y};
121 if (screen_set_window_property_iv(p_sub_window, SCREEN_PROPERTY_POSITION, pos)) {
122 return 0;
123 }
124 int size[2] = {width, height};
125 if (screen_set_window_property_iv(p_sub_window, SCREEN_PROPERTY_SIZE, size)) {
126 return 0;
127 }
128 return screen_flush_context(get_screen_context(), 0) == EOK;
129 }
130