SkLeanWindows.h: #include "Windows.h" fewer places

    $ git grep -l '<windows.h>' include src
    include/private/SkLeanWindows.h

    $ git grep -l SkLeanWindows.h | grep '\.h$'
    include/ports/SkTypeface_win.h
    include/utils/win/SkHRESULT.h
    include/utils/win/SkTScopedComPtr.h
    include/views/SkEvent.h
    src/core/SkMathPriv.h
    src/ports/SkTypeface_win_dw.h
    src/utils/SkThreadUtils_win.h
    src/utils/win/SkWGL.h

The same for  `#include <intrin.h>` that was found in SkMath.h.
Those functions that needed it are moved to SkMathPriv.h.

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2041943002

CQ_INCLUDE_TRYBOTS=tryserver.chromium.win:win_chromium_compile_dbg_ng,win_chromium_compile_rel_ng

Review-Url: https://codereview.chromium.org/2041943002
diff --git a/bench/MathBench.cpp b/bench/MathBench.cpp
index 877d426..05a5f8a 100644
--- a/bench/MathBench.cpp
+++ b/bench/MathBench.cpp
@@ -8,6 +8,7 @@
 #include "Benchmark.h"
 #include "SkColorPriv.h"
 #include "SkFixed.h"
+#include "SkMathPriv.h"
 #include "SkMatrix.h"
 #include "SkPaint.h"
 #include "SkRandom.h"
diff --git a/bench/nanobench.cpp b/bench/nanobench.cpp
index a7e9ebb..e5a63a0 100644
--- a/bench/nanobench.cpp
+++ b/bench/nanobench.cpp
@@ -34,6 +34,7 @@
 #include "SkData.h"
 #include "SkForceLinking.h"
 #include "SkGraphics.h"
+#include "SkLeanWindows.h"
 #include "SkOSFile.h"
 #include "SkPictureRecorder.h"
 #include "SkPictureUtils.h"
diff --git a/dm/DM.cpp b/dm/DM.cpp
index 61a3d2e..de99b6b 100644
--- a/dm/DM.cpp
+++ b/dm/DM.cpp
@@ -22,6 +22,7 @@
 #include "SkFontMgr.h"
 #include "SkGraphics.h"
 #include "SkHalf.h"
+#include "SkLeanWindows.h"
 #include "SkMD5.h"
 #include "SkMutex.h"
 #include "SkOSFile.h"
diff --git a/gm/drawbitmaprect.cpp b/gm/drawbitmaprect.cpp
index bd6fffe..4e1af7e 100644
--- a/gm/drawbitmaprect.cpp
+++ b/gm/drawbitmaprect.cpp
@@ -12,6 +12,7 @@
 #include "SkGradientShader.h"
 #include "SkImage.h"
 #include "SkImage_Base.h"
+#include "SkMathPriv.h"
 #include "SkShader.h"
 #include "SkSurface.h"
 
diff --git a/gm/drawminibitmaprect.cpp b/gm/drawminibitmaprect.cpp
index 073d5a1..d539f26 100644
--- a/gm/drawminibitmaprect.cpp
+++ b/gm/drawminibitmaprect.cpp
@@ -9,6 +9,7 @@
 #include "SkColorPriv.h"
 #include "SkGradientShader.h"
 #include "SkImage.h"
+#include "SkMathPriv.h"
 #include "SkRandom.h"
 #include "SkShader.h"
 #include "SkSurface.h"
diff --git a/include/core/SkMath.h b/include/core/SkMath.h
index 7999390..8276c5d 100644
--- a/include/core/SkMath.h
+++ b/include/core/SkMath.h
@@ -61,35 +61,6 @@
  */
 #define SkSqrt32(n)         SkSqrtBits(n, 15)
 
-//! Returns the number of leading zero bits (0...32)
-int SkCLZ_portable(uint32_t);
-
-#ifndef SkCLZ
-    #if defined(_MSC_VER)
-        #include <intrin.h>
-
-        static inline int SkCLZ(uint32_t mask) {
-            if (mask) {
-                DWORD index;
-                _BitScanReverse(&index, mask);
-                // Suppress this bogus /analyze warning. The check for non-zero
-                // guarantees that _BitScanReverse will succeed.
-#pragma warning(suppress : 6102) // Using 'index' from failed function call
-                return index ^ 0x1F;
-            } else {
-                return 32;
-            }
-        }
-    #elif defined(SK_CPU_ARM32) || defined(__GNUC__) || defined(__clang__)
-        static inline int SkCLZ(uint32_t mask) {
-            // __builtin_clz(0) is undefined, so we have to detect that case.
-            return mask ? __builtin_clz(mask) : 32;
-        }
-    #else
-        #define SkCLZ(x)    SkCLZ_portable(x)
-    #endif
-#endif
-
 /**
  *  Returns (value < 0 ? 0 : value) efficiently (i.e. no compares or branches)
  */
