Update libpng to 1.6.22beta

Intel SSE filter optimizations have been upstreamed.  Let's test on
the upstream version (which we can use in Android).

BUG=skia:4573
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1710083007

Review URL: https://codereview.chromium.org/1710083007
diff --git a/DEPS b/DEPS
index c98b3fd..58fc188 100644
--- a/DEPS
+++ b/DEPS
@@ -18,7 +18,7 @@
   "third_party/externals/nanomsg" : "https://skia.googlesource.com/third_party/nanomsg.git@0.4-beta",
   "third_party/externals/zlib"    : "https://chromium.googlesource.com/chromium/src/third_party/zlib@c4e33043fb071b6ea0a153845da625d7ed633d3d",
   # NOTE: If we update libpng, we may need to update the generated file at third_party/libpng/pnglibconf.h
-  "third_party/externals/libpng"  : "https://skia.googlesource.com/third_party/libpng.git@047737496a77eeb97f9991919dac08ca3c149711",
+  "third_party/externals/libpng"  : "https://skia.googlesource.com/third_party/libpng.git@52846504da1f519cf1bb89636caf9110e8677443",
   "third_party/externals/giflib"  : "https://android.googlesource.com/platform/external/giflib.git@ab10e256df4f684260ca239905b1cec727181f6c",
 
   "third_party/externals/dng_sdk" : "https://android.googlesource.com/platform/external/dng_sdk.git@6579353b8ee5d8aa1f1a96ae22798de9b41e19b8",
diff --git a/gyp/codec.gyp b/gyp/codec.gyp
index 08750e2..c53d955 100644
--- a/gyp/codec.gyp
+++ b/gyp/codec.gyp
@@ -48,7 +48,6 @@
         '../src/codec/SkMaskSwizzler.cpp',
         '../src/codec/SkMasks.cpp',
         '../src/codec/SkPngCodec.cpp',
-        '../src/codec/SkPngFilters.cpp',
         '../src/codec/SkSampler.cpp',
         '../src/codec/SkSampledCodec.cpp',
         '../src/codec/SkSwizzler.cpp',
