2013-08-08 22:55:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2015-04-08 18:01:54 +00:00
|
|
|
// Include here to ensure SK_SUPPORT_GPU is set correctly before it is examined.
|
|
|
|
#include "SkTypes.h"
|
|
|
|
|
2013-08-08 22:55:21 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2014-01-10 22:08:27 +00:00
|
|
|
|
2014-10-08 15:40:09 +00:00
|
|
|
#include "GrContext.h"
|
2013-08-08 22:55:21 +00:00
|
|
|
#include "GrContextFactory.h"
|
2014-10-08 15:40:09 +00:00
|
|
|
#include "GrGpu.h"
|
2015-02-13 22:20:05 +00:00
|
|
|
#include "GrGpuResourceCacheAccess.h"
|
|
|
|
#include "GrGpuResourcePriv.h"
|
2015-02-23 17:06:38 +00:00
|
|
|
#include "GrRenderTarget.h"
|
|
|
|
#include "GrRenderTargetPriv.h"
|
2015-02-11 18:49:59 +00:00
|
|
|
#include "GrResourceCache.h"
|
2015-10-02 14:49:05 +00:00
|
|
|
#include "GrResourceProvider.h"
|
2015-06-18 16:12:16 +00:00
|
|
|
#include "GrTest.h"
|
2014-10-08 15:40:09 +00:00
|
|
|
#include "SkCanvas.h"
|
2014-11-14 20:10:14 +00:00
|
|
|
#include "SkGr.h"
|
|
|
|
#include "SkMessageBus.h"
|
2014-09-18 13:09:44 +00:00
|
|
|
#include "SkSurface.h"
|
2014-01-10 22:08:27 +00:00
|
|
|
#include "Test.h"
|
2013-08-08 22:55:21 +00:00
|
|
|
|
|
|
|
static const int gWidth = 640;
|
|
|
|
static const int gHeight = 480;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2014-11-05 22:47:41 +00:00
|
|
|
static void test_cache(skiatest::Reporter* reporter, GrContext* context, SkCanvas* canvas) {
|
2013-08-08 22:55:21 +00:00
|
|
|
const SkIRect size = SkIRect::MakeWH(gWidth, gHeight);
|
|
|
|
|
|
|
|
SkBitmap src;
|
2014-02-13 14:41:43 +00:00
|
|
|
src.allocN32Pixels(size.width(), size.height());
|
2013-08-08 22:55:21 +00:00
|
|
|
src.eraseColor(SK_ColorBLACK);
|
|
|
|
size_t srcSize = src.getSize();
|
|
|
|
|
2014-05-09 14:29:32 +00:00
|
|
|
size_t initialCacheSize;
|
2015-08-27 14:41:13 +00:00
|
|
|
context->getResourceCacheUsage(nullptr, &initialCacheSize);
|
2013-08-08 22:55:21 +00:00
|
|
|
|
|
|
|
int oldMaxNum;
|
|
|
|
size_t oldMaxBytes;
|
2014-05-09 14:29:32 +00:00
|
|
|
context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes);
|
2013-08-09 07:01:22 +00:00
|
|
|
|
2013-08-08 22:55:21 +00:00
|
|
|
// Set the cache limits so we can fit 10 "src" images and the
|
|
|
|
// max number of textures doesn't matter
|
|
|
|
size_t maxCacheSize = initialCacheSize + 10*srcSize;
|
2014-05-09 14:29:32 +00:00
|
|
|
context->setResourceCacheLimits(1000, maxCacheSize);
|
2013-08-08 22:55:21 +00:00
|
|
|
|
|
|
|
SkBitmap readback;
|
2014-02-13 14:41:43 +00:00
|
|
|
readback.allocN32Pixels(size.width(), size.height());
|
2013-08-08 22:55:21 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i) {
|
|
|
|
canvas->drawBitmap(src, 0, 0);
|
|
|
|
canvas->readPixels(size, &readback);
|
|
|
|
|
|
|
|
// "modify" the src texture
|
|
|
|
src.notifyPixelsChanged();
|
|
|
|
|
2014-05-09 14:29:32 +00:00
|
|
|
size_t curCacheSize;
|
2015-08-27 14:41:13 +00:00
|
|
|
context->getResourceCacheUsage(nullptr, &curCacheSize);
|
2013-08-08 22:55:21 +00:00
|
|
|
|
|
|
|
// we should never go over the size limit
|
|
|
|
REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize);
|
|
|
|
}
|
|
|
|
|
2014-05-09 14:29:32 +00:00
|
|
|
context->setResourceCacheLimits(oldMaxNum, oldMaxBytes);
|
2013-08-08 22:55:21 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 17:09:00 +00:00
|
|
|
static void test_stencil_buffers(skiatest::Reporter* reporter, GrContext* context) {
|
|
|
|
GrSurfaceDesc smallDesc;
|
|
|
|
smallDesc.fFlags = kRenderTarget_GrSurfaceFlag;
|
|
|
|
smallDesc.fConfig = kSkia8888_GrPixelConfig;
|
|
|
|
smallDesc.fWidth = 4;
|
|
|
|
smallDesc.fHeight = 4;
|
|
|
|
smallDesc.fSampleCnt = 0;
|
|
|
|
|
2015-04-30 21:18:54 +00:00
|
|
|
GrTextureProvider* cache = context->textureProvider();
|
2015-09-14 19:56:10 +00:00
|
|
|
GrResourceProvider* resourceProvider = context->resourceProvider();
|
2015-02-19 17:09:00 +00:00
|
|
|
// Test that two budgeted RTs with the same desc share a stencil buffer.
|
2015-04-30 21:18:54 +00:00
|
|
|
SkAutoTUnref<GrTexture> smallRT0(cache->createTexture(smallDesc, true));
|
2015-02-23 17:06:38 +00:00
|
|
|
if (smallRT0 && smallRT0->asRenderTarget()) {
|
2015-09-14 19:56:10 +00:00
|
|
|
resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget());
|
2015-02-23 17:06:38 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 21:18:54 +00:00
|
|
|
SkAutoTUnref<GrTexture> smallRT1(cache->createTexture(smallDesc, true));
|
2015-02-23 17:06:38 +00:00
|
|
|
if (smallRT1 && smallRT1->asRenderTarget()) {
|
2015-09-14 19:56:10 +00:00
|
|
|
resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget());
|
2015-02-23 17:06:38 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 18:22:42 +00:00
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
smallRT0 && smallRT1 &&
|
|
|
|
smallRT0->asRenderTarget() && smallRT1->asRenderTarget() &&
|
2015-09-14 19:56:10 +00:00
|
|
|
resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
|
|
|
|
resourceProvider->attachStencilAttachment(smallRT1->asRenderTarget()));
|
2015-02-19 17:09:00 +00:00
|
|
|
|
|
|
|
// An unbudgeted RT with the same desc should also share.
|
2015-04-30 21:18:54 +00:00
|
|
|
SkAutoTUnref<GrTexture> smallRT2(cache->createTexture(smallDesc, false));
|
2015-02-23 17:06:38 +00:00
|
|
|
if (smallRT2 && smallRT2->asRenderTarget()) {
|
2015-09-14 19:56:10 +00:00
|
|
|
resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget());
|
2015-02-23 17:06:38 +00:00
|
|
|
}
|
2015-04-16 18:22:42 +00:00
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
smallRT0 && smallRT2 &&
|
|
|
|
smallRT0->asRenderTarget() && smallRT2->asRenderTarget() &&
|
2015-09-14 19:56:10 +00:00
|
|
|
resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) ==
|
|
|
|
resourceProvider->attachStencilAttachment(smallRT2->asRenderTarget()));
|
2015-02-19 17:09:00 +00:00
|
|
|
|
|
|
|
// An RT with a much larger size should not share.
|
|
|
|
GrSurfaceDesc bigDesc;
|
|
|
|
bigDesc.fFlags = kRenderTarget_GrSurfaceFlag;
|
|
|
|
bigDesc.fConfig = kSkia8888_GrPixelConfig;
|
|
|
|
bigDesc.fWidth = 400;
|
|
|
|
bigDesc.fHeight = 200;
|
|
|
|
bigDesc.fSampleCnt = 0;
|
2015-04-30 21:18:54 +00:00
|
|
|
SkAutoTUnref<GrTexture> bigRT(cache->createTexture(bigDesc, false));
|
2015-02-23 17:06:38 +00:00
|
|
|
if (bigRT && bigRT->asRenderTarget()) {
|
2015-09-14 19:56:10 +00:00
|
|
|
resourceProvider->attachStencilAttachment(bigRT->asRenderTarget());
|
2015-02-23 17:06:38 +00:00
|
|
|
}
|
2015-04-16 18:22:42 +00:00
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
smallRT0 && bigRT &&
|
|
|
|
smallRT0->asRenderTarget() && bigRT->asRenderTarget() &&
|
2015-09-14 19:56:10 +00:00
|
|
|
resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
|
|
|
|
resourceProvider->attachStencilAttachment(bigRT->asRenderTarget()));
|
2015-02-19 17:09:00 +00:00
|
|
|
|
2015-05-29 15:02:10 +00:00
|
|
|
if (context->caps()->maxSampleCount() >= 4) {
|
2015-02-19 17:09:00 +00:00
|
|
|
// An RT with a different sample count should not share.
|
|
|
|
GrSurfaceDesc smallMSAADesc = smallDesc;
|
|
|
|
smallMSAADesc.fSampleCnt = 4;
|
2015-04-30 21:18:54 +00:00
|
|
|
SkAutoTUnref<GrTexture> smallMSAART0(cache->createTexture(smallMSAADesc, false));
|
2015-02-23 17:06:38 +00:00
|
|
|
if (smallMSAART0 && smallMSAART0->asRenderTarget()) {
|
2015-09-14 19:56:10 +00:00
|
|
|
resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget());
|
2015-02-23 17:06:38 +00:00
|
|
|
}
|
2015-02-19 20:05:58 +00:00
|
|
|
#ifdef SK_BUILD_FOR_ANDROID
|
|
|
|
if (!smallMSAART0) {
|
|
|
|
// The nexus player seems to fail to create MSAA textures.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2015-02-23 17:06:38 +00:00
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
smallRT0 && smallMSAART0 &&
|
|
|
|
smallRT0->asRenderTarget() && smallMSAART0->asRenderTarget() &&
|
2015-09-14 19:56:10 +00:00
|
|
|
resourceProvider->attachStencilAttachment(smallRT0->asRenderTarget()) !=
|
|
|
|
resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()));
|
2015-02-19 17:09:00 +00:00
|
|
|
// A second MSAA RT should share with the first MSAA RT.
|
2015-04-30 21:18:54 +00:00
|
|
|
SkAutoTUnref<GrTexture> smallMSAART1(cache->createTexture(smallMSAADesc, false));
|
2015-02-23 17:06:38 +00:00
|
|
|
if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
|
2015-09-14 19:56:10 +00:00
|
|
|
resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
|
2015-02-23 17:06:38 +00:00
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
smallMSAART0 && smallMSAART1 &&
|
|
|
|
smallMSAART0->asRenderTarget() &&
|
|
|
|
smallMSAART1->asRenderTarget() &&
|
2015-09-14 19:56:10 +00:00
|
|
|
resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) ==
|
|
|
|
resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
|
2015-02-19 17:09:00 +00:00
|
|
|
// But not one with a larger sample count should not. (Also check that the request for 4
|
|
|
|
// samples didn't get rounded up to >= 8 or else they could share.).
|
2015-05-29 15:02:10 +00:00
|
|
|
if (context->caps()->maxSampleCount() >= 8 &&
|
|
|
|
smallMSAART0 && smallMSAART0->asRenderTarget() &&
|
2015-06-12 15:59:45 +00:00
|
|
|
smallMSAART0->asRenderTarget()->numColorSamples() < 8) {
|
2015-02-19 17:09:00 +00:00
|
|
|
smallMSAADesc.fSampleCnt = 8;
|
2015-04-30 21:18:54 +00:00
|
|
|
smallMSAART1.reset(cache->createTexture(smallMSAADesc, false));
|
|
|
|
SkAutoTUnref<GrTexture> smallMSAART1(cache->createTexture(smallMSAADesc, false));
|
2015-02-23 17:06:38 +00:00
|
|
|
if (smallMSAART1 && smallMSAART1->asRenderTarget()) {
|
2015-09-14 19:56:10 +00:00
|
|
|
resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget());
|
2015-02-23 17:06:38 +00:00
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter,
|
2015-04-16 18:22:42 +00:00
|
|
|
smallMSAART0 && smallMSAART1 &&
|
|
|
|
smallMSAART0->asRenderTarget() &&
|
|
|
|
smallMSAART1->asRenderTarget() &&
|
2015-09-14 19:56:10 +00:00
|
|
|
resourceProvider->attachStencilAttachment(smallMSAART0->asRenderTarget()) !=
|
|
|
|
resourceProvider->attachStencilAttachment(smallMSAART1->asRenderTarget()));
|
2015-02-19 17:09:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 16:12:16 +00:00
|
|
|
static void test_wrapped_resources(skiatest::Reporter* reporter, GrContext* context) {
|
2015-07-13 14:19:57 +00:00
|
|
|
const GrGpu* gpu = context->getGpu();
|
2015-07-15 17:16:56 +00:00
|
|
|
// this test is only valid for GL
|
|
|
|
if (!gpu || !gpu->glContextForTesting()) {
|
2015-06-18 16:12:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-13 14:19:57 +00:00
|
|
|
GrBackendObject texIDs[2];
|
2015-06-18 16:12:16 +00:00
|
|
|
static const int kW = 100;
|
|
|
|
static const int kH = 100;
|
2015-07-13 14:19:57 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
texIDs[0] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
|
|
|
|
texIDs[1] = gpu->createTestingOnlyBackendTexture(nullptr, kW, kH, kRGBA_8888_GrPixelConfig);
|
2015-07-13 14:19:57 +00:00
|
|
|
|
2015-06-18 16:12:16 +00:00
|
|
|
context->resetContext();
|
|
|
|
|
|
|
|
GrBackendTextureDesc desc;
|
|
|
|
desc.fConfig = kBGRA_8888_GrPixelConfig;
|
|
|
|
desc.fWidth = kW;
|
|
|
|
desc.fHeight = kH;
|
|
|
|
|
|
|
|
desc.fTextureHandle = texIDs[0];
|
|
|
|
SkAutoTUnref<GrTexture> borrowed(context->textureProvider()->wrapBackendTexture(
|
2015-07-16 14:46:07 +00:00
|
|
|
desc, kBorrow_GrWrapOwnership));
|
2015-06-18 16:12:16 +00:00
|
|
|
|
|
|
|
desc.fTextureHandle = texIDs[1];
|
|
|
|
SkAutoTUnref<GrTexture> adopted(context->textureProvider()->wrapBackendTexture(
|
2015-07-16 14:46:07 +00:00
|
|
|
desc, kAdopt_GrWrapOwnership));
|
2015-06-18 16:12:16 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, SkToBool(borrowed) && SkToBool(adopted));
|
|
|
|
if (!SkToBool(borrowed) || !SkToBool(adopted)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
borrowed.reset(nullptr);
|
|
|
|
adopted.reset(nullptr);
|
2015-06-18 16:12:16 +00:00
|
|
|
|
|
|
|
context->flush();
|
|
|
|
|
2015-07-14 18:02:52 +00:00
|
|
|
bool borrowedIsAlive = gpu->isTestingOnlyBackendTexture(texIDs[0]);
|
|
|
|
bool adoptedIsAlive = gpu->isTestingOnlyBackendTexture(texIDs[1]);
|
2015-06-18 16:12:16 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, borrowedIsAlive);
|
|
|
|
REPORTER_ASSERT(reporter, !adoptedIsAlive);
|
|
|
|
|
2015-07-14 18:02:52 +00:00
|
|
|
gpu->deleteTestingOnlyBackendTexture(texIDs[0]);
|
2015-06-18 16:12:16 +00:00
|
|
|
|
|
|
|
context->resetContext();
|
|
|
|
}
|
|
|
|
|
2014-07-25 15:35:45 +00:00
|
|
|
class TestResource : public GrGpuResource {
|
2015-01-21 17:32:40 +00:00
|
|
|
enum ScratchConstructor { kScratchConstructor };
|
2014-01-15 23:09:01 +00:00
|
|
|
public:
|
2015-08-13 12:19:14 +00:00
|
|
|
static const size_t kDefaultSize = 100;
|
2015-06-26 18:45:03 +00:00
|
|
|
|
2015-01-21 17:32:40 +00:00
|
|
|
/** Property that distinctly categorizes the resource.
|
|
|
|
* For example, textures have width, height, ... */
|
2015-02-06 19:54:28 +00:00
|
|
|
enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty };
|
2015-01-21 17:32:40 +00:00
|
|
|
|
2015-01-14 18:42:08 +00:00
|
|
|
TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle)
|
|
|
|
: INHERITED(gpu, lifeCycle)
|
2015-08-27 14:41:13 +00:00
|
|
|
, fToDelete(nullptr)
|
2015-01-21 17:32:40 +00:00
|
|
|
, fSize(size)
|
2015-02-06 19:54:28 +00:00
|
|
|
, fProperty(kA_SimulatedProperty) {
|
2015-01-14 18:42:08 +00:00
|
|
|
++fNumAlive;
|
|
|
|
this->registerWithCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle)
|
|
|
|
: INHERITED(gpu, lifeCycle)
|
2015-08-27 14:41:13 +00:00
|
|
|
, fToDelete(nullptr)
|
2015-01-21 17:32:40 +00:00
|
|
|
, fSize(kDefaultSize)
|
2015-02-06 19:54:28 +00:00
|
|
|
, fProperty(kA_SimulatedProperty) {
|
2014-11-17 15:34:06 +00:00
|
|
|
++fNumAlive;
|
|
|
|
this->registerWithCache();
|
|
|
|
}
|
|
|
|
|
2014-11-10 18:19:06 +00:00
|
|
|
TestResource(GrGpu* gpu)
|
2015-01-14 18:42:08 +00:00
|
|
|
: INHERITED(gpu, kCached_LifeCycle)
|
2015-08-27 14:41:13 +00:00
|
|
|
, fToDelete(nullptr)
|
2015-01-21 17:32:40 +00:00
|
|
|
, fSize(kDefaultSize)
|
2015-02-06 19:54:28 +00:00
|
|
|
, fProperty(kA_SimulatedProperty) {
|
2014-11-10 18:19:06 +00:00
|
|
|
++fNumAlive;
|
|
|
|
this->registerWithCache();
|
|
|
|
}
|
|
|
|
|
2015-02-06 19:54:28 +00:00
|
|
|
static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, bool cached = true) {
|
2015-08-26 20:07:48 +00:00
|
|
|
return new TestResource(gpu, property, cached, kScratchConstructor);
|
2014-01-15 23:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~TestResource() {
|
2014-11-05 22:47:41 +00:00
|
|
|
--fNumAlive;
|
2014-11-14 20:10:14 +00:00
|
|
|
SkSafeUnref(fToDelete);
|
2014-01-15 23:09:01 +00:00
|
|
|
}
|
|
|
|
|
2014-05-05 19:09:13 +00:00
|
|
|
void setSize(size_t size) {
|
|
|
|
fSize = size;
|
|
|
|
this->didChangeGpuMemorySize();
|
|
|
|
}
|
|
|
|
|
2014-11-05 22:47:41 +00:00
|
|
|
static int NumAlive() { return fNumAlive; }
|
2014-01-15 23:09:01 +00:00
|
|
|
|
2014-11-14 20:10:14 +00:00
|
|
|
void setUnrefWhenDestroyed(TestResource* resource) {
|
|
|
|
SkRefCnt_SafeAssign(fToDelete, resource);
|
2014-01-15 23:09:01 +00:00
|
|
|
}
|
|
|
|
|
2015-01-21 17:32:40 +00:00
|
|
|
static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) {
|
|
|
|
static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType();
|
|
|
|
GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt);
|
2015-01-23 12:24:04 +00:00
|
|
|
for (int i = 0; i < kScratchKeyFieldCnt; ++i) {
|
|
|
|
builder[i] = static_cast<uint32_t>(i + property);
|
2015-01-21 17:32:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t ExpectedScratchKeySize() {
|
|
|
|
return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt);
|
|
|
|
}
|
|
|
|
|
2014-01-15 23:09:01 +00:00
|
|
|
private:
|
2015-01-23 12:24:04 +00:00
|
|
|
static const int kScratchKeyFieldCnt = 6;
|
2015-01-21 17:32:40 +00:00
|
|
|
|
2015-01-23 15:19:22 +00:00
|
|
|
TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchConstructor)
|
|
|
|
: INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle)
|
2015-08-27 14:41:13 +00:00
|
|
|
, fToDelete(nullptr)
|
2015-01-21 17:32:40 +00:00
|
|
|
, fSize(kDefaultSize)
|
|
|
|
, fProperty(property) {
|
|
|
|
GrScratchKey scratchKey;
|
|
|
|
ComputeScratchKey(fProperty, &scratchKey);
|
|
|
|
this->setScratchKey(scratchKey);
|
|
|
|
++fNumAlive;
|
|
|
|
this->registerWithCache();
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
size_t onGpuMemorySize() const override { return fSize; }
|
2014-11-12 19:13:39 +00:00
|
|
|
|
2014-01-15 23:09:01 +00:00
|
|
|
TestResource* fToDelete;
|
2014-05-05 19:09:13 +00:00
|
|
|
size_t fSize;
|
2014-11-05 22:47:41 +00:00
|
|
|
static int fNumAlive;
|
2015-01-21 17:32:40 +00:00
|
|
|
SimulatedProperty fProperty;
|
2014-07-25 15:35:45 +00:00
|
|
|
typedef GrGpuResource INHERITED;
|
2014-01-15 23:09:01 +00:00
|
|
|
};
|
2014-11-05 22:47:41 +00:00
|
|
|
int TestResource::fNumAlive = 0;
|
2014-01-15 23:09:01 +00:00
|
|
|
|
2015-01-23 15:19:22 +00:00
|
|
|
class Mock {
|
|
|
|
public:
|
|
|
|
Mock(int maxCnt, size_t maxBytes) {
|
|
|
|
fContext.reset(GrContext::CreateMockContext());
|
|
|
|
SkASSERT(fContext);
|
|
|
|
fContext->setResourceCacheLimits(maxCnt, maxBytes);
|
2015-02-11 18:49:59 +00:00
|
|
|
GrResourceCache* cache = fContext->getResourceCache();
|
|
|
|
cache->purgeAllUnlocked();
|
|
|
|
SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
|
2014-11-14 20:10:14 +00:00
|
|
|
}
|
2015-01-23 15:19:22 +00:00
|
|
|
|
2015-02-11 18:49:59 +00:00
|
|
|
GrResourceCache* cache() { return fContext->getResourceCache(); }
|
2015-01-23 15:19:22 +00:00
|
|
|
|
|
|
|
GrContext* context() { return fContext; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkAutoTUnref<GrContext> fContext;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_no_key(skiatest::Reporter* reporter) {
|
|
|
|
Mock mock(10, 30000);
|
|
|
|
GrContext* context = mock.context();
|
2015-02-11 18:49:59 +00:00
|
|
|
GrResourceCache* cache = mock.cache();
|
2014-11-14 20:10:14 +00:00
|
|
|
|
|
|
|
// Create a bunch of resources with no keys
|
2015-08-26 20:07:48 +00:00
|
|
|
TestResource* a = new TestResource(context->getGpu());
|
|
|
|
TestResource* b = new TestResource(context->getGpu());
|
|
|
|
TestResource* c = new TestResource(context->getGpu());
|
|
|
|
TestResource* d = new TestResource(context->getGpu());
|
2014-11-14 20:10:14 +00:00
|
|
|
a->setSize(11);
|
|
|
|
b->setSize(12);
|
|
|
|
c->setSize(13);
|
|
|
|
d->setSize(14);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
|
2014-11-14 20:10:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() +
|
2015-02-11 18:49:59 +00:00
|
|
|
d->gpuMemorySize() == cache->getResourceBytes());
|
2014-11-14 20:10:14 +00:00
|
|
|
|
|
|
|
// Should be safe to purge without deleting the resources since we still have refs.
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->purgeAllUnlocked();
|
2014-11-14 20:10:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive());
|
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
// Since the resources have neither unique nor scratch keys, delete immediately upon unref.
|
2014-11-14 20:10:14 +00:00
|
|
|
|
|
|
|
a->unref();
|
|
|
|
REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
|
2014-11-14 20:10:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() ==
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->getResourceBytes());
|
2014-11-14 20:10:14 +00:00
|
|
|
|
|
|
|
c->unref();
|
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
|
2014-11-14 20:10:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() ==
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->getResourceBytes());
|
2014-11-14 20:10:14 +00:00
|
|
|
|
|
|
|
d->unref();
|
|
|
|
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
|
2014-11-14 20:10:14 +00:00
|
|
|
|
|
|
|
b->unref();
|
|
|
|
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
|
2014-11-14 20:10:14 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 12:24:04 +00:00
|
|
|
// Each integer passed as a template param creates a new domain.
|
2015-02-19 15:24:21 +00:00
|
|
|
template <int> static void make_unique_key(GrUniqueKey* key, int data) {
|
|
|
|
static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
|
|
|
|
GrUniqueKey::Builder builder(key, d, 1);
|
2015-01-23 12:24:04 +00:00
|
|
|
builder[0] = data;
|
|
|
|
}
|
|
|
|
|
2014-11-17 17:33:27 +00:00
|
|
|
static void test_budgeting(skiatest::Reporter* reporter) {
|
2015-01-23 15:19:22 +00:00
|
|
|
Mock mock(10, 300);
|
|
|
|
GrContext* context = mock.context();
|
2015-02-11 18:49:59 +00:00
|
|
|
GrResourceCache* cache = mock.cache();
|
2014-11-17 15:34:06 +00:00
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
GrUniqueKey uniqueKey;
|
|
|
|
make_unique_key<0>(&uniqueKey, 0);
|
2014-11-17 15:34:06 +00:00
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
// Create a scratch, a unique, and a wrapped resource
|
2015-01-21 17:32:40 +00:00
|
|
|
TestResource* scratch =
|
2015-02-06 19:54:28 +00:00
|
|
|
TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
|
2014-11-17 15:34:06 +00:00
|
|
|
scratch->setSize(10);
|
2015-08-26 20:07:48 +00:00
|
|
|
TestResource* unique = new TestResource(context->getGpu());
|
2015-02-19 15:24:21 +00:00
|
|
|
unique->setSize(11);
|
2015-02-19 16:24:16 +00:00
|
|
|
unique->resourcePriv().setUniqueKey(uniqueKey);
|
2015-08-26 20:07:48 +00:00
|
|
|
TestResource* wrapped = new TestResource(context->getGpu(), GrGpuResource::kBorrowed_LifeCycle);
|
2015-01-14 18:42:08 +00:00
|
|
|
wrapped->setSize(12);
|
2015-08-26 20:07:48 +00:00
|
|
|
TestResource* unbudgeted =
|
|
|
|
new TestResource(context->getGpu(), GrGpuResource::kUncached_LifeCycle);
|
2014-11-17 17:33:27 +00:00
|
|
|
unbudgeted->setSize(13);
|
2014-11-17 15:34:06 +00:00
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
// Make sure we can't add a unique key to the wrapped resource
|
|
|
|
GrUniqueKey uniqueKey2;
|
|
|
|
make_unique_key<0>(&uniqueKey2, 1);
|
2015-02-19 16:24:16 +00:00
|
|
|
wrapped->resourcePriv().setUniqueKey(uniqueKey2);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == cache->findAndRefUniqueResource(uniqueKey2));
|
2014-11-17 15:34:06 +00:00
|
|
|
|
|
|
|
// Make sure sizes are as we expect
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
|
2015-02-19 15:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
|
2014-11-17 17:33:27 +00:00
|
|
|
wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
|
2015-02-19 15:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->getBudgetedResourceBytes());
|
2014-11-17 15:34:06 +00:00
|
|
|
|
2015-01-23 20:47:59 +00:00
|
|
|
// Our refs mean that the resources are non purgeable.
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->purgeAllUnlocked();
|
|
|
|
REPORTER_ASSERT(reporter, 4 == cache->getResourceCount());
|
2015-02-19 15:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
|
2014-11-17 17:33:27 +00:00
|
|
|
wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() ==
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
|
2015-02-19 15:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() ==
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->getBudgetedResourceBytes());
|
2014-11-17 15:34:06 +00:00
|
|
|
|
|
|
|
// Unreffing the wrapped resource should free it right away.
|
|
|
|
wrapped->unref();
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
|
2015-02-19 15:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() +
|
2015-02-11 18:49:59 +00:00
|
|
|
unbudgeted->gpuMemorySize() == cache->getResourceBytes());
|
2014-11-17 15:34:06 +00:00
|
|
|
|
2014-11-17 17:33:27 +00:00
|
|
|
// Now try freeing the budgeted resources first
|
2015-08-26 20:07:48 +00:00
|
|
|
wrapped = new TestResource(context->getGpu(), GrGpuResource::kBorrowed_LifeCycle);
|
2014-11-17 15:34:06 +00:00
|
|
|
scratch->setSize(12);
|
2015-02-19 15:24:21 +00:00
|
|
|
unique->unref();
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->purgeAllUnlocked();
|
|
|
|
REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
|
2014-11-17 17:33:27 +00:00
|
|
|
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() +
|
2015-02-11 18:49:59 +00:00
|
|
|
unbudgeted->gpuMemorySize() == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes());
|
2014-11-17 15:34:06 +00:00
|
|
|
|
|
|
|
scratch->unref();
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->purgeAllUnlocked();
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
|
2014-11-17 17:33:27 +00:00
|
|
|
REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() ==
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
|
2014-11-17 15:34:06 +00:00
|
|
|
|
|
|
|
wrapped->unref();
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
|
2014-11-17 17:33:27 +00:00
|
|
|
|
|
|
|
unbudgeted->unref();
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
|
2014-11-17 15:34:06 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 18:42:08 +00:00
|
|
|
static void test_unbudgeted(skiatest::Reporter* reporter) {
|
2015-01-23 15:19:22 +00:00
|
|
|
Mock mock(10, 30000);
|
|
|
|
GrContext* context = mock.context();
|
2015-02-11 18:49:59 +00:00
|
|
|
GrResourceCache* cache = mock.cache();
|
2015-01-14 18:42:08 +00:00
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
GrUniqueKey uniqueKey;
|
|
|
|
make_unique_key<0>(&uniqueKey, 0);
|
2015-01-14 18:42:08 +00:00
|
|
|
|
|
|
|
TestResource* scratch;
|
2015-02-19 15:24:21 +00:00
|
|
|
TestResource* unique;
|
2015-01-14 18:42:08 +00:00
|
|
|
TestResource* wrapped;
|
|
|
|
TestResource* unbudgeted;
|
|
|
|
|
|
|
|
// A large uncached or wrapped resource shouldn't evict anything.
|
2015-02-06 19:54:28 +00:00
|
|
|
scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty);
|
2015-01-14 18:42:08 +00:00
|
|
|
scratch->setSize(10);
|
|
|
|
scratch->unref();
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes());
|
2015-01-14 18:42:08 +00:00
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
unique = new TestResource(context->getGpu());
|
2015-02-19 15:24:21 +00:00
|
|
|
unique->setSize(11);
|
2015-02-19 16:24:16 +00:00
|
|
|
unique->resourcePriv().setUniqueKey(uniqueKey);
|
2015-02-19 15:24:21 +00:00
|
|
|
unique->unref();
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
|
2015-01-14 18:42:08 +00:00
|
|
|
|
2015-02-11 18:49:59 +00:00
|
|
|
size_t large = 2 * cache->getResourceBytes();
|
2015-08-26 20:07:48 +00:00
|
|
|
unbudgeted = new TestResource(context->getGpu(), large, GrGpuResource::kUncached_LifeCycle);
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
|
2015-01-14 18:42:08 +00:00
|
|
|
|
|
|
|
unbudgeted->unref();
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
|
2015-01-14 18:42:08 +00:00
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
wrapped = new TestResource(context->getGpu(), large, GrGpuResource::kBorrowed_LifeCycle);
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 3 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
|
2015-01-14 18:42:08 +00:00
|
|
|
|
|
|
|
wrapped->unref();
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes());
|
|
|
|
|
|
|
|
cache->purgeAllUnlocked();
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
|
2015-01-14 18:42:08 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 22:20:05 +00:00
|
|
|
// This method can't be static because it needs to friended in GrGpuResource::CacheAccess.
|
|
|
|
void test_unbudgeted_to_scratch(skiatest::Reporter* reporter);
|
|
|
|
/*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) {
|
2015-01-23 15:19:22 +00:00
|
|
|
Mock mock(10, 300);
|
|
|
|
GrContext* context = mock.context();
|
2015-02-11 18:49:59 +00:00
|
|
|
GrResourceCache* cache = mock.cache();
|
2015-01-23 15:19:22 +00:00
|
|
|
|
|
|
|
TestResource* resource =
|
2015-02-06 19:54:28 +00:00
|
|
|
TestResource::CreateScratch(context->getGpu(), TestResource::kA_SimulatedProperty, false);
|
2015-01-23 15:19:22 +00:00
|
|
|
GrScratchKey key;
|
2015-02-06 19:54:28 +00:00
|
|
|
TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key);
|
2015-01-23 15:19:22 +00:00
|
|
|
|
|
|
|
size_t size = resource->gpuMemorySize();
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
// Since this resource is unbudgeted, it should not be reachable as scratch.
|
2015-02-13 22:20:05 +00:00
|
|
|
REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
|
2015-01-23 15:19:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
|
2015-02-13 22:20:05 +00:00
|
|
|
REPORTER_ASSERT(reporter, !resource->resourcePriv().isBudgeted());
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
|
2015-01-23 15:19:22 +00:00
|
|
|
|
|
|
|
// Once it is unrefed, it should become available as scratch.
|
|
|
|
resource->unref();
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
|
2015-08-13 12:19:14 +00:00
|
|
|
resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key, TestResource::kDefaultSize, 0));
|
2015-01-23 15:19:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, resource);
|
2015-02-13 22:20:05 +00:00
|
|
|
REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key);
|
2015-01-23 15:19:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch());
|
2015-02-13 22:20:05 +00:00
|
|
|
REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
|
2015-01-23 15:19:22 +00:00
|
|
|
|
|
|
|
if (0 == i) {
|
|
|
|
// If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try
|
|
|
|
// the above tests again.
|
2015-02-13 22:20:05 +00:00
|
|
|
resource->resourcePriv().makeUnbudgeted();
|
2015-01-23 15:19:22 +00:00
|
|
|
} else {
|
|
|
|
// After the second time around, try removing the scratch key
|
2015-02-13 22:20:05 +00:00
|
|
|
resource->resourcePriv().removeScratchKey();
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, size == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes());
|
2015-02-13 22:20:05 +00:00
|
|
|
REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid());
|
2015-01-23 15:19:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch());
|
2015-02-13 22:20:05 +00:00
|
|
|
REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted());
|
2015-01-23 15:19:22 +00:00
|
|
|
|
|
|
|
// now when it is unrefed it should die since it has no key.
|
|
|
|
resource->unref();
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes());
|
2015-01-23 15:19:22 +00:00
|
|
|
}
|
2014-11-10 18:19:06 +00:00
|
|
|
}
|
2015-01-23 15:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_duplicate_scratch_key(skiatest::Reporter* reporter) {
|
|
|
|
Mock mock(5, 30000);
|
|
|
|
GrContext* context = mock.context();
|
2015-02-11 18:49:59 +00:00
|
|
|
GrResourceCache* cache = mock.cache();
|
2014-11-10 18:19:06 +00:00
|
|
|
|
|
|
|
// Create two resources that have the same scratch key.
|
2015-02-06 19:54:28 +00:00
|
|
|
TestResource* a = TestResource::CreateScratch(context->getGpu(),
|
|
|
|
TestResource::kB_SimulatedProperty);
|
|
|
|
TestResource* b = TestResource::CreateScratch(context->getGpu(),
|
|
|
|
TestResource::kB_SimulatedProperty);
|
2014-11-10 18:19:06 +00:00
|
|
|
a->setSize(11);
|
|
|
|
b->setSize(12);
|
2015-01-21 17:32:40 +00:00
|
|
|
GrScratchKey scratchKey1;
|
2015-02-06 19:54:28 +00:00
|
|
|
TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
|
2015-01-21 17:32:40 +00:00
|
|
|
// Check for negative case consistency. (leaks upon test failure.)
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == cache->findAndRefScratchResource(scratchKey1, TestResource::kDefaultSize, 0));
|
2015-01-21 17:32:40 +00:00
|
|
|
|
|
|
|
GrScratchKey scratchKey;
|
2015-02-06 19:54:28 +00:00
|
|
|
TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
|
2015-01-21 17:32:40 +00:00
|
|
|
|
2015-02-11 18:49:59 +00:00
|
|
|
// Scratch resources are registered with GrResourceCache just by existing. There are 2.
|
2014-11-14 14:47:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
2015-02-11 18:49:59 +00:00
|
|
|
SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
|
2014-11-14 20:10:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() ==
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->getResourceBytes());
|
2014-11-10 18:19:06 +00:00
|
|
|
|
2015-01-23 20:47:59 +00:00
|
|
|
// Our refs mean that the resources are non purgeable.
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->purgeAllUnlocked();
|
2014-11-10 18:19:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
|
2014-11-10 18:19:06 +00:00
|
|
|
|
|
|
|
// Unref but don't purge
|
|
|
|
a->unref();
|
|
|
|
b->unref();
|
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
2015-02-11 18:49:59 +00:00
|
|
|
SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
|
2014-11-10 18:19:06 +00:00
|
|
|
|
2015-01-23 20:47:59 +00:00
|
|
|
// Purge again. This time resources should be purgeable.
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->purgeAllUnlocked();
|
2014-11-10 18:19:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
|
|
|
|
SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
|
2014-11-10 18:19:06 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 13:52:06 +00:00
|
|
|
static void test_remove_scratch_key(skiatest::Reporter* reporter) {
|
2015-01-23 15:19:22 +00:00
|
|
|
Mock mock(5, 30000);
|
|
|
|
GrContext* context = mock.context();
|
2015-02-11 18:49:59 +00:00
|
|
|
GrResourceCache* cache = mock.cache();
|
2014-11-25 13:52:06 +00:00
|
|
|
|
|
|
|
// Create two resources that have the same scratch key.
|
2015-02-06 19:54:28 +00:00
|
|
|
TestResource* a = TestResource::CreateScratch(context->getGpu(),
|
|
|
|
TestResource::kB_SimulatedProperty);
|
|
|
|
TestResource* b = TestResource::CreateScratch(context->getGpu(),
|
|
|
|
TestResource::kB_SimulatedProperty);
|
2014-11-25 13:52:06 +00:00
|
|
|
a->unref();
|
|
|
|
b->unref();
|
|
|
|
|
2015-01-21 17:32:40 +00:00
|
|
|
GrScratchKey scratchKey;
|
|
|
|
// Ensure that scratch key lookup is correct for negative case.
|
2015-02-06 19:54:28 +00:00
|
|
|
TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
|
2015-01-21 17:32:40 +00:00
|
|
|
// (following leaks upon test failure).
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
|
2015-01-21 17:32:40 +00:00
|
|
|
|
2015-02-11 18:49:59 +00:00
|
|
|
// Scratch resources are registered with GrResourceCache just by existing. There are 2.
|
2015-02-06 19:54:28 +00:00
|
|
|
TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
|
2014-11-25 13:52:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
2015-02-11 18:49:59 +00:00
|
|
|
SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));)
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
|
2014-11-25 13:52:06 +00:00
|
|
|
|
|
|
|
// Find the first resource and remove its scratch key
|
|
|
|
GrGpuResource* find;
|
2015-08-13 12:19:14 +00:00
|
|
|
find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
|
2015-02-13 22:20:05 +00:00
|
|
|
find->resourcePriv().removeScratchKey();
|
2014-11-25 13:52:06 +00:00
|
|
|
// It's still alive, but not cached by scratch key anymore
|
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
2015-02-11 18:49:59 +00:00
|
|
|
SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
|
2014-11-25 13:52:06 +00:00
|
|
|
|
|
|
|
// The cache should immediately delete it when it's unrefed since it isn't accessible.
|
|
|
|
find->unref();
|
|
|
|
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
|
2015-02-11 18:49:59 +00:00
|
|
|
SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));)
|
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
|
2014-11-25 13:52:06 +00:00
|
|
|
|
|
|
|
// Repeat for the second resource.
|
2015-08-13 12:19:14 +00:00
|
|
|
find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
|
2015-02-13 22:20:05 +00:00
|
|
|
find->resourcePriv().removeScratchKey();
|
2014-11-25 13:52:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
|
2015-02-11 18:49:59 +00:00
|
|
|
SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
|
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
|
2014-11-25 13:52:06 +00:00
|
|
|
|
|
|
|
// Should be able to call this multiple times with no problem.
|
2015-02-13 22:20:05 +00:00
|
|
|
find->resourcePriv().removeScratchKey();
|
2014-11-25 13:52:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
|
2015-02-11 18:49:59 +00:00
|
|
|
SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
|
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
|
2014-11-25 13:52:06 +00:00
|
|
|
|
|
|
|
find->unref();
|
|
|
|
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
|
2015-02-11 18:49:59 +00:00
|
|
|
SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));)
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
|
2014-11-25 13:52:06 +00:00
|
|
|
}
|
|
|
|
|
2015-01-21 17:32:40 +00:00
|
|
|
static void test_scratch_key_consistency(skiatest::Reporter* reporter) {
|
2015-01-23 15:19:22 +00:00
|
|
|
Mock mock(5, 30000);
|
|
|
|
GrContext* context = mock.context();
|
2015-02-11 18:49:59 +00:00
|
|
|
GrResourceCache* cache = mock.cache();
|
2015-01-21 17:32:40 +00:00
|
|
|
|
|
|
|
// Create two resources that have the same scratch key.
|
2015-02-06 19:54:28 +00:00
|
|
|
TestResource* a = TestResource::CreateScratch(context->getGpu(),
|
|
|
|
TestResource::kB_SimulatedProperty);
|
|
|
|
TestResource* b = TestResource::CreateScratch(context->getGpu(),
|
|
|
|
TestResource::kB_SimulatedProperty);
|
2015-01-21 17:32:40 +00:00
|
|
|
a->unref();
|
|
|
|
b->unref();
|
|
|
|
|
|
|
|
GrScratchKey scratchKey;
|
|
|
|
// Ensure that scratch key comparison and assignment is consistent.
|
|
|
|
GrScratchKey scratchKey1;
|
2015-02-06 19:54:28 +00:00
|
|
|
TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1);
|
2015-01-21 17:32:40 +00:00
|
|
|
GrScratchKey scratchKey2;
|
2015-02-06 19:54:28 +00:00
|
|
|
TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2);
|
2015-01-21 17:32:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize());
|
|
|
|
REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2);
|
|
|
|
REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1);
|
|
|
|
scratchKey = scratchKey1;
|
|
|
|
REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
|
|
|
|
REPORTER_ASSERT(reporter, scratchKey1 == scratchKey);
|
|
|
|
REPORTER_ASSERT(reporter, scratchKey == scratchKey1);
|
|
|
|
REPORTER_ASSERT(reporter, scratchKey2 != scratchKey);
|
|
|
|
REPORTER_ASSERT(reporter, scratchKey != scratchKey2);
|
|
|
|
scratchKey = scratchKey2;
|
|
|
|
REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize());
|
|
|
|
REPORTER_ASSERT(reporter, scratchKey1 != scratchKey);
|
|
|
|
REPORTER_ASSERT(reporter, scratchKey != scratchKey1);
|
|
|
|
REPORTER_ASSERT(reporter, scratchKey2 == scratchKey);
|
|
|
|
REPORTER_ASSERT(reporter, scratchKey == scratchKey2);
|
|
|
|
|
|
|
|
// Ensure that scratch key lookup is correct for negative case.
|
2015-02-06 19:54:28 +00:00
|
|
|
TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
|
2015-01-21 17:32:40 +00:00
|
|
|
// (following leaks upon test failure).
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0) == nullptr);
|
2015-01-21 17:32:40 +00:00
|
|
|
|
|
|
|
// Find the first resource with a scratch key and a copy of a scratch key.
|
2015-02-06 19:54:28 +00:00
|
|
|
TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey);
|
2015-08-13 12:19:14 +00:00
|
|
|
GrGpuResource* find = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, find != nullptr);
|
2015-01-21 17:32:40 +00:00
|
|
|
find->unref();
|
|
|
|
|
|
|
|
scratchKey2 = scratchKey;
|
2015-08-13 12:19:14 +00:00
|
|
|
find = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, find != nullptr);
|
2015-01-21 17:32:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, find == a || find == b);
|
|
|
|
|
2015-08-13 12:19:14 +00:00
|
|
|
GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2, TestResource::kDefaultSize, 0);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, find2 != nullptr);
|
2015-01-21 17:32:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, find2 == a || find2 == b);
|
|
|
|
REPORTER_ASSERT(reporter, find2 != find);
|
|
|
|
find2->unref();
|
|
|
|
find->unref();
|
|
|
|
}
|
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
static void test_duplicate_unique_key(skiatest::Reporter* reporter) {
|
2015-01-23 15:19:22 +00:00
|
|
|
Mock mock(5, 30000);
|
|
|
|
GrContext* context = mock.context();
|
2015-02-11 18:49:59 +00:00
|
|
|
GrResourceCache* cache = mock.cache();
|
2014-11-10 18:19:06 +00:00
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
GrUniqueKey key;
|
|
|
|
make_unique_key<0>(&key, 0);
|
2014-11-05 22:47:41 +00:00
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
// Create two resources that we will attempt to register with the same unique key.
|
2015-08-26 20:07:48 +00:00
|
|
|
TestResource* a = new TestResource(context->getGpu());
|
2014-11-10 18:19:06 +00:00
|
|
|
a->setSize(11);
|
2014-11-14 20:10:14 +00:00
|
|
|
|
2015-02-19 16:24:16 +00:00
|
|
|
// Set key on resource a.
|
|
|
|
a->resourcePriv().setUniqueKey(key);
|
|
|
|
REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
|
|
|
|
a->unref();
|
|
|
|
|
|
|
|
// Make sure that redundantly setting a's key works.
|
|
|
|
a->resourcePriv().setUniqueKey(key);
|
|
|
|
REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key));
|
|
|
|
a->unref();
|
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
|
|
|
|
|
|
|
|
// Create resource b and set the same key. It should replace a's unique key cache entry.
|
2015-08-26 20:07:48 +00:00
|
|
|
TestResource* b = new TestResource(context->getGpu());
|
2015-02-19 16:24:16 +00:00
|
|
|
b->setSize(12);
|
|
|
|
b->resourcePriv().setUniqueKey(key);
|
|
|
|
REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key));
|
|
|
|
b->unref();
|
2014-11-14 20:10:14 +00:00
|
|
|
|
2015-02-19 16:24:16 +00:00
|
|
|
// Still have two resources because a is still reffed.
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
|
2015-02-19 16:24:16 +00:00
|
|
|
REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes());
|
2014-11-10 18:19:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
|
|
|
|
2015-02-19 16:24:16 +00:00
|
|
|
a->unref();
|
|
|
|
// Now a should be gone.
|
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
|
|
|
|
|
|
|
|
// Now replace b with c, but make sure c can start with one unique key and change it to b's key.
|
|
|
|
// Also make b be unreffed when replacement occurs.
|
2014-11-10 18:19:06 +00:00
|
|
|
b->unref();
|
2015-08-26 20:07:48 +00:00
|
|
|
TestResource* c = new TestResource(context->getGpu());
|
2015-02-19 16:24:16 +00:00
|
|
|
GrUniqueKey differentKey;
|
|
|
|
make_unique_key<0>(&differentKey, 1);
|
|
|
|
c->setSize(13);
|
|
|
|
c->resourcePriv().setUniqueKey(differentKey);
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
|
|
|
// c replaces b and b should be immediately purged.
|
|
|
|
c->resourcePriv().setUniqueKey(key);
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
|
2015-02-19 16:24:16 +00:00
|
|
|
REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
|
2014-11-14 20:10:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
|
|
|
|
|
2015-02-19 16:24:16 +00:00
|
|
|
// c shouldn't be purged because it is ref'ed.
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->purgeAllUnlocked();
|
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
|
2015-02-19 16:24:16 +00:00
|
|
|
REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
|
2014-11-10 18:19:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
|
|
|
|
|
2015-02-19 16:24:16 +00:00
|
|
|
// Drop the ref on c, it should be kept alive because it has a unique key.
|
|
|
|
c->unref();
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
|
2015-02-19 16:24:16 +00:00
|
|
|
REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes());
|
2014-11-14 20:10:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
|
|
|
|
|
2015-02-19 16:24:16 +00:00
|
|
|
// Verify that we can find c, then remove its unique key. It should get purged immediately.
|
|
|
|
REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key));
|
|
|
|
c->resourcePriv().removeUniqueKey();
|
|
|
|
c->unref();
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
|
2014-11-10 18:19:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
|
2015-08-04 17:01:58 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
GrUniqueKey key2;
|
|
|
|
make_unique_key<0>(&key2, 0);
|
2015-08-26 20:07:48 +00:00
|
|
|
SkAutoTUnref<TestResource> d(new TestResource(context->getGpu()));
|
2015-08-04 17:01:58 +00:00
|
|
|
int foo = 4132;
|
|
|
|
SkAutoTUnref<SkData> data(SkData::NewWithCopy(&foo, sizeof(foo)));
|
|
|
|
key2.setCustomData(data.get());
|
|
|
|
d->resourcePriv().setUniqueKey(key2);
|
|
|
|
}
|
|
|
|
|
|
|
|
GrUniqueKey key3;
|
|
|
|
make_unique_key<0>(&key3, 0);
|
|
|
|
SkAutoTUnref<GrGpuResource> d2(cache->findAndRefUniqueResource(key3));
|
|
|
|
REPORTER_ASSERT(reporter, *(int*) d2->getUniqueKey().getCustomData()->data() == 4132);
|
2014-11-10 18:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_purge_invalidated(skiatest::Reporter* reporter) {
|
2015-01-23 15:19:22 +00:00
|
|
|
Mock mock(5, 30000);
|
|
|
|
GrContext* context = mock.context();
|
2015-02-11 18:49:59 +00:00
|
|
|
GrResourceCache* cache = mock.cache();
|
2014-11-10 18:19:06 +00:00
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
GrUniqueKey key1, key2, key3;
|
|
|
|
make_unique_key<0>(&key1, 1);
|
|
|
|
make_unique_key<0>(&key2, 2);
|
|
|
|
make_unique_key<0>(&key3, 3);
|
2014-11-10 18:19:06 +00:00
|
|
|
|
2015-02-06 19:54:28 +00:00
|
|
|
// Add three resources to the cache. Only c is usable as scratch.
|
2015-08-26 20:07:48 +00:00
|
|
|
TestResource* a = new TestResource(context->getGpu());
|
|
|
|
TestResource* b = new TestResource(context->getGpu());
|
2015-02-06 19:54:28 +00:00
|
|
|
TestResource* c = TestResource::CreateScratch(context->getGpu(),
|
|
|
|
TestResource::kA_SimulatedProperty);
|
2015-02-19 15:24:21 +00:00
|
|
|
a->resourcePriv().setUniqueKey(key1);
|
|
|
|
b->resourcePriv().setUniqueKey(key2);
|
|
|
|
c->resourcePriv().setUniqueKey(key3);
|
2014-01-15 23:09:01 +00:00
|
|
|
a->unref();
|
2015-02-06 19:54:28 +00:00
|
|
|
// hold b until *after* the message is sent.
|
2014-11-10 18:19:06 +00:00
|
|
|
c->unref();
|
2014-01-15 23:09:01 +00:00
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
|
|
|
|
REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
|
|
|
|
REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
|
2014-11-05 22:47:41 +00:00
|
|
|
REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
|
2015-02-06 19:54:28 +00:00
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
typedef GrUniqueKeyInvalidatedMessage Msg;
|
|
|
|
typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
|
2015-02-06 19:54:28 +00:00
|
|
|
|
|
|
|
// Invalidate two of the three, they should be purged and no longer accessible via their keys.
|
|
|
|
Bus::Post(Msg(key1));
|
|
|
|
Bus::Post(Msg(key2));
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->purgeAsNeeded();
|
2015-02-06 19:54:28 +00:00
|
|
|
// a should be deleted now, but we still have a ref on b.
|
2015-02-19 15:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
|
|
|
|
REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
|
2015-02-06 19:54:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
2015-02-19 15:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3));
|
2014-11-10 18:19:06 +00:00
|
|
|
|
|
|
|
// Invalidate the third.
|
2015-02-06 19:54:28 +00:00
|
|
|
Bus::Post(Msg(key3));
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->purgeAsNeeded();
|
2015-02-06 19:54:28 +00:00
|
|
|
// we still have a ref on b, c should be recycled as scratch.
|
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
2015-02-19 15:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3));
|
2014-11-14 20:10:14 +00:00
|
|
|
|
2015-02-06 19:54:28 +00:00
|
|
|
// make b purgeable. It should be immediately deleted since it has no key.
|
|
|
|
b->unref();
|
|
|
|
REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive());
|
|
|
|
|
|
|
|
// Make sure we actually get to c via it's scratch key, before we say goodbye.
|
|
|
|
GrScratchKey scratchKey;
|
|
|
|
TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey);
|
2015-08-13 12:19:14 +00:00
|
|
|
GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
|
2015-02-06 19:54:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, scratch == c);
|
|
|
|
SkSafeUnref(scratch);
|
|
|
|
|
|
|
|
// Get rid of c.
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->purgeAllUnlocked();
|
2015-08-13 12:19:14 +00:00
|
|
|
scratch = cache->findAndRefScratchResource(scratchKey, TestResource::kDefaultSize, 0);
|
2014-11-14 20:10:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes());
|
2015-02-06 19:54:28 +00:00
|
|
|
REPORTER_ASSERT(reporter, !scratch);
|
|
|
|
SkSafeUnref(scratch);
|
2014-01-15 23:09:01 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 20:10:14 +00:00
|
|
|
static void test_cache_chained_purge(skiatest::Reporter* reporter) {
|
2015-01-23 15:19:22 +00:00
|
|
|
Mock mock(3, 30000);
|
|
|
|
GrContext* context = mock.context();
|
2015-02-11 18:49:59 +00:00
|
|
|
GrResourceCache* cache = mock.cache();
|
2014-11-10 18:19:06 +00:00
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
GrUniqueKey key1, key2;
|
|
|
|
make_unique_key<0>(&key1, 1);
|
|
|
|
make_unique_key<0>(&key2, 2);
|
2014-01-17 17:56:21 +00:00
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
TestResource* a = new TestResource(context->getGpu());
|
|
|
|
TestResource* b = new TestResource(context->getGpu());
|
2015-02-19 15:24:21 +00:00
|
|
|
a->resourcePriv().setUniqueKey(key1);
|
|
|
|
b->resourcePriv().setUniqueKey(key2);
|
2014-11-14 20:10:14 +00:00
|
|
|
|
2015-01-23 15:19:22 +00:00
|
|
|
// Make a cycle
|
|
|
|
a->setUnrefWhenDestroyed(b);
|
|
|
|
b->setUnrefWhenDestroyed(a);
|
2014-11-05 22:47:41 +00:00
|
|
|
|
2015-01-23 15:19:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
2014-11-05 22:47:41 +00:00
|
|
|
|
2015-01-23 15:19:22 +00:00
|
|
|
a->unref();
|
|
|
|
b->unref();
|
2014-11-10 18:19:06 +00:00
|
|
|
|
2015-01-23 15:19:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
2014-11-10 18:19:06 +00:00
|
|
|
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->purgeAllUnlocked();
|
2015-01-23 15:19:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
2014-11-13 21:33:28 +00:00
|
|
|
|
2015-01-23 15:19:22 +00:00
|
|
|
// Break the cycle
|
2015-08-27 14:41:13 +00:00
|
|
|
a->setUnrefWhenDestroyed(nullptr);
|
2015-01-23 15:19:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive());
|
2014-11-14 14:47:39 +00:00
|
|
|
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->purgeAllUnlocked();
|
2015-01-23 15:19:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
|
2014-01-17 17:56:21 +00:00
|
|
|
}
|
|
|
|
|
2014-11-10 18:19:06 +00:00
|
|
|
static void test_resource_size_changed(skiatest::Reporter* reporter) {
|
2015-02-19 15:24:21 +00:00
|
|
|
GrUniqueKey key1, key2;
|
|
|
|
make_unique_key<0>(&key1, 1);
|
|
|
|
make_unique_key<0>(&key2, 2);
|
2014-05-05 19:09:13 +00:00
|
|
|
|
|
|
|
// Test changing resources sizes (both increase & decrease).
|
|
|
|
{
|
2015-01-23 15:19:22 +00:00
|
|
|
Mock mock(3, 30000);
|
|
|
|
GrContext* context = mock.context();
|
2015-02-11 18:49:59 +00:00
|
|
|
GrResourceCache* cache = mock.cache();
|
2014-05-05 19:09:13 +00:00
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
TestResource* a = new TestResource(context->getGpu());
|
2015-02-19 15:24:21 +00:00
|
|
|
a->resourcePriv().setUniqueKey(key1);
|
2014-05-05 19:09:13 +00:00
|
|
|
a->unref();
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
TestResource* b = new TestResource(context->getGpu());
|
2015-02-19 15:24:21 +00:00
|
|
|
b->resourcePriv().setUniqueKey(key2);
|
2014-05-05 19:09:13 +00:00
|
|
|
b->unref();
|
|
|
|
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
|
2014-11-10 18:19:06 +00:00
|
|
|
{
|
2015-02-19 15:24:21 +00:00
|
|
|
SkAutoTUnref<TestResource> find2(
|
|
|
|
static_cast<TestResource*>(cache->findAndRefUniqueResource(key2)));
|
2014-11-10 18:19:06 +00:00
|
|
|
find2->setSize(200);
|
2015-02-19 15:24:21 +00:00
|
|
|
SkAutoTUnref<TestResource> find1(
|
|
|
|
static_cast<TestResource*>(cache->findAndRefUniqueResource(key1)));
|
2014-11-10 18:19:06 +00:00
|
|
|
find1->setSize(50);
|
|
|
|
}
|
2014-05-05 19:09:13 +00:00
|
|
|
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
|
2014-05-05 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test increasing a resources size beyond the cache budget.
|
|
|
|
{
|
2015-01-23 15:19:22 +00:00
|
|
|
Mock mock(2, 300);
|
|
|
|
GrContext* context = mock.context();
|
2015-02-11 18:49:59 +00:00
|
|
|
GrResourceCache* cache = mock.cache();
|
2014-05-05 19:09:13 +00:00
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
TestResource* a = new TestResource(context->getGpu());
|
2014-11-10 18:19:06 +00:00
|
|
|
a->setSize(100);
|
2015-02-19 15:24:21 +00:00
|
|
|
a->resourcePriv().setUniqueKey(key1);
|
2014-05-05 19:09:13 +00:00
|
|
|
a->unref();
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
TestResource* b = new TestResource(context->getGpu());
|
2014-11-10 18:19:06 +00:00
|
|
|
b->setSize(100);
|
2015-02-19 15:24:21 +00:00
|
|
|
b->resourcePriv().setUniqueKey(key2);
|
2014-05-05 19:09:13 +00:00
|
|
|
b->unref();
|
|
|
|
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 2 == cache->getResourceCount());
|
2014-05-05 19:09:13 +00:00
|
|
|
|
2014-11-10 18:19:06 +00:00
|
|
|
{
|
2015-02-19 15:24:21 +00:00
|
|
|
SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(
|
|
|
|
cache->findAndRefUniqueResource(key2)));
|
2014-11-10 18:19:06 +00:00
|
|
|
find2->setSize(201);
|
|
|
|
}
|
2015-02-19 15:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
|
2014-05-05 19:09:13 +00:00
|
|
|
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes());
|
|
|
|
REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
|
2014-05-05 19:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 19:38:44 +00:00
|
|
|
static void test_timestamp_wrap(skiatest::Reporter* reporter) {
|
|
|
|
static const int kCount = 50;
|
|
|
|
static const int kBudgetCnt = kCount / 2;
|
|
|
|
static const int kLockedFreq = 8;
|
|
|
|
static const int kBudgetSize = 0x80000000;
|
|
|
|
|
|
|
|
SkRandom random;
|
|
|
|
|
|
|
|
// Run the test 2*kCount times;
|
|
|
|
for (int i = 0; i < 2 * kCount; ++i ) {
|
|
|
|
Mock mock(kBudgetCnt, kBudgetSize);
|
|
|
|
GrContext* context = mock.context();
|
|
|
|
GrResourceCache* cache = mock.cache();
|
|
|
|
|
|
|
|
// Pick a random number of resources to add before the timestamp will wrap.
|
|
|
|
cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1));
|
|
|
|
|
|
|
|
static const int kNumToPurge = kCount - kBudgetCnt;
|
|
|
|
|
|
|
|
SkTDArray<int> shouldPurgeIdxs;
|
|
|
|
int purgeableCnt = 0;
|
|
|
|
SkTDArray<GrGpuResource*> resourcesToUnref;
|
|
|
|
|
|
|
|
// Add kCount resources, holding onto resources at random so we have a mix of purgeable and
|
|
|
|
// unpurgeable resources.
|
|
|
|
for (int j = 0; j < kCount; ++j) {
|
|
|
|
GrUniqueKey key;
|
|
|
|
make_unique_key<0>(&key, j);
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
TestResource* r = new TestResource(context->getGpu());
|
2015-02-19 19:38:44 +00:00
|
|
|
r->resourcePriv().setUniqueKey(key);
|
|
|
|
if (random.nextU() % kLockedFreq) {
|
|
|
|
// Make this is purgeable.
|
|
|
|
r->unref();
|
|
|
|
++purgeableCnt;
|
|
|
|
if (purgeableCnt <= kNumToPurge) {
|
|
|
|
*shouldPurgeIdxs.append() = j;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*resourcesToUnref.append() = r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the correct resources were purged.
|
|
|
|
int currShouldPurgeIdx = 0;
|
|
|
|
for (int j = 0; j < kCount; ++j) {
|
|
|
|
GrUniqueKey key;
|
|
|
|
make_unique_key<0>(&key, j);
|
|
|
|
GrGpuResource* res = cache->findAndRefUniqueResource(key);
|
|
|
|
if (currShouldPurgeIdx < shouldPurgeIdxs.count() &&
|
|
|
|
shouldPurgeIdxs[currShouldPurgeIdx] == j) {
|
|
|
|
++currShouldPurgeIdx;
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == res);
|
2015-02-19 19:38:44 +00:00
|
|
|
} else {
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr != res);
|
2015-02-19 19:38:44 +00:00
|
|
|
}
|
|
|
|
SkSafeUnref(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int j = 0; j < resourcesToUnref.count(); ++j) {
|
|
|
|
resourcesToUnref[j]->unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 18:01:54 +00:00
|
|
|
static void test_flush(skiatest::Reporter* reporter) {
|
|
|
|
Mock mock(1000000, 1000000);
|
|
|
|
GrContext* context = mock.context();
|
|
|
|
GrResourceCache* cache = mock.cache();
|
|
|
|
|
|
|
|
// The current cache impl will round the max flush count to the next power of 2. So we choose a
|
|
|
|
// power of two here to keep things simpler.
|
|
|
|
static const int kFlushCount = 16;
|
|
|
|
cache->setLimits(1000000, 1000000, kFlushCount);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Insert a resource and send a flush notification kFlushCount times.
|
|
|
|
for (int i = 0; i < kFlushCount; ++i) {
|
2015-08-26 20:07:48 +00:00
|
|
|
TestResource* r = new TestResource(context->getGpu());
|
2015-04-08 18:01:54 +00:00
|
|
|
GrUniqueKey k;
|
|
|
|
make_unique_key<1>(&k, i);
|
|
|
|
r->resourcePriv().setUniqueKey(k);
|
|
|
|
r->unref();
|
|
|
|
cache->notifyFlushOccurred();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send flush notifications to the cache. Each flush should purge the oldest resource.
|
|
|
|
for (int i = 0; i < kFlushCount - 1; ++i) {
|
|
|
|
// The first resource was purged after the last flush in the initial loop, hence the -1.
|
|
|
|
REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount());
|
|
|
|
for (int j = 0; j < i; ++j) {
|
|
|
|
GrUniqueKey k;
|
|
|
|
make_unique_key<1>(&k, j);
|
|
|
|
GrGpuResource* r = cache->findAndRefUniqueResource(k);
|
|
|
|
REPORTER_ASSERT(reporter, !SkToBool(r));
|
|
|
|
SkSafeUnref(r);
|
|
|
|
}
|
|
|
|
cache->notifyFlushOccurred();
|
|
|
|
}
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
|
|
|
|
cache->purgeAllUnlocked();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a similar test but where we leave refs on some resources to prevent them from being
|
|
|
|
// purged.
|
|
|
|
{
|
|
|
|
GrGpuResource* refedResources[kFlushCount >> 1];
|
|
|
|
for (int i = 0; i < kFlushCount; ++i) {
|
2015-08-26 20:07:48 +00:00
|
|
|
TestResource* r = new TestResource(context->getGpu());
|
2015-04-08 18:01:54 +00:00
|
|
|
GrUniqueKey k;
|
|
|
|
make_unique_key<1>(&k, i);
|
|
|
|
r->resourcePriv().setUniqueKey(k);
|
|
|
|
// Leave a ref on every other resource, beginning with the first.
|
|
|
|
if (SkToBool(i & 0x1)) {
|
|
|
|
refedResources[i/2] = r;
|
|
|
|
} else {
|
|
|
|
r->unref();
|
|
|
|
}
|
|
|
|
cache->notifyFlushOccurred();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < kFlushCount; ++i) {
|
|
|
|
// Should get a resource purged every other flush.
|
|
|
|
REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
|
|
|
|
cache->notifyFlushOccurred();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unref all the resources that we kept refs on in the first loop.
|
|
|
|
for (int i = 0; i < kFlushCount >> 1; ++i) {
|
|
|
|
refedResources[i]->unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
// When we unref'ed them their timestamps got updated. So nothing should be purged until we
|
|
|
|
// get kFlushCount additional flushes. Then everything should be purged.
|
|
|
|
for (int i = 0; i < kFlushCount; ++i) {
|
|
|
|
REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
|
|
|
|
cache->notifyFlushOccurred();
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
|
|
|
|
|
|
|
|
cache->purgeAllUnlocked();
|
|
|
|
}
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
|
|
|
|
}
|
|
|
|
|
2014-11-25 13:52:06 +00:00
|
|
|
static void test_large_resource_count(skiatest::Reporter* reporter) {
|
|
|
|
// Set the cache size to double the resource count because we're going to create 2x that number
|
|
|
|
// resources, using two different key domains. Add a little slop to the bytes because we resize
|
|
|
|
// down to 1 byte after creating the resource.
|
2015-01-23 15:19:22 +00:00
|
|
|
static const int kResourceCnt = 2000;
|
2014-11-25 13:52:06 +00:00
|
|
|
|
2015-01-23 15:19:22 +00:00
|
|
|
Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000);
|
|
|
|
GrContext* context = mock.context();
|
2015-02-11 18:49:59 +00:00
|
|
|
GrResourceCache* cache = mock.cache();
|
2014-11-25 13:52:06 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < kResourceCnt; ++i) {
|
2015-02-19 15:24:21 +00:00
|
|
|
GrUniqueKey key1, key2;
|
|
|
|
make_unique_key<1>(&key1, i);
|
|
|
|
make_unique_key<2>(&key2, i);
|
2015-01-23 12:24:04 +00:00
|
|
|
|
2014-11-25 13:52:06 +00:00
|
|
|
TestResource* resource;
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
resource = new TestResource(context->getGpu());
|
2015-02-19 15:24:21 +00:00
|
|
|
resource->resourcePriv().setUniqueKey(key1);
|
2014-11-25 13:52:06 +00:00
|
|
|
resource->setSize(1);
|
|
|
|
resource->unref();
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
resource = new TestResource(context->getGpu());
|
2015-02-19 15:24:21 +00:00
|
|
|
resource->resourcePriv().setUniqueKey(key2);
|
2014-11-25 13:52:06 +00:00
|
|
|
resource->setSize(1);
|
|
|
|
resource->unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt);
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt);
|
|
|
|
REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt);
|
|
|
|
REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt);
|
|
|
|
REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt);
|
2014-11-25 13:52:06 +00:00
|
|
|
for (int i = 0; i < kResourceCnt; ++i) {
|
2015-02-19 15:24:21 +00:00
|
|
|
GrUniqueKey key1, key2;
|
|
|
|
make_unique_key<1>(&key1, i);
|
|
|
|
make_unique_key<2>(&key2, i);
|
2015-01-23 12:24:04 +00:00
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1));
|
|
|
|
REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2));
|
2014-11-25 13:52:06 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 18:49:59 +00:00
|
|
|
cache->purgeAllUnlocked();
|
2014-11-25 13:52:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0);
|
2015-02-11 18:49:59 +00:00
|
|
|
REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0);
|
|
|
|
REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0);
|
|
|
|
REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0);
|
|
|
|
REPORTER_ASSERT(reporter, cache->getResourceCount() == 0);
|
2014-11-25 13:52:06 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < kResourceCnt; ++i) {
|
2015-02-19 15:24:21 +00:00
|
|
|
GrUniqueKey key1, key2;
|
|
|
|
make_unique_key<1>(&key1, i);
|
|
|
|
make_unique_key<2>(&key2, i);
|
2015-01-23 12:24:04 +00:00
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1));
|
|
|
|
REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2));
|
2014-11-25 13:52:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 17:01:58 +00:00
|
|
|
static void test_custom_data(skiatest::Reporter* reporter) {
|
|
|
|
GrUniqueKey key1, key2;
|
|
|
|
make_unique_key<0>(&key1, 1);
|
|
|
|
make_unique_key<0>(&key2, 2);
|
|
|
|
int foo = 4132;
|
|
|
|
SkAutoTUnref<SkData> data(SkData::NewWithCopy(&foo, sizeof(foo)));
|
|
|
|
key1.setCustomData(data.get());
|
|
|
|
REPORTER_ASSERT(reporter, *(int*) key1.getCustomData()->data() == 4132);
|
|
|
|
REPORTER_ASSERT(reporter, key2.getCustomData() == nullptr);
|
|
|
|
|
|
|
|
// Test that copying a key also takes a ref on its custom data.
|
|
|
|
GrUniqueKey key3 = key1;
|
|
|
|
REPORTER_ASSERT(reporter, *(int*) key3.getCustomData()->data() == 4132);
|
|
|
|
}
|
|
|
|
|
2015-09-24 14:07:40 +00:00
|
|
|
static void test_abandoned(skiatest::Reporter* reporter) {
|
|
|
|
Mock mock(10, 300);
|
|
|
|
GrContext* context = mock.context();
|
2015-09-24 17:22:45 +00:00
|
|
|
SkAutoTUnref<GrGpuResource> resource(new TestResource(context->getGpu()));
|
2015-09-24 14:07:40 +00:00
|
|
|
context->abandonContext();
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, resource->wasDestroyed());
|
|
|
|
|
|
|
|
// Call all the public methods on resource in the abandoned state. They shouldn't crash.
|
|
|
|
|
|
|
|
int foo = 4132;
|
|
|
|
SkAutoTUnref<SkData> data(SkData::NewWithCopy(&foo, sizeof(foo)));
|
|
|
|
resource->setCustomData(data.get());
|
|
|
|
resource->getCustomData();
|
|
|
|
resource->getUniqueID();
|
|
|
|
resource->getUniqueKey();
|
|
|
|
resource->wasDestroyed();
|
|
|
|
resource->gpuMemorySize();
|
|
|
|
resource->getContext();
|
|
|
|
|
|
|
|
resource->abandon();
|
|
|
|
resource->resourcePriv().getScratchKey();
|
|
|
|
resource->resourcePriv().isBudgeted();
|
|
|
|
resource->resourcePriv().makeBudgeted();
|
|
|
|
resource->resourcePriv().makeUnbudgeted();
|
|
|
|
resource->resourcePriv().removeScratchKey();
|
|
|
|
GrUniqueKey key;
|
|
|
|
make_unique_key<0>(&key, 1);
|
|
|
|
resource->resourcePriv().setUniqueKey(key);
|
|
|
|
resource->resourcePriv().removeUniqueKey();
|
|
|
|
}
|
|
|
|
|
2014-01-15 23:09:01 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2014-01-10 22:08:27 +00:00
|
|
|
DEF_GPUTEST(ResourceCache, reporter, factory) {
|
2013-08-08 22:55:21 +00:00
|
|
|
for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
|
|
|
|
GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type);
|
|
|
|
if (!GrContextFactory::IsRenderingGLContext(glType)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
GrContext* context = factory->get(glType);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == context) {
|
2014-11-05 20:30:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-10-28 21:33:06 +00:00
|
|
|
GrSurfaceDesc desc;
|
2013-08-08 22:55:21 +00:00
|
|
|
desc.fConfig = kSkia8888_GrPixelConfig;
|
2014-10-28 21:33:06 +00:00
|
|
|
desc.fFlags = kRenderTarget_GrSurfaceFlag;
|
2013-08-08 22:55:21 +00:00
|
|
|
desc.fWidth = gWidth;
|
|
|
|
desc.fHeight = gHeight;
|
2014-09-18 13:09:44 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight);
|
2015-01-16 15:32:33 +00:00
|
|
|
SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context,
|
|
|
|
SkSurface::kNo_Budgeted, info));
|
2014-11-05 20:30:32 +00:00
|
|
|
test_cache(reporter, context, surface->getCanvas());
|
2015-02-19 17:09:00 +00:00
|
|
|
test_stencil_buffers(reporter, context);
|
2015-06-18 16:12:16 +00:00
|
|
|
test_wrapped_resources(reporter, context);
|
2014-11-05 20:09:45 +00:00
|
|
|
}
|
2014-11-05 22:47:41 +00:00
|
|
|
|
2014-11-10 18:19:06 +00:00
|
|
|
// The below tests create their own mock contexts.
|
2014-11-14 20:10:14 +00:00
|
|
|
test_no_key(reporter);
|
2014-11-17 17:33:27 +00:00
|
|
|
test_budgeting(reporter);
|
2015-01-14 18:42:08 +00:00
|
|
|
test_unbudgeted(reporter);
|
2015-01-23 15:19:22 +00:00
|
|
|
test_unbudgeted_to_scratch(reporter);
|
2015-02-19 15:24:21 +00:00
|
|
|
test_duplicate_unique_key(reporter);
|
2014-11-10 18:19:06 +00:00
|
|
|
test_duplicate_scratch_key(reporter);
|
2014-11-25 13:52:06 +00:00
|
|
|
test_remove_scratch_key(reporter);
|
2015-01-21 17:32:40 +00:00
|
|
|
test_scratch_key_consistency(reporter);
|
2014-11-10 18:19:06 +00:00
|
|
|
test_purge_invalidated(reporter);
|
2014-11-14 20:10:14 +00:00
|
|
|
test_cache_chained_purge(reporter);
|
2014-11-10 18:19:06 +00:00
|
|
|
test_resource_size_changed(reporter);
|
2015-02-19 19:38:44 +00:00
|
|
|
test_timestamp_wrap(reporter);
|
2015-04-08 18:01:54 +00:00
|
|
|
test_flush(reporter);
|
2014-11-25 13:52:06 +00:00
|
|
|
test_large_resource_count(reporter);
|
2015-08-04 17:01:58 +00:00
|
|
|
test_custom_data(reporter);
|
2015-09-24 14:07:40 +00:00
|
|
|
test_abandoned(reporter);
|
2013-08-08 22:55:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|