Improve NaCl support

- Add nacl_make script to build Skia targets for NaCl using gyp
- Add nacl_interface for command-line apps
- Add nacl_sample as front-end for SampleApp
- Add freetype to DEPS
- Various gyp tweaks for NaCl

TODO:
- Implement GL interface
- Implement font host
- Fix plumbing so that SampleApp works properly
Review URL: https://codereview.appspot.com/6671044

git-svn-id: http://skia.googlecode.com/svn/trunk@6245 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/DEPS b/DEPS
index 016213e..55a6401 100644
--- a/DEPS
+++ b/DEPS
@@ -9,6 +9,7 @@
 #
 deps = {
   "third_party/externals/angle" : "http://angleproject.googlecode.com/svn/trunk@1268",
+  "third_party/externals/freetype" : "https://android.googlesource.com/platform/external/freetype.git",
   "third_party/externals/gyp" : "http://gyp.googlecode.com/svn/trunk@1517",
   "third_party/externals/libjpeg" : "http://src.chromium.org/svn/trunk/src/third_party/libjpeg@125399",
   "third_party/externals/jsoncpp" : "http://src.chromium.org/svn/trunk/src/third_party/jsoncpp@125399",
diff --git a/bench/benchmain.cpp b/bench/benchmain.cpp
index 6f33952..80b7871 100644
--- a/bench/benchmain.cpp
+++ b/bench/benchmain.cpp
@@ -908,7 +908,7 @@
     return 0;
 }
 
-#if !defined SK_BUILD_FOR_IOS
+#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
 int main(int argc, char * const argv[]) {
     return tool_main(argc, (char**) argv);
 }
diff --git a/gm/gmmain.cpp b/gm/gmmain.cpp
index 54bbd8b..a4c88b1 100644
--- a/gm/gmmain.cpp
+++ b/gm/gmmain.cpp
@@ -1301,7 +1301,7 @@
     return (0 == testsFailed) ? 0 : -1;
 }
 
-#if !defined SK_BUILD_FOR_IOS
+#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
 int main(int argc, char * const argv[]) {
     return tool_main(argc, (char**) argv);
 }
diff --git a/gyp/SampleApp.gyp b/gyp/SampleApp.gyp
index 3b0be1b..5668428 100644
--- a/gyp/SampleApp.gyp
+++ b/gyp/SampleApp.gyp
@@ -290,6 +290,11 @@
             '../src/gpu', # To pull gl/GrGLUtil.h
           ],
         }],
+        [ 'skia_os == "nacl"', {
+          'sources': [
+            '../../nacl/src/nacl_sample.cpp',
+          ],
+        }],
       ],
       'msvs_settings': {
         'VCLinkerTool': {
diff --git a/gyp/apptype_console.gypi b/gyp/apptype_console.gypi
index 80e925d..a306193 100644
--- a/gyp/apptype_console.gypi
+++ b/gyp/apptype_console.gypi
@@ -17,6 +17,11 @@
           'android_deps.gyp:Android_EntryPoint',
         ],
       }],
