aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/SkResourceCacheTest.cpp
diff options
context:
space:
mode:
authorGravatar piotaixr <piotaixr@chromium.org>2014-09-02 11:50:59 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2014-09-02 11:51:00 -0700
commitaca21d6d2f9984da8f941b76f4ee11772d0e48ec (patch)
treee9f831a009528c17925fcef3d105ddb5cd25a320 /tests/SkResourceCacheTest.cpp
parent8159146699f691b88f60564fc0811a6da3e8693f (diff)
Rename ScaledImageCache.cpp to SkResourceCacheTest.cpp
BUG=skia: R=junov@chromium.org, reed@android.com Author: piotaixr@chromium.org Review URL: https://codereview.chromium.org/518213003
Diffstat (limited to 'tests/SkResourceCacheTest.cpp')
-rw-r--r--tests/SkResourceCacheTest.cpp74
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/SkResourceCacheTest.cpp b/tests/SkResourceCacheTest.cpp
new file mode 100644
index 0000000000..b71c7443e5
--- /dev/null
+++ b/tests/SkResourceCacheTest.cpp
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+#include "Test.h"
+#include "SkCanvas.h"
+#include "SkGraphics.h"
+#include "SkBitmapCache.h"
+
+static const int kCanvasSize = 1;
+static const int kBitmapSize = 16;
+static const int kScale = 8;
+
+static bool is_in_scaled_image_cache(const SkBitmap& orig,
+ SkScalar xScale,
+ SkScalar yScale) {
+ SkBitmap scaled;
+ float roundedImageWidth = SkScalarRoundToScalar(orig.width() * xScale);
+ float roundedImageHeight = SkScalarRoundToScalar(orig.height() * xScale);
+ return SkBitmapCache::Find(orig, roundedImageWidth, roundedImageHeight, &scaled);
+}
+
+// Draw a scaled bitmap, then return true iff it has been cached.
+static bool test_scaled_image_cache_useage() {
+ SkAutoTUnref<SkCanvas> canvas(
+ SkCanvas::NewRasterN32(kCanvasSize, kCanvasSize));
+ SkBitmap bitmap;
+ SkAssertResult(bitmap.allocN32Pixels(kBitmapSize, kBitmapSize));
+ bitmap.eraseColor(0xFFFFFFFF);
+ SkScalar scale = SkIntToScalar(kScale);
+ SkScalar scaledSize = SkIntToScalar(kBitmapSize) * scale;
+ canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize));
+ SkPaint paint;
+ paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
+
+ canvas->drawBitmapRect(bitmap,
+ SkRect::MakeLTRB(0, 0, scaledSize, scaledSize),
+ &paint);
+
+ return is_in_scaled_image_cache(bitmap, scale, scale);
+}
+
+// http://crbug.com/389439
+DEF_TEST(ResourceCache_SingleAllocationByteLimit, reporter) {
+ size_t originalByteLimit = SkGraphics::GetResourceCacheTotalByteLimit();
+ size_t originalAllocationLimit =
+ SkGraphics::GetResourceCacheSingleAllocationByteLimit();
+
+ size_t size = kBitmapSize * kScale * kBitmapSize * kScale
+ * SkColorTypeBytesPerPixel(kN32_SkColorType);
+
+ SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
+ SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
+ SkGraphics::SetResourceCacheSingleAllocationByteLimit(0); // No limit
+
+ REPORTER_ASSERT(reporter, test_scaled_image_cache_useage());
+
+ SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
+ SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
+ SkGraphics::SetResourceCacheSingleAllocationByteLimit(size * 2); // big enough
+
+ REPORTER_ASSERT(reporter, test_scaled_image_cache_useage());
+
+ SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
+ SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
+ SkGraphics::SetResourceCacheSingleAllocationByteLimit(size / 2); // too small
+
+ REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage());
+
+ SkGraphics::SetResourceCacheSingleAllocationByteLimit(originalAllocationLimit);
+ SkGraphics::SetResourceCacheTotalByteLimit(originalByteLimit);
+}