Debian Codegraph @

android/source/src/java/org/mozilla/gecko/gfx/DisplayPortCalculator.java

raw
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 package org.mozilla.gecko.gfx;
7
8 import android.graphics.PointF;
9 import android.graphics.RectF;
10 import android.util.Log;
11
12 import org.libreoffice.LOKitShell;
13 import org.libreoffice.LibreOfficeMainActivity;
14 import org.mozilla.gecko.util.FloatUtils;
15
16 final class DisplayPortCalculator {
17 private static final String LOGTAG = DisplayPortCalculator.class.getSimpleName();
18 private static final PointF ZERO_VELOCITY = new PointF(0, 0);
19
20 // Keep this in sync with the TILEDLAYERBUFFER_TILE_SIZE defined in gfx/layers/TiledLayerBuffer.h
21 private static final int TILE_SIZE = 256;
22
23 private final DisplayPortStrategy sStrategy;
24
25 DisplayPortCalculator(LibreOfficeMainActivity context) {
26 sStrategy = new VelocityBiasStrategy(context);
27 }
28
29 DisplayPortMetrics calculate(ImmutableViewportMetrics metrics, PointF velocity) {
30 return sStrategy.calculate(metrics, (velocity == null ? ZERO_VELOCITY : velocity));
31 }
32
33 boolean aboutToCheckerboard(ImmutableViewportMetrics metrics, PointF velocity, DisplayPortMetrics displayPort) {
34 if (displayPort == null) {
35 return true;
36 }
37 return sStrategy.aboutToCheckerboard(metrics, (velocity == null ? ZERO_VELOCITY : velocity), displayPort);
38 }
39
40 private static abstract class DisplayPortStrategy {
41 /** Calculates a displayport given a viewport and panning velocity. */
42 public abstract DisplayPortMetrics calculate(ImmutableViewportMetrics metrics, PointF velocity);
43 /** Returns true if a checkerboard is about to be visible and we should not throttle drawing. */
44 public abstract boolean aboutToCheckerboard(ImmutableViewportMetrics metrics, PointF velocity, DisplayPortMetrics displayPort);
45 }
46
47 /**
48 * Expand the given margins such that when they are applied on the viewport, the resulting rect
49 * does not have any partial tiles, except when it is clipped by the page bounds. This assumes
50 * the tiles are TILE_SIZE by TILE_SIZE and start at the origin, such that there will always be
51 * a tile at (0,0)-(TILE_SIZE,TILE_SIZE)).
52 */
53 private static DisplayPortMetrics getTileAlignedDisplayPortMetrics(RectF margins, ImmutableViewportMetrics metrics) {
54 float left = metrics.viewportRectLeft - margins.left;
55 float top = metrics.viewportRectTop - margins.top;
56 float right = metrics.viewportRectRight + margins.right;
57 float bottom = metrics.viewportRectBottom + margins.bottom;
58 left = (float) Math.max(metrics.pageRectLeft, TILE_SIZE * Math.floor(left / TILE_SIZE));
59 top = (float) Math.max(metrics.pageRectTop, TILE_SIZE * Math.floor(top / TILE_SIZE));
60 right = (float) Math.min(metrics.pageRectRight, TILE_SIZE * Math.ceil(right / TILE_SIZE));
61 bottom = (float) Math.min(metrics.pageRectBottom, TILE_SIZE * Math.ceil(bottom / TILE_SIZE));
62 return new DisplayPortMetrics(left, top, right, bottom);
63 }
64
65 /**
66 * Adjust the given margins so if they are applied on the viewport in the metrics, the resulting rect
67 * does not exceed the page bounds. This code will maintain the total margin amount for a given axis;
68 * it assumes that margins.left + metrics.getWidth() + margins.right is less than or equal to
69 * metrics.getPageWidth(); and the same for the y axis.
70 */
71 private static RectF shiftMarginsForPageBounds(RectF margins, ImmutableViewportMetrics metrics) {
72 // check how much we're overflowing in each direction. note that at most one of leftOverflow
73 // and rightOverflow can be greater than zero, and at most one of topOverflow and bottomOverflow
74 // can be greater than zero, because of the assumption described in the method javadoc.
75 float leftOverflow = metrics.pageRectLeft - (metrics.viewportRectLeft - margins.left);
76 float rightOverflow = (metrics.viewportRectRight + margins.right) - metrics.pageRectRight;
77 float topOverflow = metrics.pageRectTop - (metrics.viewportRectTop - margins.top);
78 float bottomOverflow = (metrics.viewportRectBottom + margins.bottom) - metrics.pageRectBottom;
79
80 // if the margins overflow the page bounds, shift them to other side on the same axis
81 if (leftOverflow > 0) {
82 margins.left -= leftOverflow;
83 margins.right += leftOverflow;
84 } else if (rightOverflow > 0) {
85 margins.right -= rightOverflow;
86 margins.left += rightOverflow;
87 }
88 if (topOverflow > 0) {
89 margins.top -= topOverflow;
90 margins.bottom += topOverflow;
91 } else if (bottomOverflow > 0) {
92 margins.bottom -= bottomOverflow;
93 margins.top += bottomOverflow;
94 }
95 return margins;
96 }
97
98 /**
99 * This class implements the variation with a small fixed-size margin with velocity bias.
100 * In this variation, the default margins are pretty small relative to the view size, but
101 * they are affected by the panning velocity. Specifically, if we are panning on one axis,
102 * we remove the margins on the other axis because we are likely axis-locked. Also once
103 * we are panning in one direction above a certain threshold velocity, we shift the buffer
104 * so that it is almost entirely in the direction of the pan, with a little bit in the
105 * reverse direction.
106 */
107 private static class VelocityBiasStrategy extends DisplayPortStrategy {
108 // The length of each axis of the display port will be the corresponding view length
109 // multiplied by this factor.
110 private final float SIZE_MULTIPLIER;
111 // The velocity above which we apply the velocity bias
112 private final float VELOCITY_THRESHOLD;
113 // How much of the buffer to keep in the reverse direction of the velocity
114 private final float REVERSE_BUFFER;
115 // If the visible rect is within the danger zone we start redrawing to minimize
116 // checkerboarding. the danger zone amount is a linear function of the form:
117 // viewportsize * (base + velocity * incr)
118 // where base and incr are configurable values.
119 private final float DANGER_ZONE_BASE_X_MULTIPLIER;
120 private final float DANGER_ZONE_BASE_Y_MULTIPLIER;
121 private final float DANGER_ZONE_INCR_X_MULTIPLIER;
122 private final float DANGER_ZONE_INCR_Y_MULTIPLIER;
123
124 VelocityBiasStrategy(LibreOfficeMainActivity context) {
125 SIZE_MULTIPLIER = 2.0f;
126 VELOCITY_THRESHOLD = LOKitShell.getDpi(context) * 0.032f;
127 REVERSE_BUFFER = 0.2f;
128 DANGER_ZONE_BASE_X_MULTIPLIER = 1.0f;
129 DANGER_ZONE_BASE_Y_MULTIPLIER = 1.0f;
130 DANGER_ZONE_INCR_X_MULTIPLIER = 0;
131 DANGER_ZONE_INCR_Y_MULTIPLIER = 0;
132 }
133
134 /**
135 * Split the given amounts into margins based on the VELOCITY_THRESHOLD and REVERSE_BUFFER values.
136 * If the velocity is above the VELOCITY_THRESHOLD on an axis, split the amount into REVERSE_BUFFER
137 * and 1.0 - REVERSE_BUFFER fractions. The REVERSE_BUFFER fraction is set as the margin in the
138 * direction opposite to the velocity, and the remaining fraction is set as the margin in the direction
139 * of the velocity. If the velocity is lower than VELOCITY_THRESHOLD, split the amount evenly into the
140 * two margins on that axis.
141 */
142 private RectF velocityBiasedMargins(float xAmount, float yAmount, PointF velocity) {
143 RectF margins = new RectF();
144
145 if (velocity.x > VELOCITY_THRESHOLD) {
146 margins.left = xAmount * REVERSE_BUFFER;
147 } else if (velocity.x < -VELOCITY_THRESHOLD) {
148 margins.left = xAmount * (1.0f - REVERSE_BUFFER);
149 } else {
150 margins.left = xAmount / 2.0f;
151 }
152 margins.right = xAmount - margins.left;
153
154 if (velocity.y > VELOCITY_THRESHOLD) {
155 margins.top = yAmount * REVERSE_BUFFER;
156 } else if (velocity.y < -VELOCITY_THRESHOLD) {
157 margins.top = yAmount * (1.0f - REVERSE_BUFFER);
158 } else {
159 margins.top = yAmount / 2.0f;
160 }
161 margins.bottom = yAmount - margins.top;
162
163 return margins;
164 }
165
166 public DisplayPortMetrics calculate(ImmutableViewportMetrics metrics, PointF velocity) {
167 float displayPortWidth = metrics.getWidth() * SIZE_MULTIPLIER;
168 float displayPortHeight = metrics.getHeight() * SIZE_MULTIPLIER;
169
170 // but if we're panning on one axis, set the margins for the other axis to zero since we are likely
171 // axis locked and won't be displaying that extra area.
172 if (Math.abs(velocity.x) > VELOCITY_THRESHOLD && FloatUtils.fuzzyEquals(velocity.y, 0)) {
173 displayPortHeight = metrics.getHeight();
174 } else if (Math.abs(velocity.y) > VELOCITY_THRESHOLD && FloatUtils.fuzzyEquals(velocity.x, 0)) {
175 displayPortWidth = metrics.getWidth();
176 }
177
178 // we need to avoid having a display port that is larger than the page, or we will end up
179 // painting things outside the page bounds (bug 729169).
180 displayPortWidth = Math.min(displayPortWidth, metrics.getPageWidth());
181 displayPortHeight = Math.min(displayPortHeight, metrics.getPageHeight());
182 float horizontalBuffer = displayPortWidth - metrics.getWidth();
183 float verticalBuffer = displayPortHeight - metrics.getHeight();
184
185 // split the buffer amounts into margins based on velocity, and shift it to
186 // take into account the page bounds
187 RectF margins = velocityBiasedMargins(horizontalBuffer, verticalBuffer, velocity);
188 margins = shiftMarginsForPageBounds(margins, metrics);
189
190 return getTileAlignedDisplayPortMetrics(margins, metrics);
191 }
192
193 public boolean aboutToCheckerboard(ImmutableViewportMetrics metrics, PointF velocity, DisplayPortMetrics displayPort) {
194 // calculate the danger zone amounts based on the prefs
195 float dangerZoneX = metrics.getWidth() * (DANGER_ZONE_BASE_X_MULTIPLIER + (velocity.x * DANGER_ZONE_INCR_X_MULTIPLIER));
196 float dangerZoneY = metrics.getHeight() * (DANGER_ZONE_BASE_Y_MULTIPLIER + (velocity.y * DANGER_ZONE_INCR_Y_MULTIPLIER));
197 // clamp it such that when added to the viewport, they don't exceed page size.
198 // this is a prerequisite to calling shiftMarginsForPageBounds as we do below.
199 dangerZoneX = Math.min(dangerZoneX, metrics.getPageWidth() - metrics.getWidth());
200 dangerZoneY = Math.min(dangerZoneY, metrics.getPageHeight() - metrics.getHeight());
201
202 // split the danger zone into margins based on velocity, and ensure it doesn't exceed
203 // page bounds.
204 RectF dangerMargins = velocityBiasedMargins(dangerZoneX, dangerZoneY, velocity);
205 dangerMargins = shiftMarginsForPageBounds(dangerMargins, metrics);
206
207 // we're about to checkerboard if the current viewport area + the danger zone margins
208 // fall out of the current displayport anywhere.
209 RectF adjustedViewport = new RectF(
210 metrics.viewportRectLeft - dangerMargins.left,
211 metrics.viewportRectTop - dangerMargins.top,
212 metrics.viewportRectRight + dangerMargins.right,
213 metrics.viewportRectBottom + dangerMargins.bottom);
214 return !displayPort.contains(adjustedViewport);
215 }
216
217 @Override
218 public String toString() {
219 return "VelocityBiasStrategy mult=" + SIZE_MULTIPLIER + ", threshold=" + VELOCITY_THRESHOLD + ", reverse=" + REVERSE_BUFFER
220 + ", dangerBaseX=" + DANGER_ZONE_BASE_X_MULTIPLIER + ", dangerBaseY=" + DANGER_ZONE_BASE_Y_MULTIPLIER
221 + ", dangerIncrX=" + DANGER_ZONE_INCR_Y_MULTIPLIER + ", dangerIncrY=" + DANGER_ZONE_INCR_Y_MULTIPLIER;
222 }
223 }
224 }