| 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.ui.gfx; |
| 6 | |
| 7 | import android.content.Context; |
| 8 | import android.graphics.PixelFormat; |
| 9 | import android.os.Build; |
| 10 | import android.util.DisplayMetrics; |
| 11 | import android.view.Display; |
| 12 | import android.view.WindowManager; |
| 13 | |
| 14 | import org.chromium.base.CalledByNative; |
| 15 | import org.chromium.base.JNINamespace; |
| 16 | |
| 17 | /** |
| 18 | * This class facilitates access to android information typically only |
| 19 | * available using the Java SDK, including {@link Display} properties. |
| 20 | * |
| 21 | * Currently the information consists of very raw display information (height, width, DPI scale) |
| 22 | * regarding the main display. |
| 23 | */ |
| 24 | @JNINamespace("gfx") |
| 25 | public class DeviceDisplayInfo { |
| 26 | |
| 27 | |
| 28 | private final Context mAppContext; |
| 29 | private final WindowManager mWinManager; |
| 30 | |
| 31 | private DeviceDisplayInfo(Context context) { |
| 32 | mAppContext = context.getApplicationContext(); |
| 33 | mWinManager = (WindowManager) mAppContext.getSystemService(Context.WINDOW_SERVICE); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @return Display height in physical pixels. |
| 38 | */ |
| 39 | @CalledByNative |
| 40 | public int getDisplayHeight() { |
| 41 | return getMetrics().heightPixels; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @return Display width in physical pixels. |
| 46 | */ |
| 47 | @CalledByNative |
| 48 | public int getDisplayWidth() { |
| 49 | return getMetrics().widthPixels; |
| 50 | } |
| 51 | |
| 52 | @SuppressWarnings("deprecation") |
| 53 | private int getPixelFormat() { |
| 54 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { |
| 55 | return getDisplay().getPixelFormat(); |
| 56 | } |
| 57 | // JellyBean MR1 and later always uses RGBA_8888. |
| 58 | return PixelFormat.RGBA_8888; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @return Bits per pixel. |
| 63 | */ |
| 64 | @CalledByNative |
| 65 | public int getBitsPerPixel() { |
| 66 | int format = getPixelFormat(); |
| 67 | PixelFormat info = new PixelFormat(); |
| 68 | PixelFormat.getPixelFormatInfo(format, info); |
| 69 | return info.bitsPerPixel; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @return Bits per component. |
| 74 | */ |
| 75 | @SuppressWarnings("deprecation") |
| 76 | @CalledByNative |
| 77 | public int getBitsPerComponent() { |
| 78 | int format = getPixelFormat(); |
| 79 | switch (format) { |
| 80 | case PixelFormat.RGBA_4444: |
| 81 | return 4; |
| 82 | |
| 83 | case PixelFormat.RGBA_5551: |
| 84 | return 5; |
| 85 | |
| 86 | case PixelFormat.RGBA_8888: |
| 87 | case PixelFormat.RGBX_8888: |
| 88 | case PixelFormat.RGB_888: |
| 89 | return 8; |
| 90 | |
| 91 | case PixelFormat.RGB_332: |
| 92 | return 2; |
| 93 | |
| 94 | case PixelFormat.RGB_565: |
| 95 | return 5; |
| 96 | |
| 97 | // Non-RGB formats. |
| 98 | case PixelFormat.A_8: |
| 99 | case PixelFormat.LA_88: |
| 100 | case PixelFormat.L_8: |
| 101 | return 0; |
| 102 | |
| 103 | // Unknown format. Use 8 as a sensible default. |
| 104 | default: |
| 105 | return 8; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @return A scaling factor for the Density Independent Pixel unit. |
| 111 | * 1.0 is 160dpi, 0.75 is 120dpi, 2.0 is 320dpi. |
| 112 | */ |
| 113 | @CalledByNative |
| 114 | public double getDIPScale() { |
| 115 | return getMetrics().density; |
| 116 | } |
| 117 | |
| 118 | private Display getDisplay() { |
| 119 | return mWinManager.getDefaultDisplay(); |
| 120 | } |
| 121 | |
| 122 | private DisplayMetrics getMetrics() { |
| 123 | return mAppContext.getResources().getDisplayMetrics(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Creates DeviceDisplayInfo for a given Context. |
| 128 | * @param context A context to use. |
| 129 | * @return DeviceDisplayInfo associated with a given Context. |
| 130 | */ |
| 131 | @CalledByNative |
| 132 | public static DeviceDisplayInfo create(Context context) { |
| 133 | return new DeviceDisplayInfo(context); |
| 134 | } |
| 135 | } |