[graphite] Add basic logging.

Just a placeholder for now until we have a better sense of what we want.

Bug: skia:12817
Change-Id: I75a76cb990c5a63bd2eefb5916ead3bff6742739
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/494036
Reviewed-by: Michael Ludwig <michaelludwig@google.com>
Commit-Queue: Jim Van Verth <jvanverth@google.com>
diff --git a/experimental/graphite/src/Device.cpp b/experimental/graphite/src/Device.cpp
index 9c675ab..2f552e1 100644
--- a/experimental/graphite/src/Device.cpp
+++ b/experimental/graphite/src/Device.cpp
@@ -18,6 +18,7 @@
 #include "experimental/graphite/src/DrawContext.h"
 #include "experimental/graphite/src/DrawList.h"
 #include "experimental/graphite/src/Gpu.h"
+#include "experimental/graphite/src/Log.h"
 #include "experimental/graphite/src/ResourceProvider.h"
 #include "experimental/graphite/src/Texture.h"
 #include "experimental/graphite/src/TextureProxy.h"
@@ -317,8 +318,7 @@
     Transform localToDevice(this->localToDevice44());
     if (!localToDevice.valid()) {
         // If the transform is not invertible or not finite then drawing isn't well defined.
-        // TBD: This warning should go through the general purpose graphite logging system
-        SkDebugf("[graphite] WARNING - Skipping draw with non-invertible/non-finite transform.\n");
+        SKGPU_LOG_W("Skipping draw with non-invertible/non-finite transform.");
         return;
     }
 
@@ -337,8 +337,7 @@
             this->drawShape(Shape(dst), paint, newStyle, flags | DrawFlags::kIgnorePathEffect);
             return;
         } else {
-            // TBD: This warning should go through the general purpose graphite logging system
-            SkDebugf("[graphite] WARNING - Path effect failed to apply, drawing original path.\n");
+            SKGPU_LOG_W("Path effect failed to apply, drawing original path.");
             this->drawShape(shape, paint, style, flags | DrawFlags::kIgnorePathEffect);
             return;
         }
diff --git a/experimental/graphite/src/Gpu.cpp b/experimental/graphite/src/Gpu.cpp
index 41120f1..8839cdc 100644
--- a/experimental/graphite/src/Gpu.cpp
+++ b/experimental/graphite/src/Gpu.cpp
@@ -12,6 +12,7 @@
 #include "experimental/graphite/src/Caps.h"
 #include "experimental/graphite/src/CommandBuffer.h"
 #include "experimental/graphite/src/GpuWorkSubmission.h"
+#include "experimental/graphite/src/Log.h"
 #include "experimental/graphite/src/ResourceProvider.h"
 #include "src/sksl/SkSLCompiler.h"
 
@@ -52,7 +53,11 @@
         return false;
     }
 
-    SkDEBUGCODE(if (!commandBuffer->hasWork()) SkDebugf("Submitting empty command buffer!\n");)
+#ifdef SK_DEBUG
+    if (!commandBuffer->hasWork()) {
+        SKGPU_LOG_W("Submitting empty command buffer!");
+    }
+#endif
 
     return this->onSubmit(std::move(commandBuffer));
 }
