Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(408)

Side by Side Diff: src/core/SkGlyphCache.cpp

Issue 17449012: combine glyph and image bulk alloc, and adjust initial alloc size, to reduce total waste from 50% t… (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/core/SkGlyphCache.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #include "SkGlyphCache.h" 10 #include "SkGlyphCache.h"
(...skipping 30 matching lines...) Expand all
41 } 41 }
42 } 42 }
43 #else 43 #else
44 #define RecordHashSuccess() (void)0 44 #define RecordHashSuccess() (void)0
45 #define RecordHashCollisionIf(pred) (void)0 45 #define RecordHashCollisionIf(pred) (void)0
46 #endif 46 #endif
47 #define RecordHashCollision() RecordHashCollisionIf(true) 47 #define RecordHashCollision() RecordHashCollisionIf(true)
48 48
49 /////////////////////////////////////////////////////////////////////////////// 49 ///////////////////////////////////////////////////////////////////////////////
50 50
51 #define kMinGlphAlloc (sizeof(SkGlyph) * 64) 51 // so we don't grow our arrays a lot
52 #define kMinImageAlloc (24 * 64) // should be pointsize-dependent 52 #define kMinGlyphCount 16
53 53 #define kMinGlyphImageSize (16*2)
54 #define METRICS_RESERVE_COUNT 128 // so we don't grow this array a lot 54 #define kMinAllocAmount ((sizeof(SkGlyph) + kMinGlyphImageSize) * kMinGlyphC ount)
55 55
56 SkGlyphCache::SkGlyphCache(SkTypeface* typeface, const SkDescriptor* desc) 56 SkGlyphCache::SkGlyphCache(SkTypeface* typeface, const SkDescriptor* desc)
57 : fGlyphAlloc(kMinGlphAlloc) 57 : fGlyphAlloc(kMinAllocAmount) {
58 , fImageAlloc(kMinImageAlloc) {
59 SkASSERT(typeface); 58 SkASSERT(typeface);
60 59
61 fPrev = fNext = NULL; 60 fPrev = fNext = NULL;
62 61
63 fDesc = desc->copy(); 62 fDesc = desc->copy();
64 fScalerContext = typeface->createScalerContext(desc); 63 fScalerContext = typeface->createScalerContext(desc);
65 fScalerContext->getFontMetrics(&fFontMetrics); 64 fScalerContext->getFontMetrics(&fFontMetrics);
66 65
67 // init to 0 so that all of the pointers will be null 66 // init to 0 so that all of the pointers will be null
68 memset(fGlyphHash, 0, sizeof(fGlyphHash)); 67 memset(fGlyphHash, 0, sizeof(fGlyphHash));
69 // init with 0xFF so that the charCode field will be -1, which is invalid 68 // init with 0xFF so that the charCode field will be -1, which is invalid
70 memset(fCharToGlyphHash, 0xFF, sizeof(fCharToGlyphHash)); 69 memset(fCharToGlyphHash, 0xFF, sizeof(fCharToGlyphHash));
71 70
72 fMemoryUsed = sizeof(*this) + kMinGlphAlloc + kMinImageAlloc; 71 fMemoryUsed = sizeof(*this);
73 72
74 fGlyphArray.setReserve(METRICS_RESERVE_COUNT); 73 fGlyphArray.setReserve(kMinGlyphCount);
75 74
76 fMetricsCount = 0; 75 fMetricsCount = 0;
77 fAdvanceCount = 0; 76 fAdvanceCount = 0;
78 fAuxProcList = NULL; 77 fAuxProcList = NULL;
79 } 78 }
80 79
81 SkGlyphCache::~SkGlyphCache() { 80 SkGlyphCache::~SkGlyphCache() {
81 #if 0
82 {
83 size_t ptrMem = fGlyphArray.count() * sizeof(SkGlyph*);
84 size_t glyphAlloc = fGlyphAlloc.totalCapacity();
85 size_t glyphHashUsed = 0;
86 size_t uniHashUsed = 0;
87 for (int i = 0; i < kHashCount; ++i) {
88 glyphHashUsed += fGlyphHash[i] ? sizeof(fGlyphHash[0]) : 0;
89 uniHashUsed += fCharToGlyphHash[i].fID != 0xFFFFFFFF ? sizeof(fCharT oGlyphHash[0]) : 0;
90 }
91 size_t glyphUsed = fGlyphArray.count() * sizeof(SkGlyph);
92 size_t imageUsed = 0;
93 for (int i = 0; i < fGlyphArray.count(); ++i) {
94 const SkGlyph& g = *fGlyphArray[i];
95 if (g.fImage) {
96 imageUsed += g.fHeight * g.rowBytes();
97 }
98 }
99
100 printf("glyphPtrArray,%zu, Alloc,%zu, imageUsed,%zu, glyphUsed,%zu, glyp hHashAlloc,%zu, glyphHashUsed,%zu, unicharHashAlloc,%zu, unicharHashUsed,%zu\n",
101 ptrMem, glyphAlloc, imageUsed, glyphUsed, sizeof(fGlyphHash), g lyphHashUsed, sizeof(fCharToGlyphHash), uniHashUsed);
102
103 }
104 #endif
82 SkGlyph** gptr = fGlyphArray.begin(); 105 SkGlyph** gptr = fGlyphArray.begin();
83 SkGlyph** stop = fGlyphArray.end(); 106 SkGlyph** stop = fGlyphArray.end();
84 while (gptr < stop) { 107 while (gptr < stop) {
85 SkPath* path = (*gptr)->fPath; 108 SkPath* path = (*gptr)->fPath;
86 if (path) { 109 if (path) {
87 SkDELETE(path); 110 SkDELETE(path);
88 } 111 }
89 gptr += 1; 112 gptr += 1;
90 } 113 }
91 SkDescriptor::Free(fDesc); 114 SkDescriptor::Free(fDesc);
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 fMetricsCount += 1; 312 fMetricsCount += 1;
290 } 313 }
291 314
292 return glyph; 315 return glyph;
293 } 316 }
294 317
295 const void* SkGlyphCache::findImage(const SkGlyph& glyph) { 318 const void* SkGlyphCache::findImage(const SkGlyph& glyph) {
296 if (glyph.fWidth > 0 && glyph.fWidth < kMaxGlyphWidth) { 319 if (glyph.fWidth > 0 && glyph.fWidth < kMaxGlyphWidth) {
297 if (glyph.fImage == NULL) { 320 if (glyph.fImage == NULL) {
298 size_t size = glyph.computeImageSize(); 321 size_t size = glyph.computeImageSize();
299 const_cast<SkGlyph&>(glyph).fImage = fImageAlloc.alloc(size, 322 const_cast<SkGlyph&>(glyph).fImage = fGlyphAlloc.alloc(size,
300 SkChunkAlloc::kReturnNil_AllocFailType); 323 SkChunkAlloc::kReturnNil_AllocFailType);
301 // check that alloc() actually succeeded 324 // check that alloc() actually succeeded
302 if (glyph.fImage) { 325 if (glyph.fImage) {
303 fScalerContext->getImage(glyph); 326 fScalerContext->getImage(glyph);
304 // TODO: the scaler may have changed the maskformat during 327 // TODO: the scaler may have changed the maskformat during
305 // getImage (e.g. from AA or LCD to BW) which means we may have 328 // getImage (e.g. from AA or LCD to BW) which means we may have
306 // overallocated the buffer. Check if the new computedImageSize 329 // overallocated the buffer. Check if the new computedImageSize
307 // is smaller, and if so, strink the alloc size in fImageAlloc. 330 // is smaller, and if so, strink the alloc size in fImageAlloc.
308 fMemoryUsed += size; 331 fMemoryUsed += size;
309 } 332 }
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after
701 724
702 #ifdef SK_DEBUG 725 #ifdef SK_DEBUG
703 void SkGlyphCache::validate() const { 726 void SkGlyphCache::validate() const {
704 #ifdef SK_DEBUG_GLYPH_CACHE 727 #ifdef SK_DEBUG_GLYPH_CACHE
705 int count = fGlyphArray.count(); 728 int count = fGlyphArray.count();
706 for (int i = 0; i < count; i++) { 729 for (int i = 0; i < count; i++) {
707 const SkGlyph* glyph = fGlyphArray[i]; 730 const SkGlyph* glyph = fGlyphArray[i];
708 SkASSERT(glyph); 731 SkASSERT(glyph);
709 SkASSERT(fGlyphAlloc.contains(glyph)); 732 SkASSERT(fGlyphAlloc.contains(glyph));
710 if (glyph->fImage) { 733 if (glyph->fImage) {
711 SkASSERT(fImageAlloc.contains(glyph->fImage)); 734 SkASSERT(fGlyphAlloc.contains(glyph->fImage));
712 } 735 }
713 } 736 }
714 #endif 737 #endif
715 } 738 }
716 #endif 739 #endif
717 740
718 /////////////////////////////////////////////////////////////////////////////// 741 ///////////////////////////////////////////////////////////////////////////////
719 /////////////////////////////////////////////////////////////////////////////// 742 ///////////////////////////////////////////////////////////////////////////////
720 743
721 #include "SkTypefaceCache.h" 744 #include "SkTypefaceCache.h"
(...skipping 20 matching lines...) Expand all
742 return tls ? tls->getFontCacheLimit() : 0; 765 return tls ? tls->getFontCacheLimit() : 0;
743 } 766 }
744 767
745 void SkGraphics::SetTLSFontCacheLimit(size_t bytes) { 768 void SkGraphics::SetTLSFontCacheLimit(size_t bytes) {
746 if (0 == bytes) { 769 if (0 == bytes) {
747 SkGlyphCache_Globals::DeleteTLS(); 770 SkGlyphCache_Globals::DeleteTLS();
748 } else { 771 } else {
749 SkGlyphCache_Globals::GetTLS().setFontCacheLimit(bytes); 772 SkGlyphCache_Globals::GetTLS().setFontCacheLimit(bytes);
750 } 773 }
751 } 774 }
OLDNEW
« no previous file with comments | « src/core/SkGlyphCache.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698