diff options
Diffstat (limited to 'Source/WebCore/platform/graphics')
3 files changed, 80 insertions, 24 deletions
diff --git a/Source/WebCore/platform/graphics/android/context/GraphicsOperation.h b/Source/WebCore/platform/graphics/android/context/GraphicsOperation.h index c9c382a..edcdc35 100644 --- a/Source/WebCore/platform/graphics/android/context/GraphicsOperation.h +++ b/Source/WebCore/platform/graphics/android/context/GraphicsOperation.h @@ -586,27 +586,22 @@ private: class DrawPosText : public Operation { public: DrawPosText(const void* text, size_t byteLength, - const SkPoint pos[], const SkPaint& paint) - : m_byteLength(byteLength) + const SkPoint pos[], const SkPaint* paint) + : m_text(text) + , m_byteLength(byteLength) + , m_pos(pos) , m_paint(paint) - { - size_t points = paint.countText(text, byteLength); - m_pos = new SkPoint[points]; - memcpy(m_pos, pos, sizeof(SkPoint) * points); - m_text = malloc(byteLength); - memcpy(m_text, text, byteLength); - } - ~DrawPosText() { delete m_pos; free(m_text); } + {} virtual bool applyImpl(PlatformGraphicsContext* context) { - context->drawPosText(m_text, m_byteLength, m_pos, m_paint); + context->drawPosText(m_text, m_byteLength, m_pos, *m_paint); return true; } TYPE(DrawPosTextOperation) private: - void* m_text; + const void* m_text; size_t m_byteLength; - SkPoint* m_pos; - SkPaint m_paint; + const SkPoint* m_pos; + const SkPaint* m_paint; }; } diff --git a/Source/WebCore/platform/graphics/android/context/PlatformGraphicsContextRecording.cpp b/Source/WebCore/platform/graphics/android/context/PlatformGraphicsContextRecording.cpp index 5efecd8..464224a 100644 --- a/Source/WebCore/platform/graphics/android/context/PlatformGraphicsContextRecording.cpp +++ b/Source/WebCore/platform/graphics/android/context/PlatformGraphicsContextRecording.cpp @@ -98,7 +98,24 @@ public: static const bool safeToCompareToEmptyOrDeleted = false; }; +class SkPaintHash { +public: + static unsigned hash(const SkPaint* const& paint) + { + return StringHasher::hashMemory(paint, sizeof(SkPaint)); + } + + static bool equal(const SkPaint* const& a, + const SkPaint* const& b) + { + return a && b && (*a == *b); + } + + static const bool safeToCompareToEmptyOrDeleted = false; +}; + typedef HashSet<PlatformGraphicsContext::State*, StateHash> StateHashSet; +typedef HashSet<const SkPaint*, SkPaintHash> SkPaintHashSet; class CanvasState { public: @@ -187,11 +204,13 @@ private: // the last thing destroyed. LinearAllocator m_operationHeap; LinearAllocator m_canvasStateHeap; + // Used by both PlatformGraphicsContext::State and SkPaint, as both are + // roughly the same size (72 bytes vs. 76 bytes, respectively) LinearAllocator m_stateHeap; public: RecordingImpl() : m_canvasStateHeap(sizeof(CanvasState)) - , m_stateHeap(sizeof(PlatformGraphicsContext::State)) + , m_stateHeap(sizeof(SkPaint)) , m_nodeCount(0) { } @@ -199,6 +218,7 @@ public: ~RecordingImpl() { clearStates(); clearCanvasStates(); + clearSkPaints(); } PlatformGraphicsContext::State* getState(PlatformGraphicsContext::State* inState) { @@ -211,6 +231,16 @@ public: return state; } + const SkPaint* getSkPaint(const SkPaint& inPaint) { + SkPaintHashSet::iterator it = m_paints.find(&inPaint); + if (it != m_paints.end()) + return (*it); + void* buf = m_stateHeap.alloc(sizeof(SkPaint)); + SkPaint* paint = new (buf) SkPaint(inPaint); + m_paints.add(paint); + return paint; + } + void addCanvasState(CanvasState* state) { m_canvasStates.append(state); } @@ -269,14 +299,21 @@ private: m_states.clear(); } + void clearSkPaints() { + SkPaintHashSet::iterator end = m_paints.end(); + for (SkPaintHashSet::iterator it = m_paints.begin(); it != end; ++it) + (*it)->~SkPaint(); + m_paints.clear(); + } + void clearCanvasStates() { for (size_t i = 0; i < m_canvasStates.size(); i++) m_canvasStates[i]->~CanvasState(); m_canvasStates.clear(); } - // TODO: Use a global pool? StateHashSet m_states; + SkPaintHashSet m_paints; Vector<CanvasState*> m_canvasStates; }; @@ -854,13 +891,20 @@ void PlatformGraphicsContextRecording::strokeRect(const FloatRect& rect, float l appendDrawingOperation(NEW_OP(StrokeRect)(rect, lineWidth), bounds); } -void PlatformGraphicsContextRecording::drawPosText(const void* text, size_t byteLength, - const SkPoint pos[], const SkPaint& paint) +void PlatformGraphicsContextRecording::drawPosText(const void* inText, size_t byteLength, + const SkPoint inPos[], const SkPaint& inPaint) { - if (paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding) { - ALOGE("Unsupported text encoding! %d", paint.getTextEncoding()); + if (inPaint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding) { + ALOGE("Unsupported text encoding! %d", inPaint.getTextEncoding()); + return; } - FloatRect bounds = approximateTextBounds(byteLength / sizeof(uint16_t), pos, paint); + FloatRect bounds = approximateTextBounds(byteLength / sizeof(uint16_t), inPos, inPaint); + const SkPaint* paint = mRecording->recording()->getSkPaint(inPaint); + int posSize = sizeof(SkPoint) * paint->countText(inText, byteLength); + void* text = operationHeap()->alloc(posSize + byteLength); + SkPoint* pos = (SkPoint*) ((char*)text + byteLength); + memcpy(text, inText, byteLength); + memcpy(pos, inPos, posSize); appendDrawingOperation(NEW_OP(DrawPosText)(text, byteLength, pos, paint), bounds); } diff --git a/Source/WebCore/platform/graphics/android/utils/LinearAllocator.cpp b/Source/WebCore/platform/graphics/android/utils/LinearAllocator.cpp index 1899557..8aa1616 100644 --- a/Source/WebCore/platform/graphics/android/utils/LinearAllocator.cpp +++ b/Source/WebCore/platform/graphics/android/utils/LinearAllocator.cpp @@ -39,6 +39,14 @@ namespace WebCore { // our pool needs to big enough to hold at least this many items #define MIN_OBJECT_COUNT 4 +// The maximum amount of wasted space we can have per page +// Allocations exceeding this will have their own dedicated page +// If this is too low, we will malloc too much +// Too high, and we may waste too much space +#define MAX_WASTE_SIZE ((size_t)256) + +#define ALIGN(x) (x + (x % sizeof(int))) + #if LOG_NDEBUG #define ADD_ALLOCATION(size) #define RM_ALLOCATION(size) @@ -103,7 +111,8 @@ LinearAllocator::LinearAllocator(size_t averageAllocSize) m_pageSize = pcount * averageAllocSize + sizeof(LinearAllocator::Page); } else m_pageSize = TARGET_PAGE_SIZE; - m_maxAllocSize = (m_pageSize - sizeof(LinearAllocator::Page)); + m_pageSize = ALIGN(m_pageSize); + m_maxAllocSize = std::min(MAX_WASTE_SIZE, (m_pageSize - sizeof(LinearAllocator::Page))); } LinearAllocator::~LinearAllocator(void) @@ -153,9 +162,17 @@ unsigned LinearAllocator::memusage() void* LinearAllocator::alloc(size_t size) { + size = ALIGN(size); if (size > m_maxAllocSize) { - ALOGE("Allocation too large! (%d exceeds max size %d)", size, m_maxAllocSize); - return 0; + // Allocation is too large, create a dedicated page for the allocation + ADD_ALLOCATION(size); + void* buf = malloc(size + sizeof(LinearAllocator::Page)); + Page* page = new (buf) Page(); + page->setNext(m_pages); + m_pages = page; + if (!m_currentPage) + m_currentPage = m_pages; + return start(page); } ensureNext(size); void* ptr = m_next; |