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_FrameBuffer_hpp
16 #define	sw_FrameBuffer_hpp
17 
18 #include "Reactor/Nucleus.hpp"
19 #include "Renderer/Surface.hpp"
20 #include "Common/Thread.hpp"
21 
22 namespace sw
23 {
24 	class Surface;
25 
26 	struct BlitState
27 	{
28 		int width;
29 		int height;
30 		Format destFormat;
31 		Format sourceFormat;
32 		int stride;
33 		int cursorWidth;
34 		int cursorHeight;
35 	};
36 
37 	class FrameBuffer
38 	{
39 	public:
40 		FrameBuffer(int width, int height, bool fullscreen, bool topLeftOrigin);
41 
42 		virtual ~FrameBuffer();
43 
44 		int getWidth() const;
45 		int getHeight() const;
46 		int getStride() const;
47 
48 		virtual void flip(void *source, Format sourceFormat, size_t sourceStride) = 0;
49 		virtual void blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride) = 0;
50 
51 		virtual void *lock() = 0;
52 		virtual void unlock() = 0;
53 
54 		static void setCursorImage(sw::Surface *cursor);
55 		static void setCursorOrigin(int x0, int y0);
56 		static void setCursorPosition(int x, int y);
57 
58 		static Routine *copyRoutine(const BlitState &state);
59 
60 	protected:
61 		void copy(void *source, Format format, size_t stride);
62 		int width;
63 		int height;
64 		Format sourceFormat;
65 		Format destFormat;
66 		int stride;
67 		bool windowed;
68 
69 		void *locked;   // Video memory back buffer
70 
71 	private:
72 		void copyLocked();
73 
74 		static void threadFunction(void *parameters);
75 
76 		void *target;   // Render target buffer
77 
78 		void (*blitFunction)(void *dst, void *src);
79 		Routine *blitRoutine;
80 		BlitState blitState;
81 
82 		static void blend(const BlitState &state, const Pointer<Byte> &d, const Pointer<Byte> &s, const Pointer<Byte> &c);
83 
84 		static void *cursor;
85 		static int cursorWidth;
86 		static int cursorHeight;
87 		static int cursorHotspotX;
88 		static int cursorHotspotY;
89 		static int cursorPositionX;
90 		static int cursorPositionY;
91 		static int cursorX;
92 		static int cursorY;
93 
94 		Thread *blitThread;
95 		Event syncEvent;
96 		Event blitEvent;
97 		volatile bool terminate;
98 
99 		static bool topLeftOrigin;
100 	};
101 }
102 
103 #endif	 //	sw_FrameBuffer_hpp
104