From 7028443d1d70a2497f315443dfd45a157f300854 Mon Sep 17 00:00:00 2001 From: Mike Klein Date: Tue, 19 Sep 2017 13:07:27 -0400 Subject: fix race in SkFontHost_mac.cpp We've not got any sort of synchronization guarding gSupportsLCD. If we simply move the work into its initializer, it will happen once in a thread safe way. Change-Id: I86d87bea2850d0ce4d3886069f6f1f968d323343 Reviewed-on: https://skia-review.googlesource.com/48582 Reviewed-by: Jim Van Verth Commit-Queue: Mike Klein --- src/ports/SkFontHost_mac.cpp | 46 +++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 24 deletions(-) (limited to 'src/ports') diff --git a/src/ports/SkFontHost_mac.cpp b/src/ports/SkFontHost_mac.cpp index 2564ab5b04..d8bac13cdd 100644 --- a/src/ports/SkFontHost_mac.cpp +++ b/src/ports/SkFontHost_mac.cpp @@ -206,30 +206,28 @@ static CGAffineTransform MatrixToCGAffineTransform(const SkMatrix& matrix) { * smoothing is applied a gamma of 2.0 will be used, if not a gamma of 1.0. */ static bool supports_LCD() { - static int gSupportsLCD = -1; - if (gSupportsLCD >= 0) { - return (bool) gSupportsLCD; - } - uint32_t rgb = 0; - UniqueCFRef colorspace(CGColorSpaceCreateDeviceRGB()); - UniqueCFRef cgContext( - CGBitmapContextCreate(&rgb, 1, 1, 8, 4, colorspace.get(), BITMAP_INFO_RGB)); - UniqueCFRef ctFont(CTFontCreateWithName(CFSTR("Helvetica"), 16, nullptr)); - CGContextSetShouldSmoothFonts(cgContext.get(), true); - CGContextSetShouldAntialias(cgContext.get(), true); - CGContextSetTextDrawingMode(cgContext.get(), kCGTextFill); - CGContextSetGrayFillColor(cgContext.get(), 1, 1); - CGPoint point = CGPointMake(-1, 0); - static const UniChar pipeChar = '|'; - CGGlyph pipeGlyph; - CTFontGetGlyphsForCharacters(ctFont.get(), &pipeChar, &pipeGlyph, 1); - CTFontDrawGlyphs(ctFont.get(), &pipeGlyph, &point, 1, cgContext.get()); - - uint32_t r = (rgb >> 16) & 0xFF; - uint32_t g = (rgb >> 8) & 0xFF; - uint32_t b = (rgb >> 0) & 0xFF; - gSupportsLCD = (r != g || r != b); - return (bool) gSupportsLCD; + static bool gSupportsLCD = []{ + uint32_t rgb = 0; + UniqueCFRef colorspace(CGColorSpaceCreateDeviceRGB()); + UniqueCFRef cgContext( + CGBitmapContextCreate(&rgb, 1, 1, 8, 4, colorspace.get(), BITMAP_INFO_RGB)); + UniqueCFRef ctFont(CTFontCreateWithName(CFSTR("Helvetica"), 16, nullptr)); + CGContextSetShouldSmoothFonts(cgContext.get(), true); + CGContextSetShouldAntialias(cgContext.get(), true); + CGContextSetTextDrawingMode(cgContext.get(), kCGTextFill); + CGContextSetGrayFillColor(cgContext.get(), 1, 1); + CGPoint point = CGPointMake(-1, 0); + static const UniChar pipeChar = '|'; + CGGlyph pipeGlyph; + CTFontGetGlyphsForCharacters(ctFont.get(), &pipeChar, &pipeGlyph, 1); + CTFontDrawGlyphs(ctFont.get(), &pipeGlyph, &point, 1, cgContext.get()); + + uint32_t r = (rgb >> 16) & 0xFF; + uint32_t g = (rgb >> 8) & 0xFF; + uint32_t b = (rgb >> 0) & 0xFF; + return (r != g || r != b); + }(); + return gSupportsLCD; } class Offscreen { -- cgit v1.2.3