sunxi_x_g2d.c 9.17 KB
Newer Older
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
/*
 * Copyright © 2013 Siarhei Siamashka <siarhei.siamashka@gmail.com>
 *
 * 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 (including the next
 * paragraph) 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
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

28
29
#include <pixman.h>

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
#include "xorgVersion.h"
#include "xf86_OSproc.h"
#include "xf86.h"
#include "xf86drm.h"
#include "dri2.h"
#include "damage.h"
#include "fb.h"

#include "fbdev_priv.h"
#include "sunxi_disp.h"
#include "sunxi_x_g2d.h"

/*
 * The code below is borrowed from "xserver/fb/fbwindow.c"
 */

static void
xCopyWindowProc(DrawablePtr pSrcDrawable,
                 DrawablePtr pDstDrawable,
                 GCPtr pGC,
                 BoxPtr pbox,
                 int nbox,
                 int dx,
                 int dy,
                 Bool reverse, Bool upsidedown, Pixel bitplane, void *closure)
{
    FbBits *src;
    FbStride srcStride;
    int srcBpp;
    int srcXoff, srcYoff;
    FbBits *dst;
    FbStride dstStride;
    int dstBpp;
    int dstXoff, dstYoff;
    ScreenPtr pScreen = pDstDrawable->pScreen;
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    sunxi_disp_t *disp = SUNXI_DISP(pScrn);

    fbGetDrawable(pSrcDrawable, src, srcStride, srcBpp, srcXoff, srcYoff);
    fbGetDrawable(pDstDrawable, dst, dstStride, dstBpp, dstXoff, dstYoff);

    if (srcBpp == 32 && dstBpp == 32 &&
72
73
        disp->framebuffer_addr == (void *)src &&
        disp->framebuffer_addr == (void *)dst &&
74
75
76
        (dy + srcYoff != dstYoff || dx + srcXoff + 1 >= dstXoff))
    {
        while (nbox--) {
77
78
79
80
81
82
            sunxi_g2d_blt(disp, (uint32_t *)src, (uint32_t *)dst,
                          srcStride, dstStride,
                          srcBpp, dstBpp, (pbox->x1 + dx + srcXoff),
                          (pbox->y1 + dy + srcYoff), (pbox->x1 + dstXoff),
                          (pbox->y1 + dstYoff), (pbox->x2 - pbox->x1),
                          (pbox->y2 - pbox->y1));
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
            pbox++;
        }
    }
    else {
        while (nbox--) {
            fbBlt(src + (pbox->y1 + dy + srcYoff) * srcStride,
                  srcStride,
                  (pbox->x1 + dx + srcXoff) * srcBpp,
                  dst + (pbox->y1 + dstYoff) * dstStride,
                  dstStride,
                  (pbox->x1 + dstXoff) * dstBpp,
                  (pbox->x2 - pbox->x1) * dstBpp,
                  (pbox->y2 - pbox->y1),
              GXcopy, FB_ALLONES, dstBpp, reverse, upsidedown);
            pbox++;
        }
    }

    fbFinishAccess(pDstDrawable);
    fbFinishAccess(pSrcDrawable);
}

static void
xCopyWindow(WindowPtr pWin, DDXPointRec ptOldOrg, RegionPtr prgnSrc)
{
    RegionRec rgnDst;
    int dx, dy;

    PixmapPtr pPixmap = fbGetWindowPixmap(pWin);
    DrawablePtr pDrawable = &pPixmap->drawable;

    dx = ptOldOrg.x - pWin->drawable.x;
    dy = ptOldOrg.y - pWin->drawable.y;
    RegionTranslate(prgnSrc, -dx, -dy);

    RegionNull(&rgnDst);

    RegionIntersect(&rgnDst, &pWin->borderClip, prgnSrc);

#ifdef COMPOSITE
    if (pPixmap->screen_x || pPixmap->screen_y)
        RegionTranslate(&rgnDst, -pPixmap->screen_x, -pPixmap->screen_y);
#endif

    miCopyRegion(pDrawable, pDrawable,
                 0, &rgnDst, dx, dy, xCopyWindowProc, 0, 0);

    RegionUninit(&rgnDst);
    fbValidateDrawable(&pWin->drawable);
}

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
/*****************************************************************************/

