blob: bce7d2cae8b9c85661d1c9be14c9c63dd9ad5c53 [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)
bsalomon744998e2014-08-28 09:54:34 -070025 , fUniqueID(CreateUniqueID())
26 , fScratchKey(GrResourceKey::NullScratchKey()) {
bsalomon@google.com72830222013-01-23 20:25:22 +000027 if (isWrapped) {
robertphillips@google.com9ef04262013-10-29 14:06:15 +000028 fFlags = kWrapped_FlagBit;
bsalomon@google.com72830222013-01-23 20:25:22 +000029 } else {
30 fFlags = 0;
31 }
bsalomon16961262014-08-26 14:01:07 -070032}
33
34void GrGpuResource::registerWithCache() {
bsalomonc8dc1f72014-08-21 13:02:13 -070035 get_resource_cache2(fGpu)->insertResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000036}
37
bsalomon6d3fe022014-07-25 08:35:45 -070038GrGpuResource::~GrGpuResource() {
bsalomonc44be0e2014-07-25 07:32:33 -070039 SkASSERT(0 == fRefCnt);
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000040 // subclass should have released this.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000041 SkASSERT(this->wasDestroyed());
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000042}
43
bsalomon16961262014-08-26 14:01:07 -070044void GrGpuResource::release() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000045 if (NULL != fGpu) {
46 this->onRelease();
bsalomonc8dc1f72014-08-21 13:02:13 -070047 get_resource_cache2(fGpu)->removeResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000048 fGpu = NULL;
49 }
50}
51
bsalomon6d3fe022014-07-25 08:35:45 -070052void GrGpuResource::abandon() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000053 if (NULL != fGpu) {
54 this->onAbandon();
bsalomonc8dc1f72014-08-21 13:02:13 -070055 get_resource_cache2(fGpu)->removeResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000056 fGpu = NULL;
57 }
58}
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000059
bsalomon6d3fe022014-07-25 08:35:45 -070060const GrContext* GrGpuResource::getContext() const {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000061 if (NULL != fGpu) {
62 return fGpu->getContext();
63 } else {
64 return NULL;
65 }
66}
67
bsalomon6d3fe022014-07-25 08:35:45 -070068GrContext* GrGpuResource::getContext() {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000069 if (NULL != fGpu) {
70 return fGpu->getContext();
71 } else {
72 return NULL;
73 }
74}
bsalomonc44be0e2014-07-25 07:32:33 -070075
bsalomon744998e2014-08-28 09:54:34 -070076void GrGpuResource::setScratchKey(const GrResourceKey& scratchKey) {
77 SkASSERT(fScratchKey.isNullScratch());
78 SkASSERT(scratchKey.isScratch());
79 SkASSERT(!scratchKey.isNullScratch());
80 fScratchKey = scratchKey;
81}
82
bsalomon6d3fe022014-07-25 08:35:45 -070083uint32_t GrGpuResource::CreateUniqueID() {
bsalomonc44be0e2014-07-25 07:32:33 -070084 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}