From 682a85cb3ff0b200d4fe0f7ef41a4e1c57dea233 Mon Sep 17 00:00:00 2001 From: John Reck Date: Tue, 14 Aug 2012 16:53:39 -0700 Subject: Move text allocations to LinearAllocator Change-Id: I3ef492679e94bdb452033ee3af3162b88d323d57 --- .../graphics/android/context/GraphicsOperation.h | 17 ++++++---------- .../context/PlatformGraphicsContextRecording.cpp | 11 ++++++++--- .../graphics/android/utils/LinearAllocator.cpp | 23 +++++++++++++++++++--- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/Source/WebCore/platform/graphics/android/context/GraphicsOperation.h b/Source/WebCore/platform/graphics/android/context/GraphicsOperation.h index c70c295..edcdc35 100644 --- a/Source/WebCore/platform/graphics/android/context/GraphicsOperation.h +++ b/Source/WebCore/platform/graphics/android/context/GraphicsOperation.h @@ -587,25 +587,20 @@ class DrawPosText : public Operation { public: DrawPosText(const void* text, size_t byteLength, const SkPoint pos[], const SkPaint* paint) - : m_byteLength(byteLength) + : m_text(text) + , m_byteLength(byteLength) + , m_pos(pos) , m_paint(paint) - { - size_t points = m_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); return true; } TYPE(DrawPosTextOperation) private: - void* m_text; + const void* m_text; size_t m_byteLength; - SkPoint* m_pos; + 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 24ee47e..464224a 100644 --- a/Source/WebCore/platform/graphics/android/context/PlatformGraphicsContextRecording.cpp +++ b/Source/WebCore/platform/graphics/android/context/PlatformGraphicsContextRecording.cpp @@ -891,15 +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& inPaint) +void PlatformGraphicsContextRecording::drawPosText(const void* inText, size_t byteLength, + const SkPoint inPos[], const SkPaint& inPaint) { if (inPaint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding) { ALOGE("Unsupported text encoding! %d", inPaint.getTextEncoding()); return; } - FloatRect bounds = approximateTextBounds(byteLength / sizeof(uint16_t), pos, inPaint); + 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; -- cgit v1.1