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 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     SK_API 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     /** See addIdleProc. */
46     enum class IdleState {
47         kFlushed,
48         kFinished
49     };
50     /**
51      * Installs a proc on this texture. It will be called when the texture becomes "idle". There
52      * are two types of idle states as indicated by IdleState. For managed backends (e.g. GL where
53      * a driver typically handles CPU/GPU synchronization of resource access) there is no difference
54      * between the two. They both mean "all work related to the resource has been flushed to the
55      * backend API and the texture is not owned outside the resource cache".
56      *
57      * If the API is unmanaged (e.g. Vulkan) then kFinished has the additional constraint that the
58      * work flushed to the GPU is finished.
59      */
addIdleProc(sk_sp<GrRefCntedCallback> idleProc,IdleState)60     virtual void addIdleProc(sk_sp<GrRefCntedCallback> idleProc, IdleState) {
61         // This is the default implementation for the managed case where the IdleState can be
62         // ignored. Unmanaged backends, e.g. Vulkan, must override this to consider IdleState.
63         fIdleProcs.push_back(std::move(idleProc));
64     }
65     /** Helper version of addIdleProc that creates the ref-counted wrapper. */
addIdleProc(GrRefCntedCallback::Callback callback,GrRefCntedCallback::Context context,IdleState state)66     void addIdleProc(GrRefCntedCallback::Callback callback,
67                      GrRefCntedCallback::Context context,
68                      IdleState state) {
69         this->addIdleProc(sk_make_sp<GrRefCntedCallback>(callback, context), state);
70     }
71 
72     /** Access methods that are only to be used within Skia code. */
73     inline GrTexturePriv texturePriv();
74     inline const GrTexturePriv texturePriv() const;
75 
76 protected:
77     GrTexture(GrGpu*, const SkISize&, GrPixelConfig, GrProtected, GrTextureType, GrMipMapsStatus);
78 
79     virtual bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) = 0;
80 
81     SkTArray<sk_sp<GrRefCntedCallback>> fIdleProcs;
82 
willRemoveLastRef()83     void willRemoveLastRef() override {
84         // We're about to be idle in the resource cache. Do our part to trigger the idle callbacks.
85         fIdleProcs.reset();
86     }
87     void computeScratchKey(GrScratchKey*) const override;
88 
89 private:
90     size_t onGpuMemorySize() const override;
91     void markMipMapsDirty();
92     void markMipMapsClean();
93 
94     GrTextureType                 fTextureType;
95     GrMipMapsStatus               fMipMapsStatus;
96     int                           fMaxMipMapLevel;
97     friend class GrTexturePriv;
98 
99     typedef GrSurface INHERITED;
100 };
101 
102 #endif
103