1 /*
2  * Copyright (C) 2007,2008,2009,2012 Intel Corporation.
3  * Copyright (C) 2018 DisplayLink (UK) Ltd.
4  * Copyright (C) 2020 Red Hat
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use, copy,
10  * modify, merge, publish, distribute, sublicense, and/or sell copies
11  * of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  *
26  */
27 
28 #include "cogl-config.h"
29 
30 #include "driver/gl/cogl-gl-framebuffer-back.h"
31 
32 #include <gio/gio.h>
33 
34 #include "cogl-context-private.h"
35 #include "cogl-framebuffer-private.h"
36 #include "cogl-offscreen-private.h"
37 #include "driver/gl/cogl-util-gl-private.h"
38 
39 struct _CoglGlFramebufferBack
40 {
41   CoglGlFramebuffer parent;
42 
43   gboolean dirty_bitmasks;
44   CoglFramebufferBits bits;
45 };
46 
G_DEFINE_TYPE(CoglGlFramebufferBack,cogl_gl_framebuffer_back,COGL_TYPE_GL_FRAMEBUFFER)47 G_DEFINE_TYPE (CoglGlFramebufferBack, cogl_gl_framebuffer_back,
48                COGL_TYPE_GL_FRAMEBUFFER)
49 
50 static gboolean
51 ensure_bits_initialized (CoglGlFramebufferBack *gl_framebuffer_back)
52 {
53   CoglFramebufferDriver *driver = COGL_FRAMEBUFFER_DRIVER (gl_framebuffer_back);
54   CoglFramebuffer *framebuffer =
55     cogl_framebuffer_driver_get_framebuffer (driver);
56   CoglContext *ctx = cogl_framebuffer_get_context (framebuffer);
57   CoglFramebufferBits *bits = &gl_framebuffer_back->bits;
58   g_autoptr (GError) error = NULL;
59 
60   if (!gl_framebuffer_back->dirty_bitmasks)
61     return TRUE;
62 
63   cogl_context_flush_framebuffer_state (ctx,
64                                         framebuffer,
65                                         framebuffer,
66                                         COGL_FRAMEBUFFER_STATE_BIND);
67 
68 #ifdef HAVE_COGL_GL
69   if (ctx->driver == COGL_DRIVER_GL3)
70     {
71       const struct {
72         GLenum attachment, pname;
73         size_t offset;
74       } params[] = {
75         {
76           .attachment = GL_BACK_LEFT,
77           .pname = GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE,
78           .offset = offsetof (CoglFramebufferBits, red),
79         },
80         {
81           .attachment = GL_BACK_LEFT,
82           .pname = GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE,
83           .offset = offsetof (CoglFramebufferBits, green),
84         },
85         {
86           .attachment = GL_BACK_LEFT,
87           .pname = GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE,
88           .offset = offsetof (CoglFramebufferBits, blue),
89         },
90         {
91           .attachment = GL_BACK_LEFT,
92           .pname = GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
93           .offset = offsetof (CoglFramebufferBits, alpha),
94         },
95         {
96           .attachment = GL_DEPTH,
97           .pname = GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE,
98           .offset = offsetof (CoglFramebufferBits, depth),
99         },
100         {
101           .attachment = GL_STENCIL,
102           .pname = GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE,
103           .offset = offsetof (CoglFramebufferBits, stencil),
104         },
105       };
106       int i;
107 
108       for (i = 0; i < G_N_ELEMENTS (params); i++)
109         {
110           int *value =
111             (int *) ((uint8_t *) bits + params[i].offset);
112 
113           GE (ctx, glGetFramebufferAttachmentParameteriv (GL_FRAMEBUFFER,
114                                                           params[i].attachment,
115                                                           params[i].pname,
116                                                           value));
117         }
118     }
119   else
120 #endif /* HAVE_COGL_GL */
121     {
122       GE (ctx, glGetIntegerv (GL_RED_BITS, &bits->red));
123       GE (ctx, glGetIntegerv (GL_GREEN_BITS, &bits->green));
124       GE (ctx, glGetIntegerv (GL_BLUE_BITS, &bits->blue));
125       GE (ctx, glGetIntegerv (GL_ALPHA_BITS, &bits->alpha));
126       GE (ctx, glGetIntegerv (GL_DEPTH_BITS, &bits->depth));
127       GE (ctx, glGetIntegerv (GL_STENCIL_BITS, &bits->stencil));
128     }
129 
130   COGL_NOTE (FRAMEBUFFER,
131              "RGBA/D/S Bits for framebuffer[%p, %s]: %d, %d, %d, %d, %d, %d",
132              framebuffer,
133              G_OBJECT_TYPE_NAME (framebuffer),
134              bits->red,
135              bits->blue,
136              bits->green,
137              bits->alpha,
138              bits->depth,
139              bits->stencil);
140 
141   gl_framebuffer_back->dirty_bitmasks = FALSE;
142 
143   return TRUE;
144 }
145 
146 static void
cogl_gl_framebuffer_back_query_bits(CoglFramebufferDriver * driver,CoglFramebufferBits * bits)147 cogl_gl_framebuffer_back_query_bits (CoglFramebufferDriver *driver,
148                                      CoglFramebufferBits   *bits)
149 {
150   CoglGlFramebufferBack *gl_framebuffer_back = COGL_GL_FRAMEBUFFER_BACK (driver);
151 
152   if (!ensure_bits_initialized (gl_framebuffer_back))
153     return;
154 
155   *bits = gl_framebuffer_back->bits;
156 }
157 
158 static void
cogl_gl_framebuffer_back_discard_buffers(CoglFramebufferDriver * driver,unsigned long buffers)159 cogl_gl_framebuffer_back_discard_buffers (CoglFramebufferDriver *driver,
160                                           unsigned long          buffers)
161 {
162   CoglFramebuffer *framebuffer =
163     cogl_framebuffer_driver_get_framebuffer (driver);
164   CoglContext *ctx = cogl_framebuffer_get_context (framebuffer);
165   GLenum attachments[3];
166   int i = 0;
167 
168   if (!ctx->glDiscardFramebuffer)
169     return;
170 
171   if (buffers & COGL_BUFFER_BIT_COLOR)
172     attachments[i++] = GL_COLOR;
173   if (buffers & COGL_BUFFER_BIT_DEPTH)
174     attachments[i++] = GL_DEPTH;
175   if (buffers & COGL_BUFFER_BIT_STENCIL)
176     attachments[i++] = GL_STENCIL;
177 
178   cogl_context_flush_framebuffer_state (ctx,
179                                         framebuffer,
180                                         framebuffer,
181                                         COGL_FRAMEBUFFER_STATE_BIND);
182   GE (ctx, glDiscardFramebuffer (GL_FRAMEBUFFER, i, attachments));
183 }
184 
185 static void
cogl_gl_framebuffer_back_bind(CoglGlFramebuffer * gl_framebuffer,GLenum target)186 cogl_gl_framebuffer_back_bind (CoglGlFramebuffer *gl_framebuffer,
187                                GLenum             target)
188 {
189   CoglGlFramebufferBack *gl_framebuffer_back =
190     COGL_GL_FRAMEBUFFER_BACK (gl_framebuffer);
191   CoglFramebufferDriver *driver = COGL_FRAMEBUFFER_DRIVER (gl_framebuffer_back);
192   CoglFramebuffer *framebuffer =
193     cogl_framebuffer_driver_get_framebuffer (driver);
194   CoglContext *ctx = cogl_framebuffer_get_context (framebuffer);
195 
196   cogl_onscreen_bind (COGL_ONSCREEN (framebuffer));
197 
198   GE (ctx, glBindFramebuffer (target, 0));
199 
200   /* Initialise the glDrawBuffer state the first time the context
201    * is bound to the default framebuffer. If the winsys is using a
202    * surfaceless context for the initial make current then the
203    * default draw buffer will be GL_NONE so we need to correct
204    * that. We can't do it any earlier because binding GL_BACK when
205    * there is no default framebuffer won't work */
206   if (!ctx->was_bound_to_onscreen)
207     {
208       if (ctx->glDrawBuffer)
209         {
210           GE (ctx, glDrawBuffer (GL_BACK));
211         }
212       else if (ctx->glDrawBuffers)
213         {
214           /* glDrawBuffer isn't available on GLES 3.0 so we need
215            * to be able to use glDrawBuffers as well. On GLES 2
216            * neither is available but the state should always be
217            * GL_BACK anyway so we don't need to set anything. On
218            * desktop GL this must be GL_BACK_LEFT instead of
219            * GL_BACK but as this code path will only be hit for
220            * GLES we can just use GL_BACK. */
221           static const GLenum buffers[] = { GL_BACK };
222 
223           GE (ctx, glDrawBuffers (G_N_ELEMENTS (buffers), buffers));
224         }
225 
226       ctx->was_bound_to_onscreen = TRUE;
227     }
228 }
229 
230 static void
cogl_gl_framebuffer_back_flush_stereo_mode_state(CoglGlFramebuffer * gl_framebuffer)231 cogl_gl_framebuffer_back_flush_stereo_mode_state (CoglGlFramebuffer *gl_framebuffer)
232 {
233   CoglFramebufferDriver *driver = COGL_FRAMEBUFFER_DRIVER (gl_framebuffer);
234   CoglFramebuffer *framebuffer =
235     cogl_framebuffer_driver_get_framebuffer (driver);
236   CoglContext *ctx = cogl_framebuffer_get_context (framebuffer);
237   GLenum draw_buffer = GL_BACK;
238 
239   if (!ctx->glDrawBuffer)
240     return;
241 
242   /* The one-shot default draw buffer setting in _cogl_framebuffer_gl_bind
243    * must have already happened. If not it would override what we set here. */
244   g_assert (ctx->was_bound_to_onscreen);
245 
246   switch (cogl_framebuffer_get_stereo_mode (framebuffer))
247     {
248     case COGL_STEREO_BOTH:
249       draw_buffer = GL_BACK;
250       break;
251     case COGL_STEREO_LEFT:
252       draw_buffer = GL_BACK_LEFT;
253       break;
254     case COGL_STEREO_RIGHT:
255       draw_buffer = GL_BACK_RIGHT;
256       break;
257     }
258 
259   if (ctx->current_gl_draw_buffer != draw_buffer)
260     {
261       GE (ctx, glDrawBuffer (draw_buffer));
262       ctx->current_gl_draw_buffer = draw_buffer;
263     }
264 }
265 
266 CoglGlFramebufferBack *
cogl_gl_framebuffer_back_new(CoglFramebuffer * framebuffer,const CoglFramebufferDriverConfig * driver_config,GError ** error)267 cogl_gl_framebuffer_back_new (CoglFramebuffer                    *framebuffer,
268                               const CoglFramebufferDriverConfig  *driver_config,
269                               GError                            **error)
270 {
271   if (!COGL_IS_ONSCREEN (framebuffer))
272     {
273       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
274                    "Incompatible framebuffer");
275       return NULL;
276     }
277 
278   return g_object_new (COGL_TYPE_GL_FRAMEBUFFER_BACK,
279                        "framebuffer", framebuffer,
280                        NULL);
281 }
282 
283 static void
cogl_gl_framebuffer_back_init(CoglGlFramebufferBack * gl_framebuffer_back)284 cogl_gl_framebuffer_back_init (CoglGlFramebufferBack *gl_framebuffer_back)
285 {
286   gl_framebuffer_back->dirty_bitmasks = TRUE;
287 }
288 
289 static void
cogl_gl_framebuffer_back_class_init(CoglGlFramebufferBackClass * klass)290 cogl_gl_framebuffer_back_class_init (CoglGlFramebufferBackClass *klass)
291 {
292   CoglFramebufferDriverClass *driver_class =
293     COGL_FRAMEBUFFER_DRIVER_CLASS (klass);
294   CoglGlFramebufferClass *gl_framebuffer_class =
295     COGL_GL_FRAMEBUFFER_CLASS (klass);
296 
297   driver_class->query_bits = cogl_gl_framebuffer_back_query_bits;
298   driver_class->discard_buffers = cogl_gl_framebuffer_back_discard_buffers;
299 
300   gl_framebuffer_class->bind = cogl_gl_framebuffer_back_bind;
301   gl_framebuffer_class->flush_stereo_mode_state =
302     cogl_gl_framebuffer_back_flush_stereo_mode_state;
303 }
304