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