blob: 27e27ce3cefa9ac0be90db6be9b91250df711669 [file] [log] [blame]
robertphillips@google.comdd743fe2012-04-05 14:40:53 +00001/*
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
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000011#include "SkTypes.h"
robertphillips@google.comdd743fe2012-04-05 14:40:53 +000012#include "gl/GrGLInterface.h"
13
14////////////////////////////////////////////////////////////////////////////////
15// This object is used to track the OpenGL objects. We don't use real
rmistry@google.comfbfcd562012-08-23 18:09:54 +000016// reference counting (i.e., we don't free the objects when their ref count
robertphillips@google.comdd743fe2012-04-05 14:40:53 +000017// 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
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000021class GrFakeRefObj : SkNoncopyable {
robertphillips@google.comdd743fe2012-04-05 14:40:53 +000022public:
rmistry@google.comfbfcd562012-08-23 18:09:54 +000023 GrFakeRefObj()
robertphillips@google.comdd743fe2012-04-05 14:40:53 +000024 : fRef(0)
robertphillips@google.comdd743fe2012-04-05 14:40:53 +000025 , fMarkedForDeletion(false)
26 , fDeleted(false) {
27
robertphillips@google.com0dd84a32012-04-16 16:24:35 +000028 // source for globally unique IDs - 0 is reserved!
rmistry@google.comfbfcd562012-08-23 18:09:54 +000029 static int fNextID = 0;
robertphillips@google.comdd743fe2012-04-05 14:40:53 +000030
31 fID = ++fNextID;
32 }
33 virtual ~GrFakeRefObj() {};
34
rmistry@google.comfbfcd562012-08-23 18:09:54 +000035 void ref() {
36 fRef++;
robertphillips@google.comdd743fe2012-04-05 14:40:53 +000037 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000038 void unref() {
39 fRef--;
robertphillips@google.comdd743fe2012-04-05 14:40:53 +000040 GrAlwaysAssert(fRef >= 0);
41
rmistry@google.comfbfcd562012-08-23 18:09:54 +000042 // often in OpenGL a given object may still be in use when the
robertphillips@google.comdd743fe2012-04-05 14:40:53 +000043 // 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) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000046 this->deleteAction();
robertphillips@google.comdd743fe2012-04-05 14:40:53 +000047 }
48 }
robertphillips@google.com0dd84a32012-04-16 16:24:35 +000049 int getRefCount() const { return fRef; }
robertphillips@google.comdd743fe2012-04-05 14:40:53 +000050
robertphillips@google.com0dd84a32012-04-16 16:24:35 +000051 GrGLuint getID() const { return fID; }
robertphillips@google.comdd743fe2012-04-05 14:40:53 +000052
robertphillips@google.com0dd84a32012-04-16 16:24:35 +000053 void setMarkedForDeletion() { fMarkedForDeletion = true; }
54 bool getMarkedForDeletion() const { return fMarkedForDeletion; }
robertphillips@google.comdd743fe2012-04-05 14:40:53 +000055
robertphillips@google.com0dd84a32012-04-16 16:24:35 +000056 bool getDeleted() const { return fDeleted; }
robertphillips@google.comdd743fe2012-04-05 14:40:53 +000057
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
64protected:
65private:
robertphillips@google.com0dd84a32012-04-16 16:24:35 +000066 int fRef; // ref count
robertphillips@google.com0dd84a32012-04-16 16:24:35 +000067 GrGLuint fID; // globally unique ID
robertphillips@google.comdd743fe2012-04-05 14:40:53 +000068 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!
robertphillips@google.com0dd84a32012-04-16 16:24:35 +000074 void setDeleted() { fDeleted = true; }
robertphillips@google.comdd743fe2012-04-05 14:40:53 +000075};
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