ResourceCacheTest.cpp revision 11c6b39cfa24f812ceb115589f51a60a56ef14fe
1c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org/*
2c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org * Copyright 2013 Google Inc.
3c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org *
4c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
5c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org * found in the LICENSE file.
6c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org */
7c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org
8c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org#if SK_SUPPORT_GPU
94ee16bfaedb14aff8cf102f1f0722ff2529a9699tfarina@chromium.org
10c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org#include "GrContextFactory.h"
11c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org#include "GrResourceCache.h"
12c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org#include "SkGpuDevice.h"
134ee16bfaedb14aff8cf102f1f0722ff2529a9699tfarina@chromium.org#include "Test.h"
14c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org
15c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.orgstatic const int gWidth = 640;
16c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.orgstatic const int gHeight = 480;
17c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org
18c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org////////////////////////////////////////////////////////////////////////////////
1917f1ae63d57a3f3d3a0ae50e1b25b3f8b18fd328skia.committer@gmail.comstatic void test_cache(skiatest::Reporter* reporter,
20c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org                       GrContext* context,
21c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org                       SkCanvas* canvas) {
22c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
23c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org
24c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    SkBitmap src;
25deee496cd30070e52556dcb538c2e5eb39b66b81mike@reedtribe.org    src.allocN32Pixels(size.width(), size.height());
26c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    src.eraseColor(SK_ColorBLACK);
27c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    size_t srcSize = src.getSize();
28c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org
29c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    size_t initialCacheSize = context->getGpuTextureCacheBytes();
30c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org
31c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    int oldMaxNum;
32c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    size_t oldMaxBytes;
33c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    context->getTextureCacheLimits(&oldMaxNum, &oldMaxBytes);
3417f1ae63d57a3f3d3a0ae50e1b25b3f8b18fd328skia.committer@gmail.com
35c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    // Set the cache limits so we can fit 10 "src" images and the
36c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    // max number of textures doesn't matter
37c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    size_t maxCacheSize = initialCacheSize + 10*srcSize;
38c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    context->setTextureCacheLimits(1000, maxCacheSize);
39c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org
40c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    SkBitmap readback;
41deee496cd30070e52556dcb538c2e5eb39b66b81mike@reedtribe.org    readback.allocN32Pixels(size.width(), size.height());
42c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org
43c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    for (int i = 0; i < 100; ++i) {
44c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        canvas->drawBitmap(src, 0, 0);
45c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        canvas->readPixels(size, &readback);
46c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org
47c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        // "modify" the src texture
48c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        src.notifyPixelsChanged();
49c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org
50c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        size_t curCacheSize = context->getGpuTextureCacheBytes();
51c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org
52c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        // we should never go over the size limit
53c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
54c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    }
55c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org
56c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    context->setTextureCacheLimits(oldMaxNum, oldMaxBytes);
57c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org}
58c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org
59089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.orgclass TestResource : public GrCacheable {
6011c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    static const size_t kDefaultSize = 100;
6111c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
62c665804300096c2e7617379835bb83d715538788commit-bot@chromium.orgpublic:
63c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    SK_DECLARE_INST_COUNT(TestResource);
6411c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    TestResource(size_t size = kDefaultSize)
65089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org        : fCache(NULL)
6611c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        , fToDelete(NULL)
6711c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        , fSize(size) {
68c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org        ++fAlive;
69c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    }
70c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org
71c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    ~TestResource() {
72c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org        --fAlive;
73c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org        if (NULL != fToDelete) {
74c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org            // Breaks our little 2-element cycle below.
75c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org            fToDelete->setDeleteWhenDestroyed(NULL, NULL);
76c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org            fCache->deleteResource(fToDelete->getCacheEntry());
77c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org        }
78c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    }
79c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org
8011c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    void setSize(size_t size) {
8111c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        fSize = size;
8211c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        this->didChangeGpuMemorySize();
8311c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    }
8411c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
8511c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    size_t gpuMemorySize() const SK_OVERRIDE { return fSize; }
86089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org
87089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    bool isValidOnGpu() const SK_OVERRIDE { return true; }
88c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org
89c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    static int alive() { return fAlive; }
90c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org
91c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    void setDeleteWhenDestroyed(GrResourceCache* cache, TestResource* resource) {
92c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org        fCache = cache;
93c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org        fToDelete = resource;
94c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    }
95c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org
96c665804300096c2e7617379835bb83d715538788commit-bot@chromium.orgprivate:
97c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    GrResourceCache* fCache;
98c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    TestResource* fToDelete;
9911c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    size_t fSize;
100c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    static int fAlive;
101c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org
102089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    typedef GrCacheable INHERITED;
103c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org};
104c665804300096c2e7617379835bb83d715538788commit-bot@chromium.orgint TestResource::fAlive = 0;
105c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org
106c665804300096c2e7617379835bb83d715538788commit-bot@chromium.orgstatic void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* context) {
107c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    GrCacheID::Domain domain = GrCacheID::GenerateDomain();
108c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    GrCacheID::Key keyData;
109c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    keyData.fData64[0] = 5;
110c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    keyData.fData64[1] = 18;
111c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
112c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    GrResourceKey key(GrCacheID(domain, keyData), t, 0);
113c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org
114c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    GrResourceCache cache(5, 30000);
115c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org
116c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    // Add two resources with the same key that delete each other from the cache when destroyed.
117089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    TestResource* a = new TestResource();
118089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    TestResource* b = new TestResource();
119c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    cache.addResource(key, a);
120c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    cache.addResource(key, b);
121c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    // Circle back.
122c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    a->setDeleteWhenDestroyed(&cache, b);
123c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    b->setDeleteWhenDestroyed(&cache, a);
124c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    a->unref();
125c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    b->unref();
126c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org
127c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    // Add a third independent resource also with the same key.
128089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    GrCacheable* r = new TestResource();
129c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    cache.addResource(key, r);
130c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    r->unref();
131c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org
132c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    // Invalidate all three, all three should be purged and destroyed.
133c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    REPORTER_ASSERT(reporter, 3 == TestResource::alive());
134c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    const GrResourceInvalidatedMessage msg = { key };
135c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    SkMessageBus<GrResourceInvalidatedMessage>::Post(msg);
136c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    cache.purgeAsNeeded();
137c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org    REPORTER_ASSERT(reporter, 0 == TestResource::alive());
138c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org}
139c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org
140bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.orgstatic void test_cache_delete_on_destruction(skiatest::Reporter* reporter,
141bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org                                             GrContext* context) {
142bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org    GrCacheID::Domain domain = GrCacheID::GenerateDomain();
143bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org    GrCacheID::Key keyData;
144bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org    keyData.fData64[0] = 5;
145bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org    keyData.fData64[1] = 0;
146bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org    GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
147bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org
148bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org    GrResourceKey key(GrCacheID(domain, keyData), t, 0);
149bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org
150bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org    {
151bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org        {
152bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org            GrResourceCache cache(3, 30000);
153089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org            TestResource* a = new TestResource();
154089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org            TestResource* b = new TestResource();
155bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org            cache.addResource(key, a);
156bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org            cache.addResource(key, b);
157bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org
158bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org            a->setDeleteWhenDestroyed(&cache, b);
159bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org            b->setDeleteWhenDestroyed(&cache, a);
160bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org
161bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org            a->unref();
162bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org            b->unref();
163bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org            REPORTER_ASSERT(reporter, 2 == TestResource::alive());
164bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org        }
165bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org        REPORTER_ASSERT(reporter, 0 == TestResource::alive());
166bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org    }
167bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org    {
168bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org        GrResourceCache cache(3, 30000);
169089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org        TestResource* a = new TestResource();
170089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org        TestResource* b = new TestResource();
171bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org        cache.addResource(key, a);
172bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org        cache.addResource(key, b);
173bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org
174bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org        a->setDeleteWhenDestroyed(&cache, b);
175bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org        b->setDeleteWhenDestroyed(&cache, a);
176bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org
177bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org        a->unref();
178bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org        b->unref();
179bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org
180bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org        cache.deleteResource(a->getCacheEntry());
181bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org
182bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org        REPORTER_ASSERT(reporter, 0 == TestResource::alive());
183bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org    }
184bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org}
185bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org
18611c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.orgstatic void test_resource_size_changed(skiatest::Reporter* reporter,
18711c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org                                       GrContext* context) {
18811c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    GrCacheID::Domain domain = GrCacheID::GenerateDomain();
18911c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
19011c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
19111c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    GrCacheID::Key key1Data;
19211c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    key1Data.fData64[0] = 0;
19311c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    key1Data.fData64[1] = 0;
19411c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    GrResourceKey key1(GrCacheID(domain, key1Data), t, 0);
19511c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
19611c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    GrCacheID::Key key2Data;
19711c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    key2Data.fData64[0] = 1;
19811c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    key2Data.fData64[1] = 0;
19911c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    GrResourceKey key2(GrCacheID(domain, key2Data), t, 0);
20011c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
20111c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    // Test changing resources sizes (both increase & decrease).
20211c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    {
20311c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        GrResourceCache cache(2, 300);
20411c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
20511c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        TestResource* a = new TestResource(0);
20611c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        a->setSize(100); // Test didChangeGpuMemorySize() when not in the cache.
20711c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        cache.addResource(key1, a);
20811c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        a->unref();
20911c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
21011c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        TestResource* b = new TestResource(0);
21111c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        b->setSize(100);
21211c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        cache.addResource(key2, b);
21311c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        b->unref();
21411c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
21511c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
21611c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
21711c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
21811c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        static_cast<TestResource*>(cache.find(key2))->setSize(200);
21911c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        static_cast<TestResource*>(cache.find(key1))->setSize(50);
22011c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
22111c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        REPORTER_ASSERT(reporter, 250 == cache.getCachedResourceBytes());
22211c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
22311c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    }
22411c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
22511c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    // Test increasing a resources size beyond the cache budget.
22611c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    {
22711c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        GrResourceCache cache(2, 300);
22811c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
22911c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        TestResource* a = new TestResource(100);
23011c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        cache.addResource(key1, a);
23111c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        a->unref();
23211c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
23311c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        TestResource* b = new TestResource(100);
23411c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        cache.addResource(key2, b);
23511c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        b->unref();
23611c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
23711c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
23811c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
23911c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
24011c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        static_cast<TestResource*>(cache.find(key2))->setSize(201);
24111c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        REPORTER_ASSERT(reporter, NULL == cache.find(key1));
24211c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
24311c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        REPORTER_ASSERT(reporter, 201 == cache.getCachedResourceBytes());
24411c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        REPORTER_ASSERT(reporter, 1 == cache.getCachedResourceCount());
24511c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    }
24611c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
24711c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    // Test changing the size of an exclusively-held resource.
24811c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    {
24911c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        GrResourceCache cache(2, 300);
25011c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
25111c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        TestResource* a = new TestResource(100);
25211c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        cache.addResource(key1, a);
25311c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        cache.makeExclusive(a->getCacheEntry());
25411c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
25511c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        TestResource* b = new TestResource(100);
25611c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        cache.addResource(key2, b);
25711c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        b->unref();
25811c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
25911c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
26011c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
26111c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        REPORTER_ASSERT(reporter, NULL == cache.find(key1));
26211c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
26311c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        a->setSize(200);
26411c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
26511c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        REPORTER_ASSERT(reporter, 300 == cache.getCachedResourceBytes());
26611c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
26711c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        // Internal resource cache validation will test the detached size (debug mode only).
26811c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
26911c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        cache.makeNonExclusive(a->getCacheEntry());
27011c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        a->unref();
27111c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
27211c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        REPORTER_ASSERT(reporter, 300 == cache.getCachedResourceBytes());
27311c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
27411c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        REPORTER_ASSERT(reporter, NULL != cache.find(key1));
27511c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        // Internal resource cache validation will test the detached size (debug mode only).
27611c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    }
27711c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org}
27811c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
279c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org////////////////////////////////////////////////////////////////////////////////
2804ee16bfaedb14aff8cf102f1f0722ff2529a9699tfarina@chromium.orgDEF_GPUTEST(ResourceCache, reporter, factory) {
281c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
282c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
283c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        if (!GrContextFactory::IsRenderingGLContext(glType)) {
284c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org            continue;
285c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        }
286c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        GrContext* context = factory->get(glType);
287c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        if (NULL == context) {
288c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org            continue;
289c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        }
290c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org
291c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        GrTextureDesc desc;
292c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        desc.fConfig = kSkia8888_GrPixelConfig;
293c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        desc.fFlags = kRenderTarget_GrTextureFlagBit;
294c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        desc.fWidth = gWidth;
295c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        desc.fHeight = gHeight;
296c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org
297c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
298c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, texture.get())));
299c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        SkCanvas canvas(device.get());
300c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org
301c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org        test_cache(reporter, context, &canvas);
302c665804300096c2e7617379835bb83d715538788commit-bot@chromium.org        test_purge_invalidated(reporter, context);
303bd58febffb103ea830bf027c5a95313548f7ea8ecommit-bot@chromium.org        test_cache_delete_on_destruction(reporter, context);
30411c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        test_resource_size_changed(reporter, context);
305c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org    }
306c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org}
307c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org
308c28f555f7f4259216a4a524cae6676ab2c56871acommit-bot@chromium.org#endif
309