blob: 4a72a50c499689b8ae48f871e58afb68161a45fd [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
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000010#include "GrGpuObject.h"
bsalomon@google.com8fe72472011-03-30 21:26:44 +000011#include "GrGpu.h"
12
bsalomonc44be0e2014-07-25 07:32:33 -070013GrGpuObject::GrGpuObject(GrGpu* gpu, bool isWrapped)
14 : 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
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000026GrGpuObject::~GrGpuObject() {
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
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000032void GrGpuObject::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
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000040void GrGpuObject::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
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000048const GrContext* GrGpuObject::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
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000056GrContext* GrGpuObject::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
64uint32_t GrGpuObject::CreateUniqueID() {
65 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}