Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include <unistd.h> |
michael@0 | 7 | #include <errno.h> |
michael@0 | 8 | #include "AndroidDirectTexture.h" |
michael@0 | 9 | #include "nsRect.h" |
michael@0 | 10 | |
michael@0 | 11 | namespace mozilla { |
michael@0 | 12 | |
michael@0 | 13 | AndroidDirectTexture::AndroidDirectTexture(uint32_t width, uint32_t height, uint32_t usage, |
michael@0 | 14 | gfxImageFormat format) : |
michael@0 | 15 | mLock("AndroidDirectTexture.mLock") |
michael@0 | 16 | , mNeedFlip(false) |
michael@0 | 17 | , mWidth(width) |
michael@0 | 18 | , mHeight(height) |
michael@0 | 19 | , mFormat(format) |
michael@0 | 20 | , mPendingReallocBuffer(nullptr) |
michael@0 | 21 | { |
michael@0 | 22 | mFrontBuffer = new AndroidGraphicBuffer(width, height, usage, format); |
michael@0 | 23 | mBackBuffer = new AndroidGraphicBuffer(width, height, usage, format); |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | AndroidDirectTexture::~AndroidDirectTexture() |
michael@0 | 27 | { |
michael@0 | 28 | if (mFrontBuffer) { |
michael@0 | 29 | delete mFrontBuffer; |
michael@0 | 30 | mFrontBuffer = nullptr; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | if (mBackBuffer) { |
michael@0 | 34 | delete mBackBuffer; |
michael@0 | 35 | mBackBuffer = nullptr; |
michael@0 | 36 | } |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | void |
michael@0 | 40 | AndroidDirectTexture::ReallocPendingBuffer() |
michael@0 | 41 | { |
michael@0 | 42 | // We may have reallocated while the current back buffer was being |
michael@0 | 43 | // used as the front buffer. If we have such a reallocation pending |
michael@0 | 44 | // and the current back buffer is the target buffer, do it now. |
michael@0 | 45 | // |
michael@0 | 46 | // It is assumed that mLock is already acquired |
michael@0 | 47 | if (mPendingReallocBuffer == mBackBuffer) { |
michael@0 | 48 | mBackBuffer->Reallocate(mWidth, mHeight, mFormat); |
michael@0 | 49 | mPendingReallocBuffer = nullptr; |
michael@0 | 50 | } |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | bool |
michael@0 | 54 | AndroidDirectTexture::Lock(uint32_t aUsage, unsigned char **bits) |
michael@0 | 55 | { |
michael@0 | 56 | nsIntRect rect(0, 0, mWidth, mHeight); |
michael@0 | 57 | return Lock(aUsage, rect, bits); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | bool |
michael@0 | 61 | AndroidDirectTexture::Lock(uint32_t aUsage, const nsIntRect& aRect, unsigned char **bits) |
michael@0 | 62 | { |
michael@0 | 63 | mLock.Lock(); |
michael@0 | 64 | ReallocPendingBuffer(); |
michael@0 | 65 | |
michael@0 | 66 | int result; |
michael@0 | 67 | while ((result = mBackBuffer->Lock(aUsage, aRect, bits)) == -EBUSY) { |
michael@0 | 68 | usleep(1000); // 1ms |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | return result == 0; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | bool |
michael@0 | 75 | AndroidDirectTexture::Unlock(bool aFlip) |
michael@0 | 76 | { |
michael@0 | 77 | if (aFlip) { |
michael@0 | 78 | mNeedFlip = true; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | bool result = mBackBuffer->Unlock(); |
michael@0 | 82 | mLock.Unlock(); |
michael@0 | 83 | |
michael@0 | 84 | return result; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | bool |
michael@0 | 88 | AndroidDirectTexture::Reallocate(uint32_t aWidth, uint32_t aHeight) { |
michael@0 | 89 | return Reallocate(aWidth, aHeight, mFormat); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | bool |
michael@0 | 93 | AndroidDirectTexture::Reallocate(uint32_t aWidth, uint32_t aHeight, gfxImageFormat aFormat) |
michael@0 | 94 | { |
michael@0 | 95 | MutexAutoLock lock(mLock); |
michael@0 | 96 | |
michael@0 | 97 | // We only reallocate the current back buffer. The front buffer is likely |
michael@0 | 98 | // in use, so we'll reallocate it on the first Lock() after it is rotated |
michael@0 | 99 | // to the back. |
michael@0 | 100 | bool result = mBackBuffer->Reallocate(aWidth, aHeight, aFormat); |
michael@0 | 101 | if (result) { |
michael@0 | 102 | mPendingReallocBuffer = mFrontBuffer; |
michael@0 | 103 | |
michael@0 | 104 | mWidth = aWidth; |
michael@0 | 105 | mHeight = aHeight; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | return result; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | bool |
michael@0 | 112 | AndroidDirectTexture::Bind() |
michael@0 | 113 | { |
michael@0 | 114 | MutexAutoLock lock(mLock); |
michael@0 | 115 | |
michael@0 | 116 | if (mNeedFlip) { |
michael@0 | 117 | AndroidGraphicBuffer* tmp = mBackBuffer; |
michael@0 | 118 | mBackBuffer = mFrontBuffer; |
michael@0 | 119 | mFrontBuffer = tmp; |
michael@0 | 120 | mNeedFlip = false; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | return mFrontBuffer->Bind(); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | } /* mozilla */ |