diff --git a/experimental/graphite/src/Log.h b/experimental/graphite/src/Log.h
new file mode 100644
index 0000000..303dd73
--- /dev/null
+++ b/experimental/graphite/src/Log.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2022 Google LLC
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef skgpu_Log_DEFINED
+#define skgpu_Log_DEFINED
+
+namespace skgpu {
+enum class Priority : int {
+    kError = 0,
+    kWarning = 1,
+    kDebug = 2,
+};
+};  // namespace skgpu
+
+#if !defined(SKGPU_LOWEST_ACTIVE_PRIORITY)
+#ifdef SK_DEBUG
+    #define SKGPU_LOWEST_ACTIVE_PRIORITY Priority::kWarning
+#else
+    #define SKGPU_LOWEST_ACTIVE_PRIORITY Priority::kError
+#endif
+#endif
+#define SKGPU_LOG(priority, fmt, ...) \
+    if (priority <= SKGPU_LOWEST_ACTIVE_PRIORITY) { \
+        SkDebugf("[graphite] " fmt "\n", ##__VA_ARGS__); \
+    }
+#define SKGPU_LOG_E(fmt, ...) SKGPU_LOG(skgpu::Priority::kError, "** ERROR ** " fmt, ##__VA_ARGS__)
+#define SKGPU_LOG_W(fmt, ...) SKGPU_LOG(skgpu::Priority::kWarning, "WARNING - " fmt, ##__VA_ARGS__)
+#define SKGPU_LOG_D(fmt, ...) SKGPU_LOG(skgpu::Priority::kDebug, fmt, ##__VA_ARGS__)
+
+#endif // skgpu_Log_DEFINED
diff --git a/experimental/graphite/src/RenderPassTask.cpp b/experimental/graphite/src/RenderPassTask.cpp
index 287d878..87ab43c 100644
--- a/experimental/graphite/src/RenderPassTask.cpp
+++ b/experimental/graphite/src/RenderPassTask.cpp
@@ -10,6 +10,7 @@
 #include "experimental/graphite/src/CommandBuffer.h"
 #include "experimental/graphite/src/ContextPriv.h"
 #include "experimental/graphite/src/DrawPass.h"
+#include "experimental/graphite/src/Log.h"
 #include "experimental/graphite/src/ResourceProvider.h"
 #include "experimental/graphite/src/Texture.h"
 #include "experimental/graphite/src/TextureProxy.h"
@@ -46,9 +47,9 @@
     // Instantiate the target
     if (fTarget) {
         if (!fTarget->instantiate(resourceProvider)) {
-            SkDebugf("WARNING: given invalid texture proxy. Will not create renderpass!\n");
-            SkDebugf("Dimensions are (%d, %d).\n", fTarget->dimensions().width(),
-                     fTarget->dimensions().height());
+            SKGPU_LOG_W("Given invalid texture proxy. Will not create renderpass!");
+            SKGPU_LOG_W("Dimensions are (%d, %d).",
+                        fTarget->dimensions().width(), fTarget->dimensions().height());
             return;
         }
     }
diff --git a/experimental/graphite/src/mtl/MtlCommandBuffer.h b/experimental/graphite/src/mtl/MtlCommandBuffer.h
index c7b392e..2af5084 100644
--- a/experimental/graphite/src/mtl/MtlCommandBuffer.h
+++ b/experimental/graphite/src/mtl/MtlCommandBuffer.h
@@ -10,6 +10,7 @@
 
 #include "experimental/graphite/src/CommandBuffer.h"
 #include "experimental/graphite/src/GpuWorkSubmission.h"
+#include "experimental/graphite/src/Log.h"
 
 #include <memory>
 
@@ -40,8 +41,8 @@
             [(*fCommandBuffer) waitUntilCompleted];
         }
         if (!this->isFinished()) {
-            SkDebugf("Unfinished command buffer status: %d\n",
-                     (int)(*fCommandBuffer).status);
+            SKGPU_LOG_E("Unfinished command buffer status: %d",
+                        (int)(*fCommandBuffer).status);
             SkASSERT(false);
         }
     }
diff --git a/experimental/graphite/src/mtl/MtlCommandBuffer.mm b/experimental/graphite/src/mtl/MtlCommandBuffer.mm
index b217654..dc3672a 100644
--- a/experimental/graphite/src/mtl/MtlCommandBuffer.mm
+++ b/experimental/graphite/src/mtl/MtlCommandBuffer.mm
@@ -7,6 +7,7 @@
 
 #include "experimental/graphite/src/mtl/MtlCommandBuffer.h"
 
+#include "experimental/graphite/src/Log.h"
 #include "experimental/graphite/src/TextureProxy.h"
 #include "experimental/graphite/src/mtl/MtlBlitCommandEncoder.h"
 #include "experimental/graphite/src/mtl/MtlBuffer.h"
@@ -61,11 +62,10 @@
 #endif
     [(*fCommandBuffer) commit];
 
-    // TODO: better error reporting
     if ((*fCommandBuffer).status == MTLCommandBufferStatusError) {
         NSString* description = (*fCommandBuffer).error.localizedDescription;
         const char* errorString = [description UTF8String];
-        SkDebugf("Error submitting command buffer: %s\n", errorString);
+        SKGPU_LOG_E("Failure submitting command buffer: %s", errorString);
     }
 
     return ((*fCommandBuffer).status != MTLCommandBufferStatusError);
