2019-04-11 14:40:54 +08:00
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
2019-03-31 01:37:06 +08:00
|
|
|
//
|
|
|
|
|
// 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
|
2019-08-18 10:23:54 +08:00
|
|
|
#include "../Font.h"
|
|
|
|
|
#include "../Color.h"
|
|
|
|
|
#include "../../math/math.h"
|
|
|
|
|
#include "../../base/Resource.h"
|
|
|
|
|
#include <dwrite.h>
|
2019-03-31 01:37:06 +08:00
|
|
|
#include <d2d1.h>
|
2019-08-18 10:23:54 +08:00
|
|
|
#include <d2d1_1.h>
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
namespace kiwano
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
namespace DX
|
|
|
|
|
{
|
|
|
|
|
template <typename T>
|
|
|
|
|
inline void SafeRelease(T*& ptr)
|
|
|
|
|
{
|
|
|
|
|
if (ptr != nullptr)
|
|
|
|
|
{
|
|
|
|
|
ptr->Release();
|
|
|
|
|
ptr = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-18 10:23:54 +08:00
|
|
|
template <typename T>
|
|
|
|
|
inline T* SafeAcquire(T* ptr)
|
|
|
|
|
{
|
|
|
|
|
if (ptr != nullptr)
|
|
|
|
|
{
|
|
|
|
|
ptr->AddRef();
|
|
|
|
|
}
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-08 13:46:14 +08:00
|
|
|
//
|
|
|
|
|
// Point2F
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
inline D2D1_POINT_2F const& ConvertToPoint2F(Vec2 const& vec2)
|
|
|
|
|
{
|
|
|
|
|
return reinterpret_cast<D2D1_POINT_2F const&>(vec2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline D2D1_POINT_2F& ConvertToPoint2F(Vec2& vec2)
|
|
|
|
|
{
|
|
|
|
|
return reinterpret_cast<D2D1_POINT_2F&>(vec2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline const D2D1_POINT_2F* ConvertToPoint2F(const Vec2* vec2)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-08 13:46:14 +08:00
|
|
|
return reinterpret_cast<const D2D1_POINT_2F*>(vec2);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-08 13:46:14 +08:00
|
|
|
inline D2D1_POINT_2F* ConvertToPoint2F(Vec2* vec2)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-08 13:46:14 +08:00
|
|
|
return reinterpret_cast<D2D1_POINT_2F*>(vec2);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-08 13:46:14 +08:00
|
|
|
//
|
|
|
|
|
// SizeF
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
inline D2D1_SIZE_F const& ConvertToSizeF(Vec2 const& vec2)
|
|
|
|
|
{
|
|
|
|
|
return reinterpret_cast<D2D1_SIZE_F const&>(vec2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline D2D1_SIZE_F& ConvertToSizeF(Vec2& vec2)
|
|
|
|
|
{
|
|
|
|
|
return reinterpret_cast<D2D1_SIZE_F&>(vec2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline const D2D1_SIZE_F* ConvertToSizeF(const Vec2* vec2)
|
|
|
|
|
{
|
|
|
|
|
return reinterpret_cast<const D2D1_SIZE_F*>(vec2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline D2D1_SIZE_F* ConvertToSizeF(Vec2* vec2)
|
|
|
|
|
{
|
|
|
|
|
return reinterpret_cast<D2D1_SIZE_F*>(vec2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2019-08-20 19:32:36 +08:00
|
|
|
// RectF
|
2019-08-08 13:46:14 +08:00
|
|
|
//
|
|
|
|
|
|
2019-08-20 19:32:36 +08:00
|
|
|
inline D2D1_RECT_F const& ConvertToRectF(Rect const& rect)
|
|
|
|
|
{
|
|
|
|
|
return reinterpret_cast<D2D1_RECT_F const&>(rect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline D2D1_RECT_F& ConvertToRectF(Rect& rect)
|
|
|
|
|
{
|
|
|
|
|
return reinterpret_cast<D2D1_RECT_F&>(rect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline const D2D1_RECT_F* ConvertToRectF(const Rect* rect)
|
|
|
|
|
{
|
|
|
|
|
return reinterpret_cast<const D2D1_RECT_F*>(rect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline D2D1_RECT_F* ConvertToRectF(Rect* rect)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-20 19:32:36 +08:00
|
|
|
return reinterpret_cast<D2D1_RECT_F*>(rect);
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-08 13:46:14 +08:00
|
|
|
//
|
2019-08-20 19:32:36 +08:00
|
|
|
// ColorF
|
2019-08-08 13:46:14 +08:00
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
inline D2D1_COLOR_F const& ConvertToColorF(Color const& color)
|
|
|
|
|
{
|
|
|
|
|
return reinterpret_cast<D2D1_COLOR_F const&>(color);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-08 13:46:14 +08:00
|
|
|
inline D2D1_COLOR_F& ConvertToColorF(Color& color)
|
|
|
|
|
{
|
|
|
|
|
return reinterpret_cast<D2D1_COLOR_F&>(color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline const D2D1_COLOR_F* ConvertToColorF(const Color* color)
|
|
|
|
|
{
|
|
|
|
|
return reinterpret_cast<const D2D1_COLOR_F*>(color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline D2D1_COLOR_F* ConvertToColorF(Color* color)
|
|
|
|
|
{
|
|
|
|
|
return reinterpret_cast<D2D1_COLOR_F*>(color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2019-08-20 19:32:36 +08:00
|
|
|
// MatrixF
|
2019-08-08 13:46:14 +08:00
|
|
|
//
|
|
|
|
|
|
2019-08-14 23:36:29 +08:00
|
|
|
inline D2D1_MATRIX_3X2_F const& ConvertToMatrix3x2F(Matrix3x2 const& matrix)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
return reinterpret_cast<D2D1_MATRIX_3X2_F const&>(matrix);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 23:36:29 +08:00
|
|
|
inline D2D1_MATRIX_3X2_F& ConvertToMatrix3x2F(Matrix3x2& matrix)
|
2019-08-08 13:46:14 +08:00
|
|
|
{
|
|
|
|
|
return reinterpret_cast<D2D1_MATRIX_3X2_F&>(matrix);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 23:36:29 +08:00
|
|
|
inline const D2D1_MATRIX_3X2_F* ConvertToMatrix3x2F(const Matrix3x2* matrix)
|
2019-07-29 14:48:17 +08:00
|
|
|
{
|
|
|
|
|
return reinterpret_cast<const D2D1_MATRIX_3X2_F*>(matrix);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 23:36:29 +08:00
|
|
|
inline D2D1_MATRIX_3X2_F* ConvertToMatrix3x2F(Matrix3x2* matrix)
|
2019-08-08 13:46:14 +08:00
|
|
|
{
|
|
|
|
|
return reinterpret_cast<D2D1_MATRIX_3X2_F*>(matrix);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-31 01:37:06 +08:00
|
|
|
// Converts a length in device-independent pixels (DIPs) to a length in physical pixels.
|
2019-08-18 22:49:44 +08:00
|
|
|
inline Float32 ConvertDipsToPixels(Float32 dips, Float32 dpi)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2019-08-18 22:49:44 +08:00
|
|
|
static const Float32 dips_per_inch = 96.0f;
|
2019-03-31 01:37:06 +08:00
|
|
|
return math::Floor(dips * dpi / dips_per_inch + 0.5f); // Round to nearest integer.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-18 10:23:54 +08:00
|
|
|
|
|
|
|
|
namespace kiwano
|
|
|
|
|
{
|
|
|
|
|
MIDL_INTERFACE("5706684a-bf6d-4b03-b627-094758a33032")
|
|
|
|
|
KGE_API ID2DDeviceResources
|
|
|
|
|
: public IUnknown
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static HRESULT Create(ID2DDeviceResources** device_resources);
|
|
|
|
|
|
2019-08-23 13:00:43 +08:00
|
|
|
virtual HRESULT CreateBitmapConverter(
|
|
|
|
|
_Out_ ComPtr<IWICFormatConverter>& converter,
|
|
|
|
|
_In_opt_ ComPtr<IWICBitmapSource> source,
|
|
|
|
|
_In_ REFWICPixelFormatGUID format,
|
|
|
|
|
WICBitmapDitherType dither,
|
|
|
|
|
_In_opt_ ComPtr<IWICPalette> palette,
|
|
|
|
|
double alpha_threshold_percent,
|
|
|
|
|
WICBitmapPaletteType palette_translate
|
2019-08-18 10:23:54 +08:00
|
|
|
) = 0;
|
|
|
|
|
|
2019-08-23 13:00:43 +08:00
|
|
|
virtual HRESULT CreateBitmapFromConverter(
|
2019-08-18 10:23:54 +08:00
|
|
|
_Out_ ComPtr<ID2D1Bitmap>& bitmap,
|
2019-08-23 13:00:43 +08:00
|
|
|
_In_opt_ const D2D1_BITMAP_PROPERTIES* properties,
|
|
|
|
|
_In_ ComPtr<IWICFormatConverter> converter
|
|
|
|
|
) = 0;
|
|
|
|
|
|
|
|
|
|
virtual HRESULT CreateBitmapDecoderFromFile(
|
|
|
|
|
_Out_ ComPtr<IWICBitmapDecoder>& decoder,
|
|
|
|
|
const String& file_path
|
|
|
|
|
) = 0;
|
|
|
|
|
|
|
|
|
|
virtual HRESULT CreateBitmapDecoderFromResource(
|
|
|
|
|
_Out_ ComPtr<IWICBitmapDecoder>& decoder,
|
|
|
|
|
const Resource& resource
|
2019-08-18 10:23:54 +08:00
|
|
|
) = 0;
|
|
|
|
|
|
|
|
|
|
virtual HRESULT CreateTextFormat(
|
|
|
|
|
_Out_ ComPtr<IDWriteTextFormat>& text_format,
|
|
|
|
|
_In_ Font const& font
|
|
|
|
|
) const = 0;
|
|
|
|
|
|
|
|
|
|
virtual HRESULT CreateTextLayout(
|
|
|
|
|
_Out_ ComPtr<IDWriteTextLayout>& text_layout,
|
|
|
|
|
_In_ String const& text,
|
|
|
|
|
_In_ ComPtr<IDWriteTextFormat> const& text_format
|
|
|
|
|
) const = 0;
|
|
|
|
|
|
|
|
|
|
virtual HRESULT SetD2DDevice(
|
|
|
|
|
_In_ ComPtr<ID2D1Device> const& device
|
|
|
|
|
) = 0;
|
|
|
|
|
|
|
|
|
|
virtual void SetTargetBitmap(
|
|
|
|
|
_In_ ComPtr<ID2D1Bitmap1> 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(); }
|
|
|
|
|
|
2019-08-27 08:28:14 +08:00
|
|
|
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(); }
|
2019-08-21 16:33:41 +08:00
|
|
|
|
2019-08-18 10:23:54 +08:00
|
|
|
protected:
|
|
|
|
|
ComPtr<ID2D1Factory1> factory_;
|
|
|
|
|
ComPtr<ID2D1Device> device_;
|
|
|
|
|
ComPtr<ID2D1DeviceContext> device_context_;
|
|
|
|
|
ComPtr<ID2D1Bitmap1> target_bitmap_;
|
|
|
|
|
|
|
|
|
|
ComPtr<IWICImagingFactory> imaging_factory_;
|
|
|
|
|
ComPtr<IDWriteFactory> dwrite_factory_;
|
2019-08-21 16:33:41 +08:00
|
|
|
|
2019-08-27 08:28:14 +08:00
|
|
|
ComPtr<ID2D1StrokeStyle> miter_stroke_style_;
|
|
|
|
|
ComPtr<ID2D1StrokeStyle> bevel_stroke_style_;
|
|
|
|
|
ComPtr<ID2D1StrokeStyle> round_stroke_style_;
|
2019-08-18 10:23:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|