summaryrefslogtreecommitdiff
path: root/src/glamo-draw.c
blob: c7be92bc11be96f86f6ccbfa786595ae56671a4c (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
/*
 * Copyright  2007 OpenMoko, Inc.
 *
 * This driver is based on Xati,
 * Copyright  2003 Eric Anholt
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "glamo-log.h"
#include "glamo.h"
#include "glamo-regs.h"
#include "glamo-cmdq.h"
#include "glamo-draw.h"
#include "glamo-engine.h"

static const CARD8 GLAMOSolidRop[16] = {
    /* GXclear      */      0x00,         /* 0 */
    /* GXand        */      0xa0,         /* src AND dst */
    /* GXandReverse */      0x50,         /* src AND NOT dst */
    /* GXcopy       */      0xf0,         /* src */
    /* GXandInverted*/      0x0a,         /* NOT src AND dst */
    /* GXnoop       */      0xaa,         /* dst */
    /* GXxor        */      0x5a,         /* src XOR dst */
    /* GXor         */      0xfa,         /* src OR dst */
    /* GXnor        */      0x05,         /* NOT src AND NOT dst */
    /* GXequiv      */      0xa5,         /* NOT src XOR dst */
    /* GXinvert     */      0x55,         /* NOT dst */
    /* GXorReverse  */      0xf5,         /* src OR NOT dst */
    /* GXcopyInverted*/     0x0f,         /* NOT src */
    /* GXorInverted */      0xaf,         /* NOT src OR dst */
    /* GXnand       */      0x5f,         /* NOT src OR NOT dst */
    /* GXset        */      0xff,         /* 1 */
};

static const CARD8 GLAMOBltRop[16] = {
    /* GXclear      */      0x00,         /* 0 */
    /* GXand        */      0x88,         /* src AND dst */
    /* GXandReverse */      0x44,         /* src AND NOT dst */
    /* GXcopy       */      0xcc,         /* src */
    /* GXandInverted*/      0x22,         /* NOT src AND dst */
    /* GXnoop       */      0xaa,         /* dst */
    /* GXxor        */      0x66,         /* src XOR dst */
    /* GXor         */      0xee,         /* src OR dst */
    /* GXnor        */      0x11,         /* NOT src AND NOT dst */
    /* GXequiv      */      0x99,         /* NOT src XOR dst */
    /* GXinvert     */      0x55,         /* NOT dst */
    /* GXorReverse  */      0xdd,         /* src OR NOT dst */
    /* GXcopyInverted*/     0x33,         /* NOT src */
    /* GXorInverted */      0xbb,         /* NOT src OR dst */
    /* GXnand       */      0x77,         /* NOT src OR NOT dst */
    /* GXset        */      0xff,         /* 1 */
};

/********************************
 * exa entry points declarations
 ********************************/

static Bool
GLAMODrawExaInit(ScrnInfoPtr pScrn, size_t mem_start, size_t mem_size);

Bool
GLAMOExaPrepareSolid(PixmapPtr      pPixmap,
		     int            alu,
		     Pixel          planemask,
		     Pixel          fg);

void
GLAMOExaSolid(PixmapPtr pPixmap, int x1, int y1, int x2, int y2);

void
GLAMOExaDoneSolid(PixmapPtr pPixmap);

void
GLAMOExaCopy(PixmapPtr pDstPixmap,
	     int    srcX,
	     int    srcY,
	     int    dstX,
	     int    dstY,
	     int    width,
	     int    height);

void
GLAMOExaDoneCopy(PixmapPtr pDstPixmap);

Bool
GLAMOExaCheckComposite(int op,
	       PicturePtr   pSrcPicture,
	       PicturePtr   pMaskPicture,
	       PicturePtr   pDstPicture);


Bool
GLAMOExaPrepareComposite(int                op,
			 PicturePtr         pSrcPicture,
			 PicturePtr         pMaskPicture,
			 PicturePtr         pDstPicture,
			 PixmapPtr          pSrc,
			 PixmapPtr          pMask,
			 PixmapPtr          pDst);

void
GLAMOExaComposite(PixmapPtr pDst,
		 int srcX,
		 int srcY,
		 int maskX,
		 int maskY,
		 int dstX,
		 int dstY,
		 int width,
		 int height);

Bool
GLAMOExaPrepareCopy(PixmapPtr       pSrcPixmap,
		    PixmapPtr       pDstPixmap,
		    int             dx,
		    int             dy,
		    int             alu,
		    Pixel           planemask);

void
GLAMOExaDoneComposite(PixmapPtr pDst);


Bool
GLAMOExaUploadToScreen(PixmapPtr pDst,
		       int x,
		       int y,
		       int w,
		       int h,
		       char *src,
		       int src_pitch);
