summaryrefslogtreecommitdiff
path: root/hw/darwin/quartz/fullscreen/fullscreen.c
blob: 470528943d9ef8a7f5a075c55ac64480dd5cbe01 (plain)
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
 * Screen routines for full screen Quartz mode
 *
 * Copyright (c) 2002-2003 Torrey T. Lyons. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * TORREY T. LYONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Except as contained in this notice, the name(s) of the above copyright
 * holders shall not be used in advertising or otherwise to promote the sale,
 * use or other dealings in this Software without prior written authorization.
 */
/* $XFree86: xc/programs/Xserver/hw/darwin/quartz/fullscreen/fullscreen.c,v 1.4 2003/12/09 04:41:27 torrey Exp $ */

#include "quartzCommon.h"
#include "darwin.h"
#include "quartz.h"
#include "quartzCursor.h"
#include "colormapst.h"
#include "scrnintstr.h"
#include "micmap.h"
#include "shadow.h"

// Full screen specific per screen storage structure
typedef struct {
    CGDirectDisplayID   displayID;
    CFDictionaryRef     xDisplayMode;
    CFDictionaryRef     aquaDisplayMode;
    CGDirectPaletteRef  xPalette;
    CGDirectPaletteRef  aquaPalette;
    unsigned char      *framebuffer;
    unsigned char      *shadowPtr;
} FSScreenRec, *FSScreenPtr;

#define FULLSCREEN_PRIV(pScreen) \
    ((FSScreenPtr)pScreen->devPrivates[fsScreenIndex].ptr)

static int                  fsScreenIndex;
static CGDirectDisplayID   *quartzDisplayList = NULL;
static int                  quartzNumScreens = 0;
static FSScreenPtr          quartzScreens[MAXSCREENS];

static int                  darwinCmapPrivateIndex = -1;
static unsigned long        darwinCmapGeneration = 0;

#define CMAP_PRIV(pCmap) \
    ((CGDirectPaletteRef) (pCmap)->devPrivates[darwinCmapPrivateIndex].ptr)

/*
 =============================================================================

 Colormap handling

 =============================================================================
*/

/*
 * FSInitCmapPrivates
 *  Colormap privates may be allocated after the default colormap has
 *  already been created for some screens.  This initialization procedure
 *  is called for each default colormap that is found.
 */
static Bool
FSInitCmapPrivates(
    ColormapPtr         pCmap)
{
    return TRUE;
}


/*
 * FSCreateColormap
 *  This is a callback from X after a new colormap is created.
 *  We allocate a new CoreGraphics pallete for each colormap.
 */
static Bool
FSCreateColormap(
    ColormapPtr         pCmap)
{
    CGDirectPaletteRef  pallete;

    // Allocate private storage for the hardware dependent colormap info.
    if (darwinCmapGeneration != serverGeneration) {
        if ((darwinCmapPrivateIndex =
                AllocateColormapPrivateIndex(FSInitCmapPrivates)) < 0)
        {
            return FALSE;
        }
        darwinCmapGeneration = serverGeneration;
    }

    pallete = CGPaletteCreateDefaultColorPalette();
    if (!pallete) return FALSE;

    CMAP_PRIV(pCmap) = pallete;
    return TRUE;
}


/*
 * FSDestroyColormap
 *  This is called by DIX FreeColormap after it has uninstalled a colormap
 *  and notified all interested parties. We deallocated the corresponding
 *  CoreGraphics pallete.
 */
static void
FSDestroyColormap(
    ColormapPtr         pCmap)
{
    CGPaletteRelease( CMAP_PRIV(pCmap) );
}


/*
 * FSInstallColormap
 *  Set the current CoreGraphics pallete to the pallete corresponding
 *  to the provided colormap.
 */
static void
FSInstallColormap(
    ColormapPtr         pCmap)
{
    CGDirectPaletteRef  palette = CMAP_PRIV(pCmap);
    ScreenPtr           pScreen = pCmap->pScreen;
    FSScreenPtr         fsDisplayInfo = FULLSCREEN_PRIV(pScreen);

    // Inform all interested parties that the map is being changed.
    miInstallColormap(pCmap);

    if (quartzServerVisible)
        CGDisplaySetPalette(fsDisplayInfo->displayID, palette);

    fsDisplayInfo->xPalette = palette;
}


/*
 * FSStoreColors
 *  This is a callback from X to change the hardware colormap
 *  when using PsuedoColor in full screen mode.
 */