@@ -342,8 +342,8 @@
                                                            indexOffset, 1, baseVertex, 0);
 
     } else {
-        // TODO: Do nothing, fatal failure, or just the regular graphite error reporting overhaul?
-        SkDebugf("[graphite] WARNING - Skipping unsupported draw call.\n");
+        // TODO: Do nothing, fatal failure, or just the regular graphite error reporting?
+        SKGPU_LOG_E("Skipping unsupported draw call.");
     }
 }
 
@@ -372,8 +372,8 @@
                                                            indexOffset, instanceCount,
                                                            baseVertex, baseInstance);
     } else {
-        // TODO: Do nothing, fatal failure, or just the regular graphite error reporting overhaul?
-        SkDebugf("[graphite] WARNING - Skipping unsupported draw call.\n");
+        // TODO: Do nothing, fatal failure, or just the regular graphite error reporting?
+        SKGPU_LOG_W("Skipping unsupported draw call.");
     }
 }
 
diff --git a/experimental/graphite/src/mtl/MtlGpu.mm b/experimental/graphite/src/mtl/MtlGpu.mm
index d1a6346..a1e26b9 100644
--- a/experimental/graphite/src/mtl/MtlGpu.mm
+++ b/experimental/graphite/src/mtl/MtlGpu.mm
@@ -10,6 +10,7 @@
 #include "experimental/graphite/include/BackendTexture.h"
 #include "experimental/graphite/include/TextureInfo.h"
 #include "experimental/graphite/src/Caps.h"
+#include "experimental/graphite/src/Log.h"
 #include "experimental/graphite/src/mtl/MtlCommandBuffer.h"
 #include "experimental/graphite/src/mtl/MtlResourceProvider.h"
 #include "experimental/graphite/src/mtl/MtlTexture.h"
@@ -21,11 +22,11 @@
     if (@available(macOS 10.14, iOS 11.0, *)) {
         // no warning needed
     } else {
-        SkDebugf("*** Error ***: Skia's Graphite backend no longer supports this OS version.\n");
+        SKGPU_LOG_E("Skia's Graphite backend no longer supports this OS version.");
 #ifdef SK_BUILD_FOR_IOS
-        SkDebugf("Minimum supported version is iOS 11.0.\n");
+        SKGPU_LOG_E("Minimum supported version is iOS 11.0.");
 #else
-        SkDebugf("Minimum supported version is MacOS 10.14.\n");
+        SKGPU_LOG_E("Minimum supported version is MacOS 10.14.");
 #endif
         return nullptr;
     }
diff --git a/experimental/graphite/src/mtl/MtlGraphicsPipeline.mm b/experimental/graphite/src/mtl/MtlGraphicsPipeline.mm
index ad61e28..2438a1e 100644
--- a/experimental/graphite/src/mtl/MtlGraphicsPipeline.mm
+++ b/experimental/graphite/src/mtl/MtlGraphicsPipeline.mm
@@ -10,6 +10,7 @@
 #include "experimental/graphite/include/TextureInfo.h"
 #include "experimental/graphite/src/ContextPriv.h"
 #include "experimental/graphite/src/GraphicsPipelineDesc.h"
+#include "experimental/graphite/src/Log.h"
 #include "experimental/graphite/src/Renderer.h"
 #include "experimental/graphite/src/mtl/MtlGpu.h"
 #include "experimental/graphite/src/mtl/MtlResourceProvider.h"
@@ -422,8 +423,7 @@
             [gpu->device() newRenderPipelineStateWithDescriptor:psoDescriptor.get()
                                                           error:&error]);
     if (!pso) {
-        SkDebugf("Pipeline creation failure\n");
-        SkDebugf("Errors:\n%s", error.debugDescription.UTF8String);
+        SKGPU_LOG_E("Pipeline creation failure:\n%s", error.debugDescription.UTF8String);
         return nullptr;
     }
 
diff --git a/gn/graphite.gni b/gn/graphite.gni
index 1df7065..ba41ea4 100644
--- a/gn/graphite.gni
+++ b/gn/graphite.gni
@@ -57,6 +57,7 @@
   "$_src/GraphicsPipelineDesc.h",
   "$_src/Image_Graphite.cpp",
   "$_src/Image_Graphite.h",
+  "$_src/Log.h",
   "$_src/PaintParams.cpp",
   "$_src/PaintParams.h",
   "$_src/Recorder.cpp",