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

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

Issue 2416403002: Reland of Android: support multiple displays on C++ side (Closed)
Patch Set: Restore GetDisplayNearestWindow(0) functionality to return primary display. 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 686946293b2db01635b583cd84e20d51ee7e7394..2bd5b012da4c13382456eeaf06938e6ee636a9eb 100644
--- a/ui/android/java/src/org/chromium/ui/display/DisplayAndroid.java
+++ b/ui/android/java/src/org/chromium/ui/display/DisplayAndroid.java
@@ -6,14 +6,18 @@ 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.EnumSet;
+import java.util.Set;
import java.util.WeakHashMap;
/**
@@ -36,6 +40,15 @@ public class DisplayAndroid {
void onRotationChanged(int rotation);
}
+ // Enumeration telling what DisplayAndroid property has changed after update.
+ public enum PropertyChange {
boliu 2016/11/07 17:05:33 we don't ever use enums on android because it it's
Tima Vaisburd 2016/11/07 18:26:04 My original intent was to use a bitmask, but then
boliu 2016/11/07 18:46:29 revert
Tima Vaisburd 2016/11/07 23:52:24 I tried, but it did not go well. I did not realize
boliu 2016/11/08 00:02:47 perhaps that suggests the jni should happen on Dis
Tima Vaisburd 2016/11/09 01:17:01 I came up with DisplayAndroid holding the native p
+ PHYSICAL_SIZE,
+ SIZE,
+ DIP_SCALE,
+ PIXEL_FORMAT,
+ ROTATION,
+ }
+
private static final String TAG = "DisplayAndroid";
private static final DisplayAndroidObserver[] EMPTY_OBSERVER_ARRAY =
@@ -49,6 +62,8 @@ public class DisplayAndroid {
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
@@ -107,6 +122,13 @@ public class DisplayAndroid {
}
/**
+ * @return Display id as defined in Android's Display.
+ */
+ public int getSdkDisplayId() {
+ return mSdkDisplayId;
+ }
+
+ /**
* @return Display height in physical pixels.
*/
public int getDisplayHeight() {
@@ -142,6 +164,26 @@ public class DisplayAndroid {
}
/**
+ * @return current orientation in degrees. One of the values 0, 90, 180, 270.
+ */
+ public int getRotationDegrees() {
+ switch (mRotation) {
+ case Surface.ROTATION_0:
+ return 0;
+ case Surface.ROTATION_90:
+ return 90;
+ case Surface.ROTATION_180:
+ return 180;
+ case Surface.ROTATION_270:
+ return 270;
+ }
+
+ // This should not happen.
+ assert false;
+ return 0;
+ }
+
+ /**
* @return A scaling factor for the Density Independent Pixel unit.
*/
public float getDIPScale() {
@@ -149,6 +191,48 @@ public class DisplayAndroid {
}
/**
+ * @return Number of bits per pixel.
+ */
+ public int getBitsPerPixel() {
+ return mPixelFormatInfo.bitsPerPixel;
+ }
+
+ /**
+ * @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;
+ }
+ }
+
+ /**
* Add observer. Note repeat observers will be called only one.
* Observers are held only weakly by Display.
*/
@@ -185,11 +269,18 @@ public class DisplayAndroid {
mSize = new Point();
mPhysicalSize = new Point();
mDisplayMetrics = new DisplayMetrics();
- updateFromDisplay(display);
+ mPixelFormatInfo = new PixelFormat();
}
+ @SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
- /* package */ void updateFromDisplay(Display display) {
+ /* package */ Set<PropertyChange> 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);
@@ -199,11 +290,28 @@ public class DisplayAndroid {
display.getRealSize(mPhysicalSize);
}
- int newRotation = display.getRotation();
- boolean rotationChanged = newRotation != mRotation;
- mRotation = newRotation;
+ // 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();
+
+ Set<PropertyChange> changes = EnumSet.noneOf(PropertyChange.class);
+ if (!oldSize.equals(mSize)) changes.add(PropertyChange.SIZE);
+ if (!oldPhysicalSize.equals(mPhysicalSize)) changes.add(PropertyChange.PHYSICAL_SIZE);
+ if (oldDensity != mDisplayMetrics.density) changes.add(PropertyChange.DIP_SCALE);
+ if (oldPixelFormatId != mPixelFormatId) changes.add(PropertyChange.PIXEL_FORMAT);
+ if (oldRotation != mRotation) changes.add(PropertyChange.ROTATION);
+
+ return changes;
+ }
- if (rotationChanged) {
+ /* package */ void notifyObservers(Set<PropertyChange> changes) {
+ if (changes.contains(PropertyChange.ROTATION)) {
DisplayAndroidObserver[] observers = getObservers();
for (DisplayAndroidObserver o : observers) {
o.onRotationChanged(mRotation);

Powered by Google App Engine
This is Rietveld 408576698