michael@0: /* michael@0: * Copyright (C) 2010 The Android Open Source Project michael@0: * Copyright (C) 2012 Mozilla Foundation michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #include "base/basictypes.h" michael@0: michael@0: #include "CameraCommon.h" michael@0: #include "GonkNativeWindow.h" michael@0: #include "GonkNativeWindowClient.h" michael@0: #include "nsDebug.h" michael@0: michael@0: /** michael@0: * DOM_CAMERA_LOGI() is enabled in debug builds, and turned on by setting michael@0: * NSPR_LOG_MODULES=Camera:N environment variable, where N >= 3. michael@0: * michael@0: * CNW_LOGE() is always enabled. michael@0: */ michael@0: #define CNW_LOGD(...) DOM_CAMERA_LOGI(__VA_ARGS__) michael@0: #define CNW_LOGE(...) {(void)printf_stderr(__VA_ARGS__);} michael@0: michael@0: using namespace android; michael@0: using namespace mozilla::layers; michael@0: michael@0: GonkNativeWindowClient::GonkNativeWindowClient(const sp& window) michael@0: : mNativeWindow(window) { michael@0: GonkNativeWindowClient::init(); michael@0: } michael@0: michael@0: GonkNativeWindowClient::~GonkNativeWindowClient() { michael@0: if (mConnectedToCpu) { michael@0: GonkNativeWindowClient::disconnect(NATIVE_WINDOW_API_CPU); michael@0: } michael@0: } michael@0: michael@0: void GonkNativeWindowClient::init() { michael@0: // Initialize the ANativeWindow function pointers. michael@0: ANativeWindow::setSwapInterval = hook_setSwapInterval; michael@0: ANativeWindow::dequeueBuffer = hook_dequeueBuffer; michael@0: ANativeWindow::cancelBuffer = hook_cancelBuffer; michael@0: ANativeWindow::lockBuffer = hook_lockBuffer; michael@0: ANativeWindow::queueBuffer = hook_queueBuffer; michael@0: ANativeWindow::query = hook_query; michael@0: ANativeWindow::perform = hook_perform; michael@0: michael@0: mReqWidth = 0; michael@0: mReqHeight = 0; michael@0: mReqFormat = 0; michael@0: mReqUsage = 0; michael@0: mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO; michael@0: mDefaultWidth = 0; michael@0: mDefaultHeight = 0; michael@0: mTransformHint = 0; michael@0: mConnectedToCpu = false; michael@0: } michael@0: michael@0: michael@0: int GonkNativeWindowClient::hook_setSwapInterval(ANativeWindow* window, int interval) { michael@0: GonkNativeWindowClient* c = getSelf(window); michael@0: return c->setSwapInterval(interval); michael@0: } michael@0: michael@0: int GonkNativeWindowClient::hook_dequeueBuffer(ANativeWindow* window, michael@0: ANativeWindowBuffer** buffer) { michael@0: GonkNativeWindowClient* c = getSelf(window); michael@0: return c->dequeueBuffer(buffer); michael@0: } michael@0: michael@0: int GonkNativeWindowClient::hook_cancelBuffer(ANativeWindow* window, michael@0: ANativeWindowBuffer* buffer) { michael@0: GonkNativeWindowClient* c = getSelf(window); michael@0: return c->cancelBuffer(buffer); michael@0: } michael@0: michael@0: int GonkNativeWindowClient::hook_lockBuffer(ANativeWindow* window, michael@0: ANativeWindowBuffer* buffer) { michael@0: GonkNativeWindowClient* c = getSelf(window); michael@0: return c->lockBuffer(buffer); michael@0: } michael@0: michael@0: int GonkNativeWindowClient::hook_queueBuffer(ANativeWindow* window, michael@0: ANativeWindowBuffer* buffer) { michael@0: GonkNativeWindowClient* c = getSelf(window); michael@0: return c->queueBuffer(buffer); michael@0: } michael@0: michael@0: int GonkNativeWindowClient::hook_query(const ANativeWindow* window, michael@0: int what, int* value) { michael@0: const GonkNativeWindowClient* c = getSelf(window); michael@0: return c->query(what, value); michael@0: } michael@0: michael@0: int GonkNativeWindowClient::hook_perform(ANativeWindow* window, int operation, ...) { michael@0: va_list args; michael@0: va_start(args, operation); michael@0: GonkNativeWindowClient* c = getSelf(window); michael@0: return c->perform(operation, args); michael@0: } michael@0: michael@0: int GonkNativeWindowClient::setSwapInterval(int interval) { michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: int GonkNativeWindowClient::dequeueBuffer(android_native_buffer_t** buffer) { michael@0: CNW_LOGD("GonkNativeWindowClient::dequeueBuffer"); michael@0: Mutex::Autolock lock(mMutex); michael@0: int buf = -1; michael@0: status_t result = mNativeWindow->dequeueBuffer(&buf, mReqWidth, mReqHeight, michael@0: mReqFormat, mReqUsage); michael@0: if (result < 0) { michael@0: CNW_LOGD("dequeueBuffer: ISurfaceTexture::dequeueBuffer(%d, %d, %d, %d)" michael@0: "failed: %d", mReqWidth, mReqHeight, mReqFormat, mReqUsage, michael@0: result); michael@0: return result; michael@0: } michael@0: sp& gbuf(mSlots[buf]); michael@0: if (result & ISurfaceTexture::RELEASE_ALL_BUFFERS) { michael@0: freeAllBuffers(); michael@0: } michael@0: michael@0: if ((result & ISurfaceTexture::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) { michael@0: result = mNativeWindow->requestBuffer(buf, &gbuf); michael@0: if (result != NO_ERROR) { michael@0: CNW_LOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed: %d", michael@0: result); michael@0: return result; michael@0: } michael@0: } michael@0: *buffer = gbuf.get(); michael@0: return OK; michael@0: } michael@0: michael@0: int GonkNativeWindowClient::cancelBuffer(ANativeWindowBuffer* buffer) { michael@0: CNW_LOGD("GonkNativeWindowClient::cancelBuffer"); michael@0: Mutex::Autolock lock(mMutex); michael@0: int i = getSlotFromBufferLocked(buffer); michael@0: if (i < 0) { michael@0: return i; michael@0: } michael@0: mNativeWindow->cancelBuffer(i); michael@0: return OK; michael@0: } michael@0: michael@0: int GonkNativeWindowClient::getSlotFromBufferLocked( michael@0: android_native_buffer_t* buffer) const { michael@0: bool dumpedState = false; michael@0: for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { michael@0: // XXX: Dump the slots whenever we hit a NULL entry while searching for michael@0: // a buffer. michael@0: if (mSlots[i] == NULL) { michael@0: if (!dumpedState) { michael@0: CNW_LOGD("getSlotFromBufferLocked: encountered NULL buffer in slot %d " michael@0: "looking for buffer %p", i, buffer->handle); michael@0: for (int j = 0; j < NUM_BUFFER_SLOTS; j++) { michael@0: if (mSlots[j] == NULL) { michael@0: CNW_LOGD("getSlotFromBufferLocked: %02d: NULL", j); michael@0: } else { michael@0: CNW_LOGD("getSlotFromBufferLocked: %02d: %p", j, mSlots[j]->handle); michael@0: } michael@0: } michael@0: dumpedState = true; michael@0: } michael@0: } michael@0: michael@0: if (mSlots[i] != NULL && mSlots[i]->handle == buffer->handle) { michael@0: return i; michael@0: } michael@0: } michael@0: CNW_LOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: michael@0: int GonkNativeWindowClient::lockBuffer(android_native_buffer_t* buffer) { michael@0: CNW_LOGD("GonkNativeWindowClient::lockBuffer"); michael@0: Mutex::Autolock lock(mMutex); michael@0: return OK; michael@0: } michael@0: michael@0: int GonkNativeWindowClient::queueBuffer(android_native_buffer_t* buffer) { michael@0: CNW_LOGD("GonkNativeWindowClient::queueBuffer"); michael@0: Mutex::Autolock lock(mMutex); michael@0: int64_t timestamp; michael@0: if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) { michael@0: timestamp = systemTime(SYSTEM_TIME_MONOTONIC); michael@0: CNW_LOGD("GonkNativeWindowClient::queueBuffer making up timestamp: %.2f ms", michael@0: timestamp / 1000000.f); michael@0: } else { michael@0: timestamp = mTimestamp; michael@0: } michael@0: int i = getSlotFromBufferLocked(buffer); michael@0: if (i < 0) { michael@0: return i; michael@0: } michael@0: status_t err = mNativeWindow->queueBuffer(i, timestamp, michael@0: &mDefaultWidth, &mDefaultHeight, &mTransformHint); michael@0: if (err != OK) { michael@0: CNW_LOGE("queueBuffer: error queuing buffer to GonkNativeWindow, %d", err); michael@0: } michael@0: return err; michael@0: } michael@0: michael@0: int GonkNativeWindowClient::query(int what, int* value) const { michael@0: CNW_LOGD("query"); michael@0: { // scope for the lock michael@0: Mutex::Autolock lock(mMutex); michael@0: switch (what) { michael@0: case NATIVE_WINDOW_FORMAT: michael@0: if (mReqFormat) { michael@0: *value = mReqFormat; michael@0: return NO_ERROR; michael@0: } michael@0: break; michael@0: case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: michael@0: *value = 0; michael@0: return NO_ERROR; michael@0: case NATIVE_WINDOW_CONCRETE_TYPE: michael@0: *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT; michael@0: return NO_ERROR; michael@0: case NATIVE_WINDOW_DEFAULT_WIDTH: michael@0: *value = mDefaultWidth; michael@0: return NO_ERROR; michael@0: case NATIVE_WINDOW_DEFAULT_HEIGHT: michael@0: *value = mDefaultHeight; michael@0: return NO_ERROR; michael@0: case NATIVE_WINDOW_TRANSFORM_HINT: michael@0: *value = mTransformHint; michael@0: return NO_ERROR; michael@0: } michael@0: } michael@0: return mNativeWindow->query(what, value); michael@0: } michael@0: michael@0: int GonkNativeWindowClient::perform(int operation, va_list args) michael@0: { michael@0: int res = NO_ERROR; michael@0: switch (operation) { michael@0: case NATIVE_WINDOW_CONNECT: michael@0: // deprecated. must return NO_ERROR. michael@0: break; michael@0: case NATIVE_WINDOW_DISCONNECT: michael@0: // deprecated. must return NO_ERROR. michael@0: break; michael@0: case NATIVE_WINDOW_SET_SCALING_MODE: michael@0: return NO_ERROR; michael@0: case NATIVE_WINDOW_SET_USAGE: michael@0: res = dispatchSetUsage(args); michael@0: break; michael@0: case NATIVE_WINDOW_SET_CROP: michael@0: //not implemented michael@0: break; michael@0: case NATIVE_WINDOW_SET_BUFFER_COUNT: michael@0: res = dispatchSetBufferCount(args); michael@0: break; michael@0: case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY: michael@0: res = dispatchSetBuffersGeometry(args); michael@0: break; michael@0: case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM: michael@0: //not implemented michael@0: break; michael@0: case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP: michael@0: res = dispatchSetBuffersTimestamp(args); michael@0: break; michael@0: case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS: michael@0: res = dispatchSetBuffersDimensions(args); michael@0: break; michael@0: case NATIVE_WINDOW_SET_BUFFERS_FORMAT: michael@0: res = dispatchSetBuffersFormat(args); michael@0: break; michael@0: case NATIVE_WINDOW_LOCK: michael@0: res = INVALID_OPERATION;// not supported michael@0: break; michael@0: case NATIVE_WINDOW_UNLOCK_AND_POST: michael@0: res = INVALID_OPERATION;// not supported michael@0: break; michael@0: case NATIVE_WINDOW_API_CONNECT: michael@0: res = dispatchConnect(args); michael@0: break; michael@0: case NATIVE_WINDOW_API_DISCONNECT: michael@0: res = dispatchDisconnect(args); michael@0: break; michael@0: case NATIVE_WINDOW_SET_BUFFERS_SIZE: michael@0: //not implemented michael@0: break; michael@0: default: michael@0: res = NAME_NOT_FOUND; michael@0: break; michael@0: } michael@0: return res; michael@0: } michael@0: michael@0: int GonkNativeWindowClient::dispatchConnect(va_list args) { michael@0: int api = va_arg(args, int); michael@0: return connect(api); michael@0: } michael@0: michael@0: int GonkNativeWindowClient::dispatchDisconnect(va_list args) { michael@0: int api = va_arg(args, int); michael@0: return disconnect(api); michael@0: } michael@0: michael@0: int GonkNativeWindowClient::dispatchSetUsage(va_list args) { michael@0: int usage = va_arg(args, int); michael@0: return setUsage(usage); michael@0: } michael@0: michael@0: int GonkNativeWindowClient::dispatchSetBufferCount(va_list args) { michael@0: size_t bufferCount = va_arg(args, size_t); michael@0: return setBufferCount(bufferCount); michael@0: } michael@0: michael@0: int GonkNativeWindowClient::dispatchSetBuffersGeometry(va_list args) { michael@0: int w = va_arg(args, int); michael@0: int h = va_arg(args, int); michael@0: int f = va_arg(args, int); michael@0: int err = setBuffersDimensions(w, h); michael@0: if (err != 0) { michael@0: return err; michael@0: } michael@0: return setBuffersFormat(f); michael@0: } michael@0: michael@0: int GonkNativeWindowClient::dispatchSetBuffersDimensions(va_list args) { michael@0: int w = va_arg(args, int); michael@0: int h = va_arg(args, int); michael@0: return setBuffersDimensions(w, h); michael@0: } michael@0: michael@0: int GonkNativeWindowClient::dispatchSetBuffersFormat(va_list args) { michael@0: int f = va_arg(args, int); michael@0: return setBuffersFormat(f); michael@0: } michael@0: michael@0: int GonkNativeWindowClient::dispatchSetBuffersTimestamp(va_list args) { michael@0: int64_t timestamp = va_arg(args, int64_t); michael@0: return setBuffersTimestamp(timestamp); michael@0: } michael@0: michael@0: int GonkNativeWindowClient::connect(int api) { michael@0: CNW_LOGD("GonkNativeWindowClient::connect"); michael@0: Mutex::Autolock lock(mMutex); michael@0: int err = mNativeWindow->connect(api, michael@0: &mDefaultWidth, &mDefaultHeight, &mTransformHint); michael@0: if (!err && api == NATIVE_WINDOW_API_CPU) { michael@0: mConnectedToCpu = true; michael@0: } michael@0: return err; michael@0: } michael@0: michael@0: int GonkNativeWindowClient::disconnect(int api) { michael@0: CNW_LOGD("GonkNativeWindowClient::disconnect"); michael@0: Mutex::Autolock lock(mMutex); michael@0: freeAllBuffers(); michael@0: int err = mNativeWindow->disconnect(api); michael@0: if (!err) { michael@0: mReqFormat = 0; michael@0: mReqWidth = 0; michael@0: mReqHeight = 0; michael@0: mReqUsage = 0; michael@0: if (api == NATIVE_WINDOW_API_CPU) { michael@0: mConnectedToCpu = false; michael@0: } michael@0: } michael@0: return err; michael@0: } michael@0: michael@0: int GonkNativeWindowClient::setUsage(uint32_t reqUsage) michael@0: { michael@0: CNW_LOGD("GonkNativeWindowClient::setUsage"); michael@0: Mutex::Autolock lock(mMutex); michael@0: mReqUsage = reqUsage; michael@0: return OK; michael@0: } michael@0: michael@0: int GonkNativeWindowClient::setBufferCount(int bufferCount) michael@0: { michael@0: CNW_LOGD("GonkNativeWindowClient::setBufferCount"); michael@0: Mutex::Autolock lock(mMutex); michael@0: michael@0: status_t err = mNativeWindow->setBufferCount(bufferCount); michael@0: if (err == NO_ERROR) { michael@0: freeAllBuffers(); michael@0: } michael@0: michael@0: return err; michael@0: } michael@0: michael@0: int GonkNativeWindowClient::setBuffersDimensions(int w, int h) michael@0: { michael@0: CNW_LOGD("GonkNativeWindowClient::setBuffersDimensions"); michael@0: Mutex::Autolock lock(mMutex); michael@0: michael@0: if (w<0 || h<0) michael@0: return BAD_VALUE; michael@0: michael@0: if ((w && !h) || (!w && h)) michael@0: return BAD_VALUE; michael@0: michael@0: mReqWidth = w; michael@0: mReqHeight = h; michael@0: michael@0: status_t err = OK; michael@0: michael@0: return err; michael@0: } michael@0: michael@0: int GonkNativeWindowClient::setBuffersFormat(int format) michael@0: { michael@0: CNW_LOGD("GonkNativeWindowClient::setBuffersFormat"); michael@0: Mutex::Autolock lock(mMutex); michael@0: michael@0: if (format<0) michael@0: return BAD_VALUE; michael@0: michael@0: mReqFormat = format; michael@0: michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: michael@0: int GonkNativeWindowClient::setBuffersTimestamp(int64_t timestamp) michael@0: { michael@0: CNW_LOGD("GonkNativeWindowClient::setBuffersTimestamp"); michael@0: Mutex::Autolock lock(mMutex); michael@0: mTimestamp = timestamp; michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: void GonkNativeWindowClient::freeAllBuffers() { michael@0: for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { michael@0: mSlots[i] = 0; michael@0: } michael@0: } michael@0: