blob: ee7143faec9ad5fdfef05819c5ebf24a7864b424 [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"
halcanary805ef152014-07-17 06:58:01 -07008#include "SkCanvas.h"
halcanary1d1795b2014-07-18 09:18:40 -07009#include "SkGraphics.h"
10#include "SkScaledImageCache.h"
halcanary805ef152014-07-17 06:58:01 -070011
12static const int kCanvasSize = 1;
13static const int kBitmapSize = 16;
14static const int kScale = 8;
15
halcanary1d1795b2014-07-18 09:18:40 -070016static 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.
29static bool test_scaled_image_cache_useage() {
halcanary805ef152014-07-17 06:58:01 -070030 SkAutoTUnref<SkCanvas> canvas(
31 SkCanvas::NewRasterN32(kCanvasSize, kCanvasSize));
32 SkBitmap bitmap;
33 SkAssertResult(bitmap.allocN32Pixels(kBitmapSize, kBitmapSize));
halcanary0db38cc2014-07-17 10:17:28 -070034 bitmap.eraseColor(0xFFFFFFFF);
halcanary1d1795b2014-07-18 09:18:40 -070035 SkScalar scale = SkIntToScalar(kScale);
36 SkScalar scaledSize = SkIntToScalar(kBitmapSize) * scale;
halcanary805ef152014-07-17 06:58:01 -070037 canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize));
38 SkPaint paint;
39 paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
halcanary1d1795b2014-07-18 09:18:40 -070040
halcanary805ef152014-07-17 06:58:01 -070041 canvas->drawBitmapRect(bitmap,
42 SkRect::MakeLTRB(0, 0, scaledSize, scaledSize),
43 &paint);
halcanary1d1795b2014-07-18 09:18:40 -070044
45 return is_in_scaled_image_cache(bitmap, scale, scale);
halcanary805ef152014-07-17 06:58:01 -070046}
47
48// http://crbug.com/389439
49DEF_TEST(ScaledImageCache_SingleAllocationByteLimit, reporter) {
halcanary1d1795b2014-07-18 09:18:40 -070050 size_t originalByteLimit = SkGraphics::GetImageCacheTotalByteLimit();
halcanary805ef152014-07-17 06:58:01 -070051 size_t originalAllocationLimit =
52 SkGraphics::GetImageCacheSingleAllocationByteLimit();
53
54 size_t size = kBitmapSize * kScale * kBitmapSize * kScale
55 * SkColorTypeBytesPerPixel(kN32_SkColorType);
56
halcanary1d1795b2014-07-18 09:18:40 -070057 SkGraphics::SetImageCacheTotalByteLimit(0); // clear cache
58 SkGraphics::SetImageCacheTotalByteLimit(2 * size);
59 SkGraphics::SetImageCacheSingleAllocationByteLimit(0); // No limit
halcanary805ef152014-07-17 06:58:01 -070060
halcanary1d1795b2014-07-18 09:18:40 -070061 REPORTER_ASSERT(reporter, test_scaled_image_cache_useage());
halcanary805ef152014-07-17 06:58:01 -070062
halcanary1d1795b2014-07-18 09:18:40 -070063 SkGraphics::SetImageCacheTotalByteLimit(0); // clear cache
64 SkGraphics::SetImageCacheTotalByteLimit(2 * size);
65 SkGraphics::SetImageCacheSingleAllocationByteLimit(size * 2); // big enough
halcanary805ef152014-07-17 06:58:01 -070066
halcanary1d1795b2014-07-18 09:18:40 -070067 REPORTER_ASSERT(reporter, test_scaled_image_cache_useage());
halcanary805ef152014-07-17 06:58:01 -070068
halcanary1d1795b2014-07-18 09:18:40 -070069 SkGraphics::SetImageCacheTotalByteLimit(0); // clear cache
70 SkGraphics::SetImageCacheTotalByteLimit(2 * size);
71 SkGraphics::SetImageCacheSingleAllocationByteLimit(size / 2); // too small
halcanary805ef152014-07-17 06:58:01 -070072
halcanary1d1795b2014-07-18 09:18:40 -070073 REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage());
halcanary805ef152014-07-17 06:58:01 -070074
75 SkGraphics::SetImageCacheSingleAllocationByteLimit(originalAllocationLimit);
halcanary1d1795b2014-07-18 09:18:40 -070076 SkGraphics::SetImageCacheTotalByteLimit(originalByteLimit);
halcanary805ef152014-07-17 06:58:01 -070077}