blob: 6b324a7d8471e8b3bd0398ccd7d3f6de8f6dcf13 [file] [log] [blame]
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +00008#if SK_SUPPORT_GPU
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00009
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000010#include "GrContextFactory.h"
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000011#include "GrResourceCache.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000012#include "SkGpuDevice.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000013#include "Test.h"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000014
15static const int gWidth = 640;
16static const int gHeight = 480;
17
18////////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000019static void test_cache(skiatest::Reporter* reporter,
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000020 GrContext* context,
21 SkCanvas* canvas) {
22 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
23
24 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000025 src.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000026 src.eraseColor(SK_ColorBLACK);
27 size_t srcSize = src.getSize();
28
29 size_t initialCacheSize = context->getGpuTextureCacheBytes();
30
31 int oldMaxNum;
32 size_t oldMaxBytes;
33 context->getTextureCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000034
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000035 // Set the cache limits so we can fit 10 "src" images and the
36 // max number of textures doesn't matter
37 size_t maxCacheSize = initialCacheSize + 10*srcSize;
38 context->setTextureCacheLimits(1000, maxCacheSize);
39
40 SkBitmap readback;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000041 readback.allocN32Pixels(size.width(), size.height());
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000042
43 for (int i = 0; i < 100; ++i) {
44 canvas->drawBitmap(src, 0, 0);
45 canvas->readPixels(size, &readback);
46
47 // "modify" the src texture
48 src.notifyPixelsChanged();
49
50 size_t curCacheSize = context->getGpuTextureCacheBytes();
51
52 // we should never go over the size limit
53 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
54 }
55
56 context->setTextureCacheLimits(oldMaxNum, oldMaxBytes);
57}
58
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000059class TestResource : public GrResource {
60public:
61 SK_DECLARE_INST_COUNT(TestResource);
62 explicit TestResource(GrGpu* gpu)
63 : INHERITED(gpu, false)
64 , fCache(NULL)
65 , fToDelete(NULL) {
66 ++fAlive;
67 }
68
69 ~TestResource() {
70 --fAlive;
71 if (NULL != fToDelete) {
72 // Breaks our little 2-element cycle below.
73 fToDelete->setDeleteWhenDestroyed(NULL, NULL);
74 fCache->deleteResource(fToDelete->getCacheEntry());
75 }
76 this->release();
77 }
78
79 size_t sizeInBytes() const SK_OVERRIDE { return 100; }
80
81 static int alive() { return fAlive; }
82
83 void setDeleteWhenDestroyed(GrResourceCache* cache, TestResource* resource) {
84 fCache = cache;
85 fToDelete = resource;
86 }
87
88private:
89 GrResourceCache* fCache;
90 TestResource* fToDelete;
91 static int fAlive;
92
93 typedef GrResource INHERITED;
94};
95int TestResource::fAlive = 0;
96
97static void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* context) {
98 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
99 GrCacheID::Key keyData;
100 keyData.fData64[0] = 5;
101 keyData.fData64[1] = 18;
102 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
103 GrResourceKey key(GrCacheID(domain, keyData), t, 0);
104
105 GrResourceCache cache(5, 30000);
106
107 // Add two resources with the same key that delete each other from the cache when destroyed.
108 TestResource* a = new TestResource(context->getGpu());
109 TestResource* b = new TestResource(context->getGpu());
110 cache.addResource(key, a);
111 cache.addResource(key, b);
112 // Circle back.
113 a->setDeleteWhenDestroyed(&cache, b);
114 b->setDeleteWhenDestroyed(&cache, a);
115 a->unref();
116 b->unref();
117
118 // Add a third independent resource also with the same key.
119 GrResource* r = new TestResource(context->getGpu());
120 cache.addResource(key, r);
121 r->unref();
122
123 // Invalidate all three, all three should be purged and destroyed.
124 REPORTER_ASSERT(reporter, 3 == TestResource::alive());
125 const GrResourceInvalidatedMessage msg = { key };
126 SkMessageBus<GrResourceInvalidatedMessage>::Post(msg);
127 cache.purgeAsNeeded();
128 REPORTER_ASSERT(reporter, 0 == TestResource::alive());
129}
130
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000131static void test_cache_delete_on_destruction(skiatest::Reporter* reporter,
132 GrContext* context) {
133 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
134 GrCacheID::Key keyData;
135 keyData.fData64[0] = 5;
136 keyData.fData64[1] = 0;
137 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
138
139 GrResourceKey key(GrCacheID(domain, keyData), t, 0);
140
141 {
142 {
143 GrResourceCache cache(3, 30000);
144 TestResource* a = new TestResource(context->getGpu());
145 TestResource* b = new TestResource(context->getGpu());
146 cache.addResource(key, a);
147 cache.addResource(key, b);
148
149 a->setDeleteWhenDestroyed(&cache, b);
150 b->setDeleteWhenDestroyed(&cache, a);
151
152 a->unref();
153 b->unref();
154 REPORTER_ASSERT(reporter, 2 == TestResource::alive());
155 }
156 REPORTER_ASSERT(reporter, 0 == TestResource::alive());
157 }
158 {
159 GrResourceCache cache(3, 30000);
160 TestResource* a = new TestResource(context->getGpu());
161 TestResource* b = new TestResource(context->getGpu());
162 cache.addResource(key, a);
163 cache.addResource(key, b);
164
165 a->setDeleteWhenDestroyed(&cache, b);
166 b->setDeleteWhenDestroyed(&cache, a);
167
168 a->unref();
169 b->unref();
170
171 cache.deleteResource(a->getCacheEntry());
172
173 REPORTER_ASSERT(reporter, 0 == TestResource::alive());
174 }
175}
176
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000177////////////////////////////////////////////////////////////////////////////////
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000178DEF_GPUTEST(ResourceCache, reporter, factory) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000179 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
180 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
181 if (!GrContextFactory::IsRenderingGLContext(glType)) {
182 continue;
183 }
184 GrContext* context = factory->get(glType);
185 if (NULL == context) {
186 continue;
187 }
188
189 GrTextureDesc desc;
190 desc.fConfig = kSkia8888_GrPixelConfig;
191 desc.fFlags = kRenderTarget_GrTextureFlagBit;
192 desc.fWidth = gWidth;
193 desc.fHeight = gHeight;
194
195 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
196 SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, texture.get())));
197 SkCanvas canvas(device.get());
198
199 test_cache(reporter, context, &canvas);
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000200 test_purge_invalidated(reporter, context);
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000201 test_cache_delete_on_destruction(reporter, context);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000202 }
203}
204
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +0000205#endif