2014-06-30 15:26:50 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
|
|
|
#include "GrContext.h"
|
|
|
|
#include "GrContextFactory.h"
|
|
|
|
#include "GrLayerCache.h"
|
2014-07-13 16:00:50 +00:00
|
|
|
#include "SkPictureRecorder.h"
|
2014-06-30 15:26:50 +00:00
|
|
|
#include "Test.h"
|
|
|
|
|
2014-07-22 17:18:06 +00:00
|
|
|
class TestingAccess {
|
2014-06-30 15:26:50 +00:00
|
|
|
public:
|
|
|
|
static int NumLayers(GrLayerCache* cache) {
|
|
|
|
return cache->numLayers();
|
|
|
|
}
|
2014-07-22 17:18:06 +00:00
|
|
|
static void Purge(GrLayerCache* cache, uint32_t pictureID) {
|
|
|
|
cache->purge(pictureID);
|
|
|
|
}
|
2014-06-30 15:26:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Add several layers to the cache
|
|
|
|
static void create_layers(skiatest::Reporter* reporter,
|
|
|
|
GrLayerCache* cache,
|
2014-07-29 13:07:19 +00:00
|
|
|
const SkPicture& picture,
|
|
|
|
int numToAdd,
|
|
|
|
int idOffset) {
|
|
|
|
|
|
|
|
for (int i = 0; i < numToAdd; ++i) {
|
2014-08-18 15:50:03 +00:00
|
|
|
GrCachedLayer* layer = cache->findLayerOrCreate(picture.uniqueID(),
|
2014-07-31 18:02:38 +00:00
|
|
|
idOffset+i+1, idOffset+i+2,
|
|
|
|
SkMatrix::I());
|
2014-09-05 20:34:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, layer);
|
2014-08-18 15:50:03 +00:00
|
|
|
GrCachedLayer* temp = cache->findLayer(picture.uniqueID(), idOffset+i+1, idOffset+i+2, SkMatrix::I());
|
2014-07-29 13:07:19 +00:00
|
|
|
REPORTER_ASSERT(reporter, temp == layer);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, TestingAccess::NumLayers(cache) == idOffset + i + 1);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, picture.uniqueID() == layer->pictureID());
|
2014-07-31 18:02:38 +00:00
|
|
|
REPORTER_ASSERT(reporter, layer->start() == idOffset + i + 1);
|
|
|
|
REPORTER_ASSERT(reporter, layer->stop() == idOffset + i + 2);
|
|
|
|
REPORTER_ASSERT(reporter, layer->ctm() == SkMatrix::I());
|
2014-07-29 13:07:19 +00:00
|
|
|
REPORTER_ASSERT(reporter, NULL == layer->texture());
|
|
|
|
REPORTER_ASSERT(reporter, !layer->isAtlased());
|
|
|
|
}
|
|
|
|
|
|
|
|
cache->trackPicture(&picture);
|
|
|
|
}
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-07-29 13:07:19 +00:00
|
|
|
static void lock_layer(skiatest::Reporter* reporter,
|
|
|
|
GrLayerCache* cache,
|
|
|
|
GrCachedLayer* layer) {
|
|
|
|
// Make the layer 512x512 (so it can be atlased)
|
|
|
|
GrTextureDesc desc;
|
|
|
|
desc.fWidth = 512;
|
|
|
|
desc.fHeight = 512;
|
|
|
|
desc.fConfig = kSkia8888_GrPixelConfig;
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-08-18 15:50:03 +00:00
|
|
|
bool needsRerendering = cache->lock(layer, desc, false);
|
|
|
|
REPORTER_ASSERT(reporter, needsRerendering);
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-08-18 15:50:03 +00:00
|
|
|
needsRerendering = cache->lock(layer, desc, false);
|
|
|
|
REPORTER_ASSERT(reporter, !needsRerendering);
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-09-05 20:34:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, layer->texture());
|
2014-07-29 13:07:19 +00:00
|
|
|
REPORTER_ASSERT(reporter, layer->locked());
|
2014-06-30 15:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This test case exercises the public API of the GrLayerCache class.
|
|
|
|
// In particular it checks its interaction with the resource cache (w.r.t.
|
|
|
|
// locking & unlocking textures).
|
|
|
|
// TODO: need to add checks on VRAM usage!
|
|
|
|
DEF_GPUTEST(GpuLayerCache, reporter, factory) {
|
2014-07-29 13:07:19 +00:00
|
|
|
static const int kInitialNumLayers = 5;
|
|
|
|
|
2014-07-17 17:50:59 +00:00
|
|
|
for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
|
|
|
|
GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-07-17 17:50:59 +00:00
|
|
|
if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-07-17 17:50:59 +00:00
|
|
|
GrContext* context = factory->get(glCtxType);
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-07-17 17:50:59 +00:00
|
|
|
if (NULL == context) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-07-17 17:50:59 +00:00
|
|
|
SkPictureRecorder recorder;
|
|
|
|
recorder.beginRecording(1, 1);
|
|
|
|
SkAutoTUnref<const SkPicture> picture(recorder.endRecording());
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-07-17 17:50:59 +00:00
|
|
|
GrLayerCache cache(context);
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-07-29 13:07:19 +00:00
|
|
|
create_layers(reporter, &cache, *picture, kInitialNumLayers, 0);
|
2014-07-17 17:50:59 +00:00
|
|
|
|
2014-07-29 13:07:19 +00:00
|
|
|
for (int i = 0; i < kInitialNumLayers; ++i) {
|
2014-08-18 15:50:03 +00:00
|
|
|
GrCachedLayer* layer = cache.findLayer(picture->uniqueID(), i+1, i+2, SkMatrix::I());
|
2014-09-05 20:34:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, layer);
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-07-29 13:07:19 +00:00
|
|
|
lock_layer(reporter, &cache, layer);
|
2014-07-17 17:50:59 +00:00
|
|
|
|
|
|
|
// The first 4 layers should be in the atlas (and thus have non-empty
|
|
|
|
// rects)
|
|
|
|
if (i < 4) {
|
|
|
|
REPORTER_ASSERT(reporter, layer->isAtlased());
|
|
|
|
} else {
|
2014-07-29 13:07:19 +00:00
|
|
|
// The 5th layer couldn't fit in the atlas
|
|
|
|
REPORTER_ASSERT(reporter, !layer->isAtlased());
|
2014-07-17 17:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-07-17 17:50:59 +00:00
|
|
|
// Unlock the textures
|
2014-07-29 13:07:19 +00:00
|
|
|
for (int i = 0; i < kInitialNumLayers; ++i) {
|
2014-08-18 15:50:03 +00:00
|
|
|
GrCachedLayer* layer = cache.findLayer(picture->uniqueID(), i+1, i+2, SkMatrix::I());
|
2014-09-05 20:34:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, layer);
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-07-17 17:50:59 +00:00
|
|
|
cache.unlock(layer);
|
|
|
|
}
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-07-29 13:07:19 +00:00
|
|
|
for (int i = 0; i < kInitialNumLayers; ++i) {
|
2014-08-18 15:50:03 +00:00
|
|
|
GrCachedLayer* layer = cache.findLayer(picture->uniqueID(), i+1, i+2, SkMatrix::I());
|
2014-09-05 20:34:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, layer);
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-07-29 13:07:19 +00:00
|
|
|
REPORTER_ASSERT(reporter, !layer->locked());
|
|
|
|
// The first 4 layers should still be in the atlas.
|
2014-07-17 17:50:59 +00:00
|
|
|
if (i < 4) {
|
2014-09-05 20:34:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, layer->texture());
|
2014-07-17 17:50:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, layer->isAtlased());
|
|
|
|
} else {
|
2014-07-29 13:07:19 +00:00
|
|
|
// The final layer should be unlocked.
|
2014-07-17 17:50:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, NULL == layer->texture());
|
|
|
|
REPORTER_ASSERT(reporter, !layer->isAtlased());
|
|
|
|
}
|
|
|
|
}
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-07-29 13:07:19 +00:00
|
|
|
{
|
|
|
|
// Add an additional layer. Since all the layers are unlocked this
|
|
|
|
// will force out the first atlased layer
|
|
|
|
create_layers(reporter, &cache, *picture, 1, kInitialNumLayers);
|
2014-08-18 15:50:03 +00:00
|
|
|
GrCachedLayer* layer = cache.findLayer(picture->uniqueID(),
|
2014-07-31 18:02:38 +00:00
|
|
|
kInitialNumLayers+1, kInitialNumLayers+2,
|
|
|
|
SkMatrix::I());
|
2014-09-05 20:34:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, layer);
|
2014-07-29 13:07:19 +00:00
|
|
|
|
|
|
|
lock_layer(reporter, &cache, layer);
|
|
|
|
cache.unlock(layer);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < kInitialNumLayers+1; ++i) {
|
2014-08-18 15:50:03 +00:00
|
|
|
GrCachedLayer* layer = cache.findLayer(picture->uniqueID(), i+1, i+2, SkMatrix::I());
|
2014-07-29 13:07:19 +00:00
|
|
|
// 3 old layers plus the new one should be in the atlas.
|
|
|
|
if (1 == i || 2 == i || 3 == i || 5 == i) {
|
2014-09-05 20:34:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, layer);
|
2014-07-29 13:07:19 +00:00
|
|
|
REPORTER_ASSERT(reporter, !layer->locked());
|
2014-09-05 20:34:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, layer->texture());
|
2014-07-29 13:07:19 +00:00
|
|
|
REPORTER_ASSERT(reporter, layer->isAtlased());
|
|
|
|
} else if (4 == i) {
|
|
|
|
// The one that was never atlased should still be around
|
2014-09-05 20:34:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, layer);
|
2014-07-29 13:07:19 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, NULL == layer->texture());
|
|
|
|
REPORTER_ASSERT(reporter, !layer->isAtlased());
|
|
|
|
} else {
|
|
|
|
// The one bumped out of the atlas (i.e., 0) should be gone
|
|
|
|
REPORTER_ASSERT(reporter, NULL == layer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-22 17:18:06 +00:00
|
|
|
//--------------------------------------------------------------------
|
2014-07-17 17:50:59 +00:00
|
|
|
// Free them all SkGpuDevice-style. This will not free up the
|
|
|
|
// atlas' texture but will eliminate all the layers.
|
2014-07-22 17:18:06 +00:00
|
|
|
TestingAccess::Purge(&cache, picture->uniqueID());
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-07-22 17:18:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0);
|
2014-07-17 17:50:59 +00:00
|
|
|
// TODO: add VRAM/resource cache check here
|
2014-07-22 17:18:06 +00:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
// Test out the GrContext-style purge. This should remove all the layers
|
|
|
|
// and the atlas.
|
2014-07-17 17:50:59 +00:00
|
|
|
// Re-create the layers
|
2014-07-29 13:07:19 +00:00
|
|
|
create_layers(reporter, &cache, *picture, kInitialNumLayers, 0);
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-07-17 17:50:59 +00:00
|
|
|
// Free them again GrContext-style. This should free up everything.
|
|
|
|
cache.freeAll();
|
2014-06-30 15:26:50 +00:00
|
|
|
|
2014-07-22 17:18:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0);
|
|
|
|
// TODO: add VRAM/resource cache check here
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
// Test out the MessageBus-style purge. This will not free the atlas
|
|
|
|
// but should eliminate the free-floating layers.
|
2014-07-29 13:07:19 +00:00
|
|
|
create_layers(reporter, &cache, *picture, kInitialNumLayers, 0);
|
2014-07-22 17:18:06 +00:00
|
|
|
|
|
|
|
picture.reset(NULL);
|
|
|
|
cache.processDeletedPictures();
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0);
|
2014-07-17 17:50:59 +00:00
|
|
|
// TODO: add VRAM/resource cache check here
|
|
|
|
}
|
2014-06-30 15:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|