blob: 845094b3ea842f1a567cf475503c582f09f81c57 [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"
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000013
14static const int gWidth = 640;
15static const int gHeight = 480;
16
17////////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000018static void test_cache(skiatest::Reporter* reporter,
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000019 GrContext* context,
20 SkCanvas* canvas) {
21 const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
22
23 SkBitmap src;
24 src.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height());
25 src.allocPixels();
26 src.eraseColor(SK_ColorBLACK);
27 size_t srcSize = src.getSize();
28
29 size_t initialCacheSize = context->getGpuTextureCacheBytes();
30
31 int oldMaxNum;
32 size_t oldMaxBytes;
33 context->getTextureCacheLimits(&oldMaxNum, &oldMaxBytes);
skia.committer@gmail.com17f1ae62013-08-09 07:01:22 +000034
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000035 // Set the cache limits so we can fit 10 "src" images and the
36 // max number of textures doesn't matter
37 size_t maxCacheSize = initialCacheSize + 10*srcSize;
38 context->setTextureCacheLimits(1000, maxCacheSize);
39
40 SkBitmap readback;
41 readback.setConfig(SkBitmap::kARGB_8888_Config, size.width(), size.height());
42 readback.allocPixels();
43
44 for (int i = 0; i < 100; ++i) {
45 canvas->drawBitmap(src, 0, 0);
46 canvas->readPixels(size, &readback);
47
48 // "modify" the src texture
49 src.notifyPixelsChanged();
50
51 size_t curCacheSize = context->getGpuTextureCacheBytes();
52
53 // we should never go over the size limit
54 REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
55 }
56
57 context->setTextureCacheLimits(oldMaxNum, oldMaxBytes);
58}
59
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000060DEF_GPUTEST(ResourceCache, reporter, factory) {
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000061 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
62 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
63 if (!GrContextFactory::IsRenderingGLContext(glType)) {
64 continue;
65 }
66 GrContext* context = factory->get(glType);
67 if (NULL == context) {
68 continue;
69 }
70
71 GrTextureDesc desc;
72 desc.fConfig = kSkia8888_GrPixelConfig;
73 desc.fFlags = kRenderTarget_GrTextureFlagBit;
74 desc.fWidth = gWidth;
75 desc.fHeight = gHeight;
76
77 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
78 SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, texture.get())));
79 SkCanvas canvas(device.get());
80
81 test_cache(reporter, context, &canvas);
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000082 }
83}
84
commit-bot@chromium.orgc28f5552013-08-08 22:55:21 +000085#endif