aboutsummaryrefslogtreecommitdiff
path: root/xorg-server/hw/xfree86/shadowfb/shadow.c
blob: d2481ed8aca51b15165ae7415b9f671265387cb3 (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
/*
   Copyright (C) 1999.  The XFree86 Project Inc.
   Copyright 2014 Red Hat, Inc.

   Written by Mark Vojkovich (mvojkovi@ucsd.edu)
   Pre-fb-write callbacks and RENDER support - Nolan Leake (nolan@vmware.com)
*/

#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif

#include <X11/X.h>
#include <X11/Xproto.h>
#include "misc.h"
#include "pixmapstr.h"
#include "input.h"
#include <X11/fonts/font.h>
#include "mi.h"
#include "scrnintstr.h"
#include "windowstr.h"
#include "gcstruct.h"
#include "dixfontstr.h"
#include <X11/fonts/fontstruct.h>
#include "xf86.h"
#include "xf86str.h"
#include "shadowfb.h"

#include "picturestr.h"

static Bool ShadowCloseScreen(ScreenPtr pScreen);
static Bool ShadowCreateRootWindow(WindowPtr pWin);

typedef struct {
    ScrnInfoPtr pScrn;
    RefreshAreaFuncPtr preRefresh;
    RefreshAreaFuncPtr postRefresh;
    CloseScreenProcPtr CloseScreen;
    CreateWindowProcPtr CreateWindow;
} ShadowScreenRec, *ShadowScreenPtr;

static DevPrivateKeyRec ShadowScreenKeyRec;

static ShadowScreenPtr
shadowfbGetScreenPrivate(ScreenPtr pScreen)
{
    return dixLookupPrivate(&(pScreen)->devPrivates, &ShadowScreenKeyRec);
}

Bool
ShadowFBInit2(ScreenPtr pScreen,
              RefreshAreaFuncPtr preRefreshArea,
              RefreshAreaFuncPtr postRefreshArea)
{
    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
    ShadowScreenPtr pPriv;

    if (!preRefreshArea && !postRefreshArea)
        return FALSE;

    if (!dixRegisterPrivateKey(&ShadowScreenKeyRec, PRIVATE_SCREEN, 0))
        return FALSE;

    if (!(pPriv = (ShadowScreenPtr) malloc(sizeof(ShadowScreenRec))))
        return FALSE;

    dixSetPrivate(&pScreen->devPrivates, &ShadowScreenKeyRec, pPriv);

    pPriv->pScrn = pScrn;
    pPriv->preRefresh = preRefreshArea;
    pPriv->postRefresh = postRefreshArea;

    pPriv->CloseScreen = pScreen->CloseScreen;
    pPriv->CreateWindow = pScreen->CreateWindow;

    pScreen->CloseScreen = ShadowCloseScreen;
    pScreen->CreateWindow = ShadowCreateRootWindow;

    return TRUE;
}

Bool
ShadowFBInit(ScreenPtr pScreen, RefreshAreaFuncPtr refreshArea)
{
    return ShadowFBInit2(pScreen, NULL, refreshArea);
}

/*
 * Note that we don't do DamageEmpty, or indeed look at the region inside the
 * DamagePtr at all.  This is an optimization, believe it or not.  The
 * incoming RegionPtr is the new damage, and if we were to empty the region
 * miext/damage would just have to waste time reallocating and re-unioning
 * it every time, whereas if we leave it around the union gets fast-pathed
 * away.
 */

static void
shadowfbReportPre(DamagePtr damage, RegionPtr reg, void *closure)
{
    ShadowScreenPtr pPriv = closure;

    if (!pPriv->pScrn->vtSema)
        return;

    pPriv->preRefresh(pPriv->pScrn, RegionNumRects(reg), RegionRects(reg));
}

static void
shadowfbReportPost(DamagePtr damage, RegionPtr reg, void *closure)
{
    ShadowScreenPtr pPriv = closure;

    if (!pPriv->pScrn->vtSema)
        return;

    pPriv->postRefresh(pPriv->pScrn, RegionNumRects(reg), RegionRects(reg));
}

static Bool
ShadowCreateRootWindow(WindowPtr pWin)
{
    Bool ret;
    ScreenPtr pScreen = pWin->drawable.pScreen;
    ShadowScreenPtr pPriv = shadowfbGetScreenPrivate(pScreen);

    /* paranoia */
    if (pWin != pScreen->root)
        ErrorF("ShadowCreateRootWindow called unexpectedly\n");

    /* call down, but don't hook ourselves back in; we know the first time
     * we're called it's for the root window.
     */
    pScreen->CreateWindow = pPriv->CreateWindow;
    ret = pScreen->CreateWindow(pWin);

    /* this might look like it leaks, but the damage code reaps listeners
     * when their drawable disappears.
     */
    if (ret) {
        DamagePtr damage;

        if (pPriv->preRefresh) {
            damage = DamageCreate(shadowfbReportPre, NULL,
                                  DamageReportRawRegion,
                                  TRUE, pScreen, pPriv);
            DamageRegister(&pWin->drawable, damage);
        }

        if (pPriv->postRefresh) {
            damage = DamageCreate(shadowfbReportPost, NULL,
                                  DamageReportRawRegion,
                                  TRUE, pScreen, pPriv);
            DamageSetReportAfterOp(damage, TRUE);
            DamageRegister(&pWin->drawable, damage);
        }
    }

    return ret;
}

static Bool
ShadowCloseScreen(ScreenPtr pScreen)
{
    ShadowScreenPtr pPriv = shadowfbGetScreenPrivate(pScreen);

    pScreen->CloseScreen = pPriv->CloseScreen;

    free(pPriv);

    return (*pScreen->CloseScreen) (pScreen);
}