OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "SkCodec.h" | 8 #include "SkCodec.h" |
9 #include "SkCodecPriv.h" | 9 #include "SkCodecPriv.h" |
10 #include "SkSampler.h" | 10 #include "SkSampler.h" |
11 #include "SkUtils.h" | 11 #include "SkUtils.h" |
12 | 12 |
13 void SkSampler::Fill(const SkImageInfo& info, void* dst, size_t rowBytes, | 13 void SkSampler::Fill(const SkImageInfo& info, void* dst, size_t rowBytes, |
14 uint32_t colorOrIndex, SkCodec::ZeroInitialized zeroInit) { | 14 uint64_t colorOrIndex, SkCodec::ZeroInitialized zeroInit) { |
15 SkASSERT(dst != nullptr); | 15 SkASSERT(dst != nullptr); |
16 | 16 |
17 // Calculate bytes to fill. We use getSafeSize since the last row may not b e padded. | 17 // Calculate bytes to fill. We use getSafeSize since the last row may not b e padded. |
18 const size_t bytesToFill = info.getSafeSize(rowBytes); | 18 const size_t bytesToFill = info.getSafeSize(rowBytes); |
19 const int width = info.width(); | 19 const int width = info.width(); |
20 const int numRows = info.height(); | 20 const int numRows = info.height(); |
21 | 21 |
22 // Use the proper memset routine to fill the remaining bytes | 22 // Use the proper memset routine to fill the remaining bytes |
23 switch (info.colorType()) { | 23 switch (info.colorType()) { |
24 case kRGBA_8888_SkColorType: | 24 case kRGBA_8888_SkColorType: |
25 case kBGRA_8888_SkColorType: { | 25 case kBGRA_8888_SkColorType: { |
26 // If memory is zero initialized, we may not need to fill | 26 // If memory is zero initialized, we may not need to fill |
27 uint32_t color = colorOrIndex; | 27 uint32_t color = colorOrIndex; |
28 if (SkCodec::kYes_ZeroInitialized == zeroInit && 0 == color) { | 28 if (SkCodec::kYes_ZeroInitialized == zeroInit && 0 == color) { |
29 return; | 29 return; |
30 } | 30 } |
31 | 31 |
32 // We must fill row by row in the case of unaligned row bytes | 32 uint32_t* dstRow = (uint32_t*) dst; |
33 if (SkIsAlign4((size_t) dst) && SkIsAlign4(rowBytes)) { | 33 for (int row = 0; row < numRows; row++) { |
34 sk_memset32((uint32_t*) dst, color, | 34 sk_memset32((uint32_t*) dstRow, color, width); |
msarett
2016/09/13 14:30:54
There's not a big performance difference between c
| |
35 (uint32_t) bytesToFill / sizeof(SkPMColor)); | 35 dstRow = SkTAddOffset<uint32_t>(dstRow, rowBytes); |
36 } else { | |
37 // We must fill row by row in the case of unaligned row bytes. This is an | |
38 // unlikely, slow case. | |
39 SkCodecPrintf("Warning: Strange number of row bytes, fill will b e slow.\n"); | |
40 uint32_t* dstRow = (uint32_t*) dst; | |
41 for (int row = 0; row < numRows; row++) { | |
42 for (int col = 0; col < width; col++) { | |
43 dstRow[col] = color; | |
44 } | |
45 dstRow = SkTAddOffset<uint32_t>(dstRow, rowBytes); | |
46 } | |
47 } | 36 } |
48 break; | 37 break; |
49 } | 38 } |
50 case kRGB_565_SkColorType: { | 39 case kRGB_565_SkColorType: { |
51 // If the destination is k565, the caller passes in a 16-bit color. | 40 // If the destination is k565, the caller passes in a 16-bit color. |
52 // We will not assert that the high bits of colorOrIndex must be zer oed. | 41 // We will not assert that the high bits of colorOrIndex must be zer oed. |
53 // This allows us to take advantage of the fact that the low 16 bits of an | 42 // This allows us to take advantage of the fact that the low 16 bits of an |
54 // SKPMColor may be a valid a 565 color. For example, the low 16 | 43 // SKPMColor may be a valid a 565 color. For example, the low 16 |
55 // bits of SK_ColorBLACK are identical to the 565 representation | 44 // bits of SK_ColorBLACK are identical to the 565 representation |
56 // for black. | 45 // for black. |
57 | 46 |
58 // If memory is zero initialized, we may not need to fill | 47 // If memory is zero initialized, we may not need to fill |
59 uint16_t color = (uint16_t) colorOrIndex; | 48 uint16_t color = (uint16_t) colorOrIndex; |
60 if (SkCodec::kYes_ZeroInitialized == zeroInit && 0 == color) { | 49 if (SkCodec::kYes_ZeroInitialized == zeroInit && 0 == color) { |
61 return; | 50 return; |
62 } | 51 } |
63 | 52 |
64 if (SkIsAlign2((size_t) dst) && SkIsAlign2(rowBytes)) { | 53 uint16_t* dstRow = (uint16_t*) dst; |
65 sk_memset16((uint16_t*) dst, color, (uint32_t) bytesToFill / siz eof(uint16_t)); | 54 for (int row = 0; row < numRows; row++) { |
66 } else { | 55 sk_memset16((uint16_t*) dstRow, color, width); |
67 // We must fill row by row in the case of unaligned row bytes. This is an | 56 dstRow = SkTAddOffset<uint16_t>(dstRow, rowBytes); |
68 // unlikely, slow case. | |
69 SkCodecPrintf("Warning: Strange number of row bytes, fill will b e slow.\n"); | |
70 uint16_t* dstRow = (uint16_t*) dst; | |
71 for (int row = 0; row < numRows; row++) { | |
72 for (int col = 0; col < width; col++) { | |
73 dstRow[col] = color; | |
74 } | |
75 dstRow = SkTAddOffset<uint16_t>(dstRow, rowBytes); | |
76 } | |
77 } | 57 } |
78 break; | 58 break; |
79 } | 59 } |
80 case kIndex_8_SkColorType: | 60 case kIndex_8_SkColorType: |
81 // On an index destination color type, always assume the input is an index. | 61 // On an index destination color type, always assume the input is an index. |
82 // Fall through | 62 // Fall through |
83 case kGray_8_SkColorType: | 63 case kGray_8_SkColorType: |
84 // If the destination is kGray, the caller passes in an 8-bit color. | 64 // If the destination is kGray, the caller passes in an 8-bit color. |
85 // We will not assert that the high bits of colorOrIndex must be zer oed. | 65 // We will not assert that the high bits of colorOrIndex must be zer oed. |
86 // This allows us to take advantage of the fact that the low 8 bits of an | 66 // This allows us to take advantage of the fact that the low 8 bits of an |
87 // SKPMColor may be a valid a grayscale color. For example, the low 8 | 67 // SKPMColor may be a valid a grayscale color. For example, the low 8 |
88 // bits of SK_ColorBLACK are identical to the grayscale representati on | 68 // bits of SK_ColorBLACK are identical to the grayscale representati on |
89 // for black. | 69 // for black. |
90 | 70 |
91 // If memory is zero initialized, we may not need to fill | 71 // If memory is zero initialized, we may not need to fill |
92 if (SkCodec::kYes_ZeroInitialized == zeroInit && 0 == (uint8_t) colo rOrIndex) { | 72 if (SkCodec::kYes_ZeroInitialized == zeroInit && 0 == (uint8_t) colo rOrIndex) { |
93 return; | 73 return; |
94 } | 74 } |
95 | 75 |
96 memset(dst, (uint8_t) colorOrIndex, bytesToFill); | 76 memset(dst, (uint8_t) colorOrIndex, bytesToFill); |
97 break; | 77 break; |
78 case kRGBA_F16_SkColorType: { | |
79 uint64_t color = colorOrIndex; | |
80 if (SkCodec::kYes_ZeroInitialized == zeroInit && 0 == color) { | |
81 return; | |
82 } | |
83 | |
84 uint64_t* dstRow = (uint64_t*) dst; | |
85 for (int row = 0; row < numRows; row++) { | |
86 sk_memset64((uint64_t*) dstRow, color, width); | |
87 dstRow = SkTAddOffset<uint64_t>(dstRow, rowBytes); | |
88 } | |
89 break; | |
90 } | |
98 default: | 91 default: |
99 SkCodecPrintf("Error: Unsupported dst color type for fill(). Doing nothing.\n"); | 92 SkCodecPrintf("Error: Unsupported dst color type for fill(). Doing nothing.\n"); |
100 SkASSERT(false); | 93 SkASSERT(false); |
101 break; | 94 break; |
102 } | 95 } |
103 } | 96 } |
OLD | NEW |