Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #include "SkPaint.h" | 9 #include "SkPaint.h" |
| 10 #include "SkAnnotation.h" | 10 #include "SkAnnotation.h" |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 244 | 244 |
| 245 void SkPaint::setPaintOptionsAndroid(const SkPaintOptionsAndroid& options) { | 245 void SkPaint::setPaintOptionsAndroid(const SkPaintOptionsAndroid& options) { |
| 246 if (options != fPaintOptionsAndroid) { | 246 if (options != fPaintOptionsAndroid) { |
| 247 fPaintOptionsAndroid = options; | 247 fPaintOptionsAndroid = options; |
| 248 GEN_ID_INC; | 248 GEN_ID_INC; |
| 249 fDirtyBits |= kPaintOptionsAndroid_DirtyBit; | 249 fDirtyBits |= kPaintOptionsAndroid_DirtyBit; |
| 250 } | 250 } |
| 251 } | 251 } |
| 252 #endif | 252 #endif |
| 253 | 253 |
| 254 SkPaint::FilterLevel SkPaint::getFilterLevel() const { | |
| 255 int level = 0; | |
| 256 if (fFlags & kFilterBitmap_Flag) { | |
| 257 level |= 1; | |
| 258 } | |
| 259 if (fFlags & kHighQualityFilterBitmap_Flag) { | |
| 260 level |= 2; | |
| 261 } | |
| 262 return (FilterLevel)level; | |
| 263 } | |
| 264 | |
| 265 void SkPaint::setFilterLevel(FilterLevel level) { | 254 void SkPaint::setFilterLevel(FilterLevel level) { |
| 266 unsigned mask = kFilterBitmap_Flag | kHighQualityFilterBitmap_Flag; | 255 GEN_ID_INC_EVAL((unsigned) level != fFilterLevel); |
| 267 unsigned flags = 0; | 256 fFilterLevel = level; |
| 268 if (level & 1) { | 257 fDirtyBits |= kBitfields_DirtyBit; |
| 269 flags |= kFilterBitmap_Flag; | |
| 270 } | |
| 271 if (level & 2) { | |
| 272 flags |= kHighQualityFilterBitmap_Flag; | |
| 273 } | |
| 274 this->setFlags((fFlags & ~mask) | flags); | |
| 275 } | 258 } |
| 276 | 259 |
| 277 void SkPaint::setHinting(Hinting hintingLevel) { | 260 void SkPaint::setHinting(Hinting hintingLevel) { |
| 278 GEN_ID_INC_EVAL((unsigned) hintingLevel != fHinting); | 261 GEN_ID_INC_EVAL((unsigned) hintingLevel != fHinting); |
| 279 fHinting = hintingLevel; | 262 fHinting = hintingLevel; |
| 280 fDirtyBits |= kBitfields_DirtyBit; | 263 fDirtyBits |= kBitfields_DirtyBit; |
| 281 } | 264 } |
| 282 | 265 |
| 283 void SkPaint::setFlags(uint32_t flags) { | 266 void SkPaint::setFlags(uint32_t flags) { |
| 284 GEN_ID_INC_EVAL(fFlags != flags); | 267 GEN_ID_INC_EVAL(fFlags != flags); |
| (...skipping 1734 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2019 } | 2002 } |
| 2020 | 2003 |
| 2021 static uint32_t pack_4(unsigned a, unsigned b, unsigned c, unsigned d) { | 2004 static uint32_t pack_4(unsigned a, unsigned b, unsigned c, unsigned d) { |
| 2022 SkASSERT(a == (uint8_t)a); | 2005 SkASSERT(a == (uint8_t)a); |
| 2023 SkASSERT(b == (uint8_t)b); | 2006 SkASSERT(b == (uint8_t)b); |
| 2024 SkASSERT(c == (uint8_t)c); | 2007 SkASSERT(c == (uint8_t)c); |
| 2025 SkASSERT(d == (uint8_t)d); | 2008 SkASSERT(d == (uint8_t)d); |
| 2026 return (a << 24) | (b << 16) | (c << 8) | d; | 2009 return (a << 24) | (b << 16) | (c << 8) | d; |
| 2027 } | 2010 } |
| 2028 | 2011 |
| 2012 #ifdef SK_DEBUG | |
|
robertphillips
2014/04/15 17:46:15
ASSEERT?
reed1
2014/04/15 18:57:46
Done.
| |
| 2013 static void ASSEERT_FITS_IN(uint32_t value, int bitCount) { | |
| 2014 SkASSERT(bitCount > 0 && bitCount <= 32); | |
| 2015 uint32_t mask = ~0U; | |
| 2016 mask >>= (32 - bitCount); | |
| 2017 SkASSERT(0 == (value & ~mask)); | |
| 2018 } | |
| 2019 #else | |
| 2020 #define ASSEERT_FITS_IN(value, bitcount) | |
| 2021 #endif | |
| 2022 | |
| 2029 enum FlatFlags { | 2023 enum FlatFlags { |
| 2030 kHasTypeface_FlatFlag = 0x01, | 2024 kHasTypeface_FlatFlag = 0x01, |
| 2031 kHasEffects_FlatFlag = 0x02, | 2025 kHasEffects_FlatFlag = 0x02, |
| 2032 kHasNonDefaultPaintOptionsAndroid_FlatFlag = 0x04, | 2026 kHasNonDefaultPaintOptionsAndroid_FlatFlag = 0x04, |
| 2033 }; | 2027 }; |
| 2034 | 2028 |
| 2029 static uint32_t pack_paint_flags(unsigned flags, unsigned hint, unsigned align, | |
| 2030 unsigned filter, unsigned flatFlags) { | |
|
robertphillips
2014/04/15 17:46:15
All this math with raw numbers makes me nervous -
reed1
2014/04/15 18:57:46
Working on it. Hard to do everything and not make
| |
| 2031 ASSEERT_FITS_IN(flags, 16); | |
| 2032 ASSEERT_FITS_IN(hint, 2); | |
| 2033 ASSEERT_FITS_IN(align, 2); | |
| 2034 ASSEERT_FITS_IN(filter, 2); | |
| 2035 ASSEERT_FITS_IN(flatFlags, 32 - (16 + 2 + 2 + 2)); | |
| 2036 | |
| 2037 // left-align the fields of "known" size, and right-align the last (flatFlag s) so it can easly | |
| 2038 // add more bits in the future. | |
| 2039 return (flags << 16) | (hint << 14) | (align << 12) | (filter << 10) | flatF lags; | |
| 2040 } | |
| 2041 | |
| 2042 static unsigned unpack_paint_flags(SkPaint* paint, uint32_t packed) { | |
| 2043 paint->setFlags(packed >> 16); | |
| 2044 paint->setHinting((SkPaint::Hinting)((packed >> 14) & 0x3)); | |
| 2045 paint->setTextAlign((SkPaint::Align)((packed >> 12) & 0x3)); | |
| 2046 paint->setFilterLevel((SkPaint::FilterLevel)((packed >> 10) & 0x3)); | |
| 2047 return packed & 0x7; | |
| 2048 } | |
| 2049 | |
| 2050 static unsigned unpack_paint_flags_v22(SkPaint* paint, uint32_t packed) { | |
| 2051 enum { | |
| 2052 kFilterBitmap_Flag = 0x02, | |
| 2053 kHighQualityFilterBitmap_Flag = 0x4000, | |
| 2054 | |
| 2055 kAll_Flags = kFilterBitmap_Flag | kHighQualityFilterBitmap_Flag | |
| 2056 }; | |
| 2057 | |
| 2058 // previously flags:16, textAlign:8, flatFlags:8 | |
| 2059 // now flags:16, hinting:4, textAlign:4, flatFlags:8 | |
| 2060 unsigned flags = packed >> 16; | |
| 2061 int filter = 0; | |
| 2062 if (flags & kFilterBitmap_Flag) { | |
| 2063 filter |= 1; | |
| 2064 } | |
| 2065 if (flags & kHighQualityFilterBitmap_Flag) { | |
| 2066 filter |= 2; | |
| 2067 } | |
| 2068 paint->setFilterLevel((SkPaint::FilterLevel)filter); | |
| 2069 flags &= ~kAll_Flags; // remove these (now dead) bit flags | |
| 2070 | |
|
robertphillips
2014/04/15 17:46:15
flags?
reed1
2014/04/15 18:57:46
Done.
| |
| 2071 paint->setFlags(packed >> 16); | |
| 2072 | |
| 2073 // hinting added later. 0 in this nibble means use the default. | |
| 2074 uint32_t hinting = (packed >> 12) & 0xF; | |
| 2075 paint->setHinting(0 == hinting ? SkPaint::kNormal_Hinting : static_cast<SkPa int::Hinting>(hinting-1)); | |
| 2076 paint->setTextAlign(static_cast<SkPaint::Align>((packed >> 8) & 0xF)); | |
| 2077 return packed & 0x7; | |
| 2078 } | |
| 2079 | |
| 2035 // The size of a flat paint's POD fields | 2080 // The size of a flat paint's POD fields |
| 2036 static const uint32_t kPODPaintSize = 5 * sizeof(SkScalar) + | 2081 static const uint32_t kPODPaintSize = 5 * sizeof(SkScalar) + |
| 2037 1 * sizeof(SkColor) + | 2082 1 * sizeof(SkColor) + |
| 2038 1 * sizeof(uint16_t) + | 2083 1 * sizeof(uint16_t) + |
| 2039 6 * sizeof(uint8_t); | 2084 6 * sizeof(uint8_t); |
| 2040 | 2085 |
| 2041 /* To save space/time, we analyze the paint, and write a truncated version of | 2086 /* To save space/time, we analyze the paint, and write a truncated version of |
| 2042 it if there are not tricky elements like shaders, etc. | 2087 it if there are not tricky elements like shaders, etc. |
| 2043 */ | 2088 */ |
| 2044 void SkPaint::flatten(SkWriteBuffer& buffer) const { | 2089 void SkPaint::flatten(SkWriteBuffer& buffer) const { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 2065 | 2110 |
| 2066 SkASSERT(SkAlign4(kPODPaintSize) == kPODPaintSize); | 2111 SkASSERT(SkAlign4(kPODPaintSize) == kPODPaintSize); |
| 2067 uint32_t* ptr = buffer.reserve(kPODPaintSize); | 2112 uint32_t* ptr = buffer.reserve(kPODPaintSize); |
| 2068 | 2113 |
| 2069 ptr = write_scalar(ptr, this->getTextSize()); | 2114 ptr = write_scalar(ptr, this->getTextSize()); |
| 2070 ptr = write_scalar(ptr, this->getTextScaleX()); | 2115 ptr = write_scalar(ptr, this->getTextScaleX()); |
| 2071 ptr = write_scalar(ptr, this->getTextSkewX()); | 2116 ptr = write_scalar(ptr, this->getTextSkewX()); |
| 2072 ptr = write_scalar(ptr, this->getStrokeWidth()); | 2117 ptr = write_scalar(ptr, this->getStrokeWidth()); |
| 2073 ptr = write_scalar(ptr, this->getStrokeMiter()); | 2118 ptr = write_scalar(ptr, this->getStrokeMiter()); |
| 2074 *ptr++ = this->getColor(); | 2119 *ptr++ = this->getColor(); |
| 2075 // previously flags:16, textAlign:8, flatFlags:8 | 2120 |
| 2076 // now flags:16, hinting:4, textAlign:4, flatFlags:8 | 2121 *ptr++ = pack_paint_flags(this->getFlags(), this->getHinting(), this->getTex tAlign(), |
| 2077 *ptr++ = (this->getFlags() << 16) | | 2122 this->getFilterLevel(), flatFlags); |
| 2078 // hinting added later. 0 in this nibble means use the default. | |
| 2079 ((this->getHinting()+1) << 12) | | |
| 2080 (this->getTextAlign() << 8) | | |
| 2081 flatFlags; | |
| 2082 *ptr++ = pack_4(this->getStrokeCap(), this->getStrokeJoin(), | 2123 *ptr++ = pack_4(this->getStrokeCap(), this->getStrokeJoin(), |
| 2083 this->getStyle(), this->getTextEncoding()); | 2124 this->getStyle(), this->getTextEncoding()); |
| 2084 | 2125 |
| 2085 // now we're done with ptr and the (pre)reserved space. If we need to write | 2126 // now we're done with ptr and the (pre)reserved space. If we need to write |
| 2086 // additional fields, use the buffer directly | 2127 // additional fields, use the buffer directly |
| 2087 if (flatFlags & kHasTypeface_FlatFlag) { | 2128 if (flatFlags & kHasTypeface_FlatFlag) { |
| 2088 buffer.writeTypeface(this->getTypeface()); | 2129 buffer.writeTypeface(this->getTypeface()); |
| 2089 } | 2130 } |
| 2090 if (flatFlags & kHasEffects_FlatFlag) { | 2131 if (flatFlags & kHasEffects_FlatFlag) { |
| 2091 buffer.writeFlattenable(this->getPathEffect()); | 2132 buffer.writeFlattenable(this->getPathEffect()); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 2105 } | 2146 } |
| 2106 } | 2147 } |
| 2107 #ifdef SK_BUILD_FOR_ANDROID | 2148 #ifdef SK_BUILD_FOR_ANDROID |
| 2108 if (flatFlags & kHasNonDefaultPaintOptionsAndroid_FlatFlag) { | 2149 if (flatFlags & kHasNonDefaultPaintOptionsAndroid_FlatFlag) { |
| 2109 this->getPaintOptionsAndroid().flatten(buffer); | 2150 this->getPaintOptionsAndroid().flatten(buffer); |
| 2110 } | 2151 } |
| 2111 #endif | 2152 #endif |
| 2112 } | 2153 } |
| 2113 | 2154 |
| 2114 void SkPaint::unflatten(SkReadBuffer& buffer) { | 2155 void SkPaint::unflatten(SkReadBuffer& buffer) { |
| 2115 uint8_t flatFlags = 0; | |
| 2116 SkASSERT(SkAlign4(kPODPaintSize) == kPODPaintSize); | 2156 SkASSERT(SkAlign4(kPODPaintSize) == kPODPaintSize); |
| 2117 const void* podData = buffer.skip(kPODPaintSize); | 2157 const void* podData = buffer.skip(kPODPaintSize); |
| 2118 const uint32_t* pod = reinterpret_cast<const uint32_t*>(podData); | 2158 const uint32_t* pod = reinterpret_cast<const uint32_t*>(podData); |
| 2119 | 2159 |
| 2120 // the order we read must match the order we wrote in flatten() | 2160 // the order we read must match the order we wrote in flatten() |
| 2121 this->setTextSize(read_scalar(pod)); | 2161 this->setTextSize(read_scalar(pod)); |
| 2122 this->setTextScaleX(read_scalar(pod)); | 2162 this->setTextScaleX(read_scalar(pod)); |
| 2123 this->setTextSkewX(read_scalar(pod)); | 2163 this->setTextSkewX(read_scalar(pod)); |
| 2124 this->setStrokeWidth(read_scalar(pod)); | 2164 this->setStrokeWidth(read_scalar(pod)); |
| 2125 this->setStrokeMiter(read_scalar(pod)); | 2165 this->setStrokeMiter(read_scalar(pod)); |
| 2126 this->setColor(*pod++); | 2166 this->setColor(*pod++); |
| 2127 | 2167 |
| 2128 // previously flags:16, textAlign:8, flatFlags:8 | 2168 const int picVer = buffer.pictureVersion(); |
| 2129 // now flags:16, hinting:4, textAlign:4, flatFlags:8 | 2169 unsigned flatFlags = 0; |
| 2170 if (picVer > 0 && picVer <= 22) { | |
| 2171 flatFlags = unpack_paint_flags_v22(this, *pod++); | |
| 2172 } else { | |
| 2173 flatFlags = unpack_paint_flags(this, *pod++); | |
| 2174 } | |
| 2175 | |
| 2130 uint32_t tmp = *pod++; | 2176 uint32_t tmp = *pod++; |
| 2131 this->setFlags(tmp >> 16); | |
| 2132 | |
| 2133 // hinting added later. 0 in this nibble means use the default. | |
| 2134 uint32_t hinting = (tmp >> 12) & 0xF; | |
| 2135 this->setHinting(0 == hinting ? kNormal_Hinting : static_cast<Hinting>(hinti ng-1)); | |
| 2136 | |
| 2137 this->setTextAlign(static_cast<Align>((tmp >> 8) & 0xF)); | |
| 2138 | |
| 2139 flatFlags = tmp & 0xFF; | |
| 2140 | |
| 2141 tmp = *pod++; | |
| 2142 this->setStrokeCap(static_cast<Cap>((tmp >> 24) & 0xFF)); | 2177 this->setStrokeCap(static_cast<Cap>((tmp >> 24) & 0xFF)); |
| 2143 this->setStrokeJoin(static_cast<Join>((tmp >> 16) & 0xFF)); | 2178 this->setStrokeJoin(static_cast<Join>((tmp >> 16) & 0xFF)); |
| 2144 this->setStyle(static_cast<Style>((tmp >> 8) & 0xFF)); | 2179 this->setStyle(static_cast<Style>((tmp >> 8) & 0xFF)); |
| 2145 this->setTextEncoding(static_cast<TextEncoding>((tmp >> 0) & 0xFF)); | 2180 this->setTextEncoding(static_cast<TextEncoding>((tmp >> 0) & 0xFF)); |
| 2146 | 2181 |
| 2147 if (flatFlags & kHasTypeface_FlatFlag) { | 2182 if (flatFlags & kHasTypeface_FlatFlag) { |
| 2148 this->setTypeface(buffer.readTypeface()); | 2183 this->setTypeface(buffer.readTypeface()); |
| 2149 } else { | 2184 } else { |
| 2150 this->setTypeface(NULL); | 2185 this->setTypeface(NULL); |
| 2151 } | 2186 } |
| (...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2684 } | 2719 } |
| 2685 #ifdef SK_BUILD_FOR_ANDROID | 2720 #ifdef SK_BUILD_FOR_ANDROID |
| 2686 if (dirty & kPaintOptionsAndroid_DirtyBit) { | 2721 if (dirty & kPaintOptionsAndroid_DirtyBit) { |
| 2687 SkPaintOptionsAndroid options; | 2722 SkPaintOptionsAndroid options; |
| 2688 options.unflatten(buffer); | 2723 options.unflatten(buffer); |
| 2689 paint->setPaintOptionsAndroid(options); | 2724 paint->setPaintOptionsAndroid(options); |
| 2690 } | 2725 } |
| 2691 #endif | 2726 #endif |
| 2692 SkASSERT(dirty == paint->fDirtyBits); | 2727 SkASSERT(dirty == paint->fDirtyBits); |
| 2693 } | 2728 } |
| OLD | NEW |