2014-07-17 13:58:01 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
2014-07-18 16:18:40 +00:00
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkScaledImageCache.h"
|
2014-07-17 13:58:01 +00:00
|
|
|
|
|
|
|
static const int kCanvasSize = 1;
|
|
|
|
static const int kBitmapSize = 16;
|
|
|
|
static const int kScale = 8;
|
|
|
|
|
2014-07-18 16:18:40 +00:00
|
|
|
static bool is_in_scaled_image_cache(const SkBitmap& orig,
|
|
|
|
SkScalar xScale,
|
|
|
|
SkScalar yScale) {
|
|
|
|
SkBitmap scaled;
|
|
|
|
SkScaledImageCache::ID* id = SkScaledImageCache::FindAndLock(
|
|
|
|
orig, SkScalarInvert(xScale), SkScalarInvert(yScale), &scaled);
|
|
|
|
if (id) {
|
|
|
|
SkScaledImageCache::Unlock(id);
|
|
|
|
}
|
|
|
|
return id != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw a scaled bitmap, then return true iff it has been cached.
|
|
|
|
static bool test_scaled_image_cache_useage() {
|
2014-07-17 13:58:01 +00:00
|
|
|
SkAutoTUnref<SkCanvas> canvas(
|
|
|
|
SkCanvas::NewRasterN32(kCanvasSize, kCanvasSize));
|
|
|
|
SkBitmap bitmap;
|
|
|
|
SkAssertResult(bitmap.allocN32Pixels(kBitmapSize, kBitmapSize));
|
2014-07-17 17:17:28 +00:00
|
|
|
bitmap.eraseColor(0xFFFFFFFF);
|
2014-07-18 16:18:40 +00:00
|
|
|
SkScalar scale = SkIntToScalar(kScale);
|
|
|
|
SkScalar scaledSize = SkIntToScalar(kBitmapSize) * scale;
|
2014-07-17 13:58:01 +00:00
|
|
|
canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize));
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
|
2014-07-18 16:18:40 +00:00
|
|
|
|
2014-07-17 13:58:01 +00:00
|
|
|
canvas->drawBitmapRect(bitmap,
|
|
|
|
SkRect::MakeLTRB(0, 0, scaledSize, scaledSize),
|
|
|
|
&paint);
|
2014-07-18 16:18:40 +00:00
|
|
|
|
|
|
|
return is_in_scaled_image_cache(bitmap, scale, scale);
|
2014-07-17 13:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// http://crbug.com/389439
|
|
|
|
DEF_TEST(ScaledImageCache_SingleAllocationByteLimit, reporter) {
|
2014-07-18 16:18:40 +00:00
|
|
|
size_t originalByteLimit = SkGraphics::GetImageCacheTotalByteLimit();
|
2014-07-17 13:58:01 +00:00
|
|
|
size_t originalAllocationLimit =
|
|
|
|
SkGraphics::GetImageCacheSingleAllocationByteLimit();
|
|
|
|
|
|
|
|
size_t size = kBitmapSize * kScale * kBitmapSize * kScale
|
|
|
|
* SkColorTypeBytesPerPixel(kN32_SkColorType);
|
|
|
|
|
2014-07-18 16:18:40 +00:00
|
|
|
SkGraphics::SetImageCacheTotalByteLimit(0); // clear cache
|
|
|
|
SkGraphics::SetImageCacheTotalByteLimit(2 * size);
|
|
|
|
SkGraphics::SetImageCacheSingleAllocationByteLimit(0); // No limit
|
2014-07-17 13:58:01 +00:00
|
|
|
|
2014-07-18 16:18:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, test_scaled_image_cache_useage());
|
2014-07-17 13:58:01 +00:00
|
|
|
|
2014-07-18 16:18:40 +00:00
|
|
|
SkGraphics::SetImageCacheTotalByteLimit(0); // clear cache
|
|
|
|
SkGraphics::SetImageCacheTotalByteLimit(2 * size);
|
|
|
|
SkGraphics::SetImageCacheSingleAllocationByteLimit(size * 2); // big enough
|
2014-07-17 13:58:01 +00:00
|
|
|
|
2014-07-18 16:18:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, test_scaled_image_cache_useage());
|
2014-07-17 13:58:01 +00:00
|
|
|
|
2014-07-18 16:18:40 +00:00
|
|
|
SkGraphics::SetImageCacheTotalByteLimit(0); // clear cache
|
|
|
|
SkGraphics::SetImageCacheTotalByteLimit(2 * size);
|
|
|
|
SkGraphics::SetImageCacheSingleAllocationByteLimit(size / 2); // too small
|
2014-07-17 13:58:01 +00:00
|
|
|
|
2014-07-18 16:18:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage());
|
2014-07-17 13:58:01 +00:00
|
|
|
|
|
|
|
SkGraphics::SetImageCacheSingleAllocationByteLimit(originalAllocationLimit);
|
2014-07-18 16:18:40 +00:00
|
|
|
SkGraphics::SetImageCacheTotalByteLimit(originalByteLimit);
|
2014-07-17 13:58:01 +00:00
|
|
|
}
|