blob: 922e3b36b5a47541874507dcba23bc008c4f5ab1 [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
26 * key, and does not have a content key.
bsalomon453cf402014-11-11 14:15:57 -080027 */
28 bool isScratch() const {
bsalomon563ff602015-02-02 17:25:26 -080029 return !fResource->getContentKey().isValid() && fResource->fScratchKey.isValid() &&
bsalomon3582d3e2015-02-13 14:20:05 -080030 fResource->resourcePriv().isBudgeted();
bsalomon453cf402014-11-11 14:15:57 -080031 }
32
bsalomon10e23ca2014-11-25 05:52:06 -080033 /**
bsalomon84c8e622014-11-17 09:33:27 -080034 * Is the resource object wrapping an externally allocated GPU resource?
35 */
bsalomon5236cf42015-01-14 10:42:08 -080036 bool isWrapped() const { return GrGpuResource::kWrapped_LifeCycle == fResource->fLifeCycle; }
bsalomon3582d3e2015-02-13 14:20:05 -080037
bsalomonc2f35b72015-01-23 07:19:22 -080038 /**
bsalomon12299ab2014-11-14 13:33:09 -080039 * Called by the cache to delete the resource under normal circumstances.
40 */
41 void release() {
42 fResource->release();
bsalomon63c992f2015-01-23 12:47:59 -080043 if (fResource->isPurgeable()) {
bsalomon12299ab2014-11-14 13:33:09 -080044 SkDELETE(fResource);
45 }
46 }
47
48 /**
49 * Called by the cache to delete the resource when the backend 3D context is no longer valid.
50 */
51 void abandon() {
52 fResource->abandon();
bsalomon63c992f2015-01-23 12:47:59 -080053 if (fResource->isPurgeable()) {
bsalomon12299ab2014-11-14 13:33:09 -080054 SkDELETE(fResource);
55 }
56 }
57
bsalomon3582d3e2015-02-13 14:20:05 -080058 CacheAccess(GrGpuResource* resource) : fResource(resource) {}
59 CacheAccess(const CacheAccess& that) : fResource(that.fResource) {}
bsalomon453cf402014-11-11 14:15:57 -080060 CacheAccess& operator=(const CacheAccess&); // unimpl
61
62 // No taking addresses of this type.
63 const CacheAccess* operator&() const;
64 CacheAccess* operator&();
65
66 GrGpuResource* fResource;
67
68 friend class GrGpuResource; // to construct/copy this type.
bsalomon3582d3e2015-02-13 14:20:05 -080069 friend class GrResourceCache; // to use this type
70 friend void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); // for unit testing
bsalomon453cf402014-11-11 14:15:57 -080071};
72
73inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); }
74
75inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const {
76 return CacheAccess(const_cast<GrGpuResource*>(this));
77}
78
79#endif