Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(340)

Side by Side Diff: tests/ScaledImageCache.cpp

Issue 401833002: Make ScaledImageCache unit test less dependent on global state. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: tomhudson comments Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2014 Google Inc. 2 * Copyright 2014 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 #include "Test.h" 7 #include "Test.h"
8 #include "SkCanvas.h"
8 #include "SkGraphics.h" 9 #include "SkGraphics.h"
9 #include "SkCanvas.h" 10 #include "SkScaledImageCache.h"
10 11
11 static const int kCanvasSize = 1; 12 static const int kCanvasSize = 1;
12 static const int kBitmapSize = 16; 13 static const int kBitmapSize = 16;
13 static const int kScale = 8; 14 static const int kScale = 8;
14 15
15 static size_t test_scaled_image_cache_useage() { 16 static bool is_in_scaled_image_cache(const SkBitmap& orig,
17 SkScalar xScale,
18 SkScalar yScale) {
19 SkBitmap scaled;
20 SkScaledImageCache::ID* id = SkScaledImageCache::FindAndLock(
21 orig, SkScalarInvert(xScale), SkScalarInvert(yScale), &scaled);
22 if (id) {
23 SkScaledImageCache::Unlock(id);
24 }
25 return id != NULL;
26 }
27
28 // Draw a scaled bitmap, then return true iff it has been cached.
29 static bool test_scaled_image_cache_useage() {
16 SkAutoTUnref<SkCanvas> canvas( 30 SkAutoTUnref<SkCanvas> canvas(
17 SkCanvas::NewRasterN32(kCanvasSize, kCanvasSize)); 31 SkCanvas::NewRasterN32(kCanvasSize, kCanvasSize));
18 SkBitmap bitmap; 32 SkBitmap bitmap;
19 SkAssertResult(bitmap.allocN32Pixels(kBitmapSize, kBitmapSize)); 33 SkAssertResult(bitmap.allocN32Pixels(kBitmapSize, kBitmapSize));
20 bitmap.eraseColor(0xFFFFFFFF); 34 bitmap.eraseColor(0xFFFFFFFF);
21 SkScalar scaledSize = SkIntToScalar(kScale * kBitmapSize); 35 SkScalar scale = SkIntToScalar(kScale);
36 SkScalar scaledSize = SkIntToScalar(kBitmapSize) * scale;
22 canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize)); 37 canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize));
23 SkPaint paint; 38 SkPaint paint;
24 paint.setFilterLevel(SkPaint::kHigh_FilterLevel); 39 paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
25 size_t bytesUsed = SkGraphics::GetImageCacheBytesUsed(); 40
26 canvas->drawBitmapRect(bitmap, 41 canvas->drawBitmapRect(bitmap,
27 SkRect::MakeLTRB(0, 0, scaledSize, scaledSize), 42 SkRect::MakeLTRB(0, 0, scaledSize, scaledSize),
28 &paint); 43 &paint);
29 return SkGraphics::GetImageCacheBytesUsed() - bytesUsed; 44
45 return is_in_scaled_image_cache(bitmap, scale, scale);
30 } 46 }
31 47
32 // http://crbug.com/389439 48 // http://crbug.com/389439
33 DEF_TEST(ScaledImageCache_SingleAllocationByteLimit, reporter) { 49 DEF_TEST(ScaledImageCache_SingleAllocationByteLimit, reporter) {
34 size_t originalByteLimit = SkGraphics::GetImageCacheByteLimit(); 50 size_t originalByteLimit = SkGraphics::GetImageCacheTotalByteLimit();
35 size_t originalAllocationLimit = 51 size_t originalAllocationLimit =
36 SkGraphics::GetImageCacheSingleAllocationByteLimit(); 52 SkGraphics::GetImageCacheSingleAllocationByteLimit();
37 53
38 size_t size = kBitmapSize * kScale * kBitmapSize * kScale 54 size_t size = kBitmapSize * kScale * kBitmapSize * kScale
39 * SkColorTypeBytesPerPixel(kN32_SkColorType); 55 * SkColorTypeBytesPerPixel(kN32_SkColorType);
40 56
41 SkGraphics::SetImageCacheByteLimit(0); // clear cache 57 SkGraphics::SetImageCacheTotalByteLimit(0); // clear cache
42 SkGraphics::SetImageCacheByteLimit(2 * size); 58 SkGraphics::SetImageCacheTotalByteLimit(2 * size);
43 SkGraphics::SetImageCacheSingleAllocationByteLimit(0); 59 SkGraphics::SetImageCacheSingleAllocationByteLimit(0); // No limit
44 60
45 REPORTER_ASSERT(reporter, size == test_scaled_image_cache_useage()); 61 REPORTER_ASSERT(reporter, test_scaled_image_cache_useage());
46 62
47 SkGraphics::SetImageCacheByteLimit(0); // clear cache 63 SkGraphics::SetImageCacheTotalByteLimit(0); // clear cache
48 SkGraphics::SetImageCacheByteLimit(2 * size); 64 SkGraphics::SetImageCacheTotalByteLimit(2 * size);
49 SkGraphics::SetImageCacheSingleAllocationByteLimit(size * 2); 65 SkGraphics::SetImageCacheSingleAllocationByteLimit(size * 2); // big enough
50 66
51 REPORTER_ASSERT(reporter, size == test_scaled_image_cache_useage()); 67 REPORTER_ASSERT(reporter, test_scaled_image_cache_useage());
52 68
53 SkGraphics::SetImageCacheByteLimit(0); // clear cache 69 SkGraphics::SetImageCacheTotalByteLimit(0); // clear cache
54 SkGraphics::SetImageCacheByteLimit(2 * size); 70 SkGraphics::SetImageCacheTotalByteLimit(2 * size);
55 SkGraphics::SetImageCacheSingleAllocationByteLimit(size / 2); 71 SkGraphics::SetImageCacheSingleAllocationByteLimit(size / 2); // too small
56 72
57 REPORTER_ASSERT(reporter, 0 == test_scaled_image_cache_useage()); 73 REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage());
58 74
59 SkGraphics::SetImageCacheSingleAllocationByteLimit(originalAllocationLimit); 75 SkGraphics::SetImageCacheSingleAllocationByteLimit(originalAllocationLimit);
60 SkGraphics::SetImageCacheByteLimit(originalByteLimit); 76 SkGraphics::SetImageCacheTotalByteLimit(originalByteLimit);
61 } 77 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698