@@ -116,30 +87,6 @@
 }
 
 /**
- *  Returns the smallest power-of-2 that is >= the specified value. If value
- *  is already a power of 2, then it is returned unchanged. It is undefined
- *  if value is <= 0.
- */
-static inline int SkNextPow2(int value) {
-    SkASSERT(value > 0);
-    return 1 << (32 - SkCLZ(value - 1));
-}
-
-/**
- *  Returns the log2 of the specified value, were that value to be rounded up
- *  to the next power of 2. It is undefined to pass 0. Examples:
- *  SkNextLog2(1) -> 0
- *  SkNextLog2(2) -> 1
- *  SkNextLog2(3) -> 2
- *  SkNextLog2(4) -> 2
- *  SkNextLog2(5) -> 3
- */
-static inline int SkNextLog2(uint32_t value) {
-    SkASSERT(value != 0);
-    return 32 - SkCLZ(value - 1);
-}
-
-/**
  *  Returns true if value is a power of 2. Does not explicitly check for
  *  value <= 0.
  */
diff --git a/include/core/SkPostConfig.h b/include/core/SkPostConfig.h
index c3255b8..9cae12f 100644
--- a/include/core/SkPostConfig.h
+++ b/include/core/SkPostConfig.h
@@ -107,26 +107,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 #ifdef SK_BUILD_FOR_WIN
-#  ifndef WIN32_LEAN_AND_MEAN
-#    define WIN32_LEAN_AND_MEAN
-#    define WIN32_IS_MEAN_WAS_LOCALLY_DEFINED
-#  endif
-#  ifndef NOMINMAX
-#    define NOMINMAX
-#    define NOMINMAX_WAS_LOCALLY_DEFINED
-#  endif
-#
-#  include <windows.h>
-#
-#  ifdef WIN32_IS_MEAN_WAS_LOCALLY_DEFINED
-#    undef WIN32_IS_MEAN_WAS_LOCALLY_DEFINED
-#    undef WIN32_LEAN_AND_MEAN
-#  endif
-#  ifdef NOMINMAX_WAS_LOCALLY_DEFINED
-#    undef NOMINMAX_WAS_LOCALLY_DEFINED
-#    undef NOMINMAX
-#  endif
-#
 #  ifndef SK_A32_SHIFT
 #    define SK_A32_SHIFT 24
 #    define SK_R32_SHIFT 16
diff --git a/include/core/SkPreConfig.h b/include/core/SkPreConfig.h
index 928c1bd..c410c98 100644
--- a/include/core/SkPreConfig.h
+++ b/include/core/SkPreConfig.h
@@ -210,7 +210,7 @@
 #endif
 
 #if defined(SKIA_DLL)
-    #if defined(WIN32)
+    #if defined(SK_BUILD_FOR_WIN32)
         #if SKIA_IMPLEMENTATION
             #define SK_API __declspec(dllexport)
         #else
