sunxi_x_g2d.c 8.79 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
#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_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];
65
    SunxiG2D *private = SUNXI_G2D(pScrn);
66
67
68
69

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

70
71
72
73
74
75
76
77
78
    while (nbox--) {
        if (!private->blt2d_overlapped_blt(private->blt2d_self,
                                           (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))) {
            /* fallback to fbBlt */
79
80
81
82
83
84
85
86
            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),
87
                  GXcopy, FB_ALLONES, dstBpp, reverse, upsidedown);
88
        }
89
        pbox++;
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
    }

    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);
}

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*****************************************************************************/

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];
149
    SunxiG2D *private = SUNXI_G2D(pScrn);
150
151
152
153
154

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

    while (nbox--) {
155
        /* first try G2D */
156
157
158
159
160
161
162
163
        Bool done = private->blt2d_overlapped_blt(
                             private->blt2d_self,
                             (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));
164
165
166
167

        /* then pixman (NEON) */
        if (!done && !reverse && !upsidedown) {
            done = pixman_blt((uint32_t *)src, (uint32_t *)dst, srcStride, dstStride,
168
169
170
171
172
                 srcBpp, dstBpp, (pbox->x1 + dx + srcXoff),
                 (pbox->y1 + dy + srcYoff), (pbox->x1 + dstXoff),
                 (pbox->y1 + dstYoff), (pbox->x2 - pbox->x1),
                 (pbox->y2 - pbox->y1));
        }
173
174
175

        /* fallback to fbBlt if other methods did not work */
        if (!done) {
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
            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 &&
203
        (pSrcDrawable->bitsPerPixel == 32 || pSrcDrawable->bitsPerPixel == 16))
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
    {
        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;
}

/*****************************************************************************/
238

239
SunxiG2D *SunxiG2D_Init(ScreenPtr pScreen, blt2d_i *blt2d)
240
{
241
    SunxiG2D *private = calloc(1, sizeof(SunxiG2D));
242
243
244
245
246
247
    if (!private) {
        xf86DrvMsg(pScreen->myNum, X_INFO,
            "SunxiG2D_Init: calloc failed\n");
        return NULL;
    }

248
249
250
251
    /* Cache the pointers from blt2d_i here */
    private->blt2d_self = blt2d->self;
    private->blt2d_overlapped_blt = blt2d->overlapped_blt;

252
253
254
255
    /* Wrap the current CopyWindow function */
    private->CopyWindow = pScreen->CopyWindow;
    pScreen->CopyWindow = xCopyWindow;

256
257
258
259
    /* Wrap the current CreateGC function */
    private->CreateGC = pScreen->CreateGC;
    pScreen->CreateGC = xCreateGC;

260
261
262
263
264
265
266
267
268
    return private;
}

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

    pScreen->CopyWindow = private->CopyWindow;
269
270
271
272
273
    pScreen->CreateGC   = private->CreateGC;

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