Kiwano Engine v1.3.x
D2DDeviceResources.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/render/DirectX/helper.h>
23#include <kiwano/render/DirectX/FontLoader.h>
24
25namespace kiwano
26{
27namespace graphics
28{
29namespace directx
30{
31
32MIDL_INTERFACE("5706684a-bf6d-4b03-b627-094758a33032")
33KGE_API ID2DDeviceResources : public IUnknown
34{
35public:
36 virtual HRESULT Initialize(_In_ ComPtr<IDXGIDevice> dxgi_device, _In_ ComPtr<IDXGISwapChain> dxgi_swap_chain,
37 FLOAT dpi) = 0;
38
39 virtual HRESULT CreateDeviceContext(_Out_ ComPtr<ID2D1DeviceContext> & device_ctx) = 0;
40
41 virtual HRESULT CreateBitmapSourceFromMemory(_Out_ ComPtr<IWICBitmapSource> & source, _In_ UINT width,
42 _In_ UINT height, _In_ UINT cbStride, _In_ UINT cbBufferSize,
43 _In_ BYTE * buffer, _In_ REFWICPixelFormatGUID cPixelFormat) = 0;
44
45 virtual HRESULT CreateBitmapConverter(_Out_ ComPtr<IWICFormatConverter> & converter,
46 _In_opt_ ComPtr<IWICBitmapSource> source, _In_ REFWICPixelFormatGUID format,
47 WICBitmapDitherType dither, _In_opt_ ComPtr<IWICPalette> palette,
48 double alpha_threshold_percent, WICBitmapPaletteType palette_translate) = 0;
49
50 virtual HRESULT CreateBitmapFromConverter(_Out_ ComPtr<ID2D1Bitmap> & bitmap,
51 _In_opt_ const D2D1_BITMAP_PROPERTIES* properties,
52 _In_ ComPtr<IWICFormatConverter> converter) = 0;
53
54 virtual HRESULT CreateBitmapDecoderFromFile(_Out_ ComPtr<IWICBitmapDecoder> & decoder, _In_ LPCWSTR file_path) = 0;
55
56 virtual HRESULT CreateBitmapDecoderFromResource(_Out_ ComPtr<IWICBitmapDecoder> & decoder, _In_ void* data,
57 DWORD data_size) = 0;
58
59 virtual HRESULT CreateTextFormat(_Out_ ComPtr<IDWriteTextFormat> & text_format, _In_ LPCWSTR family,
60 _In_ ComPtr<IDWriteFontCollection> collection, DWRITE_FONT_WEIGHT weight,
61 DWRITE_FONT_STYLE style, DWRITE_FONT_STRETCH stretch, FLOAT font_size) = 0;
62
63 virtual HRESULT CreateTextLayout(_Out_ ComPtr<IDWriteTextLayout> & text_layout, _In_ LPCWSTR text, UINT32 length,
64 _In_ ComPtr<IDWriteTextFormat> text_format) = 0;
65
66 virtual HRESULT CreateFontCollectionFromFiles(_Out_ ComPtr<IDWriteFontCollection> & font_collection,
67 const Vector<String>& file_paths) = 0;
68
69 virtual HRESULT CreateFontCollectionFromBinaryData(_Out_ ComPtr<IDWriteFontCollection> & font_collection,
70 const Vector<BinaryData>& data) = 0;
71
72 virtual HRESULT GetFontFamilyNames(_Out_ Vector<String> & family_names,
73 _In_ ComPtr<IDWriteFontCollection> font_collection) = 0;
74
75 virtual FLOAT GetDpi() const = 0;
76
77 virtual HRESULT SetDpi(FLOAT dpi) = 0;
78
79 virtual HRESULT SetLogicalSize(float width, float height) = 0;
80
81 virtual HRESULT HandleDeviceLost(_In_ ComPtr<IDXGIDevice> dxgi_device,
82 _In_ ComPtr<IDXGISwapChain> dxgi_swap_chain) = 0;
83
84 virtual void DiscardResources() = 0;
85
86 virtual void ResetTextRenderingParams(_In_ HMONITOR monitor) = 0;
87
88 inline ID2D1Factory1* GetFactory()
89 {
90 KGE_ASSERT(factory_);
91 return factory_.Get();
92 }
93
94 inline IWICImagingFactory* GetWICImagingFactory()
95 {
96 KGE_ASSERT(imaging_factory_);
97 return imaging_factory_.Get();
98 }
99
100 inline IDWriteFactory* GetDWriteFactory()
101 {
102 KGE_ASSERT(dwrite_factory_);
103 return dwrite_factory_.Get();
104 }
105
106 inline ID2D1Device* GetDevice()
107 {
108 KGE_ASSERT(device_);
109 return device_.Get();
110 }
111
112 inline ID2D1DeviceContext* GetDeviceContext()
113 {
114 KGE_ASSERT(device_context_);
115 return device_context_.Get();
116 }
117
118protected:
119 ComPtr<ID2D1Factory1> factory_;
120 ComPtr<ID2D1Device> device_;
121 ComPtr<ID2D1DeviceContext> device_context_;
122
123 ComPtr<IWICImagingFactory> imaging_factory_;
124 ComPtr<IDWriteFactory> dwrite_factory_;
125};
126
127extern ComPtr<ID2DDeviceResources> GetD2DDeviceResources();
128
129} // namespace directx
130} // namespace graphics
131} // namespace kiwano