[webkit-dev] Determining the device pixel density of target device

Enrique Ocaña González eocanha at igalia.com
Wed Mar 5 03:26:40 PST 2014


El Martes, 4 de marzo de 2014 20:59:10 AgentX escribió:

> I was working with *responsive images* in Webkit and I came across this
> *‘deviceScaleFactor’* attribute with determines the pixel density on the
> target device.
> I was unable to find out how does Webkit determine it, that is which
> functions does it use and where can I find them in the Source Code? All I
> was able to find was that it used a function *‘page->deviceScaleFactor()’*
> which somehow returned the scale factor but I was unable to find the exact
> function which actually computes the scale factor.

First of all, excuse me if some answer is inaccurate. I'm not acquainted with 
that part of the code and I'm only going to expose my findings just in case 
they might be helpful for you.

TL; DR: Based on my analysis of the code, the scale factor is set by the end 
user program in EFL, got from NSWindow in Mac and never set in GTK+, where 1 
(the default) is used.

The full story:

After git grepping [1][2] the code several times, I've found that 
WebCore::Page initializes its m_deviceScaleFactor to 1 in its constructor. 
Then, other code call WebCore::Page::setDeviceScaleFactor(), but that depends 
on each particular port.

In the case of the EFL port, the actual scale factor (ratio) is set here:

WebKit/efl/ewk/ewk_view.h:EAPI Eina_Bool 
ewk_view_device_pixel_ratio_set(Evas_Object *o, float ratio);
WebKit2/UIProcess/API/efl/ewk_view.h:EAPI Eina_Bool 
ewk_view_device_pixel_ratio_set(Evas_Object *o, float ratio);

and nobody (except the tests) uses that code, it's API for the end user 
program, so we can't know who actually computes the scale factor in that port.

Similar delegation to external code seems to happen on Mac, either manually:

WebKit/mac/WebView/WebViewPrivate.h:- (void)_setCustomBackingScaleFactor:
(CGFloat)overrideScaleFactor;

...or automatically, where the scale factor is taken from NSWindow[3]:

WebKit/mac/WebView/WebView.mm:- (float)_deviceScaleFactor

Regarding WebKitGTK+, I haven't found any specific code to set the scale 
factor. Maybe they always use the default one.

There's also a field in WebPageCreationParameters which determines the scale 
factor to be used in WebKit::WebPage at creation time by the WebProcess. Now 
using Eclipse's code analysis features, I've found that it's set by the 
UIProcess based on the result of:

WebKit2/UIProcess/WebPageProxy.cpp:float WebPageProxy::deviceScaleFactor() 
const

In the end, its value depends on 
WebPageProxy::setIntrinsicDeviceScaleFactor(), apparently not used by anybody, 
and WKPageSetCustomBackingScaleFactor(), used only by the tests and by EFL's 
EwkView.ewk_view_device_pixel_ratio_set(), already mentioned before.

The final summary is actually the TL;DR section in the begining. Take it with 
a grain of salt. Of course, experts acquainted by the code have the final word 
on this analysis.

Regards.

[1] git grep deviceScaleFactor | grep -v ChangeLog
[2] git grep -e '->setDeviceScaleFactor' | grep -v ChangeLog
[3] 
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSWindow_Class/Reference/Reference.html#//apple_ref/occ/instm/NSWindow/backingScaleFactor

-- 
Enrique Ocaña González


More information about the webkit-dev mailing list