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" |
| 13 | |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 14 | #include "SkEmptyShader.h" |
| 15 | |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 16 | #define COUNT(T) + 1 |
| 17 | static const int kRecordTypes = SK_RECORD_TYPES(COUNT); |
| 18 | #undef COUNT |
| 19 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 20 | // Tallies the types of commands it sees into a histogram. |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 21 | class Tally { |
| 22 | public: |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 23 | Tally() { sk_bzero(&fHistogram, sizeof(fHistogram)); } |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 24 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 25 | 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.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 30 | |
| 31 | private: |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 32 | int fHistogram[kRecordTypes]; |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | DEF_TEST(Recorder, r) { |
| 36 | SkRecord record; |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 37 | SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, 1920, 1080); |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 38 | |
| 39 | recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint()); |
| 40 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 41 | Tally tally; |
| 42 | record.visit(tally); |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 43 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 44 | REPORTER_ASSERT(r, 1 == tally.count<SkRecords::DrawRect>()); |
commit-bot@chromium.org | b736962 | 2014-04-08 20:17:26 +0000 | [diff] [blame] | 45 | } |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 46 | |
| 47 | // Regression test for leaking refs held by optional arguments. |
| 48 | DEF_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.org | 12a0412 | 2014-04-15 18:00:57 +0000 | [diff] [blame^] | 53 | SkRect bounds = SkRect::MakeWH(320, 240); |
commit-bot@chromium.org | 653d518 | 2014-04-15 14:27:14 +0000 | [diff] [blame] | 54 | 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 | } |