static void
FSStoreColors(
    ColormapPtr         pCmap,
    int                 numEntries,
    xColorItem          *pdefs)
{
    CGDirectPaletteRef  palette = CMAP_PRIV(pCmap);
    ScreenPtr           pScreen = pCmap->pScreen;
    FSScreenPtr         fsDisplayInfo = FULLSCREEN_PRIV(pScreen);
    CGDeviceColor       color;
    int                 i;

    if (! palette)
        return;

    for (i = 0; i < numEntries; i++) {
        color.red   = pdefs[i].red   / 65535.0;
        color.green = pdefs[i].green / 65535.0;
        color.blue  = pdefs[i].blue  / 65535.0;
        CGPaletteSetColorAtIndex(palette, color, pdefs[i].pixel);
    }

    // Update hardware colormap
    if (quartzServerVisible)
        CGDisplaySetPalette(fsDisplayInfo->displayID, palette);
}


/*
 =============================================================================

 Switching between Aqua and X

 =============================================================================
*/

/*
 * FSCapture
 *  Capture the screen so we can draw. Called directly from the main thread
 *  to synchronize with hiding the menubar.
 */
static void FSCapture(void)
{
    int i;

    if (quartzRootless) return;

    for (i = 0; i < quartzNumScreens; i++) {
        FSScreenPtr fsDisplayInfo = quartzScreens[i];
        CGDirectDisplayID cgID = fsDisplayInfo->displayID;

        if (!CGDisplayIsCaptured(cgID)) {
            CGDisplayCapture(cgID);
            fsDisplayInfo->aquaDisplayMode = CGDisplayCurrentMode(cgID);
            if (fsDisplayInfo->xDisplayMode != fsDisplayInfo->aquaDisplayMode)
                CGDisplaySwitchToMode(cgID, fsDisplayInfo->xDisplayMode);
            if (fsDisplayInfo->xPalette)
                CGDisplaySetPalette(cgID, fsDisplayInfo->xPalette);
        }
    }
}


/*
 * FSRelease
 *  Release the screen so others can draw.
 */
static void FSRelease(void)
{
    int i;

    if (quartzRootless) return;

    for (i = 0; i < quartzNumScreens; i++) {
        FSScreenPtr fsDisplayInfo = quartzScreens[i];
        CGDirectDisplayID cgID = fsDisplayInfo->displayID;

        if (CGDisplayIsCaptured(cgID)) {
            if (fsDisplayInfo->xDisplayMode != fsDisplayInfo->aquaDisplayMode)
                CGDisplaySwitchToMode(cgID, fsDisplayInfo->aquaDisplayMode);
            if (fsDisplayInfo->aquaPalette)
                CGDisplaySetPalette(cgID, fsDisplayInfo->aquaPalette);
            CGDisplayRelease(cgID);
        }
    }
}


/*
 * FSSuspendScreen
 *  Suspend X11 cursor and drawing to the screen.
 */
static void FSSuspendScreen(
    ScreenPtr pScreen)
{
    QuartzSuspendXCursor(pScreen);
    xf86SetRootClip(pScreen, FALSE);
}


/*
 * FSResumeScreen
 *  Resume X11 cursor and drawing to the screen.
 */
static void FSResumeScreen(
    ScreenPtr pScreen,
    int x,			// cursor location
    int y )
{
    QuartzResumeXCursor(pScreen, x, y);
    xf86SetRootClip(pScreen, TRUE);
}


/*
 =============================================================================

 Screen initialization

 =============================================================================
*/

/*
 * FSDisplayInit
 *  Full screen specific initialization called from InitOutput.
 */
static void FSDisplayInit(void)
{
    static unsigned long generation = 0;
    CGDisplayCount quartzDisplayCount = 0;

    ErrorF("Display mode: Full screen Quartz -- Direct Display\n");

    // Allocate private storage for each screen's mode specific info
    if (generation != serverGeneration) {
        fsScreenIndex = AllocateScreenPrivateIndex();
        generation = serverGeneration;
    }

    // Find all the CoreGraphics displays
    CGGetActiveDisplayList(0, NULL, &quartzDisplayCount);
    quartzDisplayList = xalloc(quartzDisplayCount * sizeof(CGDirectDisplayID));
    CGGetActiveDisplayList(quartzDisplayCount, quartzDisplayList,
                           &quartzDisplayCount);

    darwinScreensFound = quartzDisplayCount;
    atexit(FSRelease);
}


