Commit 952b1a87 authored by Liquan(Max) Gu's avatar Liquan(Max) Gu Committed by Commit Bot

[FCP++] Refactor Image Paint Detector

This CL refactors the codes of Image Paint Detecor. This will make it easier to
support background image for the metric.

Specifically, this CL will do the following things:
1. Move GetImageUrl() into an anonymous function.
2. Simplify the logic of IsJustLoaded(). Take out the IsLoad() part from it and move
it into an anonymous function.

Background image support will be done by extending these two functions.

Bug: 869924
Change-Id: Idfe4794612e1d5daa71afb03f8191700ce3d65bb
Reviewed-on: https://chromium-review.googlesource.com/c/1333990Reviewed-by: default avatarSteve Kobes <skobes@chromium.org>
Commit-Queue: Liquan (Max) Gǔ <maxlg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#607682}
parent d6a18004
......@@ -8,6 +8,7 @@
#include "third_party/blink/renderer/core/inspector/identifiers_factory.h"
#include "third_party/blink/renderer/core/layout/layout_image.h"
#include "third_party/blink/renderer/core/layout/layout_image_resource.h"
#include "third_party/blink/renderer/core/layout/layout_video.h"
#include "third_party/blink/renderer/core/layout/layout_view.h"
#include "third_party/blink/renderer/core/layout/svg/layout_svg_image.h"
#include "third_party/blink/renderer/core/page/chrome_client.h"
......@@ -21,6 +22,44 @@
namespace blink {
namespace {
String GetImageUrl(const LayoutObject& object) {
if (object.IsImage()) {
const ImageResourceContent* cached_image =
ToLayoutImage(&object)->CachedImage();
return cached_image ? cached_image->Url().StrippedForUseAsReferrer() : "";
}
if (object.IsVideo()) {
const ImageResourceContent* cached_image =
ToLayoutVideo(&object)->CachedImage();
return cached_image ? cached_image->Url().StrippedForUseAsReferrer() : "";
}
DCHECK(object.IsSVGImage());
const LayoutImageResource* image_resource =
ToLayoutSVGImage(&object)->ImageResource();
const ImageResourceContent* cached_image = image_resource->CachedImage();
return cached_image ? cached_image->Url().StrippedForUseAsReferrer() : "";
}
bool IsLoaded(const LayoutObject& object) {
if (object.IsImage()) {
const ImageResourceContent* cached_image =
ToLayoutImage(&object)->CachedImage();
return cached_image ? cached_image->IsLoaded() : false;
}
if (object.IsVideo()) {
const ImageResourceContent* cached_image =
ToLayoutVideo(&object)->CachedImage();
return cached_image ? cached_image->IsLoaded() : false;
}
DCHECK(object.IsSVGImage());
const LayoutImageResource* image_resource =
ToLayoutSVGImage(&object)->ImageResource();
const ImageResourceContent* cached_image = image_resource->CachedImage();
return cached_image ? cached_image->IsLoaded() : false;
}
} // namespace
// Set a big enough limit for the number of nodes to ensure memory usage is
// capped. Exceeding such limit will deactivate the algorithm.
constexpr size_t kImageNodeNumberLimit = 5000;
......@@ -236,18 +275,6 @@ void ImagePaintTimingDetector::RecordImage(const LayoutObject& object,
if (size_zero_ids_.Contains(node_id))
return;
ImageResourceContent* cachedImg;
if (object.IsSVGImage()) {
const LayoutImageResource* imageResource =
ToLayoutSVGImage(&object)->ImageResource();
if (imageResource)
cachedImg = imageResource->CachedImage();
} else {
DCHECK(object.IsImage() || object.IsVideo());
// Both image and video can be casted to LayoutImage, since LayoutVideo
// inherits from LayoutImage.
cachedImg = ToLayoutImage(&object)->CachedImage();
}
if (!id_record_map_.Contains(node_id)) {
recorded_node_count_++;
if (recorded_node_count_ < kImageNodeNumberLimit) {
......@@ -269,8 +296,7 @@ void ImagePaintTimingDetector::RecordImage(const LayoutObject& object,
}
std::unique_ptr<ImageRecord> record = std::make_unique<ImageRecord>();
record->node_id = node_id;
record->image_url =
!cachedImg ? "" : cachedImg->Url().StrippedForUseAsReferrer();
record->image_url = GetImageUrl(object);
// Mind that first_size has to be assigned at the push of
// largest_image_heap_ since it's the sorting key.
record->first_size = rect_size;
......@@ -285,8 +311,8 @@ void ImagePaintTimingDetector::RecordImage(const LayoutObject& object,
}
}
if (id_record_map_.Contains(node_id) &&
IsJustLoaded(cachedImg, *id_record_map_.at(node_id))) {
if (id_record_map_.Contains(node_id) && !id_record_map_.at(node_id)->loaded &&
IsLoaded(object)) {
records_pending_timing_.push(node_id);
ImageRecord* record = id_record_map_.at(node_id);
record->frame_index = frame_index_;
......@@ -302,12 +328,6 @@ void ImagePaintTimingDetector::RecordImage(const LayoutObject& object,
}
}
bool ImagePaintTimingDetector::IsJustLoaded(
const ImageResourceContent* cachedImg,
const ImageRecord& record) const {
return cachedImg && cachedImg->IsLoaded() && !record.loaded;
}
ImageRecord* ImagePaintTimingDetector::FindLargestPaintCandidate() {
while (!largest_image_heap_.empty() && !largest_image_heap_.top()) {
// Discard the elements that have been removed from |id_record_map_|.
......
......@@ -21,7 +21,6 @@ class IntRect;
class LayoutObject;
class TracedValue;
class LocalFrameView;
class ImageResourceContent;
class ImageRecord : public base::SupportsWeakPtr<ImageRecord> {
public:
......@@ -88,8 +87,6 @@ class CORE_EXPORT ImagePaintTimingDetector final
void OnLargestImagePaintDetected(const ImageRecord&);
void OnLastImagePaintDetected(const ImageRecord&);
bool IsJustLoaded(const ImageResourceContent* cachedImg,
const ImageRecord&) const;
void Analyze();
base::RepeatingCallback<void(WebLayerTreeView::ReportTimeCallback)>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment