2014-04-11 18:33:31 +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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
|
|
|
|
|
|
|
#include "include/core/SkPictureRecorder.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "src/core/SkRecord.h"
|
|
|
|
#include "src/core/SkRecorder.h"
|
|
|
|
#include "src/core/SkRecords.h"
|
2014-04-15 14:27:14 +00:00
|
|
|
|
2014-04-08 20:17:26 +00:00
|
|
|
#define COUNT(T) + 1
|
|
|
|
static const int kRecordTypes = SK_RECORD_TYPES(COUNT);
|
|
|
|
#undef COUNT
|
|
|
|
|
2014-04-08 23:31:35 +00:00
|
|
|
// Tallies the types of commands it sees into a histogram.
|
2014-04-08 20:17:26 +00:00
|
|
|
class Tally {
|
|
|
|
public:
|
2014-04-08 23:31:35 +00:00
|
|
|
Tally() { sk_bzero(&fHistogram, sizeof(fHistogram)); }
|
2014-04-08 20:17:26 +00:00
|
|
|
|
2014-04-08 23:31:35 +00:00
|
|
|
template <typename T>
|
|
|
|
void operator()(const T&) { ++fHistogram[T::kType]; }
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
int count() const { return fHistogram[T::kType]; }
|
2014-04-08 20:17:26 +00:00
|
|
|
|
2014-04-22 16:57:20 +00:00
|
|
|
void apply(const SkRecord& record) {
|
2015-08-19 16:51:00 +00:00
|
|
|
for (int i = 0; i < record.count(); i++) {
|
2016-03-22 18:46:53 +00:00
|
|
|
record.visit(i, *this);
|
2014-04-22 16:57:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-08 20:17:26 +00:00
|
|
|
private:
|
2014-04-08 23:31:35 +00:00
|
|
|
int fHistogram[kRecordTypes];
|
2014-04-08 20:17:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_TEST(Recorder, r) {
|
|
|
|
SkRecord record;
|
2014-05-29 16:52:40 +00:00
|
|
|
SkRecorder recorder(&record, 1920, 1080);
|
2014-04-08 20:17:26 +00:00
|
|
|
|
|
|
|
recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint());
|
|
|
|
|
2014-04-08 23:31:35 +00:00
|
|
|
Tally tally;
|
2014-04-22 16:57:20 +00:00
|
|
|
tally.apply(record);
|
2014-04-08 23:31:35 +00:00
|
|
|
REPORTER_ASSERT(r, 1 == tally.count<SkRecords::DrawRect>());
|
2014-04-08 20:17:26 +00:00
|
|
|
}
|
2014-04-15 14:27:14 +00:00
|
|
|
|
|
|
|
// Regression test for leaking refs held by optional arguments.
|
|
|
|
DEF_TEST(Recorder_RefLeaking, r) {
|
|
|
|
// We use SaveLayer to test:
|
|
|
|
// - its SkRect argument is optional and SkRect is POD. Just testing that that works.
|
|
|
|
// - its SkPaint argument is optional and SkPaint is not POD. The bug was here.
|
|
|
|
|
2014-04-15 18:00:57 +00:00
|
|
|
SkRect bounds = SkRect::MakeWH(320, 240);
|
2014-04-15 14:27:14 +00:00
|
|
|
SkPaint paint;
|
2019-04-09 17:55:36 +00:00
|
|
|
paint.setShader(SkShaders::Empty());
|
2014-04-15 14:27:14 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(r, paint.getShader()->unique());
|
|
|
|
{
|
|
|
|
SkRecord record;
|
2014-05-29 16:52:40 +00:00
|
|
|
SkRecorder recorder(&record, 1920, 1080);
|
2014-04-15 14:27:14 +00:00
|
|
|
recorder.saveLayer(&bounds, &paint);
|
|
|
|
REPORTER_ASSERT(r, !paint.getShader()->unique());
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(r, paint.getShader()->unique());
|
|
|
|
}
|
2014-08-07 19:19:50 +00:00
|
|
|
|
2014-10-16 18:58:39 +00:00
|
|
|
DEF_TEST(Recorder_drawImage_takeReference, reporter) {
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image;
|
2014-10-16 18:58:39 +00:00
|
|
|
{
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRasterN32Premul(100, 100));
|
2014-10-16 18:58:39 +00:00
|
|
|
surface->getCanvas()->clear(SK_ColorGREEN);
|
2016-03-17 17:51:11 +00:00
|
|
|
image = surface->makeImageSnapshot();
|
2014-10-16 18:58:39 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
SkRecord record;
|
|
|
|
SkRecorder recorder(&record, 100, 100);
|
|
|
|
|
|
|
|
// DrawImage is supposed to take a reference
|
2016-03-17 17:51:11 +00:00
|
|
|
recorder.drawImage(image, 0, 0);
|
2014-10-16 18:58:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, !image->unique());
|
|
|
|
|
|
|
|
Tally tally;
|
|
|
|
tally.apply(record);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImage>());
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, image->unique());
|
|
|
|
|
|
|
|
{
|
|
|
|
SkRecord record;
|
|
|
|
SkRecorder recorder(&record, 100, 100);
|
|
|
|
|
|
|
|
// DrawImageRect is supposed to take a reference
|
2016-03-17 17:51:11 +00:00
|
|
|
recorder.drawImageRect(image, SkRect::MakeWH(100, 100), nullptr);
|
2014-10-16 18:58:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, !image->unique());
|
|
|
|
|
|
|
|
Tally tally;
|
|
|
|
tally.apply(record);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImageRect>());
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, image->unique());
|
|
|
|
}
|