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 | 74beb89 | 2013-03-21 23:40:56 -0700 | [diff] [blame] | 65 | mCTable = ctable; |
| 66 | mPixels = NULL; |
| 67 | mFormat = 0; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 68 | SkSafeRef(ctable); |
| 69 | setImmutable(); |
| 70 | } |
| 71 | |
| 72 | virtual ~ScreenshotPixelRef() { |
Mathias Agopian | 74beb89 | 2013-03-21 23:40:56 -0700 | [diff] [blame] | 73 | SkSafeUnref(mCTable); |
| 74 | delete [] mPixels; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | status_t update(const sp<IBinder>& display, int width, int height, |
| 78 | int minLayer, int maxLayer, bool allLayers) { |
| 79 | status_t res = (width > 0 && height > 0) |
| 80 | ? (allLayers |
| 81 | ? mScreenshot.update(display, width, height) |
| 82 | : mScreenshot.update(display, width, height, minLayer, maxLayer)) |
| 83 | : mScreenshot.update(display); |
Mathias Agopian | 74beb89 | 2013-03-21 23:40:56 -0700 | [diff] [blame] | 84 | if (res == NO_ERROR) { |
| 85 | mWidth = mScreenshot.getWidth(); |
| 86 | mHeight = mScreenshot.getHeight(); |
| 87 | mStride = mScreenshot.getStride(); |
| 88 | mFormat = mScreenshot.getFormat(); |
| 89 | switch (mFormat) { |
| 90 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 91 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 92 | case HAL_PIXEL_FORMAT_RGB_565: |
| 93 | break; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 94 | |
Mathias Agopian | 74beb89 | 2013-03-21 23:40:56 -0700 | [diff] [blame] | 95 | case HAL_PIXEL_FORMAT_BGRA_8888: { |
| 96 | // common format not supported by Bitmap.java |
| 97 | size_t w = mScreenshot.getWidth(); |
| 98 | size_t h = mScreenshot.getHeight(); |
| 99 | size_t s = mScreenshot.getStride(); |
| 100 | |
| 101 | mPixels = new uint32_t[s*h]; |
| 102 | if (mPixels == NULL) { |
| 103 | res = NO_MEMORY; |
| 104 | break; |
| 105 | } |
| 106 | |
| 107 | uint32_t const* src = (uint32_t const*)mScreenshot.getPixels(); |
| 108 | uint32_t* dst = mPixels; |
| 109 | for (size_t y=0 ; y<h ; y++) { |
| 110 | for (size_t x=0 ; x<w ; x++) { |
| 111 | // convert BGRA (0xARGB) to RGBA (0xABGR) |
| 112 | uint32_t pixel = src[x]; |
| 113 | dst[x] = (pixel & 0xFF00FF00) | |
| 114 | ((pixel >> 16) & 0xFF) | |
| 115 | ((pixel << 16) & 0xFF0000); |
| 116 | } |
| 117 | src += s; |
| 118 | dst += s; |
| 119 | } |
| 120 | |
| 121 | mFormat = HAL_PIXEL_FORMAT_RGBA_8888; |
| 122 | mScreenshot.release(); |
| 123 | } break; |
| 124 | |
| 125 | default: |
| 126 | // ugh. that's a real problem |
| 127 | res = BAD_VALUE; |
| 128 | mScreenshot.release(); |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | return res; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | uint32_t getWidth() const { |
Mathias Agopian | 74beb89 | 2013-03-21 23:40:56 -0700 | [diff] [blame] | 136 | return mWidth; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | uint32_t getHeight() const { |
Mathias Agopian | 74beb89 | 2013-03-21 23:40:56 -0700 | [diff] [blame] | 140 | return mHeight; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | uint32_t getStride() const { |
Mathias Agopian | 74beb89 | 2013-03-21 23:40:56 -0700 | [diff] [blame] | 144 | return mStride; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | uint32_t getFormat() const { |
Mathias Agopian | 74beb89 | 2013-03-21 23:40:56 -0700 | [diff] [blame] | 148 | return mFormat; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | protected: |
| 152 | // overrides from SkPixelRef |
| 153 | virtual void* onLockPixels(SkColorTable** ct) { |
Mathias Agopian | 74beb89 | 2013-03-21 23:40:56 -0700 | [diff] [blame] | 154 | *ct = mCTable; |
| 155 | return mPixels ? (void*)mPixels : (void*)mScreenshot.getPixels(); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | virtual void onUnlockPixels() { |
| 159 | } |
| 160 | |
Kristian Monsen | e32e2b3 | 2013-02-14 21:35:35 -0800 | [diff] [blame] | 161 | SK_DECLARE_UNFLATTENABLE_OBJECT() |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 162 | private: |
| 163 | ScreenshotClient mScreenshot; |
Mathias Agopian | 74beb89 | 2013-03-21 23:40:56 -0700 | [diff] [blame] | 164 | SkColorTable* mCTable; |
| 165 | uint32_t* mPixels; |
| 166 | uint32_t mWidth; |
| 167 | uint32_t mHeight; |
| 168 | uint32_t mStride; |
| 169 | uint32_t mFormat; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 170 | |
| 171 | typedef SkPixelRef INHERITED; |
| 172 | }; |
| 173 | |
| 174 | |
| 175 | // ---------------------------------------------------------------------------- |
| 176 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 177 | static jint nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 178 | jstring nameStr, jint w, jint h, jint format, jint flags) { |
| 179 | ScopedUtfChars name(env, nameStr); |
| 180 | sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj)); |
| 181 | sp<SurfaceControl> surface = client->createSurface( |
| 182 | String8(name.c_str()), w, h, format, flags); |
| 183 | if (surface == NULL) { |
| 184 | jniThrowException(env, OutOfResourcesException, NULL); |
| 185 | return 0; |
| 186 | } |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 187 | surface->incStrong((void *)nativeCreate); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 188 | return int(surface.get()); |
| 189 | } |
| 190 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 191 | static void nativeRelease(JNIEnv* env, jclass clazz, jint nativeObject) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 192 | sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject)); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 193 | ctrl->decStrong((void *)nativeCreate); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 196 | static void nativeDestroy(JNIEnv* env, jclass clazz, jint nativeObject) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 197 | sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject)); |
| 198 | ctrl->clear(); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 199 | ctrl->decStrong((void *)nativeCreate); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static inline SkBitmap::Config convertPixelFormat(PixelFormat format) { |
| 203 | /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then |
| 204 | we can map to SkBitmap::kARGB_8888_Config, and optionally call |
| 205 | bitmap.setIsOpaque(true) on the resulting SkBitmap (as an accelerator) |
| 206 | */ |
| 207 | switch (format) { |
| 208 | case PIXEL_FORMAT_RGBX_8888: return SkBitmap::kARGB_8888_Config; |
| 209 | case PIXEL_FORMAT_RGBA_8888: return SkBitmap::kARGB_8888_Config; |
| 210 | case PIXEL_FORMAT_RGBA_4444: return SkBitmap::kARGB_4444_Config; |
| 211 | case PIXEL_FORMAT_RGB_565: return SkBitmap::kRGB_565_Config; |
| 212 | case PIXEL_FORMAT_A_8: return SkBitmap::kA8_Config; |
| 213 | default: return SkBitmap::kNo_Config; |
| 214 | } |
| 215 | } |
| 216 | |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 217 | static jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz, jobject displayTokenObj, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 218 | jint width, jint height, jint minLayer, jint maxLayer, bool allLayers) { |
| 219 | sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj); |
| 220 | if (displayToken == NULL) { |
| 221 | return NULL; |
| 222 | } |
| 223 | |
| 224 | ScreenshotPixelRef* pixels = new ScreenshotPixelRef(NULL); |
| 225 | if (pixels->update(displayToken, width, height, |
| 226 | minLayer, maxLayer, allLayers) != NO_ERROR) { |
| 227 | delete pixels; |
| 228 | return NULL; |
| 229 | } |
| 230 | |
| 231 | uint32_t w = pixels->getWidth(); |
| 232 | uint32_t h = pixels->getHeight(); |
| 233 | uint32_t s = pixels->getStride(); |
| 234 | uint32_t f = pixels->getFormat(); |
| 235 | ssize_t bpr = s * android::bytesPerPixel(f); |
| 236 | |
| 237 | SkBitmap* bitmap = new SkBitmap(); |
| 238 | bitmap->setConfig(convertPixelFormat(f), w, h, bpr); |
| 239 | if (f == PIXEL_FORMAT_RGBX_8888) { |
| 240 | bitmap->setIsOpaque(true); |
| 241 | } |
| 242 | |
| 243 | if (w > 0 && h > 0) { |
| 244 | bitmap->setPixelRef(pixels)->unref(); |
| 245 | bitmap->lockPixels(); |
| 246 | } else { |
| 247 | // be safe with an empty bitmap. |
| 248 | delete pixels; |
| 249 | bitmap->setPixels(NULL); |
| 250 | } |
| 251 | |
| 252 | return GraphicsJNI::createBitmap(env, bitmap, false, NULL); |
| 253 | } |
| 254 | |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 255 | static void nativeScreenshot(JNIEnv* env, jclass clazz, |
| 256 | jobject displayTokenObj, jobject surfaceObj, |
| 257 | jint width, jint height, jint minLayer, jint maxLayer, bool allLayers) { |
| 258 | sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj); |
| 259 | if (displayToken != NULL) { |
| 260 | sp<Surface> consumer = android_view_Surface_getSurface(env, surfaceObj); |
| 261 | if (consumer != NULL) { |
| 262 | if (allLayers) { |
| 263 | minLayer = 0; |
| 264 | maxLayer = -1; |
| 265 | } |
| 266 | ScreenshotClient::capture( |
| 267 | displayToken, consumer->getIGraphicBufferProducer(), |
| 268 | width, height, uint32_t(minLayer), uint32_t(maxLayer)); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 273 | static void nativeOpenTransaction(JNIEnv* env, jclass clazz) { |
| 274 | SurfaceComposerClient::openGlobalTransaction(); |
| 275 | } |
| 276 | |
| 277 | static void nativeCloseTransaction(JNIEnv* env, jclass clazz) { |
| 278 | SurfaceComposerClient::closeGlobalTransaction(); |
| 279 | } |
| 280 | |
| 281 | static void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz) { |
| 282 | SurfaceComposerClient::setAnimationTransaction(); |
| 283 | } |
| 284 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 285 | static void nativeSetLayer(JNIEnv* env, jclass clazz, jint nativeObject, jint zorder) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 286 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 287 | status_t err = ctrl->setLayer(zorder); |
| 288 | if (err < 0 && err != NO_INIT) { |
| 289 | doThrowIAE(env); |
| 290 | } |
| 291 | } |
| 292 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 293 | 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] | 294 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 295 | status_t err = ctrl->setPosition(x, y); |
| 296 | if (err < 0 && err != NO_INIT) { |
| 297 | doThrowIAE(env); |
| 298 | } |
| 299 | } |
| 300 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 301 | 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] | 302 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 303 | status_t err = ctrl->setSize(w, h); |
| 304 | if (err < 0 && err != NO_INIT) { |
| 305 | doThrowIAE(env); |
| 306 | } |
| 307 | } |
| 308 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 309 | 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] | 310 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 311 | status_t err = ctrl->setFlags(flags, mask); |
| 312 | if (err < 0 && err != NO_INIT) { |
| 313 | doThrowIAE(env); |
| 314 | } |
| 315 | } |
| 316 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 317 | static void nativeSetTransparentRegionHint(JNIEnv* env, jclass clazz, jint nativeObject, jobject regionObj) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 318 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 319 | SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj); |
| 320 | if (!region) { |
| 321 | doThrowIAE(env); |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | const SkIRect& b(region->getBounds()); |
| 326 | Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom)); |
| 327 | if (region->isComplex()) { |
| 328 | SkRegion::Iterator it(*region); |
| 329 | while (!it.done()) { |
| 330 | const SkIRect& r(it.rect()); |
| 331 | reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom); |
| 332 | it.next(); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | status_t err = ctrl->setTransparentRegionHint(reg); |
| 337 | if (err < 0 && err != NO_INIT) { |
| 338 | doThrowIAE(env); |
| 339 | } |
| 340 | } |
| 341 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 342 | static void nativeSetAlpha(JNIEnv* env, jclass clazz, jint nativeObject, jfloat alpha) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 343 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 344 | status_t err = ctrl->setAlpha(alpha); |
| 345 | if (err < 0 && err != NO_INIT) { |
| 346 | doThrowIAE(env); |
| 347 | } |
| 348 | } |
| 349 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 350 | static void nativeSetMatrix(JNIEnv* env, jclass clazz, jint nativeObject, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 351 | jfloat dsdx, jfloat dtdx, jfloat dsdy, jfloat dtdy) { |
| 352 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 353 | status_t err = ctrl->setMatrix(dsdx, dtdx, dsdy, dtdy); |
| 354 | if (err < 0 && err != NO_INIT) { |
| 355 | doThrowIAE(env); |
| 356 | } |
| 357 | } |
| 358 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 359 | static void nativeSetWindowCrop(JNIEnv* env, jclass clazz, jint nativeObject, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 360 | jint l, jint t, jint r, jint b) { |
| 361 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 362 | Rect crop(l, t, r, b); |
| 363 | status_t err = ctrl->setCrop(crop); |
| 364 | if (err < 0 && err != NO_INIT) { |
| 365 | doThrowIAE(env); |
| 366 | } |
| 367 | } |
| 368 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 369 | static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jint nativeObject, jint layerStack) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 370 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 371 | status_t err = ctrl->setLayerStack(layerStack); |
| 372 | if (err < 0 && err != NO_INIT) { |
| 373 | doThrowIAE(env); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | static jobject nativeGetBuiltInDisplay(JNIEnv* env, jclass clazz, jint id) { |
| 378 | sp<IBinder> token(SurfaceComposerClient::getBuiltInDisplay(id)); |
| 379 | return javaObjectForIBinder(env, token); |
| 380 | } |
| 381 | |
| 382 | static jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj, |
| 383 | jboolean secure) { |
| 384 | ScopedUtfChars name(env, nameObj); |
| 385 | sp<IBinder> token(SurfaceComposerClient::createDisplay( |
| 386 | String8(name.c_str()), bool(secure))); |
| 387 | return javaObjectForIBinder(env, token); |
| 388 | } |
| 389 | |
| 390 | static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz, |
| 391 | jobject tokenObj, jint nativeSurfaceObject) { |
| 392 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 393 | if (token == NULL) return; |
Mathias Agopian | ffddc9b | 2013-02-25 15:56:31 -0800 | [diff] [blame] | 394 | sp<IGraphicBufferProducer> bufferProducer; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 395 | sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject)); |
Mathias Agopian | ffddc9b | 2013-02-25 15:56:31 -0800 | [diff] [blame] | 396 | if (sur != NULL) { |
| 397 | bufferProducer = sur->getIGraphicBufferProducer(); |
| 398 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 399 | SurfaceComposerClient::setDisplaySurface(token, bufferProducer); |
| 400 | } |
| 401 | |
| 402 | static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz, |
| 403 | jobject tokenObj, jint layerStack) { |
| 404 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 405 | if (token == NULL) return; |
| 406 | |
| 407 | SurfaceComposerClient::setDisplayLayerStack(token, layerStack); |
| 408 | } |
| 409 | |
| 410 | static void nativeSetDisplayProjection(JNIEnv* env, jclass clazz, |
| 411 | jobject tokenObj, jint orientation, |
| 412 | jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom, |
| 413 | jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) { |
| 414 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 415 | if (token == NULL) return; |
| 416 | Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom); |
| 417 | Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom); |
| 418 | SurfaceComposerClient::setDisplayProjection(token, orientation, layerStackRect, displayRect); |
| 419 | } |
| 420 | |
| 421 | static jboolean nativeGetDisplayInfo(JNIEnv* env, jclass clazz, |
| 422 | jobject tokenObj, jobject infoObj) { |
| 423 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 424 | if (token == NULL) return JNI_FALSE; |
| 425 | |
| 426 | DisplayInfo info; |
| 427 | if (SurfaceComposerClient::getDisplayInfo(token, &info)) { |
| 428 | return JNI_FALSE; |
| 429 | } |
| 430 | |
| 431 | env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w); |
| 432 | env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h); |
| 433 | env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps); |
| 434 | env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density); |
| 435 | env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi); |
| 436 | env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi); |
| 437 | env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure); |
| 438 | return JNI_TRUE; |
| 439 | } |
| 440 | |
| 441 | static void nativeBlankDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) { |
| 442 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 443 | if (token == NULL) return; |
| 444 | |
| 445 | ALOGD_IF_SLOW(100, "Excessive delay in blankDisplay() while turning screen off"); |
| 446 | SurfaceComposerClient::blankDisplay(token); |
| 447 | } |
| 448 | |
| 449 | static void nativeUnblankDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) { |
| 450 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 451 | if (token == NULL) return; |
| 452 | |
| 453 | ALOGD_IF_SLOW(100, "Excessive delay in unblankDisplay() while turning screen on"); |
| 454 | SurfaceComposerClient::unblankDisplay(token); |
| 455 | } |
| 456 | |
| 457 | // ---------------------------------------------------------------------------- |
| 458 | |
| 459 | static JNINativeMethod sSurfaceControlMethods[] = { |
| 460 | {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)I", |
| 461 | (void*)nativeCreate }, |
| 462 | {"nativeRelease", "(I)V", |
| 463 | (void*)nativeRelease }, |
| 464 | {"nativeDestroy", "(I)V", |
| 465 | (void*)nativeDestroy }, |
| 466 | {"nativeScreenshot", "(Landroid/os/IBinder;IIIIZ)Landroid/graphics/Bitmap;", |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 467 | (void*)nativeScreenshotBitmap }, |
| 468 | {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/view/Surface;IIIIZ)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 469 | (void*)nativeScreenshot }, |
| 470 | {"nativeOpenTransaction", "()V", |
| 471 | (void*)nativeOpenTransaction }, |
| 472 | {"nativeCloseTransaction", "()V", |
| 473 | (void*)nativeCloseTransaction }, |
| 474 | {"nativeSetAnimationTransaction", "()V", |
| 475 | (void*)nativeSetAnimationTransaction }, |
| 476 | {"nativeSetLayer", "(II)V", |
| 477 | (void*)nativeSetLayer }, |
| 478 | {"nativeSetPosition", "(IFF)V", |
| 479 | (void*)nativeSetPosition }, |
| 480 | {"nativeSetSize", "(III)V", |
| 481 | (void*)nativeSetSize }, |
| 482 | {"nativeSetTransparentRegionHint", "(ILandroid/graphics/Region;)V", |
| 483 | (void*)nativeSetTransparentRegionHint }, |
| 484 | {"nativeSetAlpha", "(IF)V", |
| 485 | (void*)nativeSetAlpha }, |
| 486 | {"nativeSetMatrix", "(IFFFF)V", |
| 487 | (void*)nativeSetMatrix }, |
| 488 | {"nativeSetFlags", "(III)V", |
| 489 | (void*)nativeSetFlags }, |
| 490 | {"nativeSetWindowCrop", "(IIIII)V", |
| 491 | (void*)nativeSetWindowCrop }, |
| 492 | {"nativeSetLayerStack", "(II)V", |
| 493 | (void*)nativeSetLayerStack }, |
| 494 | {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;", |
| 495 | (void*)nativeGetBuiltInDisplay }, |
| 496 | {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;", |
| 497 | (void*)nativeCreateDisplay }, |
| 498 | {"nativeSetDisplaySurface", "(Landroid/os/IBinder;I)V", |
| 499 | (void*)nativeSetDisplaySurface }, |
| 500 | {"nativeSetDisplayLayerStack", "(Landroid/os/IBinder;I)V", |
| 501 | (void*)nativeSetDisplayLayerStack }, |
| 502 | {"nativeSetDisplayProjection", "(Landroid/os/IBinder;IIIIIIIII)V", |
| 503 | (void*)nativeSetDisplayProjection }, |
| 504 | {"nativeGetDisplayInfo", "(Landroid/os/IBinder;Landroid/view/SurfaceControl$PhysicalDisplayInfo;)Z", |
| 505 | (void*)nativeGetDisplayInfo }, |
| 506 | {"nativeBlankDisplay", "(Landroid/os/IBinder;)V", |
| 507 | (void*)nativeBlankDisplay }, |
| 508 | {"nativeUnblankDisplay", "(Landroid/os/IBinder;)V", |
| 509 | (void*)nativeUnblankDisplay }, |
| 510 | }; |
| 511 | |
| 512 | int register_android_view_SurfaceControl(JNIEnv* env) |
| 513 | { |
| 514 | int err = AndroidRuntime::registerNativeMethods(env, "android/view/SurfaceControl", |
| 515 | sSurfaceControlMethods, NELEM(sSurfaceControlMethods)); |
| 516 | |
| 517 | jclass clazz = env->FindClass("android/view/SurfaceControl$PhysicalDisplayInfo"); |
| 518 | gPhysicalDisplayInfoClassInfo.width = env->GetFieldID(clazz, "width", "I"); |
| 519 | gPhysicalDisplayInfoClassInfo.height = env->GetFieldID(clazz, "height", "I"); |
| 520 | gPhysicalDisplayInfoClassInfo.refreshRate = env->GetFieldID(clazz, "refreshRate", "F"); |
| 521 | gPhysicalDisplayInfoClassInfo.density = env->GetFieldID(clazz, "density", "F"); |
| 522 | gPhysicalDisplayInfoClassInfo.xDpi = env->GetFieldID(clazz, "xDpi", "F"); |
| 523 | gPhysicalDisplayInfoClassInfo.yDpi = env->GetFieldID(clazz, "yDpi", "F"); |
| 524 | gPhysicalDisplayInfoClassInfo.secure = env->GetFieldID(clazz, "secure", "Z"); |
| 525 | return err; |
| 526 | } |
| 527 | |
| 528 | }; |