Improve support for building pdfium_test with Skia in Chromium.

In order to run pdfium_test with Skia-enabled in a Chromium checkout,
there has to be a DiscardableMemoryAllocator. Add support for this,
gated behind the build_with_chromium GN variable.

Bug: chromium:1383711
Change-Id: I1023ca62eda6dac35ba30426efc875050e7d2435
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/103474
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/samples/BUILD.gn b/samples/BUILD.gn
index 7203f05..ccf7ff8 100644
--- a/samples/BUILD.gn
+++ b/samples/BUILD.gn
@@ -25,6 +25,9 @@
   if (enable_callgrind) {
     defines += [ "ENABLE_CALLGRIND" ]
   }
+  if (build_with_chromium) {
+    defines += [ "BUILD_WITH_CHROMIUM" ]
+  }
 }
 
 executable("pdfium_test") {
@@ -62,5 +65,12 @@
   }
   if (pdf_use_skia) {
     deps += [ "//skia" ]
+    if (build_with_chromium) {
+      sources += [
+        "chromium_support/discardable_memory_allocator.cc",
+        "chromium_support/discardable_memory_allocator.h",
+      ]
+      deps += [ "//base/test:test_support" ]
+    }
   }
 }
diff --git a/samples/chromium_support/DEPS b/samples/chromium_support/DEPS
new file mode 100644
index 0000000..75f0926
--- /dev/null
+++ b/samples/chromium_support/DEPS
@@ -0,0 +1,3 @@
+include_rules = [
+  '+base/test/test_discardable_memory_allocator.h',
+]
diff --git a/samples/chromium_support/discardable_memory_allocator.cc b/samples/chromium_support/discardable_memory_allocator.cc
new file mode 100644
index 0000000..f89bb1c
--- /dev/null
+++ b/samples/chromium_support/discardable_memory_allocator.cc
@@ -0,0 +1,16 @@
+// Copyright 2023 The PDFium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "samples/chromium_support/discardable_memory_allocator.h"
+
+#include "base/test/test_discardable_memory_allocator.h"
+
+namespace chromium_support {
+
+void InitializeDiscardableMemoryAllocator() {
+  static base::TestDiscardableMemoryAllocator test_memory_allocator;
+  base::DiscardableMemoryAllocator::SetInstance(&test_memory_allocator);
+}
+
+}  // namespace chromium_support
diff --git a/samples/chromium_support/discardable_memory_allocator.h b/samples/chromium_support/discardable_memory_allocator.h
new file mode 100644
index 0000000..1f91f70
--- /dev/null
+++ b/samples/chromium_support/discardable_memory_allocator.h
@@ -0,0 +1,14 @@
+// Copyright 2023 The PDFium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SAMPLES_CHROMIUM_SUPPORT_DISCARDABLE_MEMORY_ALLOCATOR_H_
+#define SAMPLES_CHROMIUM_SUPPORT_DISCARDABLE_MEMORY_ALLOCATOR_H_
+
+namespace chromium_support {
+
+void InitializeDiscardableMemoryAllocator();
+
+}  // namespace chromium_support
+
+#endif  // SAMPLES_CHROMIUM_SUPPORT_DISCARDABLE_MEMORY_ALLOCATOR_H_
diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc
index 35597ea..2693a56 100644
--- a/samples/pdfium_test.cc
+++ b/samples/pdfium_test.cc
@@ -62,7 +62,11 @@
 #include "third_party/skia/include/core/SkPixmap.h"           // nogncheck
 #include "third_party/skia/include/core/SkRefCnt.h"           // nogncheck
 #include "third_party/skia/include/core/SkSurface.h"          // nogncheck
+
+#ifdef BUILD_WITH_CHROMIUM
+#include "samples/chromium_support/discardable_memory_allocator.h"  // nogncheck
 #endif
+#endif  // PDF_ENABLE_SKIA
 
 #ifdef PDF_ENABLE_V8
 #include "testing/v8_initializer.h"
@@ -1462,14 +1466,23 @@
     return 1;
   }
 
+  const FPDF_RENDERER_TYPE renderer_type =
+      options.use_renderer_type.value_or(GetDefaultRendererType());
+#if defined(PDF_ENABLE_SKIA) && defined(BUILD_WITH_CHROMIUM)
+  if (renderer_type == FPDF_RENDERERTYPE_SKIA) {
+    // Needed to support Chromium's copy of Skia, which uses a
+    // DiscardableMemoryAllocator.
+    chromium_support::InitializeDiscardableMemoryAllocator();
+  }
+#endif
+
   FPDF_LIBRARY_CONFIG config;
   config.version = 4;
   config.m_pUserFontPaths = nullptr;
   config.m_pIsolate = nullptr;
   config.m_v8EmbedderSlot = 0;
   config.m_pPlatform = nullptr;
-  config.m_RendererType =
-      options.use_renderer_type.value_or(GetDefaultRendererType());
+  config.m_RendererType = renderer_type;
 
   std::function<void()> idler = []() {};
 #ifdef PDF_ENABLE_V8