1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.content.browser;
6 
7 import org.chromium.content.browser.webcontents.WebContentsImpl;
8 import org.chromium.content_public.browser.RenderCoordinates;
9 import org.chromium.content_public.browser.WebContents;
10 
11 /**
12  * Cached copy of all positions and scales (CSS-to-DIP-to-physical pixels)
13  * reported from the renderer.
14  * Provides wrappers and a utility class to help with coordinate transforms on the client side.
15  * Provides the internally-visible set of update methods (called from GestureListenerManagerImpl).
16  *
17  * Unless stated otherwise, all coordinates are in CSS (document) coordinate space.
18  */
19 public class RenderCoordinatesImpl implements RenderCoordinates {
20     // Scroll offset from the native in CSS.
21     private float mScrollXCss;
22     private float mScrollYCss;
23 
24     // Content size from native in CSS.
25     private float mContentWidthCss;
26     private float mContentHeightCss;
27 
28     // Last-frame render-reported viewport size in CSS.
29     private float mLastFrameViewportWidthCss;
30     private float mLastFrameViewportHeightCss;
31 
32     // Cached page scale factor from native.
33     private float mPageScaleFactor = 1.0f;
34     private float mMinPageScaleFactor = 1.0f;
35     private float mMaxPageScaleFactor = 1.0f;
36 
37     // Cached device density.
38     private float mDeviceScaleFactor = 1.0f;
39 
40     private float mTopContentOffsetYPix;
41 
fromWebContents(WebContents webContents)42     public static RenderCoordinatesImpl fromWebContents(WebContents webContents) {
43         return ((WebContentsImpl) webContents).getRenderCoordinates();
44     }
45 
46     // Internally-visible set of update methods (used by WebContentsImpl).
reset()47     public void reset() {
48         mScrollXCss = mScrollYCss = 0;
49         mPageScaleFactor = 1.0f;
50     }
51 
52     @Override
getScrollXPixInt()53     public int getScrollXPixInt() {
54         return (int) Math.floor(getScrollXPix());
55     }
56 
57     @Override
getScrollYPixInt()58     public int getScrollYPixInt() {
59         return (int) Math.floor(getScrollYPix());
60     }
61 
62     @Override
getContentOffsetYPixInt()63     public int getContentOffsetYPixInt() {
64         return (int) Math.floor(getContentOffsetYPix());
65     }
66 
67     @Override
getContentWidthPixInt()68     public int getContentWidthPixInt() {
69         return (int) Math.ceil(getContentWidthPix());
70     }
71 
72     @Override
getContentHeightPixInt()73     public int getContentHeightPixInt() {
74         return (int) Math.ceil(getContentHeightPix());
75     }
76 
77     @Override
getLastFrameViewportWidthPixInt()78     public int getLastFrameViewportWidthPixInt() {
79         return (int) Math.ceil(getLastFrameViewportWidthPix());
80     }
81 
82     @Override
getLastFrameViewportHeightPixInt()83     public int getLastFrameViewportHeightPixInt() {
84         return (int) Math.ceil(getLastFrameViewportHeightPix());
85     }
86 
87     @Override
getMaxVerticalScrollPixInt()88     public int getMaxVerticalScrollPixInt() {
89         return (int) Math.floor(getMaxVerticalScrollPix());
90     }
91 
92     @Override
getMaxHorizontalScrollPixInt()93     public int getMaxHorizontalScrollPixInt() {
94         return (int) Math.floor(getMaxHorizontalScrollPix());
95     }
96 
updateContentSizeCss(float contentWidthCss, float contentHeightCss)97     void updateContentSizeCss(float contentWidthCss, float contentHeightCss) {
98         mContentWidthCss = contentWidthCss;
99         mContentHeightCss = contentHeightCss;
100     }
101 
setDeviceScaleFactor(float dipScale)102     public void setDeviceScaleFactor(float dipScale) {
103         mDeviceScaleFactor = dipScale;
104     }
105 
updateFrameInfo(float contentWidthCss, float contentHeightCss, float viewportWidthCss, float viewportHeightCss, float minPageScaleFactor, float maxPageScaleFactor, float contentOffsetYPix)106     public void updateFrameInfo(float contentWidthCss, float contentHeightCss,
107             float viewportWidthCss, float viewportHeightCss, float minPageScaleFactor,
108             float maxPageScaleFactor, float contentOffsetYPix) {
109         mMinPageScaleFactor = minPageScaleFactor;
110         mMaxPageScaleFactor = maxPageScaleFactor;
111         mTopContentOffsetYPix = contentOffsetYPix;
112 
113         updateContentSizeCss(contentWidthCss, contentHeightCss);
114         mLastFrameViewportWidthCss = viewportWidthCss;
115         mLastFrameViewportHeightCss = viewportHeightCss;
116     }
117 
updateScrollInfo(float pageScaleFactor, float scrollXCss, float scrollYCss)118     public void updateScrollInfo(float pageScaleFactor, float scrollXCss, float scrollYCss) {
119         mPageScaleFactor = pageScaleFactor;
120         mScrollXCss = scrollXCss;
121         mScrollYCss = scrollYCss;
122     }
123 
124     /**
125      * @return Horizontal scroll offset in CSS pixels.
126      */
getScrollX()127     public float getScrollX() {
128         return mScrollXCss;
129     }
130 
131     /**
132      * @return Vertical scroll offset in CSS pixels.
133      */
getScrollY()134     public float getScrollY() {
135         return mScrollYCss;
136     }
137 
138     /**
139      * @return Horizontal scroll offset in physical pixels.
140      */
getScrollXPix()141     public float getScrollXPix() {
142         return fromLocalCssToPix(mScrollXCss);
143     }
144 
145     /**
146      * @return Vertical scroll offset in physical pixels.
147      */
getScrollYPix()148     public float getScrollYPix() {
149         return fromLocalCssToPix(mScrollYCss);
150     }
151 
152     /**
153      * @return Width of the content in CSS pixels.
154      */
getContentWidthCss()155     public float getContentWidthCss() {
156         return mContentWidthCss;
157     }
158 
159     /**
160      * @return Height of the content in CSS pixels.
161      */
getContentHeightCss()162     public float getContentHeightCss() {
163         return mContentHeightCss;
164     }
165 
166     /**
167      * @return The Physical on-screen Y offset amount below the browser controls.
168      */
getContentOffsetYPix()169     public float getContentOffsetYPix() {
170         return mTopContentOffsetYPix;
171     }
172 
173     /**
174      * @return Current page scale factor (maps CSS pixels to DIP pixels).
175      */
getPageScaleFactor()176     public float getPageScaleFactor() {
177         return mPageScaleFactor;
178     }
179 
180     /**
181      * @return Minimum page scale factor to be used with the content.
182      */
getMinPageScaleFactor()183     public float getMinPageScaleFactor() {
184         return mMinPageScaleFactor;
185     }
186 
187     /**
188      * @return Maximum page scale factor to be used with the content.
189      */
getMaxPageScaleFactor()190     public float getMaxPageScaleFactor() {
191         return mMaxPageScaleFactor;
192     }
193 
194     /**
195      * @return Current device scale factor (maps DIP pixels to physical pixels).
196      */
getDeviceScaleFactor()197     public float getDeviceScaleFactor() {
198         return mDeviceScaleFactor;
199     }
200 
201     /**
202      * @return Local CSS converted to physical coordinates.
203      */
fromLocalCssToPix(float css)204     public float fromLocalCssToPix(float css) {
205         return css * mPageScaleFactor * mDeviceScaleFactor;
206     }
207 
208     // Private methods
209 
210     // Approximate width of the content in physical pixels.
getContentWidthPix()211     private float getContentWidthPix() {
212         return fromLocalCssToPix(mContentWidthCss);
213     }
214 
215     // Approximate height of the content in physical pixels.
getContentHeightPix()216     private float getContentHeightPix() {
217         return fromLocalCssToPix(mContentHeightCss);
218     }
219 
220     // Render-reported width of the viewport in physical pixels (approximate).
getLastFrameViewportWidthPix()221     private float getLastFrameViewportWidthPix() {
222         return fromLocalCssToPix(mLastFrameViewportWidthCss);
223     }
224 
225     // Render-reported height of the viewport in physical pixels (approximate).
getLastFrameViewportHeightPix()226     private float getLastFrameViewportHeightPix() {
227         return fromLocalCssToPix(mLastFrameViewportHeightCss);
228     }
229 
230     // Maximum possible horizontal scroll in physical pixels.
getMaxHorizontalScrollPix()231     private float getMaxHorizontalScrollPix() {
232         return getContentWidthPix() - getLastFrameViewportWidthPix();
233     }
234 
235     // Maximum possible vertical scroll in physical pixels.
getMaxVerticalScrollPix()236     private float getMaxVerticalScrollPix() {
237         return getContentHeightPix() - getLastFrameViewportHeightPix();
238     }
239 }
240