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