Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / gpu / GrResourceCache2.cpp
1
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
10 #include "GrResourceCache2.h"
11 #include "GrGpuResource.h"  
12 #include "SkRefCnt.h"
13
14 GrResourceCache2::~GrResourceCache2() {
15     this->releaseAll();
16 }
17
18 void GrResourceCache2::insertResource(GrGpuResource* resource) {
19     SkASSERT(resource);
20     SkASSERT(!resource->wasDestroyed());
21     SkASSERT(!this->isInCache(resource));
22     fResources.addToHead(resource);
23     ++fCount;
24     if (!resource->getScratchKey().isNullScratch()) {
25         fScratchMap.insert(resource->getScratchKey(), resource);
26     }
27 }
28
29 void GrResourceCache2::removeResource(GrGpuResource* resource) {
30     SkASSERT(this->isInCache(resource));
31     fResources.remove(resource);    
32     if (!resource->getScratchKey().isNullScratch()) {
33         fScratchMap.remove(resource->getScratchKey(), resource);
34     }
35     --fCount;
36 }
37
38 void GrResourceCache2::abandonAll() {
39     while (GrGpuResource* head = fResources.head()) {
40         SkASSERT(!head->wasDestroyed());
41         head->abandon();
42         // abandon should have already removed this from the list.
43         SkASSERT(head != fResources.head());
44     }
45     SkASSERT(!fScratchMap.count());
46     SkASSERT(!fCount);
47 }
48
49 void GrResourceCache2::releaseAll() {
50     while (GrGpuResource* head = fResources.head()) {
51         SkASSERT(!head->wasDestroyed());
52         head->release();
53         // release should have already removed this from the list.
54         SkASSERT(head != fResources.head());
55     }
56     SkASSERT(!fScratchMap.count());
57     SkASSERT(!fCount);
58 }
59
60 class GrResourceCache2::AvailableForScratchUse {
61 public:
62     AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { }
63
64     bool operator()(const GrGpuResource* resource) const {
65         if (!resource->reffedOnlyByCache() || !resource->isScratch()) {
66             return false;
67         }
68
69         return !fRejectPendingIO || !resource->internalHasPendingIO();
70     }
71
72 private:
73     bool fRejectPendingIO;
74 };
75
76 GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey,
77                                                            uint32_t flags) {
78     SkASSERT(scratchKey.isScratch());
79
80     if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) {
81         GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true));
82         if (resource) {
83             return SkRef(resource);
84         } else if (flags & kRequireNoPendingIO_ScratchFlag) {
85             return NULL;
86         }
87         // TODO: fail here when kPrefer is specified, we didn't find a resource without pending io,
88         // but there is still space in our budget for the resource.
89     }
90     return SkSafeRef(fScratchMap.find(scratchKey, AvailableForScratchUse(false)));
91 }