Home | History | Annotate | Download | only in gpu
      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 "GrBackendSurface.h"
     13 #include "GrSamplerState.h"
     14 #include "GrSurface.h"
     15 #include "SkImage.h"
     16 #include "SkPoint.h"
     17 #include "SkRefCnt.h"
     18 #include "../private/GrTypesPriv.h"
     19 
     20 class GrTexturePriv;
     21 
     22 class SK_API GrTexture : virtual public GrSurface {
     23 public:
     24     GrTexture* asTexture() override { return this; }
     25     const GrTexture* asTexture() const override { return this; }
     26 
     27     virtual GrBackendTexture getBackendTexture() const = 0;
     28 
     29     /**
     30      * This function indicates that the texture parameters (wrap mode, filtering, ...) have been
     31      * changed externally to Skia.
     32      */
     33     virtual void textureParamsModified() = 0;
     34 
     35     /**
     36      * This function steals the backend texture from a uniquely owned GrTexture with no pending
     37      * IO, passing it out to the caller. The GrTexture is deleted in the process.
     38      *
     39      * Note that if the GrTexture is not uniquely owned (no other refs), or has pending IO, this
     40      * function will fail.
     41      */
     42     static bool StealBackendTexture(sk_sp<GrTexture>,
     43                                     GrBackendTexture*,
     44                                     SkImage::BackendTextureReleaseProc*);
     45 
     46 #ifdef SK_DEBUG
     47     void validate() const {
     48         this->INHERITED::validate();
     49     }
     50 #endif
     51 
     52     /**
     53      * Installs a proc on this texture. It will be called when the texture becomes "idle". Idle is
     54      * defined to mean that the texture has no refs or pending IOs and that GPU I/O operations on
     55      * the texture are completed if the backend API disallows deletion of a texture before such
     56      * operations occur (e.g. Vulkan). After the idle proc is called it is removed. The idle proc
     57      * will always be called before the texture is destroyed, even in unusual shutdown scenarios
     58      * (e.g. GrContext::abandonContext()).
     59      */
     60     virtual void addIdleProc(sk_sp<GrRefCntedCallback> callback) {
     61         callback->addChild(std::move(fIdleCallback));
     62         fIdleCallback = std::move(callback);
     63     }
     64 
     65     /** Access methods that are only to be used within Skia code. */
     66     inline GrTexturePriv texturePriv();
     67     inline const GrTexturePriv texturePriv() const;
     68 
     69 protected:
     70     GrTexture(GrGpu*, const GrSurfaceDesc&, GrTextureType, GrMipMapsStatus);
     71 
     72     virtual bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) = 0;
     73 
     74     sk_sp<GrRefCntedCallback> fIdleCallback;
     75 
     76     void willRemoveLastRefOrPendingIO() override {
     77         // We're about to be idle in the resource cache. Do our part to trigger the idle callback.
     78         fIdleCallback.reset();
     79     }
     80 
     81 private:
     82     void computeScratchKey(GrScratchKey*) const override;
     83     size_t onGpuMemorySize() const override;
     84     void markMipMapsDirty();
     85     void markMipMapsClean();
     86 
     87     GrTextureType                 fTextureType;
     88     GrMipMapsStatus               fMipMapsStatus;
     89     int                           fMaxMipMapLevel;
     90     friend class GrTexturePriv;
     91 
     92     typedef GrSurface INHERITED;
     93 };
     94 
     95 #endif
     96