2016-02-19 16:19:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 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 "SkBitmap.h"
|
|
|
|
#include "SkImage.h"
|
|
|
|
#include "SkImageFilter.h"
|
2016-04-27 18:31:23 +00:00
|
|
|
#include "SkImageFilterCache.h"
|
2016-02-19 16:19:40 +00:00
|
|
|
#include "SkMatrix.h"
|
|
|
|
#include "SkSpecialImage.h"
|
|
|
|
|
|
|
|
static const int kSmallerSize = 10;
|
|
|
|
static const int kPad = 3;
|
|
|
|
static const int kFullSize = kSmallerSize + 2 * kPad;
|
|
|
|
|
|
|
|
static SkBitmap create_bm() {
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocN32Pixels(kFullSize, kFullSize, true);
|
|
|
|
bm.eraseColor(SK_ColorTRANSPARENT);
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the cache can return a cached image
|
|
|
|
static void test_find_existing(skiatest::Reporter* reporter,
|
2016-03-17 21:31:39 +00:00
|
|
|
const sk_sp<SkSpecialImage>& image,
|
|
|
|
const sk_sp<SkSpecialImage>& subset) {
|
2016-02-19 16:19:40 +00:00
|
|
|
static const size_t kCacheSize = 1000000;
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<SkImageFilterCache> cache(SkImageFilterCache::Create(kCacheSize));
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
SkIRect clip = SkIRect::MakeWH(100, 100);
|
2016-04-27 18:31:23 +00:00
|
|
|
SkImageFilterCacheKey key1(0, SkMatrix::I(), clip, image->uniqueID(), image->subset());
|
|
|
|
SkImageFilterCacheKey key2(0, SkMatrix::I(), clip, subset->uniqueID(), subset->subset());
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
SkIPoint offset = SkIPoint::Make(3, 4);
|
2016-03-17 21:31:39 +00:00
|
|
|
cache->set(key1, image.get(), offset);
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
SkIPoint foundOffset;
|
|
|
|
|
|
|
|
SkSpecialImage* foundImage = cache->get(key1, &foundOffset);
|
|
|
|
REPORTER_ASSERT(reporter, foundImage);
|
|
|
|
REPORTER_ASSERT(reporter, offset == foundOffset);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, !cache->get(key2, &foundOffset));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If either id is different or the clip or the matrix are different the
|
|
|
|
// cached image won't be found. Even if it is caching the same bitmap.
|
|
|
|
static void test_dont_find_if_diff_key(skiatest::Reporter* reporter,
|
2016-03-17 21:31:39 +00:00
|
|
|
const sk_sp<SkSpecialImage>& image,
|
|
|
|
const sk_sp<SkSpecialImage>& subset) {
|
2016-02-19 16:19:40 +00:00
|
|
|
static const size_t kCacheSize = 1000000;
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<SkImageFilterCache> cache(SkImageFilterCache::Create(kCacheSize));
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
SkIRect clip1 = SkIRect::MakeWH(100, 100);
|
|
|
|
SkIRect clip2 = SkIRect::MakeWH(200, 200);
|
2016-04-27 18:31:23 +00:00
|
|
|
SkImageFilterCacheKey key0(0, SkMatrix::I(), clip1, image->uniqueID(), image->subset());
|
|
|
|
SkImageFilterCacheKey key1(1, SkMatrix::I(), clip1, image->uniqueID(), image->subset());
|
|
|
|
SkImageFilterCacheKey key2(0, SkMatrix::MakeTrans(5, 5), clip1,
|
2016-02-19 16:19:40 +00:00
|
|
|
image->uniqueID(), image->subset());
|
2016-04-27 18:31:23 +00:00
|
|
|
SkImageFilterCacheKey key3(0, SkMatrix::I(), clip2, image->uniqueID(), image->subset());
|
|
|
|
SkImageFilterCacheKey key4(0, SkMatrix::I(), clip1, subset->uniqueID(), subset->subset());
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
SkIPoint offset = SkIPoint::Make(3, 4);
|
2016-03-17 21:31:39 +00:00
|
|
|
cache->set(key0, image.get(), offset);
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
SkIPoint foundOffset;
|
|
|
|
REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset));
|
|
|
|
REPORTER_ASSERT(reporter, !cache->get(key2, &foundOffset));
|
|
|
|
REPORTER_ASSERT(reporter, !cache->get(key3, &foundOffset));
|
|
|
|
REPORTER_ASSERT(reporter, !cache->get(key4, &foundOffset));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test purging when the max cache size is exceeded
|
2016-03-17 21:31:39 +00:00
|
|
|
static void test_internal_purge(skiatest::Reporter* reporter, const sk_sp<SkSpecialImage>& image) {
|
2016-02-19 16:19:40 +00:00
|
|
|
SkASSERT(image->getSize());
|
2016-02-21 21:36:50 +00:00
|
|
|
const size_t kCacheSize = image->getSize() + 10;
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<SkImageFilterCache> cache(SkImageFilterCache::Create(kCacheSize));
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
SkIRect clip = SkIRect::MakeWH(100, 100);
|
2016-04-27 18:31:23 +00:00
|
|
|
SkImageFilterCacheKey key1(0, SkMatrix::I(), clip, image->uniqueID(), image->subset());
|
|
|
|
SkImageFilterCacheKey key2(1, SkMatrix::I(), clip, image->uniqueID(), image->subset());
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
SkIPoint offset = SkIPoint::Make(3, 4);
|
2016-03-17 21:31:39 +00:00
|
|
|
cache->set(key1, image.get(), offset);
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
SkIPoint foundOffset;
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, cache->get(key1, &foundOffset));
|
|
|
|
|
|
|
|
// This should knock the first one out of the cache
|
2016-03-17 21:31:39 +00:00
|
|
|
cache->set(key2, image.get(), offset);
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, cache->get(key2, &foundOffset));
|
|
|
|
REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exercise the purgeByKeys and purge methods
|
|
|
|
static void test_explicit_purging(skiatest::Reporter* reporter,
|
2016-03-17 21:31:39 +00:00
|
|
|
const sk_sp<SkSpecialImage>& image,
|
|
|
|
const sk_sp<SkSpecialImage>& subset) {
|
2016-02-19 16:19:40 +00:00
|
|
|
static const size_t kCacheSize = 1000000;
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<SkImageFilterCache> cache(SkImageFilterCache::Create(kCacheSize));
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
SkIRect clip = SkIRect::MakeWH(100, 100);
|
2016-04-27 18:31:23 +00:00
|
|
|
SkImageFilterCacheKey key1(0, SkMatrix::I(), clip, image->uniqueID(), image->subset());
|
|
|
|
SkImageFilterCacheKey key2(1, SkMatrix::I(), clip, subset->uniqueID(), image->subset());
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
SkIPoint offset = SkIPoint::Make(3, 4);
|
2016-03-17 21:31:39 +00:00
|
|
|
cache->set(key1, image.get(), offset);
|
|
|
|
cache->set(key2, image.get(), offset);
|
2016-04-18 21:49:57 +00:00
|
|
|
SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->count());)
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
SkIPoint foundOffset;
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, cache->get(key1, &foundOffset));
|
|
|
|
REPORTER_ASSERT(reporter, cache->get(key2, &foundOffset));
|
|
|
|
|
|
|
|
cache->purgeByKeys(&key1, 1);
|
2016-04-18 21:49:57 +00:00
|
|
|
SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->count());)
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset));
|
|
|
|
REPORTER_ASSERT(reporter, cache->get(key2, &foundOffset));
|
|
|
|
|
|
|
|
cache->purge();
|
2016-04-18 21:49:57 +00:00
|
|
|
SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->count());)
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset));
|
|
|
|
REPORTER_ASSERT(reporter, !cache->get(key2, &foundOffset));
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(ImageFilterCache_RasterBacked, reporter) {
|
|
|
|
SkBitmap srcBM = create_bm();
|
|
|
|
|
|
|
|
const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize);
|
|
|
|
|
2016-04-20 18:48:36 +00:00
|
|
|
sk_sp<SkSpecialImage> fullImg(SkSpecialImage::MakeFromRaster(full, srcBM));
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
|
|
|
|
|
2016-04-20 18:48:36 +00:00
|
|
|
sk_sp<SkSpecialImage> subsetImg(SkSpecialImage::MakeFromRaster(subset, srcBM));
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
test_find_existing(reporter, fullImg, subsetImg);
|
|
|
|
test_dont_find_if_diff_key(reporter, fullImg, subsetImg);
|
|
|
|
test_internal_purge(reporter, fullImg);
|
|
|
|
test_explicit_purging(reporter, fullImg, subsetImg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-19 18:41:12 +00:00
|
|
|
// Shared test code for both the raster and gpu-backed image cases
|
2016-03-17 21:31:39 +00:00
|
|
|
static void test_image_backed(skiatest::Reporter* reporter, const sk_sp<SkImage>& srcImage) {
|
2016-02-19 16:19:40 +00:00
|
|
|
const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize);
|
|
|
|
|
2016-11-14 20:09:56 +00:00
|
|
|
sk_sp<SkSpecialImage> fullImg(
|
|
|
|
SkSpecialImage::MakeFromImage(full, srcImage, SkDestinationSurfaceColorMode::kLegacy));
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
|
|
|
|
|
2016-11-14 20:09:56 +00:00
|
|
|
sk_sp<SkSpecialImage> subsetImg(
|
|
|
|
SkSpecialImage::MakeFromImage(subset, srcImage, SkDestinationSurfaceColorMode::kLegacy));
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
test_find_existing(reporter, fullImg, subsetImg);
|
|
|
|
test_dont_find_if_diff_key(reporter, fullImg, subsetImg);
|
|
|
|
test_internal_purge(reporter, fullImg);
|
|
|
|
test_explicit_purging(reporter, fullImg, subsetImg);
|
|
|
|
}
|
|
|
|
|
2016-02-19 18:41:12 +00:00
|
|
|
DEF_TEST(ImageFilterCache_ImageBackedRaster, reporter) {
|
|
|
|
SkBitmap srcBM = create_bm();
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> srcImage(SkImage::MakeFromBitmap(srcBM));
|
2016-02-19 18:41:12 +00:00
|
|
|
|
2016-03-17 21:31:39 +00:00
|
|
|
test_image_backed(reporter, srcImage);
|
2016-02-19 18:41:12 +00:00
|
|
|
}
|
|
|
|
|
2016-02-19 16:19:40 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "GrContext.h"
|
|
|
|
|
2016-02-19 18:41:12 +00:00
|
|
|
static GrTexture* create_texture(GrContext* context) {
|
2016-02-19 16:19:40 +00:00
|
|
|
SkBitmap srcBM = create_bm();
|
|
|
|
|
|
|
|
GrSurfaceDesc desc;
|
2016-10-14 20:48:17 +00:00
|
|
|
desc.fConfig = kRGBA_8888_GrPixelConfig;
|
2016-02-19 16:19:40 +00:00
|
|
|
desc.fFlags = kNone_GrSurfaceFlags;
|
|
|
|
desc.fWidth = kFullSize;
|
|
|
|
desc.fHeight = kFullSize;
|
|
|
|
|
2016-02-25 16:33:02 +00:00
|
|
|
return context->textureProvider()->createTexture(desc, SkBudgeted::kNo, srcBM.getPixels(), 0);
|
2016-02-19 18:41:12 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 15:07:26 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterCache_ImageBackedGPU, reporter, ctxInfo) {
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<GrTexture> srcTexture(create_texture(ctxInfo.grContext()));
|
2016-02-19 18:41:12 +00:00
|
|
|
if (!srcTexture) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GrBackendTextureDesc backendDesc;
|
2016-10-14 13:16:21 +00:00
|
|
|
backendDesc.fConfig = kRGBA_8888_GrPixelConfig;
|
2016-02-19 18:41:12 +00:00
|
|
|
backendDesc.fFlags = kNone_GrBackendTextureFlag;
|
|
|
|
backendDesc.fWidth = kFullSize;
|
|
|
|
backendDesc.fHeight = kFullSize;
|
|
|
|
backendDesc.fSampleCnt = 0;
|
|
|
|
backendDesc.fTextureHandle = srcTexture->getTextureHandle();
|
2016-05-11 13:33:06 +00:00
|
|
|
sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(ctxInfo.grContext(), backendDesc, kPremul_SkAlphaType));
|
2016-02-19 18:41:12 +00:00
|
|
|
if (!srcImage) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-17 21:31:39 +00:00
|
|
|
test_image_backed(reporter, srcImage);
|
2016-02-19 18:41:12 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 16:59:58 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterCache_GPUBacked, reporter, ctxInfo) {
|
2016-02-19 18:41:12 +00:00
|
|
|
|
2016-05-11 13:33:06 +00:00
|
|
|
sk_sp<GrTexture> srcTexture(create_texture(ctxInfo.grContext()));
|
2016-02-19 16:19:40 +00:00
|
|
|
if (!srcTexture) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize);
|
|
|
|
|
2016-04-20 18:48:36 +00:00
|
|
|
sk_sp<SkSpecialImage> fullImg(SkSpecialImage::MakeFromGpu(full,
|
2016-03-17 21:31:39 +00:00
|
|
|
kNeedNewImageUniqueID_SpecialImage,
|
2016-07-21 14:15:37 +00:00
|
|
|
srcTexture, nullptr));
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
|
|
|
|
|
2016-04-20 18:48:36 +00:00
|
|
|
sk_sp<SkSpecialImage> subsetImg(SkSpecialImage::MakeFromGpu(subset,
|
2016-02-19 16:19:40 +00:00
|
|
|
kNeedNewImageUniqueID_SpecialImage,
|
2016-07-21 14:15:37 +00:00
|
|
|
srcTexture, nullptr));
|
2016-02-19 16:19:40 +00:00
|
|
|
|
|
|
|
test_find_existing(reporter, fullImg, subsetImg);
|
|
|
|
test_dont_find_if_diff_key(reporter, fullImg, subsetImg);
|
|
|
|
test_internal_purge(reporter, fullImg);
|
|
|
|
test_explicit_purging(reporter, fullImg, subsetImg);
|
|
|
|
}
|
|
|
|
#endif
|