Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(697)

Unified Diff: Source/core/html/parser/HTMLSrcsetParser.cpp

Issue 667763004: Srcset resource selection use a geometric mean to determine resource. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Review nits Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/html/parser/HTMLSrcsetParser.cpp
diff --git a/Source/core/html/parser/HTMLSrcsetParser.cpp b/Source/core/html/parser/HTMLSrcsetParser.cpp
index 89b39e65d98b77ba0333b4a20cc6c342a01c2fdf..96a5deaca8eb01f34be8deeae41f001bf7708a64 100644
--- a/Source/core/html/parser/HTMLSrcsetParser.cpp
+++ b/Source/core/html/parser/HTMLSrcsetParser.cpp
@@ -321,6 +321,39 @@ static void parseImageCandidatesFromSrcsetAttribute(const String& attribute, Vec
parseImageCandidatesFromSrcsetAttribute<UChar>(attribute, attribute.characters16(), attribute.length(), imageCandidates, document);
}
+static int selectionLogic(Vector<ImageCandidate>& imageCandidates, float deviceScaleFactor, bool ignoreSrc)
+{
+ unsigned i = 0;
+
+ for (; i < imageCandidates.size() - 1; ++i) {
+ unsigned next = i + 1;
+ float nextDensity;
+ float currentDensity;
+ float geometricMean;
+ if (ignoreSrc) {
+ if (imageCandidates[i].srcOrigin())
+ continue;
+ if (imageCandidates[next].srcOrigin()) {
+ ++next;
+ if (next >= imageCandidates.size())
+ break;
+ ASSERT(!imageCandidates[next].srcOrigin());
+ }
+ }
+
+ nextDensity = imageCandidates[next].density();
+ if (nextDensity < deviceScaleFactor)
+ continue;
+
+ currentDensity = imageCandidates[i].density();
+ geometricMean = sqrt(currentDensity * nextDensity);
+ if (deviceScaleFactor >= geometricMean)
+ return next;
+ break;
+ }
+ return i;
+}
+
static ImageCandidate pickBestImageCandidate(float deviceScaleFactor, float sourceSize, Vector<ImageCandidate>& imageCandidates)
{
const float defaultDensityValue = 1.0;
@@ -340,16 +373,9 @@ static ImageCandidate pickBestImageCandidate(float deviceScaleFactor, float sour
std::stable_sort(imageCandidates.begin(), imageCandidates.end(), compareByDensity);
- unsigned i;
- for (i = 0; i < imageCandidates.size() - 1; ++i) {
- if ((imageCandidates[i].density() >= deviceScaleFactor) && (!ignoreSrc || !imageCandidates[i].srcOrigin()))
- break;
- }
+ unsigned i = selectionLogic(imageCandidates, deviceScaleFactor, ignoreSrc);
+ ASSERT(i < imageCandidates.size());
- if (imageCandidates[i].srcOrigin() && ignoreSrc) {
- ASSERT(i > 0);
- --i;
- }
float winningDensity = imageCandidates[i].density();
unsigned winner = i;

Powered by Google App Engine
This is Rietveld 408576698