115
115
#include " WebGLVertexArrayObject.h"
116
116
#include " WebGLVertexArrayObjectOES.h"
117
117
#include " WebXRSystem.h"
118
+ #include " WorkerClient.h"
119
+ #include " WorkerGlobalScope.h"
118
120
#include < JavaScriptCore/ConsoleMessage.h>
119
121
#include < JavaScriptCore/JSCInlines.h>
120
122
#include < JavaScriptCore/ScriptCallStack.h>
@@ -482,6 +484,22 @@ static void removeActiveContext(WebGLRenderingContextBase& context)
482
484
ASSERT_UNUSED (didContain, didContain);
483
485
}
484
486
487
+ static GraphicsClient* getGraphicsClient (CanvasBase& canvas)
488
+ {
489
+ if (auto * canvasElement = dynamicDowncast<HTMLCanvasElement>(canvas)) {
490
+ Document& document = canvasElement->document ();
491
+ RefPtr<Frame> frame = document.frame ();
492
+ if (!frame)
493
+ return nullptr ;
494
+
495
+ return document.view ()->root ()->hostWindow ();
496
+ }
497
+ if (is<WorkerGlobalScope>(canvas.scriptExecutionContext ()))
498
+ return downcast<WorkerGlobalScope>(canvas.scriptExecutionContext ())->workerClient ();
499
+
500
+ return nullptr ;
501
+ }
502
+
485
503
std::unique_ptr<WebGLRenderingContextBase> WebGLRenderingContextBase::create (CanvasBase& canvas, WebGLContextAttributes& attributes, WebGLVersion type)
486
504
{
487
505
auto scriptExecutionContext = canvas.scriptExecutionContext ();
@@ -496,54 +514,53 @@ std::unique_ptr<WebGLRenderingContextBase> WebGLRenderingContextBase::create(Can
496
514
UNUSED_PARAM (type);
497
515
#endif
498
516
499
- HostWindow* hostWindow = nullptr ;
517
+ GraphicsClient* graphicsClient = getGraphicsClient (canvas) ;
500
518
501
519
auto * canvasElement = dynamicDowncast<HTMLCanvasElement>(canvas);
502
520
521
+ if (!scriptExecutionContext->settingsValues ().webGLEnabled ) {
522
+ canvasElement->dispatchEvent (WebGLContextEvent::create (eventNames ().webglcontextcreationerrorEvent ,
523
+ Event::CanBubble::No, Event::IsCancelable::Yes, " Web page was not allowed to create a WebGL context." _s));
524
+ return nullptr ;
525
+ }
526
+
527
+ if (scriptExecutionContext->settingsValues ().forceWebGLUsesLowPower ) {
528
+ if (attributes.powerPreference == GraphicsContextGLPowerPreference::HighPerformance)
529
+ LOG (WebGL, " Overriding powerPreference from high-performance to low-power." );
530
+ attributes.powerPreference = GraphicsContextGLPowerPreference::LowPower;
531
+ }
532
+
503
533
if (canvasElement) {
504
534
Document& document = canvasElement->document ();
505
535
RefPtr<Frame> frame = document.frame ();
506
536
if (!frame)
507
537
return nullptr ;
508
538
509
- if (!frame->settings ().webGLEnabled ()) {
510
- canvasElement->dispatchEvent (WebGLContextEvent::create (eventNames ().webglcontextcreationerrorEvent ,
511
- Event::CanBubble::No, Event::IsCancelable::Yes, " Web page was not allowed to create a WebGL context." _s));
512
- return nullptr ;
513
- }
514
-
515
539
Document& topDocument = document.topDocument ();
516
540
Page* page = topDocument.page ();
517
- if (frame->settings ().forceWebGLUsesLowPower ()) {
518
- if (attributes.powerPreference == GraphicsContextGLPowerPreference::HighPerformance)
519
- LOG (WebGL, " Overriding powerPreference from high-performance to low-power." );
520
- attributes.powerPreference = GraphicsContextGLPowerPreference::LowPower;
521
- }
522
-
523
541
if (page)
524
542
attributes.devicePixelRatio = page->deviceScaleFactor ();
525
-
526
- hostWindow = document.view ()->root ()->hostWindow ();
527
543
}
528
544
545
+ // FIXME: Should we try get the devicePixelRatio for workers for the page that created
546
+ // the worker? What if it's a shared worker, and there's multiple answers?
547
+
529
548
attributes.noExtensions = true ;
530
549
attributes.shareResources = false ;
531
550
attributes.initialPowerPreference = attributes.powerPreference ;
532
551
attributes.webGLVersion = type;
533
552
#if PLATFORM(MAC)
534
553
// FIXME: Add MACCATALYST support for gpuIDForDisplay.
535
- if (hostWindow )
536
- attributes.windowGPUID = gpuIDForDisplay (hostWindow ->displayID ());
554
+ if (graphicsClient )
555
+ attributes.windowGPUID = gpuIDForDisplay (graphicsClient ->displayID ());
537
556
#endif
538
557
#if PLATFORM(COCOA)
539
558
attributes.useMetal = scriptExecutionContext->settingsValues ().webGLUsingMetal ;
540
559
#endif
541
560
542
561
RefPtr<GraphicsContextGL> context;
543
- if (hostWindow)
544
- context = hostWindow->createGraphicsContextGL (attributes);
545
- // FIXME: OffscreenCanvas does not support GPU process and ANGLE does not support
546
- // multi-threaded access so offscreen canvas is disabled.
562
+ if (graphicsClient)
563
+ context = graphicsClient->createGraphicsContextGL (attributes);
547
564
if (!context) {
548
565
if (canvasElement) {
549
566
canvasElement->dispatchEvent (WebGLContextEvent::create (eventNames ().webglcontextcreationerrorEvent ,
@@ -5506,28 +5523,18 @@ void WebGLRenderingContextBase::maybeRestoreContext()
5506
5523
return ;
5507
5524
}
5508
5525
5509
- auto * canvas = htmlCanvas ();
5510
- if (!canvas)
5511
- return ;
5512
-
5513
- RefPtr<Frame> frame = canvas->document ().frame ();
5514
- if (!frame)
5526
+ auto scriptExecutionContext = canvasBase ().scriptExecutionContext ();
5527
+ if (!scriptExecutionContext)
5515
5528
return ;
5516
5529
5517
- if (!frame-> settings ().webGLEnabled () )
5530
+ if (!scriptExecutionContext-> settingsValues ().webGLEnabled )
5518
5531
return ;
5519
5532
5520
- RefPtr<FrameView> view = frame->view ();
5521
- if (!view)
5522
- return ;
5523
- RefPtr<ScrollView> root = view->root ();
5524
- if (!root)
5525
- return ;
5526
- HostWindow* hostWindow = root->hostWindow ();
5527
- if (!hostWindow)
5533
+ GraphicsClient* graphicsClient = getGraphicsClient (canvasBase ());
5534
+ if (!graphicsClient)
5528
5535
return ;
5529
5536
5530
- RefPtr<GraphicsContextGL> context = hostWindow ->createGraphicsContextGL (m_attributes);
5537
+ RefPtr<GraphicsContextGL> context = graphicsClient ->createGraphicsContextGL (m_attributes);
5531
5538
if (!context) {
5532
5539
if (m_contextLostState->mode == RealLostContext)
5533
5540
m_restoreTimer.startOneShot (secondsBetweenRestoreAttempts);
@@ -5541,6 +5548,11 @@ void WebGLRenderingContextBase::maybeRestoreContext()
5541
5548
m_contextLostState = std::nullopt;
5542
5549
setupFlags ();
5543
5550
initializeNewContext ();
5551
+
5552
+ auto * canvas = htmlCanvas ();
5553
+ if (!canvas)
5554
+ return ;
5555
+
5544
5556
if (!isContextLost ())
5545
5557
canvas->dispatchEvent (WebGLContextEvent::create (eventNames ().webglcontextrestoredEvent , Event::CanBubble::No, Event::IsCancelable::Yes, emptyString ()));
5546
5558
}
0 commit comments