Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 "GonkNativeWindowKK.h" |
michael@0 | 24 | #include "GrallocImages.h" |
michael@0 | 25 | |
michael@0 | 26 | #define BI_LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__) |
michael@0 | 27 | #define BI_LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) |
michael@0 | 28 | #define BI_LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) |
michael@0 | 29 | #define BI_LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__) |
michael@0 | 30 | #define BI_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) |
michael@0 | 31 | |
michael@0 | 32 | using namespace mozilla; |
michael@0 | 33 | using namespace mozilla::layers; |
michael@0 | 34 | |
michael@0 | 35 | namespace android { |
michael@0 | 36 | |
michael@0 | 37 | GonkNativeWindow::GonkNativeWindow() : |
michael@0 | 38 | GonkConsumerBase(new GonkBufferQueue(true), false) |
michael@0 | 39 | { |
michael@0 | 40 | mConsumer->setMaxAcquiredBufferCount(GonkBufferQueue::MIN_UNDEQUEUED_BUFFERS); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | GonkNativeWindow::GonkNativeWindow(const sp<GonkBufferQueue>& bq, |
michael@0 | 44 | uint32_t consumerUsage, int bufferCount, bool controlledByApp) : |
michael@0 | 45 | GonkConsumerBase(bq, controlledByApp) |
michael@0 | 46 | { |
michael@0 | 47 | mConsumer->setConsumerUsageBits(consumerUsage); |
michael@0 | 48 | mConsumer->setMaxAcquiredBufferCount(bufferCount); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | GonkNativeWindow::~GonkNativeWindow() { |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | void GonkNativeWindow::setName(const String8& name) { |
michael@0 | 55 | Mutex::Autolock _l(mMutex); |
michael@0 | 56 | mName = name; |
michael@0 | 57 | mConsumer->setConsumerName(name); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | status_t GonkNativeWindow::acquireBuffer(BufferItem *item, |
michael@0 | 61 | nsecs_t presentWhen, bool waitForFence) { |
michael@0 | 62 | status_t err; |
michael@0 | 63 | |
michael@0 | 64 | if (!item) return BAD_VALUE; |
michael@0 | 65 | |
michael@0 | 66 | Mutex::Autolock _l(mMutex); |
michael@0 | 67 | |
michael@0 | 68 | err = acquireBufferLocked(item, presentWhen); |
michael@0 | 69 | if (err != OK) { |
michael@0 | 70 | if (err != NO_BUFFER_AVAILABLE) { |
michael@0 | 71 | BI_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err); |
michael@0 | 72 | } |
michael@0 | 73 | return err; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | if (waitForFence) { |
michael@0 | 77 | err = item->mFence->waitForever("GonkNativeWindow::acquireBuffer"); |
michael@0 | 78 | if (err != OK) { |
michael@0 | 79 | BI_LOGE("Failed to wait for fence of acquired buffer: %s (%d)", |
michael@0 | 80 | strerror(-err), err); |
michael@0 | 81 | return err; |
michael@0 | 82 | } |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | item->mGraphicBuffer = mSlots[item->mBuf].mGraphicBuffer; |
michael@0 | 86 | |
michael@0 | 87 | return OK; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | status_t GonkNativeWindow::releaseBuffer(const BufferItem &item, |
michael@0 | 91 | const sp<Fence>& releaseFence) { |
michael@0 | 92 | status_t err; |
michael@0 | 93 | |
michael@0 | 94 | Mutex::Autolock _l(mMutex); |
michael@0 | 95 | |
michael@0 | 96 | err = addReleaseFenceLocked(item.mBuf, item.mGraphicBuffer, releaseFence); |
michael@0 | 97 | |
michael@0 | 98 | err = releaseBufferLocked(item.mBuf, item.mGraphicBuffer); |
michael@0 | 99 | if (err != OK) { |
michael@0 | 100 | BI_LOGE("Failed to release buffer: %s (%d)", |
michael@0 | 101 | strerror(-err), err); |
michael@0 | 102 | } |
michael@0 | 103 | return err; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | status_t GonkNativeWindow::setDefaultBufferSize(uint32_t w, uint32_t h) { |
michael@0 | 107 | Mutex::Autolock _l(mMutex); |
michael@0 | 108 | return mConsumer->setDefaultBufferSize(w, h); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | status_t GonkNativeWindow::setDefaultBufferFormat(uint32_t defaultFormat) { |
michael@0 | 112 | Mutex::Autolock _l(mMutex); |
michael@0 | 113 | return mConsumer->setDefaultBufferFormat(defaultFormat); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | TemporaryRef<TextureClient> |
michael@0 | 117 | GonkNativeWindow::getCurrentBuffer() { |
michael@0 | 118 | Mutex::Autolock _l(mMutex); |
michael@0 | 119 | BufferItem item; |
michael@0 | 120 | |
michael@0 | 121 | // In asynchronous mode the list is guaranteed to be one buffer |
michael@0 | 122 | // deep, while in synchronous mode we use the oldest buffer. |
michael@0 | 123 | status_t err = acquireBufferLocked(&item, 0); //??? |
michael@0 | 124 | if (err != NO_ERROR) { |
michael@0 | 125 | return NULL; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | RefPtr<TextureClient> textureClient = |
michael@0 | 129 | mConsumer->getTextureClientFromBuffer(item.mGraphicBuffer.get()); |
michael@0 | 130 | if (!textureClient) { |
michael@0 | 131 | return NULL; |
michael@0 | 132 | } |
michael@0 | 133 | textureClient->SetRecycleCallback(GonkNativeWindow::RecycleCallback, this); |
michael@0 | 134 | return textureClient; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | /* static */ void |
michael@0 | 138 | GonkNativeWindow::RecycleCallback(TextureClient* client, void* closure) { |
michael@0 | 139 | GonkNativeWindow* nativeWindow = |
michael@0 | 140 | static_cast<GonkNativeWindow*>(closure); |
michael@0 | 141 | |
michael@0 | 142 | client->ClearRecycleCallback(); |
michael@0 | 143 | nativeWindow->returnBuffer(client); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | void GonkNativeWindow::returnBuffer(TextureClient* client) { |
michael@0 | 147 | BI_LOGD("GonkNativeWindow::returnBuffer"); |
michael@0 | 148 | Mutex::Autolock lock(mMutex); |
michael@0 | 149 | |
michael@0 | 150 | int index = mConsumer->getSlotFromTextureClientLocked(client); |
michael@0 | 151 | if (index < 0) { |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | sp<Fence> fence = client->GetReleaseFenceHandle().mFence; |
michael@0 | 155 | if (!fence.get()) { |
michael@0 | 156 | fence = Fence::NO_FENCE; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | status_t err; |
michael@0 | 160 | err = addReleaseFenceLocked(index, |
michael@0 | 161 | mSlots[index].mGraphicBuffer, |
michael@0 | 162 | fence); |
michael@0 | 163 | |
michael@0 | 164 | err = releaseBufferLocked(index, mSlots[index].mGraphicBuffer); |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | TemporaryRef<TextureClient> |
michael@0 | 168 | GonkNativeWindow::getTextureClientFromBuffer(ANativeWindowBuffer* buffer) { |
michael@0 | 169 | Mutex::Autolock lock(mMutex); |
michael@0 | 170 | return mConsumer->getTextureClientFromBuffer(buffer); |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | void GonkNativeWindow::setNewFrameCallback( |
michael@0 | 174 | GonkNativeWindowNewFrameCallback* callback) { |
michael@0 | 175 | BI_LOGD("setNewFrameCallback"); |
michael@0 | 176 | Mutex::Autolock lock(mMutex); |
michael@0 | 177 | mNewFrameCallback = callback; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | void GonkNativeWindow::onFrameAvailable() { |
michael@0 | 181 | GonkConsumerBase::onFrameAvailable(); |
michael@0 | 182 | |
michael@0 | 183 | if (mNewFrameCallback) { |
michael@0 | 184 | mNewFrameCallback->OnNewFrame(); |
michael@0 | 185 | } |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | } // namespace android |