blob: a0a72785d9c74c0385e52d1b08da7beeac4eca99 [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 }
bsalomon16961262014-08-26 14:01:07 -070031}
32
33void GrGpuResource::registerWithCache() {
bsalomonc8dc1f72014-08-21 13:02:13 -070034 get_resource_cache2(fGpu)->insertResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000035}
36
bsalomon6d3fe022014-07-25 08:35:45 -070037GrGpuResource::~GrGpuResource() {
bsalomonc44be0e2014-07-25 07:32:33 -070038 SkASSERT(0 == fRefCnt);
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000039 // subclass should have released this.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000040 SkASSERT(this->wasDestroyed());
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000041}
42
bsalomon16961262014-08-26 14:01:07 -070043void GrGpuResource::release() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000044 if (NULL != fGpu) {
45 this->onRelease();
bsalomonc8dc1f72014-08-21 13:02:13 -070046 get_resource_cache2(fGpu)->removeResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000047 fGpu = NULL;
48 }
49}
50
bsalomon6d3fe022014-07-25 08:35:45 -070051void GrGpuResource::abandon() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000052 if (NULL != fGpu) {
53 this->onAbandon();
bsalomonc8dc1f72014-08-21 13:02:13 -070054 get_resource_cache2(fGpu)->removeResource(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000055 fGpu = NULL;
56 }
57}
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000058
bsalomon6d3fe022014-07-25 08:35:45 -070059const GrContext* GrGpuResource::getContext() const {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000060 if (NULL != fGpu) {
61 return fGpu->getContext();
62 } else {
63 return NULL;
64 }
65}
66
bsalomon6d3fe022014-07-25 08:35:45 -070067GrContext* GrGpuResource::getContext() {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000068 if (NULL != fGpu) {
69 return fGpu->getContext();
70 } else {
71 return NULL;
72 }
73}
bsalomonc44be0e2014-07-25 07:32:33 -070074
bsalomon6d3fe022014-07-25 08:35:45 -070075uint32_t GrGpuResource::CreateUniqueID() {
bsalomonc44be0e2014-07-25 07:32:33 -070076 static int32_t gUniqueID = SK_InvalidUniqueID;
77 uint32_t id;
78 do {
79 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1);
80 } while (id == SK_InvalidUniqueID);
81 return id;
82}