blob: 384425ddc241f2890a505bc809f76057c18e30b9 [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"
13
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000014#include "SkEmptyShader.h"
15
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000016#define COUNT(T) + 1
17static const int kRecordTypes = SK_RECORD_TYPES(COUNT);
18#undef COUNT
19
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000020// Tallies the types of commands it sees into a histogram.
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000021class Tally {
22public:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000023 Tally() { sk_bzero(&fHistogram, sizeof(fHistogram)); }
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000024
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000025 template <typename T>
26 void operator()(const T&) { ++fHistogram[T::kType]; }
27
28 template <typename T>
29 int count() const { return fHistogram[T::kType]; }
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000030
31private:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000032 int fHistogram[kRecordTypes];
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000033};
34
35DEF_TEST(Recorder, r) {
36 SkRecord record;
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000037 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, 1920, 1080);
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000038
39 recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint());
40
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000041 Tally tally;
42 record.visit(tally);
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000043
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000044 REPORTER_ASSERT(r, 1 == tally.count<SkRecords::DrawRect>());
commit-bot@chromium.orgb7369622014-04-08 20:17:26 +000045}
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000046
47// Regression test for leaking refs held by optional arguments.
48DEF_TEST(Recorder_RefLeaking, r) {
49 // We use SaveLayer to test:
50 // - its SkRect argument is optional and SkRect is POD. Just testing that that works.
51 // - its SkPaint argument is optional and SkPaint is not POD. The bug was here.
52
commit-bot@chromium.org12a04122014-04-15 18:00:57 +000053 SkRect bounds = SkRect::MakeWH(320, 240);
commit-bot@chromium.org653d5182014-04-15 14:27:14 +000054 SkPaint paint;
55 paint.setShader(SkNEW(SkEmptyShader))->unref();
56
57 REPORTER_ASSERT(r, paint.getShader()->unique());
58 {
59 SkRecord record;
60 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, 1920, 1080);
61 recorder.saveLayer(&bounds, &paint);
62 REPORTER_ASSERT(r, !paint.getShader()->unique());
63 }
64 REPORTER_ASSERT(r, paint.getShader()->unique());
65}