Skip to content

Commit

Permalink
Fix HTMLVideoElement poster zoom issue
Browse files Browse the repository at this point in the history
The cached image size of the video poster has been scaled
using the style's EffectiveZoom, so it should not be scaled
again in LayoutVideo::UpdateIntrinsicSize().

Bug: 1107377
Change-Id: I57ca1ccf59531ac00aac0fbf261d3be59ede8bbc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2317487
Reviewed-by: Koji Ishii <kojii@chromium.org>
Commit-Queue: Koji Ishii <kojii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#794898}
  • Loading branch information
WangShicheng authored and Commit Bot committed Aug 5, 2020
1 parent 4d7f5e6 commit 75dc85b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 10 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,7 @@ Shen Yu <shenyu.tcv@gmail.com>
Sherry Mou <wenjinm@amazon.com>
Shez Baig <sbaig1@bloomberg.net>
Shigeki Ohtsu <shigeki.ohtsu@gmail.com>
Shicheng Wang <wangshicheng@xiaomi.com>
Shiliu Wang <aofdwsl@gmail.com>
Shiliu Wang <shiliu.wang@intel.com>
Shilpa Shri <shilpa.shri@samsung.com>
Expand Down
1 change: 1 addition & 0 deletions third_party/blink/renderer/core/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,7 @@ jumbo_source_set("unit_tests") {
"layout/layout_text_fragment_test.cc",
"layout/layout_text_test.cc",
"layout/layout_theme_test.cc",
"layout/layout_video_test.cc",
"layout/layout_view_test.cc",
"layout/line/abstract_inline_text_box_test.cc",
"layout/line/inline_text_box_test.cc",
Expand Down
31 changes: 22 additions & 9 deletions third_party/blink/renderer/core/layout/layout_video.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@

namespace blink {

namespace {

const float kInitEffectZoom = 1.0f;

} // namespace

LayoutVideo::LayoutVideo(HTMLVideoElement* video) : LayoutMedia(video) {
SetIntrinsicSize(CalculateIntrinsicSize());
SetIntrinsicSize(CalculateIntrinsicSize(kInitEffectZoom));
}

LayoutVideo::~LayoutVideo() = default;
Expand All @@ -49,8 +55,7 @@ void LayoutVideo::IntrinsicSizeChanged() {
}

void LayoutVideo::UpdateIntrinsicSize(bool is_in_layout) {
LayoutSize size = CalculateIntrinsicSize();
size.Scale(StyleRef().EffectiveZoom());
LayoutSize size = CalculateIntrinsicSize(StyleRef().EffectiveZoom());

// Never set the element size to zero when in a media document.
if (size.IsEmpty() && GetNode()->ownerDocument() &&
Expand All @@ -68,13 +73,16 @@ void LayoutVideo::UpdateIntrinsicSize(bool is_in_layout) {
}
}

LayoutSize LayoutVideo::CalculateIntrinsicSize() {
LayoutSize LayoutVideo::CalculateIntrinsicSize(float scale) {
HTMLVideoElement* video = VideoElement();
DCHECK(video);

if (RuntimeEnabledFeatures::ExperimentalProductivityFeaturesEnabled()) {
if (video->IsDefaultIntrinsicSize())
return DefaultSize();
if (video->IsDefaultIntrinsicSize()) {
LayoutSize size = DefaultSize();
size.Scale(scale);
return size;
}
}

// Spec text from 4.8.6
Expand All @@ -92,15 +100,20 @@ LayoutSize LayoutVideo::CalculateIntrinsicSize() {
if (web_media_player &&
video->getReadyState() >= HTMLVideoElement::kHaveMetadata) {
IntSize size(web_media_player->NaturalSize());
if (!size.IsEmpty())
return LayoutSize(size);
if (!size.IsEmpty()) {
LayoutSize layoutSize = LayoutSize(size);
layoutSize.Scale(scale);
return layoutSize;
}
}

if (video->IsShowPosterFlagSet() && !cached_image_size_.IsEmpty() &&
!ImageResource()->ErrorOccurred())
return cached_image_size_;

return DefaultSize();
LayoutSize size = DefaultSize();
size.Scale(scale);
return size;
}

void LayoutVideo::ImageChanged(WrappedImagePtr new_image,
Expand Down
2 changes: 1 addition & 1 deletion third_party/blink/renderer/core/layout/layout_video.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class LayoutVideo final : public LayoutMedia {
private:
void UpdateFromElement() override;

LayoutSize CalculateIntrinsicSize();
LayoutSize CalculateIntrinsicSize(float scale);
void UpdateIntrinsicSize(bool is_in_layout);

void ImageChanged(WrappedImagePtr, CanDeferInvalidation) override;
Expand Down
68 changes: 68 additions & 0 deletions third_party/blink/renderer/core/layout/layout_video_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2020 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 "third_party/blink/renderer/core/layout/layout_video.h"

#include "third_party/blink/renderer/core/layout/layout_image.h"
#include "third_party/blink/renderer/core/loader/resource/image_resource_content.h"
#include "third_party/blink/renderer/core/testing/core_unit_test_helper.h"
#include "third_party/blink/renderer/platform/graphics/unaccelerated_static_bitmap_image.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkSurface.h"

namespace blink {

class LayoutVideoTest : public RenderingTest {
public:
void CreateAndSetImage(const char* id, int width, int height) {
// Create one image with size(width, height)
sk_sp<SkColorSpace> src_rgb_color_space = SkColorSpace::MakeSRGB();
SkImageInfo raster_image_info =
SkImageInfo::MakeN32Premul(width, height, src_rgb_color_space);
sk_sp<SkSurface> surface(SkSurface::MakeRaster(raster_image_info));
sk_sp<SkImage> image = surface->makeImageSnapshot();
ImageResourceContent* image_content = ImageResourceContent::CreateLoaded(
UnacceleratedStaticBitmapImage::Create(image).get());

// Set image to video
auto* layout_image = (LayoutImage*)GetLayoutObjectByElementId(id);
layout_image->ImageResource()->SetImageResource(image_content);
}
};

TEST_F(LayoutVideoTest, PosterSizeWithNormal) {
SetBodyInnerHTML(R"HTML(
<style>
video {zoom:1}
</style>
<video id='video' />
)HTML");

CreateAndSetImage("video", 10, 10);
UpdateAllLifecyclePhasesForTest();

int width = ((LayoutBox*)GetLayoutObjectByElementId("video"))
->AbsoluteBoundingBoxRect()
.Width();
EXPECT_EQ(width, 10);
}

TEST_F(LayoutVideoTest, PosterSizeWithZoom) {
SetBodyInnerHTML(R"HTML(
<style>
video {zoom:1.5}
</style>
<video id='video' />
)HTML");

CreateAndSetImage("video", 10, 10);
UpdateAllLifecyclePhasesForTest();

int width = ((LayoutBox*)GetLayoutObjectByElementId("video"))
->AbsoluteBoundingBoxRect()
.Width();
EXPECT_EQ(width, 15);
}

} // namespace blink

0 comments on commit 75dc85b

Please sign in to comment.