Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Unified Diff: ui/android/java/src/org/chromium/ui/display/DisplayAndroid.java

Issue 2523273002: Implement Virtual Display class for Android. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/android/java/src/org/chromium/ui/display/DisplayAndroid.java
diff --git a/ui/android/java/src/org/chromium/ui/display/DisplayAndroid.java b/ui/android/java/src/org/chromium/ui/display/DisplayAndroid.java
index fc17ef77548ab1e562913d10993dd28720278112..c87daa7ba8924b8b14b6a6076471e671e050bf22 100644
--- a/ui/android/java/src/org/chromium/ui/display/DisplayAndroid.java
+++ b/ui/android/java/src/org/chromium/ui/display/DisplayAndroid.java
@@ -4,18 +4,10 @@
package org.chromium.ui.display;
-import android.annotation.TargetApi;
import android.content.Context;
-import android.graphics.PixelFormat;
-import android.graphics.Point;
-import android.os.Build;
-import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Surface;
-import org.chromium.base.CommandLine;
-import org.chromium.base.Log;
-
import java.util.WeakHashMap;
/**
@@ -25,7 +17,7 @@ import java.util.WeakHashMap;
* anywhere, as long as the corresponding WindowAndroids are destroyed. The observers are
* held weakly so to not lead to leaks.
*/
-public class DisplayAndroid {
+public abstract class DisplayAndroid {
/**
* DisplayAndroidObserver interface for changes to this Display.
*/
@@ -45,55 +37,13 @@ public class DisplayAndroid {
void onDIPScaleChanged(float dipScale);
}
- private static final String TAG = "DisplayAndroid";
-
private static final DisplayAndroidObserver[] EMPTY_OBSERVER_ARRAY =
new DisplayAndroidObserver[0];
- private final int mSdkDisplayId;
private final WeakHashMap<DisplayAndroidObserver, Object /* null */> mObservers;
// Do NOT add strong references to objects with potentially complex lifetime, like Context.
- // Updated by updateFromDisplay.
- private final Point mSize;
- private final Point mPhysicalSize;
- private final DisplayMetrics mDisplayMetrics;
- private final PixelFormat mPixelFormatInfo;
- private int mPixelFormatId;
- private int mRotation;
-
- // When this object exists, a positive value means that the forced DIP scale is set and
- // the zero means it is not. The non existing object (i.e. null reference) means that
- // the existence and value of the forced DIP scale has not yet been determined.
- private static Float sForcedDIPScale;
-
- private static boolean hasForcedDIPScale() {
- if (sForcedDIPScale == null) {
- String forcedScaleAsString = CommandLine.getInstance().getSwitchValue(
- DisplaySwitches.FORCE_DEVICE_SCALE_FACTOR);
- if (forcedScaleAsString == null) {
- sForcedDIPScale = Float.valueOf(0.0f);
- } else {
- boolean isInvalid = false;
- try {
- sForcedDIPScale = Float.valueOf(forcedScaleAsString);
- // Negative values are discarded.
- if (sForcedDIPScale.floatValue() <= 0.0f) isInvalid = true;
- } catch (NumberFormatException e) {
- // Strings that do not represent numbers are discarded.
- isInvalid = true;
- }
-
- if (isInvalid) {
- Log.w(TAG, "Ignoring invalid forced DIP scale '" + forcedScaleAsString + "'");
- sForcedDIPScale = Float.valueOf(0.0f);
- }
- }
- }
- return sForcedDIPScale.floatValue() > 0;
- }
-
- private static DisplayAndroidManager getManager() {
+ protected static DisplayAndroidManager getManager() {
return DisplayAndroidManager.getInstance();
}
@@ -120,50 +70,38 @@ public class DisplayAndroid {
/**
* @return Display id as defined in Android's Display.
*/
- public int getSdkDisplayId() {
- return mSdkDisplayId;
- }
+ public abstract int getSdkDisplayId();
/**
* @return Display height in physical pixels.
*/
- public int getDisplayHeight() {
- return mSize.y;
- }
+ public abstract int getDisplayHeight();
/**
* @return Display width in physical pixels.
*/
- public int getDisplayWidth() {
- return mSize.x;
- }
+ public abstract int getDisplayWidth();
/**
* @return Real physical display height in physical pixels. Or 0 if not supported.
*/
- public int getPhysicalDisplayHeight() {
- return mPhysicalSize.y;
- }
+ public abstract int getPhysicalDisplayHeight();
/**
* @return Real physical display width in physical pixels. Or 0 if not supported.
*/
- public int getPhysicalDisplayWidth() {
- return mPhysicalSize.x;
- }
+ public abstract int getPhysicalDisplayWidth();
/**
* @return current orientation. One of Surface.ORIENTATION_* values.
*/
- public int getRotation() {
- return mRotation;
- }
+ public abstract int getRotation();
/**
* @return current orientation in degrees. One of the values 0, 90, 180, 270.
*/
/* package */ int getRotationDegrees() {
- switch (mRotation) {
+ switch (getRotation()) {
case Surface.ROTATION_0:
return 0;
case Surface.ROTATION_90:
@@ -182,51 +120,18 @@ public class DisplayAndroid {
/**
* @return A scaling factor for the Density Independent Pixel unit.
*/
- public float getDipScale() {
- return mDisplayMetrics.density;
- }
+ public abstract float getDipScale();
/**
* @return Number of bits per pixel.
*/
- /* package */ int getBitsPerPixel() {
- return mPixelFormatInfo.bitsPerPixel;
- }
+ /* package */ abstract int getBitsPerPixel();
/**
* @return Number of bits per each color component.
*/
@SuppressWarnings("deprecation")
- /* package */ int getBitsPerComponent() {
- switch (mPixelFormatId) {
- case PixelFormat.RGBA_4444:
- return 4;
-
- case PixelFormat.RGBA_5551:
- return 5;
-
- case PixelFormat.RGBA_8888:
- case PixelFormat.RGBX_8888:
- case PixelFormat.RGB_888:
- return 8;
-
- case PixelFormat.RGB_332:
- return 2;
-
- case PixelFormat.RGB_565:
- return 5;
-
- // Non-RGB formats.
- case PixelFormat.A_8:
- case PixelFormat.LA_88:
- case PixelFormat.L_8:
- return 0;
-
- // Unknown format. Use 8 as a sensible default.
- default:
- return 8;
- }
- }
+ /* package */ abstract int getBitsPerComponent();
/**
* Add observer. Note repeat observers will be called only one.
@@ -259,69 +164,11 @@ public class DisplayAndroid {
getManager().startAccurateListening();
}
- /* package */ DisplayAndroid(Display display) {
- mSdkDisplayId = display.getDisplayId();
+ public DisplayAndroid() {
mObservers = new WeakHashMap<>();
- mSize = new Point();
- mPhysicalSize = new Point();
- mDisplayMetrics = new DisplayMetrics();
- mPixelFormatInfo = new PixelFormat();
- }
-
- @SuppressWarnings("deprecation")
- @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
- /* package */ void updateFromDisplay(Display display) {
- final Point oldSize = new Point(mSize);
- final Point oldPhysicalSize = new Point(mPhysicalSize);
- final float oldDensity = mDisplayMetrics.density;
- final int oldPixelFormatId = mPixelFormatId;
- final int oldRotation = mRotation;
-
- display.getSize(mSize);
- display.getMetrics(mDisplayMetrics);
-
- if (hasForcedDIPScale()) mDisplayMetrics.density = sForcedDIPScale.floatValue();
-
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
- display.getRealSize(mPhysicalSize);
- }
-
- // JellyBean MR1 and later always uses RGBA_8888.
- mPixelFormatId = (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
- ? display.getPixelFormat()
- : PixelFormat.RGBA_8888;
- if (oldPixelFormatId != mPixelFormatId) {
- PixelFormat.getPixelFormatInfo(mPixelFormatId, mPixelFormatInfo);
- }
-
- mRotation = display.getRotation();
-
- final boolean noChanges = oldSize.equals(mSize) && oldPhysicalSize.equals(mPhysicalSize)
- && oldDensity == mDisplayMetrics.density && oldPixelFormatId == mPixelFormatId
- && oldRotation == mRotation;
- if (noChanges) return;
-
- getManager().updateDisplayOnNativeSide(this);
-
- if (oldRotation != mRotation) {
- DisplayAndroidObserver[] observers = getObservers();
- for (DisplayAndroidObserver o : observers) {
- o.onRotationChanged(mRotation);
- }
- }
-
- // Intentional comparison of floats: we assume that if scales differ,
- // they differ significantly.
- boolean dipScaleChanged = oldDensity != mDisplayMetrics.density;
- if (dipScaleChanged) {
- DisplayAndroidObserver[] observers = getObservers();
- for (DisplayAndroidObserver o : observers) {
- o.onDIPScaleChanged(mDisplayMetrics.density);
- }
- }
}
- private DisplayAndroidObserver[] getObservers() {
+ protected DisplayAndroidObserver[] getObservers() {
// Makes a copy to allow concurrent edit.
return mObservers.keySet().toArray(EMPTY_OBSERVER_ARRAY);
}

Powered by Google App Engine
This is Rietveld 408576698