1 /**
2  * ae.ui.video.software.common
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <ae@cy.md>
12  */
13 
14 module ae.ui.video.software.common;
15 
16 
17 /// Mixin implementing Renderer methods using Canvas.
18 /// Mixin context: "bitmap" must return a Canvas-like object.
19 mixin template SoftwareRenderer()
20 {
21 	import gd = ae.utils.graphics.draw;
22 
23 	override void putPixel(int x, int y, COLOR color)
24 	{
25 		gd.safePut(bitmap, x, y, color);
26 	} ///
27 
28 	override void putPixels(Pixel[] pixels)
29 	{
30 		foreach (ref pixel; pixels)
31 			gd.safePut(bitmap, pixel.x, pixel.y, pixel.color);
32 	} ///
33 
34 	override void line(float x0, float y0, float x1, float y1, COLOR color)
35 	{
36 		gd.aaLine(bitmap, x0, y0, x1, y1, color);
37 	} ///
38 
39 	override void vline(int x, int y0, int y1, COLOR color)
40 	{
41 		gd.vline(bitmap, x, y0, y1, color);
42 	} ///
43 
44 	override void hline(int x0, int x1, int y, COLOR color)
45 	{
46 		gd.hline(bitmap, x0, x1, y, color);
47 	} ///
48 
49 	override void fillRect(int x0, int y0, int x1, int y1, COLOR color)
50 	{
51 		gd.fillRect(bitmap, x0, y0, x1, y1, color);
52 	} ///
53 
54 	override void fillRect(float x0, float y0, float x1, float y1, COLOR color)
55 	{
56 		gd.aaFillRect(bitmap, x0, y0, x1, y1, color);
57 	} ///
58 
59 	override void clear()
60 	{
61 		gd.clear(bitmap, COLOR.init);
62 	} ///
63 
64 	override void draw(int x, int y, TextureSource source, int u0, int v0, int u1, int v1)
65 	{
66 		auto w = bitmap.crop(x, y, x+(u1-u0), y+(v1-v0));
67 		// TODO: use drawTo when drawing entire image
68 		// source.drawTo(w.toRef());
69 		source.getPixels.crop(u0, v0, u1, v1).blitTo(w);
70 	} ///
71 
72 	override void draw(float x0, float y0, float x1, float y1, TextureSource source, int u0, int v0, int u1, int v1)
73 	{
74 		// assert(0, "TODO");
75 	} ///
76 }
77 
78 unittest
79 {
80 	import ae.utils.graphics.color;
81 	import ae.utils.graphics.image;
82 
83 	import ae.ui.video.renderer;
84 
85 	class C : Renderer
86 	{
87 		Image!COLOR bitmap;
88 
89 		mixin SoftwareRenderer;
90 	}
91 }