OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkMipMap.h" | 8 #include "SkMipMap.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 int64_t size = sk_64_mul(levelCount + 1, sizeof(Level)) + pixelSize; | 116 int64_t size = sk_64_mul(levelCount + 1, sizeof(Level)) + pixelSize; |
117 if (!sk_64_isS32(size)) { | 117 if (!sk_64_isS32(size)) { |
118 return NULL; | 118 return NULL; |
119 } | 119 } |
120 return (Level*)sk_malloc_throw(sk_64_asS32(size)); | 120 return (Level*)sk_malloc_throw(sk_64_asS32(size)); |
121 } | 121 } |
122 | 122 |
123 SkMipMap* SkMipMap::Build(const SkBitmap& src) { | 123 SkMipMap* SkMipMap::Build(const SkBitmap& src) { |
124 void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src); | 124 void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src); |
125 | 125 |
126 const SkBitmap::Config config = src.config(); | 126 const SkColorType ct = src.colorType(); |
127 switch (config) { | 127 switch (ct) { |
128 case SkBitmap::kARGB_8888_Config: | 128 case kRGBA_8888_SkColorType: |
| 129 case kBGRA_8888_SkColorType: |
129 proc = downsampleby2_proc32; | 130 proc = downsampleby2_proc32; |
130 break; | 131 break; |
131 case SkBitmap::kRGB_565_Config: | 132 case kRGB_565_SkColorType: |
132 proc = downsampleby2_proc16; | 133 proc = downsampleby2_proc16; |
133 break; | 134 break; |
134 case SkBitmap::kARGB_4444_Config: | 135 case kARGB_4444_SkColorType: |
135 proc = downsampleby2_proc4444; | 136 proc = downsampleby2_proc4444; |
136 break; | 137 break; |
137 case SkBitmap::kIndex8_Config: | |
138 case SkBitmap::kA8_Config: | |
139 default: | 138 default: |
140 return NULL; // don't build mipmaps for these configs | 139 return NULL; // don't build mipmaps for these configs |
141 } | 140 } |
142 | 141 |
143 SkAutoLockPixels alp(src); | 142 SkAutoLockPixels alp(src); |
144 if (!src.readyToDraw()) { | 143 if (!src.readyToDraw()) { |
145 return NULL; | 144 return NULL; |
146 } | 145 } |
147 | 146 |
148 // whip through our loop to compute the exact size needed | 147 // whip through our loop to compute the exact size needed |
149 size_t size = 0; | 148 size_t size = 0; |
150 int countLevels = 0; | 149 int countLevels = 0; |
151 { | 150 { |
152 int width = src.width(); | 151 int width = src.width(); |
153 int height = src.height(); | 152 int height = src.height(); |
154 for (;;) { | 153 for (;;) { |
155 width >>= 1; | 154 width >>= 1; |
156 height >>= 1; | 155 height >>= 1; |
157 if (0 == width || 0 == height) { | 156 if (0 == width || 0 == height) { |
158 break; | 157 break; |
159 } | 158 } |
160 size += SkBitmap::ComputeRowBytes(config, width) * height; | 159 size += SkColorTypeMinRowBytes(ct, width) * height; |
161 countLevels += 1; | 160 countLevels += 1; |
162 } | 161 } |
163 } | 162 } |
164 if (0 == countLevels) { | 163 if (0 == countLevels) { |
165 return NULL; | 164 return NULL; |
166 } | 165 } |
167 | 166 |
168 Level* levels = SkMipMap::AllocLevels(countLevels, size); | 167 Level* levels = SkMipMap::AllocLevels(countLevels, size); |
169 if (NULL == levels) { | 168 if (NULL == levels) { |
170 return NULL; | 169 return NULL; |
171 } | 170 } |
172 | 171 |
173 uint8_t* baseAddr = (uint8_t*)&levels[countLevels]; | 172 uint8_t* baseAddr = (uint8_t*)&levels[countLevels]; |
174 uint8_t* addr = baseAddr; | 173 uint8_t* addr = baseAddr; |
175 int width = src.width(); | 174 int width = src.width(); |
176 int height = src.height(); | 175 int height = src.height(); |
177 uint32_t rowBytes; | 176 uint32_t rowBytes; |
178 SkBitmap srcBM(src); | 177 SkBitmap srcBM(src); |
179 | 178 |
180 for (int i = 0; i < countLevels; ++i) { | 179 for (int i = 0; i < countLevels; ++i) { |
181 width >>= 1; | 180 width >>= 1; |
182 height >>= 1; | 181 height >>= 1; |
183 rowBytes = SkToU32(SkBitmap::ComputeRowBytes(config, width)); | 182 rowBytes = SkToU32(SkColorTypeMinRowBytes(ct, width)); |
184 | 183 |
185 levels[i].fPixels = addr; | 184 levels[i].fPixels = addr; |
186 levels[i].fWidth = width; | 185 levels[i].fWidth = width; |
187 levels[i].fHeight = height; | 186 levels[i].fHeight = height; |
188 levels[i].fRowBytes = rowBytes; | 187 levels[i].fRowBytes = rowBytes; |
189 levels[i].fScale = (float)width / src.width(); | 188 levels[i].fScale = (float)width / src.width(); |
190 | 189 |
| 190 SkImageInfo info = SkImageInfo::Make(width, height, ct, |
| 191 srcBM.alphaType()); |
191 SkBitmap dstBM; | 192 SkBitmap dstBM; |
192 dstBM.setConfig(config, width, height, rowBytes); | 193 dstBM.installPixels(info, addr, rowBytes, NULL, NULL); |
193 dstBM.setPixels(addr); | 194 dstBM.lockPixels(); |
194 | 195 |
195 srcBM.lockPixels(); | 196 srcBM.lockPixels(); |
196 for (int y = 0; y < height; y++) { | 197 for (int y = 0; y < height; y++) { |
197 for (int x = 0; x < width; x++) { | 198 for (int x = 0; x < width; x++) { |
198 proc(&dstBM, x, y, srcBM); | 199 proc(&dstBM, x, y, srcBM); |
199 } | 200 } |
200 } | 201 } |
201 srcBM.unlockPixels(); | 202 srcBM.unlockPixels(); |
202 | 203 |
203 srcBM = dstBM; | 204 srcBM = dstBM; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 } | 248 } |
248 | 249 |
249 if (level > fCount) { | 250 if (level > fCount) { |
250 level = fCount; | 251 level = fCount; |
251 } | 252 } |
252 if (levelPtr) { | 253 if (levelPtr) { |
253 *levelPtr = fLevels[level - 1]; | 254 *levelPtr = fLevels[level - 1]; |
254 } | 255 } |
255 return true; | 256 return true; |
256 } | 257 } |
OLD | NEW |