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
//! Support for piet Direct2D back-end.

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::*;

/// The `RenderContext` for the Direct2D backend, which is selected.
pub type Piet<'a> = D2DRenderContext<'a>;

/// The associated brush type for this backend.
///
/// This type matches `RenderContext::Brush`
pub type Brush = GenericBrush;

/// The associated text factory for this backend.
///
/// This type matches `RenderContext::Text`
pub type PietText<'a> = D2DText<'a>;

/// The associated font type for this backend.
///
/// This type matches `RenderContext::Text::Font`
pub type PietFont = D2DFont;

/// The associated font builder for this backend.
///
/// This type matches `RenderContext::Text::FontBuilder`
pub type PietFontBuilder<'a> = D2DFontBuilder<'a>;

/// The associated text layout type for this backend.
///
/// This type matches `RenderContext::Text::TextLayout`
pub type PietTextLayout = D2DTextLayout;

/// The associated text layout builder for this backend.
///
/// This type matches `RenderContext::Text::TextLayoutBuilder`
pub type PietTextLayoutBuilder<'a> = D2DTextLayoutBuilder<'a>;

/// The associated image type for this backend.
///
/// This type matches `RenderContext::Image`
pub type Image = Bitmap;

/// A struct that can be used to create bitmap render contexts.
pub struct Device {
    d2d: direct2d::Factory,
    dwrite: directwrite::Factory,
    d3d: direct3d11::Device,
    d3d_ctx: direct3d11::DeviceContext,
    device: direct2d::Device,
}

/// A struct provides a `RenderContext` and then can have its bitmap extracted.
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 {
    /// Create a new device.
    ///
    /// This creates new Direct2D and DirectWrite factories, a Direct3D
    /// device, and a Direct2D device.
    pub fn new() -> Result<Device, piet::Error> {
        let d2d = direct2d::Factory::new().unwrap();
        let dwrite = directwrite::Factory::new().unwrap();

        // Initialize a D3D Device
        let (_, d3d, d3d_ctx) = direct3d11::Device::create()
            .with_flags(CreateDeviceFlags::BGRA_SUPPORT)
            .build()
            .unwrap();

        // Create the D2D Device and Context
        let device = direct2d::Device::create(&d2d, &d3d.as_dxgi()).unwrap();

        Ok(Device {
            d2d,
            dwrite,
            d3d,
            d3d_ctx,
            device,
        })
    }

    /// Create a new bitmap target.
    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();

        // Create a texture to render to
        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();

        // Bind the backing texture to a D2D Bitmap
        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> {
    /// Get a piet `RenderContext` for the bitmap.
    ///
    /// Note: caller is responsible for calling `finish` on the render
    /// context at the end of rendering.
    pub fn render_context<'b>(&'b mut self) -> D2DRenderContext<'b> {
        D2DRenderContext::new(self.d2d, self.dwrite, &mut self.context)
    }

    /// Get raw RGBA pixels from the bitmap.
    pub fn into_raw_pixels(mut self, fmt: ImageFormat) -> Result<Vec<u8>, piet::Error> {
        // TODO: convert other formats.
        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()?;

        // TODO: Have a safe way to accomplish this :D
        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)
    }
}