1 //
2 // Copyright 2015 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // BlitGL.h: Defines the BlitGL class, a helper for blitting textures
8 
9 #ifndef LIBANGLE_RENDERER_GL_BLITGL_H_
10 #define LIBANGLE_RENDERER_GL_BLITGL_H_
11 
12 #include "angle_gl.h"
13 #include "common/angleutils.h"
14 #include "libANGLE/Error.h"
15 #include "libANGLE/angletypes.h"
16 #include "libANGLE/renderer/gl/formatutilsgl.h"
17 
18 #include <map>
19 
20 namespace angle
21 {
22 struct FeaturesGL;
23 }  // namespace angle
24 
25 namespace gl
26 {
27 class Framebuffer;
28 class ImageIndex;
29 }  // namespace gl
30 
31 namespace rx
32 {
33 
34 class FramebufferGL;
35 class FunctionsGL;
36 class RenderbufferGL;
37 class StateManagerGL;
38 class TextureGL;
39 struct VertexArrayStateGL;
40 
41 class BlitGL : angle::NonCopyable
42 {
43   public:
44     BlitGL(const FunctionsGL *functions,
45            const angle::FeaturesGL &features,
46            StateManagerGL *stateManager);
47     ~BlitGL();
48 
49     angle::Result copyImageToLUMAWorkaroundTexture(const gl::Context *context,
50                                                    GLuint texture,
51                                                    gl::TextureType textureType,
52                                                    gl::TextureTarget target,
53                                                    GLenum lumaFormat,
54                                                    size_t level,
55                                                    const gl::Rectangle &sourceArea,
56                                                    GLenum internalFormat,
57                                                    gl::Framebuffer *source);
58 
59     angle::Result copySubImageToLUMAWorkaroundTexture(const gl::Context *context,
60                                                       GLuint texture,
61                                                       gl::TextureType textureType,
62                                                       gl::TextureTarget target,
63                                                       GLenum lumaFormat,
64                                                       size_t level,
65                                                       const gl::Offset &destOffset,
66                                                       const gl::Rectangle &sourceArea,
67                                                       gl::Framebuffer *source);
68 
69     angle::Result blitColorBufferWithShader(const gl::Context *context,
70                                             const gl::Framebuffer *source,
71                                             const gl::Framebuffer *dest,
72                                             const gl::Rectangle &sourceArea,
73                                             const gl::Rectangle &destArea,
74                                             GLenum filter,
75                                             bool writeAlpha);
76 
77     angle::Result blitColorBufferWithShader(const gl::Context *context,
78                                             const gl::Framebuffer *source,
79                                             const GLuint destFramebuffer,
80                                             const gl::Rectangle &sourceArea,
81                                             const gl::Rectangle &destArea,
82                                             GLenum filter,
83                                             bool writeAlpha);
84 
85     angle::Result blitColorBufferWithShader(const gl::Context *context,
86                                             const gl::Framebuffer *source,
87                                             const GLuint destTexture,
88                                             const gl::TextureTarget destTarget,
89                                             const size_t destLevel,
90                                             const gl::Rectangle &sourceArea,
91                                             const gl::Rectangle &destArea,
92                                             GLenum filter,
93                                             bool writeAlpha);
94 
95     angle::Result copySubTexture(const gl::Context *context,
96                                  TextureGL *source,
97                                  size_t sourceLevel,
98                                  GLenum sourceComponentType,
99                                  GLuint destID,
100                                  gl::TextureTarget destTarget,
101                                  size_t destLevel,
102                                  GLenum destComponentType,
103                                  const gl::Extents &sourceSize,
104                                  const gl::Rectangle &sourceArea,
105                                  const gl::Offset &destOffset,
106                                  bool needsLumaWorkaround,
107                                  GLenum lumaFormat,
108                                  bool unpackFlipY,
109                                  bool unpackPremultiplyAlpha,
110                                  bool unpackUnmultiplyAlpha,
111                                  bool *copySucceededOut);
112 
113     angle::Result copySubTextureCPUReadback(const gl::Context *context,
114                                             TextureGL *source,
115                                             size_t sourceLevel,
116                                             GLenum sourceSizedInternalFormat,
117                                             TextureGL *dest,
118                                             gl::TextureTarget destTarget,
119                                             size_t destLevel,
120                                             GLenum destFormat,
121                                             GLenum destType,
122                                             const gl::Extents &sourceSize,
123                                             const gl::Rectangle &sourceArea,
124                                             const gl::Offset &destOffset,
125                                             bool needsLumaWorkaround,
126                                             GLenum lumaFormat,
127                                             bool unpackFlipY,
128                                             bool unpackPremultiplyAlpha,
129                                             bool unpackUnmultiplyAlpha);
130 
131     angle::Result copyTexSubImage(const gl::Context *context,
132                                   TextureGL *source,
133                                   size_t sourceLevel,
134                                   TextureGL *dest,
135                                   gl::TextureTarget destTarget,
136                                   size_t destLevel,
137                                   const gl::Rectangle &sourceArea,
138                                   const gl::Offset &destOffset,
139                                   bool *copySucceededOut);
140 
141     angle::Result clearRenderableTexture(const gl::Context *context,
142                                          TextureGL *source,
143                                          GLenum sizedInternalFormat,
144                                          int numTextureLayers,
145                                          const gl::ImageIndex &imageIndex,
146                                          bool *clearSucceededOut);
147 
148     angle::Result clearRenderbuffer(const gl::Context *context,
149                                     RenderbufferGL *source,
150                                     GLenum sizedInternalFormat);
151 
152     angle::Result clearFramebuffer(const gl::Context *context, FramebufferGL *source);
153 
154     angle::Result clearRenderableTextureAlphaToOne(const gl::Context *context,
155                                                    GLuint texture,
156                                                    gl::TextureTarget target,
157                                                    size_t level);
158 
159     angle::Result generateSRGBMipmap(const gl::Context *context,
160                                      TextureGL *source,
161                                      GLuint baseLevel,
162                                      GLuint levelCount,
163                                      const gl::Extents &sourceBaseLevelSize);
164 
165     angle::Result initializeResources(const gl::Context *context);
166 
167   private:
168     angle::Result orphanScratchTextures(const gl::Context *context);
169     angle::Result setScratchTextureParameter(const gl::Context *context,
170                                              GLenum param,
171                                              GLenum value);
172     angle::Result setVAOState(const gl::Context *context);
173     angle::Result initializeVAOState(const gl::Context *context);
174 
175     const FunctionsGL *mFunctions;
176     const angle::FeaturesGL &mFeatures;
177     StateManagerGL *mStateManager;
178 
179     struct BlitProgram
180     {
181         GLuint program                = 0;
182         GLint sourceTextureLocation   = -1;
183         GLint scaleLocation           = -1;
184         GLint offsetLocation          = -1;
185         GLint multiplyAlphaLocation   = -1;
186         GLint unMultiplyAlphaLocation = -1;
187     };
188 
189     angle::Result getBlitProgram(const gl::Context *context,
190                                  gl::TextureType sourceTextureType,
191                                  GLenum sourceComponentType,
192                                  GLenum destComponentType,
193                                  BlitProgram **program);
194 
195     bool mResourcesInitialized = false;
196 
197     // SourceType, SourceComponentType, DestComponentType
198     using BlitProgramType = std::tuple<gl::TextureType, GLenum, GLenum>;
199     std::map<BlitProgramType, BlitProgram> mBlitPrograms;
200 
201     GLuint mScratchTextures[2] = {0};
202     GLuint mScratchFBO         = 0;
203 
204     GLuint mVAO                   = 0;
205     VertexArrayStateGL *mVAOState = nullptr;
206     bool mOwnsVAOState            = false;
207 
208     const GLuint mTexcoordAttribLocation = 0;
209     GLuint mVertexBuffer                 = 0;
210 
211     nativegl::TexImageFormat mSRGBMipmapGenerationFormat;
212 };
213 }  // namespace rx
214 
215 #endif  // LIBANGLE_RENDERER_GL_BLITGL_H_
216