Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / gpu / gl / GrGLCaps.h
1 /*
2  * Copyright 2012 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7
8
9 #ifndef GrGLCaps_DEFINED
10 #define GrGLCaps_DEFINED
11
12 #include "GrDrawTargetCaps.h"
13 #include "GrGLStencilBuffer.h"
14 #include "SkChecksum.h"
15 #include "SkTHashCache.h"
16 #include "SkTArray.h"
17
18 class GrGLContextInfo;
19
20 /**
21  * Stores some capabilities of a GL context. Most are determined by the GL
22  * version and the extensions string. It also tracks formats that have passed
23  * the FBO completeness test.
24  */
25 class GrGLCaps : public GrDrawTargetCaps {
26 public:
27     SK_DECLARE_INST_COUNT(GrGLCaps)
28
29     typedef GrGLStencilBuffer::Format StencilFormat;
30
31     /**
32      * The type of MSAA for FBOs supported. Different extensions have different
33      * semantics of how / when a resolve is performed.
34      */
35     enum MSFBOType {
36         /**
37          * no support for MSAA FBOs
38          */
39         kNone_MSFBOType = 0,
40         /**
41          * GL3.0-style MSAA FBO (GL_ARB_framebuffer_object).
42          */
43         kDesktop_ARB_MSFBOType,
44         /**
45          * earlier GL_EXT_framebuffer* extensions
46          */
47         kDesktop_EXT_MSFBOType,
48         /**
49          * Similar to kDesktop_ARB but with additional restrictions on glBlitFramebuffer.
50          */
51         kES_3_0_MSFBOType,
52         /**
53          * GL_APPLE_framebuffer_multisample ES extension
54          */
55         kES_Apple_MSFBOType,
56         /**
57          * GL_IMG_multisampled_render_to_texture. This variation does not have MSAA renderbuffers.
58          * Instead the texture is multisampled when bound to the FBO and then resolved automatically
59          * when read. It also defines an alternate value for GL_MAX_SAMPLES (which we call
60          * GR_GL_MAX_SAMPLES_IMG).
61          */
62         kES_IMG_MsToTexture_MSFBOType,
63         /**
64          * GL_EXT_multisampled_render_to_texture. Same as the IMG one above but uses the standard
65          * GL_MAX_SAMPLES value.
66          */
67         kES_EXT_MsToTexture_MSFBOType,
68
69         kLast_MSFBOType = kES_EXT_MsToTexture_MSFBOType
70     };
71
72     enum InvalidateFBType {
73         kNone_InvalidateFBType,
74         kDiscard_InvalidateFBType,       //<! glDiscardFramebuffer()
75         kInvalidate_InvalidateFBType,     //<! glInvalidateFramebuffer()
76
77         kLast_InvalidateFBType = kInvalidate_InvalidateFBType
78     };
79
80     enum MapBufferType {
81         kNone_MapBufferType,
82         kMapBuffer_MapBufferType,         // glMapBuffer()
83         kMapBufferRange_MapBufferType,    // glMapBufferRange()
84         kChromium_MapBufferType,          // GL_CHROMIUM_map_sub
85
86         kLast_MapBufferType = kChromium_MapBufferType,
87     };
88
89     /**
90      * Creates a GrGLCaps that advertises no support for any extensions,
91      * formats, etc. Call init to initialize from a GrGLContextInfo.
92      */
93     GrGLCaps();
94
95     GrGLCaps(const GrGLCaps& caps);
96
97     GrGLCaps& operator = (const GrGLCaps& caps);
98
99     /**
100      * Resets the caps such that nothing is supported.
101      */
102     virtual void reset() SK_OVERRIDE;
103
104     /**
105      * Initializes the GrGLCaps to the set of features supported in the current
106      * OpenGL context accessible via ctxInfo.
107      */
108     bool init(const GrGLContextInfo& ctxInfo, const GrGLInterface* glInterface);
109
110     /**
111      * Call to note that a color config has been verified as a valid color
112      * attachment. This may save future calls to glCheckFramebufferStatus
113      * using isConfigVerifiedColorAttachment().
114      */
115     void markConfigAsValidColorAttachment(GrPixelConfig config) {
116         fVerifiedColorConfigs.markVerified(config);
117     }
118
119     /**
120      * Call to check whether a config has been verified as a valid color
121      * attachment.
122      */
123     bool isConfigVerifiedColorAttachment(GrPixelConfig config) const {
124         return fVerifiedColorConfigs.isVerified(config);
125     }
126
127     /**
128      * Call to note that a color config / stencil format pair passed
129      * FBO status check. We may skip calling glCheckFramebufferStatus for
130      * this combination in the future using
131      * isColorConfigAndStencilFormatVerified().
132      */
133     void markColorConfigAndStencilFormatAsVerified(
134                     GrPixelConfig config,
135                     const GrGLStencilBuffer::Format& format);
136
137     /**
138      * Call to check whether color config / stencil format pair has already
139      * passed FBO status check.
140      */
141     bool isColorConfigAndStencilFormatVerified(
142                     GrPixelConfig config,
143                     const GrGLStencilBuffer::Format& format) const;
144
145     /**
146      * Reports the type of MSAA FBO support.
147      */
148     MSFBOType msFBOType() const { return fMSFBOType; }
149
150     /**
151      * Does the supported MSAA FBO extension have MSAA renderbuffers?
152      */
153     bool usesMSAARenderBuffers() const {
154         return kNone_MSFBOType != fMSFBOType &&
155                kES_IMG_MsToTexture_MSFBOType != fMSFBOType &&
156                kES_EXT_MsToTexture_MSFBOType != fMSFBOType;
157     }
158
159     /**
160      * Is the MSAA FBO extension one where the texture is multisampled when bound to an FBO and
161      * then implicitly resolved when read.
162      */
163     bool usesImplicitMSAAResolve() const {
164         return kES_IMG_MsToTexture_MSFBOType == fMSFBOType ||
165                kES_EXT_MsToTexture_MSFBOType == fMSFBOType;
166     }
167
168     /**
169      * Some helper functions for encapsulating various extensions to read FB Buffer on openglES
170      *
171      * TODO(joshualitt) On desktop opengl 4.2+ we can achieve something similar to this effect
172      */
173     bool fbFetchSupport() const { return fFBFetchSupport; }
174
175     const char* fbFetchColorName() const { return fFBFetchColorName; }
176
177     const char* fbFetchExtensionString() const { return fFBFetchExtensionString; }
178
179     InvalidateFBType invalidateFBType() const { return fInvalidateFBType; }
180
181     /// What type of buffer mapping is supported?
182     MapBufferType mapBufferType() const { return fMapBufferType; }
183
184     /**
185      * Gets an array of legal stencil formats. These formats are not guaranteed
186      * to be supported by the driver but are legal GLenum names given the GL
187      * version and extensions supported.
188      */
189     const SkTArray<StencilFormat, true>& stencilFormats() const {
190         return fStencilFormats;
191     }
192
193     /// The maximum number of fragment uniform vectors (GLES has min. 16).
194     int maxFragmentUniformVectors() const { return fMaxFragmentUniformVectors; }
195
196     /// maximum number of attribute values per vertex
197     int maxVertexAttributes() const { return fMaxVertexAttributes; }
198
199     /// maximum number of texture units accessible in the fragment shader.
200     int maxFragmentTextureUnits() const { return fMaxFragmentTextureUnits; }
201
202     /// maximum number of fixed-function texture coords, or zero if no fixed-function.
203     int maxFixedFunctionTextureCoords() const { return fMaxFixedFunctionTextureCoords; }
204
205     /// ES requires an extension to support RGBA8 in RenderBufferStorage
206     bool rgba8RenderbufferSupport() const { return fRGBA8RenderbufferSupport; }
207
208     /**
209      * Depending on the ES extensions present the BGRA external format may
210      * correspond either a BGRA or RGBA internalFormat. On desktop GL it is
211      * RGBA.
212      */
213     bool bgraIsInternalFormat() const { return fBGRAIsInternalFormat; }
214
215     /// GL_ARB_texture_swizzle support
216     bool textureSwizzleSupport() const { return fTextureSwizzleSupport; }
217
218     /// Is there support for GL_UNPACK_ROW_LENGTH
219     bool unpackRowLengthSupport() const { return fUnpackRowLengthSupport; }
220
221     /// Is there support for GL_UNPACK_FLIP_Y
222     bool unpackFlipYSupport() const { return fUnpackFlipYSupport; }
223
224     /// Is there support for GL_PACK_ROW_LENGTH
225     bool packRowLengthSupport() const { return fPackRowLengthSupport; }
226
227     /// Is there support for GL_PACK_REVERSE_ROW_ORDER
228     bool packFlipYSupport() const { return fPackFlipYSupport; }
229
230     /// Is there support for texture parameter GL_TEXTURE_USAGE
231     bool textureUsageSupport() const { return fTextureUsageSupport; }
232
233     /// Is there support for glTexStorage
234     bool texStorageSupport() const { return fTexStorageSupport; }
235
236     /// Is there support for GL_RED and GL_R8
237     bool textureRedSupport() const { return fTextureRedSupport; }
238
239     /// Is GL_ARB_IMAGING supported
240     bool imagingSupport() const { return fImagingSupport; }
241
242     /// Is GL_ARB_fragment_coord_conventions supported?
243     bool fragCoordConventionsSupport() const { return fFragCoordsConventionSupport; }
244
245     /// Is there support for Vertex Array Objects?
246     bool vertexArrayObjectSupport() const { return fVertexArrayObjectSupport; }
247
248     /// Use indices or vertices in CPU arrays rather than VBOs for dynamic content.
249     bool useNonVBOVertexAndIndexDynamicData() const {
250         return fUseNonVBOVertexAndIndexDynamicData;
251     }
252
253     /// Does ReadPixels support the provided format/type combo?
254     bool readPixelsSupported(const GrGLInterface* intf,
255                              GrGLenum format,
256                              GrGLenum type,
257                              GrGLenum currFboFormat) const;
258
259     bool isCoreProfile() const { return fIsCoreProfile; }
260
261
262     bool fullClearIsFree() const { return fFullClearIsFree; }
263
264     bool dropsTileOnZeroDivide() const { return fDropsTileOnZeroDivide; }
265
266     /**
267      * Returns a string containing the caps info.
268      */
269     virtual SkString dump() const SK_OVERRIDE;
270
271     /**
272      * LATC can appear under one of three possible names. In order to know
273      * which GL internal format to use, we need to keep track of which name
274      * we found LATC under. The default is LATC.
275      */
276     enum LATCAlias {
277         kLATC_LATCAlias,
278         kRGTC_LATCAlias,
279         k3DC_LATCAlias
280     };
281
282     LATCAlias latcAlias() const { return fLATCAlias; }
283
284 private:
285     /**
286      * Maintains a bit per GrPixelConfig. It is used to avoid redundantly
287      * performing glCheckFrameBufferStatus for the same config.
288      */
289     struct VerifiedColorConfigs {
290         VerifiedColorConfigs() {
291             this->reset();
292         }
293
294         void reset() {
295             for (int i = 0; i < kNumUints; ++i) {
296                 fVerifiedColorConfigs[i] = 0;
297             }
298         }
299
300         static const int kNumUints = (kGrPixelConfigCnt  + 31) / 32;
301         uint32_t fVerifiedColorConfigs[kNumUints];
302
303         void markVerified(GrPixelConfig config) {
304 #if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
305                 return;
306 #endif
307             int u32Idx = config / 32;
308             int bitIdx = config % 32;
309             fVerifiedColorConfigs[u32Idx] |= 1 << bitIdx;
310         }
311
312         bool isVerified(GrPixelConfig config) const {
313 #if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
314             return false;
315 #endif
316             int u32Idx = config / 32;
317             int bitIdx = config % 32;
318             return SkToBool(fVerifiedColorConfigs[u32Idx] & (1 << bitIdx));
319         }
320     };
321
322     void initFSAASupport(const GrGLContextInfo&, const GrGLInterface*);
323     void initStencilFormats(const GrGLContextInfo&);
324     // This must be called after initFSAASupport().
325     void initConfigRenderableTable(const GrGLContextInfo&);
326     void initConfigTexturableTable(const GrGLContextInfo&, const GrGLInterface*);
327
328     bool doReadPixelsSupported(const GrGLInterface* intf,
329                                    GrGLenum format,
330                                    GrGLenum type) const;
331
332     // tracks configs that have been verified to pass the FBO completeness when
333     // used as a color attachment
334     VerifiedColorConfigs fVerifiedColorConfigs;
335
336     SkTArray<StencilFormat, true> fStencilFormats;
337     // tracks configs that have been verified to pass the FBO completeness when
338     // used as a color attachment when a particular stencil format is used
339     // as a stencil attachment.
340     SkTArray<VerifiedColorConfigs, true> fStencilVerifiedColorConfigs;
341
342     int fMaxFragmentUniformVectors;
343     int fMaxVertexAttributes;
344     int fMaxFragmentTextureUnits;
345     int fMaxFixedFunctionTextureCoords;
346
347     MSFBOType           fMSFBOType;
348     InvalidateFBType    fInvalidateFBType;
349     MapBufferType       fMapBufferType;
350     LATCAlias           fLATCAlias;
351
352     bool fRGBA8RenderbufferSupport : 1;
353     bool fBGRAIsInternalFormat : 1;
354     bool fTextureSwizzleSupport : 1;
355     bool fUnpackRowLengthSupport : 1;
356     bool fUnpackFlipYSupport : 1;
357     bool fPackRowLengthSupport : 1;
358     bool fPackFlipYSupport : 1;
359     bool fTextureUsageSupport : 1;
360     bool fTexStorageSupport : 1;
361     bool fTextureRedSupport : 1;
362     bool fImagingSupport  : 1;
363     bool fTwoFormatLimit : 1;
364     bool fFragCoordsConventionSupport : 1;
365     bool fVertexArrayObjectSupport : 1;
366     bool fUseNonVBOVertexAndIndexDynamicData : 1;
367     bool fIsCoreProfile : 1;
368     bool fFullClearIsFree : 1;
369     bool fDropsTileOnZeroDivide : 1;
370     // TODO(joshualitt) encapsulate the FB Fetch logic in a feature object
371     bool fFBFetchSupport : 1;
372
373     const char* fFBFetchColorName;
374     const char* fFBFetchExtensionString;
375
376     class ReadPixelsSupportedFormats {
377     public:
378         struct Key {
379             GrGLenum fFormat;
380             GrGLenum fType;
381             GrGLenum fFboFormat;
382
383             bool operator==(const Key& rhs) const {
384                 return fFormat == rhs.fFormat
385                         && fType == rhs.fType
386                         && fFboFormat == rhs.fFboFormat;
387             }
388
389             uint32_t getHash() const {
390                 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(this), sizeof(*this));
391             }
392         };
393
394         ReadPixelsSupportedFormats(Key key, bool value) : fKey(key), fValue(value) {
395         }
396
397         static const Key& GetKey(const ReadPixelsSupportedFormats& element) {
398             return element.fKey;
399         }
400
401         static uint32_t Hash(const Key& key) {
402             return key.getHash();
403         }
404
405         bool value() const {
406             return fValue;
407         }
408     private:
409         Key fKey;
410         bool fValue;
411     };
412
413     mutable SkTHashCache<ReadPixelsSupportedFormats,
414                          ReadPixelsSupportedFormats::Key> fReadPixelsSupportedCache;
415
416     typedef GrDrawTargetCaps INHERITED;
417 };
418
419 #endif