GrMtlStencilAttachment.mm 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright 2018 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. #include "src/gpu/mtl/GrMtlGpu.h"
  8. #include "src/gpu/mtl/GrMtlUtil.h"
  9. #if !__has_feature(objc_arc)
  10. #error This file must be compiled with Arc. Use -fobjc-arc flag
  11. #endif
  12. GrMtlStencilAttachment::GrMtlStencilAttachment(GrMtlGpu* gpu,
  13. const Format& format,
  14. const id<MTLTexture> stencilView)
  15. : GrStencilAttachment(gpu, stencilView.width, stencilView.height, format.fStencilBits,
  16. stencilView.sampleCount)
  17. , fFormat(format)
  18. , fStencilView(stencilView) {
  19. this->registerWithCache(SkBudgeted::kYes);
  20. }
  21. GrMtlStencilAttachment* GrMtlStencilAttachment::Create(GrMtlGpu* gpu,
  22. int width,
  23. int height,
  24. int sampleCnt,
  25. const Format& format) {
  26. MTLTextureDescriptor* desc =
  27. [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:format.fInternalFormat
  28. width:width
  29. height:height
  30. mipmapped:NO];
  31. desc.resourceOptions = MTLResourceStorageModePrivate;
  32. desc.usage = MTLTextureUsageRenderTarget;
  33. desc.sampleCount = sampleCnt;
  34. if (sampleCnt > 1) {
  35. desc.textureType = MTLTextureType2DMultisample;
  36. }
  37. return new GrMtlStencilAttachment(gpu, format, [gpu->device() newTextureWithDescriptor:desc]);
  38. }
  39. GrMtlStencilAttachment::~GrMtlStencilAttachment() {
  40. // should have been released or abandoned first
  41. SkASSERT(!fStencilView);
  42. }
  43. size_t GrMtlStencilAttachment::onGpuMemorySize() const {
  44. uint64_t size = this->width();
  45. size *= this->height();
  46. size *= fFormat.fTotalBits;
  47. size *= this->numSamples();
  48. return static_cast<size_t>(size / 8);
  49. }
  50. void GrMtlStencilAttachment::onRelease() {
  51. fStencilView = nullptr;
  52. GrStencilAttachment::onRelease();
  53. }
  54. void GrMtlStencilAttachment::onAbandon() {
  55. fStencilView = nullptr;
  56. GrStencilAttachment::onAbandon();
  57. }
  58. GrMtlGpu* GrMtlStencilAttachment::getMtlGpu() const {
  59. SkASSERT(!this->wasDestroyed());
  60. return static_cast<GrMtlGpu*>(this->getGpu());
  61. }