1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef sw_Blitter_hpp
16 #define sw_Blitter_hpp
17 
18 #include "Surface.hpp"
19 #include "RoutineCache.hpp"
20 #include "Reactor/Nucleus.hpp"
21 
22 #include <string.h>
23 
24 namespace sw
25 {
26 	class Blitter
27 	{
28 		enum Options : unsigned char
29 		{
30 			FILTER_POINT = 0x00,
31 			WRITE_RED = 0x01,
32 			WRITE_GREEN = 0x02,
33 			WRITE_BLUE = 0x04,
34 			WRITE_ALPHA = 0x08,
35 			WRITE_RGBA = WRITE_RED | WRITE_GREEN | WRITE_BLUE | WRITE_ALPHA,
36 			FILTER_LINEAR = 0x10,
37 			CLEAR_OPERATION = 0x20
38 		};
39 
40 		struct BlitState
41 		{
operator ==sw::Blitter::BlitState42 			bool operator==(const BlitState &state) const
43 			{
44 				return memcmp(this, &state, sizeof(BlitState)) == 0;
45 			}
46 
47 			Format sourceFormat;
48 			Format destFormat;
49 			Blitter::Options options;
50 		};
51 
52 		struct BlitData
53 		{
54 			void *source;
55 			void *dest;
56 			int sPitchB;
57 			int dPitchB;
58 
59 			float x0;
60 			float y0;
61 			float w;
62 			float h;
63 
64 			int y0d;
65 			int y1d;
66 			int x0d;
67 			int x1d;
68 
69 			int sWidth;
70 			int sHeight;
71 		};
72 
73 	public:
74 		Blitter();
75 
76 		virtual ~Blitter();
77 
78 		void clear(void* pixel, sw::Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask);
79 		void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter);
80 		void blit3D(Surface *source, Surface *dest);
81 
82 	private:
83 		bool read(Float4 &color, Pointer<Byte> element, Format format);
84 		bool write(Float4 &color, Pointer<Byte> element, Format format, const Blitter::Options& options);
85 		bool read(Int4 &color, Pointer<Byte> element, Format format);
86 		bool write(Int4 &color, Pointer<Byte> element, Format format, const Blitter::Options& options);
87 		static bool GetScale(float4& scale, Format format);
88 		static bool ApplyScaleAndClamp(Float4& value, const BlitState& state);
89 		void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, const Blitter::Options& options);
90 		bool blitReactor(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, const Blitter::Options& options);
91 		Routine *generate(BlitState &state);
92 
93 		RoutineCache<BlitState> *blitCache;
94 		BackoffLock criticalSection;
95 	};
96 
97 	extern Blitter blitter;
98 }
99 
100 #endif   // sw_Blitter_hpp
101