GrMtlDepthStencil.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2019 Google Inc.
  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. #ifndef GrMtlDepthStencil_DEFINED
  8. #define GrMtlDepthStencil_DEFINED
  9. #import <Metal/Metal.h>
  10. #include "include/gpu/GrTypes.h"
  11. #include "src/core/SkOpts.h"
  12. #include <atomic>
  13. class GrMtlGpu;
  14. class GrStencilSettings;
  15. // A wrapper for a MTLDepthStencilState object with caching support.
  16. class GrMtlDepthStencil : public SkRefCnt {
  17. public:
  18. static GrMtlDepthStencil* Create(const GrMtlGpu*, const GrStencilSettings&, GrSurfaceOrigin);
  19. ~GrMtlDepthStencil() { fMtlDepthStencilState = nil; }
  20. id<MTLDepthStencilState> mtlDepthStencil() const { return fMtlDepthStencilState; }
  21. struct Key {
  22. struct Face {
  23. uint32_t fReadMask;
  24. uint32_t fWriteMask;
  25. uint32_t fOps;
  26. };
  27. Face fFront;
  28. Face fBack;
  29. bool operator==(const Key& that) const {
  30. return this->fFront.fReadMask == that.fFront.fReadMask &&
  31. this->fFront.fWriteMask == that.fFront.fWriteMask &&
  32. this->fFront.fOps == that.fFront.fOps &&
  33. this->fBack.fReadMask == that.fBack.fReadMask &&
  34. this->fBack.fWriteMask == that.fBack.fWriteMask &&
  35. this->fBack.fOps == that.fBack.fOps;
  36. }
  37. };
  38. // Helpers for hashing GrMtlSampler
  39. static Key GenerateKey(const GrStencilSettings&, GrSurfaceOrigin);
  40. static const Key& GetKey(const GrMtlDepthStencil& depthStencil) { return depthStencil.fKey; }
  41. static uint32_t Hash(const Key& key) {
  42. return SkOpts::hash(reinterpret_cast<const uint32_t*>(&key), sizeof(Key));
  43. }
  44. private:
  45. GrMtlDepthStencil(id<MTLDepthStencilState> mtlDepthStencilState, Key key)
  46. : fMtlDepthStencilState(mtlDepthStencilState)
  47. , fKey(key) {}
  48. id<MTLDepthStencilState> fMtlDepthStencilState;
  49. Key fKey;
  50. };
  51. #endif