Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chromoting; | 5 package org.chromium.chromoting; |
| 6 | 6 |
| 7 import android.graphics.Matrix; | 7 import android.graphics.Matrix; |
| 8 import android.graphics.PointF; | 8 import android.graphics.PointF; |
| 9 import android.graphics.Rect; | 9 import android.graphics.Rect; |
| 10 import android.graphics.RectF; | 10 import android.graphics.RectF; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 * Shifts the viewport by the passed in deltas (in image coordinates). | 43 * Shifts the viewport by the passed in deltas (in image coordinates). |
| 44 * | 44 * |
| 45 * @param useScreenCenter Determines whether to use the desired viewport pos ition or the actual | 45 * @param useScreenCenter Determines whether to use the desired viewport pos ition or the actual |
| 46 * center of the screen for positioning. | 46 * center of the screen for positioning. |
| 47 * @param deltaX The distance (in image coordinates) to move the viewport al ong the x-axis. | 47 * @param deltaX The distance (in image coordinates) to move the viewport al ong the x-axis. |
| 48 * @param deltaY The distance (in image coordinates) to move the viewport al ong the y-axis. | 48 * @param deltaY The distance (in image coordinates) to move the viewport al ong the y-axis. |
| 49 * @return A point representing the new center position of the viewport. | 49 * @return A point representing the new center position of the viewport. |
| 50 */ | 50 */ |
| 51 public PointF moveViewportCenter(boolean useScreenCenter, float deltaX, floa t deltaY) { | 51 public PointF moveViewportCenter(boolean useScreenCenter, float deltaX, floa t deltaY) { |
| 52 PointF viewportCenter; | 52 PointF viewportCenter; |
| 53 synchronized (mRenderData) { | 53 RectF bounds = new RectF(0, 0, mRenderData.imageWidth, mRenderData.image Height); |
|
Lambros
2016/08/23 21:54:42
Unrelated to this CL, but maybe move this line fur
Yuwei
2016/08/23 22:07:55
Done.
| |
| 54 RectF bounds = new RectF(0, 0, mRenderData.imageWidth, mRenderData.i mageHeight); | 54 if (useScreenCenter) { |
| 55 if (useScreenCenter) { | 55 viewportCenter = getTrueViewportCenter(); |
| 56 viewportCenter = getTrueViewportCenter(); | 56 } else { |
| 57 } else { | 57 viewportCenter = new PointF(mViewportPosition.x, mViewportPosition.y ); |
| 58 viewportCenter = new PointF(mViewportPosition.x, mViewportPositi on.y); | 58 } |
| 59 } | |
| 60 | 59 |
| 61 viewportCenter.offset(deltaX, deltaY); | 60 viewportCenter.offset(deltaX, deltaY); |
| 62 if (viewportCenter.x < bounds.left) { | 61 if (viewportCenter.x < bounds.left) { |
| 63 viewportCenter.x = bounds.left; | 62 viewportCenter.x = bounds.left; |
| 64 } else if (viewportCenter.x > bounds.right) { | 63 } else if (viewportCenter.x > bounds.right) { |
| 65 viewportCenter.x = bounds.right; | 64 viewportCenter.x = bounds.right; |
| 66 } | 65 } |
| 67 | 66 |
| 68 if (viewportCenter.y < bounds.top) { | 67 if (viewportCenter.y < bounds.top) { |
| 69 viewportCenter.y = bounds.top; | 68 viewportCenter.y = bounds.top; |
| 70 } else if (viewportCenter.y > bounds.bottom) { | 69 } else if (viewportCenter.y > bounds.bottom) { |
| 71 viewportCenter.y = bounds.bottom; | 70 viewportCenter.y = bounds.bottom; |
| 72 } | 71 } |
| 73 | 72 |
| 74 mViewportPosition.set(viewportCenter); | 73 mViewportPosition.set(viewportCenter); |
| 75 } | |
| 76 | 74 |
| 77 return viewportCenter; | 75 return viewportCenter; |
| 78 } | 76 } |
| 79 | 77 |
| 80 /** | 78 /** |
| 81 * Sets the desired center position of the viewport. | 79 * Sets the desired center position of the viewport. |
| 82 * | 80 * |
| 83 * @param newX The new x coordinate value for the desired center position. | 81 * @param newX The new x coordinate value for the desired center position. |
| 84 * @param newY The new y coordinate value for the desired center position. | 82 * @param newY The new y coordinate value for the desired center position. |
| 85 */ | 83 */ |
| 86 public void setViewportPosition(float newX, float newY) { | 84 public void setViewportPosition(float newX, float newY) { |
| 87 synchronized (mRenderData) { | 85 mViewportPosition.set(newX, newY); |
| 88 mViewportPosition.set(newX, newY); | |
| 89 } | |
| 90 } | 86 } |
| 91 | 87 |
| 92 /** | 88 /** |
| 93 * Returns the current size of the viewport. This size includes the offset calculations for | 89 * Returns the current size of the viewport. This size includes the offset calculations for |
| 94 * any visible system UI. | 90 * any visible system UI. |
| 95 * | 91 * |
| 96 * @return A point representing the current size of the viewport. | 92 * @return A point representing the current size of the viewport. |
| 97 */ | 93 */ |
| 98 private PointF getViewportSize() { | 94 private PointF getViewportSize() { |
| 99 float adjustedScreenWidth, adjustedScreenHeight; | 95 float adjustedScreenWidth = mRenderData.screenWidth - mSystemUiOffsetPix els.right; |
| 100 synchronized (mRenderData) { | 96 float adjustedScreenHeight = mRenderData.screenHeight - mSystemUiOffsetP ixels.bottom; |
| 101 adjustedScreenWidth = mRenderData.screenWidth - mSystemUiOffsetPixel s.right; | |
| 102 adjustedScreenHeight = mRenderData.screenHeight - mSystemUiOffsetPix els.bottom; | |
| 103 } | |
| 104 | 97 |
| 105 return new PointF(adjustedScreenWidth, adjustedScreenHeight); | 98 return new PointF(adjustedScreenWidth, adjustedScreenHeight); |
| 106 } | 99 } |
| 107 | 100 |
| 108 /** | 101 /** |
| 109 * Returns the true center position of the viewport (in image coordinates). | 102 * Returns the true center position of the viewport (in image coordinates). |
| 110 * | 103 * |
| 111 * @return A point representing the true center position of the viewport. | 104 * @return A point representing the true center position of the viewport. |
| 112 */ | 105 */ |
| 113 private PointF getTrueViewportCenter() { | 106 private PointF getTrueViewportCenter() { |
| 114 synchronized (mRenderData) { | 107 PointF viewportSize = getViewportSize(); |
| 115 PointF viewportSize = getViewportSize(); | |
| 116 | 108 |
| 117 // Find the center point of the viewport on the screen. | 109 // Find the center point of the viewport on the screen. |
| 118 float[] viewportPosition = {((float) viewportSize.x / 2), ((float) v iewportSize.y / 2)}; | 110 float[] viewportPosition = {((float) viewportSize.x / 2), ((float) viewp ortSize.y / 2)}; |
| 119 | 111 |
| 120 // Convert the screen position to an image position. | 112 // Convert the screen position to an image position. |
| 121 Matrix screenToImage = new Matrix(); | 113 Matrix screenToImage = new Matrix(); |
| 122 mRenderData.transform.invert(screenToImage); | 114 mRenderData.transform.invert(screenToImage); |
| 123 screenToImage.mapPoints(viewportPosition); | 115 screenToImage.mapPoints(viewportPosition); |
| 124 return new PointF(viewportPosition[0], viewportPosition[1]); | 116 return new PointF(viewportPosition[0], viewportPosition[1]); |
| 125 } | |
| 126 } | 117 } |
| 127 | 118 |
| 128 /** | 119 /** |
| 129 * Sets the offset values used to calculate the space used by system UI. | 120 * Sets the offset values used to calculate the space used by system UI. |
| 130 * | 121 * |
| 131 * @param left The space used by system UI on the left edge of the screen. | 122 * @param left The space used by system UI on the left edge of the screen. |
| 132 * @param top The space used by system UI on the top edge of the screen. | 123 * @param top The space used by system UI on the top edge of the screen. |
| 133 * @param right The space used by system UI on the right edge of the screen. | 124 * @param right The space used by system UI on the right edge of the screen. |
| 134 * @param bottom The space used by system UI on the bottom edge of the scree n. | 125 * @param bottom The space used by system UI on the bottom edge of the scree n. |
| 135 */ | 126 */ |
| 136 public void setSystemUiOffsetValues(int left, int top, int right, int bottom ) { | 127 public void setSystemUiOffsetValues(int left, int top, int right, int bottom ) { |
| 137 synchronized (mRenderData) { | 128 mSystemUiOffsetPixels.set(left, top, right, bottom); |
| 138 mSystemUiOffsetPixels.set(left, top, right, bottom); | |
| 139 } | |
| 140 } | 129 } |
| 141 | 130 |
| 142 /** Repositions the image by zooming it such that the image is displayed wit hout borders. */ | 131 /** Repositions the image by zooming it such that the image is displayed wit hout borders. */ |
| 143 public void resizeImageToFitScreen() { | 132 public void resizeImageToFitScreen() { |
| 144 synchronized (mRenderData) { | 133 // Protect against being called before the image has been initialized. |
| 145 // Protect against being called before the image has been initialize d. | 134 if (mRenderData.imageWidth == 0 || mRenderData.imageHeight == 0) { |
| 146 if (mRenderData.imageWidth == 0 || mRenderData.imageHeight == 0) { | 135 return; |
| 147 return; | 136 } |
| 148 } | |
| 149 | 137 |
| 150 float widthRatio = (float) mRenderData.screenWidth / mRenderData.ima geWidth; | 138 float widthRatio = (float) mRenderData.screenWidth / mRenderData.imageWi dth; |
| 151 float heightRatio = (float) mRenderData.screenHeight / mRenderData.i mageHeight; | 139 float heightRatio = (float) mRenderData.screenHeight / mRenderData.image Height; |
| 152 float screenToImageScale = Math.max(widthRatio, heightRatio); | 140 float screenToImageScale = Math.max(widthRatio, heightRatio); |
| 153 | 141 |
| 154 // If the image is smaller than the screen in either dimension, then we want to scale it | 142 // If the image is smaller than the screen in either dimension, then we want to scale it |
| 155 // up to fit both and fill the screen with the image of the remote d esktop. | 143 // up to fit both and fill the screen with the image of the remote deskt op. |
| 156 if (screenToImageScale > 1.0f) { | 144 if (screenToImageScale > 1.0f) { |
| 157 mRenderData.transform.setScale(screenToImageScale, screenToImage Scale); | 145 mRenderData.transform.setScale(screenToImageScale, screenToImageScal e); |
| 158 } | |
| 159 } | 146 } |
| 160 | 147 |
| 161 repositionImage(false); | 148 repositionImage(false); |
| 162 } | 149 } |
| 163 | 150 |
| 164 /** | 151 /** |
| 165 * Repositions the image by translating it (without affecting the zoom level ). | 152 * Repositions the image by translating it (without affecting the zoom level ). |
| 166 * | 153 * |
| 167 * @param centerViewport Determines whether the viewport will be translated to the desired | 154 * @param centerViewport Determines whether the viewport will be translated to the desired |
| 168 * center position before being adjusted to fit the sc reen boundaries. | 155 * center position before being adjusted to fit the sc reen boundaries. |
| 169 */ | 156 */ |
| 170 public void repositionImage(boolean centerViewport) { | 157 public void repositionImage(boolean centerViewport) { |
| 171 PointF viewportSize = getViewportSize(); | 158 PointF viewportSize = getViewportSize(); |
| 172 synchronized (mRenderData) { | 159 // The goal of the code below is to position the viewport as close to th e desired center |
| 173 // The goal of the code below is to position the viewport as close t o the desired center | 160 // position as possible whilst keeping as much of the desktop in view as possible. |
| 174 // position as possible whilst keeping as much of the desktop in vie w as possible. | 161 // To achieve these goals, we first position the desktop image at the de sired center |
| 175 // To achieve these goals, we first position the desktop image at th e desired center | 162 // point and then re-position it to maximize the viewable area. |
| 176 // point and then re-position it to maximize the viewable area. | 163 if (centerViewport) { |
| 177 if (centerViewport) { | 164 // Map the current viewport position to screen coordinates. |
| 178 // Map the current viewport position to screen coordinates. | 165 float[] viewportPosition = {mViewportPosition.x, mViewportPosition.y }; |
| 179 float[] viewportPosition = {mViewportPosition.x, mViewportPositi on.y}; | 166 mRenderData.transform.mapPoints(viewportPosition); |
| 180 mRenderData.transform.mapPoints(viewportPosition); | |
| 181 | 167 |
| 182 // Translate so the viewport is displayed in the middle of the s creen. | 168 // Translate so the viewport is displayed in the middle of the scree n. |
| 183 mRenderData.transform.postTranslate( | 169 mRenderData.transform.postTranslate( |
| 184 ((float) viewportSize.x / 2) - viewportPosition[0], | 170 ((float) viewportSize.x / 2) - viewportPosition[0], |
| 185 ((float) viewportSize.y / 2) - viewportPosition[1]); | 171 ((float) viewportSize.y / 2) - viewportPosition[1]); |
| 186 } | 172 } |
| 187 | 173 |
| 188 // Get the coordinates of the desktop rectangle (top-left/bottom-rig ht corners) in | 174 // Get the coordinates of the desktop rectangle (top-left/bottom-right c orners) in |
| 189 // screen coordinates. Order is: left, top, right, bottom. | 175 // screen coordinates. Order is: left, top, right, bottom. |
| 190 RectF rectScreen = new RectF(0, 0, mRenderData.imageWidth, mRenderDa ta.imageHeight); | 176 RectF rectScreen = new RectF(0, 0, mRenderData.imageWidth, mRenderData.i mageHeight); |
| 191 mRenderData.transform.mapRect(rectScreen); | 177 mRenderData.transform.mapRect(rectScreen); |
| 192 | 178 |
| 193 float leftDelta = rectScreen.left; | 179 float leftDelta = rectScreen.left; |
| 194 float rightDelta = | 180 float rightDelta = |
| 195 rectScreen.right - mRenderData.screenWidth + mSystemUiOffset Pixels.right; | 181 rectScreen.right - mRenderData.screenWidth + mSystemUiOffsetPixe ls.right; |
| 196 float topDelta = rectScreen.top; | 182 float topDelta = rectScreen.top; |
| 197 float bottomDelta = | 183 float bottomDelta = |
| 198 rectScreen.bottom - mRenderData.screenHeight + mSystemUiOffs etPixels.bottom; | 184 rectScreen.bottom - mRenderData.screenHeight + mSystemUiOffsetPi xels.bottom; |
| 199 float xAdjust = 0; | 185 float xAdjust = 0; |
| 200 float yAdjust = 0; | 186 float yAdjust = 0; |
| 201 | 187 |
| 202 if (rectScreen.right - rectScreen.left < viewportSize.x) { | 188 if (rectScreen.right - rectScreen.left < viewportSize.x) { |
| 203 // Image is narrower than the screen, so center it. | 189 // Image is narrower than the screen, so center it. |
| 204 xAdjust = -(rightDelta + leftDelta) / 2; | 190 xAdjust = -(rightDelta + leftDelta) / 2; |
| 205 } else if (leftDelta > 0 && rightDelta > 0) { | 191 } else if (leftDelta > 0 && rightDelta > 0) { |
| 206 // Panning the image left will show more of it. | 192 // Panning the image left will show more of it. |
| 207 xAdjust = -Math.min(leftDelta, rightDelta); | 193 xAdjust = -Math.min(leftDelta, rightDelta); |
| 208 } else if (leftDelta < 0 && rightDelta < 0) { | 194 } else if (leftDelta < 0 && rightDelta < 0) { |
| 209 // Pan the image right. | 195 // Pan the image right. |
| 210 xAdjust = Math.min(-leftDelta, -rightDelta); | 196 xAdjust = Math.min(-leftDelta, -rightDelta); |
| 211 } | 197 } |
| 212 | 198 |
| 213 // Apply similar logic for yAdjust. | 199 // Apply similar logic for yAdjust. |
| 214 if (rectScreen.bottom - rectScreen.top < viewportSize.y) { | 200 if (rectScreen.bottom - rectScreen.top < viewportSize.y) { |
| 215 yAdjust = -(bottomDelta + topDelta) / 2; | 201 yAdjust = -(bottomDelta + topDelta) / 2; |
| 216 } else if (topDelta > 0 && bottomDelta > 0) { | 202 } else if (topDelta > 0 && bottomDelta > 0) { |
| 217 yAdjust = -Math.min(topDelta, bottomDelta); | 203 yAdjust = -Math.min(topDelta, bottomDelta); |
| 218 } else if (topDelta < 0 && bottomDelta < 0) { | 204 } else if (topDelta < 0 && bottomDelta < 0) { |
| 219 yAdjust = Math.min(-topDelta, -bottomDelta); | 205 yAdjust = Math.min(-topDelta, -bottomDelta); |
| 220 } | 206 } |
| 221 | 207 |
| 222 mRenderData.transform.postTranslate(xAdjust, yAdjust); | 208 mRenderData.transform.postTranslate(xAdjust, yAdjust); |
| 223 | 209 |
| 224 mViewer.transformationChanged(); | 210 mViewer.transformationChanged(); |
| 225 } | |
| 226 } | 211 } |
| 227 | 212 |
| 228 /** | 213 /** |
| 229 * Repositions the image by translating and zooming it, to keep the zoom lev el within sensible | 214 * Repositions the image by translating and zooming it, to keep the zoom lev el within sensible |
| 230 * limits. The minimum zoom level is chosen to avoid black space around all 4 sides. The | 215 * limits. The minimum zoom level is chosen to avoid black space around all 4 sides. The |
| 231 * maximum zoom level is set arbitrarily, so that the user can zoom out agai n in a reasonable | 216 * maximum zoom level is set arbitrarily, so that the user can zoom out agai n in a reasonable |
| 232 * time, and to prevent arithmetic overflow problems from displaying the ima ge. | 217 * time, and to prevent arithmetic overflow problems from displaying the ima ge. |
| 233 * | 218 * |
| 234 * @param centerViewport Determines whether the viewport will be translated to the desired | 219 * @param centerViewport Determines whether the viewport will be translated to the desired |
| 235 * center position before being adjusted to fit the sc reen boundaries. | 220 * center position before being adjusted to fit the sc reen boundaries. |
| 236 */ | 221 */ |
| 237 public void repositionImageWithZoom(boolean centerViewport) { | 222 public void repositionImageWithZoom(boolean centerViewport) { |
| 238 synchronized (mRenderData) { | 223 // Avoid division by zero in case this gets called before the image size is initialized. |
| 239 // Avoid division by zero in case this gets called before the image size is initialized. | 224 if (mRenderData.imageWidth == 0 || mRenderData.imageHeight == 0) { |
| 240 if (mRenderData.imageWidth == 0 || mRenderData.imageHeight == 0) { | 225 return; |
| 241 return; | 226 } |
| 242 } | |
| 243 | 227 |
| 244 // Zoom out if the zoom level is too high. | 228 // Zoom out if the zoom level is too high. |
| 245 float currentZoomLevel = mRenderData.transform.mapRadius(1.0f); | 229 float currentZoomLevel = mRenderData.transform.mapRadius(1.0f); |
| 246 if (currentZoomLevel > MAX_ZOOM_FACTOR) { | 230 if (currentZoomLevel > MAX_ZOOM_FACTOR) { |
| 247 mRenderData.transform.setScale(MAX_ZOOM_FACTOR, MAX_ZOOM_FACTOR) ; | 231 mRenderData.transform.setScale(MAX_ZOOM_FACTOR, MAX_ZOOM_FACTOR); |
| 248 } | 232 } |
| 249 | 233 |
| 250 // Get image size scaled to screen coordinates. | 234 // Get image size scaled to screen coordinates. |
| 251 float[] imageSize = {mRenderData.imageWidth, mRenderData.imageHeight }; | 235 float[] imageSize = {mRenderData.imageWidth, mRenderData.imageHeight}; |
| 252 mRenderData.transform.mapVectors(imageSize); | 236 mRenderData.transform.mapVectors(imageSize); |
| 253 | 237 |
| 254 if (imageSize[0] < mRenderData.screenWidth && imageSize[1] < mRender Data.screenHeight) { | 238 if (imageSize[0] < mRenderData.screenWidth && imageSize[1] < mRenderData .screenHeight) { |
| 255 // Displayed image is too small in both directions, so apply the minimum zoom | 239 // Displayed image is too small in both directions, so apply the min imum zoom |
| 256 // level needed to fit either the width or height. | 240 // level needed to fit either the width or height. |
| 257 float scale = Math.min((float) mRenderData.screenWidth / mRender Data.imageWidth, | 241 float scale = Math.min((float) mRenderData.screenWidth / mRenderData .imageWidth, |
| 258 (float) mRenderData.screenHeight / mRende rData.imageHeight); | 242 (float) mRenderData.screenHeight / mRenderDat a.imageHeight); |
| 259 mRenderData.transform.setScale(scale, scale); | 243 mRenderData.transform.setScale(scale, scale); |
| 260 } | |
| 261 } | 244 } |
| 262 | 245 |
| 263 repositionImage(centerViewport); | 246 repositionImage(centerViewport); |
| 264 } | 247 } |
| 265 } | 248 } |
| OLD | NEW |