1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/gonk/nativewindow/GonkNativeWindowJB.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,180 @@ 1.4 +/* 1.5 + * Copyright (C) 2012 The Android Open Source Project 1.6 + * Copyright (C) 2013 Mozilla Foundation 1.7 + * 1.8 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.9 + * you may not use this file except in compliance with the License. 1.10 + * You may obtain a copy of the License at 1.11 + * 1.12 + * http://www.apache.org/licenses/LICENSE-2.0 1.13 + * 1.14 + * Unless required by applicable law or agreed to in writing, software 1.15 + * distributed under the License is distributed on an "AS IS" BASIS, 1.16 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.17 + * See the License for the specific language governing permissions and 1.18 + * limitations under the License. 1.19 + */ 1.20 + 1.21 +//#define LOG_NDEBUG 0 1.22 +#define LOG_TAG "GonkNativeWindow" 1.23 +#define ATRACE_TAG ATRACE_TAG_GRAPHICS 1.24 +#include <utils/Log.h> 1.25 + 1.26 +#include "GonkNativeWindowJB.h" 1.27 +#include "GrallocImages.h" 1.28 +#include "mozilla/layers/ImageBridgeChild.h" 1.29 +#include "mozilla/RefPtr.h" 1.30 + 1.31 +#define BI_LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__) 1.32 +#define BI_LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) 1.33 +#define BI_LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) 1.34 +#define BI_LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__) 1.35 +#define BI_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) 1.36 + 1.37 +using namespace mozilla; 1.38 +using namespace mozilla::layers; 1.39 + 1.40 +namespace android { 1.41 + 1.42 +GonkNativeWindow::GonkNativeWindow() : 1.43 + GonkConsumerBase(new GonkBufferQueue(true) ) 1.44 +{ 1.45 + mBufferQueue->setMaxAcquiredBufferCount(GonkBufferQueue::MIN_UNDEQUEUED_BUFFERS); 1.46 +} 1.47 + 1.48 +GonkNativeWindow::~GonkNativeWindow() { 1.49 +} 1.50 + 1.51 +void GonkNativeWindow::setName(const String8& name) { 1.52 + Mutex::Autolock _l(mMutex); 1.53 + mName = name; 1.54 + mBufferQueue->setConsumerName(name); 1.55 +} 1.56 +#if ANDROID_VERSION >= 18 1.57 +status_t GonkNativeWindow::acquireBuffer(BufferItem *item, bool waitForFence) { 1.58 + status_t err; 1.59 + 1.60 + if (!item) return BAD_VALUE; 1.61 + 1.62 + Mutex::Autolock _l(mMutex); 1.63 + 1.64 + err = acquireBufferLocked(item); 1.65 + if (err != OK) { 1.66 + if (err != NO_BUFFER_AVAILABLE) { 1.67 + BI_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err); 1.68 + } 1.69 + return err; 1.70 + } 1.71 + 1.72 + if (waitForFence) { 1.73 + err = item->mFence->waitForever("GonkNativeWindow::acquireBuffer"); 1.74 + if (err != OK) { 1.75 + BI_LOGE("Failed to wait for fence of acquired buffer: %s (%d)", 1.76 + strerror(-err), err); 1.77 + return err; 1.78 + } 1.79 + } 1.80 + 1.81 + item->mGraphicBuffer = mSlots[item->mBuf].mGraphicBuffer; 1.82 + 1.83 + return OK; 1.84 +} 1.85 + 1.86 +status_t GonkNativeWindow::releaseBuffer(const BufferItem &item, 1.87 + const sp<Fence>& releaseFence) { 1.88 + status_t err; 1.89 + 1.90 + Mutex::Autolock _l(mMutex); 1.91 + 1.92 + err = addReleaseFenceLocked(item.mBuf, releaseFence); 1.93 + 1.94 + err = releaseBufferLocked(item.mBuf); 1.95 + if (err != OK) { 1.96 + BI_LOGE("Failed to release buffer: %s (%d)", 1.97 + strerror(-err), err); 1.98 + } 1.99 + return err; 1.100 +} 1.101 +#endif 1.102 + 1.103 +status_t GonkNativeWindow::setDefaultBufferSize(uint32_t w, uint32_t h) { 1.104 + Mutex::Autolock _l(mMutex); 1.105 + return mBufferQueue->setDefaultBufferSize(w, h); 1.106 +} 1.107 + 1.108 +status_t GonkNativeWindow::setDefaultBufferFormat(uint32_t defaultFormat) { 1.109 + Mutex::Autolock _l(mMutex); 1.110 + return mBufferQueue->setDefaultBufferFormat(defaultFormat); 1.111 +} 1.112 + 1.113 +TemporaryRef<TextureClient> 1.114 +GonkNativeWindow::getCurrentBuffer() { 1.115 + Mutex::Autolock _l(mMutex); 1.116 + GonkBufferQueue::BufferItem item; 1.117 + 1.118 + // In asynchronous mode the list is guaranteed to be one buffer 1.119 + // deep, while in synchronous mode we use the oldest buffer. 1.120 + status_t err = acquireBufferLocked(&item); 1.121 + if (err != NO_ERROR) { 1.122 + return NULL; 1.123 + } 1.124 + 1.125 + RefPtr<TextureClient> textureClient = 1.126 + mBufferQueue->getTextureClientFromBuffer(item.mGraphicBuffer.get()); 1.127 + if (!textureClient) { 1.128 + return NULL; 1.129 + } 1.130 + textureClient->SetRecycleCallback(GonkNativeWindow::RecycleCallback, this); 1.131 + return textureClient; 1.132 +} 1.133 + 1.134 +/* static */ void 1.135 +GonkNativeWindow::RecycleCallback(TextureClient* client, void* closure) { 1.136 + GonkNativeWindow* nativeWindow = 1.137 + static_cast<GonkNativeWindow*>(closure); 1.138 + 1.139 + client->ClearRecycleCallback(); 1.140 + nativeWindow->returnBuffer(client); 1.141 +} 1.142 + 1.143 +void GonkNativeWindow::returnBuffer(TextureClient* client) { 1.144 + BI_LOGD("GonkNativeWindow::returnBuffer"); 1.145 + Mutex::Autolock lock(mMutex); 1.146 + 1.147 + int index = mBufferQueue->getSlotFromTextureClientLocked(client); 1.148 + if (index < 0) { 1.149 + } 1.150 + 1.151 + sp<Fence> fence = client->GetReleaseFenceHandle().mFence; 1.152 + if (!fence.get()) { 1.153 + fence = Fence::NO_FENCE; 1.154 + } 1.155 + 1.156 + status_t err; 1.157 + err = addReleaseFenceLocked(index, fence); 1.158 + 1.159 + err = releaseBufferLocked(index); 1.160 +} 1.161 + 1.162 +TemporaryRef<TextureClient> 1.163 +GonkNativeWindow::getTextureClientFromBuffer(ANativeWindowBuffer* buffer) { 1.164 + Mutex::Autolock lock(mMutex); 1.165 + return mBufferQueue->getTextureClientFromBuffer(buffer); 1.166 +} 1.167 + 1.168 +void GonkNativeWindow::setNewFrameCallback( 1.169 + GonkNativeWindowNewFrameCallback* callback) { 1.170 + BI_LOGD("setNewFrameCallback"); 1.171 + Mutex::Autolock lock(mMutex); 1.172 + mNewFrameCallback = callback; 1.173 +} 1.174 + 1.175 +void GonkNativeWindow::onFrameAvailable() { 1.176 + GonkConsumerBase::onFrameAvailable(); 1.177 + 1.178 + if (mNewFrameCallback) { 1.179 + mNewFrameCallback->OnNewFrame(); 1.180 + } 1.181 +} 1.182 + 1.183 +} // namespace android