2014-10-23 14:58:17 +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 "SkCachedData.h"
|
|
|
|
#include "SkMaskCache.h"
|
|
|
|
#include "SkResourceCache.h"
|
|
|
|
#include "Test.h"
|
|
|
|
|
|
|
|
enum LockedState {
|
|
|
|
kUnlocked,
|
|
|
|
kLocked,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum CachedState {
|
|
|
|
kNotInCache,
|
|
|
|
kInCache,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void check_data(skiatest::Reporter* reporter, SkCachedData* data,
|
|
|
|
int refcnt, CachedState cacheState, LockedState lockedState) {
|
|
|
|
REPORTER_ASSERT(reporter, data->testing_only_getRefCnt() == refcnt);
|
|
|
|
REPORTER_ASSERT(reporter, data->testing_only_isInCache() == (kInCache == cacheState));
|
2015-08-27 14:41:13 +00:00
|
|
|
bool isLocked = (data->data() != nullptr);
|
2014-10-23 14:58:17 +00:00
|
|
|
REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked));
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(RRectMaskCache, reporter) {
|
|
|
|
SkResourceCache cache(1024);
|
|
|
|
|
|
|
|
SkScalar sigma = 0.8f;
|
|
|
|
SkRect rect = SkRect::MakeWH(100, 100);
|
|
|
|
SkRRect rrect;
|
|
|
|
rrect.setRectXY(rect, 30, 30);
|
|
|
|
SkBlurStyle style = kNormal_SkBlurStyle;
|
|
|
|
SkBlurQuality quality = kLow_SkBlurQuality;
|
|
|
|
SkMask mask;
|
|
|
|
|
2014-10-23 19:42:46 +00:00
|
|
|
SkCachedData* data = SkMaskCache::FindAndRef(sigma, style, quality, rrect, &mask, &cache);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == data);
|
2014-10-23 14:58:17 +00:00
|
|
|
|
|
|
|
size_t size = 256;
|
|
|
|
data = cache.newCachedData(size);
|
|
|
|
memset(data->writable_data(), 0xff, size);
|
|
|
|
mask.fBounds.setXYWH(0, 0, 100, 100);
|
|
|
|
mask.fRowBytes = 100;
|
|
|
|
mask.fFormat = SkMask::kBW_Format;
|
2014-10-23 19:42:46 +00:00
|
|
|
SkMaskCache::Add(sigma, style, quality, rrect, mask, data, &cache);
|
2014-10-23 14:58:17 +00:00
|
|
|
check_data(reporter, data, 2, kInCache, kLocked);
|
|
|
|
|
|
|
|
data->unref();
|
|
|
|
check_data(reporter, data, 1, kInCache, kUnlocked);
|
|
|
|
|
|
|
|
sk_bzero(&mask, sizeof(mask));
|
2014-10-23 19:42:46 +00:00
|
|
|
data = SkMaskCache::FindAndRef(sigma, style, quality, rrect, &mask, &cache);
|
2014-10-23 14:58:17 +00:00
|
|
|
REPORTER_ASSERT(reporter, data);
|
|
|
|
REPORTER_ASSERT(reporter, data->size() == size);
|
|
|
|
REPORTER_ASSERT(reporter, mask.fBounds.top() == 0 && mask.fBounds.bottom() == 100);
|
|
|
|
REPORTER_ASSERT(reporter, data->data() == (const void*)mask.fImage);
|
|
|
|
check_data(reporter, data, 2, kInCache, kLocked);
|
|
|
|
|
|
|
|
cache.purgeAll();
|
|
|
|
check_data(reporter, data, 1, kNotInCache, kLocked);
|
|
|
|
data->unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(RectsMaskCache, reporter) {
|
|
|
|
SkResourceCache cache(1024);
|
|
|
|
|
|
|
|
SkScalar sigma = 0.8f;
|
|
|
|
SkRect rect = SkRect::MakeWH(100, 100);
|
|
|
|
SkRect rects[2] = {rect};
|
|
|
|
SkBlurStyle style = kNormal_SkBlurStyle;
|
2014-10-23 19:42:46 +00:00
|
|
|
SkBlurQuality quality = kLow_SkBlurQuality;
|
2014-10-23 14:58:17 +00:00
|
|
|
SkMask mask;
|
|
|
|
|
2014-10-23 19:42:46 +00:00
|
|
|
SkCachedData* data = SkMaskCache::FindAndRef(sigma, style, quality, rects, 1, &mask, &cache);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == data);
|
2014-10-23 14:58:17 +00:00
|
|
|
|
|
|
|
size_t size = 256;
|
|
|
|
data = cache.newCachedData(size);
|
|
|
|
memset(data->writable_data(), 0xff, size);
|
|
|
|
mask.fBounds.setXYWH(0, 0, 100, 100);
|
|
|
|
mask.fRowBytes = 100;
|
|
|
|
mask.fFormat = SkMask::kBW_Format;
|
2014-10-23 19:42:46 +00:00
|
|
|
SkMaskCache::Add(sigma, style, quality, rects, 1, mask, data, &cache);
|
2014-10-23 14:58:17 +00:00
|
|
|
check_data(reporter, data, 2, kInCache, kLocked);
|
|
|
|
|
|
|
|
data->unref();
|
|
|
|
check_data(reporter, data, 1, kInCache, kUnlocked);
|
|
|
|
|
|
|
|
sk_bzero(&mask, sizeof(mask));
|
2014-10-23 19:42:46 +00:00
|
|
|
data = SkMaskCache::FindAndRef(sigma, style, quality, rects, 1, &mask, &cache);
|
2014-10-23 14:58:17 +00:00
|
|
|
REPORTER_ASSERT(reporter, data);
|
|
|
|
REPORTER_ASSERT(reporter, data->size() == size);
|
|
|
|
REPORTER_ASSERT(reporter, mask.fBounds.top() == 0 && mask.fBounds.bottom() == 100);
|
|
|
|
REPORTER_ASSERT(reporter, data->data() == (const void*)mask.fImage);
|
|
|
|
check_data(reporter, data, 2, kInCache, kLocked);
|
|
|
|
|
|
|
|
cache.purgeAll();
|
|
|
|
check_data(reporter, data, 1, kNotInCache, kLocked);
|
|
|
|
data->unref();
|
|
|
|
}
|