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
|
|
|
|
|
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;
|
2015-08-27 14:41:13 +00:00
|
|
|
GrRecordReplaceDraw(pic, &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) {
|
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);
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
GrRecordReplaceDraw(pic, &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) {
|
2014-09-29 12:32:49 +00:00
|
|
|
SkAutoTUnref<const SkPicture> pic;
|
|
|
|
|
|
|
|
{
|
|
|
|
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());
|
|
|
|
pic.reset(recorder.endRecording());
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
texture.reset(context->textureProvider()->createTexture(desc, false, 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);
|
2015-08-27 14:41:13 +00:00
|
|
|
GrRecordReplaceDraw(pic, &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 {
|
|
|
|
REPORTER_ASSERT(r, 1 == numLayers);
|
2014-12-11 15:07:37 +00:00
|
|
|
}
|
2014-09-03 18:04:30 +00:00
|
|
|
}
|
|
|
|
|
2015-08-19 16:51:00 +00:00
|
|
|
DEF_GPUTEST(RecordReplaceDraw, r, factory) {
|
2014-12-03 15:33:57 +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-12-03 15:33:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
test_replacements(r, context, false);
|
2015-11-04 12:31:06 +00:00
|
|
|
test_replacements(r, context, true);
|
2014-12-03 15:33:57 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-03 18:04:30 +00:00
|
|
|
|
|
|
|
#endif
|