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 | |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 8 | #include "Test.h" |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 9 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 10 | #if SK_SUPPORT_GPU |
| 11 | |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 12 | #include "GrContextFactory.h" |
| 13 | #include "GrLayerCache.h" |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 14 | #include "GrRecordReplaceDraw.h" |
| 15 | #include "RecordTestUtils.h" |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 16 | #include "SkBBHFactory.h" |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 17 | #include "SkPictureRecorder.h" |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 18 | #include "SkRecordDraw.h" |
| 19 | #include "SkRecorder.h" |
| 20 | #include "SkUtils.h" |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 21 | |
| 22 | static const int kWidth = 100; |
| 23 | static const int kHeight = 100; |
| 24 | |
robertphillips | 783fe16 | 2015-01-07 07:28:41 -0800 | [diff] [blame] | 25 | class JustOneDraw : public SkPicture::AbortCallback { |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 26 | public: |
| 27 | JustOneDraw() : fCalls(0) {} |
| 28 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | bool abort() override { return fCalls++ > 0; } |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 30 | private: |
| 31 | int fCalls; |
| 32 | }; |
| 33 | |
| 34 | // Make sure the abort callback works |
| 35 | DEF_TEST(RecordReplaceDraw_Abort, r) { |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 36 | SkAutoTUnref<const SkPicture> pic; |
| 37 | |
| 38 | { |
| 39 | // Record two commands. |
| 40 | SkPictureRecorder recorder; |
| 41 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); |
| 42 | |
| 43 | canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)), SkPaint()); |
| 44 | canvas->clipRect(SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight))); |
| 45 | |
| 46 | pic.reset(recorder.endRecording()); |
| 47 | } |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 48 | |
| 49 | SkRecord rerecord; |
| 50 | SkRecorder canvas(&rerecord, kWidth, kHeight); |
| 51 | |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 52 | JustOneDraw callback; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 53 | GrRecordReplaceDraw(pic, &canvas, nullptr, SkMatrix::I(), &callback); |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 54 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 55 | switch (rerecord.count()) { |
| 56 | case 3: |
| 57 | assert_type<SkRecords::Save>(r, rerecord, 0); |
| 58 | assert_type<SkRecords::DrawRect>(r, rerecord, 1); |
| 59 | assert_type<SkRecords::Restore>(r, rerecord, 2); |
| 60 | break; |
| 61 | case 1: |
| 62 | assert_type<SkRecords::DrawRect>(r, rerecord, 0); |
| 63 | break; |
| 64 | default: |
| 65 | REPORTER_ASSERT(r, false); |
| 66 | } |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Make sure GrRecordReplaceDraw balances unbalanced saves |
| 70 | DEF_TEST(RecordReplaceDraw_Unbalanced, r) { |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 71 | SkAutoTUnref<const SkPicture> pic; |
| 72 | |
| 73 | { |
| 74 | SkPictureRecorder recorder; |
| 75 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); |
| 76 | |
| 77 | // We won't balance this, but GrRecordReplaceDraw will for us. |
| 78 | canvas->save(); |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 79 | canvas->scale(2, 2); |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 80 | pic.reset(recorder.endRecording()); |
| 81 | } |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 82 | |
| 83 | SkRecord rerecord; |
| 84 | SkRecorder canvas(&rerecord, kWidth, kHeight); |
| 85 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 86 | GrRecordReplaceDraw(pic, &canvas, nullptr, SkMatrix::I(), nullptr/*callback*/); |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 87 | |
reed | 7bc0b42 | 2014-12-11 07:30:58 -0800 | [diff] [blame] | 88 | // ensure rerecord is balanced (in this case by checking that the count is odd) |
| 89 | REPORTER_ASSERT(r, (rerecord.count() & 1) == 1); |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 90 | } |
| 91 | |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 92 | // Test out the layer replacement functionality with and w/o a BBH |
robertphillips | 07b41eb | 2015-11-04 04:31:06 -0800 | [diff] [blame] | 93 | void test_replacements(skiatest::Reporter* r, GrContext* context, bool doReplace) { |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 94 | SkAutoTUnref<const SkPicture> pic; |
| 95 | |
| 96 | { |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 97 | SkPictureRecorder recorder; |
robertphillips | 07b41eb | 2015-11-04 04:31:06 -0800 | [diff] [blame] | 98 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); |
mtklein | c88ceda | 2014-12-04 07:53:21 -0800 | [diff] [blame] | 99 | SkPaint paint; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 100 | canvas->saveLayer(nullptr, &paint); |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 101 | canvas->clear(SK_ColorRED); |
| 102 | canvas->restore(); |
| 103 | canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth / 2), SkIntToScalar(kHeight / 2)), |
| 104 | SkPaint()); |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 105 | pic.reset(recorder.endRecording()); |
| 106 | } |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 107 | |
robertphillips | 07b41eb | 2015-11-04 04:31:06 -0800 | [diff] [blame] | 108 | SkAutoTUnref<GrTexture> texture; |
mtklein | c88ceda | 2014-12-04 07:53:21 -0800 | [diff] [blame] | 109 | SkPaint paint; |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 110 | GrLayerCache* layerCache = context->getLayerCache(); |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 111 | |
robertphillips | 07b41eb | 2015-11-04 04:31:06 -0800 | [diff] [blame] | 112 | if (doReplace) { |
| 113 | int key[1] = { 0 }; |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 114 | |
robertphillips | 07b41eb | 2015-11-04 04:31:06 -0800 | [diff] [blame] | 115 | GrCachedLayer* layer = layerCache->findLayerOrCreate(pic->uniqueID(), 0, 2, |
| 116 | SkIRect::MakeWH(kWidth, kHeight), |
| 117 | SkIRect::MakeWH(kWidth, kHeight), |
| 118 | SkMatrix::I(), key, 1, &paint); |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 119 | |
robertphillips | 07b41eb | 2015-11-04 04:31:06 -0800 | [diff] [blame] | 120 | GrSurfaceDesc desc; |
| 121 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 122 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 123 | desc.fWidth = kWidth; |
| 124 | desc.fHeight = kHeight; |
| 125 | desc.fSampleCnt = 0; |
| 126 | |
| 127 | texture.reset(context->textureProvider()->createTexture(desc, false, nullptr, 0)); |
robertphillips | 60029a5 | 2015-11-09 13:51:06 -0800 | [diff] [blame] | 128 | layer->setTexture(texture, SkIRect::MakeWH(kWidth, kHeight), false); |
robertphillips | 07b41eb | 2015-11-04 04:31:06 -0800 | [diff] [blame] | 129 | } |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 130 | |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 131 | SkRecord rerecord; |
| 132 | SkRecorder canvas(&rerecord, kWidth, kHeight); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 133 | GrRecordReplaceDraw(pic, &canvas, layerCache, SkMatrix::I(), nullptr/*callback*/); |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 134 | |
robertphillips | 07b41eb | 2015-11-04 04:31:06 -0800 | [diff] [blame] | 135 | int numLayers = count_instances_of_type<SkRecords::SaveLayer>(rerecord); |
| 136 | if (doReplace) { |
| 137 | REPORTER_ASSERT(r, 0 == numLayers); |
| 138 | } else { |
| 139 | REPORTER_ASSERT(r, 1 == numLayers); |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 140 | } |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 141 | } |
| 142 | |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 143 | DEF_GPUTEST(RecordReplaceDraw, r, factory) { |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 144 | for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
| 145 | GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type); |
| 146 | if (!GrContextFactory::IsRenderingGLContext(glType)) { |
| 147 | continue; |
| 148 | } |
| 149 | GrContext* context = factory->get(glType); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 150 | if (nullptr == context) { |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 151 | continue; |
| 152 | } |
| 153 | |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 154 | test_replacements(r, context, false); |
robertphillips | 07b41eb | 2015-11-04 04:31:06 -0800 | [diff] [blame] | 155 | test_replacements(r, context, true); |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 158 | |
| 159 | #endif |