Home | History | Annotate | Download | only in debug
      1 /*
      2  * Copyright 2012 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 
      8 #ifndef GrFakeRefObj_DEFINED
      9 #define GrFakeRefObj_DEFINED
     10 
     11 #include "SkTypes.h"
     12 #include "gl/GrGLInterface.h"
     13 
     14 ////////////////////////////////////////////////////////////////////////////////
     15 // This object is used to track the OpenGL objects. We don't use real
     16 // reference counting (i.e., we don't free the objects when their ref count
     17 // goes to 0) so that we can detect invalid memory accesses. The refs we
     18 // are tracking in this class are actually OpenGL's references to the objects
     19 // not "ours"
     20 // Each object also gets a unique globally identifying ID
     21 class GrFakeRefObj : SkNoncopyable {
     22 public:
     23     GrFakeRefObj()
     24         : fRef(0)
     25         , fMarkedForDeletion(false)
     26         , fDeleted(false) {
     27 
     28         // source for globally unique IDs - 0 is reserved!
     29         static int fNextID = 0;
     30 
     31         fID = ++fNextID;
     32     }
     33     virtual ~GrFakeRefObj() {};
     34 
     35     void ref() {
     36         fRef++;
     37     }
     38     void unref() {
     39         fRef--;
     40         GrAlwaysAssert(fRef >= 0);
     41 
     42         // often in OpenGL a given object may still be in use when the
     43         // delete call is made. In these cases the object is marked
     44         // for deletion and then freed when it is no longer in use
     45         if (0 == fRef && fMarkedForDeletion) {
     46             this->deleteAction();
     47         }
     48     }
     49     int getRefCount() const             { return fRef; }
     50 
     51     GrGLuint getID() const              { return fID; }
     52 
     53     void setMarkedForDeletion()         { fMarkedForDeletion = true; }
     54     bool getMarkedForDeletion() const   { return fMarkedForDeletion; }
     55 
     56     bool getDeleted() const             { return fDeleted; }
     57 
     58     // The deleteAction fires if the object has been marked for deletion but
     59     // couldn't be deleted earlier due to refs
     60     virtual void deleteAction() {
     61         this->setDeleted();
     62     }
     63 
     64 protected:
     65 private:
     66     int         fRef;               // ref count
     67     GrGLuint    fID;                // globally unique ID
     68     bool        fMarkedForDeletion;
     69     // The deleted flag is only set when OpenGL thinks the object is deleted
     70     // It is obviously still allocated w/in this framework
     71     bool        fDeleted;
     72 
     73     // setDeleted should only ever appear in the deleteAction method!
     74     void setDeleted()                   { fDeleted = true; }
     75 };
     76 
     77 ////////////////////////////////////////////////////////////////////////////////
     78 // Each class derived from GrFakeRefObj should use this macro to add a
     79 // factory creation entry point. This entry point is used by the GrGLDebug
     80 // object to instantiate the various objects
     81 // all globally unique IDs
     82 #define GR_DEFINE_CREATOR(className)                        \
     83     public:                                                 \
     84     static GrFakeRefObj *create ## className() {            \
     85         return SkNEW(className);                            \
     86     }
     87 
     88 #endif // GrFakeRefObj_DEFINED
     89