1 /* 2 * Copyright 2022 Google LLC. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef skgpu_piet_PietTypes_DEFINED 9 #define skgpu_piet_PietTypes_DEFINED 10 11 #include <pgpu.h> 12 13 #include <memory> 14 #include <optional> 15 16 namespace skgpu::piet { 17 18 using Transform = PgpuTransform; 19 identity_transform()20inline Transform identity_transform() { return {1, 0, 0, 1, 0, 0}; } 21 22 /** 23 * Wrapper around a pgpu-render type that automatically frees resources on destruction by calling 24 * the appropriate pgpu-render API function. 25 */ 26 template <typename Type, void (*DtorFunc)(Type*)> class Object { 27 public: Object(Type * handle)28 explicit Object(Type* handle) : fHandle(handle) {} Object()29 Object() : fHandle(nullptr) {} 30 31 Object(Object&&) = default; 32 Object& operator=(Object&&) = default; 33 34 virtual ~Object() = default; 35 get()36 Type* get() const { return fHandle.get(); } 37 38 operator bool() const { return this->get() != nullptr; } 39 40 protected: reset()41 void reset() { fHandle = nullptr; } 42 43 private: 44 Object(const Object&) = delete; 45 Object& operator=(const Object&) = delete; 46 47 struct Deleter { operatorDeleter48 void operator()(Type* t) { 49 if (t) { 50 DtorFunc(t); 51 } 52 } 53 }; 54 std::unique_ptr<Type, Deleter> fHandle; 55 }; 56 57 } // namespace skgpu::piet 58 59 #endif // skgpu_piet_PietTypes_DEFINED 60