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 | 00b76bd | 2014-09-03 14:05:49 -0700 | [diff] [blame^] | 14 | GrGpuRef::~GrGpuRef() { |
| 15 | SkASSERT(0 == fRefCnt); |
| 16 | SkASSERT(0 == fPendingReads); |
| 17 | SkASSERT(0 == fPendingWrites); |
| 18 | // Set to invalid values. |
| 19 | SkDEBUGCODE(fRefCnt = fPendingReads = fPendingWrites = -10;) |
| 20 | } |
| 21 | |
| 22 | /////////////////////////////////////////////////////////////////////////////// |
| 23 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 24 | static inline GrResourceCache2* get_resource_cache2(GrGpu* gpu) { |
| 25 | SkASSERT(NULL != gpu); |
| 26 | SkASSERT(NULL != gpu->getContext()); |
| 27 | SkASSERT(NULL != gpu->getContext()->getResourceCache2()); |
| 28 | return gpu->getContext()->getResourceCache2(); |
| 29 | } |
| 30 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 31 | GrGpuResource::GrGpuResource(GrGpu* gpu, bool isWrapped) |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 32 | : fGpu(gpu) |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 33 | , fCacheEntry(NULL) |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 34 | , fUniqueID(CreateUniqueID()) |
| 35 | , fScratchKey(GrResourceKey::NullScratchKey()) { |
bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 36 | if (isWrapped) { |
robertphillips@google.com | 9ef0426 | 2013-10-29 14:06:15 +0000 | [diff] [blame] | 37 | fFlags = kWrapped_FlagBit; |
bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 38 | } else { |
| 39 | fFlags = 0; |
| 40 | } |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void GrGpuResource::registerWithCache() { |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 44 | get_resource_cache2(fGpu)->insertResource(this); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 45 | } |
| 46 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 47 | GrGpuResource::~GrGpuResource() { |
bsalomon@google.com | 76b7fcc | 2012-04-27 17:24:09 +0000 | [diff] [blame] | 48 | // subclass should have released this. |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 49 | SkASSERT(this->wasDestroyed()); |
bsalomon@google.com | 76b7fcc | 2012-04-27 17:24:09 +0000 | [diff] [blame] | 50 | } |
| 51 | |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 52 | void GrGpuResource::release() { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 53 | if (NULL != fGpu) { |
| 54 | this->onRelease(); |
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 | } |
| 59 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 60 | void GrGpuResource::abandon() { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 61 | if (NULL != fGpu) { |
| 62 | this->onAbandon(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 63 | get_resource_cache2(fGpu)->removeResource(this); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 64 | fGpu = NULL; |
| 65 | } |
| 66 | } |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 67 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 68 | const GrContext* GrGpuResource::getContext() const { |
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 | } |
| 75 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 76 | GrContext* GrGpuResource::getContext() { |
bsalomon@google.com | f7b5c1e | 2011-11-15 19:42:07 +0000 | [diff] [blame] | 77 | if (NULL != fGpu) { |
| 78 | return fGpu->getContext(); |
| 79 | } else { |
| 80 | return NULL; |
| 81 | } |
| 82 | } |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 83 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 84 | void GrGpuResource::setScratchKey(const GrResourceKey& scratchKey) { |
| 85 | SkASSERT(fScratchKey.isNullScratch()); |
| 86 | SkASSERT(scratchKey.isScratch()); |
| 87 | SkASSERT(!scratchKey.isNullScratch()); |
| 88 | fScratchKey = scratchKey; |
| 89 | } |
| 90 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 91 | uint32_t GrGpuResource::CreateUniqueID() { |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 92 | static int32_t gUniqueID = SK_InvalidUniqueID; |
| 93 | uint32_t id; |
| 94 | do { |
| 95 | id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1); |
| 96 | } while (id == SK_InvalidUniqueID); |
| 97 | return id; |
| 98 | } |