ResourceCacheTest.cpp revision c28f555f7f4259216a4a524cae6676ab2c56871a
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
15static const int gWidth = 640;
16static const int gHeight = 480;
17
18////////////////////////////////////////////////////////////////////////////////
19static void test_cache(skiatest::Reporter* reporter,
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);
35
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////////////////////////////////////////////////////////////////////////////////
62static 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"
89DEFINE_GPUTESTCLASS("ResourceCache", ResourceCacheTestClass, TestResourceCache)
90
91#endif
92