blob: 4b47a1c93296e1d825b92da1819136412cc13012 [file] [log] [blame]
bsalomon453cf402014-11-11 14:15:57 -08001
2/*
3 * Copyright 2014 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.
7 */
8
9#ifndef GrGpuResourceCacheAccess_DEFINED
10#define GrGpuResourceCacheAccess_DEFINED
11
12#include "GrGpuResource.h"
bsalomon3582d3e2015-02-13 14:20:05 -080013#include "GrGpuResourcePriv.h"
14
15namespace skiatest {
16 class Reporter;
17}
bsalomon453cf402014-11-11 14:15:57 -080018
19/**
bsalomon3582d3e2015-02-13 14:20:05 -080020 * This class allows GrResourceCache increased privileged access to GrGpuResource objects.
bsalomon453cf402014-11-11 14:15:57 -080021 */
22class GrGpuResource::CacheAccess {
bsalomon3582d3e2015-02-13 14:20:05 -080023private:
bsalomon453cf402014-11-11 14:15:57 -080024 /**
bsalomonc2f35b72015-01-23 07:19:22 -080025 * Is the resource currently cached as scratch? This means it is cached, has a valid scratch
bsalomon8718aaf2015-02-19 07:24:21 -080026 * key, and does not have a unique key.
bsalomon453cf402014-11-11 14:15:57 -080027 */
28 bool isScratch() const {
bsalomon8718aaf2015-02-19 07:24:21 -080029 return !fResource->getUniqueKey().isValid() && fResource->fScratchKey.isValid() &&
bsalomon5ec26ae2016-02-25 08:33:02 -080030 SkBudgeted::kYes == fResource->resourcePriv().isBudgeted();
bsalomon453cf402014-11-11 14:15:57 -080031 }
32
bsalomon10e23ca2014-11-25 05:52:06 -080033 /**
bsalomon6dc6f5f2015-06-18 09:12:16 -070034 * Is the resource object wrapping an externally allocated GPU resource that Skia has not taken
35 * ownership of.
36 */
37 bool isBorrowed() const { return GrGpuResource::kBorrowed_LifeCycle == fResource->fLifeCycle; }
38
39 /**
40 * Is the resource object wrapping an externally allocated GPU resource that Skia has taken
41 * ownership of.
42 */
43 bool isAdopted() const { return GrGpuResource::kAdopted_LifeCycle == fResource->fLifeCycle; }
bsalomon3582d3e2015-02-13 14:20:05 -080044
bsalomonc2f35b72015-01-23 07:19:22 -080045 /**
bsalomon12299ab2014-11-14 13:33:09 -080046 * Called by the cache to delete the resource under normal circumstances.
47 */
48 void release() {
49 fResource->release();
halcanary385fe4d2015-08-26 13:07:48 -070050 if (fResource->isPurgeable()) {
51 delete fResource;
bsalomon12299ab2014-11-14 13:33:09 -080052 }
53 }
54
55 /**
56 * Called by the cache to delete the resource when the backend 3D context is no longer valid.
57 */
58 void abandon() {
59 fResource->abandon();
halcanary385fe4d2015-08-26 13:07:48 -070060 if (fResource->isPurgeable()) {
61 delete fResource;
bsalomon12299ab2014-11-14 13:33:09 -080062 }
63 }
64
bsalomonf99e9612015-02-19 08:24:16 -080065 /** Called by the cache to assign a new unique key. */
66 void setUniqueKey(const GrUniqueKey& key) { fResource->fUniqueKey = key; }
67
68 /** Called by the cache to make the unique key invalid. */
69 void removeUniqueKey() { fResource->fUniqueKey.reset(); }
70
bsalomon9f2d1572015-02-17 11:47:40 -080071 uint32_t timestamp() const { return fResource->fTimestamp; }
72 void setTimestamp(uint32_t ts) { fResource->fTimestamp = ts; }
73
74 int* accessCacheIndex() const { return &fResource->fCacheArrayIndex; }
75
bsalomon3582d3e2015-02-13 14:20:05 -080076 CacheAccess(GrGpuResource* resource) : fResource(resource) {}
77 CacheAccess(const CacheAccess& that) : fResource(that.fResource) {}
bsalomon453cf402014-11-11 14:15:57 -080078 CacheAccess& operator=(const CacheAccess&); // unimpl
79
80 // No taking addresses of this type.
81 const CacheAccess* operator&() const;
82 CacheAccess* operator&();
83
84 GrGpuResource* fResource;
85
86 friend class GrGpuResource; // to construct/copy this type.
bsalomon3582d3e2015-02-13 14:20:05 -080087 friend class GrResourceCache; // to use this type
88 friend void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); // for unit testing
bsalomon453cf402014-11-11 14:15:57 -080089};
90
91inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); }
92
93inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const {
94 return CacheAccess(const_cast<GrGpuResource*>(this));
95}
96
97#endif