2 * Copyright (C) 2013 Canonical Ltd
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * Authored by: Thomas Voss <thomas.voss@canonical.com>
17 * Ricardo Salveti de Araujo <ricardo.salveti@canonical.com>
20 #include <hybris/surface_flinger/surface_flinger_compatibility_layer.h>
21 #include <hybris/internal/surface_flinger_compatibility_layer_internal.h>
23 #include <utils/misc.h>
25 #include <gui/SurfaceComposerClient.h>
26 #include <gui/ISurfaceComposer.h>
27 #include <ui/DisplayInfo.h>
28 #include <ui/PixelFormat.h>
29 #include <ui/Region.h>
37 void report_failed_to_allocate_surface_flinger_composer_client_on_creation()
39 printf("Problem allocating an object of type SurfaceComposerClient during client creation");
42 void report_failed_to_get_egl_default_display_on_creation()
44 printf("Problem accessing default egl display during client creation");
47 void report_failed_to_initialize_egl_on_creation()
49 printf("Problem initializing egl during client creation");
52 void report_failed_to_choose_egl_config_on_creation()
54 printf("Problem choosing egl config on creation");
57 void report_surface_control_is_null_during_creation()
59 printf("Could not acquire surface control object during surface creation");
62 void report_surface_is_null_during_creation()
64 printf("Could not acquire surface from surface control during surface creation");
68 void sf_blank(size_t display_id
)
70 android::sp
<android::IBinder
> display
;
72 if (display_id
== 0) {
73 display
= android::SurfaceComposerClient::getBuiltInDisplay(
74 android::ISurfaceComposer::eDisplayIdMain
);
75 } else if (display_id
== 1) {
76 display
= android::SurfaceComposerClient::getBuiltInDisplay(
77 android::ISurfaceComposer::eDisplayIdHdmi
);
79 fprintf(stderr
, "Warning: sf_blank invalid display_id (0 || 1)\n");
83 android::SurfaceComposerClient::blankDisplay(display
);
86 void sf_unblank(size_t display_id
)
88 android::sp
<android::IBinder
> display
;
90 if (display_id
== 0) {
91 display
= android::SurfaceComposerClient::getBuiltInDisplay(
92 android::ISurfaceComposer::eDisplayIdMain
);
93 } else if (display_id
== 1) {
94 display
= android::SurfaceComposerClient::getBuiltInDisplay(
95 android::ISurfaceComposer::eDisplayIdHdmi
);
97 fprintf(stderr
, "Warning: sf_unblank invalid display_id (0 || 1)\n");
101 android::SurfaceComposerClient::unblankDisplay(display
);
104 size_t sf_get_display_width(size_t display_id
)
106 android::sp
<android::IBinder
> display
;
108 if (display_id
== 0) {
109 display
= android::SurfaceComposerClient::getBuiltInDisplay(
110 android::ISurfaceComposer::eDisplayIdMain
);
111 } else if (display_id
== 1) {
112 display
= android::SurfaceComposerClient::getBuiltInDisplay(
113 android::ISurfaceComposer::eDisplayIdHdmi
);
115 fprintf(stderr
, "Warning: sf_get_display_width invalid display_id (0 || 1)\n");
119 android::DisplayInfo info
;
120 android::SurfaceComposerClient::getDisplayInfo(display
, &info
);
124 size_t sf_get_display_height(size_t display_id
)
126 android::sp
<android::IBinder
> display
;
128 if (display_id
== 0) {
129 display
= android::SurfaceComposerClient::getBuiltInDisplay(
130 android::ISurfaceComposer::eDisplayIdMain
);
131 } else if (display_id
== 1) {
132 display
= android::SurfaceComposerClient::getBuiltInDisplay(
133 android::ISurfaceComposer::eDisplayIdHdmi
);
135 fprintf(stderr
, "Warning: sf_get_display_height invalid display_id (0 || 1)\n");
139 android::DisplayInfo info
;
140 android::SurfaceComposerClient::getDisplayInfo(display
, &info
);
144 SfClient
* sf_client_create_full(bool egl_support
)
146 SfClient
* client
= new SfClient();
148 client
->client
= new android::SurfaceComposerClient();
149 if (client
->client
== NULL
) {
150 report_failed_to_allocate_surface_flinger_composer_client_on_creation();
155 client
->egl_support
= egl_support
;
157 client
->egl_display
= eglGetDisplay(EGL_DEFAULT_DISPLAY
);
158 if (client
->egl_display
== EGL_NO_DISPLAY
) {
159 report_failed_to_get_egl_default_display_on_creation();
165 int rc
= eglInitialize(client
->egl_display
, &major
, &minor
);
166 if (rc
== EGL_FALSE
) {
167 report_failed_to_initialize_egl_on_creation();
173 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
174 EGL_RENDERABLE_TYPE
, EGL_OPENGL_ES2_BIT
,
179 if (eglChooseConfig(client
->egl_display
,
181 &client
->egl_config
, 1,
183 report_failed_to_choose_egl_config_on_creation();
188 EGLint context_attribs
[] = {
189 EGL_CONTEXT_CLIENT_VERSION
, 2,
193 client
->egl_context
= eglCreateContext(
203 SfClient
* sf_client_create()
205 return sf_client_create_full(true);
208 EGLDisplay
sf_client_get_egl_display(SfClient
* client
)
212 if (client
->egl_support
)
213 return client
->egl_display
;
215 fprintf(stderr
, "Warning: sf_client_get_egl_display not supported, EGL "
216 "support disabled\n");
221 EGLConfig
sf_client_get_egl_config(SfClient
* client
)
225 if (client
->egl_support
)
226 return client
->egl_config
;
228 fprintf(stderr
, "Warning: sf_client_get_egl_config not supported, EGL "
229 "support disabled\n");
234 void sf_client_begin_transaction(SfClient
* client
)
237 client
->client
->openGlobalTransaction();
240 void sf_client_end_transaction(SfClient
* client
)
243 client
->client
->closeGlobalTransaction();
246 SfSurface
* sf_surface_create(SfClient
* client
, SfSurfaceCreationParameters
* params
)
251 SfSurface
* surface
= new SfSurface();
252 surface
->client
= client
;
253 surface
->surface_control
= surface
->client
->client
->createSurface(
254 android::String8(params
->name
),
257 android::PIXEL_FORMAT_RGBA_8888
,
260 if (surface
->surface_control
== NULL
) {
261 report_surface_control_is_null_during_creation();
266 surface
->surface
= surface
->surface_control
->getSurface();
268 if (surface
->surface
== NULL
) {
269 report_surface_is_null_during_creation();
274 sf_client_begin_transaction(client
);
276 surface
->surface_control
->setPosition(params
->x
, params
->y
);
277 surface
->surface_control
->setLayer(params
->layer
);
278 surface
->surface_control
->setAlpha(params
->alpha
);
280 sf_client_end_transaction(client
);
282 if (params
->create_egl_window_surface
) {
283 if (client
->egl_support
) {
284 android::sp
<ANativeWindow
> anw(surface
->surface
);
285 surface
->egl_surface
= eglCreateWindowSurface(
286 surface
->client
->egl_display
,
287 surface
->client
->egl_config
,
291 fprintf(stderr
, "Warning: params->create_egl_window_surface not "
292 "supported, EGL support disabled\n");
298 EGLSurface
sf_surface_get_egl_surface(SfSurface
* surface
)
302 if (surface
->client
->egl_support
)
303 return surface
->egl_surface
;
305 fprintf(stderr
, "Warning: sf_surface_get_egl_surface not supported, "
306 "EGL support disabled\n");
311 EGLNativeWindowType
sf_surface_get_egl_native_window(SfSurface
* surface
)
314 return surface
->surface
.get();
317 void sf_surface_make_current(SfSurface
* surface
)
321 if (surface
->client
->egl_support
) {
323 surface
->client
->egl_display
,
324 surface
->egl_surface
,
325 surface
->egl_surface
,
326 surface
->client
->egl_context
);
328 fprintf(stderr
, "Warning: sf_surface_make_current not supported, EGL "
329 "support disabled\n");
333 void sf_surface_move_to(SfSurface
* surface
, int x
, int y
)
336 surface
->surface_control
->setPosition(x
, y
);
339 void sf_surface_set_size(SfSurface
* surface
, int w
, int h
)
342 surface
->surface_control
->setSize(w
, h
);
345 void sf_surface_set_layer(SfSurface
* surface
, int layer
)
348 surface
->surface_control
->setLayer(layer
);
351 void sf_surface_set_alpha(SfSurface
* surface
, float alpha
)
354 surface
->surface_control
->setAlpha(alpha
);