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> |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 21 | #include <gui/Surface.h> |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 22 | #include <android_runtime/android_view_Surface.h> |
Glenn Kasten | 846db33 | 2011-03-04 11:44:32 -0800 | [diff] [blame] | 23 | #include <android_runtime/android_graphics_SurfaceTexture.h> |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 24 | |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 25 | using namespace android; |
| 26 | |
| 27 | ANativeWindow* ANativeWindow_fromSurface(JNIEnv* env, jobject surface) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 28 | sp<ANativeWindow> win = android_view_Surface_getNativeWindow(env, surface); |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 29 | if (win != NULL) { |
| 30 | win->incStrong((void*)ANativeWindow_acquire); |
| 31 | } |
| 32 | return win.get(); |
| 33 | } |
| 34 | |
| 35 | void ANativeWindow_acquire(ANativeWindow* window) { |
| 36 | window->incStrong((void*)ANativeWindow_acquire); |
| 37 | } |
| 38 | |
| 39 | void ANativeWindow_release(ANativeWindow* window) { |
| 40 | window->decStrong((void*)ANativeWindow_acquire); |
| 41 | } |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 42 | |
| 43 | static int32_t getWindowProp(ANativeWindow* window, int what) { |
| 44 | int value; |
| 45 | int res = window->query(window, what, &value); |
| 46 | return res < 0 ? res : value; |
| 47 | } |
| 48 | |
| 49 | int32_t ANativeWindow_getWidth(ANativeWindow* window) { |
| 50 | return getWindowProp(window, NATIVE_WINDOW_WIDTH); |
| 51 | } |
| 52 | |
| 53 | int32_t ANativeWindow_getHeight(ANativeWindow* window) { |
| 54 | return getWindowProp(window, NATIVE_WINDOW_HEIGHT); |
| 55 | } |
| 56 | |
| 57 | int32_t ANativeWindow_getFormat(ANativeWindow* window) { |
| 58 | return getWindowProp(window, NATIVE_WINDOW_FORMAT); |
| 59 | } |
Dianne Hackborn | 8ae5a8e | 2010-07-01 18:44:46 -0700 | [diff] [blame] | 60 | |
| 61 | int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window, int32_t width, |
Mathias Agopian | 3026a1c | 2010-10-24 18:35:26 -0700 | [diff] [blame] | 62 | int32_t height, int32_t format) { |
Michael I. Gold | 0e5ed70 | 2012-04-09 19:51:55 -0700 | [diff] [blame] | 63 | int32_t err = native_window_set_buffers_format(window, format); |
Mathias Agopian | 09d7ed7 | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 64 | if (!err) { |
Michael I. Gold | 0e5ed70 | 2012-04-09 19:51:55 -0700 | [diff] [blame] | 65 | err = native_window_set_buffers_user_dimensions(window, width, height); |
| 66 | if (!err) { |
| 67 | int mode = NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 68 | if (width && height) { |
| 69 | mode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW; |
| 70 | } |
| 71 | err = native_window_set_scaling_mode(window, mode); |
| 72 | } |
Mathias Agopian | 09d7ed7 | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 73 | } |
| 74 | return err; |
Dianne Hackborn | 8ae5a8e | 2010-07-01 18:44:46 -0700 | [diff] [blame] | 75 | } |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 76 | |
| 77 | int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer, |
| 78 | ARect* inOutDirtyBounds) { |
Mathias Agopian | 949be32 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 79 | return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds); |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | int32_t ANativeWindow_unlockAndPost(ANativeWindow* window) { |
Mathias Agopian | 949be32 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 83 | return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST); |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 84 | } |