Update RenderTarget

This commit is contained in:
Nomango 2019-12-27 10:51:34 +08:00
parent 8629d65797
commit ab50ec78b6
21 changed files with 268 additions and 238 deletions

View File

@ -40,12 +40,12 @@ namespace kiwano
void Canvas::BeginDraw() void Canvas::BeginDraw()
{ {
rt_.BeginDraw(); rt_->BeginDraw();
} }
void Canvas::EndDraw() void Canvas::EndDraw()
{ {
rt_.EndDraw(); rt_->EndDraw();
cache_expired_ = true; cache_expired_ = true;
} }
@ -89,7 +89,7 @@ namespace kiwano
void Canvas::SetBrushOpacity(float opacity) void Canvas::SetBrushOpacity(float opacity)
{ {
rt_.SetOpacity(opacity); rt_->SetOpacity(opacity);
} }
Color Canvas::GetStrokeColor() const Color Canvas::GetStrokeColor() const
@ -109,43 +109,43 @@ namespace kiwano
float Canvas::GetBrushOpacity() const float Canvas::GetBrushOpacity() const
{ {
return rt_.GetOpacity(); return rt_->GetOpacity();
} }
void Canvas::SetBrushTransform(Transform const& transform) void Canvas::SetBrushTransform(Transform const& transform)
{ {
rt_.SetTransform(transform.ToMatrix()); rt_->SetTransform(transform.ToMatrix());
} }
void Canvas::SetBrushTransform(Matrix3x2 const & transform) void Canvas::SetBrushTransform(Matrix3x2 const & transform)
{ {
rt_.SetTransform(transform); rt_->SetTransform(transform);
} }
void Canvas::PushLayerArea(LayerArea& area) void Canvas::PushLayerArea(LayerArea& area)
{ {
rt_.PushLayer(area); rt_->PushLayer(area);
} }
void Canvas::PopLayerArea() void Canvas::PopLayerArea()
{ {
rt_.PopLayer(); rt_->PopLayer();
} }
void Canvas::PushClipRect(Rect const& clip_rect) void Canvas::PushClipRect(Rect const& clip_rect)
{ {
rt_.PushClipRect(clip_rect); rt_->PushClipRect(clip_rect);
} }
void Canvas::PopClipRect() void Canvas::PopClipRect()
{ {
rt_.PopClipRect(); rt_->PopClipRect();
} }
void Canvas::DrawLine(Point const& begin, Point const& end) void Canvas::DrawLine(Point const& begin, Point const& end)
{ {
rt_.SetDefaultBrushColor(stroke_color_); rt_->SetDefaultBrushColor(stroke_color_);
rt_.DrawLine( rt_->DrawLine(
begin, begin,
end, end,
stroke_width_, stroke_width_,
@ -156,8 +156,8 @@ namespace kiwano
void Canvas::DrawCircle(Point const& center, float radius) void Canvas::DrawCircle(Point const& center, float radius)
{ {
rt_.SetDefaultBrushColor(stroke_color_); rt_->SetDefaultBrushColor(stroke_color_);
rt_.DrawEllipse( rt_->DrawEllipse(
center, center,
Vec2(radius, radius), Vec2(radius, radius),
stroke_width_, stroke_width_,
@ -168,8 +168,8 @@ namespace kiwano
void Canvas::DrawEllipse(Point const& center, Vec2 const& radius) void Canvas::DrawEllipse(Point const& center, Vec2 const& radius)
{ {
rt_.SetDefaultBrushColor(stroke_color_); rt_->SetDefaultBrushColor(stroke_color_);
rt_.DrawEllipse( rt_->DrawEllipse(
center, center,
radius, radius,
stroke_width_, stroke_width_,
@ -180,8 +180,8 @@ namespace kiwano
void Canvas::DrawRect(Rect const& rect) void Canvas::DrawRect(Rect const& rect)
{ {
rt_.SetDefaultBrushColor(stroke_color_); rt_->SetDefaultBrushColor(stroke_color_);
rt_.DrawRectangle( rt_->DrawRectangle(
rect, rect,
stroke_width_, stroke_width_,
stroke_style_ stroke_style_
@ -191,8 +191,8 @@ namespace kiwano
void Canvas::DrawRoundedRect(Rect const& rect, Vec2 const& radius) void Canvas::DrawRoundedRect(Rect const& rect, Vec2 const& radius)
{ {
rt_.SetDefaultBrushColor(stroke_color_); rt_->SetDefaultBrushColor(stroke_color_);
rt_.DrawRoundedRectangle( rt_->DrawRoundedRectangle(
rect, rect,
radius, radius,
stroke_width_, stroke_width_,
@ -203,8 +203,8 @@ namespace kiwano
void Canvas::FillCircle(Point const& center, float radius) void Canvas::FillCircle(Point const& center, float radius)
{ {
rt_.SetDefaultBrushColor(fill_color_); rt_->SetDefaultBrushColor(fill_color_);
rt_.FillEllipse( rt_->FillEllipse(
center, center,
Vec2(radius, radius) Vec2(radius, radius)
); );
@ -213,8 +213,8 @@ namespace kiwano
void Canvas::FillEllipse(Point const& center, Vec2 const& radius) void Canvas::FillEllipse(Point const& center, Vec2 const& radius)
{ {
rt_.SetDefaultBrushColor(fill_color_); rt_->SetDefaultBrushColor(fill_color_);
rt_.FillEllipse( rt_->FillEllipse(
center, center,
radius radius
); );
@ -223,8 +223,8 @@ namespace kiwano
void Canvas::FillRect(Rect const& rect) void Canvas::FillRect(Rect const& rect)
{ {
rt_.SetDefaultBrushColor(fill_color_); rt_->SetDefaultBrushColor(fill_color_);
rt_.FillRectangle( rt_->FillRectangle(
rect rect
); );
cache_expired_ = true; cache_expired_ = true;
@ -232,8 +232,8 @@ namespace kiwano
void Canvas::FillRoundedRect(Rect const& rect, Vec2 const& radius) void Canvas::FillRoundedRect(Rect const& rect, Vec2 const& radius)
{ {
rt_.SetDefaultBrushColor(fill_color_); rt_->SetDefaultBrushColor(fill_color_);
rt_.FillRoundedRectangle( rt_->FillRoundedRectangle(
rect, rect,
radius radius
); );
@ -244,7 +244,7 @@ namespace kiwano
{ {
if (texture) if (texture)
{ {
rt_.DrawTexture(*texture, src_rect, dest_rect); rt_->DrawTexture(*texture, src_rect, dest_rect);
cache_expired_ = true; cache_expired_ = true;
} }
} }
@ -262,7 +262,7 @@ namespace kiwano
void Canvas::DrawTextLayout(TextLayout const& layout, Point const& point) void Canvas::DrawTextLayout(TextLayout const& layout, Point const& point)
{ {
rt_.DrawTextLayout(layout, point); rt_->DrawTextLayout(layout, point);
} }
void Canvas::BeginPath(Point const& begin_pos) void Canvas::BeginPath(Point const& begin_pos)
@ -297,8 +297,8 @@ namespace kiwano
void Canvas::StrokePath() void Canvas::StrokePath()
{ {
rt_.SetDefaultBrushColor(stroke_color_); rt_->SetDefaultBrushColor(stroke_color_);
rt_.DrawGeometry( rt_->DrawGeometry(
geo_sink_.GetGeometry(), geo_sink_.GetGeometry(),
stroke_width_, stroke_width_,
stroke_style_ stroke_style_
@ -308,8 +308,8 @@ namespace kiwano
void Canvas::FillPath() void Canvas::FillPath()
{ {
rt_.SetDefaultBrushColor(fill_color_); rt_->SetDefaultBrushColor(fill_color_);
rt_.FillGeometry( rt_->FillGeometry(
geo_sink_.GetGeometry() geo_sink_.GetGeometry()
); );
cache_expired_ = true; cache_expired_ = true;
@ -317,13 +317,13 @@ namespace kiwano
void Canvas::Clear() void Canvas::Clear()
{ {
rt_.Clear(); rt_->Clear();
cache_expired_ = true; cache_expired_ = true;
} }
void Canvas::Clear(Color const& clear_color) void Canvas::Clear(Color const& clear_color)
{ {
rt_.Clear(clear_color); rt_->Clear(clear_color);
cache_expired_ = true; cache_expired_ = true;
} }
@ -337,7 +337,7 @@ namespace kiwano
{ {
if (cache_expired_) if (cache_expired_)
{ {
texture_cached_ = rt_.GetOutput(); texture_cached_ = rt_->GetOutput();
cache_expired_ = false; cache_expired_ = false;
} }
} }

View File

@ -262,16 +262,16 @@ namespace kiwano
void UpdateCache() const; void UpdateCache() const;
private: private:
float stroke_width_; float stroke_width_;
Color fill_color_; Color fill_color_;
Color stroke_color_; Color stroke_color_;
TextStyle text_style_; TextStyle text_style_;
StrokeStyle stroke_style_; StrokeStyle stroke_style_;
GeometrySink geo_sink_; GeometrySink geo_sink_;
TextureRenderTargetPtr rt_;
mutable bool cache_expired_; mutable bool cache_expired_;
mutable TexturePtr texture_cached_; mutable TexturePtr texture_cached_;
mutable TextureRenderTarget rt_;
}; };
/** @} */ /** @} */

View File

@ -73,7 +73,7 @@ namespace kiwano
SetSize(Size{ static_cast<float>(gif_->GetWidthInPixels()), static_cast<float>(gif_->GetHeightInPixels()) }); SetSize(Size{ static_cast<float>(gif_->GetWidthInPixels()), static_cast<float>(gif_->GetHeightInPixels()) });
if (!frame_rt_.IsValid()) if (!frame_rt_)
{ {
Renderer::instance().CreateTextureRenderTarget(frame_rt_); Renderer::instance().CreateTextureRenderTarget(frame_rt_);
} }
@ -129,9 +129,10 @@ namespace kiwano
void GifSprite::ComposeNextFrame() void GifSprite::ComposeNextFrame()
{ {
KGE_ASSERT(gif_ && gif_->IsValid()); KGE_ASSERT(frame_rt_);
KGE_ASSERT(gif_);
if (frame_rt_.IsValid()) if (frame_rt_->IsValid())
{ {
do do
{ {
@ -170,6 +171,7 @@ namespace kiwano
void GifSprite::OverlayNextFrame() void GifSprite::OverlayNextFrame()
{ {
KGE_ASSERT(frame_rt_);
KGE_ASSERT(gif_ && gif_->IsValid()); KGE_ASSERT(gif_ && gif_->IsValid());
frame_ = gif_->GetFrame(next_index_); frame_ = gif_->GetFrame(next_index_);
@ -179,9 +181,9 @@ namespace kiwano
SaveComposedFrame(); SaveComposedFrame();
} }
if (frame_rt_.IsValid()) if (frame_rt_->IsValid())
{ {
frame_rt_.BeginDraw(); frame_rt_->BeginDraw();
if (next_index_ == 0) if (next_index_ == 0)
{ {
@ -190,12 +192,12 @@ namespace kiwano
if (frame_.raw) if (frame_.raw)
{ {
frame_rt_.DrawTexture(*frame_.raw, nullptr, &frame_.rect); frame_rt_->DrawTexture(*frame_.raw, nullptr, &frame_.rect);
} }
frame_rt_.EndDraw(); frame_rt_->EndDraw();
frame_to_render_ = frame_rt_.GetOutput(); frame_to_render_ = frame_rt_->GetOutput();
if (frame_to_render_) if (frame_to_render_)
{ {
next_index_ = (++next_index_) % gif_->GetFramesCount(); next_index_ = (++next_index_) % gif_->GetFramesCount();
@ -215,7 +217,8 @@ namespace kiwano
void GifSprite::SaveComposedFrame() void GifSprite::SaveComposedFrame()
{ {
TexturePtr frame_to_be_saved = frame_rt_.GetOutput(); KGE_ASSERT(frame_rt_);
TexturePtr frame_to_be_saved = frame_rt_->GetOutput();
HRESULT hr = frame_to_be_saved ? S_OK : E_FAIL; HRESULT hr = frame_to_be_saved ? S_OK : E_FAIL;
@ -223,17 +226,11 @@ namespace kiwano
{ {
if (!saved_frame_) if (!saved_frame_)
{ {
auto size = frame_to_be_saved->GetSizeInPixels(); saved_frame_ = new Texture;
auto prop = D2D1::BitmapProperties(frame_to_be_saved->GetPixelFormat()); frame_rt_->CreateTexture(*saved_frame_, frame_to_be_saved->GetSizeInPixels(), frame_to_be_saved->GetPixelFormat());
ComPtr<ID2D1Bitmap> saved_bitmap;
hr = frame_rt_.GetRenderTarget()->CreateBitmap(D2D1::SizeU(size.x, size.y), prop, &saved_bitmap);
if (SUCCEEDED(hr))
{
saved_frame_->SetBitmap(saved_bitmap);
}
} }
hr = saved_frame_ ? S_OK : E_FAIL;
} }
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
@ -246,11 +243,12 @@ namespace kiwano
void GifSprite::RestoreSavedFrame() void GifSprite::RestoreSavedFrame()
{ {
KGE_ASSERT(frame_rt_);
HRESULT hr = saved_frame_ ? S_OK : E_FAIL; HRESULT hr = saved_frame_ ? S_OK : E_FAIL;
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
TexturePtr frame_to_copy_to = frame_rt_.GetOutput(); TexturePtr frame_to_copy_to = frame_rt_->GetOutput();
hr = frame_to_copy_to ? S_OK : E_FAIL; hr = frame_to_copy_to ? S_OK : E_FAIL;
@ -265,13 +263,14 @@ namespace kiwano
void GifSprite::ClearCurrentFrameArea() void GifSprite::ClearCurrentFrameArea()
{ {
frame_rt_.BeginDraw(); KGE_ASSERT(frame_rt_);
frame_rt_->BeginDraw();
frame_rt_.PushClipRect(frame_.rect); frame_rt_->PushClipRect(frame_.rect);
frame_rt_.Clear(); frame_rt_->Clear();
frame_rt_.PopClipRect(); frame_rt_->PopClipRect();
return frame_rt_.EndDraw(); return frame_rt_->EndDraw();
} }
} }

View File

@ -151,18 +151,18 @@ namespace kiwano
void ClearCurrentFrameArea(); void ClearCurrentFrameArea();
private: private:
bool animating_; bool animating_;
int total_loop_count_; int total_loop_count_;
int loop_count_; int loop_count_;
size_t next_index_; size_t next_index_;
Duration frame_elapsed_; Duration frame_elapsed_;
LoopDoneCallback loop_cb_; LoopDoneCallback loop_cb_;
DoneCallback done_cb_; DoneCallback done_cb_;
GifImagePtr gif_; GifImagePtr gif_;
GifImage::Frame frame_; GifImage::Frame frame_;
TexturePtr saved_frame_; TexturePtr saved_frame_;
TexturePtr frame_to_render_; TexturePtr frame_to_render_;
TextureRenderTarget frame_rt_; TextureRenderTargetPtr frame_rt_;
}; };
/** @} */ /** @} */

View File

@ -49,7 +49,7 @@ namespace kiwano
Rect ShapeActor::GetBoundingBox() const Rect ShapeActor::GetBoundingBox() const
{ {
if (!geo_) if (!geo_.IsValid())
return Rect{}; return Rect{};
return geo_.GetBoundingBox(GetTransformMatrix()); return geo_.GetBoundingBox(GetTransformMatrix());
@ -83,7 +83,7 @@ namespace kiwano
void ShapeActor::SetGeometry(Geometry const& geometry) void ShapeActor::SetGeometry(Geometry const& geometry)
{ {
geo_ = geometry; geo_ = geometry;
if (geo_) if (geo_.IsValid())
{ {
bounds_ = geo_.GetBoundingBox(); bounds_ = geo_.GetBoundingBox();
SetSize(bounds_.GetSize()); SetSize(bounds_.GetSize());
@ -97,7 +97,7 @@ namespace kiwano
void ShapeActor::OnRender(RenderTarget* rt) void ShapeActor::OnRender(RenderTarget* rt)
{ {
if (geo_ && CheckVisibilty(rt)) if (geo_.IsValid() && CheckVisibilty(rt))
{ {
PrepareRender(rt); PrepareRender(rt);
@ -317,7 +317,7 @@ namespace kiwano
sink_.EndPath(closed); sink_.EndPath(closed);
Geometry geo = sink_.GetGeometry(); Geometry geo = sink_.GetGeometry();
if (geo) if (geo.IsValid())
{ {
SetGeometry(geo); SetGeometry(geo);
} }

View File

@ -60,7 +60,7 @@ namespace kiwano
void ActionWalk::Init(Actor* target) void ActionWalk::Init(Actor* target)
{ {
if (!path_) if (!path_.IsValid())
{ {
Done(); Done();
return; return;
@ -121,7 +121,7 @@ namespace kiwano
void ActionWalk::ClearPath() void ActionWalk::ClearPath()
{ {
path_.SetGeometry(nullptr); path_.Clear();
} }
} }

View File

@ -90,48 +90,32 @@ namespace kiwano
); );
}; };
// ť­ąĘ /**
* \~chinese
* @brief ť­Ë˘
*/
class KGE_API Brush class KGE_API Brush
{ {
public: public:
Brush(); Brush();
Brush( Brush(Color const& color);
Color const& color
);
Brush( Brush(SolidColorStyle const& style);
SolidColorStyle const& style
);
Brush( Brush(LinearGradientStyle const& style);
LinearGradientStyle const& style
);
Brush( Brush(RadialGradientStyle const& style);
RadialGradientStyle const& style
);
bool IsValid() const; bool IsValid() const;
inline void SetColor( void SetColor(Color const& color);
Color const& color
)
{
SetStyle(SolidColorStyle{ color });
}
void SetStyle( void SetStyle(SolidColorStyle const& style);
SolidColorStyle const& style
);
void SetStyle( void SetStyle(LinearGradientStyle const& style);
LinearGradientStyle const& style
);
void SetStyle( void SetStyle(RadialGradientStyle const& style);
RadialGradientStyle const& style
);
float GetOpacity() const; float GetOpacity() const;
@ -149,9 +133,7 @@ namespace kiwano
Type GetType() const { return type_; } Type GetType() const { return type_; }
public: public:
Brush( Brush(ComPtr<ID2D1Brush> brush);
ComPtr<ID2D1Brush> brush
);
void SetBrush(ComPtr<ID2D1Brush> const& brush); void SetBrush(ComPtr<ID2D1Brush> const& brush);
@ -163,4 +145,9 @@ namespace kiwano
ComPtr<ID2D1Brush> raw_; ComPtr<ID2D1Brush> raw_;
}; };
inline void Brush::SetColor(Color const& color)
{
SetStyle(SolidColorStyle{ color });
}
} }

View File

@ -27,16 +27,6 @@ namespace kiwano
{ {
} }
Font::Font(String const& font_file)
{
Load(font_file);
}
Font::Font(Resource const& font_resource)
{
Load(font_resource);
}
bool Font::Load(String const& font_file) bool Font::Load(String const& font_file)
{ {
try try

View File

@ -39,10 +39,6 @@ namespace kiwano
public: public:
Font(); Font();
Font(String const& font_file);
Font(Resource const& font_resource);
bool Load(String const& font_file); bool Load(String const& font_file);
bool Load(Resource const& font_resource); bool Load(Resource const& font_resource);

View File

@ -33,11 +33,6 @@ namespace kiwano
{ {
} }
Geometry::Geometry(ComPtr<ID2D1Geometry> geo)
: geo_(geo)
{
}
bool Geometry::IsValid() const bool Geometry::IsValid() const
{ {
return geo_ != nullptr; return geo_ != nullptr;
@ -92,6 +87,11 @@ namespace kiwano
return false; return false;
} }
void Geometry::Clear()
{
geo_.reset();
}
void Geometry::CombineWith(GeometrySink& sink, Geometry input, CombineMode mode, Matrix3x2 const& input_matrix) const void Geometry::CombineWith(GeometrySink& sink, Geometry input, CombineMode mode, Matrix3x2 const& input_matrix) const
{ {
if (geo_ && input.geo_) if (geo_ && input.geo_)
@ -277,14 +277,17 @@ namespace kiwano
{ {
EndPath(); EndPath();
} }
return Geometry(path_geo_);
Geometry geo;
geo.SetGeometry(path_geo_);
return geo;
} }
void GeometrySink::Init() void GeometrySink::Init()
{ {
if (!path_geo_) if (!path_geo_)
{ {
Renderer::instance().CreatePathGeometrySink(*this); Renderer::instance().CreateGeometrySink(*this);
} }
} }

View File

@ -23,26 +23,20 @@
namespace kiwano namespace kiwano
{ {
class RenderTarget;
class Renderer;
class GeometrySink; class GeometrySink;
// 섯부竟 // 섯부竟
class KGE_API Geometry class KGE_API Geometry
{ {
public: friend class RenderTarget;
// 几何体组合模式 friend class Renderer;
enum class CombineMode friend class GeometrySink;
{
Union, /* 并集 (A + B) */
Intersect, /* 交集 (A + B) */
Xor, /* 对称差集 ((A - B) + (B - A)) */
Exclude /* 差集 (A - B) */
};
public: public:
Geometry(); Geometry();
Geometry(ComPtr<ID2D1Geometry> geo);
bool IsValid() const; bool IsValid() const;
// 삿혤棍학관鍋분 // 삿혤棍학관鍋분
@ -72,6 +66,19 @@ namespace kiwano
Vec2& tangent Vec2& tangent
) const; ) const;
// 헌뇜近榴
void Clear();
public:
// 섯부竟莉북친駕
enum class CombineMode
{
Union, /* 깻섞 (A + B) */
Intersect, /* 슥섞 (A + B) */
Xor, /* 뚤냔뀌섞 ((A - B) + (B - A)) */
Exclude /* 뀌섞 (A - B) */
};
// 莉북섯부竟 // 莉북섯부竟
void CombineWith( void CombineWith(
GeometrySink& sink, GeometrySink& sink,
@ -87,6 +94,7 @@ namespace kiwano
Matrix3x2 const& input_matrix = Matrix3x2() Matrix3x2 const& input_matrix = Matrix3x2()
) const; ) const;
public:
// 눼쉔殮窟 // 눼쉔殮窟
static Geometry CreateLine( static Geometry CreateLine(
Point const& begin, Point const& begin,
@ -116,13 +124,11 @@ namespace kiwano
Vec2 const& radius Vec2 const& radius
); );
public: private:
inline ComPtr<ID2D1Geometry> GetGeometry() const { return geo_; } inline ComPtr<ID2D1Geometry> GetGeometry() const { return geo_; }
inline void SetGeometry(ComPtr<ID2D1Geometry> geometry) { geo_ = geometry; } inline void SetGeometry(ComPtr<ID2D1Geometry> geometry) { geo_ = geometry; }
inline operator bool() const { return IsValid(); }
private: private:
ComPtr<ID2D1Geometry> geo_; ComPtr<ID2D1Geometry> geo_;
}; };
@ -132,8 +138,12 @@ namespace kiwano
class KGE_API GeometrySink class KGE_API GeometrySink
: protected Noncopyable : protected Noncopyable
{ {
friend class Geometry;
friend class Renderer;
public: public:
GeometrySink(); GeometrySink();
~GeometrySink(); ~GeometrySink();
// 역迦警속쨌쓺 // 역迦警속쨌쓺
@ -187,7 +197,7 @@ namespace kiwano
// 밑균직 // 밑균직
void Close(); void Close();
public: private:
inline ComPtr<ID2D1PathGeometry> GetPathGeometry() const { return path_geo_; } inline ComPtr<ID2D1PathGeometry> GetPathGeometry() const { return path_geo_; }
inline void SetPathGeometry(ComPtr<ID2D1PathGeometry> path) { path_geo_ = path; } inline void SetPathGeometry(ComPtr<ID2D1PathGeometry> path) { path_geo_ = path; }

View File

@ -31,17 +31,6 @@ namespace kiwano
{ {
} }
GifImage::GifImage(String const& file_path)
{
Load(file_path);
}
GifImage::GifImage(Resource const& res)
: GifImage()
{
Load(res);
}
bool GifImage::Load(String const& file_path) bool GifImage::Load(String const& file_path)
{ {
Renderer::instance().CreateGifImage(*this, file_path); Renderer::instance().CreateGifImage(*this, file_path);

View File

@ -24,19 +24,19 @@
namespace kiwano namespace kiwano
{ {
class Renderer;
KGE_DECLARE_SMART_PTR(GifImage); KGE_DECLARE_SMART_PTR(GifImage);
// GIF ͼÏñ // GIF ͼÏñ
class KGE_API GifImage class KGE_API GifImage
: public ObjectBase : public ObjectBase
{ {
friend class Renderer;
public: public:
GifImage(); GifImage();
GifImage(String const& file_path);
GifImage(Resource const& res);
bool Load(String const& file_path); bool Load(String const& file_path);
bool Load(Resource const& res); bool Load(Resource const& res);
@ -70,11 +70,11 @@ namespace kiwano
Frame GetFrame(uint32_t index); Frame GetFrame(uint32_t index);
private:
ComPtr<IWICBitmapDecoder> GetDecoder() const; ComPtr<IWICBitmapDecoder> GetDecoder() const;
void SetDecoder(ComPtr<IWICBitmapDecoder> decoder); void SetDecoder(ComPtr<IWICBitmapDecoder> decoder);
private:
HRESULT GetGlobalMetadata(); HRESULT GetGlobalMetadata();
private: private:

View File

@ -23,9 +23,13 @@
namespace kiwano namespace kiwano
{ {
class RenderTarget;
// 暠꿔 // 暠꿔
class KGE_API LayerArea class KGE_API LayerArea
{ {
friend class RenderTarget;
public: public:
LayerArea(); LayerArea();
@ -53,7 +57,7 @@ namespace kiwano
// <20>零섯부촁꿔긴뻣 // <20>零섯부촁꿔긴뻣
inline void SetMaskTransform(Matrix3x2 const& matrix) { mask_transform_ = matrix; } inline void SetMaskTransform(Matrix3x2 const& matrix) { mask_transform_ = matrix; }
public: private:
inline ComPtr<ID2D1Layer> GetLayer() const { return layer_; } inline ComPtr<ID2D1Layer> GetLayer() const { return layer_; }
inline void SetLayer(ComPtr<ID2D1Layer> layer) { layer_ = layer; } inline void SetLayer(ComPtr<ID2D1Layer> layer) { layer_ = layer; }

View File

@ -397,6 +397,29 @@ namespace kiwano
ThrowIfFailed(hr); ThrowIfFailed(hr);
} }
void RenderTarget::CreateTexture(Texture& texture, math::Vec2T<uint32_t> size, D2D1_PIXEL_FORMAT format)
{
HRESULT hr = S_OK;
if (!render_target_)
{
hr = E_UNEXPECTED;
}
if (SUCCEEDED(hr))
{
ComPtr<ID2D1Bitmap> saved_bitmap;
hr = render_target_->CreateBitmap(D2D1::SizeU(size.x, size.y), D2D1::BitmapProperties(format), &saved_bitmap);
if (SUCCEEDED(hr))
{
texture.SetBitmap(saved_bitmap);
}
}
ThrowIfFailed(hr);
}
void RenderTarget::CreateLayer(LayerArea& layer) void RenderTarget::CreateLayer(LayerArea& layer)
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
@ -714,26 +737,22 @@ namespace kiwano
{ {
} }
TexturePtr TextureRenderTarget::GetOutput() TexturePtr TextureRenderTarget::GetOutput() const
{ {
HRESULT hr = E_FAIL; HRESULT hr = E_FAIL;
TexturePtr output; TexturePtr output;
if (GetRenderTarget()) if (bitmap_rt_)
{ {
ComPtr<ID2D1BitmapRenderTarget> bitmap_rt; ComPtr<ID2D1BitmapRenderTarget> bitmap_rt = bitmap_rt_;
hr = GetRenderTarget()->QueryInterface<ID2D1BitmapRenderTarget>(&bitmap_rt); ComPtr<ID2D1Bitmap> bitmap;
hr = bitmap_rt->GetBitmap(&bitmap);
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
ComPtr<ID2D1Bitmap> bitmap; output = new Texture;
hr = bitmap_rt->GetBitmap(&bitmap); output->SetBitmap(bitmap);
if (SUCCEEDED(hr))
{
output = new Texture;
output->SetBitmap(bitmap);
}
} }
} }

View File

@ -20,6 +20,7 @@
#pragma once #pragma once
#include <kiwano/core/time.h> #include <kiwano/core/time.h>
#include <kiwano/core/ObjectBase.h>
#include <kiwano/core/win32/ComPtr.hpp> #include <kiwano/core/win32/ComPtr.hpp>
#include <kiwano/renderer/Brush.h> #include <kiwano/renderer/Brush.h>
#include <kiwano/renderer/Texture.h> #include <kiwano/renderer/Texture.h>
@ -30,6 +31,11 @@
namespace kiwano namespace kiwano
{ {
class Renderer;
KGE_DECLARE_SMART_PTR(RenderTarget);
KGE_DECLARE_SMART_PTR(TextureRenderTarget);
// 文字抗锯齿模式 // 文字抗锯齿模式
enum class TextAntialiasMode enum class TextAntialiasMode
{ {
@ -42,7 +48,7 @@ namespace kiwano
// 渲染目标 // 渲染目标
class KGE_API RenderTarget class KGE_API RenderTarget
: public Noncopyable : public ObjectBase
{ {
public: public:
bool IsValid() const; bool IsValid() const;
@ -51,10 +57,6 @@ namespace kiwano
void EndDraw(); void EndDraw();
void CreateLayer(
LayerArea& layer
);
void DrawGeometry( void DrawGeometry(
Geometry const& geometry, Geometry const& geometry,
float stroke_width, float stroke_width,
@ -123,6 +125,16 @@ namespace kiwano
Point const& offset = Point{} Point const& offset = Point{}
); );
void CreateTexture(
Texture& texture,
math::Vec2T<uint32_t> size,
D2D1_PIXEL_FORMAT format
);
void CreateLayer(
LayerArea& layer
);
void PushClipRect( void PushClipRect(
Rect const& clip_rect Rect const& clip_rect
); );
@ -203,19 +215,17 @@ namespace kiwano
inline Status const& GetStatus() const { return status_; } inline Status const& GetStatus() const { return status_; }
protected:
inline ComPtr<ID2D1RenderTarget> GetRenderTarget() const { KGE_ASSERT(render_target_); return render_target_; } inline ComPtr<ID2D1RenderTarget> GetRenderTarget() const { KGE_ASSERT(render_target_); return render_target_; }
inline ComPtr<ITextRenderer> GetTextRenderer() const { KGE_ASSERT(text_renderer_); return text_renderer_; } inline ComPtr<ITextRenderer> GetTextRenderer() const { KGE_ASSERT(text_renderer_); return text_renderer_; }
ComPtr<ID2D1StrokeStyle> GetStrokeStyle(StrokeStyle style); ComPtr<ID2D1StrokeStyle> GetStrokeStyle(StrokeStyle style);
public: protected:
RenderTarget(); RenderTarget();
HRESULT CreateDeviceResources( HRESULT CreateDeviceResources(ComPtr<ID2D1RenderTarget> rt, ComPtr<ID2DDeviceResources> dev_res);
ComPtr<ID2D1RenderTarget> rt,
ComPtr<ID2DDeviceResources> dev_res
);
void DiscardDeviceResources(); void DiscardDeviceResources();
@ -239,9 +249,36 @@ namespace kiwano
class KGE_API TextureRenderTarget class KGE_API TextureRenderTarget
: public RenderTarget : public RenderTarget
{ {
friend class Renderer;
public: public:
bool IsValid() const;
TexturePtr GetOutput() const;
private:
TextureRenderTarget(); TextureRenderTarget();
TexturePtr GetOutput(); ComPtr<ID2D1BitmapRenderTarget> GetBitmapRenderTarget() const;
void SetBitmapRenderTarget(ComPtr<ID2D1BitmapRenderTarget> rt);
private:
ComPtr<ID2D1BitmapRenderTarget> bitmap_rt_;
}; };
inline bool TextureRenderTarget::IsValid() const
{
return !!bitmap_rt_;
}
inline ComPtr<ID2D1BitmapRenderTarget> TextureRenderTarget::GetBitmapRenderTarget() const
{
return bitmap_rt_;
}
inline void TextureRenderTarget::SetBitmapRenderTarget(ComPtr<ID2D1BitmapRenderTarget> rt)
{
bitmap_rt_ = rt;
}
} }

View File

@ -835,7 +835,7 @@ namespace kiwano
ThrowIfFailed(hr); ThrowIfFailed(hr);
} }
void Renderer::CreatePathGeometrySink(GeometrySink& sink) void Renderer::CreateGeometrySink(GeometrySink& sink)
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
if (!d2d_res_) if (!d2d_res_)
@ -857,7 +857,7 @@ namespace kiwano
ThrowIfFailed(hr); ThrowIfFailed(hr);
} }
void Renderer::CreateTextureRenderTarget(TextureRenderTarget& render_target) void Renderer::CreateTextureRenderTarget(TextureRenderTargetPtr& render_target)
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
if (!d2d_res_) if (!d2d_res_)
@ -865,15 +865,27 @@ namespace kiwano
hr = E_UNEXPECTED; hr = E_UNEXPECTED;
} }
ComPtr<ID2D1BitmapRenderTarget> output; TextureRenderTargetPtr output;
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
hr = d2d_res_->GetDeviceContext()->CreateCompatibleRenderTarget(&output); ComPtr<ID2D1BitmapRenderTarget> bitmap_rt;
hr = d2d_res_->GetDeviceContext()->CreateCompatibleRenderTarget(&bitmap_rt);
if (SUCCEEDED(hr))
{
output = new TextureRenderTarget;
hr = output->CreateDeviceResources(bitmap_rt, d2d_res_);
}
if (SUCCEEDED(hr))
{
output->SetBitmapRenderTarget(bitmap_rt);
}
} }
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
hr = render_target.CreateDeviceResources(output, d2d_res_); render_target = output;
} }
ThrowIfFailed(hr); ThrowIfFailed(hr);

View File

@ -141,12 +141,12 @@ namespace kiwano
Vec2 const& radius Vec2 const& radius
); );
void CreatePathGeometrySink( void CreateGeometrySink(
GeometrySink& sink GeometrySink& sink
); );
void CreateTextureRenderTarget( void CreateTextureRenderTarget(
TextureRenderTarget& render_target TextureRenderTargetPtr& render_target
); );
void CreateSolidBrush( void CreateSolidBrush(

View File

@ -24,10 +24,16 @@
namespace kiwano namespace kiwano
{ {
class RenderTarget;
class Renderer;
/// \~chinese /// \~chinese
/// @brief 文本布局 /// @brief 文本布局
class KGE_API TextLayout class KGE_API TextLayout
{ {
friend class RenderTarget;
friend class Renderer;
public: public:
/// \~chinese /// \~chinese
/// @brief 构造空的文本布局 /// @brief 构造空的文本布局
@ -136,7 +142,7 @@ namespace kiwano
/// @param length 长度 /// @param length 长度
void SetStrikethrough(bool enable, uint32_t start, uint32_t length); void SetStrikethrough(bool enable, uint32_t start, uint32_t length);
public: private:
ComPtr<IDWriteTextFormat> GetTextFormat() const; ComPtr<IDWriteTextFormat> GetTextFormat() const;
void SetTextFormat(ComPtr<IDWriteTextFormat> format); void SetTextFormat(ComPtr<IDWriteTextFormat> format);

View File

@ -31,24 +31,6 @@ namespace kiwano
{ {
} }
Texture::Texture(String const& file_path)
: Texture()
{
Load(file_path);
}
Texture::Texture(Resource const& res)
: Texture()
{
Load(res);
}
Texture::Texture(ComPtr<ID2D1Bitmap> const & bitmap)
: Texture()
{
SetBitmap(bitmap);
}
Texture::~Texture() Texture::~Texture()
{ {
} }

View File

@ -24,6 +24,10 @@
namespace kiwano namespace kiwano
{ {
class RenderTarget;
class TextureRenderTarget;
class Renderer;
// 插值模式 // 插值模式
// 插值模式指定了位图在缩放和旋转时像素颜色的计算方式 // 插值模式指定了位图在缩放和旋转时像素颜色的计算方式
// Linear (双线性插值): 对周围四个像素进行两次线性插值计算, 在图像放大时可能会模糊(默认) // Linear (双线性插值): 对周围四个像素进行两次线性插值计算, 在图像放大时可能会模糊(默认)
@ -40,21 +44,13 @@ namespace kiwano
class KGE_API Texture class KGE_API Texture
: public ObjectBase : public ObjectBase
{ {
friend class RenderTarget;
friend class TextureRenderTarget;
friend class Renderer;
public: public:
Texture(); Texture();
explicit Texture(
String const& file_path
);
explicit Texture(
Resource const& res
);
explicit Texture(
ComPtr<ID2D1Bitmap> const& bitmap
);
virtual ~Texture(); virtual ~Texture();
// 加载本地文件 // 加载本地文件
@ -91,6 +87,9 @@ namespace kiwano
// 获取像素插值方式 // 获取像素插值方式
InterpolationMode GetBitmapInterpolationMode() const; InterpolationMode GetBitmapInterpolationMode() const;
// »ñÈ¡ÏñËØ¸ñʽ
D2D1_PIXEL_FORMAT GetPixelFormat() const;
// 拷贝位图内存 // 拷贝位图内存
void CopyFrom(TexturePtr copy_from); void CopyFrom(TexturePtr copy_from);
@ -103,16 +102,13 @@ namespace kiwano
// 设置默认的像素插值方式 // 设置默认的像素插值方式
static void SetDefaultInterpolationMode(InterpolationMode mode); static void SetDefaultInterpolationMode(InterpolationMode mode);
public: private:
// 获取源位图 // 获取源位图
ComPtr<ID2D1Bitmap> GetBitmap() const; ComPtr<ID2D1Bitmap> GetBitmap() const;
// 设置源位图 // 设置源位图
void SetBitmap(ComPtr<ID2D1Bitmap> bitmap); void SetBitmap(ComPtr<ID2D1Bitmap> bitmap);
// »ñÈ¡ÏñËØ¸ñʽ
D2D1_PIXEL_FORMAT GetPixelFormat() const;
private: private:
ComPtr<ID2D1Bitmap> bitmap_; ComPtr<ID2D1Bitmap> bitmap_;
InterpolationMode interpolation_mode_; InterpolationMode interpolation_mode_;