YSTest: YGDI.cpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

YGDI.cpp
浏览该文件的文档.
1 /*
2 © 2009-2015, 2019-2020 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "YSLib/Service/YModules.h"
29 #include YFM_YSLib_Service_YGDI
30 #include YFM_YSLib_Service_YBlend
31
32 using namespace ystdex;
33
34 namespace YSLib
35 {
36
37 namespace Drawing
38 {
39
40 Rect
41 operator+(const Rect& r, const Padding& m) ynothrow
42 {
43 // XXX: Conversion to 'SPos' might be implementation-defined.
44 return Rect(r.X + m.Left, r.Y + m.Top,
45 SDst(max<SPos>(0, SPos(r.Width) - m.Left - m.Right)),
46 SDst(max<SPos>(0, SPos(r.Height) - m.Top - m.Bottom)));
47 }
48
49
50 Padding
51 FetchMargin(const Rect& r, const Size& s) ynothrow
52 {
53 // XXX: Conversion to 'SPos' might be implementation-defined.
54 return Padding(r.X, SPos(s.Width) - r.X - SPos(r.Width),
55 r.Y, SPos(s.Height) - r.Y - SPos(r.Height));
56 }
57
58
59 Point
60 ClipBounds(Rect& clip, const Rect& bounds) ynothrow
61 {
62 if(!clip.IsUnstrictlyEmpty() && Clip(clip, bounds))
63 return clip.GetPoint() - bounds.GetPoint();
64 clip.GetSizeRef() = {};
65 return {};
66 }
67
68 Point
69 ClipMargin(PaintContext& pc, const Padding& m, const Size& ss) ynothrow
70 {
71 const Size& ds(pc.Target.GetSize());
72
73 if(GetHorizontalOf(m) < ds.Width && GetVerticalOf(m) < ds.Height)
74 {
75 const auto& pt(pc.Location);
76 const Point dp(max<int>(m.Left, pt.X), max<int>(m.Top, pt.Y));
77 const Point sp(dp - pt);
78 // XXX: Conversion to 'SPos' might be implementation-defined.
79 const SPos scx(min<SPos>(SPos(ss.Width), SPos(ds.Width) - m.Right
80 - dp.X) - sp.X), scy(min<SPos>(SPos(ss.Height), SPos(ds.Height)
81 - m.Bottom - dp.Y) - sp.Y);
82
83 if(scx > 0 && scy > 0)
84 {
85 pc.ClipArea &= Rect(dp, SDst(scx), SDst(scy));
86 return pc.ClipArea.GetPoint() - pt;
87 }
88 }
89 pc.ClipArea.GetSizeRef() = {};
90 return {};
91 }
92
93
94 ImplDeDtor(BasicImage)
95
96
97 CompactPixmap::CompactPixmap(ConstBitmapPtr i, SDst w, SDst h)
98 : BaseType()
99 // NOTE: The size member cannot be initialized just here, or it would be
100 // inconsistent with buffer pointer.
101 {
102 SetSize(w, h);
103 if(i)
104 CopyBitmapBuffer(GetBufferPtr(), i, GetSize());
105 }
106 CompactPixmap::CompactPixmap(unique_ptr<Pixel[]> p, const Size& s) ynothrow
107 : BaseType(std::move(p), s)
108 {}
109 CompactPixmap::CompactPixmap(const CompactPixmap& buf)
110 // XXX: $2015-03 @ %Documentation::Workflow.
111 : BaseType(), IImage()
112 {
113 SetSize(buf.GetSize());
114 if(const auto p = buf.GetBufferPtr())
115 CopyBitmapBuffer(GetBufferPtr(), p, GetSize());
116 }
117
118 void
119 CompactPixmap::SetContent(ConstBitmapPtr s, SDst w, SDst h)
120 {
121 SetSize(w, h);
122 if(YB_LIKELY(pBuffer && s))
123 CopyBitmapBuffer(pBuffer.get(), s, GetSize());
124 }
125 void
126 CompactPixmap::SetSize(const Size& s)
127 {
128 const auto area(GetAreaOf(s));
129
130 TryExpr(pBuffer.reset(YB_LIKELY(area != 0) ? new Pixel[area] : nullptr))
131 CatchThrow(std::bad_alloc&,
132 LoggedEvent("BitmapBuffer allocation failed.", Alert))
133 YAssert(bool(pBuffer) == (area != 0), "Buffer corrupted.");
134 sGraphics = s,
135 ClearImage();
136 }
137 void
138 CompactPixmap::SetSizeSwap()
139 {
140 std::swap(sGraphics.Width, sGraphics.Height);
141 ClearImage();
142 }
143
144 void
145 CompactPixmap::ClearImage() const
146 {
147 Drawing::ClearImage(GetContext());
148 }
149
150
151 CompactPixmapEx::CompactPixmapEx(ConstBitmapPtr i, SDst w, SDst h)
152 : CompactPixmapEx()
153 {
154 SetSize(w, h);
155 if(i)
156 CopyBitmapBuffer(pBuffer.get(), i, GetSize());
157 }
158 CompactPixmapEx::CompactPixmapEx(const CompactPixmapEx& buf)
159 : CompactPixmapEx()
160 {
161 SetSize(buf.GetSize());
162 if(const auto p = buf.GetBufferPtr())
163 {
164 CopyBitmapBuffer(pBuffer.get(), p, GetSize()),
165 CopyBitmapBuffer(pBufferAlpha.get(), buf.GetBufferAlphaPtr(),
166 GetSize());
167 }
168 }
169
170 void
171 CompactPixmapEx::SetSize(const Size& s)
172 {
173 const auto area(GetAreaOf(s));
174
175 try
176 {
177 unique_ptr<Pixel[]> p_new(YB_LIKELY(area != 0) ? new Pixel[area]
178 : nullptr);
179 unique_ptr<AlphaType[]> p_new_alpha(YB_LIKELY(area != 0)
180 ? new AlphaType[area] : nullptr);
181
182 yunseq(pBuffer = std::move(p_new),
183 pBufferAlpha = std::move(p_new_alpha));
184 }
185 CatchThrow(std::bad_alloc&,
186 LoggedEvent("CompactPixmapEx allocation failed.", Alert))
187 YAssert(bool(pBuffer) == (area != 0), "Buffer corrupted."),
188 YAssert(bool(pBufferAlpha) == (area != 0), "Alpha buffer corrupted.");
189 sGraphics = s,
190 ClearImage();
191 }
192
193 void
194 CompactPixmapEx::ClearImage() const
195 {
196 const auto t(size_t(GetAreaOf(sGraphics)));
197
198 ClearPixels(pBuffer.get(), t);
199 ClearPixels(pBufferAlpha.get(), t);
200 }
201
202 bool
203 CopyTo(BitmapPtr dst, const ConstGraphics& g, const Size& ds,
204 const Point& dp, const Point& sp, const Size& sc, Rotation rot)
205 {
206 if(~rot & 1 && dst && bool(g))
207 {
208 if(rot == RDeg0)
209 BlitLines<false, false>(CopyLine<true>(), dst, g.GetBufferPtr(),
210 ds, g.GetSize(), dp, sp, sc);
211 else
212 BlitLines<true, true>(CopyLine<false>(), dst, g.GetBufferPtr(),
213 ds, g.GetSize(), dp, sp, sc);
214 return true;
215 }
216 return {};
217 }
218 bool
219 CopyTo(BitmapPtr dst, const CompactPixmapEx& buf, const Size& ds,
220 const Point& dp, const Point& sp, const Size& sc, Rotation rot)
221 {
222 if(~rot & 1 && dst && bool(buf))
223 {
224 if(rot == RDeg0)
225 BlitPixels<false, false>(Shaders::BlitTransparentPoint(), dst,
226 IteratorPair(buf.GetBufferPtr(), buf.GetBufferAlphaPtr()), ds,
227 buf.GetSize(), dp, sp, sc);
228 else
229 BlitPixels<true, true>(Shaders::BlitTransparentPoint(), dst,
230 IteratorPair(buf.GetBufferPtr(), buf.GetBufferAlphaPtr()), ds,
231 buf.GetSize(), dp, sp, sc);
232 return true;
233 }
234 return {};
235 }
236
237 bool
238 BlitTo(BitmapPtr dst, const CompactPixmapEx& buf, const Size& ds,
239 const Point& dp, const Point& sp, const Size& sc, Rotation rot)
240 {
241 if(~rot & 1 && dst && bool(buf))
242 {
243 if(rot == RDeg0)
244 BlitPixels<false, false>(Shaders::BlitAlphaPoint(), dst,
245 IteratorPair(buf.GetBufferPtr(), buf.GetBufferAlphaPtr()), ds,
246 buf.GetSize(), dp, sp, sc);
247 else
248 BlitPixels<true, true>(Shaders::BlitAlphaPoint(), dst,
249 IteratorPair(buf.GetBufferPtr(), buf.GetBufferAlphaPtr()), ds,
250 buf.GetSize(), dp, sp, sc);
251 return true;
252 }
253 return {};
254 }
255
256 } // namespace Drawing;
257
258 } // namespace YSLib;
259
YSLib::Drawing::ClearImage
YF_API void ClearImage(const Graphics &)
清除图形接口上下文缓冲区。
Definition: YBlit.cpp:87
YSLib::Drawing::CompactPixmap::CompactPixmap
CompactPixmap(unique_ptr< Pixel[]>, const Size &) ynothrow
构造：使用指定位图指针和大小。
Definition: YGDI.cpp:106
YSLib::Drawing::Size::Width
SDst Width
宽和高。
Definition: YGDIBase.h:545
YSLib::Drawing::BasicImage
使用 Graphics 定义的基本图像。
Definition: YGDI.h:193
ystdex::cpp2017::swap
void swap(optional< _type > &x, optional< _type > &y)
Definition: optional.h:888
YSLib::Drawing::Size
屏幕区域大小。
Definition: YGDIBase.h:536
YSLib::Drawing::Size::Height
SDst Height
Definition: YGDIBase.h:545
YSLib::Drawing::BlitTo
dst src rot dst src rot YF_API bool BlitTo(BitmapPtr, const CompactPixmapEx &, const Size &, const Point &, const Point &, const Size &, Rotation=RDeg0)
贴图：位图缓冲区向指针指定的缓冲区以贴图算法复制。
Definition: YGDI.cpp:238
YSLib::Drawing::IteratorPair
ystdex::pair_iterator< ConstBitmapPtr, const AlphaType * > IteratorPair
Alpha 光栅化源迭代器对。
Definition: YBlit.h:560
YSLib::Drawing::GGraphics< unique_ptr< Pixel[]> >::pBuffer
PointerType pBuffer
显示缓冲区指针。
Definition: YGraphics.h:79
YSLib::Drawing::r
The boundary is out of the const Graphics const Rect const Point const Point Color c const Rect const Rect & r
Definition: YDraw.h:174
YSLib::Drawing::CompactPixmap::SetSize
void SetSize(const Size &) override
重新设置缓冲区大小。
Definition: YGDI.cpp:126
YSLib::Drawing::GGraphics::Height
const SizeType sGraphics Height
Definition: YGraphics.h:136
ImplDeDtor
#define ImplDeDtor(_t)
Definition: YBaseMacro.h:183
YSLib::Drawing::GGraphics< unique_ptr< Pixel[]> >::s
const SizeType & s
Definition: YGraphics.h:88
YSLib::Drawing::sp
dst sp
Definition: YGDI.h:442
YModules.h
YSLib 服务模块配置文件。
YSLib::Drawing::g
The boundary is out of the const Graphics & g
Definition: YDraw.h:161
YSLib::Drawing::CompactPixmap::ClearImage
virtual void ClearImage() const
清除缓冲区。
Definition: YGDI.cpp:145
ystdex::operator+
common_type_t< fixed_point< _tBase1, _vInt1, _vFrac1 >, fixed_point< _tBase2, _vInt2, _vFrac2 > > operator+(const fixed_point< _tBase1, _vInt1, _vFrac1 > &x, const fixed_point< _tBase2, _vInt2, _vFrac2 > &y)
Definition: rational.hpp:532
YSLib::Drawing::CompactPixmapEx::pBufferAlpha
unique_ptr< AlphaType[]> pBufferAlpha
Alpha 缓冲区指针。
Definition: YGDI.h:344
YSLib::Drawing::dp
dst dp
Definition: YGDI.h:442
YSLib::Drawing::CompactPixmap::SetSizeSwap
void SetSizeSwap()
交换宽和高；同时清除缓冲区。
Definition: YGDI.cpp:138
platform::Descriptions::Alert
@ Alert
Definition: YCommon.h:180
YSLib::Drawing::CompactPixmapEx
使用 8 位 Alpha 扩展的标准矩形像素图缓冲区。
Definition: YGDI.h:338
YSLib::YAssert
YAssert(!(v< Deref(a)), "Value less than lower bound found.")
YSLib::LoggedEvent
记录日志的异常事件类。
Definition: YException.h:51
YSLib::Drawing::GGraphics< unique_ptr< Pixel[]> >
YSLib::Drawing::Shaders::BlitAlphaPoint
像素计算：Alpha 混合。
Definition: YBlend.h:81
YSLib::Drawing::CopyLine< false >
Definition: YBlit.h:488
YSLib::Drawing::Padding
空白样式。
Definition: YGDI.h:47
YSLib::Drawing::Rotation
Rotation
逆时针旋转角度指示输出指向。
Definition: YGDIBase.h:1145
YSLib::Drawing::ClipBounds
Point ClipBounds(Rect &clip, const Rect &bounds) ynothrow
Definition: YGDI.cpp:60
YSLib::Drawing::GBinaryGroup< SPos >
YSLib::Drawing::ClearPixels
_tOut ClearPixels(_tOut dst, size_t n) ynothrowv
显示缓存操作：清除/以纯色像素填充。
Definition: YBlit.h:517
YSLib::i
return i
Definition: YCoreUtilities.h:177
YSLib::Drawing::GetSize
dst GetSize()
yunseq
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:1535
platform::SDst
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:44
YSLib::Drawing::CompactPixmap
标准矩形像素图缓冲区。
Definition: YGDI.h:233
CatchThrow
#define CatchThrow(_ex,...)
指定抛出指定异常作为异常处理器。
Definition: YBaseMacro.h:141
YSLib::Drawing::AlphaType
octet AlphaType
Alpha 分量类型。
Definition: YGDIBase.h:49
YSLib::Drawing::CompactPixmapEx::CompactPixmapEx
CompactPixmapEx(const CompactPixmapEx &)
Definition: YGDI.cpp:151
YSLib::Drawing::CopyLine
扫描线：按指定扫描顺序复制一行像素。
Definition: YBlit.h:444
YSLib::Drawing::Rect
屏幕标准矩形：表示屏幕矩形区域。
Definition: YGDIBase.h:788
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:143
YB_LIKELY
#define YB_LIKELY(_expr)
Definition: ydef.h:827
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:169
YSLib::Drawing::RDeg0
@ RDeg0
Definition: YGDIBase.h:1146
YSLib::Drawing::CopyBitmapBuffer
void CopyBitmapBuffer(_tOut p_dst, _tIn p_src, const Size &s)
Definition: YGraphics.h:50
YSLib::Drawing::bounds
The boundary is out of the const Rect & bounds
Definition: YDraw.h:127
YSLib::Drawing::GGraphics< unique_ptr< Pixel[]> >::sGraphics
SizeType sGraphics
图形区域大小。
Definition: YGraphics.h:81
YSLib::Drawing::CompactPixmapEx::ClearImage
void ClearImage() const override
清除缓冲区。
Definition: YGDI.cpp:194
YSLib::Text::buf
YB_ATTR_nodiscard YB_PURE const char * buf
Definition: TextFile.h:83
ystdex
<memory> 特性测试宏。
Definition: ydef.h:1262
YSLib::Drawing::w
The boundary is out of the const Rect SDst w
Definition: YDraw.h:127
YSLib::Drawing::GetVerticalOf
YB_ATTR_nodiscard YB_PURE const Padding &m ynothrowv GetVerticalOf
Definition: YGDI.h:151
YSLib::Drawing::FetchMargin
Padding FetchMargin(const Rect &r, const Size &s) ynothrow
Definition: YGDI.cpp:51
YSLib::Drawing::GetHorizontalOf
YB_ATTR_nodiscard YB_PURE GetHorizontalOf
Definition: YGDI.h:146
YSLib::Drawing::ConstBitmapPtr
const Pixel * ConstBitmapPtr
Definition: YGraphics.h:44
platform::p
return p p
Definition: YCLib/FileSystem.h:209
YSLib::Drawing::ClipMargin
YB_ATTR_nodiscard YF_API Point ClipMargin(PaintContext &, const Padding &, const Size &) ynothrow
根据指定边距和源的大小优化绘制上下文的剪切区域。
Definition: YGDI.cpp:69
platform::SPos
std::int16_t SPos
屏幕坐标度量。
Definition: Video.h:43
NPL::dst
YF_API const char src const string const char * dst
Definition: Dependency.h:96
YSLib::Drawing::BitmapPtr
Pixel * BitmapPtr
Definition: YGraphics.h:42
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:1199
TryExpr
#define TryExpr(...)
指定表达式在 try 块中求值。
Definition: YBaseMacro.h:120
YSLib::Drawing::Shaders::BlitTransparentPoint
像素迭代器透明操作。
Definition: YBlend.h:51
YSLib::Drawing::CopyTo
YF_API bool CopyTo(BitmapPtr, const ConstGraphics &, const Size &, const Point &, const Point &, const Size &, Rotation=RDeg0)
图形接口上下文向指针指定的缓冲区复制。
Definition: YGDI.cpp:203
YSLib::Drawing::CompactPixmapEx::SetSize
const CompactPixmapEx &buf AlphaType pBufferAlpha sizeof(AlphaType) *size_t(GetAreaOf(GetSize()))) using CompactPixmap void SetSize(const Size &) override
重新设置缓冲区大小。
Definition: YGDI.cpp:171