Bool
GLAMOExaDownloadFromScreen(PixmapPtr pSrc,
			   int x,  int y,
			   int w,  int h,
			   char *dst,
			   int dst_pitch);

void
GLAMOExaWaitMarker (ScreenPtr pScreen, int marker);

static void
GLAMOBlockHandler(pointer blockData, OSTimePtr timeout, pointer readmask)
{
	ScreenPtr pScreen = (ScreenPtr) blockData;

	exaWaitSync(pScreen);
}

static void
GLAMOWakeupHandler(pointer blockData, int result, pointer readmask)
{
}

size_t
GLAMODrawInit(ScrnInfoPtr pScrn, size_t mem_start, size_t mem_size)
{
    size_t mem_used;

    mem_used = GLAMOCMDQInit(pScrn, mem_start, mem_size);

	if (!mem_used) {
        GLAMODrawFini(pScrn);
        return FALSE;
    }

    if (!GLAMODrawExaInit(pScrn, mem_start + mem_used, mem_size - mem_used))
        return 0;

    return mem_size;
}

void
GLAMODrawFini(ScrnInfoPtr pScrn) {
    GlamoPtr pGlamo = GlamoPTR(pScrn);

    GLAMOCMDQFini(pScrn);
    if (pGlamo->exa) {
        exaDriverFini(pGlamo->pScreen);
        xfree(pGlamo->exa);
        pGlamo->exa = NULL;
    }
}

Bool
GLAMODrawEnable(ScrnInfoPtr pScrn)
{
    GlamoPtr pGlamo = GlamoPTR(pScrn);

	GLAMOEngineEnable(pGlamo, GLAMO_ENGINE_2D);
	GLAMOEngineReset(pGlamo, GLAMO_ENGINE_2D);
    if (!GLAMOCMDQEnable(pScrn)) {
        GLAMODrawDisable(pScrn);
        return FALSE;
    }
	GLAMOEngineWait(pGlamo, GLAMO_ENGINE_ALL);

    return TRUE;
}

void
GLAMODrawDisable(ScrnInfoPtr pScrn) {
    GlamoPtr pGlamo = GlamoPTR(pScrn);

    GLAMOCMDQDisable(pScrn);
	GLAMOEngineDisable(pGlamo, GLAMO_ENGINE_2D);
}

static Bool
GLAMODrawExaInit(ScrnInfoPtr pScrn, size_t mem_start, size_t mem_size)
{
    GlamoPtr pGlamo = GlamoPTR(pScrn);

	Bool success = FALSE;
	ExaDriverPtr exa;

	xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
			"EXA hardware acceleration initialising\n");

	exa = pGlamo->exa = exaDriverAlloc();
    if(!exa)
        return FALSE;

    pGlamo->exa->memoryBase = pGlamo->fbstart;
    pGlamo->exa->memorySize = mem_size + mem_start;
    /* TODO: Initalize offScreenBase based on the current screen resolution
     * (width*height*depth) */
    pGlamo->exa->offScreenBase = mem_start;

	exa->exa_major = EXA_VERSION_MAJOR;
	exa->exa_minor = EXA_VERSION_MINOR;

	exa->PrepareSolid = GLAMOExaPrepareSolid;
	exa->Solid = GLAMOExaSolid;
	exa->DoneSolid = GLAMOExaDoneSolid;

	exa->PrepareCopy = GLAMOExaPrepareCopy;
	exa->Copy = GLAMOExaCopy;
	exa->DoneCopy = GLAMOExaDoneCopy;

	exa->CheckComposite = GLAMOExaCheckComposite;
	exa->PrepareComposite = GLAMOExaPrepareComposite;
	exa->Composite = GLAMOExaComposite;
	exa->DoneComposite = GLAMOExaDoneComposite;

	exa->DownloadFromScreen = GLAMOExaDownloadFromScreen;
	exa->UploadToScreen = GLAMOExaUploadToScreen;

	/*glamos->exa.MarkSync = GLAMOExaMarkSync;*/
	exa->WaitMarker = GLAMOExaWaitMarker;

	exa->pixmapOffsetAlign = 2;
	exa->pixmapPitchAlign = 2;

	exa->maxX = 640;
	exa->maxY = 640;

	exa->flags = EXA_OFFSCREEN_PIXMAPS;

	RegisterBlockAndWakeupHandlers(GLAMOBlockHandler,
				       GLAMOWakeupHandler,
				       pGlamo->pScreen);

	success = exaDriverInit(pGlamo->pScreen, exa);
	if (success) {
		ErrorF("Initialized EXA acceleration\n");
	} else {
		ErrorF("Failed to initialize EXA acceleration\n");
        xfree(pGlamo->exa);
        pGlamo->exa = NULL;
	}

	return success;
}

