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; |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 53 | GrRecordReplaceDraw(pic, &canvas, NULL, 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 | |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 86 | GrRecordReplaceDraw(pic, &canvas, NULL, SkMatrix::I(), NULL/*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 | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 93 | void test_replacements(skiatest::Reporter* r, GrContext* context, bool useBBH) { |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 94 | SkAutoTUnref<const SkPicture> pic; |
| 95 | |
| 96 | { |
| 97 | SkRTreeFactory bbhFactory; |
| 98 | SkPictureRecorder recorder; |
mtklein | 4477c3c | 2014-10-27 10:27:10 -0700 | [diff] [blame] | 99 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kWidth), SkIntToScalar(kHeight), |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 100 | useBBH ? &bbhFactory : NULL); |
| 101 | |
mtklein | c88ceda | 2014-12-04 07:53:21 -0800 | [diff] [blame] | 102 | SkPaint paint; |
| 103 | canvas->saveLayer(NULL, &paint); |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 104 | canvas->clear(SK_ColorRED); |
| 105 | canvas->restore(); |
| 106 | canvas->drawRect(SkRect::MakeWH(SkIntToScalar(kWidth / 2), SkIntToScalar(kHeight / 2)), |
| 107 | SkPaint()); |
robertphillips | ee6631e | 2014-09-29 05:32:49 -0700 | [diff] [blame] | 108 | pic.reset(recorder.endRecording()); |
| 109 | } |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 110 | |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 111 | unsigned key[1] = { 0 }; |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 112 | |
mtklein | c88ceda | 2014-12-04 07:53:21 -0800 | [diff] [blame] | 113 | SkPaint paint; |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 114 | GrLayerCache* layerCache = context->getLayerCache(); |
mtklein | c88ceda | 2014-12-04 07:53:21 -0800 | [diff] [blame] | 115 | GrCachedLayer* layer = layerCache->findLayerOrCreate(pic->uniqueID(), 0, 2, |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 116 | SkIRect::MakeWH(kWidth, kHeight), |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 117 | SkIRect::MakeWH(kWidth, kHeight), |
mtklein | c88ceda | 2014-12-04 07:53:21 -0800 | [diff] [blame] | 118 | SkMatrix::I(), key, 1, &paint); |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 119 | |
| 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 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 127 | SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc, |
| 128 | false, NULL, 0)); |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 129 | layer->setTexture(texture, SkIRect::MakeWH(kWidth, kHeight)); |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 130 | |
| 131 | SkAutoTUnref<SkBBoxHierarchy> bbh; |
| 132 | |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 133 | SkRecord rerecord; |
| 134 | SkRecorder canvas(&rerecord, kWidth, kHeight); |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 135 | GrRecordReplaceDraw(pic, &canvas, layerCache, SkMatrix::I(), NULL/*callback*/); |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 136 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 137 | int recount = rerecord.count(); |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 138 | REPORTER_ASSERT(r, 2 == recount || 4 == recount); |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 139 | |
| 140 | int index = 0; |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 141 | if (4 == recount) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 142 | assert_type<SkRecords::Save>(r, rerecord, 0); |
| 143 | index += 1; |
| 144 | } |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 145 | assert_type<SkRecords::DrawSprite>(r, rerecord, index + 0); |
| 146 | assert_type<SkRecords::DrawRect>(r, rerecord, index + 1); |
| 147 | if (4 == recount) { |
| 148 | assert_type<SkRecords::Restore>(r, rerecord, 3); |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 149 | } |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 150 | } |
| 151 | |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 152 | DEF_GPUTEST(RecordReplaceDraw, r, factory) { |
| 153 | for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
| 154 | GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type); |
| 155 | if (!GrContextFactory::IsRenderingGLContext(glType)) { |
| 156 | continue; |
| 157 | } |
| 158 | GrContext* context = factory->get(glType); |
| 159 | if (NULL == context) { |
| 160 | continue; |
| 161 | } |
| 162 | |
| 163 | test_replacements(r, context, true); |
| 164 | test_replacements(r, context, false); |
| 165 | } |
| 166 | } |
robertphillips | d982eb2 | 2014-09-03 11:04:30 -0700 | [diff] [blame] | 167 | |
| 168 | #endif |