// Copyright (c) 2016-2018 Kiwano - Nomango // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. #pragma once #include "../Font.h" #include "../Color.h" #include "../../math/math.h" #include "../../base/Resource.h" #include #include #include namespace kiwano { namespace DX { template inline void SafeRelease(T*& ptr) { if (ptr != nullptr) { ptr->Release(); ptr = nullptr; } } template inline T* SafeAcquire(T* ptr) { if (ptr != nullptr) { ptr->AddRef(); } return ptr; } // // Point2F // inline D2D1_POINT_2F const& ConvertToPoint2F(Vec2 const& vec2) { return reinterpret_cast(vec2); } inline D2D1_POINT_2F& ConvertToPoint2F(Vec2& vec2) { return reinterpret_cast(vec2); } inline const D2D1_POINT_2F* ConvertToPoint2F(const Vec2* vec2) { return reinterpret_cast(vec2); } inline D2D1_POINT_2F* ConvertToPoint2F(Vec2* vec2) { return reinterpret_cast(vec2); } // // SizeF // inline D2D1_SIZE_F const& ConvertToSizeF(Vec2 const& vec2) { return reinterpret_cast(vec2); } inline D2D1_SIZE_F& ConvertToSizeF(Vec2& vec2) { return reinterpret_cast(vec2); } inline const D2D1_SIZE_F* ConvertToSizeF(const Vec2* vec2) { return reinterpret_cast(vec2); } inline D2D1_SIZE_F* ConvertToSizeF(Vec2* vec2) { return reinterpret_cast(vec2); } // // RectF // inline D2D1_RECT_F const& ConvertToRectF(Rect const& rect) { return reinterpret_cast(rect); } inline D2D1_RECT_F& ConvertToRectF(Rect& rect) { return reinterpret_cast(rect); } inline const D2D1_RECT_F* ConvertToRectF(const Rect* rect) { return reinterpret_cast(rect); } inline D2D1_RECT_F* ConvertToRectF(Rect* rect) { return reinterpret_cast(rect); } // // ColorF // inline D2D1_COLOR_F const& ConvertToColorF(Color const& color) { return reinterpret_cast(color); } inline D2D1_COLOR_F& ConvertToColorF(Color& color) { return reinterpret_cast(color); } inline const D2D1_COLOR_F* ConvertToColorF(const Color* color) { return reinterpret_cast(color); } inline D2D1_COLOR_F* ConvertToColorF(Color* color) { return reinterpret_cast(color); } // // MatrixF // inline D2D1_MATRIX_3X2_F const& ConvertToMatrix3x2F(Matrix3x2 const& matrix) { return reinterpret_cast(matrix); } inline D2D1_MATRIX_3X2_F& ConvertToMatrix3x2F(Matrix3x2& matrix) { return reinterpret_cast(matrix); } inline const D2D1_MATRIX_3X2_F* ConvertToMatrix3x2F(const Matrix3x2* matrix) { return reinterpret_cast(matrix); } inline D2D1_MATRIX_3X2_F* ConvertToMatrix3x2F(Matrix3x2* matrix) { return reinterpret_cast(matrix); } // Converts a length in device-independent pixels (DIPs) to a length in physical pixels. inline Float32 ConvertDipsToPixels(Float32 dips, Float32 dpi) { static const Float32 dips_per_inch = 96.0f; return math::Floor(dips * dpi / dips_per_inch + 0.5f); // Round to nearest integer. } } } namespace kiwano { MIDL_INTERFACE("5706684a-bf6d-4b03-b627-094758a33032") KGE_API ID2DDeviceResources : public IUnknown { public: static HRESULT Create(ID2DDeviceResources** device_resources); virtual HRESULT CreateBitmapConverter( _Out_ ComPtr& converter, _In_opt_ ComPtr source, _In_ REFWICPixelFormatGUID format, WICBitmapDitherType dither, _In_opt_ ComPtr palette, double alpha_threshold_percent, WICBitmapPaletteType palette_translate ) = 0; virtual HRESULT CreateBitmapFromConverter( _Out_ ComPtr& bitmap, _In_opt_ const D2D1_BITMAP_PROPERTIES* properties, _In_ ComPtr converter ) = 0; virtual HRESULT CreateBitmapDecoderFromFile( _Out_ ComPtr& decoder, const String& file_path ) = 0; virtual HRESULT CreateBitmapDecoderFromResource( _Out_ ComPtr& decoder, const Resource& resource ) = 0; virtual HRESULT CreateTextFormat( _Out_ ComPtr& text_format, _In_ Font const& font ) const = 0; virtual HRESULT CreateTextLayout( _Out_ ComPtr& text_layout, _In_ String const& text, _In_ ComPtr const& text_format ) const = 0; virtual HRESULT SetD2DDevice( _In_ ComPtr const& device ) = 0; virtual void SetTargetBitmap( _In_ ComPtr const& target ) = 0; virtual void DiscardResources() = 0; inline ID2D1Factory1* GetFactory() const { KGE_ASSERT(factory_); return factory_.get(); } inline IWICImagingFactory* GetWICImagingFactory() const { KGE_ASSERT(imaging_factory_); return imaging_factory_.get(); } inline IDWriteFactory* GetDWriteFactory() const { KGE_ASSERT(dwrite_factory_); return dwrite_factory_.get(); } inline ID2D1Device* GetDevice() const { KGE_ASSERT(device_); return device_.get(); } inline ID2D1DeviceContext* GetDeviceContext() const { KGE_ASSERT(device_context_); return device_context_.get(); } inline ID2D1Bitmap1* GetTargetBitmap() const { KGE_ASSERT(target_bitmap_); return target_bitmap_.get(); } inline ID2D1StrokeStyle* GetMiterStrokeStyle() const { KGE_ASSERT(miter_stroke_style_); return miter_stroke_style_.get(); } inline ID2D1StrokeStyle* GetBevelStrokeStyle() const { KGE_ASSERT(bevel_stroke_style_); return bevel_stroke_style_.get(); } inline ID2D1StrokeStyle* GetRoundStrokeStyle() const { KGE_ASSERT(round_stroke_style_); return round_stroke_style_.get(); } protected: ComPtr factory_; ComPtr device_; ComPtr device_context_; ComPtr target_bitmap_; ComPtr imaging_factory_; ComPtr dwrite_factory_; ComPtr miter_stroke_style_; ComPtr bevel_stroke_style_; ComPtr round_stroke_style_; }; }