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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Test.h"
|
|
|
|
|
2015-04-30 21:18:54 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
#include "GrContext.h"
|
2014-12-03 15:33:57 +00:00
|
|
|
#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) {
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<SkPicture> pic;
|
2014-09-29 12:32:49 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// 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)));
|
|
|
|
|
2016-03-18 14:25:55 +00:00
|
|
|
pic = recorder.finishRecordingAsPicture();
|
2014-09-29 12:32:49 +00:00
|
|
|
}
|
2014-09-03 18:04:30 +00:00
|
|
|
|
|
|
|
SkRecord rerecord;
|
|
|
|
SkRecorder canvas(&rerecord, kWidth, kHeight);
|
|
|
|
|
|
|
|
JustOneDraw callback;
|
2016-03-18 14:25:55 +00:00
|
|
|
GrRecordReplaceDraw(pic.get(), &canvas, nullptr, 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) {
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<SkPicture> pic;
|
2014-09-29 12:32:49 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
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);
|
2016-03-18 14:25:55 +00:00
|
|
|
pic = recorder.finishRecordingAsPicture();
|
2014-09-29 12:32:49 +00:00
|
|
|
}
|
2014-09-03 18:04:30 +00:00
|
|
|
|
|
|
|
SkRecord rerecord;
|
|
|
|
SkRecorder canvas(&rerecord, kWidth, kHeight);
|
|
|
|
|
2016-03-18 14:25:55 +00:00
|
|
|
GrRecordReplaceDraw(pic.get(), &canvas, nullptr, SkMatrix::I(), nullptr/*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
|
2015-11-04 12:31:06 +00:00
|
|
|
void test_replacements(skiatest::Reporter* r, GrContext* context, bool doReplace) {
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<SkPicture> pic;
|
2014-09-29 12:32:49 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
SkPictureRecorder recorder;
|
2015-11-04 12:31:06 +00:00
|
|
|
SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
|
2014-12-04 15:53:21 +00:00
|
|
|
SkPaint paint;
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(nullptr, &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());
|
2016-03-18 14:25:55 +00:00
|
|
|
pic = recorder.finishRecordingAsPicture();
|
2014-09-29 12:32:49 +00:00
|
|
|
}
|
2014-09-03 18:04:30 +00:00
|
|
|
|
2015-11-04 12:31:06 +00:00
|
|
|
SkAutoTUnref<GrTexture> texture;
|
2014-12-04 15:53:21 +00:00
|
|
|
SkPaint paint;
|
2014-12-03 15:33:57 +00:00
|
|
|
GrLayerCache* layerCache = context->getLayerCache();
|
|
|
|
|
2015-11-04 12:31:06 +00:00
|
|
|
if (doReplace) {
|
|
|
|
int key[1] = { 0 };
|
2014-12-03 15:33:57 +00:00
|
|
|
|
2015-11-04 12:31:06 +00:00
|
|
|
GrCachedLayer* layer = layerCache->findLayerOrCreate(pic->uniqueID(), 0, 2,
|
|
|
|
SkIRect::MakeWH(kWidth, kHeight),
|
|
|
|
SkIRect::MakeWH(kWidth, kHeight),
|
|
|
|
SkMatrix::I(), key, 1, &paint);
|
2014-09-03 18:04:30 +00:00
|
|
|
|
2015-11-04 12:31:06 +00:00
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fConfig = kSkia8888_GrPixelConfig;
|
|
|
|
desc.fFlags = kRenderTarget_GrSurfaceFlag;
|
|
|
|
desc.fWidth = kWidth;
|
|
|
|
desc.fHeight = kHeight;
|
|
|
|
desc.fSampleCnt = 0;
|
|
|
|
|
2016-02-25 16:33:02 +00:00
|
|
|
texture.reset(context->textureProvider()->createTexture(
|
|
|
|
desc, SkBudgeted::kNo, nullptr, 0));
|
2015-11-09 21:51:06 +00:00
|
|
|
layer->setTexture(texture, SkIRect::MakeWH(kWidth, kHeight), false);
|
2015-11-04 12:31:06 +00:00
|
|
|
}
|
2014-09-03 18:04:30 +00:00
|
|
|
|
|
|
|
SkRecord rerecord;
|
|
|
|
SkRecorder canvas(&rerecord, kWidth, kHeight);
|
2016-03-18 14:25:55 +00:00
|
|
|
GrRecordReplaceDraw(pic.get(), &canvas, layerCache, SkMatrix::I(), nullptr/*callback*/);
|
2014-09-03 18:04:30 +00:00
|
|
|
|
2015-11-04 12:31:06 +00:00
|
|
|
int numLayers = count_instances_of_type<SkRecords::SaveLayer>(rerecord);
|
|
|
|
if (doReplace) {
|
|
|
|
REPORTER_ASSERT(r, 0 == numLayers);
|
|
|
|
} else {
|
2016-03-29 16:03:52 +00:00
|
|
|
REPORTER_ASSERT(r, 1 == numLayers);
|
2014-12-11 15:07:37 +00:00
|
|
|
}
|
2014-09-03 18:04:30 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 16:59:58 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RecordReplaceDraw, r, ctxInfo) {
|
2016-04-05 19:59:06 +00:00
|
|
|
test_replacements(r, ctxInfo.fGrContext, true);
|
|
|
|
test_replacements(r, ctxInfo.fGrContext, false);
|
2014-12-03 15:33:57 +00:00
|
|
|
}
|
2014-09-03 18:04:30 +00:00
|
|
|
|
|
|
|
#endif
|