diff --git a/include/gpu/GrTypes.h b/include/gpu/GrTypes.h
index 9025945..979afb3 100644
--- a/include/gpu/GrTypes.h
+++ b/include/gpu/GrTypes.h
@@ -108,20 +108,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 /**
- *  Return the next power of 2 >= n.
- */
-static inline uint32_t GrNextPow2(uint32_t n) {
-    return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
-}
-
-static inline int GrNextPow2(int n) {
-    SkASSERT(n >= 0); // this impl only works for non-neg.
-    return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
-}
-
-///////////////////////////////////////////////////////////////////////////////
-
-/**
  * Possible 3D APIs that may be used by Ganesh.
  */
 enum GrBackend {
diff --git a/include/ports/SkTypeface_win.h b/include/ports/SkTypeface_win.h
index e18ed93..1945b75 100644
--- a/include/ports/SkTypeface_win.h
+++ b/include/ports/SkTypeface_win.h
@@ -8,6 +8,7 @@
 #ifndef SkTypeface_win_DEFINED
 #define SkTypeface_win_DEFINED
 
+#include "../private/SkLeanWindows.h"
 #include "SkTypeface.h"
 
 #ifdef SK_BUILD_FOR_WIN
diff --git a/include/private/SkLeanWindows.h b/include/private/SkLeanWindows.h
new file mode 100644
index 0000000..2bdddbb
--- /dev/null
+++ b/include/private/SkLeanWindows.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+#ifndef SkLeanWindows_DEFINED
+#define SkLeanWindows_DEFINED
+
+#include "SkTypes.h"
+
+#ifdef SK_BUILD_FOR_WIN
+#  ifndef WIN32_LEAN_AND_MEAN
+#    define WIN32_LEAN_AND_MEAN
+#    define WIN32_IS_MEAN_WAS_LOCALLY_DEFINED
+#  endif
+#  ifndef NOMINMAX
+#    define NOMINMAX
+#    define NOMINMAX_WAS_LOCALLY_DEFINED
+#  endif
+#
+#  include <windows.h>
+#
+#  ifdef WIN32_IS_MEAN_WAS_LOCALLY_DEFINED
+#    undef WIN32_IS_MEAN_WAS_LOCALLY_DEFINED
+#    undef WIN32_LEAN_AND_MEAN
+#  endif
+#  ifdef NOMINMAX_WAS_LOCALLY_DEFINED
+#    undef NOMINMAX_WAS_LOCALLY_DEFINED
+#    undef NOMINMAX
+#  endif
+#endif
+
+#endif  // SkLeanWindows_DEFINED
diff --git a/include/private/SkRecords.h b/include/private/SkRecords.h
index 1e274e9..d6ef12f 100644
--- a/include/private/SkRecords.h
+++ b/include/private/SkRecords.h
@@ -21,6 +21,14 @@
 #include "SkString.h"
 #include "SkTextBlob.h"
 
+// Windows.h, will pull in all of the GDI defines.  GDI #defines
+// DrawText to DrawTextA or DrawTextW, but SkRecord has a struct
+// called DrawText. Since this file does not use GDI, undefing
+// DrawText makes things less confusing.
+#ifdef DrawText
+#undef DrawText
+#endif
+
 namespace SkRecords {
 
 // A list of all the types of canvas calls we can record.
diff --git a/include/utils/win/SkAutoCoInitialize.h b/include/utils/win/SkAutoCoInitialize.h
index 35fcce5..0470978 100644
--- a/include/utils/win/SkAutoCoInitialize.h
+++ b/include/utils/win/SkAutoCoInitialize.h
@@ -12,6 +12,8 @@
 
 #ifdef SK_BUILD_FOR_WIN
 
+#include "../../private/SkLeanWindows.h"
+
 /**
  * An instance of this class initializes COM on creation
  * and closes the COM library on destruction.
diff --git a/include/utils/win/SkHRESULT.h b/include/utils/win/SkHRESULT.h
index a0ffd59..69a7118 100644
--- a/include/utils/win/SkHRESULT.h
+++ b/include/utils/win/SkHRESULT.h
@@ -11,6 +11,8 @@
 #include "SkTypes.h"
 #ifdef SK_BUILD_FOR_WIN
 
+#include "../../private/SkLeanWindows.h"
+
 void SkTraceHR(const char* file, unsigned long line,
                HRESULT hr, const char* msg);
 
diff --git a/include/utils/win/SkIStream.h b/include/utils/win/SkIStream.h
index 4a72f5b..28a59f9 100644
--- a/include/utils/win/SkIStream.h
+++ b/include/utils/win/SkIStream.h
@@ -14,8 +14,7 @@
 
 #ifdef SK_BUILD_FOR_WIN
 
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
+#include "../../private/SkLeanWindows.h"
 #include <ole2.h>
 
 class SkStream;
diff --git a/include/utils/win/SkTScopedComPtr.h b/include/utils/win/SkTScopedComPtr.h
index d2d925c..6fb6143 100644
--- a/include/utils/win/SkTScopedComPtr.h
+++ b/include/utils/win/SkTScopedComPtr.h
@@ -8,7 +8,7 @@
 #ifndef SkTScopedComPtr_DEFINED
 #define SkTScopedComPtr_DEFINED
 
-#include "SkTypes.h"
+#include "../../private/SkLeanWindows.h"
 
 #ifdef SK_BUILD_FOR_WIN
 
diff --git a/include/views/SkEvent.h b/include/views/SkEvent.h
index b8fc00e..6495530 100644
--- a/include/views/SkEvent.h
+++ b/include/views/SkEvent.h
@@ -12,6 +12,8 @@
 #include "SkMetaData.h"
 #include "SkString.h"
 
+#include "../private/SkLeanWindows.h"
+
 /** Unique 32bit id used to identify an instance of SkEventSink. When events are
     posted, they are posted to a specific sinkID. When it is time to dispatch the
     event, the sinkID is used to find the specific SkEventSink object. If it is found,
diff --git a/src/core/SkEdge.cpp b/src/core/SkEdge.cpp
index 49df95b..d91c3e6 100644
--- a/src/core/SkEdge.cpp
+++ b/src/core/SkEdge.cpp
@@ -8,7 +8,7 @@
 
 #include "SkEdge.h"
 #include "SkFDot6.h"
-#include "SkMath.h"
+#include "SkMathPriv.h"
 
 /*
     In setLine, setQuadratic, setCubic, the first thing we do is to convert
diff --git a/src/core/SkMathPriv.h b/src/core/SkMathPriv.h
index 1b64a6c..fca09f3 100644
--- a/src/core/SkMathPriv.h
+++ b/src/core/SkMathPriv.h
@@ -92,4 +92,70 @@
     static inline uint32_t SkBSwap32(uint32_t v) { return __builtin_bswap32(v); }
 #endif
 
+//! Returns the number of leading zero bits (0...32)
+int SkCLZ_portable(uint32_t);
+
+#ifndef SkCLZ
+    #if defined(SK_BUILD_FOR_WIN32)
+        #include <intrin.h>
+
+        static inline int SkCLZ(uint32_t mask) {
+            if (mask) {
+                unsigned long index;
+                _BitScanReverse(&index, mask);
+                // Suppress this bogus /analyze warning. The check for non-zero
+                // guarantees that _BitScanReverse will succeed.
+#pragma warning(suppress : 6102) // Using 'index' from failed function call
+                return index ^ 0x1F;
+            } else {
+                return 32;
+            }
+        }
+    #elif defined(SK_CPU_ARM32) || defined(__GNUC__) || defined(__clang__)
+        static inline int SkCLZ(uint32_t mask) {
+            // __builtin_clz(0) is undefined, so we have to detect that case.
+            return mask ? __builtin_clz(mask) : 32;
+        }
+    #else
+        #define SkCLZ(x)    SkCLZ_portable(x)
+    #endif
+#endif
+
+/**
+ *  Returns the smallest power-of-2 that is >= the specified value. If value
+ *  is already a power of 2, then it is returned unchanged. It is undefined
+ *  if value is <= 0.
+ */
+static inline int SkNextPow2(int value) {
+    SkASSERT(value > 0);
+    return 1 << (32 - SkCLZ(value - 1));
+}
+
+/**
+ *  Returns the log2 of the specified value, were that value to be rounded up
+ *  to the next power of 2. It is undefined to pass 0. Examples:
+ *  SkNextLog2(1) -> 0
+ *  SkNextLog2(2) -> 1
+ *  SkNextLog2(3) -> 2
+ *  SkNextLog2(4) -> 2
+ *  SkNextLog2(5) -> 3
+ */
+static inline int SkNextLog2(uint32_t value) {
+    SkASSERT(value != 0);
+    return 32 - SkCLZ(value - 1);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+/**
+ *  Return the next power of 2 >= n.
+ */
+static inline uint32_t GrNextPow2(uint32_t n) {
+    return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
+}
+
+static inline int GrNextPow2(int n) {
+    SkASSERT(n >= 0); // this impl only works for non-neg.
+    return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
+}
 #endif
diff --git a/src/core/SkMipMap.cpp b/src/core/SkMipMap.cpp
index 76f1718..e6f4bad 100644
--- a/src/core/SkMipMap.cpp
+++ b/src/core/SkMipMap.cpp
@@ -9,7 +9,7 @@
 #include "SkBitmap.h"
 #include "SkColorPriv.h"
 #include "SkHalf.h"
-#include "SkMath.h"
+#include "SkMathPriv.h"
 #include "SkNx.h"
 #include "SkTypes.h"
 
diff --git a/src/core/SkScan_Hairline.cpp b/src/core/SkScan_Hairline.cpp
index a023ca1..fc9e828 100644
--- a/src/core/SkScan_Hairline.cpp
+++ b/src/core/SkScan_Hairline.cpp
@@ -7,6 +7,7 @@
 
 #include "SkScan.h"
 #include "SkBlitter.h"
+#include "SkMathPriv.h"
 #include "SkRasterClip.h"
 #include "SkFDot6.h"
 #include "SkLineClipper.h"
diff --git a/src/core/SkSemaphore.cpp b/src/core/SkSemaphore.cpp
index 2286c0b..1ebe51b 100644
--- a/src/core/SkSemaphore.cpp
+++ b/src/core/SkSemaphore.cpp
@@ -5,6 +5,7 @@
  * found in the LICENSE file.
  */
 
+#include "../private/SkLeanWindows.h"
 #include "../private/SkSemaphore.h"
 
 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
diff --git a/src/core/SkTSort.h b/src/core/SkTSort.h
index 51b103c..7101bab 100644
--- a/src/core/SkTSort.h
+++ b/src/core/SkTSort.h
@@ -10,7 +10,7 @@
 #define SkTSort_DEFINED
 
 #include "SkTypes.h"
-#include "SkMath.h"
+#include "SkMathPriv.h"
 
 /* A comparison functor which performs the comparison 'a < b'. */
 template <typename T> struct SkTCompareLT {
diff --git a/src/core/SkTaskGroup.cpp b/src/core/SkTaskGroup.cpp
index 7a58c86..d151510 100644
--- a/src/core/SkTaskGroup.cpp
+++ b/src/core/SkTaskGroup.cpp
@@ -5,6 +5,7 @@
  * found in the LICENSE file.
  */
 
+#include "SkLeanWindows.h"
 #include "SkOnce.h"
 #include "SkSemaphore.h"
 #include "SkSpinlock.h"
diff --git a/src/core/SkThreadID.cpp b/src/core/SkThreadID.cpp
index 589b4da..948884d 100644
--- a/src/core/SkThreadID.cpp
+++ b/src/core/SkThreadID.cpp
@@ -5,10 +5,10 @@
  * found in the LICENSE file.
  */
 
+#include "SkLeanWindows.h"
 #include "SkThreadID.h"
 
 #ifdef SK_BUILD_FOR_WIN
-    #include <windows.h>
     SkThreadID SkGetThreadID() { return GetCurrentThreadId(); }
 #else
     #include <pthread.h>
diff --git a/src/core/SkTime.cpp b/src/core/SkTime.cpp
index e89d5b9..4ed0892 100644
--- a/src/core/SkTime.cpp
+++ b/src/core/SkTime.cpp
@@ -5,6 +5,7 @@
  * found in the LICENSE file.
  */
 
+#include "SkLeanWindows.h"
 #include "SkString.h"
 #include "SkTime.h"
 #include "SkTypes.h"
@@ -25,10 +26,8 @@
     }
 }
 
-
 #ifdef SK_BUILD_FOR_WIN32
 
-#include "windows.h"
 void SkTime::GetDateTime(DateTime* dt) {
     if (dt) {
         SYSTEMTIME st;
diff --git a/src/effects/gradients/SkClampRange.cpp b/src/effects/gradients/SkClampRange.cpp
index 0bf30f1..5fd1c03 100644
--- a/src/effects/gradients/SkClampRange.cpp
+++ b/src/effects/gradients/SkClampRange.cpp
@@ -6,7 +6,7 @@
  */
 
 #include "SkClampRange.h"
-#include "SkMath.h"
+#include "SkMathPriv.h"
 
 static int SkCLZ64(uint64_t value) {
     int count = 0;
diff --git a/src/gpu/GrGpu.cpp b/src/gpu/GrGpu.cpp
index 691c0ab..a381f31 100644
--- a/src/gpu/GrGpu.cpp
+++ b/src/gpu/GrGpu.cpp
@@ -21,7 +21,7 @@
 #include "GrStencilAttachment.h"
 #include "GrSurfacePriv.h"
 #include "GrTexturePriv.h"
-#include "SkTypes.h"
+#include "SkMathPriv.h"
 
 GrMesh& GrMesh::operator =(const GrMesh& di) {
     fPrimitiveType  = di.fPrimitiveType;
diff --git a/src/gpu/GrPathUtils.cpp b/src/gpu/GrPathUtils.cpp
index fcb32cb..aa55dad 100644
--- a/src/gpu/GrPathUtils.cpp
+++ b/src/gpu/GrPathUtils.cpp
@@ -9,6 +9,7 @@
 
 #include "GrTypes.h"
 #include "SkGeometry.h"
+#include "SkMathPriv.h"
 
 SkScalar GrPathUtils::scaleToleranceToSrc(SkScalar devTol,
                                           const SkMatrix& viewM,
diff --git a/src/gpu/GrRectanizer_pow2.h b/src/gpu/GrRectanizer_pow2.h
index 451589d..296e052 100644
--- a/src/gpu/GrRectanizer_pow2.h
+++ b/src/gpu/GrRectanizer_pow2.h
@@ -9,6 +9,7 @@
 #define GrRectanizer_pow2_DEFINED
 
 #include "GrRectanizer.h"
+#include "SkMathPriv.h"
 #include "SkPoint.h"
 
 // This Rectanizer quantizes the incoming rects to powers of 2. Each power
diff --git a/src/gpu/GrResourceProvider.cpp b/src/gpu/GrResourceProvider.cpp
index 9fa202a..efbcdba 100644
--- a/src/gpu/GrResourceProvider.cpp
+++ b/src/gpu/GrResourceProvider.cpp
@@ -15,6 +15,7 @@
 #include "GrResourceCache.h"
 #include "GrResourceKey.h"
 #include "GrStencilAttachment.h"
+#include "SkMathPriv.h"
 
 GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
 
diff --git a/src/gpu/GrTextureProvider.cpp b/src/gpu/GrTextureProvider.cpp
index 1053ab2..69792ed 100644
--- a/src/gpu/GrTextureProvider.cpp
+++ b/src/gpu/GrTextureProvider.cpp
@@ -10,6 +10,7 @@
 #include "GrResourceCache.h"
 #include "GrGpu.h"
 #include "../private/GrSingleOwner.h"
+#include "SkMathPriv.h"
 #include "SkTArray.h"
 
 #define ASSERT_SINGLE_OWNER \
diff --git a/src/gpu/batches/GrAtlasTextBatch.cpp b/src/gpu/batches/GrAtlasTextBatch.cpp
index 5a41457..32a64c3 100644
--- a/src/gpu/batches/GrAtlasTextBatch.cpp
+++ b/src/gpu/batches/GrAtlasTextBatch.cpp
@@ -11,6 +11,7 @@
 #include "GrResourceProvider.h"
 
 #include "SkGlyphCache.h"
+#include "SkMathPriv.h"
 
 #include "effects/GrBitmapTextGeoProc.h"
 #include "effects/GrDistanceFieldGeoProc.h"
diff --git a/src/gpu/gl/win/GrGLCreateNativeInterface_win.cpp b/src/gpu/gl/win/GrGLCreateNativeInterface_win.cpp
index 1eae5d7..5f26fdd 100644
--- a/src/gpu/gl/win/GrGLCreateNativeInterface_win.cpp
+++ b/src/gpu/gl/win/GrGLCreateNativeInterface_win.cpp
@@ -7,11 +7,11 @@
 #include "SkTypes.h"
 #if defined(SK_BUILD_FOR_WIN32)
 
+#include "SkLeanWindows.h"
+
 #include "gl/GrGLInterface.h"
 #include "gl/GrGLAssembleInterface.h"
 #include "gl/GrGLUtil.h"
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
 
 class AutoLibraryUnload {
 public:
diff --git a/src/ports/SkDebug_win.cpp b/src/ports/SkDebug_win.cpp
index b0c5725..da1e2b5 100644
--- a/src/ports/SkDebug_win.cpp
+++ b/src/ports/SkDebug_win.cpp
@@ -5,16 +5,16 @@
  * found in the LICENSE file.
  */
 
-
-
 #include "SkTypes.h"
+
 #if defined(SK_BUILD_FOR_WIN32)
 
-static const size_t kBufferSize = 2048;
+#include "SkLeanWindows.h"
 
 #include <stdarg.h>
 #include <stdio.h>
-#include <windows.h>
+
+static const size_t kBufferSize = 2048;
 
 void SkDebugf(const char format[], ...) {
     char    buffer[kBufferSize + 1];
diff --git a/src/ports/SkFontHost_mac.cpp b/src/ports/SkFontHost_mac.cpp
index 0cc436f..c129a6b2 100644
--- a/src/ports/SkFontHost_mac.cpp
+++ b/src/ports/SkFontHost_mac.cpp
@@ -29,6 +29,7 @@
 #include "SkFontMgr.h"
 #include "SkGlyph.h"
 #include "SkMaskGamma.h"
+#include "SkMathPriv.h"
 #include "SkMutex.h"
 #include "SkOTTable_glyf.h"
 #include "SkOTTable_head.h"
diff --git a/src/ports/SkOSFile_win.cpp b/src/ports/SkOSFile_win.cpp
index 6bdf9ab..cf46cea 100644
--- a/src/ports/SkOSFile_win.cpp
+++ b/src/ports/SkOSFile_win.cpp
@@ -8,8 +8,8 @@
 #include "SkTypes.h"
 #if defined(SK_BUILD_FOR_WIN32)
 
+#include "SkLeanWindows.h"
 #include "SkOSFile.h"
-
 #include "SkTFitsIn.h"
 
 #include <io.h>
diff --git a/src/ports/SkOSLibrary_win.cpp b/src/ports/SkOSLibrary_win.cpp
index 63d8d2d..b6d8dd3 100644
--- a/src/ports/SkOSLibrary_win.cpp
+++ b/src/ports/SkOSLibrary_win.cpp
@@ -8,7 +8,7 @@
 #if defined(SK_BUILD_FOR_WIN32)
 
 #include "SkOSLibrary.h"
-#include <windows.h>
+#include "SkLeanWindows.h"
 
 void* DynamicLoadLibrary(const char* libraryName) {
     return LoadLibraryA(libraryName);
diff --git a/src/ports/SkTLS_win.cpp b/src/ports/SkTLS_win.cpp
index 8bc55a0..c349ad9 100644
--- a/src/ports/SkTLS_win.cpp
+++ b/src/ports/SkTLS_win.cpp
@@ -7,8 +7,9 @@
 #include "SkTypes.h"
 #if defined(SK_BUILD_FOR_WIN32)
 
-#include "SkTLS.h"
+#include "SkLeanWindows.h"
 #include "SkMutex.h"
+#include "SkTLS.h"
 
 static bool gOnce = false;
 static DWORD gTlsIndex;
diff --git a/src/ports/SkTypeface_win_dw.h b/src/ports/SkTypeface_win_dw.h
index 3e429f4..11b3fb5 100644
--- a/src/ports/SkTypeface_win_dw.h
+++ b/src/ports/SkTypeface_win_dw.h
@@ -11,10 +11,10 @@
 #include "SkAdvancedTypefaceMetrics.h"
 #include "SkDWrite.h"
 #include "SkHRESULT.h"
+#include "SkLeanWindows.h"
 #include "SkTScopedComPtr.h"
 #include "SkTypeface.h"
 #include "SkTypefaceCache.h"
-#include "SkTypes.h"
 
 #include <dwrite.h>
 #if SK_HAS_DWRITE_1_H
diff --git a/src/utils/SkTextureCompressor.cpp b/src/utils/SkTextureCompressor.cpp
index e308f6e..086214e 100644
--- a/src/utils/SkTextureCompressor.cpp
+++ b/src/utils/SkTextureCompressor.cpp
@@ -14,6 +14,7 @@
 #include "SkBitmapProcShader.h"
 #include "SkData.h"
 #include "SkEndian.h"
+#include "SkMathPriv.h"
 #include "SkOpts.h"
 
 #ifndef SK_IGNORE_ETC1_SUPPORT
diff --git a/src/utils/SkTextureCompressor_ASTC.cpp b/src/utils/SkTextureCompressor_ASTC.cpp
index 02f4f17..8a96b91 100644
--- a/src/utils/SkTextureCompressor_ASTC.cpp
+++ b/src/utils/SkTextureCompressor_ASTC.cpp
@@ -10,7 +10,7 @@
 
 #include "SkBlitter.h"
 #include "SkEndian.h"
-#include "SkMath.h"
+#include "SkMathPriv.h"
 
 // This table contains the weight values for each texel. This is used in determining
 // how to convert a 12x12 grid of alpha values into a 6x5 grid of index values. Since
diff --git a/src/utils/SkThreadUtils_win.h b/src/utils/SkThreadUtils_win.h
index 51b32fa..b1de481 100644
--- a/src/utils/SkThreadUtils_win.h
+++ b/src/utils/SkThreadUtils_win.h
@@ -8,8 +8,7 @@
 #ifndef SkThreadUtils_WinData_DEFINED
 #define SkThreadUtils_WinData_DEFINED
 
-#include "SkTypes.h"
-
+#include "SkLeanWindows.h"
 #include "SkThreadUtils.h"
 
 class SkThread_WinData : SkNoncopyable {
diff --git a/src/utils/win/SkAutoCoInitialize.cpp b/src/utils/win/SkAutoCoInitialize.cpp
index 3b7b42b..f6da67d 100644
--- a/src/utils/win/SkAutoCoInitialize.cpp
+++ b/src/utils/win/SkAutoCoInitialize.cpp
@@ -8,12 +8,11 @@
 #include "SkTypes.h"
 #if defined(SK_BUILD_FOR_WIN32)
 
-
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-#include <ole2.h>
 #include "SkAutoCoInitialize.h"
 
+#include <objbase.h>
+#include <winerror.h>
+
 SkAutoCoInitialize::SkAutoCoInitialize() :
     fHR(
         CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)
diff --git a/src/utils/win/SkIStream.cpp b/src/utils/win/SkIStream.cpp
index ad9bf05..560a947 100644
--- a/src/utils/win/SkIStream.cpp
+++ b/src/utils/win/SkIStream.cpp
@@ -8,9 +8,6 @@
 #include "SkTypes.h"
 #if defined(SK_BUILD_FOR_WIN32)
 
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-#include <ole2.h>
 #include "SkIStream.h"
 #include "SkStream.h"
 
diff --git a/src/utils/win/SkWGL.h b/src/utils/win/SkWGL.h
index ebd95e7..3799377 100644
--- a/src/utils/win/SkWGL.h
+++ b/src/utils/win/SkWGL.h
@@ -10,6 +10,8 @@
 #ifndef SkWGL_DEFINED
 #define SkWGL_DEFINED
 
+#include "SkLeanWindows.h"
+
 /**
  * Working with WGL extensions can be a pain. Among the reasons is that You must
  * have a GL context to get the proc addresses, but you want to use the procs to
@@ -22,16 +24,6 @@
  * an extension.
  */
 
-#if !defined(WIN32_LEAN_AND_MEAN)
-    #define WIN32_LEAN_AND_MEAN
-    #define SK_LOCAL_LEAN_AND_MEAN
-#endif
-#include <windows.h>
-#if defined(SK_LOCAL_LEAN_AND_MEAN)
-    #undef WIN32_LEAN_AND_MEAN
-    #undef SK_LOCAL_LEAN_AND_MEAN
-#endif
-
 #define SK_WGL_DRAW_TO_WINDOW                       0x2001
 #define SK_WGL_ACCELERATION                         0x2003
 #define SK_WGL_SUPPORT_OPENGL                       0x2010
diff --git a/src/views/win/SkOSWindow_win.cpp b/src/views/win/SkOSWindow_win.cpp
index a5aafd0..c93d60b 100644
--- a/src/views/win/SkOSWindow_win.cpp
+++ b/src/views/win/SkOSWindow_win.cpp
@@ -8,6 +8,8 @@
 
 #if defined(SK_BUILD_FOR_WIN)
 
+#include "SkLeanWindows.h"
+
 #include <GL/gl.h>
 #include <WindowsX.h>
 #include "win/SkWGL.h"
diff --git a/src/views/win/skia_win.cpp b/src/views/win/skia_win.cpp
index 8996dda..fc07bd4 100644
--- a/src/views/win/skia_win.cpp
+++ b/src/views/win/skia_win.cpp
@@ -4,10 +4,11 @@
  * Use of this source code is governed by a BSD-style license that can be
  * found in the LICENSE file.
  */
-#include <windows.h>
-#include <tchar.h>
 
 #include "SkTypes.h"
+
+#include <tchar.h>
+
 #include "SkApplication.h"
 #include "SkOSWindow_Win.h"
 
diff --git a/src/xps/SkXPSDevice.cpp b/src/xps/SkXPSDevice.cpp
index eb983fc..ca0ee04 100644
--- a/src/xps/SkXPSDevice.cpp
+++ b/src/xps/SkXPSDevice.cpp
@@ -8,6 +8,8 @@
 #include "SkTypes.h"
 #if defined(SK_BUILD_FOR_WIN32)
 
+#include "SkLeanWindows.h"
+
 #ifndef UNICODE
 #define UNICODE
 #endif
diff --git a/tests/ClipStackTest.cpp b/tests/ClipStackTest.cpp
index a18d1ef..c2ad21b 100644
--- a/tests/ClipStackTest.cpp
+++ b/tests/ClipStackTest.cpp
@@ -1011,7 +1011,7 @@
     }
 }
 
-#if defined(WIN32)
+#ifdef SK_BUILD_FOR_WIN
     #define SUPPRESS_VISIBILITY_WARNING
 #else
     #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
diff --git a/tests/PathCoverageTest.cpp b/tests/PathCoverageTest.cpp
index 303ddcc..2551563 100644
--- a/tests/PathCoverageTest.cpp
+++ b/tests/PathCoverageTest.cpp
@@ -5,7 +5,7 @@
  * found in the LICENSE file.
  */
 
-#include "SkMath.h"
+#include "SkMathPriv.h"
 #include "SkPoint.h"
 #include "SkScalar.h"
 #include "Test.h"
diff --git a/tests/PathTest.cpp b/tests/PathTest.cpp
index 9de5f81..5bc3d95 100644
--- a/tests/PathTest.cpp
+++ b/tests/PathTest.cpp
@@ -180,7 +180,7 @@
     }
 }
 
-#if defined(WIN32)
+#ifdef SK_BUILD_FOR_WIN
     #define SUPPRESS_VISIBILITY_WARNING
 #else
     #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
diff --git a/tests/SubsetPath.cpp b/tests/SubsetPath.cpp
index e67df12..6a7660e 100644
--- a/tests/SubsetPath.cpp
+++ b/tests/SubsetPath.cpp
@@ -6,6 +6,7 @@
  */
 
 #include "SubsetPath.h"
+#include "SkMathPriv.h"
 
 SubsetPath::SubsetPath(const SkPath& path)
         : fPath(path)
diff --git a/tools/CrashHandler.cpp b/tools/CrashHandler.cpp
index 5428116..f141e55 100644
--- a/tools/CrashHandler.cpp
+++ b/tools/CrashHandler.cpp
@@ -7,7 +7,7 @@
 
 #include "CrashHandler.h"
 
-#include "SkTypes.h"
+#include "../private/SkLeanWindows.h"
 
 #include <stdlib.h>
 
diff --git a/tools/gpu/GrTest.cpp b/tools/gpu/GrTest.cpp
index 8fdab2e..c797dc0 100644
--- a/tools/gpu/GrTest.cpp
+++ b/tools/gpu/GrTest.cpp
@@ -16,6 +16,7 @@
 
 #include "SkGpuDevice.h"
 #include "SkGrPriv.h"
+#include "SkMathPriv.h"
 #include "SkString.h"
 
 #include "text/GrBatchFontCache.h"