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