commit-bot@chromium.org | c4b21e6 | 2014-04-11 18:33:31 +0000 | [diff] [blame] | 1 | /* |
| 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.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | |
| 10 | #include "SkRecord.h" |
| 11 | #include "SkRecorder.h" |
| 12 | #include "SkRecords.h" |
reed | e1085e0 | 2014-07-03 07:26:01 -0700 | [diff] [blame] | 13 | #include "SkShader.h" |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 14 | |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 15 | #define COUNT(T) + 1 |
| 16 | static const int kRecordTypes = SK_RECORD_TYPES(COUNT); |
| 17 | #undef COUNT |
| 18 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 19 | // Tallies the types of commands it sees into a histogram. |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 20 | class Tally { |
| 21 | public: |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 22 | Tally() { sk_bzero(&fHistogram, sizeof(fHistogram)); } |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 23 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 24 | 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.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 29 | |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 30 | void apply(const SkRecord& record) { |
| 31 | for (unsigned i = 0; i < record.count(); i++) { |
commit-bot@chromium.org | c71da1f | 2014-05-07 21:16:09 +0000 | [diff] [blame] | 32 | record.visit<void>(i, *this); |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 36 | private: |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 37 | int fHistogram[kRecordTypes]; |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | DEF_TEST(Recorder, r) { |
| 41 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 42 | SkRecorder recorder(&record, 1920, 1080); |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 43 | |
| 44 | recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint()); |
| 45 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 46 | Tally tally; |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 47 | tally.apply(record); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 48 | REPORTER_ASSERT(r, 1 == tally.count<SkRecords::DrawRect>()); |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 49 | } |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 50 | |
| 51 | // Regression test for leaking refs held by optional arguments. |
| 52 | DEF_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.org | 12a0412 | 2014-04-15 18:00:57 +0000 | [diff] [blame] | 57 | SkRect bounds = SkRect::MakeWH(320, 240); |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 58 | SkPaint paint; |
reed | e1085e0 | 2014-07-03 07:26:01 -0700 | [diff] [blame] | 59 | paint.setShader(SkShader::CreateEmptyShader())->unref(); |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 60 | |
| 61 | REPORTER_ASSERT(r, paint.getShader()->unique()); |
| 62 | { |
| 63 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 64 | SkRecorder recorder(&record, 1920, 1080); |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 65 | recorder.saveLayer(&bounds, &paint); |
| 66 | REPORTER_ASSERT(r, !paint.getShader()->unique()); |
| 67 | } |
| 68 | REPORTER_ASSERT(r, paint.getShader()->unique()); |
| 69 | } |