| 
									
										
										
										
											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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | #include <kiwano/render/RenderContext.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-20 18:22:52 +08:00
										 |  |  | #include <kiwano/render/Renderer.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace kiwano | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-20 18:22:52 +08:00
										 |  |  | RenderContextPtr RenderContext::Create(Texture& texture) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return Renderer::GetInstance().CreateTextureRenderContext(texture, nullptr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RenderContextPtr RenderContext::Create(Texture& texture, const Size& size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return Renderer::GetInstance().CreateTextureRenderContext(texture, &size); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | RenderContext::RenderContext() | 
					
						
							|  |  |  |     : collecting_status_(false) | 
					
						
							|  |  |  |     , fast_global_transform_(true) | 
					
						
							|  |  |  |     , brush_opacity_(1.0f) | 
					
						
							|  |  |  |     , antialias_(true) | 
					
						
							|  |  |  |     , text_antialias_(TextAntialiasMode::GrayScale) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RenderContext::BeginDraw() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (collecting_status_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         status_.start      = Time::Now(); | 
					
						
							|  |  |  |         status_.primitives = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RenderContext::EndDraw() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (collecting_status_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         status_.duration = Time::Now() - status_.start; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RenderContext::SetGlobalTransform(const Matrix3x2* matrix) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (matrix) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         global_transform_      = *matrix; | 
					
						
							|  |  |  |         fast_global_transform_ = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         fast_global_transform_ = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | void RenderContext::SetCollectingStatus(bool enable) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     collecting_status_ = enable; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | void RenderContext::IncreasePrimitivesCount(uint32_t increase) const | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     if (collecting_status_) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |         status_.primitives += increase; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | float RenderContext::GetBrushOpacity() const | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     return brush_opacity_; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | BrushPtr RenderContext::GetCurrentBrush() const | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     return current_brush_; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | const Matrix3x2& RenderContext::GetGlobalTransform() const | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     return global_transform_; | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | void RenderContext::SetBrushOpacity(float opacity) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     brush_opacity_ = opacity; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | void RenderContext::SetGlobalTransform(const Matrix3x2& matrix) | 
					
						
							| 
									
										
										
										
											2020-02-03 14:15:30 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     SetGlobalTransform(&matrix); | 
					
						
							| 
									
										
										
										
											2020-02-03 14:15:30 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  | void RenderContext::SetCurrentBrush(BrushPtr brush) | 
					
						
							| 
									
										
										
										
											2020-02-03 14:15:30 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-09 18:41:59 +08:00
										 |  |  |     current_brush_ = brush; | 
					
						
							| 
									
										
										
										
											2020-02-03 14:15:30 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  | void RenderContext::SetCurrentStrokeStyle(StrokeStylePtr stroke) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:32:08 +08:00
										 |  |  |     current_stroke_ = stroke; | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | }  // namespace kiwano
 |