Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(111)

Side by Side Diff: tests/ResourceCacheTest.cpp

Issue 1187523005: Add support for creating texture backed images where Skia will delete the texture. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Add default param to support Chrome's current callers Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tests/GrSurfaceTest.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 // Include here to ensure SK_SUPPORT_GPU is set correctly before it is examined. 8 // Include here to ensure SK_SUPPORT_GPU is set correctly before it is examined.
9 #include "SkTypes.h" 9 #include "SkTypes.h"
10 10
11 #if SK_SUPPORT_GPU 11 #if SK_SUPPORT_GPU
12 12
13 #include "GrContext.h" 13 #include "GrContext.h"
14 #include "GrContextFactory.h" 14 #include "GrContextFactory.h"
15 #include "gl/GrGLInterface.h"
15 #include "GrGpu.h" 16 #include "GrGpu.h"
16 #include "GrGpuResourceCacheAccess.h" 17 #include "GrGpuResourceCacheAccess.h"
17 #include "GrGpuResourcePriv.h" 18 #include "GrGpuResourcePriv.h"
18 #include "GrRenderTarget.h" 19 #include "GrRenderTarget.h"
19 #include "GrRenderTargetPriv.h" 20 #include "GrRenderTargetPriv.h"
20 #include "GrResourceCache.h" 21 #include "GrResourceCache.h"
22 #include "GrTest.h"
21 #include "SkCanvas.h" 23 #include "SkCanvas.h"
22 #include "SkGr.h" 24 #include "SkGr.h"
23 #include "SkMessageBus.h" 25 #include "SkMessageBus.h"
24 #include "SkSurface.h" 26 #include "SkSurface.h"
25 #include "Test.h" 27 #include "Test.h"
28 #include "../src/gpu/gl/GrGLDefines.h"
29 #include "../src/gpu/gl/GrGLUtil.h"
26 30
27 static const int gWidth = 640; 31 static const int gWidth = 640;
28 static const int gHeight = 480; 32 static const int gHeight = 480;
29 33
30 //////////////////////////////////////////////////////////////////////////////// 34 ////////////////////////////////////////////////////////////////////////////////
31 static void test_cache(skiatest::Reporter* reporter, GrContext* context, SkCanva s* canvas) { 35 static void test_cache(skiatest::Reporter* reporter, GrContext* context, SkCanva s* canvas) {
32 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight); 36 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
33 37
34 SkBitmap src; 38 SkBitmap src;
35 src.allocN32Pixels(size.width(), size.height()); 39 src.allocN32Pixels(size.width(), size.height());
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 REPORTER_ASSERT(reporter, 170 REPORTER_ASSERT(reporter,
167 smallMSAART0 && smallMSAART1 && 171 smallMSAART0 && smallMSAART1 &&
168 smallMSAART0->asRenderTarget() && 172 smallMSAART0->asRenderTarget() &&
169 smallMSAART1->asRenderTarget() && 173 smallMSAART1->asRenderTarget() &&
170 smallMSAART0->asRenderTarget()->renderTargetPriv().getSt encilAttachment() != 174 smallMSAART0->asRenderTarget()->renderTargetPriv().getSt encilAttachment() !=
171 smallMSAART1->asRenderTarget()->renderTargetPriv().getSt encilAttachment()); 175 smallMSAART1->asRenderTarget()->renderTargetPriv().getSt encilAttachment());
172 } 176 }
173 } 177 }
174 } 178 }
175 179
180 static void test_wrapped_resources(skiatest::Reporter* reporter, GrContext* cont ext) {
181 GrTestTarget tt;
182 context->getTestTarget(&tt);
183
184 const GrGLInterface* gl = tt.glInterface();
185 if (!gl) {
186 return;
187 }
188
189 GrGLuint texIDs[2];
190 static const int kW = 100;
191 static const int kH = 100;
192 GR_GL_CALL(gl, GenTextures(2, texIDs));
193 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0));
194 GR_GL_CALL(gl, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
195 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, texIDs[0]));
196 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, kW, kH, 0, GR_GL_ RGBA,
197 GR_GL_UNSIGNED_BYTE, NULL));
198 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, texIDs[1]));
199 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, kW, kH, 0, GR_GL_ RGBA,
200 GR_GL_UNSIGNED_BYTE, NULL));
201 context->resetContext();
202
203 GrBackendTextureDesc desc;
204 desc.fConfig = kBGRA_8888_GrPixelConfig;
205 desc.fWidth = kW;
206 desc.fHeight = kH;
207
208 desc.fTextureHandle = texIDs[0];
209 SkAutoTUnref<GrTexture> borrowed(context->textureProvider()->wrapBackendText ure(
210 desc, kBorrow_GrWrapOwnership));
211
212 desc.fTextureHandle = texIDs[1];
213 SkAutoTUnref<GrTexture> adopted(context->textureProvider()->wrapBackendTextu re(
214 desc, kAdopt_GrWrapOwnership));
215
216 REPORTER_ASSERT(reporter, SkToBool(borrowed) && SkToBool(adopted));
217 if (!SkToBool(borrowed) || !SkToBool(adopted)) {
218 return;
219 }
220
221 borrowed.reset(NULL);
222 adopted.reset(NULL);
223
224 context->flush();
225
226 GrGLboolean borrowedIsAlive;
227 GrGLboolean adoptedIsAlive;
228 GR_GL_CALL_RET(gl, borrowedIsAlive, IsTexture(texIDs[0]));
229 GR_GL_CALL_RET(gl, adoptedIsAlive, IsTexture(texIDs[1]));
230
231 REPORTER_ASSERT(reporter, borrowedIsAlive);
232 REPORTER_ASSERT(reporter, !adoptedIsAlive);
233
234 GR_GL_CALL(gl, GenTextures(1, &texIDs[0]));
235
236 context->resetContext();
237 }
238
176 class TestResource : public GrGpuResource { 239 class TestResource : public GrGpuResource {
177 static const size_t kDefaultSize = 100; 240 static const size_t kDefaultSize = 100;
178 enum ScratchConstructor { kScratchConstructor }; 241 enum ScratchConstructor { kScratchConstructor };
179 public: 242 public:
180 SK_DECLARE_INST_COUNT(TestResource); 243 SK_DECLARE_INST_COUNT(TestResource);
181 /** Property that distinctly categorizes the resource. 244 /** Property that distinctly categorizes the resource.
182 * For example, textures have width, height, ... */ 245 * For example, textures have width, height, ... */
183 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty }; 246 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
184 247
185 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle) 248 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle)
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 make_unique_key<0>(&uniqueKey, 0); 413 make_unique_key<0>(&uniqueKey, 0);
351 414
352 // Create a scratch, a unique, and a wrapped resource 415 // Create a scratch, a unique, and a wrapped resource
353 TestResource* scratch = 416 TestResource* scratch =
354 TestResource::CreateScratch(context->getGpu(), TestResource::kB_Simu latedProperty); 417 TestResource::CreateScratch(context->getGpu(), TestResource::kB_Simu latedProperty);
355 scratch->setSize(10); 418 scratch->setSize(10);
356 TestResource* unique = SkNEW_ARGS(TestResource, (context->getGpu())); 419 TestResource* unique = SkNEW_ARGS(TestResource, (context->getGpu()));
357 unique->setSize(11); 420 unique->setSize(11);
358 unique->resourcePriv().setUniqueKey(uniqueKey); 421 unique->resourcePriv().setUniqueKey(uniqueKey);
359 TestResource* wrapped = SkNEW_ARGS(TestResource, 422 TestResource* wrapped = SkNEW_ARGS(TestResource,
360 (context->getGpu(), GrGpuResource::kWrapp ed_LifeCycle)); 423 (context->getGpu(), GrGpuResource::kBorro wed_LifeCycle));
361 wrapped->setSize(12); 424 wrapped->setSize(12);
362 TestResource* unbudgeted = SkNEW_ARGS(TestResource, 425 TestResource* unbudgeted = SkNEW_ARGS(TestResource,
363 (context->getGpu(), GrGpuResource::kUn cached_LifeCycle)); 426 (context->getGpu(), GrGpuResource::kUn cached_LifeCycle));
364 unbudgeted->setSize(13); 427 unbudgeted->setSize(13);
365 428
366 // Make sure we can't add a unique key to the wrapped resource 429 // Make sure we can't add a unique key to the wrapped resource
367 GrUniqueKey uniqueKey2; 430 GrUniqueKey uniqueKey2;
368 make_unique_key<0>(&uniqueKey2, 1); 431 make_unique_key<0>(&uniqueKey2, 1);
369 wrapped->resourcePriv().setUniqueKey(uniqueKey2); 432 wrapped->resourcePriv().setUniqueKey(uniqueKey2);
370 REPORTER_ASSERT(reporter, NULL == cache->findAndRefUniqueResource(uniqueKey2 )); 433 REPORTER_ASSERT(reporter, NULL == cache->findAndRefUniqueResource(uniqueKey2 ));
(...skipping 17 matching lines...) Expand all
388 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() == 451 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
389 cache->getBudgetedResourceBytes()); 452 cache->getBudgetedResourceBytes());
390 453
391 // Unreffing the wrapped resource should free it right away. 454 // Unreffing the wrapped resource should free it right away.
392 wrapped->unref(); 455 wrapped->unref();
393 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); 456 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
394 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() + 457 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
395 unbudgeted->gpuMemorySize() == cache->getResourceB ytes()); 458 unbudgeted->gpuMemorySize() == cache->getResourceB ytes());
396 459
397 // Now try freeing the budgeted resources first 460 // Now try freeing the budgeted resources first
398 wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kWrapp ed_LifeCycle)); 461 wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kBorro wed_LifeCycle));
399 scratch->setSize(12); 462 scratch->setSize(12);
400 unique->unref(); 463 unique->unref();
401 cache->purgeAllUnlocked(); 464 cache->purgeAllUnlocked();
402 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); 465 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
403 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize( ) + 466 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize( ) +
404 unbudgeted->gpuMemorySize() == cache->getResourceB ytes()); 467 unbudgeted->gpuMemorySize() == cache->getResourceB ytes());
405 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); 468 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
406 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedReso urceBytes()); 469 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedReso urceBytes());
407 470
408 scratch->unref(); 471 scratch->unref();
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); 528 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
466 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); 529 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
467 530
468 unbudgeted->unref(); 531 unbudgeted->unref();
469 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); 532 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
470 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); 533 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
471 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); 534 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
472 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); 535 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
473 536
474 wrapped = SkNEW_ARGS(TestResource, 537 wrapped = SkNEW_ARGS(TestResource,
475 (context->getGpu(), large, GrGpuResource::kWrapped_Life Cycle)); 538 (context->getGpu(), large, GrGpuResource::kBorrowed_Lif eCycle));
476 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); 539 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
477 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes()); 540 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
478 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); 541 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
479 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); 542 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
480 543
481 wrapped->unref(); 544 wrapped->unref();
482 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); 545 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
483 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); 546 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
484 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); 547 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
485 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); 548 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
(...skipping 700 matching lines...) Expand 10 before | Expand all | Expand 10 after
1186 GrSurfaceDesc desc; 1249 GrSurfaceDesc desc;
1187 desc.fConfig = kSkia8888_GrPixelConfig; 1250 desc.fConfig = kSkia8888_GrPixelConfig;
1188 desc.fFlags = kRenderTarget_GrSurfaceFlag; 1251 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1189 desc.fWidth = gWidth; 1252 desc.fWidth = gWidth;
1190 desc.fHeight = gHeight; 1253 desc.fHeight = gHeight;
1191 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); 1254 SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
1192 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, 1255 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context,
1193 SkSurface::kN o_Budgeted, info)); 1256 SkSurface::kN o_Budgeted, info));
1194 test_cache(reporter, context, surface->getCanvas()); 1257 test_cache(reporter, context, surface->getCanvas());
1195 test_stencil_buffers(reporter, context); 1258 test_stencil_buffers(reporter, context);
1259 test_wrapped_resources(reporter, context);
1196 } 1260 }
1197 1261
1198 // The below tests create their own mock contexts. 1262 // The below tests create their own mock contexts.
1199 test_no_key(reporter); 1263 test_no_key(reporter);
1200 test_budgeting(reporter); 1264 test_budgeting(reporter);
1201 test_unbudgeted(reporter); 1265 test_unbudgeted(reporter);
1202 test_unbudgeted_to_scratch(reporter); 1266 test_unbudgeted_to_scratch(reporter);
1203 test_duplicate_unique_key(reporter); 1267 test_duplicate_unique_key(reporter);
1204 test_duplicate_scratch_key(reporter); 1268 test_duplicate_scratch_key(reporter);
1205 test_remove_scratch_key(reporter); 1269 test_remove_scratch_key(reporter);
1206 test_scratch_key_consistency(reporter); 1270 test_scratch_key_consistency(reporter);
1207 test_purge_invalidated(reporter); 1271 test_purge_invalidated(reporter);
1208 test_cache_chained_purge(reporter); 1272 test_cache_chained_purge(reporter);
1209 test_resource_size_changed(reporter); 1273 test_resource_size_changed(reporter);
1210 test_timestamp_wrap(reporter); 1274 test_timestamp_wrap(reporter);
1211 test_flush(reporter); 1275 test_flush(reporter);
1212 test_large_resource_count(reporter); 1276 test_large_resource_count(reporter);
1213 } 1277 }
1214 1278
1215 #endif 1279 #endif
OLDNEW
« no previous file with comments | « tests/GrSurfaceTest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698