ANativeWindow.cpp revision 09932eceb2fcec029edc6aaa0e2bca0591613281
189ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian/* 289ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian * Copyright (C) 2010 The Android Open Source Project 389ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian * 489ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 589ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian * you may not use this file except in compliance with the License. 689ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian * You may obtain a copy of the License at 789ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian * 889ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 989ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian * 1089ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian * Unless required by applicable law or agreed to in writing, software 1189ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 1289ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1389ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian * See the License for the specific language governing permissions and 1489ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian * limitations under the License. 1589ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian */ 1689ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian 1789ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian#define LOG_TAG "ANativeWindow" 1889ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian 1989ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian#include <android/native_window.h> 2089ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian#include <system/window.h> 2189ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian 2289ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopianvoid ANativeWindow_acquire(ANativeWindow* window) { 2389ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian window->incStrong((void*)ANativeWindow_acquire); 2489ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian} 2589ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian 2689ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopianvoid ANativeWindow_release(ANativeWindow* window) { 2789ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian window->decStrong((void*)ANativeWindow_release); 2889ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian} 2989ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian 3089ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopianstatic int32_t getWindowProp(ANativeWindow* window, int what) { 3189ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian int value; 3289ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian int res = window->query(window, what, &value); 3389ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian return res < 0 ? res : value; 3489ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian} 3589ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian 3689ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopianint32_t ANativeWindow_getWidth(ANativeWindow* window) { 3789ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian return getWindowProp(window, NATIVE_WINDOW_WIDTH); 3889ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian} 3989ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian 4089ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopianint32_t ANativeWindow_getHeight(ANativeWindow* window) { 4189ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian return getWindowProp(window, NATIVE_WINDOW_HEIGHT); 4289ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian} 4389ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian 4489ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopianint32_t ANativeWindow_getFormat(ANativeWindow* window) { 4589ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian return getWindowProp(window, NATIVE_WINDOW_FORMAT); 4689ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian} 4789ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian 4889ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopianint32_t ANativeWindow_setBuffersGeometry(ANativeWindow* window, int32_t width, 4989ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian int32_t height, int32_t format) { 5089ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian int32_t err = native_window_set_buffers_format(window, format); 5189ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian if (!err) { 5289ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian err = native_window_set_buffers_user_dimensions(window, width, height); 5389ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian if (!err) { 5489ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian int mode = NATIVE_WINDOW_SCALING_MODE_FREEZE; 5589ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian if (width && height) { 5689ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian mode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW; 5789ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian } 5889ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian err = native_window_set_scaling_mode(window, mode); 5989ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian } 6089ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian } 6189ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian return err; 6289ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian} 6389ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian 6489ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopianint32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer, 6589ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian ARect* inOutDirtyBounds) { 6689ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds); 6789ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian} 6889ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian 6989ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopianint32_t ANativeWindow_unlockAndPost(ANativeWindow* window) { 7089ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST); 7189ed4c8cfd8ad64269dfcff9742e16bdd705b926Mathias Agopian} 7209932eceb2fcec029edc6aaa0e2bca0591613281Jesse Hall 7309932eceb2fcec029edc6aaa0e2bca0591613281Jesse Hallint32_t ANativeWindow_setBuffersTransform(ANativeWindow* window, int32_t transform) { 7409932eceb2fcec029edc6aaa0e2bca0591613281Jesse Hall static_assert(ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL == NATIVE_WINDOW_TRANSFORM_FLIP_H); 7509932eceb2fcec029edc6aaa0e2bca0591613281Jesse Hall static_assert(ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL == NATIVE_WINDOW_TRANSFORM_FLIP_V); 7609932eceb2fcec029edc6aaa0e2bca0591613281Jesse Hall static_assert(ANATIVEWINDOW_TRANSFORM_ROTATE_90 == NATIVE_WINDOW_TRANSFORM_ROT_90); 7709932eceb2fcec029edc6aaa0e2bca0591613281Jesse Hall 7809932eceb2fcec029edc6aaa0e2bca0591613281Jesse Hall constexpr int32_t kAllTransformBits = 7909932eceb2fcec029edc6aaa0e2bca0591613281Jesse Hall ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL | 8009932eceb2fcec029edc6aaa0e2bca0591613281Jesse Hall ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL | 8109932eceb2fcec029edc6aaa0e2bca0591613281Jesse Hall ANATIVEWINDOW_TRANSFORM_ROTATE_90; 8209932eceb2fcec029edc6aaa0e2bca0591613281Jesse Hall if (!window || !getWindowProp(window, NATIVE_WINDOW_IS_VALID)) 8309932eceb2fcec029edc6aaa0e2bca0591613281Jesse Hall return -EINVAL; 8409932eceb2fcec029edc6aaa0e2bca0591613281Jesse Hall if ((transform & ~kAllTransformBits) != 0) 8509932eceb2fcec029edc6aaa0e2bca0591613281Jesse Hall return -EINVAL; 8609932eceb2fcec029edc6aaa0e2bca0591613281Jesse Hall 8709932eceb2fcec029edc6aaa0e2bca0591613281Jesse Hall return native_window_set_buffers_transform(window, transform); 8809932eceb2fcec029edc6aaa0e2bca0591613281Jesse Hall} 89