[WebIDL] WebGLContextAttributes should be a dictionary
[WebKit-https.git] / Source / WebCore / html / HTMLCanvasElement.cpp
index 673a574..e734ded 100644 (file)
@@ -170,6 +170,72 @@ void HTMLCanvasElement::setWidth(unsigned value)
     setAttributeWithoutSynchronization(widthAttr, AtomicString::number(limitToOnlyHTMLNonNegative(value, defaultWidth)));
 }
 
+static inline size_t maxActivePixelMemory()
+{
+    static size_t maxPixelMemory;
+    static std::once_flag onceFlag;
+    std::call_once(onceFlag, [] {
+        maxPixelMemory = std::max(ramSize() / 4, 2151 * MB);
+    });
+    return maxPixelMemory;
+}
+
+CanvasRenderingContext* HTMLCanvasElement::getContext(const String& type)
+{
+    if (HTMLCanvasElement::is2dType(type))
+        return getContext2d(type);
+
+#if ENABLE(WEBGL)
+    if (HTMLCanvasElement::is3dType(type))
+        return getContextWebGL(type);
+#endif
+
+    return nullptr;
+}
+
+bool HTMLCanvasElement::is2dType(const String& type)
+{
+    return type == "2d";
+}
+
+CanvasRenderingContext* HTMLCanvasElement::getContext2d(const String& type)
+{
+    ASSERT_UNUSED(HTMLCanvasElement::is2dType(type), type);
+
+    if (m_context && !m_context->is2d())
+        return nullptr;
+    if (!m_context) {
+        bool usesDashboardCompatibilityMode = false;
+#if ENABLE(DASHBOARD_SUPPORT)
+        if (Settings* settings = document().settings())
+            usesDashboardCompatibilityMode = settings->usesDashboardBackwardCompatibilityMode();
+#endif
+
+        // Make sure we don't use more pixel memory than the system can support.
+        size_t requestedPixelMemory = 4 * width() * height();
+        if (activePixelMemory + requestedPixelMemory > maxActivePixelMemory()) {
+            StringBuilder stringBuilder;
+            stringBuilder.appendLiteral("Total canvas memory use exceeds the maximum limit (");
+            stringBuilder.appendNumber(maxActivePixelMemory() / 1024 / 1024);
+            stringBuilder.appendLiteral(" MB).");
+            document().addConsoleMessage(MessageSource::JS, MessageLevel::Warning, stringBuilder.toString());
+            return nullptr;
+        }
+
+        m_context = std::make_unique<CanvasRenderingContext2D>(*this, document().inQuirksMode(), usesDashboardCompatibilityMode);
+
+        downcast<CanvasRenderingContext2D>(*m_context).setUsesDisplayListDrawing(m_usesDisplayListDrawing);
+        downcast<CanvasRenderingContext2D>(*m_context).setTracksDisplayListReplay(m_tracksDisplayListReplay);
+
+#if USE(IOSURFACE_CANVAS_BACKING_STORE) || ENABLE(ACCELERATED_2D_CANVAS)
+        // Need to make sure a RenderLayer and compositing layer get created for the Canvas
+        invalidateStyleAndLayerComposition();
+#endif
+    }
+
+    return m_context.get();
+}
+
 #if ENABLE(WEBGL)
 static bool requiresAcceleratedCompositingForWebGL()
 {
@@ -193,89 +259,36 @@ static bool shouldEnableWebGL(Settings* settings)
 
     return settings->acceleratedCompositingEnabled();
 }
-#endif
 
-static inline size_t maxActivePixelMemory()
+bool HTMLCanvasElement::is3dType(const String& type)
 {
-    static size_t maxPixelMemory;
-    static std::once_flag onceFlag;
-    std::call_once(onceFlag, [] {
-        maxPixelMemory = std::max(ramSize() / 4, 2151 * MB);
-    });
-    return maxPixelMemory;
+    // Retain support for the legacy "webkit-3d" name.
+    return type == "webgl" || type == "experimental-webgl"
+#if ENABLE(WEBGL2)
+        || type == "webgl2"
+#endif
+        || type == "webkit-3d";
 }
 
-CanvasRenderingContext* HTMLCanvasElement::getContext(const String& type, CanvasContextAttributes* attrs)
+CanvasRenderingContext* HTMLCanvasElement::getContextWebGL(const String& type, WebGLContextAttributes&& attrs)
 {
-    if (is2dType(type)) {
-        if (m_context && !m_context->is2d())
-            return nullptr;
-        if (!m_context) {
-            bool usesDashboardCompatibilityMode = false;
-#if ENABLE(DASHBOARD_SUPPORT)
-            if (Settings* settings = document().settings())
-                usesDashboardCompatibilityMode = settings->usesDashboardBackwardCompatibilityMode();
-#endif
+    ASSERT(HTMLCanvasElement::is3dType(type));
 
-            // Make sure we don't use more pixel memory than the system can support.
-            size_t requestedPixelMemory = 4 * width() * height();
-            if (activePixelMemory + requestedPixelMemory > maxActivePixelMemory()) {
-                StringBuilder stringBuilder;
-                stringBuilder.appendLiteral("Total canvas memory use exceeds the maximum limit (");
-                stringBuilder.appendNumber(maxActivePixelMemory() / 1024 / 1024);
-                stringBuilder.appendLiteral(" MB).");
-                document().addConsoleMessage(MessageSource::JS, MessageLevel::Warning, stringBuilder.toString());
-                return nullptr;
-            }
-
-            m_context = std::make_unique<CanvasRenderingContext2D>(*this, document().inQuirksMode(), usesDashboardCompatibilityMode);
+    if (!shouldEnableWebGL(document().settings()))
+        return nullptr;
 
-            downcast<CanvasRenderingContext2D>(*m_context).setUsesDisplayListDrawing(m_usesDisplayListDrawing);
-            downcast<CanvasRenderingContext2D>(*m_context).setTracksDisplayListReplay(m_tracksDisplayListReplay);
+    if (m_context && !m_context->is3d())
+        return nullptr;
 
-#if USE(IOSURFACE_CANVAS_BACKING_STORE) || ENABLE(ACCELERATED_2D_CANVAS)
+    if (!m_context) {
+        m_context = WebGLRenderingContextBase::create(*this, attrs, type);
+        if (m_context) {
             // Need to make sure a RenderLayer and compositing layer get created for the Canvas
             invalidateStyleAndLayerComposition();
-#endif
         }
-        return m_context.get();
     }
-#if ENABLE(WEBGL)
-    if (shouldEnableWebGL(document().settings())) {
-
-        if (is3dType(type)) {
-            if (m_context && !m_context->is3d())
-                return nullptr;
-            if (!m_context) {
-                m_context = WebGLRenderingContextBase::create(*this, static_cast<WebGLContextAttributes*>(attrs), type);
-                if (m_context) {
-                    // Need to make sure a RenderLayer and compositing layer get created for the Canvas
-                    invalidateStyleAndLayerComposition();
-                }
-            }
-            return m_context.get();
-        }
-    }
-#else
-    UNUSED_PARAM(attrs);
-#endif
-    return nullptr;
-}
 
-bool HTMLCanvasElement::is2dType(const String& type)
-{
-    return type == "2d";
-}
-
-#if ENABLE(WEBGL)
-bool HTMLCanvasElement::is3dType(const String& type)
-{
-    // Retain support for the legacy "webkit-3d" name.
-    return type == "webgl" || type == "experimental-webgl"
-#if ENABLE(WEBGL2)
-        || type == "webgl2"
-#endif
-        || type == "webkit-3d";
+    return m_context.get();
 }
 #endif