epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 10 | #include "GrGpuResource.h" |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 11 | #include "GrResourceCache2.h" |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 12 | #include "GrGpu.h" |
| 13 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 14 | static inline GrResourceCache2* get_resource_cache2(GrGpu* gpu) { |
| 15 | SkASSERT(NULL != gpu); |
| 16 | SkASSERT(NULL != gpu->getContext()); |
| 17 | SkASSERT(NULL != gpu->getContext()->getResourceCache2()); |
| 18 | return gpu->getContext()->getResourceCache2(); |
| 19 | } |
| 20 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 21 | GrGpuResource::GrGpuResource(GrGpu* gpu, bool isWrapped) |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 22 | : fGpu(gpu) |
| 23 | , fRefCnt(1) |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 24 | , fCacheEntry(NULL) |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 25 | , fUniqueID(CreateUniqueID()) |
| 26 | , fScratchKey(GrResourceKey::NullScratchKey()) { |
bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 27 | if (isWrapped) { |
robertphillips@google.com | 9ef0426 | 2013-10-29 14:06:15 +0000 | [diff] [blame] | 28 | fFlags = kWrapped_FlagBit; |
bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 29 | } else { |
| 30 | fFlags = 0; |
| 31 | } |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void GrGpuResource::registerWithCache() { |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 35 | get_resource_cache2(fGpu)->insertResource(this); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 36 | } |
| 37 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 38 | GrGpuResource::~GrGpuResource() { |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 39 | SkASSERT(0 == fRefCnt); |
bsalomon@google.com | 76b7fcc | 2012-04-27 17:24:09 +0000 | [diff] [blame] | 40 | // subclass should have released this. |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 41 | SkASSERT(this->wasDestroyed()); |
bsalomon@google.com | 76b7fcc | 2012-04-27 17:24:09 +0000 | [diff] [blame] | 42 | } |
| 43 | |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 44 | void GrGpuResource::release() { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 45 | if (NULL != fGpu) { |
| 46 | this->onRelease(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 47 | get_resource_cache2(fGpu)->removeResource(this); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 48 | fGpu = NULL; |
| 49 | } |
| 50 | } |
| 51 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 52 | void GrGpuResource::abandon() { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 53 | if (NULL != fGpu) { |
| 54 | this->onAbandon(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 55 | get_resource_cache2(fGpu)->removeResource(this); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 56 | fGpu = NULL; |
| 57 | } |
| 58 | } |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 59 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 60 | const GrContext* GrGpuResource::getContext() const { |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 61 | if (NULL != fGpu) { |
| 62 | return fGpu->getContext(); |
| 63 | } else { |
| 64 | return NULL; |
| 65 | } |
| 66 | } |
| 67 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 68 | GrContext* GrGpuResource::getContext() { |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 69 | if (NULL != fGpu) { |
| 70 | return fGpu->getContext(); |
| 71 | } else { |
| 72 | return NULL; |
| 73 | } |
| 74 | } |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 75 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 76 | void GrGpuResource::setScratchKey(const GrResourceKey& scratchKey) { |
| 77 | SkASSERT(fScratchKey.isNullScratch()); |
| 78 | SkASSERT(scratchKey.isScratch()); |
| 79 | SkASSERT(!scratchKey.isNullScratch()); |
| 80 | fScratchKey = scratchKey; |
| 81 | } |
| 82 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 83 | uint32_t GrGpuResource::CreateUniqueID() { |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 84 | static int32_t gUniqueID = SK_InvalidUniqueID; |
| 85 | uint32_t id; |
| 86 | do { |
| 87 | id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1); |
| 88 | } while (id == SK_InvalidUniqueID); |
| 89 | return id; |
| 90 | } |