void FullscreenVideoController::LayerClient::platformCALayerLayoutSublayersOfLayer(PlatformCALayer* layer) { ASSERT_ARG(layer, layer == m_parent->m_rootChild); HTMLVideoElement* videoElement = m_parent->m_videoElement.get(); if (!videoElement) return; PlatformCALayer* videoLayer = PlatformCALayer::platformCALayer(videoElement->platformLayer()); if (!videoLayer || videoLayer->superlayer() != layer) return; FloatRect layerBounds = layer->bounds(); FloatSize videoSize = videoElement->player()->naturalSize(); float scaleFactor; if (videoSize.aspectRatio() > layerBounds.size().aspectRatio()) scaleFactor = layerBounds.width() / videoSize.width(); else scaleFactor = layerBounds.height() / videoSize.height(); videoSize.scale(scaleFactor); // Calculate the centered position based on the videoBounds and layerBounds: FloatPoint videoPosition; FloatPoint videoOrigin; videoOrigin.setX((layerBounds.width() - videoSize.width()) * 0.5); videoOrigin.setY((layerBounds.height() - videoSize.height()) * 0.5); videoLayer->setPosition(videoOrigin); videoLayer->setBounds(FloatRect(FloatPoint(), videoSize)); }
static void fitContextToBox(GraphicsContext* context, const FloatSize& srcImageSize, const FloatSize& dstSize) { float srcRatio = srcImageSize.aspectRatio(); float dstRatio = dstSize.aspectRatio(); float scale; float translationX = 0; float translationY = 0; if (srcRatio > dstRatio) { scale = dstSize.width() / srcImageSize.width(); translationY = (dstSize.height() - scale * srcImageSize.height()) / 2; } else { scale = dstSize.height() / srcImageSize.height(); translationX = (dstSize.width() - scale * srcImageSize.width()) / 2; } context->translate(translationX, translationY); context->scale(FloatSize(scale, scale)); }