static void
xCopyNtoN(DrawablePtr pSrcDrawable,
          DrawablePtr pDstDrawable,
          GCPtr pGC,
          BoxPtr pbox,
          int nbox,
          int dx,
          int dy,
          Bool reverse, Bool upsidedown, Pixel bitplane, void *closure)
{
    CARD8 alu = pGC ? pGC->alu : GXcopy;
    FbBits pm = pGC ? fbGetGCPrivate(pGC)->pm : FB_ALLONES;
    FbBits *src;
    FbStride srcStride;
    int srcBpp;
    int srcXoff, srcYoff;
    FbBits *dst;
    FbStride dstStride;
    int dstBpp;
    int dstXoff, dstYoff;
    ScreenPtr pScreen = pDstDrawable->pScreen;
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    sunxi_disp_t *disp = SUNXI_DISP(pScrn);
159
    Bool bad_overlapping_for_g2d;
160
161
162
163

    fbGetDrawable(pSrcDrawable, src, srcStride, srcBpp, srcXoff, srcYoff);
    fbGetDrawable(pDstDrawable, dst, dstStride, dstBpp, dstXoff, dstYoff);

164
165
    bad_overlapping_for_g2d = (src == dst) && (dy + srcYoff == dstYoff) &&
                              (dx + srcXoff + 1 < dstXoff);
166
167

    while (nbox--) {
168
169
170
171
172
173
174
175
176
177
        Bool done = FALSE;

        /* first try G2D */
        if (!bad_overlapping_for_g2d) {
            done = sunxi_g2d_blt(disp, (uint32_t *)src, (uint32_t *)dst,
                                 srcStride, dstStride,
                                 srcBpp, dstBpp, (pbox->x1 + dx + srcXoff),
                                 (pbox->y1 + dy + srcYoff), (pbox->x1 + dstXoff),
                                 (pbox->y1 + dstYoff), (pbox->x2 - pbox->x1),
                                 (pbox->y2 - pbox->y1));
178
        }
179
180
181
182

        /* then pixman (NEON) */
        if (!done && !reverse && !upsidedown) {
            done = pixman_blt((uint32_t *)src, (uint32_t *)dst, srcStride, dstStride,
183
184
185
186
187
                 srcBpp, dstBpp, (pbox->x1 + dx + srcXoff),
                 (pbox->y1 + dy + srcYoff), (pbox->x1 + dstXoff),
                 (pbox->y1 + dstYoff), (pbox->x2 - pbox->x1),
                 (pbox->y2 - pbox->y1));
        }
188
189
190

        /* fallback to fbBlt if other methods did not work */
        if (!done) {
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
            fbBlt(src + (pbox->y1 + dy + srcYoff) * srcStride,
                  srcStride,
                  (pbox->x1 + dx + srcXoff) * srcBpp,
                  dst + (pbox->y1 + dstYoff) * dstStride,
                  dstStride,
                  (pbox->x1 + dstXoff) * dstBpp,
                  (pbox->x2 - pbox->x1) * dstBpp,
                  (pbox->y2 - pbox->y1), alu, pm, dstBpp, reverse, upsidedown);
        }
        pbox++;
    }

    fbFinishAccess(pDstDrawable);
    fbFinishAccess(pSrcDrawable);
}

static RegionPtr
xCopyArea(DrawablePtr pSrcDrawable,
         DrawablePtr pDstDrawable,
         GCPtr pGC,
         int xIn, int yIn, int widthSrc, int heightSrc, int xOut, int yOut)
{
    CARD8 alu = pGC ? pGC->alu : GXcopy;
    FbBits pm = pGC ? fbGetGCPrivate(pGC)->pm : FB_ALLONES;

    if (pm == FB_ALLONES && alu == GXcopy && 
        pSrcDrawable->bitsPerPixel == pDstDrawable->bitsPerPixel &&
        pSrcDrawable->bitsPerPixel == 32)
    {
        return miDoCopy(pSrcDrawable, pDstDrawable, pGC, xIn, yIn,
                    widthSrc, heightSrc, xOut, yOut, xCopyNtoN, 0, 0);
    }
    return fbCopyArea(pSrcDrawable,
                      pDstDrawable,
                      pGC,
                      xIn, yIn, widthSrc, heightSrc, xOut, yOut);
}

static Bool
xCreateGC(GCPtr pGC)
{
    ScreenPtr pScreen = pGC->pScreen;
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    SunxiG2D *self = SUNXI_G2D(pScrn);
    Bool result;

    if (!fbCreateGC(pGC))
        return FALSE;

    if (!self->pGCOps) {
        self->pGCOps = calloc(1, sizeof(GCOps));
        memcpy(self->pGCOps, pGC->ops, sizeof(GCOps));

        /* Add our own hook for CopyArea function */
        self->pGCOps->CopyArea = xCopyArea;
    }
    pGC->ops = self->pGCOps;

    return TRUE;
}

/*****************************************************************************/
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

SunxiG2D *SunxiG2D_Init(ScreenPtr pScreen)
{
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    sunxi_disp_t *disp = SUNXI_DISP(pScrn);
    SunxiG2D *private;

    if (!disp || disp->fd_g2d < 0) {
        xf86DrvMsg(pScreen->myNum, X_INFO,
            "No sunxi-g2d hardware detected (check /dev/disp and /dev/g2d)\n");
        return NULL;
    }

    private = calloc(1, sizeof(SunxiG2D));
    if (!private) {
        xf86DrvMsg(pScreen->myNum, X_INFO,
            "SunxiG2D_Init: calloc failed\n");
        return NULL;
    }

    /* Wrap the current CopyWindow function */
    private->CopyWindow = pScreen->CopyWindow;
    pScreen->CopyWindow = xCopyWindow;

277
278
279
280
    /* Wrap the current CreateGC function */
    private->CreateGC = pScreen->CreateGC;
    pScreen->CreateGC = xCreateGC;

281
282
283
284
285
286
287
288
289
    return private;
}

void SunxiG2D_Close(ScreenPtr pScreen)
{
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    SunxiG2D *private = SUNXI_G2D(pScrn);

    pScreen->CopyWindow = private->CopyWindow;
290
291
292
293
294
    pScreen->CreateGC   = private->CreateGC;

    if (private->pGCOps) {
        free(private->pGCOps);
    }
295
}