1 /* Cairo - a vector graphics library with display and print output
2  *
3  * Copyright © 2009 Chris Wilson
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it either under the terms of the GNU Lesser General Public
7  * License version 2.1 as published by the Free Software Foundation
8  * (the "LGPL") or, at your option, under the terms of the Mozilla
9  * Public License Version 1.1 (the "MPL"). If you do not alter this
10  * notice, a recipient may use your version of this file under either
11  * the MPL or the LGPL.
12  *
13  * You should have received a copy of the LGPL along with this library
14  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16  * You should have received a copy of the MPL along with this library
17  * in the file COPYING-MPL-1.1
18  *
19  * The contents of this file are subject to the Mozilla Public License
20  * Version 1.1 (the "License"); you may not use this file except in
21  * compliance with the License. You may obtain a copy of the License at
22  * http://www.mozilla.org/MPL/
23  *
24  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26  * the specific language governing rights and limitations.
27  *
28  * The Original Code is the cairo graphics library.
29  *
30  * The Initial Developer of the Original Code is Chris Wilson.
31  *
32  * Contributors(s):
33  *	Chris Wilson <chris@chris-wilson.co.uk>
34  */
35 
36 #ifndef CAIRO_DRM_PRIVATE_H
37 #define CAIRO_DRM_PRIVATE_H
38 
39 #include "cairo-drm.h"
40 
41 #include "cairo-device-private.h"
42 #include "cairo-reference-count-private.h"
43 #include "cairo-surface-private.h"
44 
45 #include <sys/types.h> /* dev_t */
46 
47 typedef struct _cairo_drm_device cairo_drm_device_t;
48 
49 typedef cairo_drm_device_t *
50 (*cairo_drm_device_create_func_t) (int fd,
51 				   dev_t dev,
52 				   int vendor_id,
53 				   int chip_id);
54 
55 typedef cairo_int_status_t
56 (*cairo_drm_device_flush_func_t) (cairo_drm_device_t *device);
57 
58 typedef cairo_int_status_t
59 (*cairo_drm_device_throttle_func_t) (cairo_drm_device_t *device);
60 
61 typedef void
62 (*cairo_drm_device_destroy_func_t) (void *data);
63 
64 typedef cairo_surface_t *
65 (*cairo_drm_surface_create_func_t) (cairo_drm_device_t *device,
66 				    cairo_format_t format,
67 				    int width, int height);
68 
69 typedef cairo_surface_t *
70 (*cairo_drm_surface_create_for_name_func_t) (cairo_drm_device_t *device,
71 				             unsigned int name,
72 					     cairo_format_t format,
73 					     int width, int height, int stride);
74 
75 typedef cairo_surface_t *
76 (*cairo_drm_surface_create_from_cacheable_image_func_t)
77     (cairo_drm_device_t *device, cairo_surface_t *image);
78 
79 typedef cairo_int_status_t
80 (*cairo_drm_surface_flink_func_t) (void *surface);
81 
82 typedef cairo_status_t
83 (*cairo_drm_surface_enable_scan_out_func_t) (void *surface);
84 
85 typedef cairo_surface_t *
86 (*cairo_drm_surface_map_to_image_func_t) (void *surface);
87 
88 typedef struct _cairo_drm_bo_backend {
89     void (*release) (void *device, void *bo);
90 } cairo_drm_bo_backend_t;
91 
92 typedef struct _cairo_drm_device_backend {
93     cairo_drm_device_flush_func_t flush;
94     cairo_drm_device_throttle_func_t throttle;
95     cairo_drm_device_destroy_func_t destroy;
96 } cairo_drm_device_backend_t;
97 
98 typedef struct _cairo_drm_surface_backend {
99     cairo_drm_surface_create_func_t create;
100     cairo_drm_surface_create_for_name_func_t create_for_name;
101     cairo_drm_surface_create_from_cacheable_image_func_t create_from_cacheable_image;
102     cairo_drm_surface_flink_func_t flink;
103     cairo_drm_surface_enable_scan_out_func_t enable_scan_out;
104     cairo_drm_surface_map_to_image_func_t map_to_image;
105 } cairo_drm_surface_backend_t;
106 
107 typedef struct _cairo_drm_bo {
108     cairo_reference_count_t ref_count;
109     uint32_t name;
110     uint32_t handle;
111     uint32_t size;
112 } cairo_drm_bo_t;
113 
114 struct _cairo_drm_device {
115     cairo_device_t base;
116 
117     int vendor_id;
118     int chip_id;
119     dev_t id;
120     int fd;
121 
122     int max_surface_size;
123 
124     cairo_drm_bo_backend_t bo;
125     cairo_drm_surface_backend_t surface;
126     cairo_drm_device_backend_t device;
127 
128     cairo_drm_device_t *next, *prev;
129 };
130 
131 typedef struct _cairo_drm_surface {
132     cairo_surface_t base;
133 
134     cairo_drm_bo_t *bo;
135 
136     cairo_format_t format;
137     int width, height, stride;
138 
139     cairo_surface_t *fallback;
140     uint32_t map_count;
141 } cairo_drm_surface_t;
142 
143 static inline cairo_drm_bo_t *
cairo_drm_bo_reference(cairo_drm_bo_t * bo)144 cairo_drm_bo_reference (cairo_drm_bo_t *bo)
145 {
146     _cairo_reference_count_inc (&bo->ref_count);
147     return bo;
148 }
149 
150 static cairo_always_inline void
cairo_drm_bo_destroy(cairo_device_t * abstract_device,cairo_drm_bo_t * bo)151 cairo_drm_bo_destroy (cairo_device_t *abstract_device,
152 		      cairo_drm_bo_t *bo)
153 {
154     if (_cairo_reference_count_dec_and_test (&bo->ref_count)) {
155 	cairo_drm_device_t *device = (cairo_drm_device_t *) abstract_device;
156 	device->bo.release (device, bo);
157     }
158 }
159 
160 cairo_private cairo_status_t
161 _cairo_drm_bo_open_for_name (const cairo_drm_device_t *dev,
162 			     cairo_drm_bo_t *bo,
163 			     uint32_t name);
164 
165 cairo_private cairo_status_t
166 _cairo_drm_bo_flink (const cairo_drm_device_t *dev,
167 		     cairo_drm_bo_t *bo);
168 
169 cairo_private void
170 _cairo_drm_bo_close (const cairo_drm_device_t *dev,
171 		     cairo_drm_bo_t *bo);
172 
173 cairo_private void
174 _cairo_drm_surface_init (cairo_drm_surface_t *surface,
175 			 cairo_format_t format,
176 			 int width, int height);
177 
178 cairo_private cairo_status_t
179 _cairo_drm_surface_finish (cairo_drm_surface_t *surface);
180 
181 cairo_private void
182 _cairo_drm_surface_get_font_options (void                  *abstract_surface,
183 				     cairo_font_options_t  *options);
184 
185 cairo_private cairo_bool_t
186 _cairo_drm_surface_get_extents (void *abstract_surface,
187 			        cairo_rectangle_int_t *rectangle);
188 
189 cairo_private cairo_int_status_t
190 _cairo_drm_surface_flink (void *abstract_surface);
191 
192 static inline cairo_drm_device_t *
_cairo_drm_device_create_in_error(cairo_status_t status)193 _cairo_drm_device_create_in_error (cairo_status_t status)
194 {
195     return (cairo_drm_device_t *) _cairo_device_create_in_error (CAIRO_STATUS_NO_MEMORY);
196 }
197 
198 cairo_private cairo_drm_device_t *
199 _cairo_drm_device_init (cairo_drm_device_t *device,
200 			int fd,
201 			dev_t devid,
202 			int vendor_id,
203 			int chip_id,
204 			int max_surface_size);
205 
206 cairo_private void
207 _cairo_drm_device_fini (cairo_drm_device_t *device);
208 
209 /* h/w specific backends */
210 
211 cairo_private cairo_drm_device_t *
212 _cairo_drm_intel_device_create (int fd, dev_t dev, int vendor_id, int chip_id);
213 
214 cairo_private cairo_drm_device_t *
215 _cairo_drm_i915_device_create (int fd, dev_t dev, int vendor_id, int chip_id);
216 
217 cairo_private cairo_drm_device_t *
218 _cairo_drm_i965_device_create (int fd, dev_t dev, int vendor_id, int chip_id);
219 
220 cairo_private cairo_drm_device_t *
221 _cairo_drm_radeon_device_create (int fd, dev_t dev, int vendor_id, int chip_id);
222 
223 #if CAIRO_HAS_GALLIUM_SURFACE
224 cairo_private cairo_drm_device_t *
225 _cairo_drm_gallium_device_create (int fd, dev_t dev, int vendor_id, int chip_id);
226 #endif
227 
228 slim_hidden_proto (cairo_drm_device_default);
229 slim_hidden_proto (cairo_drm_device_get);
230 slim_hidden_proto (cairo_drm_device_get_for_fd);
231 
232 slim_hidden_proto (cairo_drm_surface_create_for_name);
233 
234 cairo_private cairo_bool_t
235 _cairo_drm_size_is_valid (cairo_device_t *abstract_device,
236 			  int width, int height);
237 
238 #endif /* CAIRO_DRM_PRIVATE_H */
239