druid_win_shell/
paint.rs

1// Copyright 2017 The xi-editor Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Bureaucracy to create render targets for painting.
16//!
17//! Note that these are currently implemented using hwnd render targets
18//! because they are are (relatively) easy, but for high performance we want
19//! dxgi render targets so we can use present options for minimal
20//! invalidation and low-latency frame timing.
21
22use std::mem;
23use std::ptr::null_mut;
24
25use winapi::Interface;
26use winapi::ctypes::{c_void};
27use winapi::um::d2d1::*;
28use winapi::um::dcommon::*;
29use winapi::um::winuser::*;
30use winapi::shared::dxgi::*;
31use winapi::shared::dxgi1_2::*;
32use winapi::shared::dxgiformat::*;
33use winapi::shared::windef::*;
34use winapi::shared::winerror::*;
35
36use direct2d;
37use direct2d::enums::{AlphaMode, RenderTargetType};
38use direct2d::render_target::{DxgiSurfaceRenderTarget, GenericRenderTarget, HwndRenderTarget};
39
40use Error;
41use util::as_result;
42
43/// Context for painting by app into window.
44pub struct PaintCtx<'a> {
45    pub(crate) d2d_factory: &'a direct2d::Factory,
46    pub(crate) render_target: &'a mut GenericRenderTarget,
47}
48
49pub(crate) unsafe fn create_render_target(d2d_factory: &direct2d::Factory, hwnd: HWND)
50        -> Result<HwndRenderTarget, Error>
51{
52    let mut rect: RECT = mem::uninitialized();
53    GetClientRect(hwnd, &mut rect);
54    let width = (rect.right - rect.left) as u32;
55    let height = (rect.bottom - rect.top) as u32;
56    let res = HwndRenderTarget::create(d2d_factory)
57        .with_hwnd(hwnd)
58        .with_target_type(RenderTargetType::Default)
59        .with_alpha_mode(AlphaMode::Unknown)
60        .with_pixel_size(width, height)
61        .build();
62    if let Err(ref e) = res {
63        println!("Error creating hwnd render target: {:?}", e);
64    }
65    res.map_err(|_| Error::D2Error)
66}
67
68/// Create a render target from a DXGI swapchain.
69///
70/// TODO: probably want to create a DeviceContext, it's more flexible.
71pub(crate) unsafe fn create_render_target_dxgi(d2d_factory: &direct2d::Factory,
72    swap_chain: *mut IDXGISwapChain1, dpi: f32) -> Result<DxgiSurfaceRenderTarget, Error>
73{
74    let mut buffer: *mut IDXGISurface = null_mut();
75    as_result((*swap_chain).GetBuffer(0, &IDXGISurface::uuidof(),
76        &mut buffer as *mut _ as *mut *mut c_void))?;
77    let props = D2D1_RENDER_TARGET_PROPERTIES {
78        _type: D2D1_RENDER_TARGET_TYPE_DEFAULT,
79        pixelFormat: D2D1_PIXEL_FORMAT {
80            format: DXGI_FORMAT_B8G8R8A8_UNORM,
81            alphaMode: D2D1_ALPHA_MODE_IGNORE,
82        },
83        dpiX: dpi,
84        dpiY: dpi,
85        usage: D2D1_RENDER_TARGET_USAGE_NONE,
86        minLevel: D2D1_FEATURE_LEVEL_DEFAULT,
87    };
88
89    let mut render_target: *mut ID2D1RenderTarget = null_mut();
90    let res = (*d2d_factory.get_raw()).CreateDxgiSurfaceRenderTarget(
91        buffer, &props, &mut render_target);
92    (*buffer).Release();
93    if SUCCEEDED(res) {
94        // TODO: maybe use builder
95        Ok(DxgiSurfaceRenderTarget::from_raw(render_target))
96    } else {
97        Err(res.into())
98    }
99}
100
101impl<'a> PaintCtx<'a> {
102
103    /// Return the raw Direct2D factory for this painting context. Note: it's possible
104    /// this will be wrapped to make it easier to port.
105    pub fn d2d_factory(&self) -> &direct2d::Factory {
106        self.d2d_factory
107    }
108
109    /// Return the raw Direct2D RenderTarget for this painting context. Note: it's possible
110    /// this will be wrapped to make it easier to port.
111    pub fn render_target(&mut self) -> &mut GenericRenderTarget {
112        self.render_target
113    }
114}