1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /*
3  * st-cogl-wrapper.c: compatibility wrappers for various cogl api calls
4  *
5  */
6 
7 #include "st-cogl-wrapper.h"
8 
9 static CoglContext *cogl_context = NULL;
10 static gboolean supports_npot = FALSE;
11 
12 inline static gboolean
hardware_supports_npot_sizes(void)13 hardware_supports_npot_sizes (void)
14 {
15     if (cogl_context != NULL)
16         return supports_npot;
17 
18     cogl_context = st_get_cogl_context();
19     supports_npot = cogl_has_feature (cogl_context, COGL_FEATURE_ID_TEXTURE_NPOT);
20 
21     g_message ("cogl npot texture sizes %s", supports_npot ? "SUPPORTED" : "NOT supported");
22 
23     return supports_npot;
24 }
25 
26 CoglContext *
st_get_cogl_context(void)27 st_get_cogl_context (void)
28 {
29   if (G_UNLIKELY (cogl_context == NULL))
30     cogl_context = clutter_backend_get_cogl_context (clutter_get_default_backend ());
31   return cogl_context;
32 }
33 
34 /**
35  * st_cogl_texture_new_from_data_wrapper: (skip)
36  *
37  * Decides whether to use the newer (apparently safer)
38  * cogl_texture_2d_new_from_data or the older cogl_texture_new_from_data
39  * depending on if the GPU supports it.
40  */
41 
42 CoglTexture *
st_cogl_texture_new_from_data_wrapper(int width,int height,CoglTextureFlags flags,CoglPixelFormat format,CoglPixelFormat internal_format,int rowstride,const uint8_t * data)43 st_cogl_texture_new_from_data_wrapper                (int  width,
44                                                       int  height,
45                                          CoglTextureFlags  flags,
46                                           CoglPixelFormat  format,
47                                           CoglPixelFormat  internal_format,
48                                                       int  rowstride,
49                                             const uint8_t *data)
50 {
51     CoglTexture *texture = NULL;
52 
53     if (hardware_supports_npot_sizes ())
54       {
55         CoglError *error = NULL;
56 
57         texture = COGL_TEXTURE (cogl_texture_2d_new_from_data (cogl_context, width, height,
58                                                                format,
59 #if COGL_VERSION < COGL_VERSION_ENCODE (1, 18, 0)
60                                                                COGL_PIXEL_FORMAT_ANY,
61 #endif
62                                                                rowstride,
63                                                                data,
64                                                                &error));
65 
66         if (error)
67           {
68             g_debug ("(st) cogl_texture_2d_new_from_data failed: %s\n", error->message);
69             cogl_error_free (error);
70           }
71       }
72     else
73       {
74         texture = cogl_texture_new_from_data (width,
75                                               height,
76                                               flags,
77                                               format,
78                                               internal_format,
79                                               rowstride,
80                                               data);
81       }
82 
83     return texture;
84 }
85 
86 /**
87  * st_cogl_texture_new_from_file_wrapper: (skip)
88  *
89  * Decides whether to use the newer (apparently safer)
90  * cogl_texture_2d_new_from_file or the older cogl_texture_new_from_file
91  * depending on if the GPU supports it.
92  */
93 
94 CoglTexture *
st_cogl_texture_new_from_file_wrapper(const char * filename,CoglTextureFlags flags,CoglPixelFormat internal_format)95 st_cogl_texture_new_from_file_wrapper         (const char *filename,
96                                          CoglTextureFlags  flags,
97                                           CoglPixelFormat  internal_format)
98 {
99     CoglTexture *texture = NULL;
100     CoglError *error = NULL;
101 
102     if (hardware_supports_npot_sizes ())
103       {
104         texture = COGL_TEXTURE (cogl_texture_2d_new_from_file (cogl_context,
105                                                                filename,
106 #if COGL_VERSION < COGL_VERSION_ENCODE (1, 18, 0)
107                                                                COGL_PIXEL_FORMAT_ANY,
108 #endif
109                                                                &error));
110       }
111     else
112       {
113         texture = cogl_texture_new_from_file (filename,
114                                               flags,
115                                               internal_format,
116                                               &error);
117       }
118 
119     if (error)
120       {
121         g_debug ("cogl_texture_(2d)_new_from_file failed: %s\n", error->message);
122         cogl_error_free (error);
123       }
124 
125     return texture;
126 }
127 
128 /**
129  * st_cogl_texture_new_with_size_wrapper: (skip)
130  *
131  * Decides whether to use the newer (apparently safer)
132  * cogl_texture_2d_new_with_size or the older cogl_texture_new_with_size
133  * depending on if the GPU supports it.
134  */
135 
136 CoglTexture *
st_cogl_texture_new_with_size_wrapper(int width,int height,CoglTextureFlags flags,CoglPixelFormat internal_format)137 st_cogl_texture_new_with_size_wrapper           (int width,
138                                                  int height,
139                                     CoglTextureFlags flags,
140                                      CoglPixelFormat internal_format)
141 {
142     CoglTexture *texture = NULL;
143 
144     if (hardware_supports_npot_sizes ())
145       {
146         texture = COGL_TEXTURE (cogl_texture_2d_new_with_size (cogl_context,
147                                                                width,
148                                                                height
149 #if COGL_VERSION < COGL_VERSION_ENCODE (1, 18, 0)
150                                                               ,CLUTTER_CAIRO_FORMAT_ARGB32
151 #endif
152                                                               ));
153       }
154     else
155       {
156         texture = cogl_texture_new_with_size (width, height,
157                                               flags,
158                                               internal_format);
159       }
160 
161     return texture;
162 }
163 
164