| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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.chromoting.jni; |
| 6 |
| 7 import android.graphics.Bitmap; |
| 8 import android.graphics.Point; |
| 9 import android.os.Looper; |
| 10 |
| 11 import org.chromium.base.Log; |
| 12 import org.chromium.base.annotations.CalledByNative; |
| 13 import org.chromium.base.annotations.JNINamespace; |
| 14 |
| 15 import java.nio.ByteBuffer; |
| 16 import java.nio.ByteOrder; |
| 17 |
| 18 /** |
| 19 * This class is for drawing the desktop on the canvas and controlling the lifet
ime of the |
| 20 * corresponding C++ object. It only draws the desktop on the graphics (=display
) thread but also |
| 21 * has functions accessible on UI thread. |
| 22 */ |
| 23 @JNINamespace("remoting") |
| 24 public class Display { |
| 25 private static final String TAG = "Chromoting"; |
| 26 |
| 27 // Pointer to the C++ object. Casted to |long|. |
| 28 private long mNativeJniDisplayHandler; |
| 29 |
| 30 /** |
| 31 * Callback invoked on the graphics thread to repaint the desktop. Read on t
he UI and |
| 32 * graphics threads. Write only on the UI thread. |
| 33 */ |
| 34 private Runnable mRedrawCallback; |
| 35 |
| 36 /** |
| 37 * Protects access to {@link mRedrawCallback}. |
| 38 * Locking is not required when reading on the UI thread. |
| 39 */ |
| 40 private final Object mRedrawCallbackLock = new Object(); |
| 41 |
| 42 /** Protects access to {@link mFrameBitmap}. */ |
| 43 private final Object mFrameLock = new Object(); |
| 44 |
| 45 /** Bitmap holding a copy of the latest video frame. Accessed on the UI and
graphics threads. */ |
| 46 private Bitmap mFrameBitmap; |
| 47 |
| 48 /** Position of cursor hot-spot. Accessed on the graphics thread. */ |
| 49 private Point mCursorHotspot = new Point(); |
| 50 |
| 51 /** Bitmap holding the cursor shape. Accessed on the graphics thread. */ |
| 52 private Bitmap mCursorBitmap; |
| 53 |
| 54 public Display() { |
| 55 mNativeJniDisplayHandler = nativeInit(); |
| 56 } |
| 57 |
| 58 /** |
| 59 * @return the pointer to the native C++ object. |
| 60 */ |
| 61 public long getNativePointer() { |
| 62 return mNativeJniDisplayHandler; |
| 63 } |
| 64 |
| 65 /** |
| 66 * Sets the redraw callback to the provided functor. Provide a value of null
whenever the |
| 67 * window is no longer visible so that we don't continue to draw onto it. Ca
lled on the UI |
| 68 * thread. |
| 69 */ |
| 70 public void provideRedrawCallback(Runnable redrawCallback) { |
| 71 synchronized (mRedrawCallbackLock) { |
| 72 mRedrawCallback = redrawCallback; |
| 73 } |
| 74 } |
| 75 |
| 76 /** |
| 77 * Destroys its resources and the native counterpart. Called on the UI threa
d. |
| 78 */ |
| 79 public void destroy() { |
| 80 // Drop the reference to free the Bitmap for GC. |
| 81 synchronized (mFrameLock) { |
| 82 mFrameBitmap = null; |
| 83 } |
| 84 |
| 85 provideRedrawCallback(null); |
| 86 nativeDestroy(mNativeJniDisplayHandler); |
| 87 mNativeJniDisplayHandler = 0; |
| 88 } |
| 89 |
| 90 /** Forces the native graphics thread to redraw to the canvas. Called on the
UI thread. */ |
| 91 public boolean redrawGraphics() { |
| 92 if (mRedrawCallback == null) return false; |
| 93 |
| 94 nativeScheduleRedraw(mNativeJniDisplayHandler); |
| 95 return true; |
| 96 } |
| 97 |
| 98 /** |
| 99 * Called on the graphics thread to perform the redrawing callback requested
by |
| 100 * {@link #redrawGraphics}. This is a no-op if the window isn't visible (the
callback is null). |
| 101 */ |
| 102 @CalledByNative |
| 103 void redrawGraphicsInternal() { |
| 104 Runnable callback; |
| 105 synchronized (mRedrawCallbackLock) { |
| 106 callback = mRedrawCallback; |
| 107 } |
| 108 if (callback != null) { |
| 109 callback.run(); |
| 110 } |
| 111 } |
| 112 |
| 113 /** |
| 114 * Returns a bitmap of the latest video frame. Called on the native graphics
thread when |
| 115 * DesktopView is repainted. |
| 116 */ |
| 117 public Bitmap getVideoFrame() { |
| 118 if (Looper.myLooper() == Looper.getMainLooper()) { |
| 119 Log.w(TAG, "Canvas being redrawn on UI thread"); |
| 120 } |
| 121 |
| 122 synchronized (mFrameLock) { |
| 123 return mFrameBitmap; |
| 124 } |
| 125 } |
| 126 |
| 127 /** |
| 128 * Set a new video frame. Called on the native graphics thread when a new fr
ame is allocated. |
| 129 */ |
| 130 @CalledByNative |
| 131 void setVideoFrame(Bitmap bitmap) { |
| 132 if (Looper.myLooper() == Looper.getMainLooper()) { |
| 133 Log.w(TAG, "Video frame updated on UI thread"); |
| 134 } |
| 135 |
| 136 synchronized (mFrameLock) { |
| 137 mFrameBitmap = bitmap; |
| 138 } |
| 139 } |
| 140 |
| 141 /** |
| 142 * Creates a new Bitmap to hold video frame pixels. The returned Bitmap is r
eferenced by native |
| 143 * code which writes the decoded frame pixels to it. |
| 144 */ |
| 145 @CalledByNative |
| 146 static Bitmap newBitmap(int width, int height) { |
| 147 return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
| 148 } |
| 149 |
| 150 /** |
| 151 * Updates the cursor shape. This is called on the graphics thread when rece
iving a new cursor |
| 152 * shape from the host. |
| 153 */ |
| 154 @CalledByNative |
| 155 void updateCursorShape(int width, int height, int hotspotX, int hotspotY, By
teBuffer buffer) { |
| 156 mCursorHotspot = new Point(hotspotX, hotspotY); |
| 157 |
| 158 int[] data = new int[width * height]; |
| 159 buffer.order(ByteOrder.LITTLE_ENDIAN); |
| 160 buffer.asIntBuffer().get(data, 0, data.length); |
| 161 mCursorBitmap = Bitmap.createBitmap(data, width, height, Bitmap.Config.A
RGB_8888); |
| 162 } |
| 163 |
| 164 /** Position of cursor hotspot within cursor image. Called on the graphics t
hread. */ |
| 165 public Point getCursorHotspot() { |
| 166 return mCursorHotspot; |
| 167 } |
| 168 |
| 169 /** Returns the current cursor shape. Called on the graphics thread. */ |
| 170 public Bitmap getCursorBitmap() { |
| 171 return mCursorBitmap; |
| 172 } |
| 173 |
| 174 private native long nativeInit(); |
| 175 |
| 176 private native void nativeDestroy(long nativeJniDisplayHandler); |
| 177 |
| 178 /** Schedules a redraw on the native graphics thread. */ |
| 179 private native void nativeScheduleRedraw(long nativeJniDisplayHandler); |
| 180 } |
| OLD | NEW |