OLD | NEW |
| (Empty) |
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 "SkCanvas.h" | |
9 #include "SkGraphics.h" | |
10 #include "SkBitmapCache.h" | |
11 | |
12 static const int kCanvasSize = 1; | |
13 static const int kBitmapSize = 16; | |
14 static const int kScale = 8; | |
15 | |
16 static bool is_in_scaled_image_cache(const SkBitmap& orig, | |
17 SkScalar xScale, | |
18 SkScalar yScale) { | |
19 SkBitmap scaled; | |
20 float roundedImageWidth = SkScalarRoundToScalar(orig.width() * xScale); | |
21 float roundedImageHeight = SkScalarRoundToScalar(orig.height() * xScale); | |
22 return SkBitmapCache::Find(orig, roundedImageWidth, roundedImageHeight, &sca
led); | |
23 } | |
24 | |
25 // Draw a scaled bitmap, then return true iff it has been cached. | |
26 static bool test_scaled_image_cache_useage() { | |
27 SkAutoTUnref<SkCanvas> canvas( | |
28 SkCanvas::NewRasterN32(kCanvasSize, kCanvasSize)); | |
29 SkBitmap bitmap; | |
30 SkAssertResult(bitmap.allocN32Pixels(kBitmapSize, kBitmapSize)); | |
31 bitmap.eraseColor(0xFFFFFFFF); | |
32 SkScalar scale = SkIntToScalar(kScale); | |
33 SkScalar scaledSize = SkIntToScalar(kBitmapSize) * scale; | |
34 canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize)); | |
35 SkPaint paint; | |
36 paint.setFilterLevel(SkPaint::kHigh_FilterLevel); | |
37 | |
38 canvas->drawBitmapRect(bitmap, | |
39 SkRect::MakeLTRB(0, 0, scaledSize, scaledSize), | |
40 &paint); | |
41 | |
42 return is_in_scaled_image_cache(bitmap, scale, scale); | |
43 } | |
44 | |
45 // http://crbug.com/389439 | |
46 DEF_TEST(ResourceCache_SingleAllocationByteLimit, reporter) { | |
47 size_t originalByteLimit = SkGraphics::GetResourceCacheTotalByteLimit(); | |
48 size_t originalAllocationLimit = | |
49 SkGraphics::GetResourceCacheSingleAllocationByteLimit(); | |
50 | |
51 size_t size = kBitmapSize * kScale * kBitmapSize * kScale | |
52 * SkColorTypeBytesPerPixel(kN32_SkColorType); | |
53 | |
54 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache | |
55 SkGraphics::SetResourceCacheTotalByteLimit(2 * size); | |
56 SkGraphics::SetResourceCacheSingleAllocationByteLimit(0); // No limit | |
57 | |
58 REPORTER_ASSERT(reporter, test_scaled_image_cache_useage()); | |
59 | |
60 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache | |
61 SkGraphics::SetResourceCacheTotalByteLimit(2 * size); | |
62 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size * 2); // big eno
ugh | |
63 | |
64 REPORTER_ASSERT(reporter, test_scaled_image_cache_useage()); | |
65 | |
66 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache | |
67 SkGraphics::SetResourceCacheTotalByteLimit(2 * size); | |
68 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size / 2); // too sma
ll | |
69 | |
70 REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage()); | |
71 | |
72 SkGraphics::SetResourceCacheSingleAllocationByteLimit(originalAllocationLimi
t); | |
73 SkGraphics::SetResourceCacheTotalByteLimit(originalByteLimit); | |
74 } | |
OLD | NEW |