rsObjectBase.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef ANDROID_RS_OBJECT_BASE_H
  17. #define ANDROID_RS_OBJECT_BASE_H
  18. #include "rsUtils.h"
  19. #include "rsDefines.h"
  20. #include "rsInternalDefines.h"
  21. namespace android {
  22. namespace renderscript {
  23. class Context;
  24. class OStream;
  25. // An element is a group of Components that occupies one cell in a structure.
  26. class ObjectBase {
  27. public:
  28. static const bool gDebugStacks = false;
  29. static const bool gDebugReferences = false;
  30. static const bool gDebugLeaks = false;
  31. static const bool gDebugLifetime = false;
  32. ObjectBase(Context *rsc); // NOLINT, implicit
  33. void incSysRef() const;
  34. bool decSysRef() const;
  35. void incUserRef() const;
  36. bool decUserRef() const;
  37. bool zeroUserRef() const;
  38. static bool checkDelete(const ObjectBase *);
  39. const char * getName() const {
  40. return mName;
  41. }
  42. void assignName(const char *s) {mName = s;}
  43. void setName(const char *);
  44. void setName(const char *, uint32_t len);
  45. Context * getContext() const {return mRSC;}
  46. virtual bool freeChildren();
  47. static void zeroAllUserRef(Context *rsc);
  48. static void freeAllChildren(Context *rsc);
  49. static void dumpAll(Context *rsc);
  50. virtual void dumpLOGV(const char *prefix) const;
  51. virtual void serialize(Context *rsc, OStream *stream) const = 0;
  52. virtual RsA3DClassID getClassId() const = 0;
  53. static bool isValid(const Context *rsc, const ObjectBase *obj);
  54. // The async lock is taken during object creation in non-rs threads
  55. // and object deletion in the rs thread.
  56. static void asyncLock();
  57. static void asyncUnlock();
  58. virtual void callUpdateCacheObject(const Context *rsc, void *dstObj) const;
  59. protected:
  60. // Called inside the async lock for any object list management that is
  61. // necessary in derived classes.
  62. virtual void preDestroy() const;
  63. Context *mRSC;
  64. virtual ~ObjectBase();
  65. private:
  66. static pthread_mutex_t gObjectInitMutex;
  67. void add() const;
  68. void remove() const;
  69. const char* mName;
  70. mutable int32_t mSysRefCount;
  71. mutable int32_t mUserRefCount;
  72. mutable const ObjectBase * mPrev;
  73. mutable const ObjectBase * mNext;
  74. class DebugHelper *mDH;
  75. };
  76. template<class T>
  77. class ObjectBaseRef {
  78. public:
  79. ObjectBaseRef() {
  80. mRef = nullptr;
  81. }
  82. ObjectBaseRef(const ObjectBaseRef &ref) {
  83. mRef = ref.get();
  84. if (mRef) {
  85. mRef->incSysRef();
  86. }
  87. }
  88. ObjectBaseRef(T *ref) { // NOLINT, implicit
  89. mRef = ref;
  90. if (mRef) {
  91. ref->incSysRef();
  92. }
  93. }
  94. ObjectBaseRef & operator= (const ObjectBaseRef &ref) {
  95. if (&ref != this) {
  96. set(ref);
  97. }
  98. return *this;
  99. }
  100. ~ObjectBaseRef() {
  101. clear();
  102. }
  103. void set(T *ref) {
  104. if (mRef != ref) {
  105. clear();
  106. mRef = ref;
  107. if (mRef) {
  108. ref->incSysRef();
  109. }
  110. }
  111. }
  112. void set(const ObjectBaseRef &ref) {
  113. set(ref.mRef);
  114. }
  115. void clear() {
  116. if (mRef) {
  117. mRef->decSysRef();
  118. }
  119. mRef = nullptr;
  120. }
  121. inline T * get() const {
  122. return mRef;
  123. }
  124. inline T * operator-> () const {
  125. return mRef;
  126. }
  127. protected:
  128. T * mRef;
  129. };
  130. } // namespace renderscript
  131. } // namespace android
  132. #endif //ANDROID_RS_OBJECT_BASE_H