xref: /OK3568_Linux_fs/external/xserver/glamor/glamor_fbo.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright © 2009 Intel Corporation
3  * Copyright © 1998 Keith Packard
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Zhigang Gong <zhigang.gong@gmail.com>
26  *
27  */
28 
29 #include <stdlib.h>
30 
31 #include "glamor_priv.h"
32 
33 void
glamor_destroy_fbo(glamor_screen_private * glamor_priv,glamor_pixmap_fbo * fbo)34 glamor_destroy_fbo(glamor_screen_private *glamor_priv,
35                    glamor_pixmap_fbo *fbo)
36 {
37     glamor_make_current(glamor_priv);
38 
39     if (fbo->fb)
40         glDeleteFramebuffers(1, &fbo->fb);
41     if (fbo->tex)
42         glDeleteTextures(1, &fbo->tex);
43 
44     free(fbo);
45 }
46 
47 static int
glamor_pixmap_ensure_fb(glamor_screen_private * glamor_priv,glamor_pixmap_fbo * fbo)48 glamor_pixmap_ensure_fb(glamor_screen_private *glamor_priv,
49                         glamor_pixmap_fbo *fbo)
50 {
51     int status, err = 0;
52 
53     glamor_make_current(glamor_priv);
54 
55     if (fbo->fb == 0)
56         glGenFramebuffers(1, &fbo->fb);
57     assert(fbo->tex != 0);
58     glBindFramebuffer(GL_FRAMEBUFFER, fbo->fb);
59     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
60                            GL_TEXTURE_2D, fbo->tex, 0);
61     status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
62     if (status != GL_FRAMEBUFFER_COMPLETE) {
63         const char *str;
64 
65         switch (status) {
66         case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
67             str = "incomplete attachment";
68             break;
69         case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
70             str = "incomplete/missing attachment";
71             break;
72         case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
73             str = "incomplete draw buffer";
74             break;
75         case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
76             str = "incomplete read buffer";
77             break;
78         case GL_FRAMEBUFFER_UNSUPPORTED:
79             str = "unsupported";
80             break;
81         case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
82             str = "incomplete multiple";
83             break;
84         default:
85             str = "unknown error";
86             break;
87         }
88 
89         glamor_fallback("glamor: Failed to create fbo, %s\n", str);
90         err = -1;
91     }
92 
93     return err;
94 }
95 
96 glamor_pixmap_fbo *
glamor_create_fbo_from_tex(glamor_screen_private * glamor_priv,PixmapPtr pixmap,int w,int h,GLint tex,int flag)97 glamor_create_fbo_from_tex(glamor_screen_private *glamor_priv,
98                            PixmapPtr pixmap, int w, int h, GLint tex, int flag)
99 {
100     const struct glamor_format *f = glamor_format_for_pixmap(pixmap);
101     glamor_pixmap_fbo *fbo;
102 
103     fbo = calloc(1, sizeof(*fbo));
104     if (fbo == NULL)
105         return NULL;
106 
107     fbo->tex = tex;
108     fbo->width = w;
109     fbo->height = h;
110     fbo->is_red = f->format == GL_RED;
111 
112     if (flag != GLAMOR_CREATE_FBO_NO_FBO) {
113         if (glamor_pixmap_ensure_fb(glamor_priv, fbo) != 0) {
114             glamor_destroy_fbo(glamor_priv, fbo);
115             fbo = NULL;
116         }
117     }
118 
119     return fbo;
120 }
121 
122 static int
_glamor_create_tex(glamor_screen_private * glamor_priv,PixmapPtr pixmap,int w,int h)123 _glamor_create_tex(glamor_screen_private *glamor_priv,
124                    PixmapPtr pixmap, int w, int h)
125 {
126     const struct glamor_format *f = glamor_format_for_pixmap(pixmap);
127     unsigned int tex;
128 
129     glamor_make_current(glamor_priv);
130     glGenTextures(1, &tex);
131     glBindTexture(GL_TEXTURE_2D, tex);
132     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
133     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
134     if (f->format == GL_RED)
135         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_RED);
136     glamor_priv->suppress_gl_out_of_memory_logging = true;
137     glTexImage2D(GL_TEXTURE_2D, 0, f->internalformat, w, h, 0,
138                  f->format, f->type, NULL);
139     glamor_priv->suppress_gl_out_of_memory_logging = false;
140 
141     if (glGetError() == GL_OUT_OF_MEMORY) {
142         if (!glamor_priv->logged_any_fbo_allocation_failure) {
143             LogMessageVerb(X_WARNING, 0, "glamor: Failed to allocate %dx%d "
144                            "FBO due to GL_OUT_OF_MEMORY.\n", w, h);
145             LogMessageVerb(X_WARNING, 0,
146                            "glamor: Expect reduced performance.\n");
147             glamor_priv->logged_any_fbo_allocation_failure = true;
148         }
149         glDeleteTextures(1, &tex);
150         return 0;
151     }
152 
153     return tex;
154 }
155 
156 glamor_pixmap_fbo *
glamor_create_fbo(glamor_screen_private * glamor_priv,PixmapPtr pixmap,int w,int h,int flag)157 glamor_create_fbo(glamor_screen_private *glamor_priv,
158                   PixmapPtr pixmap, int w, int h, int flag)
159 {
160     GLint tex = _glamor_create_tex(glamor_priv, pixmap, w, h);
161 
162     if (!tex) /* Texture creation failed due to GL_OUT_OF_MEMORY */
163         return NULL;
164 
165     return glamor_create_fbo_from_tex(glamor_priv, pixmap, w, h,
166                                       tex, flag);
167 }
168 
169 /**
170  * Create storage for the w * h region, using FBOs of the GL's maximum
171  * supported size.
172  */
173 glamor_pixmap_fbo *
glamor_create_fbo_array(glamor_screen_private * glamor_priv,PixmapPtr pixmap,int flag,int block_w,int block_h,glamor_pixmap_private * priv)174 glamor_create_fbo_array(glamor_screen_private *glamor_priv,
175                         PixmapPtr pixmap, int flag,
176                          int block_w, int block_h,
177                          glamor_pixmap_private *priv)
178 {
179     int w = pixmap->drawable.width;
180     int h = pixmap->drawable.height;
181     int block_wcnt;
182     int block_hcnt;
183     glamor_pixmap_fbo **fbo_array;
184     BoxPtr box_array;
185     int i, j;
186 
187     priv->block_w = block_w;
188     priv->block_h = block_h;
189 
190     block_wcnt = (w + block_w - 1) / block_w;
191     block_hcnt = (h + block_h - 1) / block_h;
192 
193     box_array = calloc(block_wcnt * block_hcnt, sizeof(box_array[0]));
194     if (box_array == NULL)
195         return NULL;
196 
197     fbo_array = calloc(block_wcnt * block_hcnt, sizeof(glamor_pixmap_fbo *));
198     if (fbo_array == NULL) {
199         free(box_array);
200         return FALSE;
201     }
202     for (i = 0; i < block_hcnt; i++) {
203         int block_y1, block_y2;
204         int fbo_w, fbo_h;
205 
206         block_y1 = i * block_h;
207         block_y2 = (block_y1 + block_h) > h ? h : (block_y1 + block_h);
208         fbo_h = block_y2 - block_y1;
209 
210         for (j = 0; j < block_wcnt; j++) {
211             box_array[i * block_wcnt + j].x1 = j * block_w;
212             box_array[i * block_wcnt + j].y1 = block_y1;
213             box_array[i * block_wcnt + j].x2 =
214                 (j + 1) * block_w > w ? w : (j + 1) * block_w;
215             box_array[i * block_wcnt + j].y2 = block_y2;
216             fbo_w =
217                 box_array[i * block_wcnt + j].x2 - box_array[i * block_wcnt +
218                                                              j].x1;
219             fbo_array[i * block_wcnt + j] = glamor_create_fbo(glamor_priv,
220                                                               pixmap,
221                                                               fbo_w, fbo_h,
222                                                               GLAMOR_CREATE_PIXMAP_FIXUP);
223             if (fbo_array[i * block_wcnt + j] == NULL)
224                 goto cleanup;
225         }
226     }
227 
228     priv->box = box_array[0];
229     priv->box_array = box_array;
230     priv->fbo_array = fbo_array;
231     priv->block_wcnt = block_wcnt;
232     priv->block_hcnt = block_hcnt;
233     return fbo_array[0];
234 
235  cleanup:
236     for (i = 0; i < block_wcnt * block_hcnt; i++)
237         if (fbo_array[i])
238             glamor_destroy_fbo(glamor_priv, fbo_array[i]);
239     free(box_array);
240     free(fbo_array);
241     return NULL;
242 }
243 
244 void
glamor_pixmap_clear_fbo(glamor_screen_private * glamor_priv,glamor_pixmap_fbo * fbo)245 glamor_pixmap_clear_fbo(glamor_screen_private *glamor_priv, glamor_pixmap_fbo *fbo)
246 {
247     glamor_make_current(glamor_priv);
248 
249     assert(fbo->fb != 0 && fbo->tex != 0);
250 
251     glamor_set_destination_pixmap_fbo(glamor_priv, fbo, 0, 0, fbo->width, fbo->height);
252     glClearColor(0.0, 0.0, 0.0, 0.0);
253     glClear(GL_COLOR_BUFFER_BIT);
254 }
255 
256 glamor_pixmap_fbo *
glamor_pixmap_detach_fbo(glamor_pixmap_private * pixmap_priv)257 glamor_pixmap_detach_fbo(glamor_pixmap_private *pixmap_priv)
258 {
259     glamor_pixmap_fbo *fbo;
260 
261     if (pixmap_priv == NULL)
262         return NULL;
263 
264     fbo = pixmap_priv->fbo;
265     if (fbo == NULL)
266         return NULL;
267 
268     pixmap_priv->fbo = NULL;
269     return fbo;
270 }
271 
272 /* The pixmap must not be attached to another fbo. */
273 void
glamor_pixmap_attach_fbo(PixmapPtr pixmap,glamor_pixmap_fbo * fbo)274 glamor_pixmap_attach_fbo(PixmapPtr pixmap, glamor_pixmap_fbo *fbo)
275 {
276     glamor_pixmap_private *pixmap_priv;
277 
278     pixmap_priv = glamor_get_pixmap_private(pixmap);
279 
280     if (pixmap_priv->fbo)
281         return;
282 
283     pixmap_priv->fbo = fbo;
284 
285     switch (pixmap_priv->type) {
286     case GLAMOR_TEXTURE_ONLY:
287     case GLAMOR_TEXTURE_DRM:
288         pixmap_priv->gl_fbo = GLAMOR_FBO_NORMAL;
289         pixmap->devPrivate.ptr = NULL;
290     default:
291         break;
292     }
293 }
294 
295 void
glamor_pixmap_destroy_fbo(PixmapPtr pixmap)296 glamor_pixmap_destroy_fbo(PixmapPtr pixmap)
297 {
298     ScreenPtr screen = pixmap->drawable.pScreen;
299     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
300     glamor_pixmap_private *priv = glamor_get_pixmap_private(pixmap);
301     glamor_pixmap_fbo *fbo;
302 
303     if (glamor_pixmap_priv_is_large(priv)) {
304         int i;
305 
306         for (i = 0; i < priv->block_wcnt * priv->block_hcnt; i++)
307             glamor_destroy_fbo(glamor_priv, priv->fbo_array[i]);
308         free(priv->fbo_array);
309         priv->fbo_array = NULL;
310     }
311     else {
312         fbo = glamor_pixmap_detach_fbo(priv);
313         if (fbo)
314             glamor_destroy_fbo(glamor_priv, fbo);
315     }
316 }
317 
318 Bool
glamor_pixmap_ensure_fbo(PixmapPtr pixmap,int flag)319 glamor_pixmap_ensure_fbo(PixmapPtr pixmap, int flag)
320 {
321     glamor_screen_private *glamor_priv;
322     glamor_pixmap_private *pixmap_priv;
323     glamor_pixmap_fbo *fbo;
324 
325     glamor_priv = glamor_get_screen_private(pixmap->drawable.pScreen);
326     pixmap_priv = glamor_get_pixmap_private(pixmap);
327     if (pixmap_priv->fbo == NULL) {
328 
329         fbo = glamor_create_fbo(glamor_priv, pixmap, pixmap->drawable.width,
330                                 pixmap->drawable.height, flag);
331         if (fbo == NULL)
332             return FALSE;
333 
334         glamor_pixmap_attach_fbo(pixmap, fbo);
335     }
336     else {
337         /* We do have a fbo, but it may lack of fb or tex. */
338         if (!pixmap_priv->fbo->tex)
339             pixmap_priv->fbo->tex =
340                 _glamor_create_tex(glamor_priv, pixmap, pixmap->drawable.width,
341                                    pixmap->drawable.height);
342 
343         if (flag != GLAMOR_CREATE_FBO_NO_FBO && pixmap_priv->fbo->fb == 0)
344             if (glamor_pixmap_ensure_fb(glamor_priv, pixmap_priv->fbo) != 0)
345                 return FALSE;
346     }
347 
348     return TRUE;
349 }
350 
351 _X_EXPORT void
glamor_pixmap_exchange_fbos(PixmapPtr front,PixmapPtr back)352 glamor_pixmap_exchange_fbos(PixmapPtr front, PixmapPtr back)
353 {
354     glamor_pixmap_private *front_priv, *back_priv;
355     glamor_pixmap_fbo *temp_fbo;
356 
357     front_priv = glamor_get_pixmap_private(front);
358     back_priv = glamor_get_pixmap_private(back);
359     temp_fbo = front_priv->fbo;
360     front_priv->fbo = back_priv->fbo;
361     back_priv->fbo = temp_fbo;
362 }
363