native_window.cpp revision 289b9b62372ef52a06113b83dfb870e2c2fb325a
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
24using namespace android;
25
26ANativeWindow* ANativeWindow_fromSurface(JNIEnv* env, jobject surface) {
27    sp<ANativeWindow> win = android_Surface_getNativeWindow(env, surface);
28    if (win != NULL) {
29        win->incStrong((void*)ANativeWindow_acquire);
30    }
31    return win.get();
32}
33
34void ANativeWindow_acquire(ANativeWindow* window) {
35    window->incStrong((void*)ANativeWindow_acquire);
36}
37
38void ANativeWindow_release(ANativeWindow* window) {
39    window->decStrong((void*)ANativeWindow_acquire);
40}
41
42static int32_t getWindowProp(ANativeWindow* window, int what) {
43    int value;
44    int res = window->query(window, what, &value);
45    return res < 0 ? res : value;
46}
47
48int32_t ANativeWindow_getWidth(ANativeWindow* window) {
49    return getWindowProp(window, NATIVE_WINDOW_WIDTH);
50}
51
52int32_t ANativeWindow_getHeight(ANativeWindow* window) {
53    return getWindowProp(window, NATIVE_WINDOW_HEIGHT);
54}
55
56int32_t ANativeWindow_getFormat(ANativeWindow* window) {
57    return getWindowProp(window, NATIVE_WINDOW_FORMAT);
58}
59
60int32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window, int32_t width,
61        int32_t height) {
62    native_window_set_buffers_geometry(window, width, height, 0);
63    return 0;
64}
65
66int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
67        ARect* inOutDirtyBounds) {
68    Region dirtyRegion;
69    Region* dirtyParam = NULL;
70    if (inOutDirtyBounds != NULL) {
71        dirtyRegion.set(*(Rect*)inOutDirtyBounds);
72        dirtyParam = &dirtyRegion;
73    }
74
75    Surface::SurfaceInfo info;
76    status_t res = static_cast<Surface*>(window)->lock(&info, dirtyParam);
77    if (res != OK) {
78        return -1;
79    }
80
81    outBuffer->width = (int32_t)info.w;
82    outBuffer->height = (int32_t)info.h;
83    outBuffer->stride = (int32_t)info.s;
84    outBuffer->format = (int32_t)info.format;
85    outBuffer->bits = info.bits;
86
87    if (inOutDirtyBounds != NULL) {
88        *inOutDirtyBounds = dirtyRegion.getBounds();
89    }
90
91    return 0;
92}
93
94int32_t ANativeWindow_unlockAndPost(ANativeWindow* window) {
95    status_t res = static_cast<Surface*>(window)->unlockAndPost();
96    return res == android::OK ? 0 : -1;
97}
98