blob: 8c2dc4508009a10fa181fd477636c35743b94a1d [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"
bsalomon@google.com8fe72472011-03-30 21:26:44 +000011#include "GrGpu.h"
12
bsalomon6d3fe022014-07-25 08:35:45 -070013GrGpuResource::GrGpuResource(GrGpu* gpu, bool isWrapped)
bsalomonc44be0e2014-07-25 07:32:33 -070014 : fRefCnt(1)
15 , fCacheEntry(NULL)
16 , fUniqueID(CreateUniqueID()) {
bsalomon@google.com838f6e12013-01-23 21:37:01 +000017 fGpu = gpu;
bsalomon@google.com72830222013-01-23 20:25:22 +000018 if (isWrapped) {
robertphillips@google.com9ef04262013-10-29 14:06:15 +000019 fFlags = kWrapped_FlagBit;
bsalomon@google.com72830222013-01-23 20:25:22 +000020 } else {
21 fFlags = 0;
22 }
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000023 fGpu->insertObject(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000024}
25
bsalomon6d3fe022014-07-25 08:35:45 -070026GrGpuResource::~GrGpuResource() {
bsalomonc44be0e2014-07-25 07:32:33 -070027 SkASSERT(0 == fRefCnt);
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000028 // subclass should have released this.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000029 SkASSERT(this->wasDestroyed());
bsalomon@google.com76b7fcc2012-04-27 17:24:09 +000030}
31
bsalomon6d3fe022014-07-25 08:35:45 -070032void GrGpuResource::release() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000033 if (NULL != fGpu) {
34 this->onRelease();
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000035 fGpu->removeObject(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000036 fGpu = NULL;
37 }
38}
39
bsalomon6d3fe022014-07-25 08:35:45 -070040void GrGpuResource::abandon() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000041 if (NULL != fGpu) {
42 this->onAbandon();
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000043 fGpu->removeObject(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000044 fGpu = NULL;
45 }
46}
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000047
bsalomon6d3fe022014-07-25 08:35:45 -070048const GrContext* GrGpuResource::getContext() const {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000049 if (NULL != fGpu) {
50 return fGpu->getContext();
51 } else {
52 return NULL;
53 }
54}
55
bsalomon6d3fe022014-07-25 08:35:45 -070056GrContext* GrGpuResource::getContext() {
bsalomon@google.comf7b5c1e2011-11-15 19:42:07 +000057 if (NULL != fGpu) {
58 return fGpu->getContext();
59 } else {
60 return NULL;
61 }
62}
bsalomonc44be0e2014-07-25 07:32:33 -070063
bsalomon6d3fe022014-07-25 08:35:45 -070064uint32_t GrGpuResource::CreateUniqueID() {
bsalomonc44be0e2014-07-25 07:32:33 -070065 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}