blob: 6707ebf312a0b7f71ea84fa9f0ab2220d3398e16 [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"
11#include "SkGpuDevice.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000012#include "Test.h"
13#include "TestClassDef.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
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000061DEF_GPUTEST(ResourceCache, reporter, factory) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000062 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
63 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
64 if (!GrContextFactory::IsRenderingGLContext(glType)) {
65 continue;
66 }
67 GrContext* context = factory->get(glType);
68 if (NULL == context) {
69 continue;
70 }
71
72 GrTextureDesc desc;
73 desc.fConfig = kSkia8888_GrPixelConfig;
74 desc.fFlags = kRenderTarget_GrTextureFlagBit;
75 desc.fWidth = gWidth;
76 desc.fHeight = gHeight;
77
78 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
79 SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, texture.get())));
80 SkCanvas canvas(device.get());
81
82 test_cache(reporter, context, &canvas);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000083 }
84}
85
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000086#endif