[657] | 1 | /*
|
---|
| 2 | -----------------------------------------------------------------------------
|
---|
| 3 | This source file is part of OGRE
|
---|
| 4 | (Object-oriented Graphics Rendering Engine)
|
---|
| 5 | For the latest info, see http://ogre.sourceforge.net/
|
---|
| 6 |
|
---|
| 7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
| 8 | Also see acknowledgements in Readme.html
|
---|
| 9 |
|
---|
| 10 | This program is free software; you can redistribute it and/or modify it under
|
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
| 13 | version.
|
---|
| 14 |
|
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
| 18 |
|
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
| 23 | -----------------------------------------------------------------------------
|
---|
| 24 | */
|
---|
| 25 | #include "OgreStableHeaders.h"
|
---|
| 26 | #include "OgreHardwarePixelBuffer.h"
|
---|
| 27 | #include "OgreImage.h"
|
---|
| 28 | #include "OgreException.h"
|
---|
| 29 |
|
---|
| 30 | namespace Ogre
|
---|
| 31 | {
|
---|
| 32 |
|
---|
| 33 | //-----------------------------------------------------------------------------
|
---|
| 34 | HardwarePixelBuffer::HardwarePixelBuffer(size_t width, size_t height, size_t depth,
|
---|
| 35 | PixelFormat format,
|
---|
| 36 | HardwareBuffer::Usage usage, bool useSystemMemory, bool useShadowBuffer):
|
---|
| 37 | HardwareBuffer(usage, useSystemMemory, useShadowBuffer),
|
---|
| 38 | mWidth(width), mHeight(height), mDepth(depth),
|
---|
| 39 | mFormat(format)
|
---|
| 40 | {
|
---|
| 41 | // Default
|
---|
| 42 | mRowPitch = mWidth;
|
---|
| 43 | mSlicePitch = mHeight*mWidth;
|
---|
| 44 | mSizeInBytes = mHeight*mWidth*PixelUtil::getNumElemBytes(mFormat);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | //-----------------------------------------------------------------------------
|
---|
| 48 | HardwarePixelBuffer::~HardwarePixelBuffer()
|
---|
| 49 | {
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | //-----------------------------------------------------------------------------
|
---|
| 53 | void* HardwarePixelBuffer::lock(size_t offset, size_t length, LockOptions options)
|
---|
| 54 | {
|
---|
| 55 | assert(!isLocked() && "Cannot lock this buffer, it is already locked!");
|
---|
| 56 | assert(offset == 0 && length == mSizeInBytes && "Cannot lock memory region, most lock box or entire buffer");
|
---|
| 57 |
|
---|
| 58 | Image::Box myBox(0, 0, 0, mWidth, mHeight, mDepth);
|
---|
| 59 | const PixelBox &rv = lock(myBox, options);
|
---|
| 60 | return rv.data;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | //-----------------------------------------------------------------------------
|
---|
| 64 | const PixelBox& HardwarePixelBuffer::lock(const Image::Box& lockBox, LockOptions options)
|
---|
| 65 | {
|
---|
| 66 | if (mUseShadowBuffer)
|
---|
| 67 | {
|
---|
| 68 | if (options != HBL_READ_ONLY)
|
---|
| 69 | {
|
---|
| 70 | // we have to assume a read / write lock so we use the shadow buffer
|
---|
| 71 | // and tag for sync on unlock()
|
---|
| 72 | mShadowUpdated = true;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | mCurrentLock = static_cast<HardwarePixelBuffer*>(mpShadowBuffer)->lock(lockBox, options);
|
---|
| 76 | }
|
---|
| 77 | else
|
---|
| 78 | {
|
---|
| 79 | // Lock the real buffer if there is no shadow buffer
|
---|
| 80 | mCurrentLock = lockImpl(lockBox, options);
|
---|
| 81 | mIsLocked = true;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | return mCurrentLock;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | //-----------------------------------------------------------------------------
|
---|
| 88 | const PixelBox& HardwarePixelBuffer::getCurrentLock()
|
---|
| 89 | {
|
---|
| 90 | assert(isLocked() && "Cannot get current lock: buffer not locked");
|
---|
| 91 |
|
---|
| 92 | return mCurrentLock;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | //-----------------------------------------------------------------------------
|
---|
| 96 | /// Internal implementation of lock()
|
---|
| 97 | void* HardwarePixelBuffer::lockImpl(size_t offset, size_t length, LockOptions options)
|
---|
| 98 | {
|
---|
| 99 | OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, "lockImpl(offset,length) is not valid for PixelBuffers and should never be called",
|
---|
| 100 | "HardwarePixelBuffer::lockImpl");
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | //-----------------------------------------------------------------------------
|
---|
| 104 |
|
---|
| 105 | void HardwarePixelBuffer::blit(HardwarePixelBuffer *src, const Image::Box &srcBox, const Image::Box &dstBox)
|
---|
| 106 | {
|
---|
| 107 | if(isLocked() || src->isLocked())
|
---|
| 108 | {
|
---|
| 109 | OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
|
---|
| 110 | "Source and destination buffer may not be locked!",
|
---|
| 111 | "HardwarePixelBuffer::blit");
|
---|
| 112 | }
|
---|
| 113 | if(src == this)
|
---|
| 114 | {
|
---|
| 115 | OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS,
|
---|
| 116 | "Source must not be the same object",
|
---|
| 117 | "HardwarePixelBuffer::blit" ) ;
|
---|
| 118 | }
|
---|
| 119 | const PixelBox &srclock = src->lock(srcBox, HBL_READ_ONLY);
|
---|
| 120 |
|
---|
| 121 | LockOptions method = HBL_NORMAL;
|
---|
| 122 | if(dstBox.left == 0 && dstBox.top == 0 && dstBox.front == 0 &&
|
---|
| 123 | dstBox.right == mWidth && dstBox.bottom == mHeight &&
|
---|
| 124 | dstBox.back == mDepth)
|
---|
| 125 | // Entire buffer -- we can discard the previous contents
|
---|
| 126 | method = HBL_DISCARD;
|
---|
| 127 |
|
---|
| 128 | const PixelBox &dstlock = lock(dstBox, method);
|
---|
| 129 | if(dstlock.getWidth() != srclock.getWidth() ||
|
---|
| 130 | dstlock.getHeight() != srclock.getHeight() ||
|
---|
| 131 | dstlock.getDepth() != srclock.getDepth())
|
---|
| 132 | {
|
---|
| 133 | // Scaling desired
|
---|
| 134 | Image::scale(srclock, dstlock);
|
---|
| 135 | }
|
---|
| 136 | else
|
---|
| 137 | {
|
---|
| 138 | // No scaling needed
|
---|
| 139 | PixelUtil::bulkPixelConversion(srclock, dstlock);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | unlock();
|
---|
| 143 | src->unlock();
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 |
|
---|
| 147 | void HardwarePixelBuffer::readData(size_t offset, size_t length, void* pDest)
|
---|
| 148 | {
|
---|
| 149 | // TODO
|
---|
| 150 | OGRE_EXCEPT(Exception::UNIMPLEMENTED_FEATURE,
|
---|
| 151 | "Reading a byte range is not implemented. Use blitToMemory.",
|
---|
| 152 | "HardwarePixelBuffer::readData");
|
---|
| 153 | }
|
---|
| 154 | //-----------------------------------------------------------------------------
|
---|
| 155 |
|
---|
| 156 | void HardwarePixelBuffer::writeData(size_t offset, size_t length, const void* pSource,
|
---|
| 157 | bool discardWholeBuffer)
|
---|
| 158 | {
|
---|
| 159 | // TODO
|
---|
| 160 | OGRE_EXCEPT(Exception::UNIMPLEMENTED_FEATURE,
|
---|
| 161 | "Writing a byte range is not implemented. Use blitFromMemory.",
|
---|
| 162 | "HardwarePixelBuffer::writeData");
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | //-----------------------------------------------------------------------------
|
---|
| 166 |
|
---|
| 167 | HardwarePixelBufferSharedPtr::HardwarePixelBufferSharedPtr(HardwarePixelBuffer* buf)
|
---|
| 168 | : SharedPtr<HardwarePixelBuffer>(buf)
|
---|
| 169 | {
|
---|
| 170 |
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | };
|
---|