File: WebGL2RenderingContext.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (200 lines) | stat: -rw-r--r-- 8,370 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "modules/webgl/WebGL2RenderingContext.h"

#include "bindings/modules/v8/OffscreenCanvasRenderingContext2DOrWebGLRenderingContextOrWebGL2RenderingContext.h"
#include "bindings/modules/v8/RenderingContext.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/Settings.h"
#include "core/loader/FrameLoader.h"
#include "core/loader/FrameLoaderClient.h"
#include "gpu/command_buffer/client/gles2_interface.h"
#include "modules/webgl/EXTColorBufferFloat.h"
#include "modules/webgl/EXTDisjointTimerQueryWebGL2.h"
#include "modules/webgl/EXTTextureFilterAnisotropic.h"
#include "modules/webgl/OESTextureFloatLinear.h"
#include "modules/webgl/WebGLCompressedTextureASTC.h"
#include "modules/webgl/WebGLCompressedTextureATC.h"
#include "modules/webgl/WebGLCompressedTextureETC.h"
#include "modules/webgl/WebGLCompressedTextureETC1.h"
#include "modules/webgl/WebGLCompressedTexturePVRTC.h"
#include "modules/webgl/WebGLCompressedTextureS3TC.h"
#include "modules/webgl/WebGLCompressedTextureS3TCsRGB.h"
#include "modules/webgl/WebGLContextAttributeHelpers.h"
#include "modules/webgl/WebGLContextEvent.h"
#include "modules/webgl/WebGLDebugRendererInfo.h"
#include "modules/webgl/WebGLDebugShaders.h"
#include "modules/webgl/WebGLGetBufferSubDataAsync.h"
#include "modules/webgl/WebGLLoseContext.h"
#include "platform/graphics/gpu/DrawingBuffer.h"
#include "public/platform/Platform.h"
#include "public/platform/WebGraphicsContext3DProvider.h"
#include <memory>

