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 // Surface.h: Defines the egl::Surface class, representing a drawing surface
16 // such as the client area of a window, including any back buffers.
17 // Implements EGLSurface and related functionality. [EGL 1.4] section 2.2 page 3.
18 
19 #ifndef INCLUDE_SURFACE_H_
20 #define INCLUDE_SURFACE_H_
21 
22 #include "Main/FrameBuffer.hpp"
23 #include "common/Object.hpp"
24 
25 #include <EGL/egl.h>
26 
27 namespace egl
28 {
29 class Display;
30 class Config;
31 class Texture;
32 class Image;
33 
34 class Surface : public gl::Object
35 {
36 public:
37 	virtual bool initialize();
38 	virtual void swap() = 0;
39 
40 	virtual egl::Image *getRenderTarget();
41 	virtual egl::Image *getDepthStencil();
42 
43 	void setSwapBehavior(EGLenum swapBehavior);
44 	void setSwapInterval(EGLint interval);
45 
46 	virtual EGLint getConfigID() const;
47 	virtual EGLenum getSurfaceType() const;
48 	virtual sw::Format getInternalFormat() const;
49 
50 	virtual EGLint getWidth() const;
51 	virtual EGLint getHeight() const;
52 	virtual EGLint getPixelAspectRatio() const;
53 	virtual EGLenum getRenderBuffer() const;
54 	virtual EGLenum getSwapBehavior() const;
55 	virtual EGLenum getTextureFormat() const;
56 	virtual EGLenum getTextureTarget() const;
57 	virtual EGLBoolean getLargestPBuffer() const;
58 	virtual EGLNativeWindowType getWindowHandle() const = 0;
59 
60 	virtual void setBoundTexture(egl::Texture *texture);
61 	virtual egl::Texture *getBoundTexture() const;
62 
isWindowSurface()63 	virtual bool isWindowSurface() const { return false; }
isPBufferSurface()64 	virtual bool isPBufferSurface() const { return false; }
65 
66 protected:
67 	Surface(const Display *display, const Config *config);
68 
69 	virtual ~Surface();
70 
71 	virtual void deleteResources();
72 
73 	const Display *const display;
74 	Image *depthStencil;
75 	Image *backBuffer;
76 	Texture *texture;
77 
78 	bool reset(int backbufferWidth, int backbufferHeight);
79 
80 	const Config *const config;    // EGL config surface was created with
81 	EGLint height;                 // Height of surface
82 	EGLint width;                  // Width of surface
83 //  EGLint horizontalResolution;   // Horizontal dot pitch
84 //  EGLint verticalResolution;     // Vertical dot pitch
85 	EGLBoolean largestPBuffer;     // If true, create largest pbuffer possible
86 //  EGLBoolean mipmapTexture;      // True if texture has mipmaps
87 //  EGLint mipmapLevel;            // Mipmap level to render to
88 //  EGLenum multisampleResolve;    // Multisample resolve behavior
89 	EGLint pixelAspectRatio;       // Display aspect ratio
90 	EGLenum renderBuffer;          // Render buffer
91 	EGLenum swapBehavior;          // Buffer swap behavior
92 	EGLenum textureFormat;         // Format of texture: RGB, RGBA, or no texture
93 	EGLenum textureTarget;         // Type of texture: 2D or no texture
94 //  EGLenum vgAlphaFormat;         // Alpha format for OpenVG
95 //  EGLenum vgColorSpace;          // Color space for OpenVG
96 	EGLint swapInterval;
97 };
98 
99 class WindowSurface : public Surface
100 {
101 public:
102 	WindowSurface(Display *display, const egl::Config *config, EGLNativeWindowType window);
103 	~WindowSurface() override;
104 
105 	bool initialize() override;
106 
isWindowSurface()107 	bool isWindowSurface() const override { return true; }
108 	void swap() override;
109 
110 	EGLNativeWindowType getWindowHandle() const override;
111 
112 private:
113 	void deleteResources() override;
114 	bool checkForResize();
115 	bool reset(int backBufferWidth, int backBufferHeight);
116 
117 	const EGLNativeWindowType window;
118 	sw::FrameBuffer *frameBuffer;
119 };
120 
121 class PBufferSurface : public Surface
122 {
123 public:
124 	PBufferSurface(Display *display, const egl::Config *config, EGLint width, EGLint height, EGLenum textureFormat, EGLenum textureTarget, EGLBoolean largestPBuffer);
125 	~PBufferSurface() override;
126 
isPBufferSurface()127 	bool isPBufferSurface() const override { return true; }
128 	void swap() override;
129 
130 	EGLNativeWindowType getWindowHandle() const override;
131 
132 private:
133 	void deleteResources() override;
134 };
135 }
136 
137 #endif   // INCLUDE_SURFACE_H_
138