+      [ 'skia_os == "nacl"', {
+        'dependencies': [
+          'nacl.gyp:nacl_interface',
+        ],
+      }],
       ['skia_os == "ios"', {
         'target_conditions': [
           ['_type == "executable"', {
diff --git a/gyp/common.gypi b/gyp/common.gypi
index b620226..7dc27da 100644
--- a/gyp/common.gypi
+++ b/gyp/common.gypi
@@ -21,15 +21,21 @@
     # situations, like building for iOS on a Mac.
     'variables': {
       'conditions': [
-        ['skia_os != OS and not (skia_os == "ios" and OS == "mac")',
-          {'error': '<!(Cannot build with skia_os=<(skia_os) on OS=<(OS))'}],
-        ['skia_mesa and skia_os not in ["mac", "linux"]',
-          {'error': '<!(skia_mesa=1 only supported with skia_os="mac" or "linux".)'}],
-        ['skia_angle and not skia_os == "win"',
-          {'error': '<!(skia_angle=1 only supported with skia_os="win".)'
+        [ 'skia_os != OS and not ((skia_os == "ios" and OS == "mac") or \
+                                  (skia_os == "nacl" and OS == "linux"))', {
+          'error': '<!(Cannot build with skia_os=<(skia_os) on OS=<(OS))',
         }],
-        ['skia_arch_width != 32 and skia_arch_width != 64',
-          {'error': '<!(skia_arch_width can only be 32 or 64 bits not <(skia_arch_width) bits)'
+        [ 'skia_mesa and skia_os not in ["mac", "linux"]', {
+          'error': '<!(skia_mesa=1 only supported with skia_os="mac" or "linux".)',
+        }],
+        [ 'skia_angle and not skia_os == "win"', {
+          'error': '<!(skia_angle=1 only supported with skia_os="win".)',
+        }],
+        [ 'skia_arch_width != 32 and skia_arch_width != 64', {
+          'error': '<!(skia_arch_width can only be 32 or 64 bits not <(skia_arch_width) bits)',
+        }],
+        [ 'skia_os == "nacl" and OS != "linux"', {
+          'error': '<!(Skia NaCl build only currently supported on Linux.)',
         }],
       ],
     },
diff --git a/gyp/common_conditions.gypi b/gyp/common_conditions.gypi
index ac2defb..0bbd660 100644
--- a/gyp/common_conditions.gypi
+++ b/gyp/common_conditions.gypi
@@ -98,7 +98,7 @@
       },
     ],
 
-    ['skia_os in ["linux", "freebsd", "openbsd", "solaris"]',
+    ['skia_os in ["linux", "freebsd", "openbsd", "solaris", "nacl"]',
       {
         'defines': [
           'SK_SAMPLES_FOR_X',
@@ -141,9 +141,23 @@
               '-m32',
             ],
           }],
-        ],
-        'include_dirs' : [
-          '/usr/include/freetype2',
+          [ 'skia_os == "nacl"', {
+            'defines': [
+              'SK_BUILD_FOR_NACL',
+            ],
+            'link_settings': {
+              'libraries': [
+                '-lppapi',
+                '-lppapi_cpp',
+                '-lnosys',
+                '-pthread',
+              ],
+            },
+          }, { # skia_os != "nacl"
+            'include_dirs' : [
+              '/usr/include/freetype2',
+            ],
+          }],
         ],
       },
     ],
@@ -321,7 +335,7 @@
     # static initializers if we're using a pthread-compatible thread interface.
     [ 'skia_os != "win"', {
       'defines': [
-        'SK_USE_POSIX_THREADS'
+        'SK_USE_POSIX_THREADS',
       ],
     }],
   ], # end 'conditions'
diff --git a/gyp/common_variables.gypi b/gyp/common_variables.gypi
index 6acf5bc..974e93b 100644
--- a/gyp/common_variables.gypi
+++ b/gyp/common_variables.gypi
@@ -84,7 +84,6 @@
       'skia_texture_cache_mb_limit%': 0,
       'skia_angle%': 0,
       'skia_directwrite%': 0,
-      'skia_nacl%': 0,
       'skia_gpu%': 1,
       'skia_osx_sdkroot%': 'macosx',
     },
@@ -103,7 +102,6 @@
     'skia_arch_width%': '<(skia_arch_width)',
     'skia_arch_type%': '<(skia_arch_type)',
     'skia_directwrite%': '<(skia_directwrite)',
-    'skia_nacl%': '<(skia_nacl)',
     'skia_gpu%': '<(skia_gpu)',
     'skia_osx_sdkroot%': '<(skia_osx_sdkroot)',
     'skia_static_initializers%': '<(skia_static_initializers)',
diff --git a/gyp/core.gyp b/gyp/core.gyp
index 228fd06..14b14b0 100644
--- a/gyp/core.gyp
+++ b/gyp/core.gyp
@@ -83,7 +83,7 @@
         }],
         [ 'skia_os == "android"', {
           'dependencies': [
-             'android_deps.gyp:ft2',
+             'freetype.gyp:freetype',
           ],
         }],
         [ 'skia_os == "android" and skia_arch_type == "arm" and armv7 == 1', {
diff --git a/gyp/everything.gyp b/gyp/everything.gyp
index 2cecd92..e1d516d 100644
--- a/gyp/everything.gyp
+++ b/gyp/everything.gyp
@@ -14,7 +14,7 @@
       'type': 'none',
       'dependencies': ['most.gyp:most'],
       'conditions': [
-        ['skia_os == "ios" or skia_os == "android" or (skia_os == "mac" and skia_arch_width == 32)', {
+        ['skia_os in ("ios", "android", "nacl") or (skia_os == "mac" and skia_arch_width == 32)', {
           # debugger is not supported on this platform
         }, {
           'dependencies': [ 'debugger.gyp:debugger' ],
diff --git a/gyp/freetype.gyp b/gyp/freetype.gyp
index 4a885a1..db61742 100644
--- a/gyp/freetype.gyp
+++ b/gyp/freetype.gyp
@@ -1,60 +1,82 @@
 {
   'targets': [
     {
-      'target_name': 'skfreetype',
+      'target_name': 'freetype',
       'type': 'static_library',
       'sources': [
-        '../third_party/freetype/src/base/ftbbox.c',
-        '../third_party/freetype/src/base/ftbitmap.c',
-        '../third_party/freetype/src/base/ftglyph.c',
-        '../third_party/freetype/src/base/ftlcdfil.c',
-        '../third_party/freetype/src/base/ftstroke.c',
-        '../third_party/freetype/src/base/ftxf86.c',
-        '../third_party/freetype/src/base/ftbase.c',
-        '../third_party/freetype/src/base/ftsystem.c',
-        '../third_party/freetype/src/base/ftinit.c',
-        '../third_party/freetype/src/base/ftgasp.c',
-        '../third_party/freetype/src/base/ftfstype.c',
-        '../third_party/freetype/src/raster/raster.c',
-        '../third_party/freetype/src/sfnt/sfnt.c',
-        '../third_party/freetype/src/smooth/smooth.c',
-        '../third_party/freetype/src/autofit/autofit.c',
-        '../third_party/freetype/src/truetype/truetype.c',
-        '../third_party/freetype/src/cff/cff.c',
-        '../third_party/freetype/src/psnames/psnames.c',
-        '../third_party/freetype/src/pshinter/pshinter.c',
+        # base components (required)
+        '../third_party/externals/freetype/src/base/ftsystem.c',
+        '../third_party/externals/freetype/src/base/ftinit.c',
+        '../third_party/externals/freetype/src/base/ftdebug.c',
+        '../third_party/externals/freetype/src/base/ftbase.c',
 
-# added for linker
-        '../third_party/freetype/src/lzw/ftlzw.c',
-        '../third_party/freetype/src/gzip/ftgzip.c',
-        '../third_party/freetype/src/cid/type1cid.c',
-        '../third_party/freetype/src/bdf/bdf.c',
-        '../third_party/freetype/src/psaux/psaux.c',
-        '../third_party/freetype/src/pcf/pcf.c',
-        '../third_party/freetype/src/pfr/pfr.c',
-        '../third_party/freetype/src/type1/type1.c',
-        '../third_party/freetype/src/type42/type42.c',
-        '../third_party/freetype/src/winfonts/winfnt.c',
+        '../third_party/externals/freetype/src/base/ftbbox.c',       # recommended, see <freetype/ftbbox.h>
+        '../third_party/externals/freetype/src/base/ftglyph.c',      # recommended, see <freetype/ftglyph.h>
+
+        '../third_party/externals/freetype/src/base/ftbitmap.c',     # optional, see <freetype/ftbitmap.h>
+        '../third_party/externals/freetype/src/base/ftfstype.c',     # optional
+        '../third_party/externals/freetype/src/base/ftgasp.c',       # optional, see <freetype/ftgasp.h>
+        '../third_party/externals/freetype/src/base/ftlcdfil.c',     # optional, see <freetype/ftlcdfil.h>
+        '../third_party/externals/freetype/src/base/ftmm.c',         # optional, see <freetype/ftmm.h>
+        '../third_party/externals/freetype/src/base/ftpatent.c',     # optional
+        '../third_party/externals/freetype/src/base/ftstroke.c',     # optional, see <freetype/ftstroke.h>
+        '../third_party/externals/freetype/src/base/ftsynth.c',      # optional, see <freetype/ftsynth.h>
+        '../third_party/externals/freetype/src/base/fttype1.c',      # optional, see <freetype/t1tables.h>
+        '../third_party/externals/freetype/src/base/ftwinfnt.c',     # optional, see <freetype/ftwinfnt.h>
+        '../third_party/externals/freetype/src/base/ftxf86.c',       # optional, see <freetype/ftxf86.h>
+
+        # font drivers (optional; at least one is needed)
+        '../third_party/externals/freetype/src/cff/cff.c',           # CFF/OpenType font driver
+        '../third_party/externals/freetype/src/sfnt/sfnt.c',         # SFNT files support (TrueType & OpenType)
+        '../third_party/externals/freetype/src/truetype/truetype.c', # TrueType font driver
+
+        # rasterizers (optional; at least one is needed for vector formats)
+        '../third_party/externals/freetype/src/raster/raster.c',     # monochrome rasterizer
+        '../third_party/externals/freetype/src/smooth/smooth.c',     # anti-aliasing rasterizer
+
+        # auxiliary modules (optional)
+        '../third_party/externals/freetype/src/autofit/autofit.c',   # auto hinting module
+        '../third_party/externals/freetype/src/psaux/psaux.c',       # PostScript Type 1 parsing
+        '../third_party/externals/freetype/src/pshinter/pshinter.c', # PS hinting module
+        '../third_party/externals/freetype/src/psnames/psnames.c',   # PostScript glyph names support
       ],
       'include_dirs': [
-        '../third_party/freetype/internal',
-        '../third_party/freetype/builds',
-        '../third_party/freetype/include',
-        '../third_party/freetype',
+        '../third_party/externals/freetype/internal',
+        '../third_party/externals/freetype/builds',
+        '../third_party/externals/freetype/include',
+        '../third_party/externals/freetype',
       ],
       'cflags': [
-        '-W',
-        '-Wall',
-        '-fPIC',
-        '-DPIC',
-        '-DDARWIN_NO_CARBON',
         '-DFT2_BUILD_LIBRARY',
       ],
       'direct_dependent_settings': {
         'include_dirs': [
-          '../third_party/freetype/include',  # For ft2build.h
+          '../third_party/externals/freetype/include',
         ],
       },
+      'conditions': [
+        [ 'skia_os == "mac"', {
+          'sources': [
+            '../third_party/externals/freetype/src/base/ftmac.c',        # only on the Macintosh
+          ],
+        }],
+        [ 'skia_os == "android"', {
+          # These flags are used by the Android OS.  They are probably overkill
+          # for Skia, but we add them for consistency.
+          'cflags': [
+            '-W',
+            '-Wall',
+            '-fPIC',
+            '-DPIC',
+            '-DDARWIN_NO_CARBON',
+            '-DFT2_BUILD_LIBRARY',
+            '-O2',
+          ],
+          'cflags!': [
+            '-fno-rtti', # supress warnings about invalid option of non-C++ code
+          ],
+        }],
+      ],
     },
   ],
 }
diff --git a/gyp/gpu.gyp b/gyp/gpu.gyp
index 9cb0698..d5223e4 100644
--- a/gyp/gpu.gyp
+++ b/gyp/gpu.gyp
@@ -21,6 +21,10 @@
         'sources/': [ ['exclude', '_android.(h|cpp)$'],
         ],
       }],
+      ['skia_os != "nacl"', {
+        'sources/': [ ['exclude', '_nacl.(h|cpp)$'],
+        ],
+      }],
       [ 'skia_os == "android"', {
         'defines': [
           'GR_ANDROID_BUILD=1',
@@ -209,10 +213,18 @@
           'link_settings': {
             'libraries': [
               '-lGL',
+              '-lGLU',
               '-lX11',
             ],
           },
         }],
+        [ 'skia_os == "nacl"', {
+          'link_settings': {
+            'libraries': [
+              '-lppapi_gles2',
+            ],
+          },
+        }],
         [ 'skia_mesa and skia_os == "linux"', {
           'link_settings': {
             'libraries': [
diff --git a/gyp/gpu.gypi b/gyp/gpu.gypi
index 0c45aa5..67cf603 100644
--- a/gyp/gpu.gypi
+++ b/gyp/gpu.gypi
@@ -238,6 +238,7 @@
     'skgr_native_gl_sources': [
       '<(skia_include_path)/gpu/gl/SkNativeGLContext.h',
       '<(skia_src_path)/gpu/gl/mac/SkNativeGLContext_mac.cpp',
+      '<(skia_src_path)/gpu/gl/nacl/SkNativeGLContext_nacl.cpp',
       '<(skia_src_path)/gpu/gl/win/SkNativeGLContext_win.cpp',
       '<(skia_src_path)/gpu/gl/unix/SkNativeGLContext_unix.cpp',
       '<(skia_src_path)/gpu/gl/android/SkNativeGLContext_android.cpp',
diff --git a/gyp/images.gyp b/gyp/images.gyp
index 2989afe..daf4c5f 100644
--- a/gyp/images.gyp
+++ b/gyp/images.gyp
@@ -89,7 +89,7 @@
             '../src/ports/SkImageDecoder_CG.cpp',
           ],
         }],
-        [ 'skia_os in ["linux", "freebsd", "openbsd", "solaris"]', {
+        [ 'skia_os in ["linux", "freebsd", "openbsd", "solaris", "nacl"]', {
           'sources!': [
             '../src/images/SkImageDecoder_libgif.cpp',
             '../src/images/SkMovie_gif.cpp',
diff --git a/gyp/nacl.gyp b/gyp/nacl.gyp
new file mode 100644
index 0000000..95b2a82
--- /dev/null
+++ b/gyp/nacl.gyp
@@ -0,0 +1,19 @@
+# Common entry point for all Skia executables running in NaCl
+{
+  'targets': [
+    {
+      'target_name': 'nacl_interface',
+      'type': 'static_library',
+      'dependencies': [
+        'skia_base_libs.gyp:skia_base_libs',
+      ],
+      'include_dirs': [
+        # For SkThreadUtils.h
+        '../src/utils',
+      ],
+      'sources': [
+        '../../nacl/src/nacl_interface.cpp',
+      ],
+    },
+  ],
+}
\ No newline at end of file
diff --git a/gyp/opts.gyp b/gyp/opts.gyp
index b8d7e22..cd892e8 100644
--- a/gyp/opts.gyp
+++ b/gyp/opts.gyp
@@ -32,7 +32,7 @@
       'conditions': [
         [ 'skia_arch_type == "x86" and skia_os != "ios"', {
           'conditions': [
-            [ 'skia_os in ["linux", "freebsd", "openbsd", "solaris"]', {
+            [ 'skia_os in ["linux", "freebsd", "openbsd", "solaris", "nacl"]', {
               'cflags': [
                 '-msse2',
               ],
@@ -108,7 +108,7 @@
         '../src/core',
       ],
       'conditions': [
-        [ 'skia_os in ["linux", "freebsd", "openbsd", "solaris"]', {
+        [ 'skia_os in ["linux", "freebsd", "openbsd", "solaris", "nacl"]', {
           'cflags': [
             '-mssse3',
           ],
diff --git a/gyp/ports.gyp b/gyp/ports.gyp
index f743ad4..6403693 100644
--- a/gyp/ports.gyp
+++ b/gyp/ports.gyp
@@ -20,6 +20,7 @@
         '../src/utils',
       ],
       'sources': [
+        '../src/ports/SkDebug_nacl.cpp',
         '../src/ports/SkDebug_stdio.cpp',
         '../src/ports/SkDebug_win.cpp',
         '../src/ports/SkFontDescriptor.h',
@@ -39,36 +40,51 @@
       ],
       'conditions': [
         [ 'skia_os in ["linux", "freebsd", "openbsd", "solaris"]', {
-          'conditions': [
-            [ 'skia_nacl', {
-              'defines': [
-                'SK_CAN_USE_DLOPEN=0',
-              ],
-              'sources': [
-                '../src/ports/SkFontHost_none.cpp',
-              ],
-            }, {
-              'defines': [
-                #The font host requires at least FreeType 2.3.0 at runtime.
-                'SK_FONTHOST_FREETYPE_RUNTIME_VERSION=0x020300',
-                'SK_CAN_USE_DLOPEN=1',
-              ],
-              'sources': [
-                '../src/ports/SkFontHost_FreeType.cpp',
-                '../src/ports/SkFontHost_FreeType_common.cpp',
-                '../src/ports/SkFontHost_linux.cpp',
-              ],
-              'link_settings': {
-                'libraries': [
-                  '-lfreetype',
-                  '-ldl',
-                ],
-              },
-            }],
+          'defines': [
+            #The font host requires at least FreeType 2.3.0 at runtime.
+            'SK_FONTHOST_FREETYPE_RUNTIME_VERSION=0x020300',\
+            'SK_CAN_USE_DLOPEN=1',
+          ],
+          'link_settings': {
+            'libraries': [
+              '-lfreetype',
+              '-ldl',
+            ],
+          },
+          'sources': [
+            '../src/ports/SkFontHost_FreeType.cpp',
+            '../src/ports/SkFontHost_FreeType_common.cpp',
+            '../src/ports/SkFontHost_linux.cpp',
+            '../src/ports/SkThread_pthread.cpp',
+          ],
+        }],
+        [ 'skia_os == "nacl"', {
+          'dependencies': [
+            # On other OS, we can dynamically link against freetype.  For nacl,
+            # we have to include our own version since the naclports version is
+            # too old (<0x020300) to provide the functionality we need.
+            'freetype.gyp:freetype',
+          ],
+          'export_dependent_settings': [
+            'freetype.gyp:freetype',
+          ],
+          'defines': [
+            # We use Android's repo, which provides at least FreeType 2.4.0
+            'SK_FONTHOST_FREETYPE_RUNTIME_VERSION=0x020400',\
           ],
           'sources': [
+            '../src/ports/SkFontHost_FreeType.cpp',
+            '../src/ports/SkFontHost_FreeType_common.cpp',
+            '../src/ports/SkFontHost_linux.cpp',
             '../src/ports/SkThread_pthread.cpp',
           ],
+          'sources!': [
+            '../src/ports/SkDebug_stdio.cpp',
+          ],
+        }, {
+          'sources!': [
+            '../src/ports/SkDebug_nacl.cpp',
+          ],
         }],
         [ 'skia_os == "mac"', {
           'include_dirs': [
@@ -150,7 +166,7 @@
             #TODO: include the ports/SkImageRef_ashmem.cpp for non-NDK builds
           ],
           'dependencies': [
-             'android_deps.gyp:ft2',
+             'freetype.gyp:freetype',
              'android_deps.gyp:expat',
           ],
         }],
diff --git a/gyp/utils.gyp b/gyp/utils.gyp
index 97f25b1..aa3f4a3 100644
--- a/gyp/utils.gyp
+++ b/gyp/utils.gyp
@@ -129,12 +129,6 @@
           ],
         }],
         [ 'skia_os in ["linux", "freebsd", "openbsd", "solaris"]', {
-          'link_settings': {
-            'libraries': [
-              '-lGL',
-              '-lGLU',
-            ],
-          },
           'sources!': [
             '../src/utils/SkThreadUtils_pthread_other.cpp',
           ],
@@ -176,7 +170,7 @@
             '../src/utils/win/SkIStream.cpp',
           ],
         }],
-        [ 'skia_nacl == 1', {
+        [ 'skia_os == "nacl"', {
           'sources': [
             '../src/utils/SkThreadUtils_pthread_other.cpp',
           ],
diff --git a/gyp/views.gyp b/gyp/views.gyp
index 80be87e..5f58a87 100644
--- a/gyp/views.gyp
+++ b/gyp/views.gyp
@@ -32,6 +32,7 @@
         '../include/views/SkKey.h',
         '../include/views/SkOSMenu.h',
         '../include/views/SkOSWindow_Mac.h',
+        '../include/views/SkOSWindow_NaCl.h',
         '../include/views/SkOSWindow_SDL.h',
         '../include/views/SkOSWindow_Unix.h',
         '../include/views/SkOSWindow_Win.h',
@@ -111,6 +112,17 @@
             '../src/views/win/skia_win.cpp',
           ],
         }],
+        [ 'skia_os == "nacl"', {
+          'sources!': [
+            '../src/views/unix/SkOSWindow_Unix.cpp',
+            '../src/views/unix/keysym2ucs.c',
+            '../src/views/unix/skia_unix.cpp',
+          ],
+        }, {
+          'sources!': [
+            '../src/views/nacl/SkOSWindow_NaCl.cpp',
+          ],
+        }],
         [ 'skia_gpu == 1', {
           'include_dirs': [
             '../include/gpu',
diff --git a/gyp/xml.gyp b/gyp/xml.gyp
index b7fdb83..5cf0659 100644
--- a/gyp/xml.gyp
+++ b/gyp/xml.gyp
@@ -32,7 +32,7 @@
           '../src/xml/SkXMLPullParser.cpp', #if 0 around class decl in header
       ],
       'conditions': [
-        [ 'skia_os in ["win", "mac", "linux", "freebsd", "openbsd", "solaris", "android", "ios"]', {
+        [ 'skia_os in ["win", "mac", "linux", "freebsd", "openbsd", "solaris", "android", "ios", "nacl"]', {
           'sources!': [
             # no jsapi.h by default on system
             '../include/xml/SkJS.h',
diff --git a/gyp/zlib.gyp b/gyp/zlib.gyp
index 864a059..fcd0ac3 100644
--- a/gyp/zlib.gyp
+++ b/gyp/zlib.gyp
@@ -29,7 +29,7 @@
           },
           'defines': [ 'SK_ZLIB_INCLUDE=<zlib.h>', ],
         }],
-        [ 'skia_os in ["linux", "freebsd", "openbsd", "solaris", "android"]', {
+        [ 'skia_os in ["linux", "freebsd", "openbsd", "solaris", "android", "nacl"]', {
           'link_settings': { 'libraries': [ '-lz', ], },
           'defines': [ 'SK_ZLIB_INCLUDE=<zlib.h>', ],
         }],
diff --git a/include/gpu/gl/SkNativeGLContext.h b/include/gpu/gl/SkNativeGLContext.h
index 52118da..93744e7 100644
--- a/include/gpu/gl/SkNativeGLContext.h
+++ b/include/gpu/gl/SkNativeGLContext.h
@@ -12,7 +12,7 @@
 
 #if defined(SK_BUILD_FOR_MAC)
     #include <AGL/agl.h>
-#elif defined(SK_BUILD_FOR_ANDROID)
+#elif defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_NACL)
     #include <GLES2/gl2.h>
     #include <EGL/egl.h>
 #elif defined(SK_BUILD_FOR_UNIX)
@@ -39,6 +39,10 @@
     private:
     #if defined(SK_BUILD_FOR_MAC)
         AGLContext fOldAGLContext;
+    #elif defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_NACL)
+        EGLContext fOldEGLContext;
+        EGLDisplay fOldDisplay;
+        EGLSurface fOldSurface;
     #elif defined(SK_BUILD_FOR_UNIX)
         GLXContext fOldGLXContext;
         Display* fOldDisplay;
@@ -46,10 +50,7 @@
     #elif defined(SK_BUILD_FOR_WIN32)
         HDC fOldHDC;
         HGLRC fOldHGLRC;
-    #elif defined(SK_BUILD_FOR_ANDROID)
-        EGLContext fOldEGLContext;
-        EGLDisplay fOldDisplay;
-        EGLSurface fOldSurface;
+
     #elif defined(SK_BUILD_FOR_IOS)
         void* fEAGLContext;
     #endif
@@ -62,6 +63,10 @@
 private:
 #if defined(SK_BUILD_FOR_MAC)
     AGLContext fContext;
+#elif defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_NACL)
+    EGLContext fContext;
+    EGLDisplay fDisplay;
+    EGLSurface fSurface;
 #elif defined(SK_BUILD_FOR_UNIX)
     GLXContext fContext;
     Display* fDisplay;
@@ -72,10 +77,6 @@
     HDC fDeviceContext;
     HGLRC fGlRenderContext;
     static ATOM gWC;
-#elif defined(SK_BUILD_FOR_ANDROID)
-    EGLContext fContext;
-    EGLDisplay fDisplay;
-    EGLSurface fSurface;
 #elif defined(SK_BUILD_FOR_IOS)
     void* fEAGLContext;
 #endif
diff --git a/include/views/SkOSWindow_NaCl.h b/include/views/SkOSWindow_NaCl.h
new file mode 100644
index 0000000..8c4ddc6
--- /dev/null
+++ b/include/views/SkOSWindow_NaCl.h
@@ -0,0 +1,46 @@
+
+/*
+ * Copyright 2012 Skia
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+
+#ifndef SkOSWindow_NaCl_DEFINED
+#define SkOSWindow_NaCl_DEFINED
+
+#include "SkWindow.h"
+
+class SkIRect;
+
+class SkOSWindow : public SkWindow {
+public:
+    SkOSWindow(void*) {}
+    ~SkOSWindow() {}
+
+    enum SkBackEndTypes {
+        kNone_BackEndType,
+        kNativeGL_BackEndType,
+    };
+
+    bool attach(SkBackEndTypes /* attachType */, int /* msaaSampleCount */) {
+        return true;
+    }
+    void detach() {}
+    void present() {}
+
+    virtual void onPDFSaved(const char title[], const char desc[],
+        const char path[]);
+
+protected:
+    // overrides from SkWindow
+    virtual void onHandleInval(const SkIRect&);
+    virtual void onSetTitle(const char title[]);
+
+private:
+    typedef SkWindow INHERITED;
+};
+
+#endif
+
diff --git a/include/views/SkWindow.h b/include/views/SkWindow.h
index f1d3881..ca48e27 100644
--- a/include/views/SkWindow.h
+++ b/include/views/SkWindow.h
@@ -102,7 +102,9 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
-#if defined(SK_BUILD_FOR_MAC)
+#if defined(SK_BUILD_FOR_NACL)
+    #include "SkOSWindow_NaCl.h"
+#elif defined(SK_BUILD_FOR_MAC)
     #include "SkOSWindow_Mac.h"
 #elif defined(SK_BUILD_FOR_WIN)
     #include "SkOSWindow_Win.h"
diff --git a/src/gpu/gl/nacl/SkNativeGLContext_nacl.cpp b/src/gpu/gl/nacl/SkNativeGLContext_nacl.cpp
new file mode 100644
index 0000000..59ed2bf
--- /dev/null
+++ b/src/gpu/gl/nacl/SkNativeGLContext_nacl.cpp
@@ -0,0 +1,34 @@
+
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+#include "gl/SkNativeGLContext.h"
+
+SkNativeGLContext::AutoContextRestore::AutoContextRestore() {
+}
+
+SkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
+}
+
+SkNativeGLContext::SkNativeGLContext()
+    : fContext(NULL)
+    , fDisplay(NULL)
+{
+}
+
+SkNativeGLContext::~SkNativeGLContext() {
+    this->destroyGLContext();
+}
+
+void SkNativeGLContext::destroyGLContext() {
+}
+
+const GrGLInterface* SkNativeGLContext::createGLContext() {
+    return NULL;
+}
+
+void SkNativeGLContext::makeCurrent() const {
+}
diff --git a/src/ports/SkDebug_nacl.cpp b/src/ports/SkDebug_nacl.cpp
new file mode 100644
index 0000000..b1d260f
--- /dev/null
+++ b/src/ports/SkDebug_nacl.cpp
@@ -0,0 +1,39 @@
+
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+
+
+#include "SkTypes.h"
+
+static const size_t kBufferSize = 2048;
+
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "ppapi/cpp/instance.h"
+#include "ppapi/cpp/var.h"
+
+extern pp::Instance* gPluginInstance;
+
+namespace {
+static const char* kLogPrefix = "SkDebugf:";
+}
+
+void SkDebugf(const char format[], ...) {
+    if (gPluginInstance) {
+        char buffer[kBufferSize + 1];
+        va_list args;
+        va_start(args, format);
+        sprintf(buffer, kLogPrefix);
+        vsnprintf(buffer + strlen(kLogPrefix), kBufferSize, format, args);
+        va_end(args);
+        pp::Var msg = pp::Var(buffer);
+        gPluginInstance->PostMessage(msg);
+    }
+}
+
diff --git a/tests/skia_test.cpp b/tests/skia_test.cpp
index f884a64..560abb2 100644
--- a/tests/skia_test.cpp
+++ b/tests/skia_test.cpp
@@ -190,7 +190,7 @@
     return (failCount == 0) ? 0 : 1;
 }
 
-#if !defined SK_BUILD_FOR_IOS
+#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
 int main(int argc, char * const argv[]) {
     return tool_main(argc, (char**) argv);
 }