2014-09-03 18:04:30 +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 "Test.h"
|
|
|
|
|
2014-12-03 15:33:57 +00:00
|
|
|
#include "GrContextFactory.h"
|
|
|
|
#include "GrLayerCache.h"
|
2014-09-29 12:32:49 +00:00
|
|
|
#include "GrRecordReplaceDraw.h"
|
|
|
|
#include "RecordTestUtils.h"
|
2014-09-03 18:04:30 +00:00
|
|
|
#include "SkBBHFactory.h"
|
2014-09-29 12:32:49 +00:00
|
|
|
#include "SkPictureRecorder.h"
|
2014-09-03 18:04:30 +00:00
|
|
|
#include "SkRecordDraw.h"
|
|
|
|
#include "SkRecorder.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
|
|
|
|
static const int kWidth = 100;
|
|
|
|
static const int kHeight = 100;
|
|
|
|
|
2015-01-07 15:28:41 +00:00
|
|
|
class JustOneDraw : public SkPicture::AbortCallback {
|
2014-09-03 18:04:30 +00:00
|
|
|
public:
|
|
|
|
JustOneDraw() : fCalls(0) {}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool abort() override { return fCalls++ > 0; }
|
2014-09-03 18:04:30 +00:00
|
|
|
private:
|
|
|
|
int fCalls;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Make sure the abort callback works
|
|
|
|
DEF_TEST(RecordReplaceDraw_Abort, r) {
|
2014-09-29 12:32:49 +00:00
|
|
|
SkAutoTUnref<const SkPicture> pic;
|
|
|
|
|
|
|
|
{
|
|
|
|
// Record two commands.
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
|
|
|
|
|
|
|
|
canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)), SkPaint());
|
|
|
|
canvas->clipRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)));
|
|
|
|
|
|
|
|
pic.reset(recorder.endRecording());
|
|
|
|
}
|
2014-09-03 18:04:30 +00:00
|
|
|
|
|
|
|
SkRecord rerecord;
|
|
|
|
SkRecorder canvas(&rerecord, kWidth, kHeight);
|
|
|
|
|
|
|
|
JustOneDraw callback;
|
2014-12-03 15:33:57 +00:00
|
|
|
GrRecordReplaceDraw(pic, &canvas, NULL, SkMatrix::I(), &callback);
|
2014-09-03 18:04:30 +00:00
|
|
|
|
2014-12-11 15:07:37 +00:00
|
|
|
switch (rerecord.count()) {
|
|
|
|
case 3:
|
|
|
|
assert_type<SkRecords::Save>(r, rerecord, 0);
|
|
|
|
assert_type<SkRecords::DrawRect>(r, rerecord, 1);
|
|
|
|
assert_type<SkRecords::Restore>(r, rerecord, 2);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
assert_type<SkRecords::DrawRect>(r, rerecord, 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
REPORTER_ASSERT(r, false);
|
|
|
|
}
|
2014-09-03 18:04:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure GrRecordReplaceDraw balances unbalanced saves
|
|
|
|
DEF_TEST(RecordReplaceDraw_Unbalanced, r) {
|
2014-09-29 12:32:49 +00:00
|
|
|
SkAutoTUnref<const SkPicture> pic;
|
|
|
|
|
|
|
|
{
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
|
|
|
|
|
|
|
|
// We won't balance this, but GrRecordReplaceDraw will for us.
|
|
|
|
canvas->save();
|
2014-12-11 15:07:37 +00:00
|
|
|
canvas->scale(2, 2);
|
2014-09-29 12:32:49 +00:00
|
|
|
pic.reset(recorder.endRecording());
|
|
|
|
}
|
2014-09-03 18:04:30 +00:00
|
|
|
|
|
|
|
SkRecord rerecord;
|
|
|
|
SkRecorder canvas(&rerecord, kWidth, kHeight);
|
|
|
|
|
2014-12-03 15:33:57 +00:00
|
|
|
GrRecordReplaceDraw(pic, &canvas, NULL, SkMatrix::I(), NULL/*callback*/);
|
2014-09-03 18:04:30 +00:00
|
|
|
|
2014-12-11 15:30:58 +00:00
|
|
|
// ensure rerecord is balanced (in this case by checking that the count is odd)
|
|
|
|
REPORTER_ASSERT(r, (rerecord.count() & 1) == 1);
|
2014-09-03 18:04:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test out the layer replacement functionality with and w/o a BBH
|
2014-12-03 15:33:57 +00:00
|
|
|
void test_replacements(skiatest::Reporter* r, GrContext* context, bool useBBH) {
|
2014-09-29 12:32:49 +00:00
|
|
|
SkAutoTUnref<const SkPicture> pic;
|
|
|
|
|
|
|
|
{
|
|
|
|
SkRTreeFactory bbhFactory;
|
|
|
|
SkPictureRecorder recorder;
|
2014-10-27 17:27:10 +00:00
|
|
|
SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight),
|
2014-09-29 12:32:49 +00:00
|
|
|
useBBH ? &bbhFactory : NULL);
|
|
|
|
|
2014-12-04 15:53:21 +00:00
|
|
|
SkPaint paint;
|
|
|
|
canvas->saveLayer(NULL, &paint);
|
2014-09-29 12:32:49 +00:00
|
|
|
canvas->clear(SK_ColorRED);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth / 2), SkIntToScalar(kHeight / 2)),
|
|
|
|
SkPaint());
|
|
|
|
pic.reset(recorder.endRecording());
|
|
|
|
}
|
2014-09-03 18:04:30 +00:00
|
|
|
|
2014-12-03 15:33:57 +00:00
|
|
|
unsigned key[1] = { 0 };
|
2014-12-01 17:09:27 +00:00
|
|
|
|
2014-12-04 15:53:21 +00:00
|
|
|
SkPaint paint;
|
2014-12-03 15:33:57 +00:00
|
|
|
GrLayerCache* layerCache = context->getLayerCache();
|
2014-12-04 15:53:21 +00:00
|
|
|
GrCachedLayer* layer = layerCache->findLayerOrCreate(pic->uniqueID(), 0, 2,
|
2014-12-16 16:25:55 +00:00
|
|
|
SkIRect::MakeWH(kWidth, kHeight),
|
2014-12-03 15:33:57 +00:00
|
|
|
SkIRect::MakeWH(kWidth, kHeight),
|
2014-12-04 15:53:21 +00:00
|
|
|
SkMatrix::I(), key, 1, &paint);
|
2014-12-03 15:33:57 +00:00
|
|
|
|
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fConfig = kSkia8888_GrPixelConfig;
|
|
|
|
desc.fFlags = kRenderTarget_GrSurfaceFlag;
|
|
|
|
desc.fWidth = kWidth;
|
|
|
|
desc.fHeight = kHeight;
|
|
|
|
desc.fSampleCnt = 0;
|
|
|
|
|
2015-02-06 16:49:24 +00:00
|
|
|
SkAutoTUnref<GrTexture> texture(context->createTexture(desc, false, NULL, 0));
|
2014-12-03 15:33:57 +00:00
|
|
|
layer->setTexture(texture, SkIRect::MakeWH(kWidth, kHeight));
|
2014-09-03 18:04:30 +00:00
|
|
|
|
|
|
|
SkAutoTUnref<SkBBoxHierarchy> bbh;
|
|
|
|
|
|
|
|
SkRecord rerecord;
|
|
|
|
SkRecorder canvas(&rerecord, kWidth, kHeight);
|
2014-12-03 15:33:57 +00:00
|
|
|
GrRecordReplaceDraw(pic, &canvas, layerCache, SkMatrix::I(), NULL/*callback*/);
|
2014-09-03 18:04:30 +00:00
|
|
|
|
2014-12-11 15:07:37 +00:00
|
|
|
int recount = rerecord.count();
|
2014-12-16 16:25:55 +00:00
|
|
|
REPORTER_ASSERT(r, 2 == recount || 4 == recount);
|
2014-12-11 15:07:37 +00:00
|
|
|
|
|
|
|
int index = 0;
|
2014-12-16 16:25:55 +00:00
|
|
|
if (4 == recount) {
|
2014-12-11 15:07:37 +00:00
|
|
|
assert_type<SkRecords::Save>(r, rerecord, 0);
|
|
|
|
index += 1;
|
|
|
|
}
|
2014-12-16 16:25:55 +00:00
|
|
|
assert_type<SkRecords::DrawSprite>(r, rerecord, index + 0);
|
|
|
|
assert_type<SkRecords::DrawRect>(r, rerecord, index + 1);
|
|
|
|
if (4 == recount) {
|
|
|
|
assert_type<SkRecords::Restore>(r, rerecord, 3);
|
2014-12-11 15:07:37 +00:00
|
|
|
}
|
2014-09-03 18:04:30 +00:00
|
|
|
}
|
|
|
|
|
2014-12-03 15:33:57 +00:00
|
|
|
DEF_GPUTEST(RecordReplaceDraw, r, factory) {
|
|
|
|
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);
|
|
|
|
if (NULL == context) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
test_replacements(r, context, true);
|
|
|
|
test_replacements(r, context, false);
|
|
|
|
}
|
|
|
|
}
|
2014-09-03 18:04:30 +00:00
|
|
|
|
|
|
|
#endif
|