Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "SurfaceControl" |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | |
| 21 | #include "jni.h" |
| 22 | #include "JNIHelp.h" |
| 23 | |
| 24 | #include "android_os_Parcel.h" |
| 25 | #include "android_util_Binder.h" |
| 26 | #include "android/graphics/GraphicsJNI.h" |
| 27 | #include "android/graphics/Region.h" |
| 28 | |
| 29 | #include <android_runtime/AndroidRuntime.h> |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 30 | #include <android_runtime/android_view_Surface.h> |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 31 | #include <android_runtime/android_view_SurfaceSession.h> |
| 32 | |
| 33 | #include <gui/Surface.h> |
| 34 | #include <gui/SurfaceComposerClient.h> |
| 35 | |
| 36 | #include <ui/DisplayInfo.h> |
| 37 | #include <ui/Rect.h> |
| 38 | #include <ui/Region.h> |
| 39 | |
| 40 | #include <utils/Log.h> |
| 41 | |
| 42 | #include <ScopedUtfChars.h> |
| 43 | |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | |
| 46 | namespace android { |
| 47 | |
| 48 | static const char* const OutOfResourcesException = |
| 49 | "android/view/Surface$OutOfResourcesException"; |
| 50 | |
| 51 | static struct { |
| 52 | jfieldID width; |
| 53 | jfieldID height; |
| 54 | jfieldID refreshRate; |
| 55 | jfieldID density; |
| 56 | jfieldID xDpi; |
| 57 | jfieldID yDpi; |
| 58 | jfieldID secure; |
| 59 | } gPhysicalDisplayInfoClassInfo; |
| 60 | |
| 61 | |
| 62 | class ScreenshotPixelRef : public SkPixelRef { |
| 63 | public: |
| 64 | ScreenshotPixelRef(SkColorTable* ctable) { |
Mathias Agopian | 32b9f62 | 2013-03-22 00:27:46 -0700 | [diff] [blame] | 65 | fCTable = ctable; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 66 | SkSafeRef(ctable); |
| 67 | setImmutable(); |
| 68 | } |
| 69 | |
| 70 | virtual ~ScreenshotPixelRef() { |
Mathias Agopian | 32b9f62 | 2013-03-22 00:27:46 -0700 | [diff] [blame] | 71 | SkSafeUnref(fCTable); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | status_t update(const sp<IBinder>& display, int width, int height, |
| 75 | int minLayer, int maxLayer, bool allLayers) { |
| 76 | status_t res = (width > 0 && height > 0) |
| 77 | ? (allLayers |
| 78 | ? mScreenshot.update(display, width, height) |
| 79 | : mScreenshot.update(display, width, height, minLayer, maxLayer)) |
| 80 | : mScreenshot.update(display); |
Mathias Agopian | 32b9f62 | 2013-03-22 00:27:46 -0700 | [diff] [blame] | 81 | if (res != NO_ERROR) { |
| 82 | return res; |
Mathias Agopian | 74beb89 | 2013-03-21 23:40:56 -0700 | [diff] [blame] | 83 | } |
Mathias Agopian | 32b9f62 | 2013-03-22 00:27:46 -0700 | [diff] [blame] | 84 | |
| 85 | return NO_ERROR; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | uint32_t getWidth() const { |
Mathias Agopian | 32b9f62 | 2013-03-22 00:27:46 -0700 | [diff] [blame] | 89 | return mScreenshot.getWidth(); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | uint32_t getHeight() const { |
Mathias Agopian | 32b9f62 | 2013-03-22 00:27:46 -0700 | [diff] [blame] | 93 | return mScreenshot.getHeight(); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | uint32_t getStride() const { |
Mathias Agopian | 32b9f62 | 2013-03-22 00:27:46 -0700 | [diff] [blame] | 97 | return mScreenshot.getStride(); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | uint32_t getFormat() const { |
Mathias Agopian | 32b9f62 | 2013-03-22 00:27:46 -0700 | [diff] [blame] | 101 | return mScreenshot.getFormat(); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | protected: |
| 105 | // overrides from SkPixelRef |
| 106 | virtual void* onLockPixels(SkColorTable** ct) { |
Mathias Agopian | 32b9f62 | 2013-03-22 00:27:46 -0700 | [diff] [blame] | 107 | *ct = fCTable; |
| 108 | return (void*)mScreenshot.getPixels(); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | virtual void onUnlockPixels() { |
| 112 | } |
| 113 | |
Kristian Monsen | e32e2b3 | 2013-02-14 21:35:35 -0800 | [diff] [blame] | 114 | SK_DECLARE_UNFLATTENABLE_OBJECT() |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 115 | private: |
| 116 | ScreenshotClient mScreenshot; |
Mathias Agopian | 32b9f62 | 2013-03-22 00:27:46 -0700 | [diff] [blame] | 117 | SkColorTable* fCTable; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 118 | |
| 119 | typedef SkPixelRef INHERITED; |
| 120 | }; |
| 121 | |
| 122 | |
| 123 | // ---------------------------------------------------------------------------- |
| 124 | |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 125 | static jint nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 126 | jstring nameStr, jint w, jint h, jint format, jint flags) { |
| 127 | ScopedUtfChars name(env, nameStr); |
| 128 | sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj)); |
| 129 | sp<SurfaceControl> surface = client->createSurface( |
| 130 | String8(name.c_str()), w, h, format, flags); |
| 131 | if (surface == NULL) { |
| 132 | jniThrowException(env, OutOfResourcesException, NULL); |
| 133 | return 0; |
| 134 | } |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 135 | surface->incStrong((void *)nativeCreate); |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 136 | return int(surface.get()); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 139 | static void nativeRelease(JNIEnv* env, jclass clazz, jint nativeObject) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 140 | sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject)); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 141 | ctrl->decStrong((void *)nativeCreate); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 144 | static void nativeDestroy(JNIEnv* env, jclass clazz, jint nativeObject) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 145 | sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject)); |
| 146 | ctrl->clear(); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 147 | ctrl->decStrong((void *)nativeCreate); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static inline SkBitmap::Config convertPixelFormat(PixelFormat format) { |
| 151 | /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then |
| 152 | we can map to SkBitmap::kARGB_8888_Config, and optionally call |
Leon Scroggins III | 8790be6 | 2013-12-03 16:26:51 -0500 | [diff] [blame] | 153 | bitmap.setAlphaType(kOpaque_SkAlphaType) on the resulting SkBitmap |
| 154 | (as an accelerator) |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 155 | */ |
| 156 | switch (format) { |
| 157 | case PIXEL_FORMAT_RGBX_8888: return SkBitmap::kARGB_8888_Config; |
| 158 | case PIXEL_FORMAT_RGBA_8888: return SkBitmap::kARGB_8888_Config; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 159 | case PIXEL_FORMAT_RGB_565: return SkBitmap::kRGB_565_Config; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 160 | default: return SkBitmap::kNo_Config; |
| 161 | } |
| 162 | } |
| 163 | |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 164 | static jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz, jobject displayTokenObj, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 165 | jint width, jint height, jint minLayer, jint maxLayer, bool allLayers) { |
| 166 | sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj); |
| 167 | if (displayToken == NULL) { |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | ScreenshotPixelRef* pixels = new ScreenshotPixelRef(NULL); |
| 172 | if (pixels->update(displayToken, width, height, |
| 173 | minLayer, maxLayer, allLayers) != NO_ERROR) { |
| 174 | delete pixels; |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | uint32_t w = pixels->getWidth(); |
| 179 | uint32_t h = pixels->getHeight(); |
| 180 | uint32_t s = pixels->getStride(); |
| 181 | uint32_t f = pixels->getFormat(); |
| 182 | ssize_t bpr = s * android::bytesPerPixel(f); |
| 183 | |
| 184 | SkBitmap* bitmap = new SkBitmap(); |
| 185 | bitmap->setConfig(convertPixelFormat(f), w, h, bpr); |
| 186 | if (f == PIXEL_FORMAT_RGBX_8888) { |
Leon Scroggins III | 8790be6 | 2013-12-03 16:26:51 -0500 | [diff] [blame] | 187 | bitmap->setAlphaType(kOpaque_SkAlphaType); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | if (w > 0 && h > 0) { |
| 191 | bitmap->setPixelRef(pixels)->unref(); |
| 192 | bitmap->lockPixels(); |
| 193 | } else { |
| 194 | // be safe with an empty bitmap. |
| 195 | delete pixels; |
| 196 | bitmap->setPixels(NULL); |
| 197 | } |
| 198 | |
Chris Craik | 1abf5d6 | 2013-08-16 12:47:03 -0700 | [diff] [blame] | 199 | return GraphicsJNI::createBitmap(env, bitmap, |
| 200 | GraphicsJNI::kBitmapCreateFlag_Premultiplied, NULL); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 203 | static void nativeScreenshot(JNIEnv* env, jclass clazz, |
| 204 | jobject displayTokenObj, jobject surfaceObj, |
| 205 | jint width, jint height, jint minLayer, jint maxLayer, bool allLayers) { |
| 206 | sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj); |
| 207 | if (displayToken != NULL) { |
| 208 | sp<Surface> consumer = android_view_Surface_getSurface(env, surfaceObj); |
| 209 | if (consumer != NULL) { |
| 210 | if (allLayers) { |
| 211 | minLayer = 0; |
| 212 | maxLayer = -1; |
| 213 | } |
| 214 | ScreenshotClient::capture( |
| 215 | displayToken, consumer->getIGraphicBufferProducer(), |
| 216 | width, height, uint32_t(minLayer), uint32_t(maxLayer)); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 221 | static void nativeOpenTransaction(JNIEnv* env, jclass clazz) { |
| 222 | SurfaceComposerClient::openGlobalTransaction(); |
| 223 | } |
| 224 | |
| 225 | static void nativeCloseTransaction(JNIEnv* env, jclass clazz) { |
| 226 | SurfaceComposerClient::closeGlobalTransaction(); |
| 227 | } |
| 228 | |
| 229 | static void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz) { |
| 230 | SurfaceComposerClient::setAnimationTransaction(); |
| 231 | } |
| 232 | |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 233 | static void nativeSetLayer(JNIEnv* env, jclass clazz, jint nativeObject, jint zorder) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 234 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 235 | status_t err = ctrl->setLayer(zorder); |
| 236 | if (err < 0 && err != NO_INIT) { |
| 237 | doThrowIAE(env); |
| 238 | } |
| 239 | } |
| 240 | |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 241 | static void nativeSetPosition(JNIEnv* env, jclass clazz, jint nativeObject, jfloat x, jfloat y) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 242 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 243 | status_t err = ctrl->setPosition(x, y); |
| 244 | if (err < 0 && err != NO_INIT) { |
| 245 | doThrowIAE(env); |
| 246 | } |
| 247 | } |
| 248 | |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 249 | static void nativeSetSize(JNIEnv* env, jclass clazz, jint nativeObject, jint w, jint h) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 250 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 251 | status_t err = ctrl->setSize(w, h); |
| 252 | if (err < 0 && err != NO_INIT) { |
| 253 | doThrowIAE(env); |
| 254 | } |
| 255 | } |
| 256 | |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 257 | static void nativeSetFlags(JNIEnv* env, jclass clazz, jint nativeObject, jint flags, jint mask) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 258 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 259 | status_t err = ctrl->setFlags(flags, mask); |
| 260 | if (err < 0 && err != NO_INIT) { |
| 261 | doThrowIAE(env); |
| 262 | } |
| 263 | } |
| 264 | |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 265 | static void nativeSetTransparentRegionHint(JNIEnv* env, jclass clazz, jint nativeObject, jobject regionObj) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 266 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 267 | SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj); |
| 268 | if (!region) { |
| 269 | doThrowIAE(env); |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | const SkIRect& b(region->getBounds()); |
| 274 | Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom)); |
| 275 | if (region->isComplex()) { |
| 276 | SkRegion::Iterator it(*region); |
| 277 | while (!it.done()) { |
| 278 | const SkIRect& r(it.rect()); |
| 279 | reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom); |
| 280 | it.next(); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | status_t err = ctrl->setTransparentRegionHint(reg); |
| 285 | if (err < 0 && err != NO_INIT) { |
| 286 | doThrowIAE(env); |
| 287 | } |
| 288 | } |
| 289 | |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 290 | static void nativeSetAlpha(JNIEnv* env, jclass clazz, jint nativeObject, jfloat alpha) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 291 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 292 | status_t err = ctrl->setAlpha(alpha); |
| 293 | if (err < 0 && err != NO_INIT) { |
| 294 | doThrowIAE(env); |
| 295 | } |
| 296 | } |
| 297 | |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 298 | static void nativeSetMatrix(JNIEnv* env, jclass clazz, jint nativeObject, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 299 | jfloat dsdx, jfloat dtdx, jfloat dsdy, jfloat dtdy) { |
| 300 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 301 | status_t err = ctrl->setMatrix(dsdx, dtdx, dsdy, dtdy); |
| 302 | if (err < 0 && err != NO_INIT) { |
| 303 | doThrowIAE(env); |
| 304 | } |
| 305 | } |
| 306 | |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 307 | static void nativeSetWindowCrop(JNIEnv* env, jclass clazz, jint nativeObject, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 308 | jint l, jint t, jint r, jint b) { |
| 309 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 310 | Rect crop(l, t, r, b); |
| 311 | status_t err = ctrl->setCrop(crop); |
| 312 | if (err < 0 && err != NO_INIT) { |
| 313 | doThrowIAE(env); |
| 314 | } |
| 315 | } |
| 316 | |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 317 | static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jint nativeObject, jint layerStack) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 318 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 319 | status_t err = ctrl->setLayerStack(layerStack); |
| 320 | if (err < 0 && err != NO_INIT) { |
| 321 | doThrowIAE(env); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | static jobject nativeGetBuiltInDisplay(JNIEnv* env, jclass clazz, jint id) { |
| 326 | sp<IBinder> token(SurfaceComposerClient::getBuiltInDisplay(id)); |
| 327 | return javaObjectForIBinder(env, token); |
| 328 | } |
| 329 | |
| 330 | static jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj, |
| 331 | jboolean secure) { |
| 332 | ScopedUtfChars name(env, nameObj); |
| 333 | sp<IBinder> token(SurfaceComposerClient::createDisplay( |
| 334 | String8(name.c_str()), bool(secure))); |
| 335 | return javaObjectForIBinder(env, token); |
| 336 | } |
| 337 | |
Jesse Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 338 | static void nativeDestroyDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) { |
| 339 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 340 | if (token == NULL) return; |
| 341 | SurfaceComposerClient::destroyDisplay(token); |
| 342 | } |
| 343 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 344 | static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz, |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 345 | jobject tokenObj, jint nativeSurfaceObject) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 346 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 347 | if (token == NULL) return; |
Mathias Agopian | ffddc9b | 2013-02-25 15:56:31 -0800 | [diff] [blame] | 348 | sp<IGraphicBufferProducer> bufferProducer; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 349 | sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject)); |
Mathias Agopian | ffddc9b | 2013-02-25 15:56:31 -0800 | [diff] [blame] | 350 | if (sur != NULL) { |
| 351 | bufferProducer = sur->getIGraphicBufferProducer(); |
| 352 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 353 | SurfaceComposerClient::setDisplaySurface(token, bufferProducer); |
| 354 | } |
| 355 | |
| 356 | static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz, |
| 357 | jobject tokenObj, jint layerStack) { |
| 358 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 359 | if (token == NULL) return; |
| 360 | |
| 361 | SurfaceComposerClient::setDisplayLayerStack(token, layerStack); |
| 362 | } |
| 363 | |
| 364 | static void nativeSetDisplayProjection(JNIEnv* env, jclass clazz, |
| 365 | jobject tokenObj, jint orientation, |
| 366 | jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom, |
| 367 | jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) { |
| 368 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 369 | if (token == NULL) return; |
| 370 | Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom); |
| 371 | Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom); |
| 372 | SurfaceComposerClient::setDisplayProjection(token, orientation, layerStackRect, displayRect); |
| 373 | } |
| 374 | |
| 375 | static jboolean nativeGetDisplayInfo(JNIEnv* env, jclass clazz, |
| 376 | jobject tokenObj, jobject infoObj) { |
| 377 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 378 | if (token == NULL) return JNI_FALSE; |
| 379 | |
| 380 | DisplayInfo info; |
| 381 | if (SurfaceComposerClient::getDisplayInfo(token, &info)) { |
| 382 | return JNI_FALSE; |
| 383 | } |
| 384 | |
| 385 | env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w); |
| 386 | env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h); |
| 387 | env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps); |
| 388 | env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density); |
| 389 | env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi); |
| 390 | env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi); |
| 391 | env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure); |
| 392 | return JNI_TRUE; |
| 393 | } |
| 394 | |
| 395 | static void nativeBlankDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) { |
| 396 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 397 | if (token == NULL) return; |
| 398 | |
| 399 | ALOGD_IF_SLOW(100, "Excessive delay in blankDisplay() while turning screen off"); |
| 400 | SurfaceComposerClient::blankDisplay(token); |
| 401 | } |
| 402 | |
| 403 | static void nativeUnblankDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) { |
| 404 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 405 | if (token == NULL) return; |
| 406 | |
| 407 | ALOGD_IF_SLOW(100, "Excessive delay in unblankDisplay() while turning screen on"); |
| 408 | SurfaceComposerClient::unblankDisplay(token); |
| 409 | } |
| 410 | |
| 411 | // ---------------------------------------------------------------------------- |
| 412 | |
| 413 | static JNINativeMethod sSurfaceControlMethods[] = { |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 414 | {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)I", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 415 | (void*)nativeCreate }, |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 416 | {"nativeRelease", "(I)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 417 | (void*)nativeRelease }, |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 418 | {"nativeDestroy", "(I)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 419 | (void*)nativeDestroy }, |
| 420 | {"nativeScreenshot", "(Landroid/os/IBinder;IIIIZ)Landroid/graphics/Bitmap;", |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 421 | (void*)nativeScreenshotBitmap }, |
| 422 | {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/view/Surface;IIIIZ)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 423 | (void*)nativeScreenshot }, |
| 424 | {"nativeOpenTransaction", "()V", |
| 425 | (void*)nativeOpenTransaction }, |
| 426 | {"nativeCloseTransaction", "()V", |
| 427 | (void*)nativeCloseTransaction }, |
| 428 | {"nativeSetAnimationTransaction", "()V", |
| 429 | (void*)nativeSetAnimationTransaction }, |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 430 | {"nativeSetLayer", "(II)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 431 | (void*)nativeSetLayer }, |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 432 | {"nativeSetPosition", "(IFF)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 433 | (void*)nativeSetPosition }, |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 434 | {"nativeSetSize", "(III)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 435 | (void*)nativeSetSize }, |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 436 | {"nativeSetTransparentRegionHint", "(ILandroid/graphics/Region;)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 437 | (void*)nativeSetTransparentRegionHint }, |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 438 | {"nativeSetAlpha", "(IF)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 439 | (void*)nativeSetAlpha }, |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 440 | {"nativeSetMatrix", "(IFFFF)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 441 | (void*)nativeSetMatrix }, |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 442 | {"nativeSetFlags", "(III)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 443 | (void*)nativeSetFlags }, |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 444 | {"nativeSetWindowCrop", "(IIIII)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 445 | (void*)nativeSetWindowCrop }, |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 446 | {"nativeSetLayerStack", "(II)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 447 | (void*)nativeSetLayerStack }, |
| 448 | {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;", |
| 449 | (void*)nativeGetBuiltInDisplay }, |
| 450 | {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;", |
| 451 | (void*)nativeCreateDisplay }, |
Jesse Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 452 | {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V", |
| 453 | (void*)nativeDestroyDisplay }, |
Narayan Kamath | 7023df0 | 2014-01-27 14:21:38 +0000 | [diff] [blame^] | 454 | {"nativeSetDisplaySurface", "(Landroid/os/IBinder;I)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 455 | (void*)nativeSetDisplaySurface }, |
| 456 | {"nativeSetDisplayLayerStack", "(Landroid/os/IBinder;I)V", |
| 457 | (void*)nativeSetDisplayLayerStack }, |
| 458 | {"nativeSetDisplayProjection", "(Landroid/os/IBinder;IIIIIIIII)V", |
| 459 | (void*)nativeSetDisplayProjection }, |
| 460 | {"nativeGetDisplayInfo", "(Landroid/os/IBinder;Landroid/view/SurfaceControl$PhysicalDisplayInfo;)Z", |
| 461 | (void*)nativeGetDisplayInfo }, |
| 462 | {"nativeBlankDisplay", "(Landroid/os/IBinder;)V", |
| 463 | (void*)nativeBlankDisplay }, |
| 464 | {"nativeUnblankDisplay", "(Landroid/os/IBinder;)V", |
| 465 | (void*)nativeUnblankDisplay }, |
| 466 | }; |
| 467 | |
| 468 | int register_android_view_SurfaceControl(JNIEnv* env) |
| 469 | { |
| 470 | int err = AndroidRuntime::registerNativeMethods(env, "android/view/SurfaceControl", |
| 471 | sSurfaceControlMethods, NELEM(sSurfaceControlMethods)); |
| 472 | |
| 473 | jclass clazz = env->FindClass("android/view/SurfaceControl$PhysicalDisplayInfo"); |
| 474 | gPhysicalDisplayInfoClassInfo.width = env->GetFieldID(clazz, "width", "I"); |
| 475 | gPhysicalDisplayInfoClassInfo.height = env->GetFieldID(clazz, "height", "I"); |
| 476 | gPhysicalDisplayInfoClassInfo.refreshRate = env->GetFieldID(clazz, "refreshRate", "F"); |
| 477 | gPhysicalDisplayInfoClassInfo.density = env->GetFieldID(clazz, "density", "F"); |
| 478 | gPhysicalDisplayInfoClassInfo.xDpi = env->GetFieldID(clazz, "xDpi", "F"); |
| 479 | gPhysicalDisplayInfoClassInfo.yDpi = env->GetFieldID(clazz, "yDpi", "F"); |
| 480 | gPhysicalDisplayInfoClassInfo.secure = env->GetFieldID(clazz, "secure", "Z"); |
| 481 | return err; |
| 482 | } |
| 483 | |
| 484 | }; |