Commit 9c0cae25 authored by ckitagawa's avatar ckitagawa Committed by Commit Bot

[Paint Preview] Create viewport class

This CL replaces the viewport state in PlayerFrameMediator with a new
PlayerFrameViewport class for managing state.

This causes a chain reaction requiring
1. A number of new method calls.
2. Removal of scale updates from moveViewport
3. Removal of scale maps for storing bitmaps

Bug: 1099722
Change-Id: Ia666485b59b0808af20055a1c023eb9e5176ecf5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2285257Reviewed-by: default avatarMehran Mahmoudi <mahmoudi@chromium.org>
Commit-Queue: Calder Kitagawa <ckitagawa@chromium.org>
Cr-Commit-Position: refs/heads/master@{#786428}
parent 5393a7e9
...@@ -62,6 +62,7 @@ android_library("java") { ...@@ -62,6 +62,7 @@ android_library("java") {
"java/src/org/chromium/components/paintpreview/player/frame/PlayerFrameView.java", "java/src/org/chromium/components/paintpreview/player/frame/PlayerFrameView.java",
"java/src/org/chromium/components/paintpreview/player/frame/PlayerFrameViewBinder.java", "java/src/org/chromium/components/paintpreview/player/frame/PlayerFrameViewBinder.java",
"java/src/org/chromium/components/paintpreview/player/frame/PlayerFrameViewDelegate.java", "java/src/org/chromium/components/paintpreview/player/frame/PlayerFrameViewDelegate.java",
"java/src/org/chromium/components/paintpreview/player/frame/PlayerFrameViewport.java",
] ]
deps = [ deps = [
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.components.paintpreview.player.frame;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.util.Size;
import androidx.annotation.VisibleForTesting;
/**
* Used to represent the viewport for a frame in the paint preview player. There should be one of
* these objects per player frame and it should be shared between various classes that manipulated
* the location. Should only be accessed on the UI thread to avoid the need for locks.
*/
public class PlayerFrameViewport {
/** The size of the viewport. */
private Size mViewportSize;
/** A 3x3 affine transformation matrix to track scale and translation. */
@VisibleForTesting
final Matrix mViewportTransform;
/** Transient storage objects to avoid allocations. */
private final Rect mViewportRect = new Rect();
private final float[] mMatrixValues = new float[9];
PlayerFrameViewport(Size size, Matrix matrix) {
mViewportSize = size;
mViewportTransform = matrix;
}
/**
* @return the width of the viewport.
*/
int getWidth() {
return mViewportSize.getWidth();
}
/**
* @return the height of the viewport.
*/
int getHeight() {
return mViewportSize.getHeight();
}
/**
* Returns the size of the viewport. Use with caution as the values of the returned object are
* not updated.
* @return the size of the viewport.
*/
Size getSize() {
return mViewportSize;
}
/**
* Returns the translation of the viewport in the X direction (AKA left).
* @return the x translation of the viewport.
*/
float getTransX() {
mViewportTransform.getValues(mMatrixValues);
return mMatrixValues[Matrix.MTRANS_X];
}
/**
* Returns the translation of the viewport in the Y direction (AKA top).
* @return the y translation of the viewport.
*/
float getTransY() {
mViewportTransform.getValues(mMatrixValues);
return mMatrixValues[Matrix.MTRANS_Y];
}
/**
* Returns the scale at which to show contents.
* @return a scale factor for the viewport.
*/
float getScale() {
mViewportTransform.getValues(mMatrixValues);
return mMatrixValues[Matrix.MSCALE_X]; // x and y should be identical here.
}
/**
* Returns the current viewport position as a rect. Use cautiously as this is an instantaneous
* snapshot and is not continually updated.
* @return a rect of the current viewport.
* */
Rect asRect() {
mViewportTransform.getValues(mMatrixValues);
final int left = Math.round(mMatrixValues[Matrix.MTRANS_X]);
final int top = Math.round(mMatrixValues[Matrix.MTRANS_Y]);
mViewportRect.set(
left, top, left + mViewportSize.getWidth(), top + mViewportSize.getHeight());
return mViewportRect;
}
/**
* Sets the size of the viewport.
* @param width The width of the viewport.
* @param height The height of the viewport.
*/
void setSize(int width, int height) {
mViewportSize = new Size(width, height);
}
/**
* Sets the position x, y (left, top) of the viewport.
* @param x The left side of the viewport.
* @param y The top of the viewport.
*/
void setTrans(float x, float y) {
mViewportTransform.getValues(mMatrixValues);
mMatrixValues[Matrix.MTRANS_X] = x;
mMatrixValues[Matrix.MTRANS_Y] = y;
mViewportTransform.setValues(mMatrixValues);
}
/**
* Sets the scale of the viewport.
* @param scaleFactor The scale of the viewport.
*/
void setScale(float scaleFactor) {
mViewportTransform.getValues(mMatrixValues);
mMatrixValues[Matrix.MSCALE_X] = scaleFactor;
mMatrixValues[Matrix.MSCALE_Y] = scaleFactor;
mViewportTransform.setValues(mMatrixValues);
}
/**
* Offsets/shifts the viewport by a set amount.
* @param dx The distance to offset on the x-axis.
* @param dy The distance to offset on the y-axis.
*/
void offset(float dx, float dy) {
mViewportTransform.postTranslate(dx, dy);
}
/**
* Affine scaling of the viewport about a focal point/pivot.
* @param scaleFactor The amount to scale by (relative to the current scale).
* @param focalX The x-coordinate of the focal point.
* @param focalY The y-coordinate of the focal point.
*/
void scale(float scaleFactor, float focalX, float focalY) {
mViewportTransform.postScale(scaleFactor, scaleFactor, -focalX, -focalY);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment