• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
11 static const int kCanvasSize = 1;
12 static const int kBitmapSize = 16;
13 static const int kScale = 8;
14 
test_scaled_image_cache_useage()15 static 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));
20     SkScalar scaledSize = SkIntToScalar(kScale * kBitmapSize);
21     canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize));
22     SkPaint paint;
23     paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
24     size_t bytesUsed = SkGraphics::GetImageCacheBytesUsed();
25     canvas->drawBitmapRect(bitmap,
26                            SkRect::MakeLTRB(0, 0, scaledSize, scaledSize),
27                            &paint);
28     return SkGraphics::GetImageCacheBytesUsed() - bytesUsed;
29 }
30 
31 // http://crbug.com/389439
DEF_TEST(ScaledImageCache_SingleAllocationByteLimit,reporter)32 DEF_TEST(ScaledImageCache_SingleAllocationByteLimit, reporter) {
33     size_t originalByteLimit = SkGraphics::GetImageCacheByteLimit();
34     size_t originalAllocationLimit =
35         SkGraphics::GetImageCacheSingleAllocationByteLimit();
36 
37     size_t size = kBitmapSize * kScale * kBitmapSize * kScale
38         * SkColorTypeBytesPerPixel(kN32_SkColorType);
39 
40     SkGraphics::SetImageCacheByteLimit(0); // clear cache
41     SkGraphics::SetImageCacheByteLimit(2 * size);
42     SkGraphics::SetImageCacheSingleAllocationByteLimit(0);
43 
44     REPORTER_ASSERT(reporter, size == test_scaled_image_cache_useage());
45 
46     SkGraphics::SetImageCacheByteLimit(0); // clear cache
47     SkGraphics::SetImageCacheByteLimit(2 * size);
48     SkGraphics::SetImageCacheSingleAllocationByteLimit(size * 2);
49 
50     REPORTER_ASSERT(reporter, size == test_scaled_image_cache_useage());
51 
52     SkGraphics::SetImageCacheByteLimit(0); // clear cache
53     SkGraphics::SetImageCacheByteLimit(2 * size);
54     SkGraphics::SetImageCacheSingleAllocationByteLimit(size / 2);
55 
56     REPORTER_ASSERT(reporter, 0 == test_scaled_image_cache_useage());
57 
58     SkGraphics::SetImageCacheSingleAllocationByteLimit(originalAllocationLimit);
59     SkGraphics::SetImageCacheByteLimit(originalByteLimit);
60 }
61