/*
 * FSFindDisplayMode
 *  Find the appropriate display mode to use in full screen mode.
 *  If display mode is not the same as the current Aqua mode, switch
 *  to the new mode.
 */
static Bool FSFindDisplayMode(
    FSScreenPtr fsDisplayInfo)
{
    CGDirectDisplayID cgID = fsDisplayInfo->displayID;
    size_t height, width, bpp;
    boolean_t exactMatch;

    fsDisplayInfo->aquaDisplayMode = CGDisplayCurrentMode(cgID);

    // If no user options, use current display mode
    if (darwinDesiredWidth == 0 && darwinDesiredDepth == -1 &&
        darwinDesiredRefresh == -1)
    {
        fsDisplayInfo->xDisplayMode = fsDisplayInfo->aquaDisplayMode;
        return TRUE;
    }

    // If the user has no choice for size, use current
    if (darwinDesiredWidth == 0) {
        width = CGDisplayPixelsWide(cgID);
        height = CGDisplayPixelsHigh(cgID);
    } else {
        width = darwinDesiredWidth;
        height = darwinDesiredHeight;
    }

    switch (darwinDesiredDepth) {
        case 0:
            bpp = 8;
            break;
        case 1:
            bpp = 16;
            break;
        case 2:
            bpp = 32;
            break;
        default:
            bpp = CGDisplayBitsPerPixel(cgID);
    }

    if (darwinDesiredRefresh == -1) {
        fsDisplayInfo->xDisplayMode =
                CGDisplayBestModeForParameters(cgID, bpp, width, height,
                        &exactMatch);
    } else {
        fsDisplayInfo->xDisplayMode =
                CGDisplayBestModeForParametersAndRefreshRate(cgID, bpp,
                        width, height, darwinDesiredRefresh, &exactMatch);
    }
    if (!exactMatch) {
        fsDisplayInfo->xDisplayMode = fsDisplayInfo->aquaDisplayMode;
        return FALSE;
    }

    // Switch to the new display mode
    CGDisplaySwitchToMode(cgID, fsDisplayInfo->xDisplayMode);
    return TRUE;
}


/*
 * FSAddScreen
 *  Do initialization of each screen for Quartz in full screen mode.
 */
static Bool FSAddScreen(
    int index,
    ScreenPtr pScreen)
{
    DarwinFramebufferPtr dfb = SCREEN_PRIV(pScreen);
    QuartzScreenPtr displayInfo = QUARTZ_PRIV(pScreen);
    CGDirectDisplayID cgID = quartzDisplayList[index];
    CGRect bounds;
    FSScreenPtr fsDisplayInfo;

    // Allocate space for private per screen fullscreen specific storage.
    fsDisplayInfo = xalloc(sizeof(FSScreenRec));
    FULLSCREEN_PRIV(pScreen) = fsDisplayInfo;

    displayInfo->displayCount = 1;
    displayInfo->displayIDs = xrealloc(displayInfo->displayIDs,
                                      1 * sizeof(CGDirectDisplayID));
    displayInfo->displayIDs[0] = cgID;

    fsDisplayInfo->displayID = cgID;
    fsDisplayInfo->xDisplayMode = 0;
    fsDisplayInfo->aquaDisplayMode = 0;
    fsDisplayInfo->xPalette = 0;
    fsDisplayInfo->aquaPalette = 0;

    // Capture full screen because X doesn't like read-only framebuffer.
    // We need to do this before we (potentially) switch the display mode.
    CGDisplayCapture(cgID);

    if (! FSFindDisplayMode(fsDisplayInfo)) {
        ErrorF("Could not support specified display mode on screen %i.\n",
               index);
        xfree(fsDisplayInfo);
        return FALSE;
    }

    // Don't need to flip y-coordinate as CoreGraphics treats (0, 0)
    // as the top left of main screen.
    bounds = CGDisplayBounds(cgID);
    dfb->x = bounds.origin.x;
    dfb->y = bounds.origin.y;
    dfb->width  = bounds.size.width;
    dfb->height = bounds.size.height;
    dfb->pitch = CGDisplayBytesPerRow(cgID);
    dfb->bitsPerPixel = CGDisplayBitsPerPixel(cgID);

    if (dfb->bitsPerPixel == 8) {
        if (CGDisplayCanSetPalette(cgID)) {
            dfb->colorType = PseudoColor;
        } else {
            dfb->colorType = StaticColor;
        }
        dfb->bitsPerComponent = 8;
        dfb->colorBitsPerPixel = 8;
    } else {
        dfb->colorType = TrueColor;
        dfb->bitsPerComponent = CGDisplayBitsPerSample(cgID);
        dfb->colorBitsPerPixel = CGDisplaySamplesPerPixel(cgID) *
                                 dfb->bitsPerComponent;
    }

    fsDisplayInfo->framebuffer = CGDisplayBaseAddress(cgID);

    // allocate shadow framebuffer
    fsDisplayInfo->shadowPtr = xalloc(dfb->pitch * dfb->height);
    dfb->framebuffer = fsDisplayInfo->shadowPtr;

    return TRUE;
}


