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