1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "WebGLExtensions.h"
7 
8 #include "GLContext.h"
9 #include "mozilla/dom/WebGLRenderingContextBinding.h"
10 #include "WebGLContext.h"
11 #include "WebGLFormats.h"
12 
13 namespace mozilla {
14 
WebGLExtensionSRGB(WebGLContext * webgl)15 WebGLExtensionSRGB::WebGLExtensionSRGB(WebGLContext* webgl)
16     : WebGLExtensionBase(webgl) {
17   MOZ_ASSERT(IsSupported(webgl), "Don't construct extension if unsupported.");
18 
19   gl::GLContext* gl = webgl->GL();
20   if (!gl->IsGLES()) {
21     // Desktop OpenGL requires the following to be enabled in order to
22     // support sRGB operations on framebuffers.
23     gl->fEnable(LOCAL_GL_FRAMEBUFFER_SRGB_EXT);
24   }
25 
26   auto& fua = webgl->mFormatUsage;
27 
28   RefPtr<gl::GLContext> gl_ = gl;  // Bug 1201275
29   const auto fnAdd = [&fua, &gl_](webgl::EffectiveFormat effFormat,
30                                   GLenum format, GLenum desktopUnpackFormat) {
31     auto usage = fua->EditUsage(effFormat);
32     usage->isFilterable = true;
33 
34     webgl::DriverUnpackInfo dui = {format, format, LOCAL_GL_UNSIGNED_BYTE};
35     const auto pi = dui.ToPacking();
36 
37     if (!gl_->IsGLES()) dui.unpackFormat = desktopUnpackFormat;
38 
39     fua->AddTexUnpack(usage, pi, dui);
40 
41     fua->AllowUnsizedTexFormat(pi, usage);
42   };
43 
44   fnAdd(webgl::EffectiveFormat::SRGB8, LOCAL_GL_SRGB, LOCAL_GL_RGB);
45   fnAdd(webgl::EffectiveFormat::SRGB8_ALPHA8, LOCAL_GL_SRGB_ALPHA,
46         LOCAL_GL_RGBA);
47 
48   auto usage = fua->EditUsage(webgl::EffectiveFormat::SRGB8_ALPHA8);
49   usage->SetRenderable();
50   fua->AllowRBFormat(LOCAL_GL_SRGB8_ALPHA8, usage);
51 }
52 
IsSupported(const WebGLContext * const webgl)53 bool WebGLExtensionSRGB::IsSupported(const WebGLContext* const webgl) {
54   if (webgl->IsWebGL2()) return false;
55 
56   return webgl->gl->IsSupported(gl::GLFeature::sRGB);
57 }
58 
59 }  // namespace mozilla
60