/*
 * FSShadowUpdate
 *  Update the damaged regions of the shadow framebuffer on the display.
 */
static void FSShadowUpdate(
    ScreenPtr pScreen,
    shadowBufPtr pBuf)
{
    DarwinFramebufferPtr dfb = SCREEN_PRIV(pScreen);
    FSScreenPtr fsDisplayInfo = FULLSCREEN_PRIV(pScreen);
    RegionPtr damage = &pBuf->damage;
    int numBox = REGION_NUM_RECTS(damage);
    BoxPtr pBox = REGION_RECTS(damage);
    int pitch = dfb->pitch;
    int bpp = dfb->bitsPerPixel/8;

    // Don't update if the X server is not visible
    if (!quartzServerVisible)
        return;

    // Loop through all the damaged boxes
    while (numBox--) {
        int width, height, offset;
        unsigned char *src, *dst;

        width = (pBox->x2 - pBox->x1) * bpp;
        height = pBox->y2 - pBox->y1;
        offset = (pBox->y1 * pitch) + (pBox->x1 * bpp);
        src = fsDisplayInfo->shadowPtr + offset;
        dst = fsDisplayInfo->framebuffer + offset;

        while (height--) {
            memcpy(dst, src, width);
            dst += pitch;
            src += pitch;
        }

        // Get the next box
        pBox++;
    }
}


/*
 * FSSetupScreen
 *  Finalize full screen specific setup of each screen.
 */
static Bool FSSetupScreen(
    int index,
    ScreenPtr pScreen)
{
    DarwinFramebufferPtr dfb = SCREEN_PRIV(pScreen);
    FSScreenPtr fsDisplayInfo = FULLSCREEN_PRIV(pScreen);
    CGDirectDisplayID cgID = fsDisplayInfo->displayID;

    // Initialize shadow framebuffer support
    if (! shadowInit(pScreen, FSShadowUpdate, NULL)) {
        ErrorF("Failed to initalize shadow framebuffer for screen %i.\n",
               index);
        return FALSE;
    }

    if (dfb->colorType == PseudoColor) {
        // Initialize colormap handling
        size_t aquaBpp;

        // If Aqua is using 8 bits we need to keep track of its pallete.
        CFNumberGetValue(CFDictionaryGetValue(fsDisplayInfo->aquaDisplayMode,
                         kCGDisplayBitsPerPixel), kCFNumberLongType, &aquaBpp);
        if (aquaBpp <= 8)
            fsDisplayInfo->aquaPalette = CGPaletteCreateWithDisplay(cgID);

        pScreen->CreateColormap = FSCreateColormap;
        pScreen->DestroyColormap = FSDestroyColormap;
        pScreen->InstallColormap = FSInstallColormap;
        pScreen->StoreColors = FSStoreColors;

    }

    quartzScreens[quartzNumScreens++] = fsDisplayInfo;
    return TRUE;
}


/*
 * Quartz display mode function list.
 */
static QuartzModeProcsRec fsModeProcs = {
    FSDisplayInit,
    FSAddScreen,
    FSSetupScreen,
    NULL,		// Not needed
    QuartzInitCursor,
    QuartzReallySetCursor,
    FSSuspendScreen,
    FSResumeScreen,
    FSCapture,
    FSRelease,
    NULL,		// No rootless code in fullscreen
    NULL,
    NULL,
    NULL,
    NULL,		// No support for DRI surfaces
    NULL
};


/*
 * QuartzModeBundleInit
 *  Initialize the display mode bundle after loading.
 */
Bool
QuartzModeBundleInit(void)
{
    quartzProcs = &fsModeProcs;
    quartzOpenGLBundle = NULL;	// Only Mesa support for now
    return TRUE;
}