1/*
2 * Copyright © 2009 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 *    Junyan He <junyan.he@linux.intel.com>
25 *
26 */
27
28/** @file glamor_trapezoid.c
29 *
30 * Trapezoid acceleration implementation
31 */
32
33#include "glamor_priv.h"
34
35#include "mipict.h"
36#include "fbpict.h"
37
38/**
39 * Creates an appropriate picture for temp mask use.
40 */
41static PicturePtr
42glamor_create_mask_picture(ScreenPtr screen,
43                           PicturePtr dst,
44                           PictFormatPtr pict_format,
45                           CARD16 width, CARD16 height)
46{
47    PixmapPtr pixmap;
48    PicturePtr picture;
49    int error;
50
51    if (!pict_format) {
52        if (dst->polyEdge == PolyEdgeSharp)
53            pict_format = PictureMatchFormat(screen, 1, PICT_a1);
54        else
55            pict_format = PictureMatchFormat(screen, 8, PICT_a8);
56        if (!pict_format)
57            return 0;
58    }
59
60    pixmap = glamor_create_pixmap(screen, 0, 0,
61                                  pict_format->depth,
62                                  GLAMOR_CREATE_PIXMAP_CPU);
63
64    if (!pixmap)
65        return 0;
66    picture = CreatePicture(0, &pixmap->drawable, pict_format,
67                            0, 0, serverClient, &error);
68    glamor_destroy_pixmap(pixmap);
69    return picture;
70}
71
72/**
73 * glamor_trapezoids will generate trapezoid mask accumulating in
74 * system memory.
75 */
76void
77glamor_trapezoids(CARD8 op,
78                  PicturePtr src, PicturePtr dst,
79                  PictFormatPtr mask_format, INT16 x_src, INT16 y_src,
80                  int ntrap, xTrapezoid *traps)
81{
82    ScreenPtr screen = dst->pDrawable->pScreen;
83    BoxRec bounds;
84    PicturePtr picture;
85    INT16 x_dst, y_dst;
86    INT16 x_rel, y_rel;
87    int width, height, stride;
88    PixmapPtr pixmap;
89    pixman_image_t *image = NULL;
90
91    /* If a mask format wasn't provided, we get to choose, but behavior should
92     * be as if there was no temporary mask the traps were accumulated into.
93     */
94    if (!mask_format) {
95        if (dst->polyEdge == PolyEdgeSharp)
96            mask_format = PictureMatchFormat(screen, 1, PICT_a1);
97        else
98            mask_format = PictureMatchFormat(screen, 8, PICT_a8);
99        for (; ntrap; ntrap--, traps++)
100            glamor_trapezoids(op, src, dst, mask_format, x_src,
101                              y_src, 1, traps);
102        return;
103    }
104
105    miTrapezoidBounds(ntrap, traps, &bounds);
106
107    if (bounds.y1 >= bounds.y2 || bounds.x1 >= bounds.x2)
108        return;
109
110    x_dst = traps[0].left.p1.x >> 16;
111    y_dst = traps[0].left.p1.y >> 16;
112
113    width = bounds.x2 - bounds.x1;
114    height = bounds.y2 - bounds.y1;
115    stride = PixmapBytePad(width, mask_format->depth);
116
117    picture = glamor_create_mask_picture(screen, dst, mask_format,
118                                         width, height);
119    if (!picture)
120        return;
121
122    image = pixman_image_create_bits(picture->format,
123                                     width, height, NULL, stride);
124    if (!image) {
125        FreePicture(picture, 0);
126        return;
127    }
128
129    for (; ntrap; ntrap--, traps++)
130        pixman_rasterize_trapezoid(image,
131                                   (pixman_trapezoid_t *) traps,
132                                   -bounds.x1, -bounds.y1);
133
134    pixmap = glamor_get_drawable_pixmap(picture->pDrawable);
135
136    screen->ModifyPixmapHeader(pixmap, width, height,
137                               mask_format->depth,
138                               BitsPerPixel(mask_format->depth),
139                               PixmapBytePad(width,
140                                             mask_format->depth),
141                               pixman_image_get_data(image));
142
143    x_rel = bounds.x1 + x_src - x_dst;
144    y_rel = bounds.y1 + y_src - y_dst;
145
146    CompositePicture(op, src, picture, dst,
147                     x_rel, y_rel,
148                     0, 0,
149                     bounds.x1, bounds.y1,
150                     bounds.x2 - bounds.x1, bounds.y2 - bounds.y1);
151
152    if (image)
153        pixman_image_unref(image);
154
155    FreePicture(picture, 0);
156}
157