blob: 7a19c25e46dcf5806db47955b7f22818d75f95a5 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com8fe72472011-03-30 21:26:44 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.com8fe72472011-03-30 21:26:44 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon6d3fe022014-07-25 08:35:45 -070010#include "GrGpuResource.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070011#include "GrResourceCache2.h"
bsalomon@google.com8fe72472011-03-30 21:26:44 +000012#include "GrGpu.h"
13
bsalomonc8dc1f72014-08-21 13:02:13 -070014static 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
bsalomon6d3fe022014-07-25 08:35:45 -070021GrGpuResource::GrGpuResource(GrGpu* gpu, bool isWrapped)
bsalomonc8dc1f72014-08-21 13:02:13 -070022 : fGpu(gpu)
23 , fRefCnt(1)
bsalomonc44be0e2014-07-25 07:32:33 -070024 , fCacheEntry(NULL)
25 , fUniqueID(CreateUniqueID()) {
bsalomon@google.com72830222013-01-23 20:25:22 +000026 if (isWrapped) {
robertphillips@google.com9ef04262013-10-29 14:06:15 +000027 fFlags = kWrapped_FlagBit;
bsalomon@google.com72830222013-01-23 20:25:22 +000028 } else {
29 fFlags = 0;
30 }
bsalomonc8dc1f72014-08-21 13:02:13 -070031 get_resource_cache2(fGpu)->insertResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000032}
33
bsalomon6d3fe022014-07-25 08:35:45 -070034GrGpuResource::~GrGpuResource() {
bsalomonc44be0e2014-07-25 07:32:33 -070035 SkASSERT(0 == fRefCnt);
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000036 // subclass should have released this.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000037 SkASSERT(this->wasDestroyed());
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000038}
39
bsalomona2b0af82014-08-26 13:11:11 -070040void GrGpuResource::release() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000041 if (NULL != fGpu) {
42 this->onRelease();
bsalomonc8dc1f72014-08-21 13:02:13 -070043 get_resource_cache2(fGpu)->removeResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000044 fGpu = NULL;
45 }
46}
47
bsalomon6d3fe022014-07-25 08:35:45 -070048void GrGpuResource::abandon() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000049 if (NULL != fGpu) {
50 this->onAbandon();
bsalomonc8dc1f72014-08-21 13:02:13 -070051 get_resource_cache2(fGpu)->removeResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000052 fGpu = NULL;
53 }
54}
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000055
bsalomon6d3fe022014-07-25 08:35:45 -070056const GrContext* GrGpuResource::getContext() const {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000057 if (NULL != fGpu) {
58 return fGpu->getContext();
59 } else {
60 return NULL;
61 }
62}
63
bsalomon6d3fe022014-07-25 08:35:45 -070064GrContext* GrGpuResource::getContext() {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000065 if (NULL != fGpu) {
66 return fGpu->getContext();
67 } else {
68 return NULL;
69 }
70}
bsalomonc44be0e2014-07-25 07:32:33 -070071
bsalomon6d3fe022014-07-25 08:35:45 -070072uint32_t GrGpuResource::CreateUniqueID() {
bsalomonc44be0e2014-07-25 07:32:33 -070073 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}