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

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

Issue 26572006: Revert "Revert "change SkColorTable to be immutable"" (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 2 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/SkBitmapProcState_procs.h ('k') | src/core/SkProcSpriteBlitter.cpp » ('j') | 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 2009 The Android Open Source Project 3 * Copyright 2009 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 "SkColorTable.h" 10 #include "SkColorTable.h"
11 #include "SkFlattenableBuffers.h" 11 #include "SkFlattenableBuffers.h"
12 #include "SkStream.h" 12 #include "SkStream.h"
13 #include "SkTemplates.h" 13 #include "SkTemplates.h"
14 14
15 SK_DEFINE_INST_COUNT(SkColorTable) 15 SK_DEFINE_INST_COUNT(SkColorTable)
16 16
17 SkColorTable::SkColorTable(int count)
18 : f16BitCache(NULL), fFlags(0)
19 {
20 if (count < 0)
21 count = 0;
22 else if (count > 256)
23 count = 256;
24
25 fCount = SkToU16(count);
26 fColors = (SkPMColor*)sk_malloc_throw(count * sizeof(SkPMColor));
27 memset(fColors, 0, count * sizeof(SkPMColor));
28
29 SkDEBUGCODE(fColorLockCount = 0;)
30 SkDEBUGCODE(f16BitCacheLockCount = 0;)
31 }
32
33 // As copy constructor is hidden in the class hierarchy, we need to call 17 // As copy constructor is hidden in the class hierarchy, we need to call
34 // default constructor explicitly to suppress a compiler warning. 18 // default constructor explicitly to suppress a compiler warning.
35 SkColorTable::SkColorTable(const SkColorTable& src) : INHERITED() { 19 SkColorTable::SkColorTable(const SkColorTable& src) : INHERITED() {
36 f16BitCache = NULL; 20 f16BitCache = NULL;
37 fFlags = src.fFlags; 21 fAlphaType = src.fAlphaType;
38 int count = src.count(); 22 int count = src.count();
39 fCount = SkToU16(count); 23 fCount = SkToU16(count);
40 fColors = reinterpret_cast<SkPMColor*>( 24 fColors = reinterpret_cast<SkPMColor*>(
41 sk_malloc_throw(count * sizeof(SkPMColor))); 25 sk_malloc_throw(count * sizeof(SkPMColor)));
42 memcpy(fColors, src.fColors, count * sizeof(SkPMColor)); 26 memcpy(fColors, src.fColors, count * sizeof(SkPMColor));
43 27
44 SkDEBUGCODE(fColorLockCount = 0;) 28 SkDEBUGCODE(fColorLockCount = 0;)
45 SkDEBUGCODE(f16BitCacheLockCount = 0;) 29 SkDEBUGCODE(f16BitCacheLockCount = 0;)
46 } 30 }
47 31
48 SkColorTable::SkColorTable(const SkPMColor colors[], int count) 32 SkColorTable::SkColorTable(const SkPMColor colors[], int count, SkAlphaType at)
49 : f16BitCache(NULL), fFlags(0) 33 : f16BitCache(NULL), fAlphaType(SkToU8(at))
50 { 34 {
51 if (count < 0) 35 SkASSERT(0 == count || NULL != colors);
36
37 if (count < 0) {
52 count = 0; 38 count = 0;
53 else if (count > 256) 39 } else if (count > 256) {
54 count = 256; 40 count = 256;
41 }
55 42
56 fCount = SkToU16(count); 43 fCount = SkToU16(count);
57 fColors = reinterpret_cast<SkPMColor*>( 44 fColors = reinterpret_cast<SkPMColor*>(
58 sk_malloc_throw(count * sizeof(SkPMColor))); 45 sk_malloc_throw(count * sizeof(SkPMColor)));
59 46
60 if (colors) 47 memcpy(fColors, colors, count * sizeof(SkPMColor));
61 memcpy(fColors, colors, count * sizeof(SkPMColor));
62 48
63 SkDEBUGCODE(fColorLockCount = 0;) 49 SkDEBUGCODE(fColorLockCount = 0;)
64 SkDEBUGCODE(f16BitCacheLockCount = 0;) 50 SkDEBUGCODE(f16BitCacheLockCount = 0;)
65 } 51 }
66 52
67 SkColorTable::~SkColorTable() 53 SkColorTable::~SkColorTable()
68 { 54 {
69 SkASSERT(fColorLockCount == 0); 55 SkASSERT(fColorLockCount == 0);
70 SkASSERT(f16BitCacheLockCount == 0); 56 SkASSERT(f16BitCacheLockCount == 0);
71 57
72 sk_free(fColors); 58 sk_free(fColors);
73 sk_free(f16BitCache); 59 sk_free(f16BitCache);
74 } 60 }
75 61
76 void SkColorTable::setFlags(unsigned flags) 62 void SkColorTable::unlockColors() {
77 {
78 fFlags = SkToU8(flags);
79 }
80
81 void SkColorTable::unlockColors(bool changed)
82 {
83 SkASSERT(fColorLockCount != 0); 63 SkASSERT(fColorLockCount != 0);
84 SkDEBUGCODE(sk_atomic_dec(&fColorLockCount);) 64 SkDEBUGCODE(sk_atomic_dec(&fColorLockCount);)
85 if (changed)
86 this->inval16BitCache();
87 }
88
89 void SkColorTable::inval16BitCache()
90 {
91 SkASSERT(f16BitCacheLockCount == 0);
92 if (f16BitCache)
93 {
94 sk_free(f16BitCache);
95 f16BitCache = NULL;
96 }
97 } 65 }
98 66
99 #include "SkColorPriv.h" 67 #include "SkColorPriv.h"
100 68
101 static inline void build_16bitcache(uint16_t dst[], const SkPMColor src[], int c ount) 69 static inline void build_16bitcache(uint16_t dst[], const SkPMColor src[],
102 { 70 int count) {
103 while (--count >= 0) 71 while (--count >= 0) {
104 *dst++ = SkPixel32ToPixel16_ToU16(*src++); 72 *dst++ = SkPixel32ToPixel16_ToU16(*src++);
73 }
105 } 74 }
106 75
107 const uint16_t* SkColorTable::lock16BitCache() 76 const uint16_t* SkColorTable::lock16BitCache() {
108 { 77 if (this->isOpaque() && NULL == f16BitCache) {
109 if (fFlags & kColorsAreOpaque_Flag) 78 f16BitCache = (uint16_t*)sk_malloc_throw(fCount * sizeof(uint16_t));
110 { 79 build_16bitcache(f16BitCache, fColors, fCount);
111 if (f16BitCache == NULL) // build the cache
112 {
113 f16BitCache = (uint16_t*)sk_malloc_throw(fCount * sizeof(uint16_t));
114 build_16bitcache(f16BitCache, fColors, fCount);
115 }
116 }
117 else // our colors have alpha, so no cache
118 {
119 this->inval16BitCache();
120 if (f16BitCache)
121 {
122 sk_free(f16BitCache);
123 f16BitCache = NULL;
124 }
125 } 80 }
126 81
127 SkDEBUGCODE(f16BitCacheLockCount += 1); 82 SkDEBUGCODE(f16BitCacheLockCount += 1);
128 return f16BitCache; 83 return f16BitCache;
129 } 84 }
130 85
131 void SkColorTable::setIsOpaque(bool isOpaque) {
132 if (isOpaque) {
133 fFlags |= kColorsAreOpaque_Flag;
134 } else {
135 fFlags &= ~kColorsAreOpaque_Flag;
136 }
137 }
138
139 /////////////////////////////////////////////////////////////////////////////// 86 ///////////////////////////////////////////////////////////////////////////////
140 87
141 SkColorTable::SkColorTable(SkFlattenableReadBuffer& buffer) { 88 SkColorTable::SkColorTable(SkFlattenableReadBuffer& buffer) {
142 f16BitCache = NULL; 89 f16BitCache = NULL;
143 SkDEBUGCODE(fColorLockCount = 0;) 90 SkDEBUGCODE(fColorLockCount = 0;)
144 SkDEBUGCODE(f16BitCacheLockCount = 0;) 91 SkDEBUGCODE(f16BitCacheLockCount = 0;)
145 92
146 fFlags = buffer.readUInt(); 93 fAlphaType = SkToU8(buffer.readUInt());
147 fCount = buffer.getArrayCount(); 94 fCount = buffer.getArrayCount();
148 fColors = (SkPMColor*)sk_malloc_throw(fCount * sizeof(SkPMColor)); 95 fColors = (SkPMColor*)sk_malloc_throw(fCount * sizeof(SkPMColor));
149 SkDEBUGCODE(const uint32_t countRead =) buffer.readColorArray(fColors); 96 SkDEBUGCODE(const uint32_t countRead =) buffer.readColorArray(fColors);
150 #ifdef SK_DEBUG 97 #ifdef SK_DEBUG
151 SkASSERT((unsigned)fCount <= 256); 98 SkASSERT((unsigned)fCount <= 256);
152 SkASSERT(countRead == fCount); 99 SkASSERT(countRead == fCount);
153 #endif 100 #endif
154 } 101 }
155 102
156 void SkColorTable::flatten(SkFlattenableWriteBuffer& buffer) const { 103 void SkColorTable::flatten(SkFlattenableWriteBuffer& buffer) const {
157 buffer.writeUInt(fFlags); 104 buffer.writeUInt(fAlphaType);
158 buffer.writeColorArray(fColors, fCount); 105 buffer.writeColorArray(fColors, fCount);
159 } 106 }
OLDNEW
« no previous file with comments | « src/core/SkBitmapProcState_procs.h ('k') | src/core/SkProcSpriteBlitter.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698