GrTexture.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 2011 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 GrTexture_DEFINED
  8. #define GrTexture_DEFINED
  9. #include "include/core/SkImage.h"
  10. #include "include/core/SkPoint.h"
  11. #include "include/core/SkRefCnt.h"
  12. #include "include/gpu/GrBackendSurface.h"
  13. #include "include/gpu/GrSamplerState.h"
  14. #include "include/gpu/GrSurface.h"
  15. #include "include/private/GrTypesPriv.h"
  16. class GrTexturePriv;
  17. class SK_API GrTexture : virtual public GrSurface {
  18. public:
  19. GrTexture* asTexture() override { return this; }
  20. const GrTexture* asTexture() const override { return this; }
  21. virtual GrBackendTexture getBackendTexture() const = 0;
  22. /**
  23. * This function indicates that the texture parameters (wrap mode, filtering, ...) have been
  24. * changed externally to Skia.
  25. */
  26. virtual void textureParamsModified() = 0;
  27. /**
  28. * This function steals the backend texture from a uniquely owned GrTexture with no pending
  29. * IO, passing it out to the caller. The GrTexture is deleted in the process.
  30. *
  31. * Note that if the GrTexture is not uniquely owned (no other refs), or has pending IO, this
  32. * function will fail.
  33. */
  34. static bool StealBackendTexture(sk_sp<GrTexture>,
  35. GrBackendTexture*,
  36. SkImage::BackendTextureReleaseProc*);
  37. #ifdef SK_DEBUG
  38. void validate() const {
  39. this->INHERITED::validate();
  40. }
  41. #endif
  42. /** See addIdleProc. */
  43. enum class IdleState {
  44. kFlushed,
  45. kFinished
  46. };
  47. /**
  48. * Installs a proc on this texture. It will be called when the texture becomes "idle". There
  49. * are two types of idle states as indicated by IdleState. For managed backends (e.g. GL where
  50. * a driver typically handles CPU/GPU synchronization of resource access) there is no difference
  51. * between the two. They both mean "all work related to the resource has been flushed to the
  52. * backend API and the texture is not owned outside the resource cache".
  53. *
  54. * If the API is unmanaged (e.g. Vulkan) then kFinished has the additional constraint that the
  55. * work flushed to the GPU is finished.
  56. */
  57. virtual void addIdleProc(sk_sp<GrRefCntedCallback> idleProc, IdleState) {
  58. // This is the default implementation for the managed case where the IdleState can be
  59. // ignored. Unmanaged backends, e.g. Vulkan, must override this to consider IdleState.
  60. fIdleProcs.push_back(std::move(idleProc));
  61. }
  62. /** Helper version of addIdleProc that creates the ref-counted wrapper. */
  63. void addIdleProc(GrRefCntedCallback::Callback callback,
  64. GrRefCntedCallback::Context context,
  65. IdleState state) {
  66. this->addIdleProc(sk_make_sp<GrRefCntedCallback>(callback, context), state);
  67. }
  68. /** Access methods that are only to be used within Skia code. */
  69. inline GrTexturePriv texturePriv();
  70. inline const GrTexturePriv texturePriv() const;
  71. protected:
  72. GrTexture(GrGpu*, const GrSurfaceDesc&, GrProtected, GrTextureType, GrMipMapsStatus);
  73. virtual bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) = 0;
  74. SkTArray<sk_sp<GrRefCntedCallback>> fIdleProcs;
  75. void willRemoveLastRefOrPendingIO() override {
  76. // We're about to be idle in the resource cache. Do our part to trigger the idle callbacks.
  77. fIdleProcs.reset();
  78. }
  79. private:
  80. void computeScratchKey(GrScratchKey*) const override;
  81. size_t onGpuMemorySize() const override;
  82. void markMipMapsDirty();
  83. void markMipMapsClean();
  84. GrTextureType fTextureType;
  85. GrMipMapsStatus fMipMapsStatus;
  86. int fMaxMipMapLevel;
  87. friend class GrTexturePriv;
  88. typedef GrSurface INHERITED;
  89. };
  90. #endif