Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify capture_image_area to work with druid direct2d surfaces #526

Merged
merged 1 commit into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions piet-direct2d/src/d2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ impl DeviceContext {
&mut self,
width: usize,
height: usize,
alpha_mode: D2D1_ALPHA_MODE,
dpi_scale: f32,
) -> Result<Bitmap, Error> {
// Maybe using TryInto would be more Rust-like.
// Note: value is set so that multiplying by 4 (for pitch) is valid.
Expand All @@ -794,18 +794,17 @@ impl DeviceContext {
width: width as u32,
height: height as u32,
};
let format = D2D1_PIXEL_FORMAT {
format: DXGI_FORMAT_R8G8B8A8_UNORM,
alphaMode: alpha_mode,
};
let props = D2D1_BITMAP_PROPERTIES1 {
pixelFormat: format,
dpiX: 96.0,
dpiY: 96.0,
bitmapOptions: D2D1_BITMAP_OPTIONS_NONE,
colorContext: null_mut(),
};

unsafe {
let pixel_format = self.0.GetPixelFormat();
let props = D2D1_BITMAP_PROPERTIES1 {
pixelFormat: pixel_format,
dpiX: dpi_scale * 96.0,
dpiY: dpi_scale * 96.0,
bitmapOptions: D2D1_BITMAP_OPTIONS_TARGET,
colorContext: null_mut(),
};

let mut ptr = null_mut();
let hr = self
.0
Expand Down
13 changes: 12 additions & 1 deletion piet-direct2d/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,15 +481,26 @@ impl<'a> RenderContext for D2DRenderContext<'a> {
let mut target_bitmap = self.rt.create_blank_bitmap(
device_size.width as usize,
device_size.height as usize,
D2D1_ALPHA_MODE_PREMULTIPLIED,
dpi_scale as f32,
)?;

let src_rect = Rect::from_origin_size(device_origin, device_size);

let d2d_dest_point = to_point2u((0.0f32, 0.0f32));
let d2d_src_rect = rect_to_rectu(src_rect);

// Clear layers from the render target
for _ in 0..self.layers.len() {
self.rt.pop_layer();
}

target_bitmap.copy_from_render_target(d2d_dest_point, self.rt, d2d_src_rect);

// Restore cleared layers
for (mask, layer) in self.layers.iter() {
self.rt.push_layer_mask(mask, layer);
}

Ok(target_bitmap)
}

Expand Down