robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [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 | |
| 8 | #if SK_SUPPORT_GPU |
| 9 | |
| 10 | #include "Test.h" |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 11 | |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 12 | #include "GrRecordReplaceDraw.h" |
| 13 | #include "RecordTestUtils.h" |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 14 | #include "SkBBHFactory.h" |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 15 | #include "SkPictureRecorder.h" |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 16 | #include "SkRecordDraw.h" |
| 17 | #include "SkRecorder.h" |
| 18 | #include "SkUtils.h" |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 19 | |
| 20 | static const int kWidth = 100; |
| 21 | static const int kHeight = 100; |
| 22 | |
| 23 | class JustOneDraw : public SkDrawPictureCallback { |
| 24 | public: |
| 25 | JustOneDraw() : fCalls(0) {} |
| 26 | |
| 27 | virtual bool abortDrawing() SK_OVERRIDE { return fCalls++ > 0; } |
| 28 | private: |
| 29 | int fCalls; |
| 30 | }; |
| 31 | |
| 32 | // Make sure the abort callback works |
| 33 | DEF_TEST(RecordReplaceDraw_Abort, r) { |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 34 | SkAutoTUnref<const SkPicture> pic; |
| 35 | |
| 36 | { |
| 37 | // Record two commands. |
| 38 | SkPictureRecorder recorder; |
| 39 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); |
| 40 | |
| 41 | canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)), SkPaint()); |
| 42 | canvas->clipRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight))); |
| 43 | |
| 44 | pic.reset(recorder.endRecording()); |
| 45 | } |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 46 | |
| 47 | SkRecord rerecord; |
| 48 | SkRecorder canvas(&rerecord, kWidth, kHeight); |
| 49 | |
| 50 | GrReplacements replacements; |
| 51 | JustOneDraw callback; |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 52 | GrRecordReplaceDraw(pic, &canvas, &replacements, SkMatrix::I(), &callback); |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 53 | |
| 54 | REPORTER_ASSERT(r, 3 == rerecord.count()); |
| 55 | assert_type<SkRecords::Save>(r, rerecord, 0); |
| 56 | assert_type<SkRecords::DrawRect>(r, rerecord, 1); |
| 57 | assert_type<SkRecords::Restore>(r, rerecord, 2); |
| 58 | } |
| 59 | |
| 60 | // Make sure GrRecordReplaceDraw balances unbalanced saves |
| 61 | DEF_TEST(RecordReplaceDraw_Unbalanced, r) { |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 62 | SkAutoTUnref<const SkPicture> pic; |
| 63 | |
| 64 | { |
| 65 | SkPictureRecorder recorder; |
| 66 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); |
| 67 | |
| 68 | // We won't balance this, but GrRecordReplaceDraw will for us. |
| 69 | canvas->save(); |
| 70 | |
| 71 | pic.reset(recorder.endRecording()); |
| 72 | } |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 73 | |
| 74 | SkRecord rerecord; |
| 75 | SkRecorder canvas(&rerecord, kWidth, kHeight); |
| 76 | |
| 77 | GrReplacements replacements; |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 78 | GrRecordReplaceDraw(pic, &canvas, &replacements, SkMatrix::I(), NULL/*callback*/); |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 79 | |
| 80 | REPORTER_ASSERT(r, 4 == rerecord.count()); |
| 81 | assert_type<SkRecords::Save>(r, rerecord, 0); |
| 82 | assert_type<SkRecords::Save>(r, rerecord, 1); |
| 83 | assert_type<SkRecords::Restore>(r, rerecord, 2); |
| 84 | assert_type<SkRecords::Restore>(r, rerecord, 3); |
| 85 | } |
| 86 | |
| 87 | static SkImage* make_image(SkColor color) { |
| 88 | const SkPMColor pmcolor = SkPreMultiplyColor(color); |
| 89 | const SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
| 90 | const size_t rowBytes = info.minRowBytes(); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 91 | const size_t size = rowBytes * info.height(); |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 92 | |
| 93 | SkAutoMalloc addr(size); |
| 94 | sk_memset32((SkPMColor*)addr.get(), pmcolor, SkToInt(size >> 2)); |
| 95 | |
| 96 | return SkImage::NewRasterCopy(info, addr.get(), rowBytes); |
| 97 | } |
| 98 | |
| 99 | // Test out the layer replacement functionality with and w/o a BBH |
| 100 | void test_replacements(skiatest::Reporter* r, bool useBBH) { |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 101 | SkAutoTUnref<const SkPicture> pic; |
| 102 | |
| 103 | { |
| 104 | SkRTreeFactory bbhFactory; |
| 105 | SkPictureRecorder recorder; |
mtklein | 4477c3c | 2014-10-27 10:27:10 -0700 | [diff] [blame] | 106 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight), |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 107 | useBBH ? &bbhFactory : NULL); |
| 108 | |
| 109 | SkAutoTDelete<SkPaint> paint(SkNEW(SkPaint)); |
| 110 | canvas->saveLayer(NULL, paint); |
| 111 | canvas->clear(SK_ColorRED); |
| 112 | canvas->restore(); |
| 113 | canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth / 2), SkIntToScalar(kHeight / 2)), |
| 114 | SkPaint()); |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 115 | pic.reset(recorder.endRecording()); |
| 116 | } |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 117 | |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame^] | 118 | int key[1] = { 0 }; |
| 119 | |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 120 | GrReplacements replacements; |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame^] | 121 | GrReplacements::ReplacementInfo* ri = replacements.newReplacement(pic->uniqueID(), |
| 122 | SkMatrix::I(), key, 1); |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 123 | ri->fStop = 2; |
| 124 | ri->fPos.set(0, 0); |
| 125 | ri->fImage = make_image(SK_ColorRED); |
robertphillips | a0537de | 2014-09-18 08:01:23 -0700 | [diff] [blame] | 126 | ri->fPaint = SkNEW(SkPaint); |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 127 | ri->fSrcRect = SkIRect::MakeWH(kWidth, kHeight); |
| 128 | |
| 129 | SkAutoTUnref<SkBBoxHierarchy> bbh; |
| 130 | |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 131 | SkRecord rerecord; |
| 132 | SkRecorder canvas(&rerecord, kWidth, kHeight); |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 133 | GrRecordReplaceDraw(pic, &canvas, &replacements, SkMatrix::I(), NULL/*callback*/); |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 134 | |
robertphillips | 46625e0 | 2014-09-08 11:37:59 -0700 | [diff] [blame] | 135 | REPORTER_ASSERT(r, 7 == rerecord.count()); |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 136 | assert_type<SkRecords::Save>(r, rerecord, 0); |
robertphillips | 46625e0 | 2014-09-08 11:37:59 -0700 | [diff] [blame] | 137 | assert_type<SkRecords::Save>(r, rerecord, 1); |
| 138 | assert_type<SkRecords::SetMatrix>(r, rerecord, 2); |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 139 | assert_type<SkRecords::DrawImageRect>(r, rerecord, 3); |
robertphillips | 46625e0 | 2014-09-08 11:37:59 -0700 | [diff] [blame] | 140 | assert_type<SkRecords::Restore>(r, rerecord, 4); |
| 141 | assert_type<SkRecords::DrawRect>(r, rerecord, 5); |
| 142 | assert_type<SkRecords::Restore>(r, rerecord, 6); |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | DEF_TEST(RecordReplaceDraw_Replace, r) { test_replacements(r, false); } |
| 146 | DEF_TEST(RecordReplaceDraw_ReplaceWithBBH, r) { test_replacements(r, true); } |
| 147 | |
| 148 | #endif |