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