blob: 9515e134e4d683238717d14029cccbbb9efe5e1c [file] [log] [blame]
halcanary805ef152014-07-17 06:58:01 -07001/*
2 * Copyright 2014 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#include "Test.h"
8#include "SkGraphics.h"
9#include "SkCanvas.h"
10
11static const int kCanvasSize = 1;
12static const int kBitmapSize = 16;
13static const int kScale = 8;
14
15static size_t test_scaled_image_cache_useage() {
16 SkAutoTUnref<SkCanvas> canvas(
17 SkCanvas::NewRasterN32(kCanvasSize, kCanvasSize));
18 SkBitmap bitmap;
19 SkAssertResult(bitmap.allocN32Pixels(kBitmapSize, kBitmapSize));
halcanary0db38cc2014-07-17 10:17:28 -070020 bitmap.eraseColor(0xFFFFFFFF);
halcanary805ef152014-07-17 06:58:01 -070021 SkScalar scaledSize = SkIntToScalar(kScale * kBitmapSize);
22 canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize));
23 SkPaint paint;
24 paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
25 size_t bytesUsed = SkGraphics::GetImageCacheBytesUsed();
26 canvas->drawBitmapRect(bitmap,
27 SkRect::MakeLTRB(0, 0, scaledSize, scaledSize),
28 &paint);
29 return SkGraphics::GetImageCacheBytesUsed() - bytesUsed;
30}
31
32// http://crbug.com/389439
33DEF_TEST(ScaledImageCache_SingleAllocationByteLimit, reporter) {
34 size_t originalByteLimit = SkGraphics::GetImageCacheByteLimit();
35 size_t originalAllocationLimit =
36 SkGraphics::GetImageCacheSingleAllocationByteLimit();
37
38 size_t size = kBitmapSize * kScale * kBitmapSize * kScale
39 * SkColorTypeBytesPerPixel(kN32_SkColorType);
40
41 SkGraphics::SetImageCacheByteLimit(0); // clear cache
42 SkGraphics::SetImageCacheByteLimit(2 * size);
43 SkGraphics::SetImageCacheSingleAllocationByteLimit(0);
44
45 REPORTER_ASSERT(reporter, size == test_scaled_image_cache_useage());
46
47 SkGraphics::SetImageCacheByteLimit(0); // clear cache
48 SkGraphics::SetImageCacheByteLimit(2 * size);
49 SkGraphics::SetImageCacheSingleAllocationByteLimit(size * 2);
50
51 REPORTER_ASSERT(reporter, size == test_scaled_image_cache_useage());
52
53 SkGraphics::SetImageCacheByteLimit(0); // clear cache
54 SkGraphics::SetImageCacheByteLimit(2 * size);
55 SkGraphics::SetImageCacheSingleAllocationByteLimit(size / 2);
56
57 REPORTER_ASSERT(reporter, 0 == test_scaled_image_cache_useage());
58
59 SkGraphics::SetImageCacheSingleAllocationByteLimit(originalAllocationLimit);
60 SkGraphics::SetImageCacheByteLimit(originalByteLimit);
61}