2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkPictureRecorder.h"
12 #include "SkRecorder.h"
13 #include "SkRecords.h"
17 static const int kRecordTypes = SK_RECORD_TYPES(COUNT);
20 // Tallies the types of commands it sees into a histogram.
23 Tally() { sk_bzero(&fHistogram, sizeof(fHistogram)); }
26 void operator()(const T&) { ++fHistogram[T::kType]; }
29 int count() const { return fHistogram[T::kType]; }
31 void apply(const SkRecord& record) {
32 for (unsigned i = 0; i < record.count(); i++) {
33 record.visit<void>(i, *this);
38 int fHistogram[kRecordTypes];
41 DEF_TEST(Recorder, r) {
43 SkRecorder recorder(&record, 1920, 1080);
45 recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint());
49 REPORTER_ASSERT(r, 1 == tally.count<SkRecords::DrawRect>());
52 // Regression test for leaking refs held by optional arguments.
53 DEF_TEST(Recorder_RefLeaking, r) {
54 // We use SaveLayer to test:
55 // - its SkRect argument is optional and SkRect is POD. Just testing that that works.
56 // - its SkPaint argument is optional and SkPaint is not POD. The bug was here.
58 SkRect bounds = SkRect::MakeWH(320, 240);
60 paint.setShader(SkShader::CreateEmptyShader())->unref();
62 REPORTER_ASSERT(r, paint.getShader()->unique());
65 SkRecorder recorder(&record, 1920, 1080);
66 recorder.saveLayer(&bounds, &paint);
67 REPORTER_ASSERT(r, !paint.getShader()->unique());
69 REPORTER_ASSERT(r, paint.getShader()->unique());
72 DEF_TEST(Recorder_RefPictures, r) {
73 SkAutoTUnref<SkPicture> pic;
77 SkCanvas* canvas = pr.beginRecording(100, 100);
78 canvas->drawColor(SK_ColorRED);
79 pic.reset(pr.endRecording());
81 REPORTER_ASSERT(r, pic->unique());
85 SkRecorder recorder(&record, 100, 100);
86 recorder.drawPicture(pic);
87 // the recorder should now also be an owner
88 REPORTER_ASSERT(r, !pic->unique());
90 // the recorder destructor should have released us (back to unique)
91 REPORTER_ASSERT(r, pic->unique());