1 /*
2  * Cogl
3  *
4  * A Low Level GPU Graphics and Utilities API
5  *
6  * Copyright (C) 2011 Intel Corporation.
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation
10  * files (the "Software"), to deal in the Software without
11  * restriction, including without limitation the rights to use, copy,
12  * modify, merge, publish, distribute, sublicense, and/or sell copies
13  * of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26  * SOFTWARE.
27  *
28  *
29  *
30  * Authors:
31  *   Neil Roberts <neil@linux.intel.com>
32  */
33 
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37 
38 #include "cogl-types.h"
39 #include "cogl-snippet-private.h"
40 #include "cogl-util.h"
41 #include "cogl-gtype-private.h"
42 
43 static void
44 _cogl_snippet_free (CoglSnippet *snippet);
45 
46 COGL_OBJECT_DEFINE (Snippet, snippet);
47 COGL_GTYPE_DEFINE_CLASS (Snippet, snippet);
48 
49 CoglSnippet *
cogl_snippet_new(CoglSnippetHook hook,const char * declarations,const char * post)50 cogl_snippet_new (CoglSnippetHook hook,
51                   const char *declarations,
52                   const char *post)
53 {
54   CoglSnippet *snippet = g_slice_new0 (CoglSnippet);
55 
56   _cogl_snippet_object_new (snippet);
57 
58   snippet->hook = hook;
59 
60   cogl_snippet_set_declarations (snippet, declarations);
61   cogl_snippet_set_post (snippet, post);
62 
63   return snippet;
64 }
65 
66 CoglSnippetHook
cogl_snippet_get_hook(CoglSnippet * snippet)67 cogl_snippet_get_hook (CoglSnippet *snippet)
68 {
69   _COGL_RETURN_VAL_IF_FAIL (cogl_is_snippet (snippet), 0);
70 
71   return snippet->hook;
72 }
73 
74 static CoglBool
_cogl_snippet_modify(CoglSnippet * snippet)75 _cogl_snippet_modify (CoglSnippet *snippet)
76 {
77   if (snippet->immutable)
78     {
79       g_warning ("A CoglSnippet should not be modified once it has been "
80                  "attached to a pipeline. Any modifications after that point "
81                  "will be ignored.");
82 
83       return FALSE;
84     }
85 
86   return TRUE;
87 }
88 
89 void
cogl_snippet_set_declarations(CoglSnippet * snippet,const char * declarations)90 cogl_snippet_set_declarations (CoglSnippet *snippet,
91                                const char *declarations)
92 {
93   _COGL_RETURN_IF_FAIL (cogl_is_snippet (snippet));
94 
95   if (!_cogl_snippet_modify (snippet))
96     return;
97 
98   g_free (snippet->declarations);
99   snippet->declarations = declarations ? g_strdup (declarations) : NULL;
100 }
101 
102 const char *
cogl_snippet_get_declarations(CoglSnippet * snippet)103 cogl_snippet_get_declarations (CoglSnippet *snippet)
104 {
105   _COGL_RETURN_VAL_IF_FAIL (cogl_is_snippet (snippet), NULL);
106 
107   return snippet->declarations;
108 }
109 
110 void
cogl_snippet_set_pre(CoglSnippet * snippet,const char * pre)111 cogl_snippet_set_pre (CoglSnippet *snippet,
112                       const char *pre)
113 {
114   _COGL_RETURN_IF_FAIL (cogl_is_snippet (snippet));
115 
116   if (!_cogl_snippet_modify (snippet))
117     return;
118 
119   g_free (snippet->pre);
120   snippet->pre = pre ? g_strdup (pre) : NULL;
121 }
122 
123 const char *
cogl_snippet_get_pre(CoglSnippet * snippet)124 cogl_snippet_get_pre (CoglSnippet *snippet)
125 {
126   _COGL_RETURN_VAL_IF_FAIL (cogl_is_snippet (snippet), NULL);
127 
128   return snippet->pre;
129 }
130 
131 void
cogl_snippet_set_replace(CoglSnippet * snippet,const char * replace)132 cogl_snippet_set_replace (CoglSnippet *snippet,
133                           const char *replace)
134 {
135   _COGL_RETURN_IF_FAIL (cogl_is_snippet (snippet));
136 
137   if (!_cogl_snippet_modify (snippet))
138     return;
139 
140   g_free (snippet->replace);
141   snippet->replace = replace ? g_strdup (replace) : NULL;
142 }
143 
144 const char *
cogl_snippet_get_replace(CoglSnippet * snippet)145 cogl_snippet_get_replace (CoglSnippet *snippet)
146 {
147   _COGL_RETURN_VAL_IF_FAIL (cogl_is_snippet (snippet), NULL);
148 
149   return snippet->replace;
150 }
151 
152 void
cogl_snippet_set_post(CoglSnippet * snippet,const char * post)153 cogl_snippet_set_post (CoglSnippet *snippet,
154                        const char *post)
155 {
156   _COGL_RETURN_IF_FAIL (cogl_is_snippet (snippet));
157 
158   if (!_cogl_snippet_modify (snippet))
159     return;
160 
161   g_free (snippet->post);
162   snippet->post = post ? g_strdup (post) : NULL;
163 }
164 
165 const char *
cogl_snippet_get_post(CoglSnippet * snippet)166 cogl_snippet_get_post (CoglSnippet *snippet)
167 {
168   _COGL_RETURN_VAL_IF_FAIL (cogl_is_snippet (snippet), NULL);
169 
170   return snippet->post;
171 }
172 
173 void
_cogl_snippet_make_immutable(CoglSnippet * snippet)174 _cogl_snippet_make_immutable (CoglSnippet *snippet)
175 {
176   snippet->immutable = TRUE;
177 }
178 
179 static void
_cogl_snippet_free(CoglSnippet * snippet)180 _cogl_snippet_free (CoglSnippet *snippet)
181 {
182   g_free (snippet->declarations);
183   g_free (snippet->pre);
184   g_free (snippet->replace);
185   g_free (snippet->post);
186   g_slice_free (CoglSnippet, snippet);
187 }
188