Allow gpu ResourceHandle class to be shared for multiple purposes
authoregdaniel <egdaniel@google.com>
Mon, 9 May 2016 18:03:48 +0000 (11:03 -0700)
committerCommit bot <commit-bot@chromium.org>
Mon, 9 May 2016 18:03:48 +0000 (11:03 -0700)
Currently this class was just used for resource handles when building up
a shader. However, I want to now use this class for things like objects
owned/held by the GrVkResourceProvider which are used by other classes.
An example of this will be for GrVkRenderTargets to hold a handle to a
collection of compatible render passes without having to own/hold onto
the render passes themselves.

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

Review-Url: https://codereview.chromium.org/1955893002

gyp/gpu.gypi
src/gpu/GrResourceHandle.h [new file with mode: 0644]
src/gpu/glsl/GrGLSLProgramDataManager.h

index f0bb333e94276625b5b33b036ed2606fed550596..6b4837a992b8461aac27c4d489d96f87326ff944 100644 (file)
       '<(skia_src_path)/gpu/GrReducedClip.h',
       '<(skia_src_path)/gpu/GrResourceCache.cpp',
       '<(skia_src_path)/gpu/GrResourceCache.h',
+      '<(skia_src_path)/gpu/GrResourceHandle.h',
       '<(skia_src_path)/gpu/GrResourceProvider.cpp',
       '<(skia_src_path)/gpu/GrResourceProvider.h',
       '<(skia_src_path)/gpu/GrShape.cpp',
diff --git a/src/gpu/GrResourceHandle.h b/src/gpu/GrResourceHandle.h
new file mode 100644 (file)
index 0000000..383bbea
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * 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 GrResourceHandle_DEFINED
+#define GrResourceHandle_DEFINED
+
+#include "SkTypes.h"
+
+// Opaque handle to a resource. Users should always use the macro below to create a specific
+// template instantiation of GrResourceHandle.
+template <typename kind> class GrResourceHandle {
+public:
+    GrResourceHandle(int value) : fValue(value) {
+        SkASSERT(this->isValid());
+    }
+
+    GrResourceHandle() : fValue(kInvalid_ResourceHandle) {}
+
+    bool operator==(const GrResourceHandle& other) const { return other.fValue == fValue; }
+    bool isValid() const { return kInvalid_ResourceHandle != fValue; }
+    int toIndex() const { SkASSERT(this->isValid()); return fValue; }
+
+private:
+    static const int kInvalid_ResourceHandle = -1;
+    int fValue;
+};
+
+// Creates a type "name", which is a specfic template instantiation of GrResourceHandle.
+#define GR_DEFINE_RESOURCE_HANDLE_CLASS(name) \
+    struct name##Kind {};  \
+    using name = GrResourceHandle<name##Kind>;
+#endif
index e578e11808868163ce18c41e0093601f78d025bf..5d502cfd7591dcf271a0c65062d26dfcfd4954c0 100644 (file)
@@ -8,6 +8,7 @@
 #ifndef GrGLSLProgramDataManager_DEFINED
 #define GrGLSLProgramDataManager_DEFINED
 
+#include "GrResourceHandle.h"
 #include "SkTypes.h"
 
 class SkMatrix;
@@ -18,28 +19,7 @@ class SkMatrix;
  */
 class GrGLSLProgramDataManager : SkNoncopyable {
 public:
-    // Opaque handle to a resource
-    class ShaderResourceHandle {
-    public:
-        ShaderResourceHandle(int value)
-            : fValue(value) {
-            SkASSERT(this->isValid());
-        }
-
-        ShaderResourceHandle()
-            : fValue(kInvalid_ShaderResourceHandle) {
-        }
-
-        bool operator==(const ShaderResourceHandle& other) const { return other.fValue == fValue; }
-        bool isValid() const { return kInvalid_ShaderResourceHandle != fValue; }
-        int toIndex() const { SkASSERT(this->isValid()); return fValue; }
-
-    private:
-        static const int kInvalid_ShaderResourceHandle = -1;
-        int fValue;
-    };
-
-    typedef ShaderResourceHandle UniformHandle;
+    GR_DEFINE_RESOURCE_HANDLE_CLASS(UniformHandle);
 
     virtual ~GrGLSLProgramDataManager() {}
 
@@ -67,7 +47,7 @@ public:
     void setSkMatrix(UniformHandle, const SkMatrix&) const;
 
     // for nvpr only
-    typedef ShaderResourceHandle VaryingHandle;
+    GR_DEFINE_RESOURCE_HANDLE_CLASS(VaryingHandle);
     virtual void setPathFragmentInputTransform(VaryingHandle u, int components,
                                                const SkMatrix& matrix) const = 0;
 
@@ -75,7 +55,6 @@ protected:
     GrGLSLProgramDataManager() {}
 
 private:
-
     typedef SkNoncopyable INHERITED;
 };