forked from servo/rust-azure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure_hl.rs
535 lines (467 loc) · 15.6 KB
/
azure_hl.rs
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//! High-level bindings to Azure.
use azure::{AzRect, AzFloat, AzIntSize, AzColor, AzColorPatternRef};
use azure::{AzStrokeOptions, AzDrawOptions, AzSurfaceFormat, AzFilter, AzDrawSurfaceOptions};
use azure::{AzBackendType, AzDrawTargetRef, AzSourceSurfaceRef, AzDataSourceSurfaceRef};
use azure::{struct__AzColor};
use azure::{struct__AzDrawOptions, struct__AzDrawSurfaceOptions, struct__AzIntSize};
use azure::{struct__AzRect, struct__AzStrokeOptions, AzGLContext, AzSkiaSharedGLContextRef};
use azure::bindgen::{AzCreateColorPattern, AzCreateDrawTarget, AzCreateDrawTargetForData};
use azure::bindgen::{AzDataSourceSurfaceGetData, AzDataSourceSurfaceGetStride};
use azure::bindgen::{AzDrawTargetClearRect};
use azure::bindgen::{AzDrawTargetCreateSourceSurfaceFromData, AzCreateSkiaSharedGLContext, AzRetainSkiaSharedGLContext};
use azure::bindgen::{AzDrawTargetDrawSurface, AzDrawTargetFillRect, AzDrawTargetFlush};
use azure::bindgen::{AzDrawTargetGetSnapshot, AzDrawTargetSetTransform, AzDrawTargetStrokeRect};
use azure::bindgen::{AzReleaseColorPattern, AzReleaseDrawTarget};
use azure::bindgen::{AzReleaseSourceSurface, AzRetainDrawTarget};
use azure::bindgen::{AzSourceSurfaceGetDataSurface, AzSourceSurfaceGetFormat};
use azure::bindgen::{AzSourceSurfaceGetSize, AzCreateSkiaDrawTargetForFBO, AzSkiaGetCurrentGLContext};
use azure::bindgen::{AzSkiaSharedGLContextMakeCurrent, AzSkiaSharedGLContextGetTextureID, AzSkiaSharedGLContextFlush};
use core::libc::types::common::c99::uint16_t;
use core::libc::c_uint;
use core::cast::transmute;
use core::ptr::{null, to_unsafe_ptr};
use geom::matrix2d::Matrix2D;
use geom::rect::Rect;
use geom::size::Size2D;
use std::arc::ARC;
use std::arc;
pub trait AsAzureRect {
fn as_azure_rect(&self) -> AzRect;
}
impl AsAzureRect for Rect<AzFloat> {
fn as_azure_rect(&self) -> AzRect {
struct__AzRect {
x: self.origin.x,
y: self.origin.y,
width: self.size.width,
height: self.size.height
}
}
}
pub trait AsAzureIntSize {
fn as_azure_int_size(&self) -> AzIntSize;
}
impl AsAzureIntSize for Size2D<i32> {
fn as_azure_int_size(&self) -> AzIntSize {
struct__AzIntSize {
width: self.width,
height: self.height
}
}
}
pub struct Color {
r: AzFloat,
g: AzFloat,
b: AzFloat,
a: AzFloat,
}
impl Color {
fn as_azure_color(&self) -> AzColor {
struct__AzColor { r: self.r, g: self.g, b: self.b, a: self.a }
}
}
pub fn Color(r: AzFloat, g: AzFloat, b: AzFloat, a: AzFloat) -> Color {
Color { r: r, g: g, b: b, a: a }
}
// FIXME: Should have a class hierarchy here starting with Pattern.
pub struct ColorPattern {
azure_color_pattern: AzColorPatternRef,
}
impl Drop for ColorPattern {
fn finalize(&self) {
unsafe {
AzReleaseColorPattern(self.azure_color_pattern);
}
}
}
pub fn ColorPattern(color: Color) -> ColorPattern {
unsafe {
ColorPattern {
azure_color_pattern: AzCreateColorPattern(&color.as_azure_color())
}
}
}
pub struct StrokeOptions {
line_width: AzFloat,
miter_limit: AzFloat,
fields: uint16_t,
}
impl StrokeOptions {
fn as_azure_stroke_options(&self) -> AzStrokeOptions {
struct__AzStrokeOptions {
mLineWidth: self.line_width,
mMiterLimit: self.miter_limit,
mDashPattern: null(),
mDashLength: 0,
mDashOffset: 0.0f as AzFloat,
fields: self.fields
}
}
}
pub fn StrokeOptions(line_width: AzFloat,
miter_limit: AzFloat,
fields: uint16_t) -> StrokeOptions {
StrokeOptions {
line_width: line_width,
miter_limit: miter_limit,
fields: fields
}
}
pub struct DrawOptions {
alpha: AzFloat,
fields: uint16_t,
}
impl DrawOptions {
fn as_azure_draw_options(&self) -> AzDrawOptions {
struct__AzDrawOptions {
mAlpha: self.alpha,
fields: self.fields
}
}
}
pub fn DrawOptions(alpha: AzFloat, fields: uint16_t) -> DrawOptions {
DrawOptions {
alpha : alpha,
fields : fields,
}
}
pub enum SurfaceFormat {
B8G8R8A8,
B8G8R8X8,
R5G6B5,
A8
}
impl SurfaceFormat {
fn as_azure_surface_format(self) -> AzSurfaceFormat {
self as AzSurfaceFormat
}
pub fn new(azure_surface_format: AzSurfaceFormat) -> SurfaceFormat {
match azure_surface_format {
0 => B8G8R8A8,
1 => B8G8R8X8,
2 => R5G6B5,
3 => A8,
_ => fail!(~"SurfaceFormat::new(): unknown Azure surface format")
}
}
}
pub enum Filter {
Linear,
Point
}
impl Filter {
fn as_azure_filter(self) -> AzFilter {
self as AzFilter
}
}
pub struct DrawSurfaceOptions {
filter: Filter,
sampling_bounds: bool,
}
impl DrawSurfaceOptions {
fn as_azure_draw_surface_options(&self) -> AzDrawSurfaceOptions {
struct__AzDrawSurfaceOptions {
fields: ((self.filter as int) | (if self.sampling_bounds { 8 } else { 0 })) as u32
}
}
}
pub fn DrawSurfaceOptions(filter: Filter, sampling_bounds: bool) -> DrawSurfaceOptions {
DrawSurfaceOptions {
filter: filter,
sampling_bounds: sampling_bounds,
}
}
#[deriving(Eq)]
pub enum BackendType {
NoBackend,
Direct2DBackend,
CoreGraphicsBackend,
CoreGraphicsAcceleratedBackend,
CairoBackend,
SkiaBackend,
RecordingBackend
}
impl BackendType {
pub fn as_azure_backend_type(self) -> AzBackendType {
match self {
NoBackend => 0,
Direct2DBackend => 1,
CoreGraphicsBackend => 2,
CoreGraphicsAcceleratedBackend => 3,
CairoBackend => 4,
SkiaBackend => 5,
RecordingBackend => 6,
}
}
}
pub struct DrawTarget {
azure_draw_target: AzDrawTargetRef,
data: Option<ARC<~[u8]>>,
skia_context: Option<AzSkiaSharedGLContextRef>
}
impl Drop for DrawTarget {
fn finalize(&self) {
unsafe {
AzReleaseDrawTarget(self.azure_draw_target);
}
}
}
pub impl DrawTarget {
pub fn new(backend: BackendType, size: Size2D<i32>, format: SurfaceFormat)
-> DrawTarget {
unsafe {
let azure_draw_target = AzCreateDrawTarget(backend.as_azure_backend_type(),
to_unsafe_ptr(&size.as_azure_int_size()),
format.as_azure_surface_format());
if azure_draw_target == ptr::null() { fail!(~"null azure draw target"); }
DrawTarget {
azure_draw_target: azure_draw_target,
data: None,
skia_context: None
}
}
}
pub fn new_with_data(backend: BackendType,
data: ~[u8],
offset: uint,
size: Size2D<i32>,
stride: i32,
format: SurfaceFormat) -> DrawTarget {
unsafe {
assert!((data.len() - offset) as i32 >= stride * size.height);
let azure_draw_target =
AzCreateDrawTargetForData(backend.as_azure_backend_type(),
to_unsafe_ptr(&data[offset]),
to_unsafe_ptr(&size.as_azure_int_size()),
stride,
format.as_azure_surface_format());
if azure_draw_target == ptr::null() { fail!(~"null azure draw target"); }
DrawTarget {
azure_draw_target: azure_draw_target,
data: Some(ARC(data)),
skia_context: None
}
}
}
pub fn new_with_fbo(backend: BackendType,
share_context: AzGLContext,
size: Size2D<i32>,
format: SurfaceFormat) -> DrawTarget {
assert!(backend == SkiaBackend);
unsafe {
let skia_context = AzCreateSkiaSharedGLContext(share_context);
let azure_draw_target = AzCreateSkiaDrawTargetForFBO(skia_context,
to_unsafe_ptr(&size.as_azure_int_size()),
format.as_azure_surface_format());
if azure_draw_target == ptr::null() { fail!(~"null azure draw target"); }
DrawTarget {
azure_draw_target: azure_draw_target,
data: None,
skia_context: Some(skia_context)
}
}
}
fn clone(&self) -> DrawTarget {
unsafe {
AzRetainDrawTarget(self.azure_draw_target);
if self.skia_context.is_some() {
AzRetainSkiaSharedGLContext(self.skia_context.get());
}
}
DrawTarget {
azure_draw_target: self.azure_draw_target,
data: match self.data {
None => None,
Some(ref arc) => Some(arc::clone(arc))
},
skia_context: self.skia_context
}
}
fn make_current(&self) {
match self.skia_context {
None => {}
Some(ctx) => {
unsafe {
AzSkiaSharedGLContextMakeCurrent(ctx);
}
}
}
}
fn get_texture(&self) -> Option<c_uint> {
match self.skia_context {
None => None,
Some(ctx) => {
unsafe {
Some(AzSkiaSharedGLContextGetTextureID(ctx))
}
}
}
}
fn flush(&self) {
unsafe {
AzDrawTargetFlush(self.azure_draw_target);
match self.skia_context {
None => {}
Some(ctx) => { AzSkiaSharedGLContextFlush(ctx); }
}
}
}
fn clear_rect(&self, rect: &Rect<AzFloat>) {
unsafe {
AzDrawTargetClearRect(self.azure_draw_target, &rect.as_azure_rect());
}
}
fn fill_rect(&self, rect: &Rect<AzFloat>, pattern: &ColorPattern) {
unsafe {
AzDrawTargetFillRect(self.azure_draw_target,
to_unsafe_ptr(&rect.as_azure_rect()),
pattern.azure_color_pattern);
}
}
fn stroke_rect(&self,
rect: &Rect<AzFloat>,
pattern: &ColorPattern,
stroke_options: &StrokeOptions,
draw_options: &DrawOptions) {
unsafe {
AzDrawTargetStrokeRect(self.azure_draw_target,
&rect.as_azure_rect(),
pattern.azure_color_pattern,
&stroke_options.as_azure_stroke_options(),
&draw_options.as_azure_draw_options());
}
}
fn draw_surface(&self,
surface: SourceSurface,
dest: Rect<AzFloat>,
source: Rect<AzFloat>,
surf_options: DrawSurfaceOptions,
options: DrawOptions) {
unsafe {
AzDrawTargetDrawSurface(self.azure_draw_target,
surface.azure_source_surface,
to_unsafe_ptr(&dest.as_azure_rect()),
to_unsafe_ptr(&source.as_azure_rect()),
to_unsafe_ptr(&surf_options.as_azure_draw_surface_options()),
to_unsafe_ptr(&options.as_azure_draw_options()));
}
}
fn snapshot(&self) -> SourceSurface {
unsafe {
let azure_surface = AzDrawTargetGetSnapshot(self.azure_draw_target);
SourceSurface(azure_surface)
}
}
fn create_source_surface_from_data(&self,
data: &[u8],
size: Size2D<i32>,
stride: i32,
format: SurfaceFormat)
-> SourceSurface {
assert!(data.len() as i32 == stride * size.height);
unsafe {
let azure_surface = AzDrawTargetCreateSourceSurfaceFromData(
self.azure_draw_target,
&data[0],
&size.as_azure_int_size(),
stride,
format.as_azure_surface_format());
SourceSurface(azure_surface)
}
}
fn set_transform(&self, matrix: &Matrix2D<AzFloat>) {
unsafe {
AzDrawTargetSetTransform(self.azure_draw_target, transmute(matrix));
}
}
}
// Ugly workaround for the lack of explicit self.
pub fn clone_mutable_draw_target(draw_target: &mut DrawTarget) -> DrawTarget {
return draw_target.clone();
}
pub struct SourceSurface {
priv azure_source_surface: AzSourceSurfaceRef,
}
impl Drop for SourceSurface {
fn finalize(&self) {
unsafe {
AzReleaseSourceSurface(self.azure_source_surface);
}
}
}
pub fn SourceSurface(azure_source_surface: AzSourceSurfaceRef) -> SourceSurface {
SourceSurface {
azure_source_surface: azure_source_surface
}
}
pub trait SourceSurfaceMethods {
fn get_azure_source_surface(&self) -> AzSourceSurfaceRef;
fn size(&self) -> Size2D<i32> {
unsafe {
let size = AzSourceSurfaceGetSize(self.get_azure_source_surface());
Size2D { width: size.width, height: size.height }
}
}
fn format(&self) -> SurfaceFormat {
unsafe {
SurfaceFormat::new(AzSourceSurfaceGetFormat(self.get_azure_source_surface()))
}
}
}
impl SourceSurface {
pub fn get_data_surface(&self) -> DataSourceSurface {
unsafe {
let data_source_surface = AzSourceSurfaceGetDataSurface(
self.azure_source_surface);
DataSourceSurface {
azure_data_source_surface: data_source_surface
}
}
}
}
impl SourceSurfaceMethods for SourceSurface {
fn get_azure_source_surface(&self) -> AzSourceSurfaceRef { self.azure_source_surface }
}
pub struct DataSourceSurface {
priv azure_data_source_surface: AzDataSourceSurfaceRef,
}
impl Drop for DataSourceSurface {
fn finalize(&self) {
unsafe {
AzReleaseSourceSurface(self.azure_data_source_surface);
}
}
}
impl DataSourceSurface {
pub fn with_data(&self, f: &fn(&[u8])) {
unsafe {
let buf = AzDataSourceSurfaceGetData(self.azure_data_source_surface);
let len = self.stride() * self.size().height;
vec::raw::buf_as_slice(buf, len as uint, f);
}
}
pub fn stride(&self) -> i32 {
unsafe {
AzDataSourceSurfaceGetStride(self.azure_data_source_surface)
}
}
// FIXME: Workaround for lack of working cross-crate default methods.
pub fn get_size(&self) -> Size2D<i32> {
self.size()
}
}
impl SourceSurfaceMethods for DataSourceSurface {
fn get_azure_source_surface(&self) -> AzSourceSurfaceRef {
self.azure_data_source_surface
}
}
pub fn current_gl_context() -> AzGLContext {
unsafe {
let r = AzSkiaGetCurrentGLContext();
println(fmt!("current gl context = %?", r));
r
}
}