Skip to content

Commit

Permalink
Reduce use of downcast<>() in WebCore/html
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=270761

Reviewed by Darin Adler.

* Source/WebCore/html/HTMLCanvasElement.cpp:
(WebCore::HTMLCanvasElement::createContextWebGL):
(WebCore::HTMLCanvasElement::getContextWebGL):
(WebCore::HTMLCanvasElement::createContextBitmapRenderer):
(WebCore::HTMLCanvasElement::reset):
* Source/WebCore/html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::setVideoFullscreenStandby): Deleted.
* Source/WebCore/html/HTMLMediaElement.h:
(WebCore::HTMLMediaElement::videoFullscreenStandby const):
(WebCore::HTMLMediaElement::setVideoFullscreenStandbyInternal):
* Source/WebCore/html/HTMLVideoElement.cpp:
(WebCore::HTMLVideoElement::setVideoFullscreenStandby):
* Source/WebCore/html/HTMLVideoElement.h:
* Source/WebCore/html/canvas/WebGLUtilities.h:
(WebCore::ScopedWebGLRestoreFramebuffer::~ScopedWebGLRestoreFramebuffer):
* Source/WebCore/html/track/TrackEvent.cpp:
(WebCore::convertToTrackEventTrack):

Canonical link: https://commits.webkit.org/275899@main
  • Loading branch information
