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
use std::fmt;
use direct2d::enums::BitmapOptions;
use direct2d::image::Bitmap;
use direct2d::render_target::RenderTag;
use direct2d::RenderTarget;
use direct3d11::flags::{BindFlags, CreateDeviceFlags};
use direct3d11::helpers::ComWrapper;
use dxgi::flags::Format;
use piet::{ErrorKind, ImageFormat};
#[doc(hidden)]
pub use piet_direct2d::*;
pub type Piet<'a> = D2DRenderContext<'a>;
pub type Brush = GenericBrush;
pub type PietText<'a> = D2DText<'a>;
pub type PietFont = D2DFont;
pub type PietFontBuilder<'a> = D2DFontBuilder<'a>;
pub type PietTextLayout = D2DTextLayout;
pub type PietTextLayoutBuilder<'a> = D2DTextLayoutBuilder<'a>;
pub type Image = Bitmap;
pub struct Device {
d2d: direct2d::Factory,
dwrite: directwrite::Factory,
d3d: direct3d11::Device,
d3d_ctx: direct3d11::DeviceContext,
device: direct2d::Device,
}
pub struct BitmapTarget<'a> {
width: usize,
height: usize,
d2d: &'a direct2d::Factory,
dwrite: &'a directwrite::Factory,
d3d: &'a direct3d11::Device,
d3d_ctx: &'a direct3d11::DeviceContext,
tex: direct3d11::Texture2D,
context: direct2d::DeviceContext,
}
trait WrapError<T> {
fn wrap(self) -> Result<T, piet::Error>;
}
#[derive(Debug)]
struct WrappedD2DTag(direct2d::Error, Option<RenderTag>);
#[derive(Debug)]
struct WrappedD3D11Error(direct3d11::Error);
#[derive(Debug)]
struct WrappedDxgiError(dxgi::Error);
impl std::error::Error for WrappedD2DTag {}
impl std::error::Error for WrappedD3D11Error {}
impl std::error::Error for WrappedDxgiError {}
impl fmt::Display for WrappedD2DTag {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Direct2D error: {}, tag {:?}", self.0, self.1)
}
}
impl fmt::Display for WrappedD3D11Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Direct3D11 error: {}", self.0)
}
}
impl fmt::Display for WrappedDxgiError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Dxgi error: {}", self.0)
}
}
impl<T> WrapError<T> for Result<T, (direct2d::Error, Option<RenderTag>)> {
fn wrap(self) -> Result<T, piet::Error> {
self.map_err(|(e, t)| {
let e: Box<dyn std::error::Error> = Box::new(WrappedD2DTag(e, t));
e.into()
})
}
}
impl<T> WrapError<T> for Result<T, direct3d11::Error> {
fn wrap(self) -> Result<T, piet::Error> {
self.map_err(|e| {
let e: Box<dyn std::error::Error> = Box::new(WrappedD3D11Error(e));
e.into()
})
}
}
impl<T> WrapError<T> for Result<T, dxgi::Error> {
fn wrap(self) -> Result<T, piet::Error> {
self.map_err(|e| {
let e: Box<dyn std::error::Error> = Box::new(WrappedDxgiError(e));
e.into()
})
}
}
impl Device {
pub fn new() -> Result<Device, piet::Error> {
let d2d = direct2d::Factory::new().unwrap();
let dwrite = directwrite::Factory::new().unwrap();
let (_, d3d, d3d_ctx) = direct3d11::Device::create()
.with_flags(CreateDeviceFlags::BGRA_SUPPORT)
.build()
.unwrap();
let device = direct2d::Device::create(&d2d, &d3d.as_dxgi()).unwrap();
Ok(Device {
d2d,
dwrite,
d3d,
d3d_ctx,
device,
})
}
pub fn bitmap_target(
&self,
width: usize,
height: usize,
pix_scale: f64,
) -> Result<BitmapTarget, piet::Error> {
let mut context = direct2d::DeviceContext::create(&self.device, false).unwrap();
let tex = direct3d11::Texture2D::create(&self.d3d)
.with_size(width as u32, height as u32)
.with_format(Format::R8G8B8A8Unorm)
.with_bind_flags(BindFlags::RENDER_TARGET | BindFlags::SHADER_RESOURCE)
.build()
.unwrap();
let target = Bitmap::create(&context)
.with_dxgi_surface(&tex.as_dxgi())
.with_dpi(96.0 * pix_scale as f32, 96.0 * pix_scale as f32)
.with_options(BitmapOptions::TARGET)
.build()
.unwrap();
context.set_target(&target);
context.begin_draw();
Ok(BitmapTarget {
width,
height,
d2d: &self.d2d,
dwrite: &self.dwrite,
d3d: &self.d3d,
d3d_ctx: &self.d3d_ctx,
tex,
context,
})
}
}
impl<'a> BitmapTarget<'a> {
pub fn render_context<'b>(&'b mut self) -> D2DRenderContext<'b> {
D2DRenderContext::new(self.d2d, self.dwrite, &mut self.context)
}
pub fn into_raw_pixels(mut self, fmt: ImageFormat) -> Result<Vec<u8>, piet::Error> {
if fmt != ImageFormat::RgbaPremul {
return Err(piet::new_error(ErrorKind::NotSupported));
}
self.context.end_draw().wrap()?;
let temp_texture = direct3d11::texture2d::Texture2D::create(self.d3d)
.with_size(self.width as u32, self.height as u32)
.with_format(direct3d11::flags::Format::R8G8B8A8Unorm)
.with_bind_flags(direct3d11::flags::BindFlags::NONE)
.with_usage(direct3d11::flags::Usage::Staging)
.with_cpu_access_flags(direct3d11::flags::CpuAccessFlags::READ)
.build()
.wrap()?;
let mut raw_pixels: Vec<u8> = Vec::with_capacity(self.width * self.height * 4);
unsafe {
let ctx = &*self.d3d_ctx.get_raw();
ctx.CopyResource(
temp_texture.get_raw() as *mut _,
self.tex.get_raw() as *mut _,
);
ctx.Flush();
let surface = temp_texture.as_dxgi();
let map = surface.map(true, false, false).wrap()?;
for y in 0..(self.height as u32) {
raw_pixels.extend_from_slice(&map.row(y)[..self.width * 4]);
}
}
Ok(raw_pixels)
}
}