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.hpp: Defines the egl::Surface class, representing a rendering 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_EGL_SURFACE_H_
20#define INCLUDE_EGL_SURFACE_H_
21
22#include "common/Object.hpp"
23#include "common/Surface.hpp"
24
25#include "Main/FrameBuffer.hpp"
26
27#include <EGL/egl.h>
28
29namespace egl
30{
31class Display;
32class Config;
33
34class Surface : public gl::Surface, public gl::Object
35{
36public:
37 virtual bool initialize();
38 virtual void swap() = 0;
39
40 egl::Image *getRenderTarget() override;
41 egl::Image *getDepthStencil() override;
42
43 void setMipmapLevel(EGLint mipmapLevel);
44 void setMultisampleResolve(EGLenum multisampleResolve);
45 void setSwapBehavior(EGLenum swapBehavior);
46 void setSwapInterval(EGLint interval);
47
48 virtual EGLint getConfigID() const;
49 virtual EGLenum getSurfaceType() const;
50
51 EGLint getWidth() const override;
52 EGLint getHeight() const override;
53 EGLenum getTextureTarget() const override;
54 virtual EGLint getMipmapLevel() const;
55 virtual EGLenum getMultisampleResolve() const;
56 virtual EGLint getPixelAspectRatio() const;
57 virtual EGLenum getRenderBuffer() const;
58 virtual EGLenum getSwapBehavior() const;
59 virtual EGLenum getTextureFormat() const;
60 virtual EGLBoolean getLargestPBuffer() const;
61 virtual EGLNativeWindowType getWindowHandle() const = 0;
62
63 void setBoundTexture(egl::Texture *texture) override;
64 virtual egl::Texture *getBoundTexture() const;
65
66 virtual bool isWindowSurface() const { return false; }
67 virtual bool isPBufferSurface() const { return false; }
68 bool hasClientBuffer() const { return clientBuffer != nullptr; }
69
70protected:
71 Surface(const Display *display, const Config *config);
72
73 ~Surface() override;
74
75 virtual void deleteResources();
76
77 sw::Format getClientBufferFormat() const;
78
79 const Display *const display;
80 const Config *const config;
81
82 Image *depthStencil = nullptr;
83 Image *backBuffer = nullptr;
84 Texture *texture = nullptr;
85
86 bool reset(int backbufferWidth, int backbufferHeight);
87
88 // Surface attributes:
89 EGLint width = 0; // Width of surface
90 EGLint height= 0; // Height of surface
91// EGLint horizontalResolution = EGL_UNKNOWN; // Horizontal dot pitch
92// EGLint verticalResolution = EGL_UNKNOWN; // Vertical dot pitch
93 EGLBoolean largestPBuffer = EGL_FALSE; // If true, create largest pbuffer possible
94// EGLBoolean mipmapTexture = EGL_FALSE; // True if texture has mipmaps
95 EGLint mipmapLevel = 0; // Mipmap level to render to
96 EGLenum multisampleResolve = EGL_MULTISAMPLE_RESOLVE_DEFAULT; // Multisample resolve behavior
97 EGLint pixelAspectRatio = EGL_UNKNOWN; // Display aspect ratio
98 EGLenum renderBuffer = EGL_BACK_BUFFER; // Render buffer
99 EGLenum swapBehavior = EGL_BUFFER_PRESERVED; // Buffer swap behavior (initial value chosen by implementation)
100 EGLenum textureFormat = EGL_NO_TEXTURE; // Format of texture: RGB, RGBA, or no texture
101 EGLenum textureTarget = EGL_NO_TEXTURE; // Type of texture: 2D or no texture
102// EGLenum vgAlphaFormat = EGL_VG_ALPHA_FORMAT_NONPRE; // Alpha format for OpenVG
103// EGLenum vgColorSpace = EGL_VG_COLORSPACE_sRGB; // Color space for OpenVG
104
105 EGLint swapInterval = 1;
106
107 // EGL_ANGLE_iosurface_client_buffer attributes:
108 EGLClientBuffer clientBuffer = nullptr;
109 EGLint clientBufferPlane;
110 EGLenum clientBufferFormat; // Format of the client buffer
111 EGLenum clientBufferType; // Type of the client buffer
112};
113
114class WindowSurface : public Surface
115{
116public:
117 WindowSurface(Display *display, const egl::Config *config, EGLNativeWindowType window);
118 ~WindowSurface() override;
119
120 bool initialize() override;
121
122 bool isWindowSurface() const override { return true; }
123 void swap() override;
124
125 EGLNativeWindowType getWindowHandle() const override;
126
127private:
128 void deleteResources() override;
129 bool checkForResize();
130 bool reset(int backBufferWidth, int backBufferHeight);
131
132 const EGLNativeWindowType window;
133 sw::FrameBuffer *frameBuffer = nullptr;
134};
135
136class PBufferSurface : public Surface
137{
138public:
139 PBufferSurface(Display *display, const egl::Config *config, EGLint width, EGLint height,
140 EGLenum textureFormat, EGLenum textureTarget, EGLenum internalFormat,
141 EGLenum textureType, EGLBoolean largestPBuffer, EGLClientBuffer clientBuffer,
142 EGLint clientBufferPlane);
143 ~PBufferSurface() override;
144
145 bool isPBufferSurface() const override { return true; }
146 void swap() override;
147
148 EGLNativeWindowType getWindowHandle() const override;
149
150private:
151 void deleteResources() override;
152};
153}
154
155#endif // INCLUDE_EGL_SURFACE_H_
156