cdumez committed Mar 11, 2024
1 parent 8d6fab9 commit 711120e
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 54 deletions.
39 changes: 23 additions & 16 deletions Source/WebCore/html/HTMLCanvasElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,18 +433,20 @@ WebGLRenderingContextBase* HTMLCanvasElement::createContextWebGL(WebGLVersion ty

// TODO(WEBXR): ensure the context is created in a compatible graphics
// adapter when there is an active immersive device.
m_context = WebGLRenderingContextBase::create(*this, attrs, type);
if (m_context) {
auto context = WebGLRenderingContextBase::create(*this, attrs, type);
WeakPtr weakContext = context.get();
m_context = WTFMove(context);
if (weakContext) {
// Need to make sure a RenderLayer and compositing layer get created for the Canvas.
invalidateStyleAndLayerComposition();
if (renderBox())
renderBox()->contentChanged(CanvasChanged);
if (CheckedPtr box = renderBox())
box->contentChanged(CanvasChanged);
#if ENABLE(WEBXR)
ASSERT(!attrs.xrCompatible || downcast<WebGLRenderingContextBase>(m_context.get())->isXRCompatible());
ASSERT(!attrs.xrCompatible || weakContext->isXRCompatible());
#endif
}

return downcast<WebGLRenderingContextBase>(m_context.get());
return weakContext.get();
}

WebGLRenderingContextBase* HTMLCanvasElement::getContextWebGL(WebGLVersion type, WebGLContextAttributes&& attrs)
Expand All @@ -453,16 +455,17 @@ WebGLRenderingContextBase* HTMLCanvasElement::getContextWebGL(WebGLVersion type,
return nullptr;

if (m_context) {
if (!m_context->isWebGL())
auto* glContext = dynamicDowncast<WebGLRenderingContextBase>(*m_context);
if (!glContext)
return nullptr;

if ((type == WebGLVersion::WebGL1) != m_context->isWebGL1())
if ((type == WebGLVersion::WebGL1) != glContext->isWebGL1())
return nullptr;

return glContext;
}

if (!m_context)
return createContextWebGL(type, WTFMove(attrs));
return &downcast<WebGLRenderingContextBase>(*m_context);
return createContextWebGL(type, WTFMove(attrs));
}

#endif // ENABLE(WEBGL)
Expand All @@ -477,15 +480,17 @@ ImageBitmapRenderingContext* HTMLCanvasElement::createContextBitmapRenderer(cons
ASSERT_UNUSED(type, HTMLCanvasElement::isBitmapRendererType(type));
ASSERT(!m_context);

m_context = ImageBitmapRenderingContext::create(*this, WTFMove(settings));
downcast<ImageBitmapRenderingContext>(m_context.get())->transferFromImageBitmap(nullptr);
auto context = ImageBitmapRenderingContext::create(*this, WTFMove(settings));
WeakPtr weakContext = *context;
m_context = WTFMove(context);
weakContext->transferFromImageBitmap(nullptr);

#if USE(IOSURFACE_CANVAS_BACKING_STORE)
// Need to make sure a RenderLayer and compositing layer get created for the Canvas.
invalidateStyleAndLayerComposition();
#endif

return static_cast<ImageBitmapRenderingContext*>(m_context.get());
return weakContext.get();
}

ImageBitmapRenderingContext* HTMLCanvasElement::getContextBitmapRenderer(const String& type, ImageBitmapRenderingContextSettings&& settings)
Expand Down Expand Up @@ -590,8 +595,10 @@ void HTMLCanvasElement::reset()

setSurfaceSize(newSize);

if (isGPUBased() && oldSize != size())
downcast<GPUBasedCanvasRenderingContext>(*m_context).reshape(width(), height());
if (m_context && oldSize != size()) {
if (auto* context = dynamicDowncast<GPUBasedCanvasRenderingContext>(*m_context))
context->reshape(width(), height());
}

if (CheckedPtr canvasRenderer = dynamicDowncast<RenderHTMLCanvas>(renderer())) {
if (oldSize != size()) {
Expand Down
30 changes: 0 additions & 30 deletions Source/WebCore/html/HTMLMediaElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7040,36 +7040,6 @@ void HTMLMediaElement::prepareForVideoFullscreenStandby()
#endif
}

WEBCORE_EXPORT void HTMLMediaElement::setVideoFullscreenStandby(bool value)
{
ASSERT(is<HTMLVideoElement>(*this));
if (m_videoFullscreenStandby == value)
return;

if (!document().page())
return;

if (!document().page()->chrome().client().supportsVideoFullscreenStandby())
return;

m_videoFullscreenStandby = value;

#if ENABLE(VIDEO_PRESENTATION_MODE)
if (RefPtr player = m_player)
player->videoFullscreenStandbyChanged();
#endif

if (m_videoFullscreenMode != VideoFullscreenModeNone)
return;

if (m_videoFullscreenStandby)
document().protectedPage()->chrome().client().enterVideoFullscreenForVideoElement(downcast<HTMLVideoElement>(*this), VideoFullscreenModeNone, m_videoFullscreenStandby);
else
document().protectedPage()->chrome().client().exitVideoFullscreenForVideoElement(downcast<HTMLVideoElement>(*this), [this, protectedThis = Ref { *this }](auto success) mutable {
m_videoFullscreenStandby = !success;
});
}

void HTMLMediaElement::willBecomeFullscreenElement(VideoFullscreenMode mode)
{
#if PLATFORM(MAC) && ENABLE(VIDEO_PRESENTATION_MODE)
Expand Down
4 changes: 3 additions & 1 deletion Source/WebCore/html/HTMLMediaElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,6 @@ class HTMLMediaElement
WEBCORE_EXPORT void enterFullscreen() override;
WEBCORE_EXPORT void exitFullscreen();
WEBCORE_EXPORT void prepareForVideoFullscreenStandby();
WEBCORE_EXPORT void setVideoFullscreenStandby(bool);

bool hasClosedCaptions() const override;
bool closedCaptionsVisible() const override;
Expand Down Expand Up @@ -733,6 +732,9 @@ class HTMLMediaElement
void mediaPlayerQueueTaskOnEventLoop(Function<void()>&&) final;
void mediaPlayerCharacteristicChanged() final;

bool videoFullscreenStandby() const { return m_videoFullscreenStandby; }
void setVideoFullscreenStandbyInternal(bool videoFullscreenStandby) { m_videoFullscreenStandby = videoFullscreenStandby; }

private:
friend class Internals;

Expand Down
30 changes: 30 additions & 0 deletions Source/WebCore/html/HTMLVideoElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,36 @@ void HTMLVideoElement::mediaPlayerEngineUpdated()
mediaPlayerRenderingModeChanged();
}

void HTMLVideoElement::setVideoFullscreenStandby(bool value)
{
if (videoFullscreenStandby() == value)
return;

if (!document().page())
return;

if (!document().page()->chrome().client().supportsVideoFullscreenStandby())
return;

setVideoFullscreenStandbyInternal(value);

#if ENABLE(VIDEO_PRESENTATION_MODE)
if (RefPtr player = this->player())
player->videoFullscreenStandbyChanged();
#endif

if (fullscreenMode() != VideoFullscreenModeNone)
return;

if (videoFullscreenStandby())
document().protectedPage()->chrome().client().enterVideoFullscreenForVideoElement(*this, VideoFullscreenModeNone, true);
else {
document().protectedPage()->chrome().client().exitVideoFullscreenForVideoElement(*this, [this, protectedThis = Ref { *this }](auto success) mutable {
setVideoFullscreenStandbyInternal(!success);
});
}
}

} // namespace WebCore

#endif // ENABLE(VIDEO)
2 changes: 2 additions & 0 deletions Source/WebCore/html/HTMLVideoElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class HTMLVideoElement final : public HTMLMediaElement, public Supplementable<HT
unsigned requestVideoFrameCallback(Ref<VideoFrameRequestCallback>&&);
void cancelVideoFrameCallback(unsigned);

WEBCORE_EXPORT void setVideoFullscreenStandby(bool);

private:
HTMLVideoElement(const QualifiedName&, Document&, bool createdByParser);

Expand Down
7 changes: 3 additions & 4 deletions Source/WebCore/html/canvas/WebGLUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,9 @@ class ScopedWebGLRestoreFramebuffer {
~ScopedWebGLRestoreFramebuffer()
{
RefPtr gl = m_context.graphicsContextGL();
if (m_context.isWebGL2()) {
auto& context2 = downcast<WebGL2RenderingContext>(m_context);
gl->bindFramebuffer(GraphicsContextGL::READ_FRAMEBUFFER, objectOrZero(context2.m_readFramebufferBinding));
gl->bindFramebuffer(GraphicsContextGL::DRAW_FRAMEBUFFER, objectOrZero(context2.m_framebufferBinding));
if (auto* gl2Ccontext = dynamicDowncast<WebGL2RenderingContext>(m_context)) {
gl->bindFramebuffer(GraphicsContextGL::READ_FRAMEBUFFER, objectOrZero(gl2Ccontext->m_readFramebufferBinding));
gl->bindFramebuffer(GraphicsContextGL::DRAW_FRAMEBUFFER, objectOrZero(gl2Ccontext->m_framebufferBinding));
} else
gl->bindFramebuffer(GraphicsContextGL::FRAMEBUFFER, objectOrZero(m_context.m_framebufferBinding));
}
Expand Down
6 changes: 3 additions & 3 deletions Source/WebCore/html/track/TrackEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ static inline std::optional<TrackEvent::TrackEventTrack> convertToTrackEventTrac
case TrackBase::BaseTrack:
return std::nullopt;
case TrackBase::TextTrack:
return TrackEvent::TrackEventTrack { RefPtr<TextTrack>(&downcast<TextTrack>(track.get())) };
return TrackEvent::TrackEventTrack { RefPtr { uncheckedDowncast<TextTrack>(WTFMove(track)) } };
case TrackBase::AudioTrack:
return TrackEvent::TrackEventTrack { RefPtr<AudioTrack>(&downcast<AudioTrack>(track.get())) };
return TrackEvent::TrackEventTrack { RefPtr { uncheckedDowncast<AudioTrack>(WTFMove(track)) } };
case TrackBase::VideoTrack:
return TrackEvent::TrackEventTrack { RefPtr<VideoTrack>(&downcast<VideoTrack>(track.get())) };
return TrackEvent::TrackEventTrack { RefPtr { uncheckedDowncast<VideoTrack>(WTFMove(track)) } };
}

ASSERT_NOT_REACHED();
Expand Down

0 comments on commit 711120e

Please sign in to comment.