static const uint32_t NUM_INIT_BLOCK_PTRS = 8;
GrTArray<void*> fBlocks;
- size_t fBlockSize;
- char fBlockInitialStorage[NUM_INIT_BLOCK_PTRS*sizeof(void*)];
+ size_t fBlockSize;
+ char fBlockInitialStorage[NUM_INIT_BLOCK_PTRS*sizeof(void*)];
size_t fItemSize;
uint32_t fItemsPerBlock;
bool fOwnFirstBlock;
- uint32_t fCount;
+ uint32_t fCount;
};
template <typename T>
public:
virtual ~GrTAllocator() {};
-
+
/**
* Create an allocator
*
* Must be at least size(T)*itemsPerBlock sized.
* Caller is responsible for freeing this memory.
*/
- GrTAllocator(uint32_t itemsPerBlock, void* initialBlock) :
- fAllocator(sizeof(T), itemsPerBlock, initialBlock)
- {}
+ GrTAllocator(uint32_t itemsPerBlock, void* initialBlock)
+ : fAllocator(sizeof(T), itemsPerBlock, initialBlock) {}
+
+ /**
+ * Create an allocator using a GrAlignedTAlloc as the initial block.
+ *
+ * @param initialBlock specifies the storage for the initial block
+ * and the size of subsequent blocks.
+ */
+ template <int N>
+ GrTAllocator(GrAlignedSTStorage<N,T>* initialBlock)
+ : fAllocator(sizeof(T), N, initialBlock->get()) {}
/**
* Adds an item and returns it.
#include "GrRect.h"
#include "GrPath.h"
#include "GrTArray.h"
+#include "GrTemplates.h"
class GrClip {
enum {
kPreAllocElements = 4,
};
- uint8_t fListMemory[sizeof(Element) * kPreAllocElements];
+ GrAlignedSTStorage<kPreAllocElements, Element> fListStorage;
GrTArray<Element> fList;
};
#endif
size_t fReservedIndexBytes;
size_t fUsedReservedVertexBytes;
size_t fUsedReservedIndexBytes;
+
+ enum {
+ kDrawPreallocCnt = 8,
+ kStatePreallocCnt = 8,
+ kClipPreallocCnt = 8,
+ };
+
+ GrAlignedSTStorage<kDrawPreallocCnt, Draw> fDrawStorage;
+ GrAlignedSTStorage<kStatePreallocCnt, SavedDrawState> fStateStorage;
+ GrAlignedSTStorage<kClipPreallocCnt, GrClip> fClipStorage;
- static const uint32_t STATES_BLOCK_SIZE = 8;
- static const uint32_t DRAWS_BLOCK_SIZE = 8;
- static const uint32_t CLIPS_BLOCK_SIZE = 8;
- static const uint32_t VERTEX_BLOCK_SIZE = 1 << 12;
- static const uint32_t INDEX_BLOCK_SIZE = 1 << 10;
- int8_t fDrawsStorage[sizeof(Draw) *
- DRAWS_BLOCK_SIZE];
- int8_t fStatesStorage[sizeof(SavedDrawState) *
- STATES_BLOCK_SIZE];
- int8_t fClipsStorage[sizeof(GrClip) *
- CLIPS_BLOCK_SIZE];
typedef GrDrawTarget INHERITED;
};
#include <new>
#include "GrTypes.h"
+#include "GrTemplates.h"
// DATA_TYPE indicates that T has a trivial cons, destructor
// and can be shallow-copied
fPreAllocMemArray = NULL;
}
+ template <int N>
+ GrTArray(GrAlignedSTStorage<N,T>* storage) {
+ GrAssert(N > 0);
+ fCount = 0;
+ fReserveCount = N;
+ fAllocCount = N;
+ fMemArray = storage->get();
+ fPreAllocMemArray = storage->get();
+ }
+
GrTArray(void* preAllocStorage, int preAllocCount) {
GrAssert(preAllocCount >= 0);
// we allow NULL,0 args and revert to the default cons. behavior
fAllocCount = GrMax(fReserveCount, fCount);
fMemArray = GrMalloc(sizeof(T) * fAllocCount);
fPreAllocMemArray = NULL;
+
if (DATA_TYPE) {
memcpy(fMemArray, array.fMemArray, sizeof(T) * fCount);
} else {
--- /dev/null
+/*
+ Copyright 2010 Google Inc.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ */
+
+#ifndef GrTemplates_DEFINED
+#define GrTemplates_DEFINED
+
+#include "GrNoncopyable.h"
+
+/**
+ * Use to cast a ptr to a different type, and maintain strict-aliasing
+ */
+template <typename Dst, typename Src> Dst GrTCast(Src src) {
+ union {
+ Src src;
+ Dst dst;
+ } data;
+ data.src = src;
+ return data.dst;
+}
+
+/**
+ * Reserves memory that is aligned on double and pointer boundaries.
+ * Hopefully this is sufficient for all practical purposes.
+ */
+template <size_t N> class GrAlignedSStorage : GrNoncopyable {
+public:
+ void* get() { return fData; }
+private:
+ union {
+ void* fPtr;
+ double fDouble;
+ char fData[N];
+ };
+};
+
+/**
+ * Reserves memory that is aligned on double and pointer boundaries.
+ * Hopefully this is sufficient for all practical purposes. Otherwise,
+ * we have to do some arcane trickery to determine alignment of non-POD
+ * types. Lifetime of the memory is the lifetime of the object.
+ */
+template <int N, typename T> class GrAlignedSTStorage : GrNoncopyable {
+public:
+ /**
+ * Returns void* because this object does not initialize the
+ * memory. Use placement new for types that require a cons.
+ */
+ void* get() { return fStorage.get(); }
+private:
+ GrAlignedSStorage<sizeof(T)*N> fStorage;
+};
+
+/**
+ * saves value of T* in and restores in destructor
+ * e.g.:
+ * {
+ * GrAutoTPtrValueRestore<int*> autoCountRestore;
+ * if (useExtra) {
+ * autoCountRestore.save(&fCount);
+ * fCount += fExtraCount;
+ * }
+ * ...
+ * } // fCount is restored
+ */
+template <typename T> class GrAutoTPtrValueRestore : public GrNoncopyable {
+public:
+ GrAutoTPtrValueRestore() : fPtr(NULL), fVal() {}
+
+ GrAutoTPtrValueRestore(T* ptr) {
+ fPtr = ptr;
+ if (NULL != ptr) {
+ fVal = *ptr;
+ }
+ }
+
+ ~GrAutoTPtrValueRestore() {
+ if (NULL != fPtr) {
+ *fPtr = fVal;
+ }
+ }
+
+ // restores previously saved value (if any) and saves value for passed T*
+ void save(T* ptr) {
+ if (NULL != fPtr) {
+ *fPtr = fVal;
+ }
+ fPtr = ptr;
+ fVal = *ptr;
+ }
+private:
+ T* fPtr;
+ T fVal;
+};
+
+#endif
\ No newline at end of file
#endif
-///////////////////////////////////////////////////////////////////////////////
-
-/**
- * Use to cast a ptr to a different type, and maintain strict-aliasing
- */
-template <typename Dst, typename Src> Dst GrTCast(Src src) {
- union {
- Src src;
- Dst dst;
- } data;
- data.src = src;
- return data.dst;
-}
-
-///////////////////////////////////////////////////////////////////////////////
-
-// saves value of T* in and restores in destructor
-// e.g.:
-// {
-// GrAutoTPtrValueRestore<int*> autoCountRestore;
-// if (useExtra) {
-// autoCountRestore.save(&fCount);
-// fCount += fExtraCount;
-// }
-// ...
-// } // fCount is restored
-//
-template <typename T>
-class GrAutoTPtrValueRestore {
-public:
- GrAutoTPtrValueRestore() : fPtr(NULL), fVal() {}
-
- GrAutoTPtrValueRestore(T* ptr) {
- fPtr = ptr;
- if (NULL != ptr) {
- fVal = *ptr;
- }
- }
-
- ~GrAutoTPtrValueRestore() {
- if (NULL != fPtr) {
- *fPtr = fVal;
- }
- }
-
- // restores previously saved value (if any) and saves value for passed T*
- void save(T* ptr) {
- if (NULL != fPtr) {
- *fPtr = fVal;
- }
- fPtr = ptr;
- fVal = *ptr;
- }
-private:
- T* fPtr;
- T fVal;
-};
///////////////////////////////////////////////////////////////////////////////
#include "GrClip.h"
GrClip::GrClip()
- : fList(fListMemory, kPreAllocElements) {
+ : fList(&fListStorage) {
fConservativeBounds.setEmpty();
fConservativeBoundsValid = true;
}
GrClip::GrClip(const GrClip& src)
- : fList(fListMemory, kPreAllocElements) {
+ : fList(&fListStorage) {
*this = src;
}
GrClip::GrClip(const GrIRect& rect)
- : fList(fListMemory, kPreAllocElements) {
+ : fList(&fListStorage) {
this->setFromIRect(rect);
}
GrClip::GrClip(const GrRect& rect)
- : fList(fListMemory, kPreAllocElements) {
+ : fList(&fListStorage) {
this->setFromRect(rect);
}
GrClip::GrClip(GrClipIterator* iter, GrScalar tx, GrScalar ty,
const GrRect* bounds)
- : fList(fListMemory, kPreAllocElements) {
+ : fList(&fListStorage) {
this->setFromIterator(iter, tx, ty, bounds);
}
GrInOrderDrawBuffer::GrInOrderDrawBuffer(GrVertexBufferAllocPool* vertexPool,
GrIndexBufferAllocPool* indexPool) :
- fDraws(DRAWS_BLOCK_SIZE, fDrawsStorage),
- fStates(STATES_BLOCK_SIZE, fStatesStorage),
- fClips(CLIPS_BLOCK_SIZE, fClipsStorage),
+ fDraws(&fDrawStorage),
+ fStates(&fStateStorage),
+ fClips(&fClipStorage),
fClipSet(true),
fLastRectVertexLayout(0),