1 // Copyright 2012 Intel Corporation
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // - Redistributions of source code must retain the above copyright notice, this
9 //   list of conditions and the following disclaimer.
10 //
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.
14 //
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.
25 
26 #include <assert.h>
27 #include <stdlib.h>
28 
29 #include "wcore_error.h"
30 #include "wcore_util.h"
31 
32 #include "linux_dl.h"
33 #include "linux_platform.h"
34 
35 struct linux_platform {
36     struct linux_dl *libgl;
37     struct linux_dl *libgles1;
38     struct linux_dl *libgles2;
39 };
40 
41 struct linux_platform*
linux_platform_create(void)42 linux_platform_create(void)
43 {
44     return wcore_calloc(sizeof(struct linux_platform));
45 }
46 
47 bool
linux_platform_destroy(struct linux_platform * self)48 linux_platform_destroy(struct linux_platform *self)
49 {
50     bool ok = true;
51 
52     if (!self)
53         return true;
54 
55     // FIXME: Waffle is unable to emit a sequence of errors.
56     ok &= linux_dl_close(self->libgl);
57     ok &= linux_dl_close(self->libgles1);
58     ok &= linux_dl_close(self->libgles2);
59 
60     free(self);
61     return ok;
62 }
63 
64 static struct linux_dl*
linux_platform_get_dl(struct linux_platform * self,int32_t waffle_dl)65 linux_platform_get_dl(struct linux_platform *self, int32_t waffle_dl)
66 {
67     struct linux_dl **dl;
68 
69     switch (waffle_dl) {
70         case WAFFLE_DL_OPENGL:     dl = &self->libgl;    break;
71         case WAFFLE_DL_OPENGL_ES1: dl = &self->libgles1; break;
72         case WAFFLE_DL_OPENGL_ES2:
73         case WAFFLE_DL_OPENGL_ES3: dl = &self->libgles2; break;
74         default:
75             assert(false);
76             return NULL;
77     }
78 
79     if (*dl == NULL)
80         *dl = linux_dl_open(waffle_dl);
81 
82     return *dl;
83 }
84 
85 bool
linux_platform_dl_can_open(struct linux_platform * self,int32_t waffle_dl)86 linux_platform_dl_can_open(struct linux_platform *self, int32_t waffle_dl)
87 {
88     struct linux_dl *dl = NULL;
89     WCORE_ERROR_DISABLED({
90         dl = linux_platform_get_dl(self, waffle_dl);
91     });
92     return dl != NULL;
93 }
94 
95 void*
linux_platform_dl_sym(struct linux_platform * self,int32_t waffle_dl,const char * name)96 linux_platform_dl_sym(struct linux_platform *self, int32_t waffle_dl,
97                       const char *name)
98 {
99     struct linux_dl *dl = linux_platform_get_dl(self, waffle_dl);
100     if (!dl)
101         return NULL;
102 
103     return linux_dl_sym(dl, name);
104 }
105