namespace blink {

// An helper function for the two create() methods. The return value is an
// indicate of whether the create() should return nullptr or not.
static bool shouldCreateContext(WebGraphicsContext3DProvider* contextProvider,
                                HTMLCanvasElement* canvas,
                                OffscreenCanvas* offscreenCanvas) {
  if (!contextProvider) {
    if (canvas) {
      canvas->dispatchEvent(WebGLContextEvent::create(
          EventTypeNames::webglcontextcreationerror, false, true,
          "Failed to create a WebGL2 context."));
    } else {
      offscreenCanvas->dispatchEvent(WebGLContextEvent::create(
          EventTypeNames::webglcontextcreationerror, false, true,
          "Failed to create a WebGL2 context."));
    }
    return false;
  }

  gpu::gles2::GLES2Interface* gl = contextProvider->contextGL();
  std::unique_ptr<Extensions3DUtil> extensionsUtil =
      Extensions3DUtil::create(gl);
  if (!extensionsUtil)
    return false;
  if (extensionsUtil->supportsExtension("GL_EXT_debug_marker")) {
    String contextLabel(
        String::format("WebGL2RenderingContext-%p", contextProvider));
    gl->PushGroupMarkerEXT(0, contextLabel.ascii().data());
  }
  return true;
}

CanvasRenderingContext* WebGL2RenderingContext::Factory::create(
    HTMLCanvasElement* canvas,
    const CanvasContextCreationAttributes& attrs,
    Document&) {
  std::unique_ptr<WebGraphicsContext3DProvider> contextProvider(
      createWebGraphicsContext3DProvider(canvas, attrs, 2));
  if (!shouldCreateContext(contextProvider.get(), canvas, nullptr))
    return nullptr;
  WebGL2RenderingContext* renderingContext =
      new WebGL2RenderingContext(canvas, std::move(contextProvider), attrs);

  if (!renderingContext->drawingBuffer()) {
    canvas->dispatchEvent(WebGLContextEvent::create(
        EventTypeNames::webglcontextcreationerror, false, true,
        "Could not create a WebGL2 context."));
    return nullptr;
  }

  renderingContext->initializeNewContext();
  renderingContext->registerContextExtensions();

  return renderingContext;
}

CanvasRenderingContext* WebGL2RenderingContext::Factory::create(
    ScriptState* scriptState,
    OffscreenCanvas* offscreenCanvas,
    const CanvasContextCreationAttributes& attrs) {
  std::unique_ptr<WebGraphicsContext3DProvider> contextProvider(
      createWebGraphicsContext3DProvider(scriptState, attrs, 2));
  if (!shouldCreateContext(contextProvider.get(), nullptr, offscreenCanvas))
    return nullptr;
  WebGL2RenderingContext* renderingContext = new WebGL2RenderingContext(
      offscreenCanvas, std::move(contextProvider), attrs);

  if (!renderingContext->drawingBuffer()) {
    offscreenCanvas->dispatchEvent(WebGLContextEvent::create(
        EventTypeNames::webglcontextcreationerror, false, true,
        "Could not create a WebGL2 context."));
    return nullptr;
  }

  renderingContext->initializeNewContext();
  renderingContext->registerContextExtensions();

  return renderingContext;
}

void WebGL2RenderingContext::Factory::onError(HTMLCanvasElement* canvas,
                                              const String& error) {
  canvas->dispatchEvent(WebGLContextEvent::create(
      EventTypeNames::webglcontextcreationerror, false, true, error));
}

WebGL2RenderingContext::WebGL2RenderingContext(
    HTMLCanvasElement* passedCanvas,
    std::unique_ptr<WebGraphicsContext3DProvider> contextProvider,
    const CanvasContextCreationAttributes& requestedAttributes)
    : WebGL2RenderingContextBase(passedCanvas,
                                 std::move(contextProvider),
                                 requestedAttributes) {}

WebGL2RenderingContext::WebGL2RenderingContext(
    OffscreenCanvas* passedOffscreenCanvas,
    std::unique_ptr<WebGraphicsContext3DProvider> contextProvider,
    const CanvasContextCreationAttributes& requestedAttributes)
    : WebGL2RenderingContextBase(passedOffscreenCanvas,
                                 std::move(contextProvider),
                                 requestedAttributes) {}

void WebGL2RenderingContext::setCanvasGetContextResult(
    RenderingContext& result) {
  result.setWebGL2RenderingContext(this);
}

void WebGL2RenderingContext::setOffscreenCanvasGetContextResult(
    OffscreenRenderingContext& result) {
  result.setWebGL2RenderingContext(this);
}

ImageBitmap* WebGL2RenderingContext::transferToImageBitmap(
    ScriptState* scriptState) {
  NOTIMPLEMENTED();
  return nullptr;
}

void WebGL2RenderingContext::registerContextExtensions() {
  // Register extensions.
  registerExtension<EXTColorBufferFloat>(m_extColorBufferFloat);
  registerExtension<EXTDisjointTimerQueryWebGL2>(m_extDisjointTimerQueryWebGL2);
  registerExtension<EXTTextureFilterAnisotropic>(m_extTextureFilterAnisotropic);
  registerExtension<OESTextureFloatLinear>(m_oesTextureFloatLinear);
  registerExtension<WebGLCompressedTextureASTC>(m_webglCompressedTextureASTC);
  registerExtension<WebGLCompressedTextureATC>(m_webglCompressedTextureATC);
  registerExtension<WebGLCompressedTextureETC>(m_webglCompressedTextureETC);
  registerExtension<WebGLCompressedTextureETC1>(m_webglCompressedTextureETC1);
  registerExtension<WebGLCompressedTexturePVRTC>(m_webglCompressedTexturePVRTC);
  registerExtension<WebGLCompressedTextureS3TC>(m_webglCompressedTextureS3TC);
  registerExtension<WebGLCompressedTextureS3TCsRGB>(
      m_webglCompressedTextureS3TCsRGB, DraftExtension);
  registerExtension<WebGLDebugRendererInfo>(m_webglDebugRendererInfo);
  registerExtension<WebGLDebugShaders>(m_webglDebugShaders);
  registerExtension<WebGLGetBufferSubDataAsync>(m_webglGetBufferSubDataAsync,
                                                DraftExtension);
  registerExtension<WebGLLoseContext>(m_webglLoseContext);
}

DEFINE_TRACE(WebGL2RenderingContext) {
  visitor->trace(m_extColorBufferFloat);
  visitor->trace(m_extDisjointTimerQueryWebGL2);
  visitor->trace(m_extTextureFilterAnisotropic);
  visitor->trace(m_oesTextureFloatLinear);
  visitor->trace(m_webglCompressedTextureASTC);
  visitor->trace(m_webglCompressedTextureATC);
  visitor->trace(m_webglCompressedTextureETC);
  visitor->trace(m_webglCompressedTextureETC1);
  visitor->trace(m_webglCompressedTexturePVRTC);
  visitor->trace(m_webglCompressedTextureS3TC);
  visitor->trace(m_webglCompressedTextureS3TCsRGB);
  visitor->trace(m_webglDebugRendererInfo);
  visitor->trace(m_webglDebugShaders);
  visitor->trace(m_webglGetBufferSubDataAsync);
  visitor->trace(m_webglLoseContext);
  WebGL2RenderingContextBase::trace(visitor);
}

DEFINE_TRACE_WRAPPERS(WebGL2RenderingContext) {
  // Extensions are managed by WebGL2RenderingContextBase.
  WebGL2RenderingContextBase::traceWrappers(visitor);
}

}  // namespace blink