[aapt2] Add a compression control option to 'compile'

aapt2 recompresses all PNGs it compiles, and by default it
uses the best (and slowest) zlib compression level, 9.

This change adds a command line option --png-compression-level
to control that, so it's possible to set it to something faster
e.g. 6 which is usually 5x faster and around 101% of the size
of level 9 PNGs

Test: manual, compile Framework
Flag: EXEMPT tool
Change-Id: Ibe5f47b82e1c13be0a4ffff0e07e675871abc956
diff --git a/tools/aapt2/cmd/Compile.cpp b/tools/aapt2/cmd/Compile.cpp
index 52372fa..a5e18d35 100644
--- a/tools/aapt2/cmd/Compile.cpp
+++ b/tools/aapt2/cmd/Compile.cpp
@@ -605,8 +605,9 @@
     }
 
     // Write the crunched PNG.
-    if (!android::WritePng(image.get(), nine_patch.get(), &crunched_png_buffer_out, {},
-                           &source_diag, context->IsVerbose())) {
+    if (!android::WritePng(image.get(), nine_patch.get(), &crunched_png_buffer_out,
+                           {.compression_level = options.png_compression_level_int}, &source_diag,
+                           context->IsVerbose())) {
       return false;
     }
 
@@ -924,6 +925,19 @@
     }
   }
 
+  if (!options_.png_compression_level) {
+    options_.png_compression_level_int = 9;
+  } else {
+    if (options_.png_compression_level->size() != 1 ||
+        options_.png_compression_level->front() < '0' ||
+        options_.png_compression_level->front() > '9') {
+      context.GetDiagnostics()->Error(
+          android::DiagMessage() << "PNG compression level should be a number in [0..9] range");
+      return 1;
+    }
+    options_.png_compression_level_int = options_.png_compression_level->front() - '0';
+  }
+
   return Compile(&context, file_collection.get(), archive_writer.get(), options_);
 }