2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * Contributor(s): 2007 Blender Foundation (refactor)
23 * ***** END GPL LICENSE BLOCK *****
26 * Subwindow opengl handling.
27 * BTW: subwindows open/close in X11 are way too slow, tried it, and choose for my own system... (ton)
31 /** \file blender/windowmanager/intern/wm_subwindow.c
34 * Internal subwindows used for OpenGL state, used for regions and screens.
39 #include "MEM_guardedalloc.h"
41 #include "DNA_windowmanager_types.h"
42 #include "DNA_screen_types.h"
44 #include "BLI_blenlib.h"
46 #include "BLI_utildefines.h"
50 #include "GPU_extensions.h"
53 #include "wm_subwindow.h"
56 * \note #wmSubWindow stored in #wmWindow but not exposed outside this C file,
57 * it seems a bit redundant (area regions can store it too, but we keep it
58 * because we can store all kind of future opengl fanciness here.
60 * We use indices and array because:
61 * - index has safety, no pointers from this C file hanging around
62 * - fast lookups of indices with array, list would give overhead
63 * - old code used it this way...
64 * - keep option open to have 2 screens using same window
67 typedef struct wmSubWindow {
68 struct wmSubWindow *next, *prev;
75 /* ******************* open, free, set, get data ******************** */
77 /* not subwindow itself */
78 static void wm_subwindow_free(wmSubWindow *UNUSED(swin))
80 /* future fancy stuff */
83 void wm_subwindows_free(wmWindow *win)
87 for (swin = win->subwindows.first; swin; swin = swin->next)
88 wm_subwindow_free(swin);
90 BLI_freelistN(&win->subwindows);
94 int wm_subwindow_get_id(wmWindow *win)
97 return win->curswin->swinid;
101 static wmSubWindow *swin_from_swinid(wmWindow *win, int swinid)
105 for (swin = win->subwindows.first; swin; swin = swin->next)
106 if (swin->swinid == swinid)
112 static void wm_swin_size_get(wmSubWindow *swin, int *x, int *y)
114 *x = BLI_rcti_size_x(&swin->winrct) + 1;
115 *y = BLI_rcti_size_y(&swin->winrct) + 1;
117 void wm_subwindow_size_get(wmWindow *win, int swinid, int *x, int *y)
119 wmSubWindow *swin = swin_from_swinid(win, swinid);
122 wm_swin_size_get(swin, x, y);
127 static void wm_swin_origin_get(wmSubWindow *swin, int *x, int *y)
129 *x = swin->winrct.xmin;
130 *y = swin->winrct.ymin;
132 void wm_subwindow_origin_get(wmWindow *win, int swinid, int *x, int *y)
134 wmSubWindow *swin = swin_from_swinid(win, swinid);
137 wm_swin_origin_get(swin, x, y);
142 static void wm_swin_matrix_get(wmWindow *win, wmSubWindow *swin, float mat[4][4])
144 /* used by UI, should find a better way to get the matrix there */
145 if (swin->swinid == win->screen->mainwin) {
148 wm_swin_size_get(swin, &width, &height);
149 orthographic_m4(mat, -GLA_PIXEL_OFS, (float)width - GLA_PIXEL_OFS, -GLA_PIXEL_OFS, (float)height - GLA_PIXEL_OFS, -100, 100);
152 glGetFloatv(GL_PROJECTION_MATRIX, (float *)mat);
155 void wm_subwindow_matrix_get(wmWindow *win, int swinid, float mat[4][4])
157 wmSubWindow *swin = swin_from_swinid(win, swinid);
160 wm_swin_matrix_get(win, swin, mat);
165 static void wm_swin_rect_get(wmSubWindow *swin, rcti *r_rect)
167 *r_rect = swin->winrct;
169 void wm_subwindow_rect_get(wmWindow *win, int swinid, rcti *r_rect)
171 wmSubWindow *swin = swin_from_swinid(win, swinid);
174 wm_swin_rect_get(swin, r_rect);
179 static void wm_swin_rect_set(wmSubWindow *swin, const rcti *rect)
181 swin->winrct = *rect;
183 void wm_subwindow_rect_set(wmWindow *win, int swinid, const rcti *rect)
185 wmSubWindow *swin = swin_from_swinid(win, swinid);
188 wm_swin_rect_set(swin, rect);
193 /* always sets pixel-precise 2D window/view matrices */
194 /* coords is in whole pixels. xmin = 15, xmax = 16: means window is 2 pix big */
195 int wm_subwindow_open(wmWindow *win, const rcti *winrct)
201 for (swin = win->subwindows.first; swin; swin = swin->next)
202 if (freewinid <= swin->swinid)
203 freewinid = swin->swinid + 1;
205 win->curswin = swin = MEM_callocN(sizeof(wmSubWindow), "swinopen");
206 BLI_addtail(&win->subwindows, swin);
208 swin->swinid = freewinid;
209 swin->winrct = *winrct;
211 /* and we appy it all right away */
212 wmSubWindowSet(win, swin->swinid);
215 wm_swin_size_get(swin, &width, &height);
216 wmOrtho2_pixelspace(width, height);
223 void wm_subwindow_close(wmWindow *win, int swinid)
225 wmSubWindow *swin = swin_from_swinid(win, swinid);
228 if (swin == win->curswin)
230 wm_subwindow_free(swin);
231 BLI_remlink(&win->subwindows, swin);
235 printf("%s: Internal error, bad winid: %d\n", __func__, swinid);
239 /* pixels go from 0-99 for a 100 pixel window */
240 void wm_subwindow_position(wmWindow *win, int swinid, const rcti *winrct)
242 wmSubWindow *swin = swin_from_swinid(win, swinid);
245 const int winsize_x = WM_window_pixels_x(win);
246 const int winsize_y = WM_window_pixels_y(win);
250 swin->winrct = *winrct;
252 /* CRITICAL, this clamping ensures that
253 * the viewport never goes outside the screen
254 * edges (assuming the x, y coords aren't
255 * outside). This caused a hardware lock
256 * on Matrox cards if it happens.
258 * Really Blender should never _ever_ try
259 * to do such a thing, but just to be safe
260 * clamp it anyway (or fix the bScreen
261 * scaling routine, and be damn sure you
262 * fixed it). - zr (2001!)
265 if (swin->winrct.xmax > winsize_x)
266 swin->winrct.xmax = winsize_x;
267 if (swin->winrct.ymax > winsize_y)
268 swin->winrct.ymax = winsize_y;
271 wmSubWindowSet(win, swinid);
272 wm_swin_size_get(swin, &width, &height);
273 wmOrtho2_pixelspace(width, height);
276 printf("%s: Internal error, bad winid: %d\n", __func__, swinid);
280 /* ---------------- WM versions of OpenGL style API calls ------------------------ */
281 /* ----------------- exported in WM_api.h ------------------------------------------------------ */
283 /* internal state, no threaded opengl! XXX */
284 static wmWindow *_curwindow = NULL;
285 static wmSubWindow *_curswin = NULL;
287 void wmSubWindowScissorSet(wmWindow *win, int swinid, const rcti *srct, bool srct_pad)
290 _curswin = swin_from_swinid(win, swinid);
292 if (_curswin == NULL) {
293 printf("%s %d: doesn't exist\n", __func__, swinid);
297 win->curswin = _curswin;
300 width = BLI_rcti_size_x(&_curswin->winrct) + 1;
301 height = BLI_rcti_size_y(&_curswin->winrct) + 1;
302 glViewport(_curswin->winrct.xmin, _curswin->winrct.ymin, width, height);
305 int scissor_width = BLI_rcti_size_x(srct);
306 int scissor_height = BLI_rcti_size_y(srct);
308 /* typically a single pixel doesn't matter,
309 * but one pixel offset is noticeable with viewport border render */
315 glScissor(srct->xmin, srct->ymin, scissor_width, scissor_height);
318 glScissor(_curswin->winrct.xmin, _curswin->winrct.ymin, width, height);
320 wmOrtho2_pixelspace(width, height);
326 /* enable the WM versions of opengl calls */
327 void wmSubWindowSet(wmWindow *win, int swinid)
329 wmSubWindowScissorSet(win, swinid, NULL, true);
332 void wmFrustum(float x1, float x2, float y1, float y2, float n, float f)
334 glMatrixMode(GL_PROJECTION);
336 glFrustum(x1, x2, y1, y2, n, f);
337 glMatrixMode(GL_MODELVIEW);
340 void wmOrtho(float x1, float x2, float y1, float y2, float n, float f)
342 glMatrixMode(GL_PROJECTION);
345 glOrtho(x1, x2, y1, y2, n, f);
347 glMatrixMode(GL_MODELVIEW);
350 void wmOrtho2(float x1, float x2, float y1, float y2)
352 /* prevent opengl from generating errors */
353 if (x1 == x2) x2 += 1.0f;
354 if (y1 == y2) y2 += 1.0f;
356 wmOrtho(x1, x2, y1, y2, -100, 100);
359 static void wmOrtho2_offset(const float x, const float y, const float ofs)
361 wmOrtho2(ofs, x + ofs, ofs, y + ofs);
365 * default pixel alignment.
367 void wmOrtho2_region_pixelspace(const struct ARegion *ar)
369 wmOrtho2_offset(ar->winx, ar->winy, -0.01f);
372 void wmOrtho2_pixelspace(const float x, const float y)
374 wmOrtho2_offset(x, y, -GLA_PIXEL_OFS);
378 * use for drawing uiBlock, any UI elements and text.
379 * \note prevents blurry text with multi-sample (FSAA), see T41749
381 void wmOrtho2_region_ui(const ARegion *ar)
383 /* note, intentionally no '+ 1',
384 * as with wmOrtho2_region_pixelspace */
385 wmOrtho2_offset(ar->winx, ar->winy, -0.01f);
388 /* *************************** Framebuffer color depth, for selection codes ********************** */
392 /* apple seems to round colors to below and up on some configs */
394 unsigned int index_to_framebuffer(int index)
396 unsigned int i = index;
398 switch (GPU_color_depth()) {
400 i = ((i & 0xF00) << 12) + ((i & 0xF0) << 8) + ((i & 0xF) << 4);
401 /* sometimes dithering subtracts! */
406 i = ((i & 0x7C00) << 9) + ((i & 0x3E0) << 6) + ((i & 0x1F) << 3);
411 default: /* 18 bits... */
412 i = ((i & 0x3F000) << 6) + ((i & 0xFC0) << 4) + ((i & 0x3F) << 2);
422 /* this is the old method as being in use for ages.... seems to work? colors are rounded to lower values */
424 unsigned int index_to_framebuffer(int index)
426 unsigned int i = index;
428 switch (GPU_color_depth()) {
430 i = ((i & 48) << 18) + ((i & 12) << 12) + ((i & 3) << 6);
434 i = ((i & 0xF00) << 12) + ((i & 0xF0) << 8) + ((i & 0xF) << 4);
435 /* sometimes dithering subtracts! */
440 i = ((i & 0x7C00) << 9) + ((i & 0x3E0) << 6) + ((i & 0x1F) << 3);
445 default: /* 18 bits... */
446 i = ((i & 0x3F000) << 6) + ((i & 0xFC0) << 4) + ((i & 0x3F) << 2);
456 void WM_framebuffer_index_set(int index)
458 const int col = index_to_framebuffer(index);
462 void WM_framebuffer_index_get(int index, int *r_col)
464 const int col = index_to_framebuffer(index);
465 char *c_col = (char *)r_col;
466 c_col[0] = (col & 0xFF); /* red */
467 c_col[1] = ((col >> 8) & 0xFF); /* green */
468 c_col[2] = ((col >> 16) & 0xFF); /* blue */
469 c_col[3] = 0xFF; /* alpha */
474 #define INDEX_FROM_BUF_8(col) (((col & 0xC00000) >> 18) + ((col & 0xC000) >> 12) + ((col & 0xC0) >> 6))
475 #define INDEX_FROM_BUF_12(col) (((col & 0xF00000) >> 12) + ((col & 0xF000) >> 8) + ((col & 0xF0) >> 4))
476 #define INDEX_FROM_BUF_15_16(col) (((col & 0xF80000) >> 9) + ((col & 0xF800) >> 6) + ((col & 0xF8) >> 3))
477 #define INDEX_FROM_BUF_18(col) (((col & 0xFC0000) >> 6) + ((col & 0xFC00) >> 4) + ((col & 0xFC) >> 2))
478 #define INDEX_FROM_BUF_24(col) (col & 0xFFFFFF)
480 int WM_framebuffer_to_index(unsigned int col)
486 switch (GPU_color_depth()) {
487 case 8: return INDEX_FROM_BUF_8(col);
488 case 12: return INDEX_FROM_BUF_12(col);
490 case 16: return INDEX_FROM_BUF_15_16(col);
491 case 24: return INDEX_FROM_BUF_24(col);
492 default: return INDEX_FROM_BUF_18(col);
496 void WM_framebuffer_to_index_array(unsigned int *col, const unsigned int size)
498 #define INDEX_BUF_ARRAY(INDEX_FROM_BUF_BITS) \
499 for (i = size; i--; col++) { \
501 *col = INDEX_FROM_BUF_BITS(c); \
508 switch (GPU_color_depth()) {
510 INDEX_BUF_ARRAY(INDEX_FROM_BUF_8);
513 INDEX_BUF_ARRAY(INDEX_FROM_BUF_12);
517 INDEX_BUF_ARRAY(INDEX_FROM_BUF_15_16);
520 INDEX_BUF_ARRAY(INDEX_FROM_BUF_24);
523 INDEX_BUF_ARRAY(INDEX_FROM_BUF_18);
528 #undef INDEX_BUF_ARRAY
532 /* ********** END MY WINDOW ************** */