1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* Contributor(s): 2007 Blender Foundation (refactor)
*
* ***** END GPL LICENSE BLOCK *****
*
*
* Subwindow opengl handling.
* BTW: subwindows open/close in X11 are way too slow, tried it, and choose for my own system... (ton)
*
*/
/** \file blender/windowmanager/intern/wm_subwindow.c
* \ingroup wm
*/
#include <string.h>
#include "MEM_guardedalloc.h"
#include "DNA_windowmanager_types.h"
#include "DNA_screen_types.h"
#include "BLI_blenlib.h"
#include "BLI_math.h"
#include "BLI_utildefines.h"
#include "BKE_context.h"
#include "BKE_global.h"
#include "GPU_compatibility.h"
#include "GPU_extensions.h"
#include "WM_api.h"
#include "wm_subwindow.h"
#include "wm_window.h"
/* wmSubWindow stored in wmWindow... but not exposed outside this C file */
/* it seems a bit redundant (area regions can store it too, but we keep it
* because we can store all kind of future opengl fanciness here */
/* we use indices and array because:
* - index has safety, no pointers from this C file hanging around
* - fast lookups of indices with array, list would give overhead
* - old code used it this way...
* - keep option open to have 2 screens using same window
*/
typedef struct wmSubWindow {
struct wmSubWindow *next, *prev;
rcti winrct;
int swinid;
} wmSubWindow;
/* ******************* open, free, set, get data ******************** */
/* not subwindow itself */
static void wm_subwindow_free(wmSubWindow *UNUSED(swin))
{
/* future fancy stuff */
}
void wm_subwindows_free(wmWindow *win)
{
wmSubWindow *swin;
for (swin = win->subwindows.first; swin; swin = swin->next)
wm_subwindow_free(swin);
BLI_freelistN(&win->subwindows);
}
int wm_subwindow_get(wmWindow *win)
{
if (win->curswin)
return win->curswin->swinid;
return 0;
}
static wmSubWindow *swin_from_swinid(wmWindow *win, int swinid)
{
wmSubWindow *swin;
for (swin = win->subwindows.first; swin; swin = swin->next)
if (swin->swinid == swinid)
break;
return swin;
}
void wm_subwindow_getsize(wmWindow *win, int swinid, int *x, int *y)
{
wmSubWindow *swin = swin_from_swinid(win, swinid);
if (swin) {
*x = swin->winrct.xmax - swin->winrct.xmin + 1;
*y = swin->winrct.ymax - swin->winrct.ymin + 1;
}
}
void wm_subwindow_getorigin(wmWindow *win, int swinid, int *x, int *y)
{
wmSubWindow *swin = swin_from_swinid(win, swinid);
if (swin) {
*x = swin->winrct.xmin;
*y = swin->winrct.ymin;
}
}
void wm_subwindow_getmatrix(wmWindow *win, int swinid, float mat[][4])
{
wmSubWindow *swin = swin_from_swinid(win, swinid);
if (swin) {
/* used by UI, should find a better way to get the matrix there */
if (swinid == win->screen->mainwin) {
int width, height;
wm_subwindow_getsize(win, swin->swinid, &width, &height);
orthographic_m4(mat, -0.375f, (float)width - 0.375f, -0.375f, (float)height - 0.375f, -100, 100);
}
else {
gpuGetMatrix(GL_PROJECTION_MATRIX, (float *)mat);
}
}
}
/* always sets pixel-precise 2D window/view matrices */
/* coords is in whole pixels. xmin = 15, xmax = 16: means window is 2 pix big */
int wm_subwindow_open(wmWindow *win, rcti *winrct)
{
wmSubWindow *swin;
int width, height;
int freewinid = 1;
for (swin = win->subwindows.first; swin; swin = swin->next)
if (freewinid <= swin->swinid)
freewinid = swin->swinid + 1;
win->curswin = swin = MEM_callocN(sizeof(wmSubWindow), "swinopen");
BLI_addtail(&win->subwindows, swin);
if (G.debug & G_DEBUG_EVENTS) {
printf("%s: swin %d added\n", __func__, freewinid);
}
swin->swinid = freewinid;
swin->winrct = *winrct;
/* and we appy it all right away */
wmSubWindowSet(win, swin->swinid);
/* extra service */
wm_subwindow_getsize(win, swin->swinid, &width, &height);
wmOrtho2(-0.375f, (float)width - 0.375f, -0.375f, (float)height - 0.375f);
gpuLoadIdentity();
return swin->swinid;
}
void wm_subwindow_close(wmWindow *win, int swinid)
{
wmSubWindow *swin = swin_from_swinid(win, swinid);
if (swin) {
if (swin == win->curswin)
win->curswin = NULL;
wm_subwindow_free(swin);
BLI_remlink(&win->subwindows, swin);
MEM_freeN(swin);
}
else {
printf("%s: Internal error, bad winid: %d\n", __func__, swinid);
}
}
/* pixels go from 0-99 for a 100 pixel window */
void wm_subwindow_position(wmWindow *win, int swinid, rcti *winrct)
{
wmSubWindow *swin = swin_from_swinid(win, swinid);
if (swin) {
int width, height;
swin->winrct = *winrct;
/* CRITICAL, this clamping ensures that
* the viewport never goes outside the screen
* edges (assuming the x, y coords aren't
* outside). This caused a hardware lock
* on Matrox cards if it happens.
*
* Really Blender should never _ever_ try
* to do such a thing, but just to be safe
* clamp it anyway (or fix the bScreen
* scaling routine, and be damn sure you
* fixed it). - zr (2001!)
*/
if (swin->winrct.xmax > win->sizex)
swin->winrct.xmax = win->sizex;
if (swin->winrct.ymax > win->sizey)
swin->winrct.ymax = win->sizey;
/* extra service */
wmSubWindowSet(win, swinid);
wm_subwindow_getsize(win, swinid, &width, &height);
wmOrtho2(-0.375f, (float)width - 0.375f, -0.375f, (float)height - 0.375f);
}
else {
printf("%s: Internal error, bad winid: %d\n", __func__, swinid);
}
}
/* ---------------- WM versions of OpenGL style API calls ------------------------ */
/* ----------------- exported in WM_api.h ------------------------------------------------------ */
/* internal state, no threaded opengl! XXX */
static wmWindow *_curwindow = NULL;
static wmSubWindow *_curswin = NULL;
void wmSubWindowScissorSet(wmWindow *win, int swinid, rcti *srct)
{
int x, y, width, height;
_curswin = swin_from_swinid(win, swinid);
if (_curswin == NULL) {
printf("%s %d: doesn't exist\n", __func__, swinid);
return;
}
win->curswin = _curswin;
_curwindow = win;
x = _curswin->winrct.xmin;
y = _curswin->winrct.ymin;
width = _curswin->winrct.xmax - _curswin->winrct.xmin + 1;
height = _curswin->winrct.ymax - _curswin->winrct.ymin + 1;
gpuViewport(x, y, width, height);
if (srct) {
x = srct->xmin;
y = srct->ymin;
width = srct->xmax - srct->xmin + 1;
height = srct->ymax - srct->ymin + 1;
}
gpuScissor(x, y, width, height);
wmOrtho2(-0.375f, (float)width - 0.375f, -0.375f, (float)height - 0.375f);
gpuLoadIdentity(); /* reset MODELVIEW */
}
/* enable the WM versions of opengl calls */
void wmSubWindowSet(wmWindow *win, int swinid)
{
wmSubWindowScissorSet(win, swinid, NULL);
}
void wmFrustum(float x1, float x2, float y1, float y2, float n, float f)
{
gpuMatrixMode(GL_PROJECTION);
gpuLoadFrustum(x1, x2, y1, y2, n, f);
gpuMatrixMode(GL_MODELVIEW);
}
void wmOrtho(float x1, float x2, float y1, float y2, float n, float f)
{
gpuMatrixMode(GL_PROJECTION);
gpuLoadOrtho(x1, x2, y1, y2, n, f);
gpuMatrixMode(GL_MODELVIEW);
}
void wmOrtho2(float x1, float x2, float y1, float y2)
{
/* make sure the window rectangle is not degenerate */
if (x1 == x2) {
x2 += 1.0f;
}
if (y1 == y2) {
y2 += 1.0f;
}
wmOrtho(x1, x2, y1, y2, -100, 100);
}
/* *************************** Framebuffer color depth, for selection codes ********************** */
#ifdef __APPLE__
/* apple seems to round colors to below and up on some configs */
unsigned int index_to_framebuffer(int index)
{
unsigned int i = index;
switch (GPU_color_depth()) {
case 12:
i = ((i & 0xF00) << 12) + ((i & 0xF0) << 8) + ((i & 0xF) << 4);
/* sometimes dithering subtracts! */
i |= 0x070707;
break;
case 15:
case 16:
i = ((i & 0x7C00) << 9) + ((i & 0x3E0) << 6) + ((i & 0x1F) << 3);
i |= 0x030303;
break;
case 24:
break;
default: /* 18 bits... */
i = ((i & 0x3F000) << 6) + ((i & 0xFC0) << 4) + ((i & 0x3F) << 2);
i |= 0x010101;
break;
}
return i;
}
#else
/* this is the old method as being in use for ages.... seems to work? colors are rounded to lower values */
unsigned int index_to_framebuffer(int index)
{
unsigned int i = index;
switch (GPU_color_depth()) {
case 8:
i = ((i & 48) << 18) + ((i & 12) << 12) + ((i & 3) << 6);
i |= 0x3F3F3F;
break;
case 12:
i = ((i & 0xF00) << 12) + ((i & 0xF0) << 8) + ((i & 0xF) << 4);
/* sometimes dithering subtracts! */
i |= 0x0F0F0F;
break;
case 15:
case 16:
i = ((i & 0x7C00) << 9) + ((i & 0x3E0) << 6) + ((i & 0x1F) << 3);
i |= 0x070707;
break;
case 24:
break;
default: /* 18 bits... */
i = ((i & 0x3F000) << 6) + ((i & 0xFC0) << 4) + ((i & 0x3F) << 2);
i |= 0x030303;
break;
}
return i;
}
#endif
void WM_set_framebuffer_index_color(int index)
{
gpuColor3x(index_to_framebuffer(index));
}
void WM_set_framebuffer_index_current_color(int index)
{
gpuCurrentColor3x(index_to_framebuffer(index));
}
int WM_framebuffer_to_index(unsigned int col)
{
if (col == 0) return 0;
switch (GPU_color_depth()) {
case 8:
return ((col & 0xC00000) >> 18) + ((col & 0xC000) >> 12) + ((col & 0xC0) >> 6);
case 12:
return ((col & 0xF00000) >> 12) + ((col & 0xF000) >> 8) + ((col & 0xF0) >> 4);
case 15:
case 16:
return ((col & 0xF80000) >> 9) + ((col & 0xF800) >> 6) + ((col & 0xF8) >> 3);
case 24:
return col & 0xFFFFFF;
default: // 18 bits...
return ((col & 0xFC0000) >> 6) + ((col & 0xFC00) >> 4) + ((col & 0xFC) >> 2);
}
}
/* ********** END MY WINDOW ************** */
|