1 // Copyright 2012 Intel Corporation
3 // All rights reserved.
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
8 // - Redistributions of source code must retain the above copyright notice, this
9 // list of conditions and the following disclaimer.
11 // - Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #if WAFFLE_ANDROID_MAJOR_VERSION == 4 && \
27 WAFFLE_ANDROID_MINOR_VERSION >= 1 || \
28 WAFFLE_ANDROID_MAJOR_VERSION >= 5
29 # include <gui/Surface.h>
30 # include <gui/SurfaceComposerClient.h>
31 #elif WAFFLE_ANROID_MAJOR_VERSION >= 4 && \
32 WAFFLE_ANDROID_MINOR_VERSION == 0
33 # include <surfaceflinger/SurfaceComposerClient.h>
35 # error "android >= 4.0 is required"
38 #include "droid_surfaceflingerlink.h"
40 #include "wcore_util.h"
41 #include "wcore_error.h"
43 using namespace android
;
47 struct droid_surfaceflinger_container
{
48 sp
<SurfaceComposerClient
> composer_client
;
51 struct droid_ANativeWindow_container
{
52 // it is important ANativeWindow* is the first element in this structure
53 ANativeWindow
* native_window
;
54 sp
<SurfaceControl
> surface_control
;
55 sp
<ANativeWindow
> window
;
58 const uint32_t droid_magic_surface_width
= 32;
59 const uint32_t droid_magic_surface_height
= 32;
60 const int32_t droid_magic_surface_z
= 0x7FFFFFFF;
62 void droid_tear_down_surfaceflinger_link(
63 droid_surfaceflinger_container
* pSFContainer
);
65 droid_surfaceflinger_container
*
66 droid_setup_surfaceflinger_link()
70 droid_surfaceflinger_container
* pSFContainer
;
72 pSFContainer
= new droid_surfaceflinger_container
;
74 if (pSFContainer
== NULL
)
77 pSFContainer
->composer_client
= new SurfaceComposerClient
;
78 iRVal
= pSFContainer
->composer_client
->initCheck();
79 if (iRVal
!= NO_ERROR
) {
80 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
81 "android::SurfaceComposerClient->initCheck != NO_ERROR");
87 droid_tear_down_surfaceflinger_link(pSFContainer
);
91 droid_ANativeWindow_container
*
95 droid_surfaceflinger_container
* pSFContainer
)
100 droid_ANativeWindow_container
* pANWContainer
;
102 pANWContainer
= new droid_ANativeWindow_container
;
104 if (pANWContainer
== NULL
)
107 // The signature of SurfaceComposerClient::createSurface() differs across
109 #if WAFFLE_ANDROID_MAJOR_VERSION == 4 && \
110 WAFFLE_ANDROID_MINOR_VERSION >= 2 || \
111 WAFFLE_ANDROID_MAJOR_VERSION >= 5
112 pANWContainer
->surface_control
=
113 pSFContainer
->composer_client
->createSurface(
114 String8("Waffle Surface"),
115 droid_magic_surface_width
, droid_magic_surface_height
,
116 PIXEL_FORMAT_RGB_888
, 0);
118 pANWContainer
->surface_control
=
119 pSFContainer
->composer_client
->createSurface(
120 String8("Waffle Surface"), 0,
121 droid_magic_surface_width
, droid_magic_surface_height
,
122 PIXEL_FORMAT_RGB_888
, 0);
125 if (pANWContainer
->surface_control
== NULL
) {
126 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
127 "Unable to get android::SurfaceControl");
128 delete pANWContainer
;
132 bRVal
= pANWContainer
->surface_control
->isValid();
134 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
135 "Acquired android::SurfaceControl is invalid");
136 delete pANWContainer
;
140 pSFContainer
->composer_client
->openGlobalTransaction();
141 iRVal
= pANWContainer
->surface_control
->setLayer(droid_magic_surface_z
);
142 if (iRVal
!= NO_ERROR
) {
143 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
144 "Error in android::SurfaceControl->setLayer");
145 delete pANWContainer
;
146 goto error_closeTransaction
;
149 pSFContainer
->composer_client
->closeGlobalTransaction();
151 pANWContainer
->window
= pANWContainer
->surface_control
->getSurface();
153 pSFContainer
->composer_client
->openGlobalTransaction();
154 iRVal
= pANWContainer
->surface_control
->setSize(width
, height
);
155 pSFContainer
->composer_client
->closeGlobalTransaction();
157 if (iRVal
!= NO_ERROR
) {
158 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
159 "error in android::SurfaceControl->setSize");
160 delete pANWContainer
;
164 pANWContainer
->native_window
= pANWContainer
->window
.get();
166 return pANWContainer
;
168 error_closeTransaction
:
169 pSFContainer
->composer_client
->closeGlobalTransaction();
172 droid_tear_down_surfaceflinger_link(pSFContainer
);
178 droid_surfaceflinger_container
* pSFContainer
,
179 droid_ANativeWindow_container
* pANWContainer
)
183 pSFContainer
->composer_client
->openGlobalTransaction();
184 iRVal
= pANWContainer
->surface_control
->show();
185 pSFContainer
->composer_client
->closeGlobalTransaction();
187 if (iRVal
!= NO_ERROR
) {
188 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
189 "Error in android::SurfaceControl->show");
196 droid_resize_surface(
197 droid_surfaceflinger_container
* pSFContainer
,
198 droid_ANativeWindow_container
* pANWContainer
,
204 pSFContainer
->composer_client
->openGlobalTransaction();
205 iRVal
= pANWContainer
->surface_control
->setSize(width
, height
);
206 pSFContainer
->composer_client
->closeGlobalTransaction();
208 if (iRVal
!= NO_ERROR
) {
209 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
210 "Error in android::SurfaceControl->setSize");
217 droid_destroy_surface(
218 droid_surfaceflinger_container
* pSFContainer
,
219 droid_ANativeWindow_container
* pANWContainer
)
221 pSFContainer
->composer_client
->openGlobalTransaction();
222 pANWContainer
->surface_control
->clear();
223 pSFContainer
->composer_client
->closeGlobalTransaction();
224 delete pANWContainer
;
229 droid_tear_down_surfaceflinger_link(
230 waffle::droid_surfaceflinger_container
* pSFContainer
)
232 if( pSFContainer
== NULL
)
235 if (pSFContainer
->composer_client
!= NULL
) {
236 pSFContainer
->composer_client
->dispose();
237 pSFContainer
->composer_client
= NULL
;
243 }; // namespace waffle
245 extern "C" struct droid_surfaceflinger_container
*
248 return reinterpret_cast<droid_surfaceflinger_container
*>(
249 waffle::droid_setup_surfaceflinger_link());
252 droid_deinit_gl(droid_surfaceflinger_container
* pSFContainer
)
254 waffle::droid_tear_down_surfaceflinger_link(
255 reinterpret_cast<waffle::droid_surfaceflinger_container
*>
260 extern "C" droid_ANativeWindow_container
*
261 droid_create_surface(
264 droid_surfaceflinger_container
* pSFContainer
)
266 waffle::droid_ANativeWindow_container
* container
=
267 waffle::droid_setup_surface(
269 reinterpret_cast<waffle::droid_surfaceflinger_container
*> (pSFContainer
));
270 return reinterpret_cast<droid_ANativeWindow_container
*>(container
);
275 droid_surfaceflinger_container
* pSFContainer
,
276 droid_ANativeWindow_container
* pANWContainer
)
278 return waffle::droid_show_surface(
279 reinterpret_cast<waffle::droid_surfaceflinger_container
*>
281 reinterpret_cast<waffle::droid_ANativeWindow_container
*>
286 droid_resize_surface(
287 droid_surfaceflinger_container
* pSFContainer
,
288 droid_ANativeWindow_container
* pANWContainer
,
292 return waffle::droid_resize_surface(
293 reinterpret_cast<waffle::droid_surfaceflinger_container
*>
295 reinterpret_cast<waffle::droid_ANativeWindow_container
*>
296 (pANWContainer
), height
, width
);
300 droid_destroy_surface(
301 droid_surfaceflinger_container
* pSFContainer
,
302 droid_ANativeWindow_container
* pANWContainer
)
304 return waffle::droid_destroy_surface(
305 reinterpret_cast<waffle::droid_surfaceflinger_container
*>
307 reinterpret_cast<waffle::droid_ANativeWindow_container
*>