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