diff --git a/gyp/libpng.gyp b/gyp/libpng.gyp
index dc58459..6cd6a04 100644
--- a/gyp/libpng.gyp
+++ b/gyp/libpng.gyp
@@ -65,38 +65,30 @@
         '../third_party/externals/libpng/pngwutil.c',
       ],
       'conditions': [
-        [ 'skia_os == "ios"', {
-          # explicitly disable looking for NEON on iOS builds
+        [ '"x86" in skia_arch_type', {
           'defines': [
-            'PNG_ARM_NEON_OPT=0',
+            'PNG_INTEL_SSE_OPT=1',
           ],
-        }, { # skia_os != "ios"
-          'dependencies': [
-            'libpng.gyp:libpng_static_neon',
+          'sources': [
+            '../third_party/externals/libpng/contrib/intel/intel_init.c',
+            '../third_party/externals/libpng/contrib/intel/filter_sse2_intrinsics.c',
           ],
         }],
-      ],
-    },
-    {
-      'target_name': 'libpng_static_neon',
-      'type': 'static_library',
-      'include_dirs': [
-        # Needed for generated pnglibconf.h and pngprefix.h
-        '../third_party/libpng',
-        '../third_party/externals/libpng',
-      ],
-      'dependencies': [
-        'zlib.gyp:zlib',
-      ],
-     'sources': [
-        '../third_party/externals/libpng/arm/arm_init.c',
-        '../third_party/externals/libpng/arm/filter_neon.S',
-        '../third_party/externals/libpng/arm/filter_neon_intrinsics.c',
-      ],
-      'conditions': [
-        ['arm_neon_optional', {
-          'cflags': [
-            '-mfpu=neon',
+        [ '(("arm64" == skia_arch_type) or                   \
+            ("arm" == skia_arch_type and arm_neon == 1)) and \
+           ("ios" != skia_os)', {
+          'defines': [
+            'PNG_ARM_NEON_OPT=2',
+            'PNG_ARM_NEON_IMPLEMENTATION=1',
+          ],
+          'sources': [
+            '../third_party/externals/libpng/arm/arm_init.c',
+            '../third_party/externals/libpng/arm/filter_neon_intrinsics.c',
+          ],
+        }],
+        [ '"ios" == skia_os', {
+          'defines': [
+            'PNG_ARM_NEON_OPT=0',
           ],
         }],
       ],
diff --git a/src/codec/SkPngCodec.cpp b/src/codec/SkPngCodec.cpp
index 974befc..e7035c6 100644
--- a/src/codec/SkPngCodec.cpp
+++ b/src/codec/SkPngCodec.cpp
@@ -12,30 +12,11 @@
 #include "SkMath.h"
 #include "SkOpts.h"
 #include "SkPngCodec.h"
-#include "SkPngFilters.h"
 #include "SkSize.h"
 #include "SkStream.h"
 #include "SkSwizzler.h"
 #include "SkTemplates.h"
 
-// png_struct::read_filter[] was added in libpng 1.5.7.
-#if defined(__SSE2__) && PNG_LIBPNG_VER >= 10507 && !defined(PNG_SKIP_SKIA_OPTS)
-    #include "pngstruct.h"
-
-    extern "C" void sk_png_init_filter_functions_sse2(png_structp png, unsigned int bpp) {
-        if (bpp == 3) {
-            png->read_filter[PNG_FILTER_VALUE_SUB  -1] =   sk_sub3_sse2;
-            png->read_filter[PNG_FILTER_VALUE_AVG  -1] =   sk_avg3_sse2;
-            png->read_filter[PNG_FILTER_VALUE_PAETH-1] = sk_paeth3_sse2;
-        }
-        if (bpp == 4) {
-            png->read_filter[PNG_FILTER_VALUE_SUB  -1] =   sk_sub4_sse2;
-            png->read_filter[PNG_FILTER_VALUE_AVG  -1] =   sk_avg4_sse2;
-            png->read_filter[PNG_FILTER_VALUE_PAETH-1] = sk_paeth4_sse2;
-        }
-    }
-#endif
-
 ///////////////////////////////////////////////////////////////////////////////
 // Callback functions
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/src/codec/SkPngFilters.cpp b/src/codec/SkPngFilters.cpp
deleted file mode 100644
index 2777939..0000000
--- a/src/codec/SkPngFilters.cpp
+++ /dev/null
@@ -1,237 +0,0 @@
-/*
- * Copyright 2016 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "SkPngFilters.h"
-
-// Functions in this file look at most 3 pixels (a,b,c) to predict the fourth (d).
-// They're positioned like this:
-//     prev:  c b
-//     row:   a d
-// The Sub filter predicts d=a, Avg d=(a+b)/2,  and Paeth predicts d to be whichever
-// of a, b, or c is closest to p=a+b-c.  (Up also exists, predicting d=b.)
-
-#if defined(__SSE2__)
-
-    static __m128i load3(const void* p) {
-        uint32_t packed;
-        memcpy(&packed, p, 3);
-        return _mm_cvtsi32_si128(packed);
-    }
-
-    static __m128i load4(const void* p) {
-        return _mm_cvtsi32_si128(*(const int*)p);
-    }
-
-    static void store3(void* p, __m128i v) {
-        uint32_t packed = _mm_cvtsi128_si32(v);
-        memcpy(p, &packed, 3);
-    }
-
-    static void store4(void* p, __m128i v) {
-        *(int*)p = _mm_cvtsi128_si32(v);
-    }
-
-    void sk_sub3_sse2(png_row_infop row_info, uint8_t* row, const uint8_t* prev) {
-        // The Sub filter predicts each pixel as the previous pixel, a.
-        // There is no pixel to the left of the first pixel.  It's encoded directly.
-        // That works with our main loop if we just say that left pixel was zero.
-        __m128i a, d = _mm_setzero_si128();
-
-        int rb = row_info->rowbytes;
-        while (rb > 0) {
-            a = d; d = load3(row);
-            d = _mm_add_epi8(d, a);
-            store3(row, d);
-
-            row += 3;
-            rb  -= 3;
-        }
-    }
-
-    void sk_sub4_sse2(png_row_infop row_info, uint8_t* row, const uint8_t* prev) {
-        // The Sub filter predicts each pixel as the previous pixel, a.
-        // There is no pixel to the left of the first pixel.  It's encoded directly.
-        // That works with our main loop if we just say that left pixel was zero.
-        __m128i a, d = _mm_setzero_si128();
-
-        int rb = row_info->rowbytes;
-        while (rb > 0) {
-            a = d; d = load4(row);
-            d = _mm_add_epi8(d, a);
-            store4(row, d);
-
-            row += 4;
-            rb  -= 4;
-        }
-    }
-
-    void sk_avg3_sse2(png_row_infop row_info, uint8_t* row, const uint8_t* prev) {
-        // The Avg filter predicts each pixel as the (truncated) average of a and b.
-        // There's no pixel to the left of the first pixel.  Luckily, it's
-        // predicted to be half of the pixel above it.  So again, this works
-        // perfectly with our loop if we make sure a starts at zero.
-        const __m128i zero = _mm_setzero_si128();
-        __m128i    b;
-        __m128i a, d = zero;
-
-        int rb = row_info->rowbytes;
-        while (rb > 0) {
-                   b = load3(prev);
-            a = d; d = load3(row );
-
-            // PNG requires a truncating average here, so sadly we can't just use _mm_avg_epu8...
-            __m128i avg = _mm_avg_epu8(a,b);
-            // ...but we can fix it up by subtracting off 1 if it rounded up.
-            avg = _mm_sub_epi8(avg, _mm_and_si128(_mm_xor_si128(a,b), _mm_set1_epi8(1)));
-
-            d = _mm_add_epi8(d, avg);
-            store3(row, d);
-
-            prev += 3;
-            row  += 3;
-            rb   -= 3;
-        }
-    }
-
-    void sk_avg4_sse2(png_row_infop row_info, uint8_t* row, const uint8_t* prev) {
-        // The Avg filter predicts each pixel as the (truncated) average of a and b.
-        // There's no pixel to the left of the first pixel.  Luckily, it's
-        // predicted to be half of the pixel above it.  So again, this works
-        // perfectly with our loop if we make sure a starts at zero.
-        const __m128i zero = _mm_setzero_si128();
-        __m128i    b;
-        __m128i a, d = zero;
-
-        int rb = row_info->rowbytes;
-        while (rb > 0) {
-                   b = load4(prev);
-            a = d; d = load4(row );
-
-            // PNG requires a truncating average here, so sadly we can't just use _mm_avg_epu8...
-            __m128i avg = _mm_avg_epu8(a,b);
-            // ...but we can fix it up by subtracting off 1 if it rounded up.
-            avg = _mm_sub_epi8(avg, _mm_and_si128(_mm_xor_si128(a,b), _mm_set1_epi8(1)));
-
-            d = _mm_add_epi8(d, avg);
-            store4(row, d);
-
-            prev += 4;
-            row  += 4;
-            rb   -= 4;
-        }
-    }
-
-    // Returns |x| for 16-bit lanes.
-    static __m128i abs_i16(__m128i x) {
-    #if defined(__SSSE3__)
-        return _mm_abs_epi16(x);
-    #else
-        // Read this all as, return x<0 ? -x : x.
-        // To negate two's complement, you flip all the bits then add 1.
-        __m128i is_negative = _mm_cmplt_epi16(x, _mm_setzero_si128());
-        x = _mm_xor_si128(x, is_negative);                     // Flip negative lanes.
-        x = _mm_add_epi16(x, _mm_srli_epi16(is_negative, 15)); // +1 to negative lanes, else +0.
-        return x;
-    #endif
-    }
-
-    // Bytewise c ? t : e.
-    static __m128i if_then_else(__m128i c, __m128i t, __m128i e) {
-    #if defined(__SSE4_1__)
-        return _mm_blendv_epi8(e,t,c);
-    #else
-        return _mm_or_si128(_mm_and_si128(c, t), _mm_andnot_si128(c, e));
-    #endif
-    }
-
-    void sk_paeth3_sse2(png_row_infop row_info, uint8_t* row, const uint8_t* prev) {
-        // Paeth tries to predict pixel d using the pixel to the left of it, a,
-        // and two pixels from the previous row, b and c:
-        //   prev: c b
-        //   row:  a d
-        // The Paeth function predicts d to be whichever of a, b, or c is nearest to p=a+b-c.
-
-        // The first pixel has no left context, and so uses an Up filter, p = b.
-        // This works naturally with our main loop's p = a+b-c if we force a and c to zero.
-        // Here we zero b and d, which become c and a respectively at the start of the loop.
-        const __m128i zero = _mm_setzero_si128();
-        __m128i c, b = zero,
-                a, d = zero;
-
-        int rb = row_info->rowbytes;
-        while (rb > 0) {
-            // It's easiest to do this math (particularly, deal with pc) with 16-bit intermediates.
-            c = b; b = _mm_unpacklo_epi8(load3(prev), zero);
-            a = d; d = _mm_unpacklo_epi8(load3(row ), zero);
-            __m128i pa = _mm_sub_epi16(b,c),   // (p-a) == (a+b-c - a) == (b-c)
-                    pb = _mm_sub_epi16(a,c),   // (p-b) == (a+b-c - b) == (a-c)
-                    pc = _mm_add_epi16(pa,pb); // (p-c) == (a+b-c - c) == (a+b-c-c) == (b-c)+(a-c)
-
-            pa = abs_i16(pa);  // |p-a|
-            pb = abs_i16(pb);  // |p-b|
-            pc = abs_i16(pc);  // |p-c|
-
-            __m128i smallest = _mm_min_epi16(pc, _mm_min_epi16(pa, pb));
-
-            // Paeth breaks ties favoring a over b over c.
-            __m128i nearest  = if_then_else(_mm_cmpeq_epi16(smallest, pa), a,
-                               if_then_else(_mm_cmpeq_epi16(smallest, pb), b,
-                                                                           c));
-
-            d = _mm_add_epi8(d, nearest);  // Note `_epi8`: we need addition to wrap modulo 255.
-            store3(row, _mm_packus_epi16(d,d));
-
-            prev += 3;
-            row  += 3;
-            rb   -= 3;
-        }
-    }
-
-    void sk_paeth4_sse2(png_row_infop row_info, uint8_t* row, const uint8_t* prev) {
-        // Paeth tries to predict pixel d using the pixel to the left of it, a,
-        // and two pixels from the previous row, b and c:
-        //   prev: c b
-        //   row:  a d
-        // The Paeth function predicts d to be whichever of a, b, or c is nearest to p=a+b-c.
-
-        // The first pixel has no left context, and so uses an Up filter, p = b.
-        // This works naturally with our main loop's p = a+b-c if we force a and c to zero.
-        // Here we zero b and d, which become c and a respectively at the start of the loop.
-        const __m128i zero = _mm_setzero_si128();
-        __m128i c, b = zero,
-                a, d = zero;
-
-        int rb = row_info->rowbytes;
-        while (rb > 0) {
-            // It's easiest to do this math (particularly, deal with pc) with 16-bit intermediates.
-            c = b; b = _mm_unpacklo_epi8(load4(prev), zero);
-            a = d; d = _mm_unpacklo_epi8(load4(row ), zero);
-            __m128i pa = _mm_sub_epi16(b,c),   // (p-a) == (a+b-c - a) == (b-c)
-                    pb = _mm_sub_epi16(a,c),   // (p-b) == (a+b-c - b) == (a-c)
-                    pc = _mm_add_epi16(pa,pb); // (p-c) == (a+b-c - c) == (a+b-c-c) == (b-c)+(a-c)
-
-            pa = abs_i16(pa);  // |p-a|
-            pb = abs_i16(pb);  // |p-b|
-            pc = abs_i16(pc);  // |p-c|
-
-            __m128i smallest = _mm_min_epi16(pc, _mm_min_epi16(pa, pb));
-
-            // Paeth breaks ties favoring a over b over c.
-            __m128i nearest  = if_then_else(_mm_cmpeq_epi16(smallest, pa), a,
-                               if_then_else(_mm_cmpeq_epi16(smallest, pb), b,
-                                                                           c));
-
-            d = _mm_add_epi8(d, nearest);  // Note `_epi8`: we need addition to wrap modulo 255.
-            store4(row, _mm_packus_epi16(d,d));
-
-            prev += 4;
-            row  += 4;
-            rb   -= 4;
-        }
-    }
-
-#endif
diff --git a/src/codec/SkPngFilters.h b/src/codec/SkPngFilters.h
deleted file mode 100644
index 3270810..0000000
--- a/src/codec/SkPngFilters.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * 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 SkPngFilters_DEFINED
-#define SkPngFilters_DEFINED
-
-#include "png.h"
-#include "SkTypes.h"
-
-// We don't bother specializing Up...
-// it's so simple it's usually already perfectly autovectorized.
-
-// These all require bpp=3 (i.e. RGB).
-void   sk_sub3_sse2(png_row_infop, uint8_t*, const uint8_t*);
-void   sk_avg3_sse2(png_row_infop, uint8_t*, const uint8_t*);
-void sk_paeth3_sse2(png_row_infop, uint8_t*, const uint8_t*);
-
-// These all require bpp=4 (i.e. RGBA).
-void   sk_sub4_sse2(png_row_infop, uint8_t*, const uint8_t*);
-void   sk_avg4_sse2(png_row_infop, uint8_t*, const uint8_t*);
-void sk_paeth4_sse2(png_row_infop, uint8_t*, const uint8_t*);
-
-#endif//SkPngFilterOpts_DEFINED
diff --git a/third_party/libpng/pnglibconf.h b/third_party/libpng/pnglibconf.h
index 15b3530..7204c6d 100644
--- a/third_party/libpng/pnglibconf.h
+++ b/third_party/libpng/pnglibconf.h
@@ -1,8 +1,8 @@
-/* libpng 1.6.19 STANDARD API DEFINITION */
+/* libpng 1.6.22beta03 STANDARD API DEFINITION */
 
 /* pnglibconf.h - library build configuration */
 
-/* Libpng version 1.6.19 - November 12, 2015 */
+/* Libpng version 1.6.22beta03 - February 8, 2016 */
 
 /* Copyright (c) 1998-2015 Glenn Randers-Pehrson */
 
@@ -109,6 +109,7 @@
 #define PNG_SIMPLIFIED_READ_SUPPORTED
 #define PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
 #define PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
+#define PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
 #define PNG_SIMPLIFIED_WRITE_SUPPORTED
 #define PNG_STDIO_SUPPORTED
 #define PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
@@ -211,13 +212,4 @@
 #define PNG_sCAL_PRECISION 5
 #define PNG_sRGB_PROFILE_CHECKS 2
 /* end of settings */
-
-/* custom settings */
-#define PNG_ARM_NEON_API_SUPPORTED
-#define PNG_ARM_NEON_CHECK_SUPPORTED
-#if defined(__SSE2__)
-    #define PNG_FILTER_OPTIMIZATIONS sk_png_init_filter_functions_sse2
-#endif
-/* end of custom settings */
-
 #endif /* PNGLCONF_H */