blob: d266fc681e3db92cc77df6842337968ac31cdb86 [file] [log] [blame]
Dianne Hackborn54a181b2010-06-30 18:35:14 -07001/*
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 Hackborn289b9b62010-07-09 11:44:11 -070020#include <android/native_window_jni.h>
Dianne Hackborn54a181b2010-06-30 18:35:14 -070021#include <surfaceflinger/Surface.h>
Dianne Hackborn289b9b62010-07-09 11:44:11 -070022#include <android_runtime/android_view_Surface.h>
Glenn Kasten846db332011-03-04 11:44:32 -080023#include <android_runtime/android_graphics_SurfaceTexture.h>
Dianne Hackborn54a181b2010-06-30 18:35:14 -070024
Dianne Hackborn289b9b62010-07-09 11:44:11 -070025using namespace android;
26
27ANativeWindow* ANativeWindow_fromSurface(JNIEnv* env, jobject surface) {
Ted Bonkenburg1ee60112011-07-26 09:51:18 -070028 sp<ANativeWindow> win = android_Surface_getNativeWindow(env, surface);
Dianne Hackborn289b9b62010-07-09 11:44:11 -070029 if (win != NULL) {
30 win->incStrong((void*)ANativeWindow_acquire);
31 }
32 return win.get();
33}
34
Glenn Kasten846db332011-03-04 11:44:32 -080035ANativeWindow* ANativeWindow_fromSurfaceTexture(JNIEnv* env, jobject surfaceTexture) {
36 sp<ANativeWindow> win = android_SurfaceTexture_getNativeWindow(env, surfaceTexture);
37 if (win != NULL) {
38 win->incStrong((void*)ANativeWindow_acquire);
39 }
40 return win.get();
41}
42
Dianne Hackborn289b9b62010-07-09 11:44:11 -070043void ANativeWindow_acquire(ANativeWindow* window) {
44 window->incStrong((void*)ANativeWindow_acquire);
45}
46
47void ANativeWindow_release(ANativeWindow* window) {
48 window->decStrong((void*)ANativeWindow_acquire);
49}
Dianne Hackborn54a181b2010-06-30 18:35:14 -070050
51static int32_t getWindowProp(ANativeWindow* window, int what) {
52 int value;
53 int res = window->query(window, what, &value);
54 return res < 0 ? res : value;
55}
56
57int32_t ANativeWindow_getWidth(ANativeWindow* window) {
58 return getWindowProp(window, NATIVE_WINDOW_WIDTH);
59}
60
61int32_t ANativeWindow_getHeight(ANativeWindow* window) {
62 return getWindowProp(window, NATIVE_WINDOW_HEIGHT);
63}
64
65int32_t ANativeWindow_getFormat(ANativeWindow* window) {
66 return getWindowProp(window, NATIVE_WINDOW_FORMAT);
67}
Dianne Hackborn8ae5a8e2010-07-01 18:44:46 -070068
69int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window, int32_t width,
Mathias Agopian3026a1c2010-10-24 18:35:26 -070070 int32_t height, int32_t format) {
Mathias Agopian09d7ed72011-07-13 15:24:42 -070071 int32_t err = native_window_set_buffers_geometry(window, width, height, format);
72 if (!err) {
73 int mode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
74 if (width && height) {
75 mode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
76 }
77 err = native_window_set_scaling_mode(window, mode);
78 }
79 return err;
Dianne Hackborn8ae5a8e2010-07-01 18:44:46 -070080}
Dianne Hackborn289b9b62010-07-09 11:44:11 -070081
82int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
83 ARect* inOutDirtyBounds) {
Mathias Agopian949be322011-07-13 17:39:11 -070084 return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds);
Dianne Hackborn289b9b62010-07-09 11:44:11 -070085}
86
87int32_t ANativeWindow_unlockAndPost(ANativeWindow* window) {
Mathias Agopian949be322011-07-13 17:39:11 -070088 return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
Dianne Hackborn289b9b62010-07-09 11:44:11 -070089}