widget/gonk/nativewindow/GonkNativeWindowJB.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rwxr-xr-x

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * Copyright (C) 2012 The Android Open Source Project
michael@0 3 * Copyright (C) 2013 Mozilla Foundation
michael@0 4 *
michael@0 5 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 6 * you may not use this file except in compliance with the License.
michael@0 7 * You may obtain a copy of the License at
michael@0 8 *
michael@0 9 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 10 *
michael@0 11 * Unless required by applicable law or agreed to in writing, software
michael@0 12 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 14 * See the License for the specific language governing permissions and
michael@0 15 * limitations under the License.
michael@0 16 */
michael@0 17
michael@0 18 //#define LOG_NDEBUG 0
michael@0 19 #define LOG_TAG "GonkNativeWindow"
michael@0 20 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
michael@0 21 #include <utils/Log.h>
michael@0 22
michael@0 23 #include "GonkNativeWindowJB.h"
michael@0 24 #include "GrallocImages.h"
michael@0 25 #include "mozilla/layers/ImageBridgeChild.h"
michael@0 26 #include "mozilla/RefPtr.h"
michael@0 27
michael@0 28 #define BI_LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
michael@0 29 #define BI_LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
michael@0 30 #define BI_LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
michael@0 31 #define BI_LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
michael@0 32 #define BI_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
michael@0 33
michael@0 34 using namespace mozilla;
michael@0 35 using namespace mozilla::layers;
michael@0 36
michael@0 37 namespace android {
michael@0 38
michael@0 39 GonkNativeWindow::GonkNativeWindow() :
michael@0 40 GonkConsumerBase(new GonkBufferQueue(true) )
michael@0 41 {
michael@0 42 mBufferQueue->setMaxAcquiredBufferCount(GonkBufferQueue::MIN_UNDEQUEUED_BUFFERS);
michael@0 43 }
michael@0 44
michael@0 45 GonkNativeWindow::~GonkNativeWindow() {
michael@0 46 }
michael@0 47
michael@0 48 void GonkNativeWindow::setName(const String8& name) {
michael@0 49 Mutex::Autolock _l(mMutex);
michael@0 50 mName = name;
michael@0 51 mBufferQueue->setConsumerName(name);
michael@0 52 }
michael@0 53 #if ANDROID_VERSION >= 18
michael@0 54 status_t GonkNativeWindow::acquireBuffer(BufferItem *item, bool waitForFence) {
michael@0 55 status_t err;
michael@0 56
michael@0 57 if (!item) return BAD_VALUE;
michael@0 58
michael@0 59 Mutex::Autolock _l(mMutex);
michael@0 60
michael@0 61 err = acquireBufferLocked(item);
michael@0 62 if (err != OK) {
michael@0 63 if (err != NO_BUFFER_AVAILABLE) {
michael@0 64 BI_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
michael@0 65 }
michael@0 66 return err;
michael@0 67 }
michael@0 68
michael@0 69 if (waitForFence) {
michael@0 70 err = item->mFence->waitForever("GonkNativeWindow::acquireBuffer");
michael@0 71 if (err != OK) {
michael@0 72 BI_LOGE("Failed to wait for fence of acquired buffer: %s (%d)",
michael@0 73 strerror(-err), err);
michael@0 74 return err;
michael@0 75 }
michael@0 76 }
michael@0 77
michael@0 78 item->mGraphicBuffer = mSlots[item->mBuf].mGraphicBuffer;
michael@0 79
michael@0 80 return OK;
michael@0 81 }
michael@0 82
michael@0 83 status_t GonkNativeWindow::releaseBuffer(const BufferItem &item,
michael@0 84 const sp<Fence>& releaseFence) {
michael@0 85 status_t err;
michael@0 86
michael@0 87 Mutex::Autolock _l(mMutex);
michael@0 88
michael@0 89 err = addReleaseFenceLocked(item.mBuf, releaseFence);
michael@0 90
michael@0 91 err = releaseBufferLocked(item.mBuf);
michael@0 92 if (err != OK) {
michael@0 93 BI_LOGE("Failed to release buffer: %s (%d)",
michael@0 94 strerror(-err), err);
michael@0 95 }
michael@0 96 return err;
michael@0 97 }
michael@0 98 #endif
michael@0 99
michael@0 100 status_t GonkNativeWindow::setDefaultBufferSize(uint32_t w, uint32_t h) {
michael@0 101 Mutex::Autolock _l(mMutex);
michael@0 102 return mBufferQueue->setDefaultBufferSize(w, h);
michael@0 103 }
michael@0 104
michael@0 105 status_t GonkNativeWindow::setDefaultBufferFormat(uint32_t defaultFormat) {
michael@0 106 Mutex::Autolock _l(mMutex);
michael@0 107 return mBufferQueue->setDefaultBufferFormat(defaultFormat);
michael@0 108 }
michael@0 109
michael@0 110 TemporaryRef<TextureClient>
michael@0 111 GonkNativeWindow::getCurrentBuffer() {
michael@0 112 Mutex::Autolock _l(mMutex);
michael@0 113 GonkBufferQueue::BufferItem item;
michael@0 114
michael@0 115 // In asynchronous mode the list is guaranteed to be one buffer
michael@0 116 // deep, while in synchronous mode we use the oldest buffer.
michael@0 117 status_t err = acquireBufferLocked(&item);
michael@0 118 if (err != NO_ERROR) {
michael@0 119 return NULL;
michael@0 120 }
michael@0 121
michael@0 122 RefPtr<TextureClient> textureClient =
michael@0 123 mBufferQueue->getTextureClientFromBuffer(item.mGraphicBuffer.get());
michael@0 124 if (!textureClient) {
michael@0 125 return NULL;
michael@0 126 }
michael@0 127 textureClient->SetRecycleCallback(GonkNativeWindow::RecycleCallback, this);
michael@0 128 return textureClient;
michael@0 129 }
michael@0 130
michael@0 131 /* static */ void
michael@0 132 GonkNativeWindow::RecycleCallback(TextureClient* client, void* closure) {
michael@0 133 GonkNativeWindow* nativeWindow =
michael@0 134 static_cast<GonkNativeWindow*>(closure);
michael@0 135
michael@0 136 client->ClearRecycleCallback();
michael@0 137 nativeWindow->returnBuffer(client);
michael@0 138 }
michael@0 139
michael@0 140 void GonkNativeWindow::returnBuffer(TextureClient* client) {
michael@0 141 BI_LOGD("GonkNativeWindow::returnBuffer");
michael@0 142 Mutex::Autolock lock(mMutex);
michael@0 143
michael@0 144 int index = mBufferQueue->getSlotFromTextureClientLocked(client);
michael@0 145 if (index < 0) {
michael@0 146 }
michael@0 147
michael@0 148 sp<Fence> fence = client->GetReleaseFenceHandle().mFence;
michael@0 149 if (!fence.get()) {
michael@0 150 fence = Fence::NO_FENCE;
michael@0 151 }
michael@0 152
michael@0 153 status_t err;
michael@0 154 err = addReleaseFenceLocked(index, fence);
michael@0 155
michael@0 156 err = releaseBufferLocked(index);
michael@0 157 }
michael@0 158
michael@0 159 TemporaryRef<TextureClient>
michael@0 160 GonkNativeWindow::getTextureClientFromBuffer(ANativeWindowBuffer* buffer) {
michael@0 161 Mutex::Autolock lock(mMutex);
michael@0 162 return mBufferQueue->getTextureClientFromBuffer(buffer);
michael@0 163 }
michael@0 164
michael@0 165 void GonkNativeWindow::setNewFrameCallback(
michael@0 166 GonkNativeWindowNewFrameCallback* callback) {
michael@0 167 BI_LOGD("setNewFrameCallback");
michael@0 168 Mutex::Autolock lock(mMutex);
michael@0 169 mNewFrameCallback = callback;
michael@0 170 }
michael@0 171
michael@0 172 void GonkNativeWindow::onFrameAvailable() {
michael@0 173 GonkConsumerBase::onFrameAvailable();
michael@0 174
michael@0 175 if (mNewFrameCallback) {
michael@0 176 mNewFrameCallback->OnNewFrame();
michael@0 177 }
michael@0 178 }
michael@0 179
michael@0 180 } // namespace android

mercurial