Bool
GLAMOExaPrepareSolid(PixmapPtr      pPix,
		     int            alu,
		     Pixel          pm,
		     Pixel          fg)
{
	ScrnInfoPtr pScrn = xf86Screens[pPix->drawable.pScreen->myNum];
	GlamoPtr pGlamo = GlamoPTR(pScrn);

	CARD32 offset;
    CARD16 op, pitch;
	FbBits mask;
	RING_LOCALS;

	if (pPix->drawable.bitsPerPixel != 16)
		GLAMO_FALLBACK(("Only 16bpp is supported\n"));

    mask = FbFullMask(16);
	if ((pm & mask) != mask)
		GLAMO_FALLBACK(("Can't do planemask 0x%08x\n",
				(unsigned int) pm));
	op = GLAMOSolidRop[alu] << 8;
	offset = exaGetPixmapOffset(pPix);
	pitch = exaGetPixmapPitch(pPix);

	BEGIN_CMDQ(16);
	OUT_REG(GLAMO_REG_2D_DST_ADDRL, offset & 0xffff);
	OUT_REG(GLAMO_REG_2D_DST_ADDRH, (offset >> 16) & 0x7f);
	OUT_REG(GLAMO_REG_2D_DST_PITCH, pitch & 0x7ff);
	OUT_REG(GLAMO_REG_2D_DST_HEIGHT, pPix->drawable.height);
	OUT_REG(GLAMO_REG_2D_PAT_FG, fg);
	OUT_REG(GLAMO_REG_2D_COMMAND2, op);
	OUT_REG(GLAMO_REG_2D_ID1, 0);
	OUT_REG(GLAMO_REG_2D_ID2, 0);
	END_CMDQ();

	return TRUE;
}

void
GLAMOExaSolid(PixmapPtr pPix, int x1, int y1, int x2, int y2)
{
	ScrnInfoPtr pScrn = xf86Screens[pPix->drawable.pScreen->myNum];
	GlamoPtr pGlamo = GlamoPTR(pScrn);

	RING_LOCALS;

	BEGIN_CMDQ(10);
	OUT_REG(GLAMO_REG_2D_DST_X, x1);
	OUT_REG(GLAMO_REG_2D_DST_Y, y1);
	OUT_REG(GLAMO_REG_2D_RECT_WIDTH, x2 - x1);
	OUT_REG(GLAMO_REG_2D_RECT_HEIGHT, y2 - y1);
	OUT_REG(GLAMO_REG_2D_COMMAND3, 0);
	END_CMDQ();
}

void
GLAMOExaDoneSolid(PixmapPtr pPix)
{
	ScrnInfoPtr pScrn = xf86Screens[pPix->drawable.pScreen->myNum];
	GlamoPtr pGlamo = GlamoPTR(pScrn);
    GLAMODispatchCMDQ(pGlamo);
	exaMarkSync(pGlamo->pScreen);
}

Bool
GLAMOExaPrepareCopy(PixmapPtr       pSrc,
		    PixmapPtr       pDst,
		    int             dx,
		    int             dy,
		    int             alu,
		    Pixel           pm)
{
	ScrnInfoPtr pScrn = xf86Screens[pSrc->drawable.pScreen->myNum];
	GlamoPtr pGlamo = GlamoPTR(pScrn);

    RING_LOCALS;

    FbBits mask;

    CARD32 src_offset, dst_offset;
    CARD16 src_pitch, dst_pitch;
    CARD16 op;

	if (pSrc->drawable.bitsPerPixel != 16 ||
	    pDst->drawable.bitsPerPixel != 16)
		GLAMO_FALLBACK(("Only 16bpp is supported"));

	mask = FbFullMask(16);
	if ((pm & mask) != mask) {
		GLAMO_FALLBACK(("Can't do planemask 0x%08x",
				(unsigned int) pm));
	}

	src_offset = exaGetPixmapOffset(pSrc);
	src_pitch = exaGetPixmapPitch(pSrc);

	dst_offset = exaGetPixmapOffset(pDst);
	dst_pitch = exaGetPixmapPitch(pDst);

	op = GLAMOBltRop[alu] << 8;

    BEGIN_CMDQ(20);
    OUT_REG(GLAMO_REG_2D_SRC_ADDRL, src_offset & 0xffff);
	OUT_REG(GLAMO_REG_2D_SRC_ADDRH, (src_offset >> 16) & 0x7f);
	OUT_REG(GLAMO_REG_2D_SRC_PITCH, src_pitch & 0x7ff);

	OUT_REG(GLAMO_REG_2D_DST_ADDRL, dst_offset & 0xffff);
	OUT_REG(GLAMO_REG_2D_DST_ADDRH, (dst_offset >> 16) & 0x7f);
	OUT_REG(GLAMO_REG_2D_DST_PITCH, dst_pitch & 0x7ff);
	OUT_REG(GLAMO_REG_2D_DST_HEIGHT, pDst->drawable.height);

	OUT_REG(GLAMO_REG_2D_COMMAND2, op);
	OUT_REG(GLAMO_REG_2D_ID1, 0);
	OUT_REG(GLAMO_REG_2D_ID2, 0);
	END_CMDQ();


	return TRUE;
}

