| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef GrGpuResource_DEFINED | 8 #ifndef GrGpuResource_DEFINED |
| 9 #define GrGpuResource_DEFINED | 9 #define GrGpuResource_DEFINED |
| 10 | 10 |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 friend class GrResourceCache; // to check IO ref counts. | 132 friend class GrResourceCache; // to check IO ref counts. |
| 133 | 133 |
| 134 template <typename, GrIOType> friend class GrPendingIOResource; | 134 template <typename, GrIOType> friend class GrPendingIOResource; |
| 135 }; | 135 }; |
| 136 | 136 |
| 137 /** | 137 /** |
| 138 * Base class for objects that can be kept in the GrResourceCache. | 138 * Base class for objects that can be kept in the GrResourceCache. |
| 139 */ | 139 */ |
| 140 class SK_API GrGpuResource : public GrIORef<GrGpuResource> { | 140 class SK_API GrGpuResource : public GrIORef<GrGpuResource> { |
| 141 public: | 141 public: |
| 142 | |
| 143 | |
| 144 enum LifeCycle { | |
| 145 /** | |
| 146 * The resource is cached and owned by Skia. Resources with this status
may be kept alive | |
| 147 * by the cache as either scratch or unique resources even when there ar
e no refs to them. | |
| 148 * The cache may release them whenever there are no refs. | |
| 149 */ | |
| 150 kCached_LifeCycle, | |
| 151 | |
| 152 /** | |
| 153 * The resource is uncached. As soon as there are no more refs to it, it
is released. Under | |
| 154 * the hood the cache may opaquely recycle it as a cached resource. | |
| 155 */ | |
| 156 kUncached_LifeCycle, | |
| 157 | |
| 158 /** | |
| 159 * Similar to uncached, but Skia does not manage the lifetime of the und
erlying backend | |
| 160 * 3D API object(s). The client is responsible for freeing those. Used t
o inject client- | |
| 161 * created GPU resources into Skia (e.g. to render to a client-created t
exture). | |
| 162 */ | |
| 163 kBorrowed_LifeCycle, | |
| 164 | |
| 165 /** | |
| 166 * An external resource with ownership transfered into Skia. Skia will f
ree the resource. | |
| 167 */ | |
| 168 kAdopted_LifeCycle, | |
| 169 }; | |
| 170 | |
| 171 /** | 142 /** |
| 172 * Tests whether a object has been abandoned or released. All objects will | 143 * Tests whether a object has been abandoned or released. All objects will |
| 173 * be in this state after their creating GrContext is destroyed or has | 144 * be in this state after their creating GrContext is destroyed or has |
| 174 * contextLost called. It's up to the client to test wasDestroyed() before | 145 * contextLost called. It's up to the client to test wasDestroyed() before |
| 175 * attempting to use an object if it holds refs on objects across | 146 * attempting to use an object if it holds refs on objects across |
| 176 * ~GrContext, freeResources with the force flag, or contextLost. | 147 * ~GrContext, freeResources with the force flag, or contextLost. |
| 177 * | 148 * |
| 178 * @return true if the object has been released or abandoned, | 149 * @return true if the object has been released or abandoned, |
| 179 * false otherwise. | 150 * false otherwise. |
| 180 */ | 151 */ |
| 181 bool wasDestroyed() const { return NULL == fGpu; } | 152 bool wasDestroyed() const { return NULL == fGpu; } |
| 182 | 153 |
| 183 /** | 154 /** |
| 184 * Retrieves the context that owns the object. Note that it is possible for | 155 * Retrieves the context that owns the object. Note that it is possible for |
| 185 * this to return NULL. When objects have been release()ed or abandon()ed | 156 * this to return NULL. When objects have been release()ed or abandon()ed |
| 186 * they no longer have an owning context. Destroying a GrContext | 157 * they no longer have an owning context. Destroying a GrContext |
| 187 * automatically releases all its resources. | 158 * automatically releases all its resources. |
| 188 */ | 159 */ |
| 189 const GrContext* getContext() const; | 160 const GrContext* getContext() const; |
| 190 GrContext* getContext(); | 161 GrContext* getContext(); |
| 191 | 162 |
| 192 /** | 163 /** |
| 193 * Retrieves the amount of GPU memory used by this resource in bytes. It is | 164 * Retrieves the amount of GPU memory used by Skia through this resource in
bytes. It is |
| 194 * approximate since we aren't aware of additional padding or copies made | 165 * approximate since we aren't aware of additional padding or copies made |
| 195 * by the driver. | 166 * by the driver. |
| 196 * | 167 * |
| 197 * @return the amount of GPU memory used in bytes | 168 * If the resource refers to external backend objects, does not count the si
ze of |
| 169 * those objects. |
| 170 * |
| 171 * @return the amount of GPU memory used in bytes by Skia |
| 198 */ | 172 */ |
| 199 size_t gpuMemorySize() const { | 173 size_t gpuMemorySize() const { |
| 200 if (kInvalidGpuMemorySize == fGpuMemorySize) { | 174 if (kInvalidGpuMemorySize == fGpuMemorySize) { |
| 201 fGpuMemorySize = this->onGpuMemorySize(); | 175 fGpuMemorySize = this->onGpuMemorySize(); |
| 202 SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize); | 176 SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize); |
| 203 } | 177 } |
| 204 return fGpuMemorySize; | 178 return fGpuMemorySize; |
| 205 } | 179 } |
| 206 | 180 |
| 207 /** | 181 /** |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 * Typically, subclasses should not need to override this, and should only | 232 * Typically, subclasses should not need to override this, and should only |
| 259 * need to override setMemoryBacking. | 233 * need to override setMemoryBacking. |
| 260 **/ | 234 **/ |
| 261 virtual void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const; | 235 virtual void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const; |
| 262 | 236 |
| 263 protected: | 237 protected: |
| 264 // This must be called by every GrGpuObject. It should be called once the ob
ject is fully | 238 // This must be called by every GrGpuObject. It should be called once the ob
ject is fully |
| 265 // initialized (i.e. not in a base class constructor). | 239 // initialized (i.e. not in a base class constructor). |
| 266 void registerWithCache(); | 240 void registerWithCache(); |
| 267 | 241 |
| 268 GrGpuResource(GrGpu*, LifeCycle); | 242 GrGpuResource(GrGpu*, SkBudgeted); |
| 269 virtual ~GrGpuResource(); | 243 virtual ~GrGpuResource(); |
| 270 | 244 |
| 271 GrGpu* getGpu() const { return fGpu; } | 245 GrGpu* getGpu() const { return fGpu; } |
| 272 | 246 |
| 273 /** Overridden to free GPU resources in the backend API. */ | 247 /** Overridden to free GPU resources in the backend API. */ |
| 274 virtual void onRelease() { } | 248 virtual void onRelease() { } |
| 275 /** Overridden to abandon any internal handles, ptrs, etc to backend API res
ources. | 249 /** Overridden to abandon any internal handles, ptrs, etc to backend API res
ources. |
| 276 This may be called when the underlying 3D context is no longer valid and
so no | 250 This may be called when the underlying 3D context is no longer valid and
so no |
| 277 backend API calls should be made. */ | 251 backend API calls should be made. */ |
| 278 virtual void onAbandon() { } | 252 virtual void onAbandon() { } |
| 279 | 253 |
| 280 bool shouldFreeResources() const { return fLifeCycle != kBorrowed_LifeCycle;
} | 254 /** Overridden to indicate whether the object references externally allocate
d backend API |
| 281 | 255 * resources. */ |
| 282 bool isExternal() const { | 256 virtual bool refsWrappedResources() const = 0; |
| 283 return GrGpuResource::kAdopted_LifeCycle == fLifeCycle || | |
| 284 GrGpuResource::kBorrowed_LifeCycle == fLifeCycle; | |
| 285 } | |
| 286 | 257 |
| 287 /** | 258 /** |
| 288 * This entry point should be called whenever gpuMemorySize() should report
a different size. | 259 * This entry point should be called whenever gpuMemorySize() should report
a different size. |
| 289 * The cache will call gpuMemorySize() to update the current size of the res
ource. | 260 * The cache will call gpuMemorySize() to update the current size of the res
ource. |
| 290 */ | 261 */ |
| 291 void didChangeGpuMemorySize() const; | 262 void didChangeGpuMemorySize() const; |
| 292 | 263 |
| 293 /** | 264 /** |
| 294 * Optionally called by the GrGpuResource subclass if the resource can be us
ed as scratch. | 265 * Optionally called by the GrGpuResource subclass if the resource can be us
ed as scratch. |
| 295 * By default resources are not usable as scratch. This should only be calle
d once. | 266 * By default resources are not usable as scratch. This should only be calle
d once. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 uint32_t fTimestamp; | 304 uint32_t fTimestamp; |
| 334 | 305 |
| 335 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0); | 306 static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0); |
| 336 GrScratchKey fScratchKey; | 307 GrScratchKey fScratchKey; |
| 337 GrUniqueKey fUniqueKey; | 308 GrUniqueKey fUniqueKey; |
| 338 | 309 |
| 339 // This is not ref'ed but abandon() or release() will be called before the G
rGpu object | 310 // This is not ref'ed but abandon() or release() will be called before the G
rGpu object |
| 340 // is destroyed. Those calls set will this to NULL. | 311 // is destroyed. Those calls set will this to NULL. |
| 341 GrGpu* fGpu; | 312 GrGpu* fGpu; |
| 342 mutable size_t fGpuMemorySize; | 313 mutable size_t fGpuMemorySize; |
| 343 | 314 SkBudgeted fBudgeted; |
| 344 LifeCycle fLifeCycle; | |
| 345 const uint32_t fUniqueID; | 315 const uint32_t fUniqueID; |
| 346 | 316 |
| 347 SkAutoTUnref<const SkData> fData; | 317 SkAutoTUnref<const SkData> fData; |
| 348 | 318 |
| 349 typedef GrIORef<GrGpuResource> INHERITED; | 319 typedef GrIORef<GrGpuResource> INHERITED; |
| 350 friend class GrIORef<GrGpuResource>; // to access notifyAllCntsAreZero and n
otifyRefCntIsZero. | 320 friend class GrIORef<GrGpuResource>; // to access notifyAllCntsAreZero and n
otifyRefCntIsZero. |
| 351 }; | 321 }; |
| 352 | 322 |
| 353 #endif | 323 #endif |
| OLD | NEW |