1 /*
2    Copyright (C) 2009 Red Hat, Inc.
3 
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions are
6    met:
7 
8        * Redistributions of source code must retain the above copyright
9          notice, this list of conditions and the following disclaimer.
10        * Redistributions in binary form must reproduce the above copyright
11          notice, this list of conditions and the following disclaimer in
12          the documentation and/or other materials provided with the
13          distribution.
14        * Neither the name of the copyright holder nor the names of its
15          contributors may be used to endorse or promote products derived
16          from this software without specific prior written permission.
17 
18    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
19    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22    HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 #ifndef H_SPICE_COMMON_DRAW
32 #define H_SPICE_COMMON_DRAW
33 
34 #include <spice/macros.h>
35 #include <spice/types.h>
36 #include <spice/enums.h>
37 #include "mem.h"
38 
39 SPICE_BEGIN_DECLS
40 
41 #define SPICE_GET_ADDRESS(addr) ((void *)(uintptr_t)(addr))
42 #define SPICE_SET_ADDRESS(addr, val) ((addr) = (uintptr_t)(val))
43 
44 typedef int32_t SPICE_FIXED28_4;
45 
46 typedef struct SpicePointFix {
47     SPICE_FIXED28_4 x;
48     SPICE_FIXED28_4 y;
49 } SpicePointFix;
50 
51 typedef struct SpicePoint {
52     int32_t x;
53     int32_t y;
54 } SpicePoint;
55 
56 typedef struct SpicePoint16 {
57     int16_t x;
58     int16_t y;
59 } SpicePoint16;
60 
61 typedef struct SpiceRect {
62     int32_t left;
63     int32_t top;
64     int32_t right;
65     int32_t bottom;
66 } SpiceRect;
67 
68 typedef struct SpicePathSeg {
69     uint32_t flags;
70     uint32_t count;
71     SpicePointFix points[0];
72 } SpicePathSeg;
73 
74 typedef struct SpicePath {
75   uint32_t num_segments;
76   SpicePathSeg *segments[0];
77 } SpicePath;
78 
79 typedef struct SpiceClipRects {
80   uint32_t num_rects;
81   SpiceRect rects[0];
82 } SpiceClipRects;
83 
84 typedef struct SpiceClip {
85     uint8_t type;
86     SpiceClipRects *rects;
87 } SpiceClip;
88 
89 typedef struct SpicePalette {
90     uint64_t unique;
91     uint16_t num_ents;
92     uint32_t ents[0];
93 } SpicePalette;
94 
95 #define SPICE_SURFACE_FMT_DEPTH(_d) ((_d) & 0x3f)
96 
97 typedef struct SpiceImageDescriptor {
98     uint64_t id;
99     uint8_t type;
100     uint8_t flags;
101     uint32_t width;
102     uint32_t height;
103 } SpiceImageDescriptor;
104 
105 typedef struct SpiceBitmap {
106     uint8_t format;
107     uint8_t flags;
108     uint32_t x;
109     uint32_t y;
110     uint32_t stride;
111     SpicePalette *palette;
112     uint64_t palette_id;
113     SpiceChunks *data;
114 } SpiceBitmap;
115 
116 typedef struct SpiceSurface {
117     uint32_t surface_id;
118 } SpiceSurface;
119 
120 typedef struct SpiceQUICData {
121     uint32_t data_size;
122     SpiceChunks *data;
123 } SpiceQUICData, SpiceLZRGBData, SpiceJPEGData, SpiceLZ4Data;
124 
125 typedef struct SpiceLZPLTData {
126     uint8_t flags;
127     uint32_t data_size;
128     SpicePalette *palette;
129     uint64_t palette_id;
130     SpiceChunks *data;
131 } SpiceLZPLTData;
132 
133 typedef struct SpiceZlibGlzRGBData {
134     uint32_t glz_data_size;
135     uint32_t data_size;
136     SpiceChunks *data;
137 } SpiceZlibGlzRGBData;
138 
139 typedef struct SpiceJPEGAlphaData {
140     uint8_t flags;
141     uint32_t jpeg_size;
142     uint32_t data_size;
143     SpiceChunks *data;
144 } SpiceJPEGAlphaData;
145 
146 
147 typedef struct SpiceImage {
148     SpiceImageDescriptor descriptor;
149     union {
150         SpiceBitmap         bitmap;
151         SpiceQUICData       quic;
152         SpiceSurface        surface;
153         SpiceLZRGBData      lz_rgb;
154         SpiceLZPLTData      lz_plt;
155         SpiceJPEGData       jpeg;
156         SpiceLZ4Data        lz4;
157         SpiceZlibGlzRGBData zlib_glz;
158         SpiceJPEGAlphaData  jpeg_alpha;
159     } u;
160 } SpiceImage;
161 
162 typedef struct SpicePattern {
163     SpiceImage *pat;
164     SpicePoint pos;
165 } SpicePattern;
166 
167 typedef struct SpiceBrush {
168     uint32_t type;
169     union {
170         uint32_t color;
171         SpicePattern pattern;
172     } u;
173 } SpiceBrush;
174 
175 typedef struct SpiceQMask {
176     uint8_t flags;
177     SpicePoint pos;
178     SpiceImage *bitmap;
179 } SpiceQMask;
180 
181 typedef struct SpiceFill {
182     SpiceBrush brush;
183     uint16_t rop_descriptor;
184     SpiceQMask mask;
185 } SpiceFill;
186 
187 typedef struct SpiceOpaque {
188     SpiceImage *src_bitmap;
189     SpiceRect src_area;
190     SpiceBrush brush;
191     uint16_t rop_descriptor;
192     uint8_t scale_mode;
193     SpiceQMask mask;
194 } SpiceOpaque;
195 
196 typedef struct SpiceCopy {
197     SpiceImage *src_bitmap;
198     SpiceRect src_area;
199     uint16_t rop_descriptor;
200     uint8_t scale_mode;
201     SpiceQMask mask;
202 } SpiceCopy, SpiceBlend;
203 
204 typedef struct SpiceTransparent {
205     SpiceImage *src_bitmap;
206     SpiceRect src_area;
207     uint32_t src_color;
208     uint32_t true_color;
209 } SpiceTransparent;
210 
211 typedef struct SpiceAlphaBlend {
212     uint16_t alpha_flags;
213     uint8_t alpha;
214     SpiceImage *src_bitmap;
215     SpiceRect src_area;
216 } SpiceAlphaBlend;
217 
218 typedef struct SpiceRop3 {
219     SpiceImage *src_bitmap;
220     SpiceRect src_area;
221     SpiceBrush brush;
222     uint8_t rop3;
223     uint8_t scale_mode;
224     SpiceQMask mask;
225 } SpiceRop3;
226 
227 /* Given in 16.16 fixed point */
228 typedef struct SpiceTransform {
229     uint32_t t00;
230     uint32_t t01;
231     uint32_t t02;
232     uint32_t t10;
233     uint32_t t11;
234     uint32_t t12;
235 } SpiceTransform;
236 
237 typedef struct SpiceComposite {
238     uint32_t flags;
239     SpiceImage *src_bitmap;
240     SpiceImage *mask_bitmap;
241     SpiceTransform src_transform;
242     SpiceTransform mask_transform;
243     SpicePoint16 src_origin;
244     SpicePoint16 mask_origin;
245 } SpiceComposite;
246 
247 typedef struct SpiceBlackness {
248     SpiceQMask mask;
249 } SpiceBlackness, SpiceInvers, SpiceWhiteness;
250 
251 typedef struct SpiceLineAttr {
252     uint8_t flags;
253     uint8_t style_nseg;
254     SPICE_FIXED28_4 *style;
255 } SpiceLineAttr;
256 
257 typedef struct SpiceStroke {
258     SpicePath *path;
259     SpiceLineAttr attr;
260     SpiceBrush brush;
261     uint16_t fore_mode;
262     uint16_t back_mode;
263 } SpiceStroke;
264 
265 typedef struct SpiceRasterGlyph {
266     SpicePoint render_pos;
267     SpicePoint glyph_origin;
268     uint16_t width;
269     uint16_t height;
270     uint8_t data[0];
271 } SpiceRasterGlyph;
272 
273 typedef struct SpiceString {
274     uint16_t length;
275     uint16_t flags;
276     SpiceRasterGlyph *glyphs[0];
277 } SpiceString;
278 
279 typedef struct SpiceText {
280     SpiceString *str;
281     SpiceRect back_area;
282     SpiceBrush fore_brush;
283     SpiceBrush back_brush;
284     uint16_t fore_mode;
285     uint16_t back_mode;
286 } SpiceText;
287 
288 typedef struct SpiceCursorHeader {
289     uint64_t unique;
290     uint16_t type;
291     uint16_t width;
292     uint16_t height;
293     uint16_t hot_spot_x;
294     uint16_t hot_spot_y;
295 } SpiceCursorHeader;
296 
spice_image_descriptor_is_lossy(const SpiceImageDescriptor * descriptor)297 static inline int spice_image_descriptor_is_lossy(const SpiceImageDescriptor *descriptor)
298 {
299     return descriptor->type == SPICE_IMAGE_TYPE_JPEG ||
300            descriptor->type == SPICE_IMAGE_TYPE_JPEG_ALPHA;
301 }
302 
303 SPICE_END_DECLS
304 
305 #endif // H_SPICE_COMMON_DRAW
306