void
GLAMOExaCopy(PixmapPtr       pDst,
	      int    srcX,
	      int    srcY,
	      int    dstX,
	      int    dstY,
	      int    width,
	      int    height)
{
	ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
	GlamoPtr pGlamo = GlamoPTR(pScrn);

	RING_LOCALS;
	BEGIN_CMDQ(14);

	OUT_REG(GLAMO_REG_2D_SRC_X, srcX);
	OUT_REG(GLAMO_REG_2D_SRC_Y, srcY);
	OUT_REG(GLAMO_REG_2D_DST_X, dstX);
	OUT_REG(GLAMO_REG_2D_DST_Y, dstY);
	OUT_REG(GLAMO_REG_2D_RECT_WIDTH, width);
	OUT_REG(GLAMO_REG_2D_RECT_HEIGHT, height);
	OUT_REG(GLAMO_REG_2D_COMMAND3, 0);
	END_CMDQ();
}

void
GLAMOExaDoneCopy(PixmapPtr pDst)
{
	ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
	GlamoPtr pGlamo = GlamoPTR(pScrn);
    GLAMODispatchCMDQ(pGlamo);
	exaMarkSync(pGlamo->pScreen);
}

Bool
GLAMOExaCheckComposite(int op,
		       PicturePtr   pSrcPicture,
		       PicturePtr   pMaskPicture,
		       PicturePtr   pDstPicture)
{
	return FALSE;
}

Bool
GLAMOExaPrepareComposite(int                op,
			 PicturePtr         pSrcPicture,
			 PicturePtr         pMaskPicture,
			 PicturePtr         pDstPicture,
			 PixmapPtr          pSrc,
			 PixmapPtr          pMask,
			 PixmapPtr          pDst)
{
	return FALSE;
}

void
GLAMOExaComposite(PixmapPtr pDst,
		 int srcX,
		 int srcY,
		 int maskX,
		 int maskY,
		 int dstX,
		 int dstY,
		 int width,
		 int height)
{
}

void
GLAMOExaDoneComposite(PixmapPtr pDst)
{
}

Bool
GLAMOExaUploadToScreen(PixmapPtr pDst,
		       int x,
		       int y,
		       int w,
		       int h,
		       char *src,
		       int src_pitch)
{
    ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
    GlamoPtr pGlamo = GlamoPTR(pScrn);
    int bpp, i;
    CARD8 *dst_offset;
    int dst_pitch;

    exaWaitSync(pScrn->pScreen);

    bpp = pDst->drawable.bitsPerPixel / 8;
    dst_pitch = exaGetPixmapPitch(pDst);
    dst_offset = pGlamo->exa->memoryBase + exaGetPixmapOffset(pDst)
                    + x*bpp + y*dst_pitch;

    for (i = 0; i < h; i++) {
        memcpy(dst_offset, src, w*bpp);
        dst_offset += dst_pitch;
        src += src_pitch;
    }

    return TRUE;
}

Bool
GLAMOExaDownloadFromScreen(PixmapPtr pSrc,
			   int x,  int y,
			   int w,  int h,
			   char *dst,
			   int dst_pitch)
{
    ScrnInfoPtr pScrn = xf86Screens[pSrc->drawable.pScreen->myNum];
    GlamoPtr pGlamo = GlamoPTR(pScrn);
    int bpp, i;
    CARD8 *dst_offset, *src;
    int src_pitch;

    exaWaitSync(pScrn->pScreen);

    bpp = pSrc->drawable.bitsPerPixel / 8;
    src_pitch = exaGetPixmapPitch(pSrc);
    src = pGlamo->exa->memoryBase + exaGetPixmapOffset(pSrc) +
            x*bpp + y*src_pitch;
    dst_offset = (unsigned char*)dst;

    for (i = 0; i < h; i++) {
        memcpy(dst_offset, src, w*bpp);
        dst_offset += dst_pitch;
        src += src_pitch;
    }

    return TRUE;
}

void
GLAMOExaWaitMarker (ScreenPtr pScreen, int marker)
{
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	GlamoPtr pGlamo = GlamoPTR(pScrn);
    GLAMODispatchCMDQ(pGlamo);
	GLAMOEngineWait(pGlamo, GLAMO_ENGINE_ALL);
}