blob: 5fcac4d93e7e4c3e23cd57b2b4a9d0c116259773 [file] [log] [blame]
commit-bot@chromium.orgc4b21e62014-04-11 18:33:31 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +00008#include "Test.h"
9
10#include "SkRecord.h"
11#include "SkRecorder.h"
12#include "SkRecords.h"
reede1085e02014-07-03 07:26:01 -070013#include "SkShader.h"
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000014
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000015#define COUNT(T) + 1
16static const int kRecordTypes = SK_RECORD_TYPES(COUNT);
17#undef COUNT
18
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000019// Tallies the types of commands it sees into a histogram.
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000020class Tally {
21public:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000022 Tally() { sk_bzero(&fHistogram, sizeof(fHistogram)); }
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000023
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000024 template <typename T>
25 void operator()(const T&) { ++fHistogram[T::kType]; }
26
27 template <typename T>
28 int count() const { return fHistogram[T::kType]; }
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000029
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000030 void apply(const SkRecord& record) {
31 for (unsigned i = 0; i < record.count(); i++) {
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000032 record.visit<void>(i, *this);
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000033 }
34 }
35
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000036private:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000037 int fHistogram[kRecordTypes];
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000038};
39
40DEF_TEST(Recorder, r) {
41 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000042 SkRecorder recorder(&record, 1920, 1080);
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000043
44 recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint());
45
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000046 Tally tally;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000047 tally.apply(record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000048 REPORTER_ASSERT(r, 1 == tally.count<SkRecords::DrawRect>());
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000049}
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000050
51// Regression test for leaking refs held by optional arguments.
52DEF_TEST(Recorder_RefLeaking, r) {
53 // We use SaveLayer to test:
54 // - its SkRect argument is optional and SkRect is POD. Just testing that that works.
55 // - its SkPaint argument is optional and SkPaint is not POD. The bug was here.
56
commit-bot@chromium.org12a04122014-04-15 18:00:57 +000057 SkRect bounds = SkRect::MakeWH(320, 240);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000058 SkPaint paint;
reede1085e02014-07-03 07:26:01 -070059 paint.setShader(SkShader::CreateEmptyShader())->unref();
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000060
61 REPORTER_ASSERT(r, paint.getShader()->unique());
62 {
63 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000064 SkRecorder recorder(&record, 1920, 1080);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000065 recorder.saveLayer(&bounds, &paint);
66 REPORTER_ASSERT(r, !paint.getShader()->unique());
67 }
68 REPORTER_ASSERT(r, paint.getShader()->unique());
69}