gfx/skia/trunk/src/gpu/gl/debug/GrFakeRefObj.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/gpu/gl/debug/GrFakeRefObj.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,94 @@
     1.4 +/*
     1.5 + * Copyright 2012 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#ifndef GrFakeRefObj_DEFINED
    1.12 +#define GrFakeRefObj_DEFINED
    1.13 +
    1.14 +#include "SkTypes.h"
    1.15 +#include "gl/GrGLInterface.h"
    1.16 +
    1.17 +////////////////////////////////////////////////////////////////////////////////
    1.18 +// This object is used to track the OpenGL objects. We don't use real
    1.19 +// reference counting (i.e., we don't free the objects when their ref count
    1.20 +// goes to 0) so that we can detect invalid memory accesses. The refs we
    1.21 +// are tracking in this class are actually OpenGL's references to the objects
    1.22 +// not "ours"
    1.23 +// Each object also gets a unique globally identifying ID
    1.24 +class GrFakeRefObj : public SkNoncopyable {
    1.25 +public:
    1.26 +    GrFakeRefObj()
    1.27 +        : fRef(0)
    1.28 +        , fHighRefCount(0)
    1.29 +        , fMarkedForDeletion(false)
    1.30 +        , fDeleted(false) {
    1.31 +
    1.32 +        // source for globally unique IDs - 0 is reserved!
    1.33 +        static int fNextID = 0;
    1.34 +
    1.35 +        fID = ++fNextID;
    1.36 +    }
    1.37 +    virtual ~GrFakeRefObj() {};
    1.38 +
    1.39 +    void ref() {
    1.40 +        fRef++;
    1.41 +        if (fHighRefCount < fRef) {
    1.42 +            fHighRefCount = fRef;
    1.43 +        }
    1.44 +    }
    1.45 +    void unref() {
    1.46 +        fRef--;
    1.47 +        GrAlwaysAssert(fRef >= 0);
    1.48 +
    1.49 +        // often in OpenGL a given object may still be in use when the
    1.50 +        // delete call is made. In these cases the object is marked
    1.51 +        // for deletion and then freed when it is no longer in use
    1.52 +        if (0 == fRef && fMarkedForDeletion) {
    1.53 +            this->deleteAction();
    1.54 +        }
    1.55 +    }
    1.56 +    int getRefCount() const             { return fRef; }
    1.57 +    int getHighRefCount() const         { return fHighRefCount; }
    1.58 +
    1.59 +    GrGLuint getID() const              { return fID; }
    1.60 +
    1.61 +    void setMarkedForDeletion()         { fMarkedForDeletion = true; }
    1.62 +    bool getMarkedForDeletion() const   { return fMarkedForDeletion; }
    1.63 +
    1.64 +    bool getDeleted() const             { return fDeleted; }
    1.65 +
    1.66 +    // The deleteAction fires if the object has been marked for deletion but
    1.67 +    // couldn't be deleted earlier due to refs
    1.68 +    virtual void deleteAction() {
    1.69 +        this->setDeleted();
    1.70 +    }
    1.71 +
    1.72 +protected:
    1.73 +private:
    1.74 +    int         fRef;               // ref count
    1.75 +    int         fHighRefCount;      // high water mark of the ref count
    1.76 +    GrGLuint    fID;                // globally unique ID
    1.77 +    bool        fMarkedForDeletion;
    1.78 +    // The deleted flag is only set when OpenGL thinks the object is deleted
    1.79 +    // It is obviously still allocated w/in this framework
    1.80 +    bool        fDeleted;
    1.81 +
    1.82 +    // setDeleted should only ever appear in the deleteAction method!
    1.83 +    void setDeleted()                   { fDeleted = true; }
    1.84 +};
    1.85 +
    1.86 +////////////////////////////////////////////////////////////////////////////////
    1.87 +// Each class derived from GrFakeRefObj should use this macro to add a
    1.88 +// factory creation entry point. This entry point is used by the GrGLDebug
    1.89 +// object to instantiate the various objects
    1.90 +// all globally unique IDs
    1.91 +#define GR_DEFINE_CREATOR(className)                        \
    1.92 +    public:                                                 \
    1.93 +    static GrFakeRefObj *create ## className() {            \
    1.94 +        return SkNEW(className);                            \
    1.95 +    }
    1.96 +
    1.97 +#endif // GrFakeRefObj_DEFINED

mercurial