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/Reactor.hpp"
21 
22 #include <string.h>
23 
24 namespace sw
25 {
26 	class Blitter
27 	{
28 		struct Options
29 		{
30 			Options() = default;
Optionssw::Blitter::Options31 			Options(bool filter, bool useStencil, bool convertSRGB)
32 				: writeMask(0xF), clearOperation(false), filter(filter), useStencil(useStencil), convertSRGB(convertSRGB), clampToEdge(false) {}
Optionssw::Blitter::Options33 			Options(unsigned int writeMask)
34 				: writeMask(writeMask), clearOperation(true), filter(false), useStencil(false), convertSRGB(true), clampToEdge(false) {}
35 
36 			union
37 			{
38 				struct
39 				{
40 					bool writeRed : 1;
41 					bool writeGreen : 1;
42 					bool writeBlue : 1;
43 					bool writeAlpha : 1;
44 				};
45 
46 				unsigned char writeMask;
47 			};
48 
49 			bool clearOperation : 1;
50 			bool filter : 1;
51 			bool useStencil : 1;
52 			bool convertSRGB : 1;
53 			bool clampToEdge : 1;
54 		};
55 
56 		struct State : Options
57 		{
58 			State() = default;
Statesw::Blitter::State59 			State(const Options &options) : Options(options) {}
60 
operator ==sw::Blitter::State61 			bool operator==(const State &state) const
62 			{
63 				return memcmp(this, &state, sizeof(State)) == 0;
64 			}
65 
66 			Format sourceFormat;
67 			Format destFormat;
68 			int destSamples;
69 		};
70 
71 		struct BlitData
72 		{
73 			void *source;
74 			void *dest;
75 			int sPitchB;
76 			int dPitchB;
77 			int dSliceB;
78 
79 			float x0;
80 			float y0;
81 			float w;
82 			float h;
83 
84 			int y0d;
85 			int y1d;
86 			int x0d;
87 			int x1d;
88 
89 			int sWidth;
90 			int sHeight;
91 		};
92 
93 	public:
94 		Blitter();
95 		virtual ~Blitter();
96 
97 		void clear(void *pixel, sw::Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask);
98 		void blit(Surface *source, const SliceRectF &sRect, Surface *dest, const SliceRect &dRect, const Options &options);
99 		void blit3D(Surface *source, Surface *dest);
100 
101 	private:
102 		bool fastClear(void *pixel, sw::Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask);
103 
104 		bool read(Float4 &color, Pointer<Byte> element, const State &state);
105 		bool write(Float4 &color, Pointer<Byte> element, const State &state);
106 		bool read(Int4 &color, Pointer<Byte> element, const State &state);
107 		bool write(Int4 &color, Pointer<Byte> element, const State &state);
108 		static bool GetScale(float4& scale, Format format);
109 		static bool ApplyScaleAndClamp(Float4 &value, const State &state, bool preScaled = false);
110 		static Int ComputeOffset(Int &x, Int &y, Int &pitchB, int bytes, bool quadLayout);
111 		static Float4 LinearToSRGB(Float4 &color);
112 		static Float4 sRGBtoLinear(Float4 &color);
113 		bool blitReactor(Surface *source, const SliceRectF &sRect, Surface *dest, const SliceRect &dRect, const Options &options);
114 		Routine *generate(const State &state);
115 
116 		RoutineCache<State> *blitCache;
117 		MutexLock criticalSection;
118 	};
119 }
120 
121 #endif   // sw_Blitter_hpp
122