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

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

Issue 134163010: Refactor read and write buffers. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: original write flags were fine Created 6 years, 10 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
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 #include "SkPaint.h" 9 #include "SkPaint.h"
10 #include "SkAnnotation.h" 10 #include "SkAnnotation.h"
11 #include "SkAutoKern.h" 11 #include "SkAutoKern.h"
12 #include "SkColorFilter.h" 12 #include "SkColorFilter.h"
13 #include "SkData.h" 13 #include "SkData.h"
14 #include "SkDeviceProperties.h" 14 #include "SkDeviceProperties.h"
15 #include "SkFontDescriptor.h" 15 #include "SkFontDescriptor.h"
16 #include "SkFontHost.h" 16 #include "SkFontHost.h"
17 #include "SkGlyphCache.h" 17 #include "SkGlyphCache.h"
18 #include "SkImageFilter.h" 18 #include "SkImageFilter.h"
19 #include "SkMaskFilter.h" 19 #include "SkMaskFilter.h"
20 #include "SkMaskGamma.h" 20 #include "SkMaskGamma.h"
21 #include "SkOrderedReadBuffer.h" 21 #include "SkReadBuffer.h"
22 #include "SkOrderedWriteBuffer.h" 22 #include "SkWriteBuffer.h"
23 #include "SkPaintDefaults.h" 23 #include "SkPaintDefaults.h"
24 #include "SkPaintOptionsAndroid.h" 24 #include "SkPaintOptionsAndroid.h"
25 #include "SkPathEffect.h" 25 #include "SkPathEffect.h"
26 #include "SkRasterizer.h" 26 #include "SkRasterizer.h"
27 #include "SkScalar.h" 27 #include "SkScalar.h"
28 #include "SkScalerContext.h" 28 #include "SkScalerContext.h"
29 #include "SkShader.h" 29 #include "SkShader.h"
30 #include "SkStringUtils.h" 30 #include "SkStringUtils.h"
31 #include "SkStroke.h" 31 #include "SkStroke.h"
32 #include "SkTextFormatParams.h" 32 #include "SkTextFormatParams.h"
(...skipping 1434 matching lines...) Expand 10 before | Expand all | Expand 10 after
1467 matrix.postTranslate(pos[i].fX - prevPos.fX, pos[i].fY - prevPos.fY); 1467 matrix.postTranslate(pos[i].fX - prevPos.fX, pos[i].fY - prevPos.fY);
1468 if (iterPath) { 1468 if (iterPath) {
1469 path->addPath(*iterPath, matrix); 1469 path->addPath(*iterPath, matrix);
1470 } 1470 }
1471 prevPos = pos[i]; 1471 prevPos = pos[i];
1472 i++; 1472 i++;
1473 } 1473 }
1474 } 1474 }
1475 1475
1476 static void add_flattenable(SkDescriptor* desc, uint32_t tag, 1476 static void add_flattenable(SkDescriptor* desc, uint32_t tag,
1477 SkOrderedWriteBuffer* buffer) { 1477 SkWriteBuffer* buffer) {
1478 buffer->writeToMemory(desc->addEntry(tag, buffer->size(), NULL)); 1478 buffer->writeToMemory(desc->addEntry(tag, buffer->bytesWritten(), NULL));
1479 } 1479 }
1480 1480
1481 // SkFontHost can override this choice in FilterRec() 1481 // SkFontHost can override this choice in FilterRec()
1482 static SkMask::Format computeMaskFormat(const SkPaint& paint) { 1482 static SkMask::Format computeMaskFormat(const SkPaint& paint) {
1483 uint32_t flags = paint.getFlags(); 1483 uint32_t flags = paint.getFlags();
1484 1484
1485 // Antialiasing being disabled trumps all other settings. 1485 // Antialiasing being disabled trumps all other settings.
1486 if (!(flags & SkPaint::kAntiAlias_Flag)) { 1486 if (!(flags & SkPaint::kAntiAlias_Flag)) {
1487 return SkMask::kBW_Format; 1487 return SkMask::kBW_Format;
1488 } 1488 }
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
1808 if (ignoreGamma) { 1808 if (ignoreGamma) {
1809 rec.setLuminanceColor(0); 1809 rec.setLuminanceColor(0);
1810 } 1810 }
1811 1811
1812 size_t descSize = sizeof(rec); 1812 size_t descSize = sizeof(rec);
1813 int entryCount = 1; 1813 int entryCount = 1;
1814 SkPathEffect* pe = this->getPathEffect(); 1814 SkPathEffect* pe = this->getPathEffect();
1815 SkMaskFilter* mf = this->getMaskFilter(); 1815 SkMaskFilter* mf = this->getMaskFilter();
1816 SkRasterizer* ra = this->getRasterizer(); 1816 SkRasterizer* ra = this->getRasterizer();
1817 1817
1818 SkOrderedWriteBuffer peBuffer, mfBuffer, raBuffer; 1818 SkWriteBuffer peBuffer, mfBuffer, raBuffer;
1819 1819
1820 if (pe) { 1820 if (pe) {
1821 peBuffer.writeFlattenable(pe); 1821 peBuffer.writeFlattenable(pe);
1822 descSize += peBuffer.size(); 1822 descSize += peBuffer.bytesWritten();
1823 entryCount += 1; 1823 entryCount += 1;
1824 rec.fMaskFormat = SkMask::kA8_Format; // force antialiasing when we do the scan conversion 1824 rec.fMaskFormat = SkMask::kA8_Format; // force antialiasing when we do the scan conversion
1825 // seems like we could support kLCD as well at this point... 1825 // seems like we could support kLCD as well at this point...
1826 } 1826 }
1827 if (mf) { 1827 if (mf) {
1828 mfBuffer.writeFlattenable(mf); 1828 mfBuffer.writeFlattenable(mf);
1829 descSize += mfBuffer.size(); 1829 descSize += mfBuffer.bytesWritten();
1830 entryCount += 1; 1830 entryCount += 1;
1831 rec.fMaskFormat = SkMask::kA8_Format; // force antialiasing with maskf ilters 1831 rec.fMaskFormat = SkMask::kA8_Format; // force antialiasing with maskf ilters
1832 /* Pre-blend is not currently applied to filtered text. 1832 /* Pre-blend is not currently applied to filtered text.
1833 The primary filter is blur, for which contrast makes no sense, 1833 The primary filter is blur, for which contrast makes no sense,
1834 and for which the destination guess error is more visible. 1834 and for which the destination guess error is more visible.
1835 Also, all existing users of blur have calibrated for linear. */ 1835 Also, all existing users of blur have calibrated for linear. */
1836 rec.ignorePreBlend(); 1836 rec.ignorePreBlend();
1837 } 1837 }
1838 if (ra) { 1838 if (ra) {
1839 raBuffer.writeFlattenable(ra); 1839 raBuffer.writeFlattenable(ra);
1840 descSize += raBuffer.size(); 1840 descSize += raBuffer.bytesWritten();
1841 entryCount += 1; 1841 entryCount += 1;
1842 rec.fMaskFormat = SkMask::kA8_Format; // force antialiasing when we do the scan conversion 1842 rec.fMaskFormat = SkMask::kA8_Format; // force antialiasing when we do the scan conversion
1843 } 1843 }
1844 1844
1845 #ifdef SK_BUILD_FOR_ANDROID 1845 #ifdef SK_BUILD_FOR_ANDROID
1846 SkOrderedWriteBuffer androidBuffer; 1846 SkWriteBuffer androidBuffer;
1847 fPaintOptionsAndroid.flatten(androidBuffer); 1847 fPaintOptionsAndroid.flatten(androidBuffer);
1848 descSize += androidBuffer.size(); 1848 descSize += androidBuffer.bytesWritten();
1849 entryCount += 1; 1849 entryCount += 1;
1850 #endif 1850 #endif
1851 1851
1852 /////////////////////////////////////////////////////////////////////////// 1852 ///////////////////////////////////////////////////////////////////////////
1853 // Now that we're done tweaking the rec, call the PostMakeRec cleanup 1853 // Now that we're done tweaking the rec, call the PostMakeRec cleanup
1854 SkScalerContext::PostMakeRec(*this, &rec); 1854 SkScalerContext::PostMakeRec(*this, &rec);
1855 1855
1856 descSize += SkDescriptor::ComputeOverhead(entryCount); 1856 descSize += SkDescriptor::ComputeOverhead(entryCount);
1857 1857
1858 SkAutoDescriptor ad(descSize); 1858 SkAutoDescriptor ad(descSize);
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
1992 1992
1993 // The size of a flat paint's POD fields 1993 // The size of a flat paint's POD fields
1994 static const uint32_t kPODPaintSize = 5 * sizeof(SkScalar) + 1994 static const uint32_t kPODPaintSize = 5 * sizeof(SkScalar) +
1995 1 * sizeof(SkColor) + 1995 1 * sizeof(SkColor) +
1996 1 * sizeof(uint16_t) + 1996 1 * sizeof(uint16_t) +
1997 6 * sizeof(uint8_t); 1997 6 * sizeof(uint8_t);
1998 1998
1999 /* To save space/time, we analyze the paint, and write a truncated version of 1999 /* To save space/time, we analyze the paint, and write a truncated version of
2000 it if there are not tricky elements like shaders, etc. 2000 it if there are not tricky elements like shaders, etc.
2001 */ 2001 */
2002 void SkPaint::flatten(SkFlattenableWriteBuffer& buffer) const { 2002 void SkPaint::flatten(SkWriteBuffer& buffer) const {
2003 uint8_t flatFlags = 0; 2003 uint8_t flatFlags = 0;
2004 if (this->getTypeface()) { 2004 if (this->getTypeface()) {
2005 flatFlags |= kHasTypeface_FlatFlag; 2005 flatFlags |= kHasTypeface_FlatFlag;
2006 } 2006 }
2007 if (asint(this->getPathEffect()) | 2007 if (asint(this->getPathEffect()) |
2008 asint(this->getShader()) | 2008 asint(this->getShader()) |
2009 asint(this->getXfermode()) | 2009 asint(this->getXfermode()) |
2010 asint(this->getMaskFilter()) | 2010 asint(this->getMaskFilter()) |
2011 asint(this->getColorFilter()) | 2011 asint(this->getColorFilter()) |
2012 asint(this->getRasterizer()) | 2012 asint(this->getRasterizer()) |
2013 asint(this->getLooper()) | 2013 asint(this->getLooper()) |
2014 asint(this->getAnnotation()) | 2014 asint(this->getAnnotation()) |
2015 asint(this->getImageFilter())) { 2015 asint(this->getImageFilter())) {
2016 flatFlags |= kHasEffects_FlatFlag; 2016 flatFlags |= kHasEffects_FlatFlag;
2017 } 2017 }
2018 #ifdef SK_BUILD_FOR_ANDROID 2018 #ifdef SK_BUILD_FOR_ANDROID
2019 if (this->getPaintOptionsAndroid() != SkPaintOptionsAndroid()) { 2019 if (this->getPaintOptionsAndroid() != SkPaintOptionsAndroid()) {
2020 flatFlags |= kHasNonDefaultPaintOptionsAndroid_FlatFlag; 2020 flatFlags |= kHasNonDefaultPaintOptionsAndroid_FlatFlag;
2021 } 2021 }
2022 #endif 2022 #endif
2023 2023
2024 if (buffer.isOrderedBinaryBuffer()) { 2024 SkASSERT(SkAlign4(kPODPaintSize) == kPODPaintSize);
2025 SkASSERT(SkAlign4(kPODPaintSize) == kPODPaintSize); 2025 uint32_t* ptr = buffer.reserve(kPODPaintSize);
2026 uint32_t* ptr = buffer.getOrderedBinaryBuffer()->reserve(kPODPaintSize);
2027 2026
2028 ptr = write_scalar(ptr, this->getTextSize()); 2027 ptr = write_scalar(ptr, this->getTextSize());
2029 ptr = write_scalar(ptr, this->getTextScaleX()); 2028 ptr = write_scalar(ptr, this->getTextScaleX());
2030 ptr = write_scalar(ptr, this->getTextSkewX()); 2029 ptr = write_scalar(ptr, this->getTextSkewX());
2031 ptr = write_scalar(ptr, this->getStrokeWidth()); 2030 ptr = write_scalar(ptr, this->getStrokeWidth());
2032 ptr = write_scalar(ptr, this->getStrokeMiter()); 2031 ptr = write_scalar(ptr, this->getStrokeMiter());
2033 *ptr++ = this->getColor(); 2032 *ptr++ = this->getColor();
2034 // previously flags:16, textAlign:8, flatFlags:8 2033 // previously flags:16, textAlign:8, flatFlags:8
2035 // now flags:16, hinting:4, textAlign:4, flatFlags:8 2034 // now flags:16, hinting:4, textAlign:4, flatFlags:8
2036 *ptr++ = (this->getFlags() << 16) | 2035 *ptr++ = (this->getFlags() << 16) |
2037 // hinting added later. 0 in this nibble means use the default. 2036 // hinting added later. 0 in this nibble means use the default.
2038 ((this->getHinting()+1) << 12) | 2037 ((this->getHinting()+1) << 12) |
2039 (this->getTextAlign() << 8) | 2038 (this->getTextAlign() << 8) |
2040 flatFlags; 2039 flatFlags;
2041 *ptr++ = pack_4(this->getStrokeCap(), this->getStrokeJoin(), 2040 *ptr++ = pack_4(this->getStrokeCap(), this->getStrokeJoin(),
2042 this->getStyle(), this->getTextEncoding()); 2041 this->getStyle(), this->getTextEncoding());
2043 } else {
2044 buffer.writeScalar(fTextSize);
2045 buffer.writeScalar(fTextScaleX);
2046 buffer.writeScalar(fTextSkewX);
2047 buffer.writeScalar(fWidth);
2048 buffer.writeScalar(fMiterLimit);
2049 buffer.writeColor(fColor);
2050 buffer.writeUInt(fFlags);
2051 buffer.writeUInt(fHinting);
2052 buffer.writeUInt(fTextAlign);
2053 buffer.writeUInt(flatFlags);
2054
2055 buffer.writeUInt(fCapType);
2056 buffer.writeUInt(fJoinType);
2057 buffer.writeUInt(fStyle);
2058 buffer.writeUInt(fTextEncoding);
2059 }
2060 2042
2061 // now we're done with ptr and the (pre)reserved space. If we need to write 2043 // now we're done with ptr and the (pre)reserved space. If we need to write
2062 // additional fields, use the buffer directly 2044 // additional fields, use the buffer directly
2063 if (flatFlags & kHasTypeface_FlatFlag) { 2045 if (flatFlags & kHasTypeface_FlatFlag) {
2064 buffer.writeTypeface(this->getTypeface()); 2046 buffer.writeTypeface(this->getTypeface());
2065 } 2047 }
2066 if (flatFlags & kHasEffects_FlatFlag) { 2048 if (flatFlags & kHasEffects_FlatFlag) {
2067 buffer.writeFlattenable(this->getPathEffect()); 2049 buffer.writeFlattenable(this->getPathEffect());
2068 buffer.writeFlattenable(this->getShader()); 2050 buffer.writeFlattenable(this->getShader());
2069 buffer.writeFlattenable(this->getXfermode()); 2051 buffer.writeFlattenable(this->getXfermode());
(...skipping 10 matching lines...) Expand all
2080 buffer.writeBool(false); 2062 buffer.writeBool(false);
2081 } 2063 }
2082 } 2064 }
2083 #ifdef SK_BUILD_FOR_ANDROID 2065 #ifdef SK_BUILD_FOR_ANDROID
2084 if (flatFlags & kHasNonDefaultPaintOptionsAndroid_FlatFlag) { 2066 if (flatFlags & kHasNonDefaultPaintOptionsAndroid_FlatFlag) {
2085 this->getPaintOptionsAndroid().flatten(buffer); 2067 this->getPaintOptionsAndroid().flatten(buffer);
2086 } 2068 }
2087 #endif 2069 #endif
2088 } 2070 }
2089 2071
2090 void SkPaint::unflatten(SkFlattenableReadBuffer& buffer) { 2072 void SkPaint::unflatten(SkReadBuffer& buffer) {
2091 uint8_t flatFlags = 0; 2073 uint8_t flatFlags = 0;
2092 if (buffer.isOrderedBinaryBuffer()) { 2074 SkASSERT(SkAlign4(kPODPaintSize) == kPODPaintSize);
2093 SkASSERT(SkAlign4(kPODPaintSize) == kPODPaintSize); 2075 const void* podData = buffer.skip(kPODPaintSize);
2094 const void* podData = buffer.getOrderedBinaryBuffer()->skip(kPODPaintSiz e); 2076 const uint32_t* pod = reinterpret_cast<const uint32_t*>(podData);
2095 const uint32_t* pod = reinterpret_cast<const uint32_t*>(podData);
2096 2077
2097 // the order we read must match the order we wrote in flatten() 2078 // the order we read must match the order we wrote in flatten()
2098 this->setTextSize(read_scalar(pod)); 2079 this->setTextSize(read_scalar(pod));
2099 this->setTextScaleX(read_scalar(pod)); 2080 this->setTextScaleX(read_scalar(pod));
2100 this->setTextSkewX(read_scalar(pod)); 2081 this->setTextSkewX(read_scalar(pod));
2101 this->setStrokeWidth(read_scalar(pod)); 2082 this->setStrokeWidth(read_scalar(pod));
2102 this->setStrokeMiter(read_scalar(pod)); 2083 this->setStrokeMiter(read_scalar(pod));
2103 this->setColor(*pod++); 2084 this->setColor(*pod++);
2104 2085
2105 // previously flags:16, textAlign:8, flatFlags:8 2086 // previously flags:16, textAlign:8, flatFlags:8
2106 // now flags:16, hinting:4, textAlign:4, flatFlags:8 2087 // now flags:16, hinting:4, textAlign:4, flatFlags:8
2107 uint32_t tmp = *pod++; 2088 uint32_t tmp = *pod++;
2108 this->setFlags(tmp >> 16); 2089 this->setFlags(tmp >> 16);
2109 2090
2110 // hinting added later. 0 in this nibble means use the default. 2091 // hinting added later. 0 in this nibble means use the default.
2111 uint32_t hinting = (tmp >> 12) & 0xF; 2092 uint32_t hinting = (tmp >> 12) & 0xF;
2112 this->setHinting(0 == hinting ? kNormal_Hinting : static_cast<Hinting>(h inting-1)); 2093 this->setHinting(0 == hinting ? kNormal_Hinting : static_cast<Hinting>(hinti ng-1));
2113 2094
2114 this->setTextAlign(static_cast<Align>((tmp >> 8) & 0xF)); 2095 this->setTextAlign(static_cast<Align>((tmp >> 8) & 0xF));
2115 2096
2116 flatFlags = tmp & 0xFF; 2097 flatFlags = tmp & 0xFF;
2117 2098
2118 tmp = *pod++; 2099 tmp = *pod++;
2119 this->setStrokeCap(static_cast<Cap>((tmp >> 24) & 0xFF)); 2100 this->setStrokeCap(static_cast<Cap>((tmp >> 24) & 0xFF));
2120 this->setStrokeJoin(static_cast<Join>((tmp >> 16) & 0xFF)); 2101 this->setStrokeJoin(static_cast<Join>((tmp >> 16) & 0xFF));
2121 this->setStyle(static_cast<Style>((tmp >> 8) & 0xFF)); 2102 this->setStyle(static_cast<Style>((tmp >> 8) & 0xFF));
2122 this->setTextEncoding(static_cast<TextEncoding>((tmp >> 0) & 0xFF)); 2103 this->setTextEncoding(static_cast<TextEncoding>((tmp >> 0) & 0xFF));
2123 } else {
2124 this->setTextSize(buffer.readScalar());
2125 this->setTextScaleX(buffer.readScalar());
2126 this->setTextSkewX(buffer.readScalar());
2127 // Skip the hinting scalar factor, which is not supported.
2128 buffer.readScalar();
2129 this->setStrokeWidth(buffer.readScalar());
2130 this->setStrokeMiter(buffer.readScalar());
2131 this->setColor(buffer.readColor());
2132 this->setFlags(buffer.readUInt());
2133 this->setHinting(static_cast<SkPaint::Hinting>(buffer.readUInt()));
2134 this->setTextAlign(static_cast<SkPaint::Align>(buffer.readUInt()));
2135 flatFlags = buffer.readUInt();
2136
2137 this->setStrokeCap(static_cast<SkPaint::Cap>(buffer.readUInt()));
2138 this->setStrokeJoin(static_cast<SkPaint::Join>(buffer.readUInt()));
2139 this->setStyle(static_cast<SkPaint::Style>(buffer.readUInt()));
2140 this->setTextEncoding(static_cast<SkPaint::TextEncoding>(buffer.readUInt ()));
2141 }
2142 2104
2143 if (flatFlags & kHasTypeface_FlatFlag) { 2105 if (flatFlags & kHasTypeface_FlatFlag) {
2144 this->setTypeface(buffer.readTypeface()); 2106 this->setTypeface(buffer.readTypeface());
2145 } else { 2107 } else {
2146 this->setTypeface(NULL); 2108 this->setTypeface(NULL);
2147 } 2109 }
2148 2110
2149 if (flatFlags & kHasEffects_FlatFlag) { 2111 if (flatFlags & kHasEffects_FlatFlag) {
2150 SkSafeUnref(this->setPathEffect(buffer.readPathEffect())); 2112 SkSafeUnref(this->setPathEffect(buffer.readPathEffect()));
2151 SkSafeUnref(this->setShader(buffer.readShader())); 2113 SkSafeUnref(this->setShader(buffer.readShader()));
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after
2581 case SkXfermode::kPlus_Mode: 2543 case SkXfermode::kPlus_Mode:
2582 return 0 == this->getAlpha(); 2544 return 0 == this->getAlpha();
2583 case SkXfermode::kDst_Mode: 2545 case SkXfermode::kDst_Mode:
2584 return true; 2546 return true;
2585 default: 2547 default:
2586 break; 2548 break;
2587 } 2549 }
2588 } 2550 }
2589 return false; 2551 return false;
2590 } 2552 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698