blob: d29ab6d486059b03bb00b92ebdf03ea3eabc0774 [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
bsalomon00b76bd2014-09-03 14:05:49 -070014GrGpuRef::~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
bsalomonc8dc1f72014-08-21 13:02:13 -070024static 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
bsalomon6d3fe022014-07-25 08:35:45 -070031GrGpuResource::GrGpuResource(GrGpu* gpu, bool isWrapped)
bsalomonc8dc1f72014-08-21 13:02:13 -070032 : fGpu(gpu)
bsalomonc44be0e2014-07-25 07:32:33 -070033 , fCacheEntry(NULL)
bsalomon744998e2014-08-28 09:54:34 -070034 , fUniqueID(CreateUniqueID())
35 , fScratchKey(GrResourceKey::NullScratchKey()) {
bsalomon@google.com72830222013-01-23 20:25:22 +000036 if (isWrapped) {
robertphillips@google.com9ef04262013-10-29 14:06:15 +000037 fFlags = kWrapped_FlagBit;
bsalomon@google.com72830222013-01-23 20:25:22 +000038 } else {
39 fFlags = 0;
40 }
bsalomon16961262014-08-26 14:01:07 -070041}
42
43void GrGpuResource::registerWithCache() {
bsalomonc8dc1f72014-08-21 13:02:13 -070044 get_resource_cache2(fGpu)->insertResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000045}
46
bsalomon6d3fe022014-07-25 08:35:45 -070047GrGpuResource::~GrGpuResource() {
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000048 // subclass should have released this.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000049 SkASSERT(this->wasDestroyed());
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000050}
51
bsalomon16961262014-08-26 14:01:07 -070052void GrGpuResource::release() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000053 if (NULL != fGpu) {
54 this->onRelease();
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}
59
bsalomon6d3fe022014-07-25 08:35:45 -070060void GrGpuResource::abandon() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000061 if (NULL != fGpu) {
62 this->onAbandon();
bsalomonc8dc1f72014-08-21 13:02:13 -070063 get_resource_cache2(fGpu)->removeResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000064 fGpu = NULL;
65 }
66}
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000067
bsalomon6d3fe022014-07-25 08:35:45 -070068const GrContext* GrGpuResource::getContext() const {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000069 if (NULL != fGpu) {
70 return fGpu->getContext();
71 } else {
72 return NULL;
73 }
74}
75
bsalomon6d3fe022014-07-25 08:35:45 -070076GrContext* GrGpuResource::getContext() {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000077 if (NULL != fGpu) {
78 return fGpu->getContext();
79 } else {
80 return NULL;
81 }
82}
bsalomonc44be0e2014-07-25 07:32:33 -070083
bsalomon744998e2014-08-28 09:54:34 -070084void GrGpuResource::setScratchKey(const GrResourceKey& scratchKey) {
85 SkASSERT(fScratchKey.isNullScratch());
86 SkASSERT(scratchKey.isScratch());
87 SkASSERT(!scratchKey.isNullScratch());
88 fScratchKey = scratchKey;
89}
90
bsalomon6d3fe022014-07-25 08:35:45 -070091uint32_t GrGpuResource::CreateUniqueID() {
bsalomonc44be0e2014-07-25 07:32:33 -070092 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}