| 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) |
| 25 | , fUniqueID(CreateUniqueID()) { |
| bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 26 | if (isWrapped) { |
| robertphillips@google.com | 9ef0426 | 2013-10-29 14:06:15 +0000 | [diff] [blame] | 27 | fFlags = kWrapped_FlagBit; |
| bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 28 | } else { |
| 29 | fFlags = 0; |
| 30 | } |
| bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 31 | get_resource_cache2(fGpu)->insertResource(this); |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 34 | GrGpuResource::~GrGpuResource() { |
| bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 35 | SkASSERT(0 == fRefCnt); |
| bsalomon@google.com | 76b7fcc | 2012-04-27 17:24:09 +0000 | [diff] [blame] | 36 | // subclass should have released this. |
| commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 37 | SkASSERT(this->wasDestroyed()); |
| bsalomon@google.com | 76b7fcc | 2012-04-27 17:24:09 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| bsalomon | a2b0af8 | 2014-08-26 13:11:11 -0700 | [diff] [blame] | 40 | void GrGpuResource::release() { |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 41 | if (NULL != fGpu) { |
| 42 | this->onRelease(); |
| bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 43 | get_resource_cache2(fGpu)->removeResource(this); |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 44 | fGpu = NULL; |
| 45 | } |
| 46 | } |
| 47 | |
| bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 48 | void GrGpuResource::abandon() { |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 49 | if (NULL != fGpu) { |
| 50 | this->onAbandon(); |
| bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 51 | get_resource_cache2(fGpu)->removeResource(this); |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 52 | fGpu = NULL; |
| 53 | } |
| 54 | } |
| bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 55 | |
| bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 56 | const GrContext* GrGpuResource::getContext() const { |
| bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 57 | if (NULL != fGpu) { |
| 58 | return fGpu->getContext(); |
| 59 | } else { |
| 60 | return NULL; |
| 61 | } |
| 62 | } |
| 63 | |
| bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 64 | GrContext* GrGpuResource::getContext() { |
| bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 65 | if (NULL != fGpu) { |
| 66 | return fGpu->getContext(); |
| 67 | } else { |
| 68 | return NULL; |
| 69 | } |
| 70 | } |
| bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 71 | |
| bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 72 | uint32_t GrGpuResource::CreateUniqueID() { |
| bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 73 | static int32_t gUniqueID = SK_InvalidUniqueID; |
| 74 | uint32_t id; |
| 75 | do { |
| 76 | id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1); |
| 77 | } while (id == SK_InvalidUniqueID); |
| 78 | return id; |
| 79 | } |