commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 8 | #include "Test.h" |
| 9 | |
| 10 | // This is a GPU test |
| 11 | #if SK_SUPPORT_GPU |
| 12 | #include "GrContextFactory.h" |
| 13 | #include "SkGpuDevice.h" |
| 14 | |
| 15 | static const int gWidth = 640; |
| 16 | static const int gHeight = 480; |
| 17 | |
| 18 | //////////////////////////////////////////////////////////////////////////////// |
skia.committer@gmail.com | 17f1ae6 | 2013-08-09 07:01:22 +0000 | [diff] [blame] | 19 | static void test_cache(skiatest::Reporter* reporter, |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 20 | 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.com | 17f1ae6 | 2013-08-09 07:01:22 +0000 | [diff] [blame] | 35 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 36 | // 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 | |
| 61 | //////////////////////////////////////////////////////////////////////////////// |
| 62 | static void TestResourceCache(skiatest::Reporter* reporter, GrContextFactory* factory) { |
| 63 | for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
| 64 | GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type); |
| 65 | if (!GrContextFactory::IsRenderingGLContext(glType)) { |
| 66 | continue; |
| 67 | } |
| 68 | GrContext* context = factory->get(glType); |
| 69 | if (NULL == context) { |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | GrTextureDesc desc; |
| 74 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 75 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 76 | desc.fWidth = gWidth; |
| 77 | desc.fHeight = gHeight; |
| 78 | |
| 79 | SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0)); |
| 80 | SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, texture.get()))); |
| 81 | SkCanvas canvas(device.get()); |
| 82 | |
| 83 | test_cache(reporter, context, &canvas); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | //////////////////////////////////////////////////////////////////////////////// |
| 88 | #include "TestClassDef.h" |
| 89 | DEFINE_GPUTESTCLASS("ResourceCache", ResourceCacheTestClass, TestResourceCache) |
| 90 | |
| 91 | #endif |