Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 "Surface" |
| 18 | #include <utils/Log.h> |
| 19 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 20 | #include <android/native_window_jni.h> |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 21 | #include <surfaceflinger/Surface.h> |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 22 | #include <android_runtime/android_view_Surface.h> |
tedbo | 0503161 | 2011-06-06 16:02:47 -0700 | [diff] [blame^] | 23 | #include <android_runtime/android_graphics_ParcelSurfaceTexture.h> |
Glenn Kasten | 846db33 | 2011-03-04 11:44:32 -0800 | [diff] [blame] | 24 | #include <android_runtime/android_graphics_SurfaceTexture.h> |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 25 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 26 | using namespace android; |
| 27 | |
| 28 | ANativeWindow* ANativeWindow_fromSurface(JNIEnv* env, jobject surface) { |
tedbo | 0503161 | 2011-06-06 16:02:47 -0700 | [diff] [blame^] | 29 | sp<ANativeWindow> win; |
| 30 | if (android_Surface_isInstanceOf(env, surface)) { |
| 31 | win = android_Surface_getNativeWindow(env, surface); |
| 32 | } else if (android_SurfaceTexture_isInstanceOf(env, surface)) { |
| 33 | win = android_SurfaceTexture_getNativeWindow(env, surface); |
| 34 | } else if (android_ParcelSurfaceTexture_isInstanceOf(env, surface)) { |
| 35 | win = android_ParcelSurfaceTexture_getNativeWindow(env, surface); |
| 36 | } |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 37 | if (win != NULL) { |
| 38 | win->incStrong((void*)ANativeWindow_acquire); |
| 39 | } |
| 40 | return win.get(); |
| 41 | } |
| 42 | |
Glenn Kasten | 846db33 | 2011-03-04 11:44:32 -0800 | [diff] [blame] | 43 | ANativeWindow* ANativeWindow_fromSurfaceTexture(JNIEnv* env, jobject surfaceTexture) { |
| 44 | sp<ANativeWindow> win = android_SurfaceTexture_getNativeWindow(env, surfaceTexture); |
| 45 | if (win != NULL) { |
| 46 | win->incStrong((void*)ANativeWindow_acquire); |
| 47 | } |
| 48 | return win.get(); |
| 49 | } |
| 50 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 51 | void ANativeWindow_acquire(ANativeWindow* window) { |
| 52 | window->incStrong((void*)ANativeWindow_acquire); |
| 53 | } |
| 54 | |
| 55 | void ANativeWindow_release(ANativeWindow* window) { |
| 56 | window->decStrong((void*)ANativeWindow_acquire); |
| 57 | } |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 58 | |
| 59 | static int32_t getWindowProp(ANativeWindow* window, int what) { |
| 60 | int value; |
| 61 | int res = window->query(window, what, &value); |
| 62 | return res < 0 ? res : value; |
| 63 | } |
| 64 | |
| 65 | int32_t ANativeWindow_getWidth(ANativeWindow* window) { |
| 66 | return getWindowProp(window, NATIVE_WINDOW_WIDTH); |
| 67 | } |
| 68 | |
| 69 | int32_t ANativeWindow_getHeight(ANativeWindow* window) { |
| 70 | return getWindowProp(window, NATIVE_WINDOW_HEIGHT); |
| 71 | } |
| 72 | |
| 73 | int32_t ANativeWindow_getFormat(ANativeWindow* window) { |
| 74 | return getWindowProp(window, NATIVE_WINDOW_FORMAT); |
| 75 | } |
Dianne Hackborn | 8ae5a8e | 2010-07-01 18:44:46 -0700 | [diff] [blame] | 76 | |
| 77 | int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window, int32_t width, |
Mathias Agopian | 3026a1c | 2010-10-24 18:35:26 -0700 | [diff] [blame] | 78 | int32_t height, int32_t format) { |
Mathias Agopian | da5a444 | 2011-03-31 20:59:58 -0700 | [diff] [blame] | 79 | return native_window_set_buffers_geometry(window, width, height, format); |
Dianne Hackborn | 8ae5a8e | 2010-07-01 18:44:46 -0700 | [diff] [blame] | 80 | } |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 81 | |
| 82 | int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer, |
| 83 | ARect* inOutDirtyBounds) { |
Jamie Gennis | bae716b | 2011-03-14 15:34:04 -0700 | [diff] [blame] | 84 | int type = -1; |
| 85 | if (window->query(window, NATIVE_WINDOW_CONCRETE_TYPE, &type) != 0 || |
| 86 | type != NATIVE_WINDOW_SURFACE) { |
| 87 | return BAD_VALUE; |
| 88 | } |
| 89 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 90 | Region dirtyRegion; |
| 91 | Region* dirtyParam = NULL; |
| 92 | if (inOutDirtyBounds != NULL) { |
| 93 | dirtyRegion.set(*(Rect*)inOutDirtyBounds); |
| 94 | dirtyParam = &dirtyRegion; |
| 95 | } |
| 96 | |
| 97 | Surface::SurfaceInfo info; |
| 98 | status_t res = static_cast<Surface*>(window)->lock(&info, dirtyParam); |
| 99 | if (res != OK) { |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | outBuffer->width = (int32_t)info.w; |
| 104 | outBuffer->height = (int32_t)info.h; |
| 105 | outBuffer->stride = (int32_t)info.s; |
| 106 | outBuffer->format = (int32_t)info.format; |
| 107 | outBuffer->bits = info.bits; |
| 108 | |
| 109 | if (inOutDirtyBounds != NULL) { |
| 110 | *inOutDirtyBounds = dirtyRegion.getBounds(); |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int32_t ANativeWindow_unlockAndPost(ANativeWindow* window) { |
| 117 | status_t res = static_cast<Surface*>(window)->unlockAndPost(); |
| 118 | return res == android::OK ? 0 : -1; |
| 119 | } |