| 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 #include "Test.h" | 8 #include "Test.h" |
| 9 | 9 |
| 10 #if SK_SUPPORT_GPU | 10 #if SK_SUPPORT_GPU |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 public: | 26 public: |
| 27 JustOneDraw() : fCalls(0) {} | 27 JustOneDraw() : fCalls(0) {} |
| 28 | 28 |
| 29 bool abort() override { return fCalls++ > 0; } | 29 bool abort() override { return fCalls++ > 0; } |
| 30 private: | 30 private: |
| 31 int fCalls; | 31 int fCalls; |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 // Make sure the abort callback works | 34 // Make sure the abort callback works |
| 35 DEF_TEST(RecordReplaceDraw_Abort, r) { | 35 DEF_TEST(RecordReplaceDraw_Abort, r) { |
| 36 SkAutoTUnref<const SkPicture> pic; | 36 sk_sp<SkPicture> pic; |
| 37 | 37 |
| 38 { | 38 { |
| 39 // Record two commands. | 39 // Record two commands. |
| 40 SkPictureRecorder recorder; | 40 SkPictureRecorder recorder; |
| 41 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntT
oScalar(kHeight)); | 41 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntT
oScalar(kHeight)); |
| 42 | 42 |
| 43 canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHe
ight)), SkPaint()); | 43 canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHe
ight)), SkPaint()); |
| 44 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHe
ight))); | 44 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHe
ight))); |
| 45 | 45 |
| 46 pic.reset(recorder.endRecording()); | 46 pic = recorder.finishRecordingAsPicture(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 SkRecord rerecord; | 49 SkRecord rerecord; |
| 50 SkRecorder canvas(&rerecord, kWidth, kHeight); | 50 SkRecorder canvas(&rerecord, kWidth, kHeight); |
| 51 | 51 |
| 52 JustOneDraw callback; | 52 JustOneDraw callback; |
| 53 GrRecordReplaceDraw(pic, &canvas, nullptr, SkMatrix::I(), &callback); | 53 GrRecordReplaceDraw(pic.get(), &canvas, nullptr, SkMatrix::I(), &callback); |
| 54 | 54 |
| 55 switch (rerecord.count()) { | 55 switch (rerecord.count()) { |
| 56 case 3: | 56 case 3: |
| 57 assert_type<SkRecords::Save>(r, rerecord, 0); | 57 assert_type<SkRecords::Save>(r, rerecord, 0); |
| 58 assert_type<SkRecords::DrawRect>(r, rerecord, 1); | 58 assert_type<SkRecords::DrawRect>(r, rerecord, 1); |
| 59 assert_type<SkRecords::Restore>(r, rerecord, 2); | 59 assert_type<SkRecords::Restore>(r, rerecord, 2); |
| 60 break; | 60 break; |
| 61 case 1: | 61 case 1: |
| 62 assert_type<SkRecords::DrawRect>(r, rerecord, 0); | 62 assert_type<SkRecords::DrawRect>(r, rerecord, 0); |
| 63 break; | 63 break; |
| 64 default: | 64 default: |
| 65 REPORTER_ASSERT(r, false); | 65 REPORTER_ASSERT(r, false); |
| 66 } | 66 } |
| 67 } | 67 } |
| 68 | 68 |
| 69 // Make sure GrRecordReplaceDraw balances unbalanced saves | 69 // Make sure GrRecordReplaceDraw balances unbalanced saves |
| 70 DEF_TEST(RecordReplaceDraw_Unbalanced, r) { | 70 DEF_TEST(RecordReplaceDraw_Unbalanced, r) { |
| 71 SkAutoTUnref<const SkPicture> pic; | 71 sk_sp<SkPicture> pic; |
| 72 | 72 |
| 73 { | 73 { |
| 74 SkPictureRecorder recorder; | 74 SkPictureRecorder recorder; |
| 75 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntT
oScalar(kHeight)); | 75 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntT
oScalar(kHeight)); |
| 76 | 76 |
| 77 // We won't balance this, but GrRecordReplaceDraw will for us. | 77 // We won't balance this, but GrRecordReplaceDraw will for us. |
| 78 canvas->save(); | 78 canvas->save(); |
| 79 canvas->scale(2, 2); | 79 canvas->scale(2, 2); |
| 80 pic.reset(recorder.endRecording()); | 80 pic = recorder.finishRecordingAsPicture(); |
| 81 } | 81 } |
| 82 | 82 |
| 83 SkRecord rerecord; | 83 SkRecord rerecord; |
| 84 SkRecorder canvas(&rerecord, kWidth, kHeight); | 84 SkRecorder canvas(&rerecord, kWidth, kHeight); |
| 85 | 85 |
| 86 GrRecordReplaceDraw(pic, &canvas, nullptr, SkMatrix::I(), nullptr/*callback*
/); | 86 GrRecordReplaceDraw(pic.get(), &canvas, nullptr, SkMatrix::I(), nullptr/*cal
lback*/); |
| 87 | 87 |
| 88 // ensure rerecord is balanced (in this case by checking that the count is o
dd) | 88 // ensure rerecord is balanced (in this case by checking that the count is o
dd) |
| 89 REPORTER_ASSERT(r, (rerecord.count() & 1) == 1); | 89 REPORTER_ASSERT(r, (rerecord.count() & 1) == 1); |
| 90 } | 90 } |
| 91 | 91 |
| 92 // Test out the layer replacement functionality with and w/o a BBH | 92 // Test out the layer replacement functionality with and w/o a BBH |
| 93 void test_replacements(skiatest::Reporter* r, GrContext* context, bool doReplace
) { | 93 void test_replacements(skiatest::Reporter* r, GrContext* context, bool doReplace
) { |
| 94 SkAutoTUnref<const SkPicture> pic; | 94 sk_sp<SkPicture> pic; |
| 95 | 95 |
| 96 { | 96 { |
| 97 SkPictureRecorder recorder; | 97 SkPictureRecorder recorder; |
| 98 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntT
oScalar(kHeight)); | 98 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntT
oScalar(kHeight)); |
| 99 SkPaint paint; | 99 SkPaint paint; |
| 100 canvas->saveLayer(nullptr, &paint); | 100 canvas->saveLayer(nullptr, &paint); |
| 101 canvas->clear(SK_ColorRED); | 101 canvas->clear(SK_ColorRED); |
| 102 canvas->restore(); | 102 canvas->restore(); |
| 103 canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth / 2), SkIntToScalar
(kHeight / 2)), | 103 canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth / 2), SkIntToScalar
(kHeight / 2)), |
| 104 SkPaint()); | 104 SkPaint()); |
| 105 pic.reset(recorder.endRecording()); | 105 pic = recorder.finishRecordingAsPicture(); |
| 106 } | 106 } |
| 107 | 107 |
| 108 SkAutoTUnref<GrTexture> texture; | 108 SkAutoTUnref<GrTexture> texture; |
| 109 SkPaint paint; | 109 SkPaint paint; |
| 110 GrLayerCache* layerCache = context->getLayerCache(); | 110 GrLayerCache* layerCache = context->getLayerCache(); |
| 111 | 111 |
| 112 if (doReplace) { | 112 if (doReplace) { |
| 113 int key[1] = { 0 }; | 113 int key[1] = { 0 }; |
| 114 | 114 |
| 115 GrCachedLayer* layer = layerCache->findLayerOrCreate(pic->uniqueID(), 0,
2, | 115 GrCachedLayer* layer = layerCache->findLayerOrCreate(pic->uniqueID(), 0,
2, |
| 116 SkIRect::MakeWH(kWi
dth, kHeight), | 116 SkIRect::MakeWH(kWi
dth, kHeight), |
| 117 SkIRect::MakeWH(kWi
dth, kHeight), | 117 SkIRect::MakeWH(kWi
dth, kHeight), |
| 118 SkMatrix::I(), key,
1, &paint); | 118 SkMatrix::I(), key,
1, &paint); |
| 119 | 119 |
| 120 GrSurfaceDesc desc; | 120 GrSurfaceDesc desc; |
| 121 desc.fConfig = kSkia8888_GrPixelConfig; | 121 desc.fConfig = kSkia8888_GrPixelConfig; |
| 122 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 122 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 123 desc.fWidth = kWidth; | 123 desc.fWidth = kWidth; |
| 124 desc.fHeight = kHeight; | 124 desc.fHeight = kHeight; |
| 125 desc.fSampleCnt = 0; | 125 desc.fSampleCnt = 0; |
| 126 | 126 |
| 127 texture.reset(context->textureProvider()->createTexture( | 127 texture.reset(context->textureProvider()->createTexture( |
| 128 desc, SkBudgeted::kNo, nullptr, 0)); | 128 desc, SkBudgeted::kNo, nullptr, 0)); |
| 129 layer->setTexture(texture, SkIRect::MakeWH(kWidth, kHeight), false); | 129 layer->setTexture(texture, SkIRect::MakeWH(kWidth, kHeight), false); |
| 130 } | 130 } |
| 131 | 131 |
| 132 SkRecord rerecord; | 132 SkRecord rerecord; |
| 133 SkRecorder canvas(&rerecord, kWidth, kHeight); | 133 SkRecorder canvas(&rerecord, kWidth, kHeight); |
| 134 GrRecordReplaceDraw(pic, &canvas, layerCache, SkMatrix::I(), nullptr/*callba
ck*/); | 134 GrRecordReplaceDraw(pic.get(), &canvas, layerCache, SkMatrix::I(), nullptr/*
callback*/); |
| 135 | 135 |
| 136 int numLayers = count_instances_of_type<SkRecords::SaveLayer>(rerecord); | 136 int numLayers = count_instances_of_type<SkRecords::SaveLayer>(rerecord); |
| 137 if (doReplace) { | 137 if (doReplace) { |
| 138 REPORTER_ASSERT(r, 0 == numLayers); | 138 REPORTER_ASSERT(r, 0 == numLayers); |
| 139 } else { | 139 } else { |
| 140 REPORTER_ASSERT(r, 1 == numLayers); | 140 REPORTER_ASSERT(r, 1 == numLayers); |
| 141 } | 141 } |
| 142 } | 142 } |
| 143 | 143 |
| 144 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RecordReplaceDraw, r, context) { | 144 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RecordReplaceDraw, r, context) { |
| 145 test_replacements(r, context, false); | 145 test_replacements(r, context, false); |
| 146 test_replacements(r, context, true); | 146 test_replacements(r, context, true); |
| 147 } | 147 } |
| 148 | 148 |
| 149 #endif | 149 #endif |
| OLD | NEW |