Magic_Game/src/kiwano/render/RenderContext.h

365 lines
8.9 KiB
C
Raw Normal View History

2019-08-16 00:50:54 +08:00
// Copyright (c) 2016-2019 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
2019-08-16 00:50:54 +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:
2020-01-21 10:09:55 +08:00
//
2019-08-16 00:50:54 +08:00
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
2019-08-16 00:50:54 +08:00
// 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-12-27 10:51:34 +08:00
#include <kiwano/core/ObjectBase.h>
2020-01-21 10:09:55 +08:00
#include <kiwano/core/Time.h>
2020-01-17 16:55:47 +08:00
#include <kiwano/render/Brush.h>
2020-02-05 19:56:22 +08:00
#include <kiwano/render/Shape.h>
2020-01-17 16:55:47 +08:00
#include <kiwano/render/LayerArea.h>
2020-01-21 10:09:55 +08:00
#include <kiwano/render/TextLayout.h>
#include <kiwano/render/Texture.h>
2020-01-21 11:05:28 +08:00
#include <kiwano/render/DirectX/TextRenderer.h>
2019-08-16 00:50:54 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
class Renderer;
KGE_DECLARE_SMART_PTR(RenderContext);
KGE_DECLARE_SMART_PTR(TextureRenderContext);
/**
* \addtogroup Render
* @{
*/
/// \~chinese
/// @brief <20><><EFBFBD>ֿ<EFBFBD><D6BF><EFBFBD><EFBFBD><EFBFBD>ģʽ
enum class TextAntialiasMode
{
Default, ///< ϵͳĬ<CDB3><C4AC>
ClearType, ///< ClearType <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GrayScale, ///< <20>Ҷȿ<D2B6><C8BF><EFBFBD><EFBFBD><EFBFBD>
None ///< <20><><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>
};
/// \~chinese
/// @brief <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @details
/// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD>ɻ<EFBFBD><C9BB><EFBFBD>ͼԪ<CDBC>Ļ<EFBFBD><C4BB>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><D8B6><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>У<EFBFBD><D0A3><EFBFBD>ڻ<EFBFBD><DABB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
class KGE_API RenderContext : public virtual ObjectBase
{
friend class Renderer;
public:
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
bool IsValid() const;
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void BeginDraw();
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void EndDraw();
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2020-02-06 16:54:47 +08:00
void DrawShape(Shape const& shape, float stroke_width, StrokeStylePtr stroke = nullptr);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2020-02-05 19:56:22 +08:00
void FillShape(Shape const& shape);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2020-02-06 16:54:47 +08:00
void DrawLine(Point const& point1, Point const& point2, float stroke_width, StrokeStylePtr stroke = nullptr);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2020-02-06 16:54:47 +08:00
void DrawRectangle(Rect const& rect, float stroke_width, StrokeStylePtr stroke = nullptr);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void FillRectangle(Rect const& rect);
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void DrawRoundedRectangle(Rect const& rect, Vec2 const& radius, float stroke_width,
2020-02-06 16:54:47 +08:00
StrokeStylePtr stroke = nullptr);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void FillRoundedRectangle(Rect const& rect, Vec2 const& radius);
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2020-02-06 16:54:47 +08:00
void DrawEllipse(Point const& center, Vec2 const& radius, float stroke_width, StrokeStylePtr stroke = nullptr);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void FillEllipse(Point const& center, Vec2 const& radius);
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void DrawTexture(Texture const& texture, Rect const& src_rect, Rect const& dest_rect);
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void DrawTexture(Texture const& texture, const Rect* src_rect = nullptr, const Rect* dest_rect = nullptr);
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void DrawTextLayout(TextLayout const& layout, Point const& offset = Point{});
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
2020-02-07 20:50:27 +08:00
void CreateTexture(Texture& texture, math::Vec2T<uint32_t> size);
2020-01-21 10:09:55 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void PushClipRect(Rect const& clip_rect);
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void PopClipRect();
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void PushLayer(LayerArea& layer);
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void PopLayer();
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void Clear();
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void Clear(Color const& clear_color);
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
float GetBrushOpacity() const;
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
BrushPtr GetCurrentBrush() const;
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
Matrix3x2 GetGlobalTransform() const;
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void SetBrushOpacity(float opacity);
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void SetCurrentBrush(BrushPtr brush);
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void SetTransform(const Matrix3x2& matrix);
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void SetGlobalTransform(const Matrix3x2& matrix);
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
void SetGlobalTransform(const Matrix3x2* matrix);
/// \~chinese
/// @brief <20><><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>ģʽ
void SetAntialiasMode(bool enabled);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֿ<EFBFBD><D6BF><EFBFBD><EFBFBD><EFBFBD>ģʽ
void SetTextAntialiasMode(TextAntialiasMode mode);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>߽<EFBFBD><DFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bool CheckVisibility(Rect const& bounds, Matrix3x2 const& transform);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD>С
void Resize(Size const& size);
public:
/// \~chinese
/// @brief <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
struct Status
{
uint32_t primitives; ///< <20><>ȾͼԪ<CDBC><D4AA><EFBFBD><EFBFBD>
Time start; ///< <20><>Ⱦ<EFBFBD><C8BE>ʼʱ<CABC><CAB1>
Duration duration; ///< <20><>Ⱦʱ<C8BE><CAB1>
Status();
};
/// \~chinese
/// @brief <20><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD><EFBFBD>
void SetCollectingStatus(bool enable);
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
Status const& GetStatus() const;
protected:
RenderContext();
ComPtr<ID2D1RenderTarget> GetRenderTarget() const;
ComPtr<ITextRenderer> GetTextRenderer() const;
private:
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
HRESULT CreateDeviceResources(ComPtr<ID2D1Factory> factory, ComPtr<ID2D1RenderTarget> ctx);
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
void DiscardDeviceResources();
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȾͼԪ<CDBC><D4AA><EFBFBD><EFBFBD>
void IncreasePrimitivesCount(uint32_t increase = 1) const;
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
void SaveDrawingState();
/// \~chinese
/// @brief <20>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>״̬
void RestoreDrawingState();
2020-01-21 10:09:55 +08:00
private:
bool antialias_;
bool fast_global_transform_;
mutable bool collecting_status_;
float brush_opacity_;
TextAntialiasMode text_antialias_;
BrushPtr current_brush_;
Rect visible_size_;
Matrix3x2 global_transform_;
mutable Status status_;
ComPtr<ITextRenderer> text_renderer_;
ComPtr<ID2D1RenderTarget> render_target_;
ComPtr<ID2D1DrawingStateBlock> drawing_state_;
2020-01-21 10:09:55 +08:00
};
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @details <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
class KGE_API TextureRenderContext : public RenderContext
{
friend class Renderer;
public:
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ч
bool IsValid() const;
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
/// @param[out] texture <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @return <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD>
bool GetOutput(Texture& texture);
private:
TextureRenderContext();
ComPtr<ID2D1BitmapRenderTarget> GetBitmapRenderTarget() const;
void SetBitmapRenderTarget(ComPtr<ID2D1BitmapRenderTarget> ctx);
private:
ComPtr<ID2D1BitmapRenderTarget> bitmap_rt_;
};
/** @} */
inline RenderContext::Status::Status()
: primitives(0)
{
}
inline RenderContext::Status const& RenderContext::GetStatus() const
{
return status_;
}
inline ComPtr<ID2D1RenderTarget> RenderContext::GetRenderTarget() const
{
KGE_ASSERT(render_target_);
return render_target_;
2020-01-21 10:09:55 +08:00
}
inline ComPtr<ITextRenderer> RenderContext::GetTextRenderer() const
{
KGE_ASSERT(text_renderer_);
return text_renderer_;
}
inline float RenderContext::GetBrushOpacity() const
{
return brush_opacity_;
}
inline BrushPtr RenderContext::GetCurrentBrush() const
{
return current_brush_;
}
inline Matrix3x2 RenderContext::GetGlobalTransform() const
{
return global_transform_;
}
inline void RenderContext::SetBrushOpacity(float opacity)
{
brush_opacity_ = opacity;
}
inline void RenderContext::SetGlobalTransform(const Matrix3x2& matrix)
{
SetGlobalTransform(&matrix);
}
inline void RenderContext::SetCurrentBrush(BrushPtr brush)
{
current_brush_ = brush;
if (current_brush_)
{
current_brush_->SetOpacity(brush_opacity_);
}
}
inline bool TextureRenderContext::IsValid() const
{
return bitmap_rt_ != nullptr;
}
inline ComPtr<ID2D1BitmapRenderTarget> TextureRenderContext::GetBitmapRenderTarget() const
{
return bitmap_rt_;
}
inline void TextureRenderContext::SetBitmapRenderTarget(ComPtr<ID2D1BitmapRenderTarget> ctx)
{
bitmap_rt_ = ctx;
2019-08-16 00:50:54 +08:00
}
2020-01-21 10:09:55 +08:00
} // namespace kiwano