native_window.cpp revision da5a4440f019074e7aa61c5531c5c61755adc44d
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
20#include <android/native_window_jni.h>
21#include <surfaceflinger/Surface.h>
22#include <android_runtime/android_view_Surface.h>
23#include <android_runtime/android_graphics_SurfaceTexture.h>
24
25using namespace android;
26
27ANativeWindow* ANativeWindow_fromSurface(JNIEnv* env, jobject surface) {
28    sp<ANativeWindow> win = android_Surface_getNativeWindow(env, surface);
29    if (win != NULL) {
30        win->incStrong((void*)ANativeWindow_acquire);
31    }
32    return win.get();
33}
34
35ANativeWindow* 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
43void ANativeWindow_acquire(ANativeWindow* window) {
44    window->incStrong((void*)ANativeWindow_acquire);
45}
46
47void ANativeWindow_release(ANativeWindow* window) {
48    window->decStrong((void*)ANativeWindow_acquire);
49}
50
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}
68
69int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window, int32_t width,
70        int32_t height, int32_t format) {
71    return native_window_set_buffers_geometry(window, width, height, format);
72}
73
74int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
75        ARect* inOutDirtyBounds) {
76    int type = -1;
77    if (window->query(window, NATIVE_WINDOW_CONCRETE_TYPE, &type) != 0 ||
78            type != NATIVE_WINDOW_SURFACE) {
79        return BAD_VALUE;
80    }
81
82    Region dirtyRegion;
83    Region* dirtyParam = NULL;
84    if (inOutDirtyBounds != NULL) {
85        dirtyRegion.set(*(Rect*)inOutDirtyBounds);
86        dirtyParam = &dirtyRegion;
87    }
88
89    Surface::SurfaceInfo info;
90    status_t res = static_cast<Surface*>(window)->lock(&info, dirtyParam);
91    if (res != OK) {
92        return -1;
93    }
94
95    outBuffer->width = (int32_t)info.w;
96    outBuffer->height = (int32_t)info.h;
97    outBuffer->stride = (int32_t)info.s;
98    outBuffer->format = (int32_t)info.format;
99    outBuffer->bits = info.bits;
100
101    if (inOutDirtyBounds != NULL) {
102        *inOutDirtyBounds = dirtyRegion.getBounds();
103    }
104
105    return 0;
106}
107
108int32_t ANativeWindow_unlockAndPost(ANativeWindow* window) {
109    status_t res = static_cast<Surface*>(window)->unlockAndPost();
110    return res == android::OK ? 0 : -1;
111}
112