Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 1 | /* |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include <BakedOpState.h> |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 20 | #include <DeferredLayerUpdater.h> |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 21 | #include <FrameBuilder.h> |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 22 | #include <LayerUpdateQueue.h> |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 23 | #include <RecordedOp.h> |
| 24 | #include <RecordingCanvas.h> |
Chris Craik | 8160f20 | 2015-12-02 14:50:25 -0800 | [diff] [blame] | 25 | #include <tests/common/TestUtils.h> |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 26 | |
| 27 | #include <unordered_map> |
| 28 | |
| 29 | namespace android { |
| 30 | namespace uirenderer { |
| 31 | |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 32 | const LayerUpdateQueue sEmptyLayerUpdateQueue; |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 33 | const FrameBuilder::LightGeometry sLightGeometry = { {100, 100, 100}, 50}; |
| 34 | |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 35 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 36 | /** |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 37 | * Virtual class implemented by each test to redirect static operation / state transitions to |
| 38 | * virtual methods. |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 39 | * |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 40 | * Virtual dispatch allows for default behaviors to be specified (very common case in below tests), |
| 41 | * and allows Renderer vs Dispatching behavior to be merged. |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 42 | * |
| 43 | * onXXXOp methods fail by default - tests should override ops they expect |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 44 | * startRepaintLayer fails by default - tests should override if expected |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 45 | * startFrame/endFrame do nothing by default - tests should override to intercept |
| 46 | */ |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 47 | class TestRendererBase { |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 48 | public: |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 49 | virtual ~TestRendererBase() {} |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 50 | virtual OffscreenBuffer* startTemporaryLayer(uint32_t, uint32_t) { |
Chris Craik | a6ac95e | 2015-11-02 18:06:59 -0800 | [diff] [blame] | 51 | ADD_FAILURE() << "Layer creation not expected in this test"; |
| 52 | return nullptr; |
| 53 | } |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 54 | virtual void startRepaintLayer(OffscreenBuffer*, const Rect& repaintRect) { |
Chris Craik | a6ac95e | 2015-11-02 18:06:59 -0800 | [diff] [blame] | 55 | ADD_FAILURE() << "Layer repaint not expected in this test"; |
| 56 | } |
| 57 | virtual void endLayer() { |
| 58 | ADD_FAILURE() << "Layer updates not expected in this test"; |
| 59 | } |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 60 | virtual void startFrame(uint32_t width, uint32_t height, const Rect& repaintRect) {} |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 61 | virtual void endFrame(const Rect& repaintRect) {} |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 62 | |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 63 | // define virtual defaults for single draw methods |
| 64 | #define X(Type) \ |
Chris Craik | a6ac95e | 2015-11-02 18:06:59 -0800 | [diff] [blame] | 65 | virtual void on##Type(const Type&, const BakedOpState&) { \ |
| 66 | ADD_FAILURE() << #Type " not expected in this test"; \ |
| 67 | } |
Chris Craik | 7cbf63d | 2016-01-06 13:46:52 -0800 | [diff] [blame] | 68 | MAP_RENDERABLE_OPS(X) |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 69 | #undef X |
| 70 | |
| 71 | // define virtual defaults for merged draw methods |
| 72 | #define X(Type) \ |
| 73 | virtual void onMerged##Type##s(const MergedBakedOpList& opList) { \ |
| 74 | ADD_FAILURE() << "Merged " #Type "s not expected in this test"; \ |
| 75 | } |
Chris Craik | 7cbf63d | 2016-01-06 13:46:52 -0800 | [diff] [blame] | 76 | MAP_MERGEABLE_OPS(X) |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 77 | #undef X |
| 78 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 79 | int getIndex() { return mIndex; } |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 80 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 81 | protected: |
| 82 | int mIndex = 0; |
| 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * Dispatches all static methods to similar formed methods on renderer, which fail by default but |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 87 | * are overridden by subclasses per test. |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 88 | */ |
| 89 | class TestDispatcher { |
| 90 | public: |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 91 | // define single op methods, which redirect to TestRendererBase |
| 92 | #define X(Type) \ |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 93 | static void on##Type(TestRendererBase& renderer, const Type& op, const BakedOpState& state) { \ |
| 94 | renderer.on##Type(op, state); \ |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 95 | } |
Chris Craik | 7cbf63d | 2016-01-06 13:46:52 -0800 | [diff] [blame] | 96 | MAP_RENDERABLE_OPS(X); |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 97 | #undef X |
| 98 | |
| 99 | // define merged op methods, which redirect to TestRendererBase |
| 100 | #define X(Type) \ |
| 101 | static void onMerged##Type##s(TestRendererBase& renderer, const MergedBakedOpList& opList) { \ |
| 102 | renderer.onMerged##Type##s(opList); \ |
| 103 | } |
Chris Craik | 7cbf63d | 2016-01-06 13:46:52 -0800 | [diff] [blame] | 104 | MAP_MERGEABLE_OPS(X); |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 105 | #undef X |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 106 | }; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 107 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 108 | class FailRenderer : public TestRendererBase {}; |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 109 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 110 | RENDERTHREAD_TEST(FrameBuilder, simple) { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 111 | class SimpleTestRenderer : public TestRendererBase { |
| 112 | public: |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 113 | void startFrame(uint32_t width, uint32_t height, const Rect& repaintRect) override { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 114 | EXPECT_EQ(0, mIndex++); |
| 115 | EXPECT_EQ(100u, width); |
| 116 | EXPECT_EQ(200u, height); |
| 117 | } |
| 118 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 119 | EXPECT_EQ(1, mIndex++); |
| 120 | } |
| 121 | void onBitmapOp(const BitmapOp& op, const BakedOpState& state) override { |
| 122 | EXPECT_EQ(2, mIndex++); |
| 123 | } |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 124 | void endFrame(const Rect& repaintRect) override { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 125 | EXPECT_EQ(3, mIndex++); |
| 126 | } |
| 127 | }; |
| 128 | |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 129 | auto node = TestUtils::createNode(0, 0, 100, 200, |
| 130 | [](RenderProperties& props, RecordingCanvas& canvas) { |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 131 | SkBitmap bitmap = TestUtils::createSkBitmap(25, 25); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 132 | canvas.drawRect(0, 0, 100, 200, SkPaint()); |
| 133 | canvas.drawBitmap(bitmap, 10, 10, nullptr); |
| 134 | }); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 135 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(100, 200), 100, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 136 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 137 | SimpleTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 138 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 139 | EXPECT_EQ(4, renderer.getIndex()); // 2 ops + start + end |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 142 | RENDERTHREAD_TEST(FrameBuilder, simpleStroke) { |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 143 | class SimpleStrokeTestRenderer : public TestRendererBase { |
| 144 | public: |
| 145 | void onPointsOp(const PointsOp& op, const BakedOpState& state) override { |
| 146 | EXPECT_EQ(0, mIndex++); |
| 147 | // even though initial bounds are empty... |
| 148 | EXPECT_TRUE(op.unmappedBounds.isEmpty()) |
| 149 | << "initial bounds should be empty, since they're unstroked"; |
| 150 | EXPECT_EQ(Rect(45, 45, 55, 55), state.computedState.clippedBounds) |
| 151 | << "final bounds should account for stroke"; |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | auto node = TestUtils::createNode(0, 0, 100, 200, |
| 156 | [](RenderProperties& props, RecordingCanvas& canvas) { |
| 157 | SkPaint strokedPaint; |
| 158 | strokedPaint.setStrokeWidth(10); |
| 159 | canvas.drawPoint(50, 50, strokedPaint); |
| 160 | }); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 161 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(100, 200), 100, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 162 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 163 | SimpleStrokeTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 164 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 165 | EXPECT_EQ(1, renderer.getIndex()); |
| 166 | } |
| 167 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 168 | RENDERTHREAD_TEST(FrameBuilder, simpleRejection) { |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 169 | auto node = TestUtils::createNode(0, 0, 200, 200, |
| 170 | [](RenderProperties& props, RecordingCanvas& canvas) { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 171 | canvas.save(SaveFlags::MatrixClip); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 172 | canvas.clipRect(200, 200, 400, 400, SkRegion::kIntersect_Op); // intersection should be empty |
| 173 | canvas.drawRect(0, 0, 400, 400, SkPaint()); |
| 174 | canvas.restore(); |
| 175 | }); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 176 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(200, 200), 200, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 177 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 178 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 179 | FailRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 180 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 183 | RENDERTHREAD_TEST(FrameBuilder, simpleBatching) { |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 184 | const int LOOPS = 5; |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 185 | class SimpleBatchingTestRenderer : public TestRendererBase { |
| 186 | public: |
| 187 | void onBitmapOp(const BitmapOp& op, const BakedOpState& state) override { |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 188 | EXPECT_TRUE(mIndex++ >= LOOPS) << "Bitmaps should be above all rects"; |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 189 | } |
| 190 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 191 | EXPECT_TRUE(mIndex++ < LOOPS) << "Rects should be below all bitmaps"; |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 192 | } |
| 193 | }; |
| 194 | |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 195 | auto node = TestUtils::createNode(0, 0, 200, 200, |
| 196 | [](RenderProperties& props, RecordingCanvas& canvas) { |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 197 | SkBitmap bitmap = TestUtils::createSkBitmap(10, 10, |
| 198 | kAlpha_8_SkColorType); // Disable merging by using alpha 8 bitmap |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 199 | |
| 200 | // Alternate between drawing rects and bitmaps, with bitmaps overlapping rects. |
| 201 | // Rects don't overlap bitmaps, so bitmaps should be brought to front as a group. |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 202 | canvas.save(SaveFlags::MatrixClip); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 203 | for (int i = 0; i < LOOPS; i++) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 204 | canvas.translate(0, 10); |
| 205 | canvas.drawRect(0, 0, 10, 10, SkPaint()); |
| 206 | canvas.drawBitmap(bitmap, 5, 0, nullptr); |
| 207 | } |
| 208 | canvas.restore(); |
| 209 | }); |
| 210 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 211 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(200, 200), 200, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 212 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 213 | SimpleBatchingTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 214 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 215 | EXPECT_EQ(2 * LOOPS, renderer.getIndex()) |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 216 | << "Expect number of ops = 2 * loop count"; |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 219 | RENDERTHREAD_TEST(FrameBuilder, clippedMerging) { |
Chris Craik | 93e53e0 | 2015-12-17 18:42:44 -0800 | [diff] [blame] | 220 | class ClippedMergingTestRenderer : public TestRendererBase { |
| 221 | public: |
| 222 | void onMergedBitmapOps(const MergedBakedOpList& opList) override { |
| 223 | EXPECT_EQ(0, mIndex); |
| 224 | mIndex += opList.count; |
| 225 | EXPECT_EQ(4u, opList.count); |
| 226 | EXPECT_EQ(Rect(10, 10, 90, 90), opList.clip); |
| 227 | EXPECT_EQ(OpClipSideFlags::Left | OpClipSideFlags::Top | OpClipSideFlags::Right, |
| 228 | opList.clipSideFlags); |
| 229 | } |
| 230 | }; |
| 231 | auto node = TestUtils::createNode(0, 0, 100, 100, |
| 232 | [](RenderProperties& props, TestCanvas& canvas) { |
| 233 | SkBitmap bitmap = TestUtils::createSkBitmap(20, 20); |
| 234 | |
| 235 | // left side clipped (to inset left half) |
| 236 | canvas.clipRect(10, 0, 50, 100, SkRegion::kReplace_Op); |
| 237 | canvas.drawBitmap(bitmap, 0, 40, nullptr); |
| 238 | |
| 239 | // top side clipped (to inset top half) |
| 240 | canvas.clipRect(0, 10, 100, 50, SkRegion::kReplace_Op); |
| 241 | canvas.drawBitmap(bitmap, 40, 0, nullptr); |
| 242 | |
| 243 | // right side clipped (to inset right half) |
| 244 | canvas.clipRect(50, 0, 90, 100, SkRegion::kReplace_Op); |
| 245 | canvas.drawBitmap(bitmap, 80, 40, nullptr); |
| 246 | |
| 247 | // bottom not clipped, just abutting (inset bottom half) |
| 248 | canvas.clipRect(0, 50, 100, 90, SkRegion::kReplace_Op); |
| 249 | canvas.drawBitmap(bitmap, 40, 70, nullptr); |
| 250 | }); |
| 251 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 252 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(100, 100), 100, 100, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 253 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | 93e53e0 | 2015-12-17 18:42:44 -0800 | [diff] [blame] | 254 | ClippedMergingTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 255 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | 93e53e0 | 2015-12-17 18:42:44 -0800 | [diff] [blame] | 256 | EXPECT_EQ(4, renderer.getIndex()); |
| 257 | } |
| 258 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 259 | RENDERTHREAD_TEST(FrameBuilder, textMerging) { |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 260 | class TextMergingTestRenderer : public TestRendererBase { |
| 261 | public: |
| 262 | void onMergedTextOps(const MergedBakedOpList& opList) override { |
| 263 | EXPECT_EQ(0, mIndex); |
| 264 | mIndex += opList.count; |
| 265 | EXPECT_EQ(2u, opList.count); |
| 266 | EXPECT_EQ(OpClipSideFlags::Top, opList.clipSideFlags); |
| 267 | EXPECT_EQ(OpClipSideFlags::Top, opList.states[0]->computedState.clipSideFlags); |
| 268 | EXPECT_EQ(OpClipSideFlags::None, opList.states[1]->computedState.clipSideFlags); |
| 269 | } |
| 270 | }; |
| 271 | auto node = TestUtils::createNode(0, 0, 400, 400, |
| 272 | [](RenderProperties& props, TestCanvas& canvas) { |
| 273 | SkPaint paint; |
| 274 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 275 | paint.setAntiAlias(true); |
| 276 | paint.setTextSize(50); |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 277 | TestUtils::drawUtf8ToCanvas(&canvas, "Test string1", paint, 100, 0); // will be top clipped |
| 278 | TestUtils::drawUtf8ToCanvas(&canvas, "Test string1", paint, 100, 100); // not clipped |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 279 | }); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 280 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(400, 400), 400, 400, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 281 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 282 | TextMergingTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 283 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 284 | EXPECT_EQ(2, renderer.getIndex()) << "Expect 2 ops"; |
| 285 | } |
| 286 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 287 | RENDERTHREAD_TEST(FrameBuilder, textStrikethrough) { |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 288 | const int LOOPS = 5; |
| 289 | class TextStrikethroughTestRenderer : public TestRendererBase { |
| 290 | public: |
| 291 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 292 | EXPECT_TRUE(mIndex++ >= LOOPS) << "Strikethrough rects should be above all text"; |
| 293 | } |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 294 | void onMergedTextOps(const MergedBakedOpList& opList) override { |
| 295 | EXPECT_EQ(0, mIndex); |
| 296 | mIndex += opList.count; |
| 297 | EXPECT_EQ(5u, opList.count); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 298 | } |
| 299 | }; |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 300 | auto node = TestUtils::createNode(0, 0, 200, 2000, |
| 301 | [](RenderProperties& props, RecordingCanvas& canvas) { |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 302 | SkPaint textPaint; |
| 303 | textPaint.setAntiAlias(true); |
| 304 | textPaint.setTextSize(20); |
| 305 | textPaint.setStrikeThruText(true); |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 306 | textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 307 | for (int i = 0; i < LOOPS; i++) { |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 308 | TestUtils::drawUtf8ToCanvas(&canvas, "test text", textPaint, 10, 100 * (i + 1)); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 309 | } |
| 310 | }); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 311 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(200, 2000), 200, 2000, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 312 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 313 | TextStrikethroughTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 314 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 315 | EXPECT_EQ(2 * LOOPS, renderer.getIndex()) |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 316 | << "Expect number of ops = 2 * loop count"; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Chris Craik | 7c02cab | 2016-03-16 17:15:12 -0700 | [diff] [blame] | 319 | static auto styles = { |
| 320 | SkPaint::kFill_Style, SkPaint::kStroke_Style, SkPaint::kStrokeAndFill_Style }; |
| 321 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 322 | RENDERTHREAD_TEST(FrameBuilder, textStyle) { |
Chris Craik | 7c02cab | 2016-03-16 17:15:12 -0700 | [diff] [blame] | 323 | class TextStyleTestRenderer : public TestRendererBase { |
| 324 | public: |
| 325 | void onMergedTextOps(const MergedBakedOpList& opList) override { |
| 326 | ASSERT_EQ(0, mIndex); |
| 327 | ASSERT_EQ(3u, opList.count); |
| 328 | mIndex += opList.count; |
| 329 | |
| 330 | int index = 0; |
| 331 | for (auto style : styles) { |
| 332 | auto state = opList.states[index++]; |
| 333 | ASSERT_EQ(style, state->op->paint->getStyle()) |
| 334 | << "Remainder of validation relies upon stable merged order"; |
| 335 | ASSERT_EQ(0, state->computedState.clipSideFlags) |
| 336 | << "Clipped bounds validation requires unclipped ops"; |
| 337 | } |
| 338 | |
| 339 | Rect fill = opList.states[0]->computedState.clippedBounds; |
| 340 | Rect stroke = opList.states[1]->computedState.clippedBounds; |
| 341 | EXPECT_EQ(stroke, opList.states[2]->computedState.clippedBounds) |
| 342 | << "Stroke+Fill should be same as stroke"; |
| 343 | |
| 344 | EXPECT_TRUE(stroke.contains(fill)); |
| 345 | EXPECT_FALSE(fill.contains(stroke)); |
| 346 | |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 347 | // outset by half the stroke width |
Chris Craik | 7c02cab | 2016-03-16 17:15:12 -0700 | [diff] [blame] | 348 | Rect outsetFill(fill); |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 349 | outsetFill.outset(5); |
Chris Craik | 7c02cab | 2016-03-16 17:15:12 -0700 | [diff] [blame] | 350 | EXPECT_EQ(stroke, outsetFill); |
| 351 | } |
| 352 | }; |
| 353 | auto node = TestUtils::createNode(0, 0, 400, 400, |
| 354 | [](RenderProperties& props, TestCanvas& canvas) { |
| 355 | SkPaint paint; |
| 356 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 357 | paint.setAntiAlias(true); |
| 358 | paint.setTextSize(50); |
| 359 | paint.setStrokeWidth(10); |
| 360 | |
| 361 | // draw 3 copies of the same text overlapping, each with a different style. |
| 362 | // They'll get merged, but with |
| 363 | for (auto style : styles) { |
| 364 | paint.setStyle(style); |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 365 | TestUtils::drawUtf8ToCanvas(&canvas, "Test string1", paint, 100, 100); |
Chris Craik | 7c02cab | 2016-03-16 17:15:12 -0700 | [diff] [blame] | 366 | } |
| 367 | }); |
| 368 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(400, 400), 400, 400, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 369 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | 7c02cab | 2016-03-16 17:15:12 -0700 | [diff] [blame] | 370 | TextStyleTestRenderer renderer; |
| 371 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
| 372 | EXPECT_EQ(3, renderer.getIndex()) << "Expect 3 ops"; |
| 373 | } |
| 374 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 375 | RENDERTHREAD_TEST(FrameBuilder, textureLayer) { |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 376 | class TextureLayerTestRenderer : public TestRendererBase { |
| 377 | public: |
| 378 | void onTextureLayerOp(const TextureLayerOp& op, const BakedOpState& state) override { |
| 379 | EXPECT_EQ(0, mIndex++); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 380 | EXPECT_EQ(Rect(50, 50, 150, 150), state.computedState.clipRect()); |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 381 | EXPECT_EQ(Rect(50, 50, 105, 105), state.computedState.clippedBounds); |
| 382 | |
| 383 | Matrix4 expected; |
| 384 | expected.loadTranslate(5, 5, 0); |
| 385 | EXPECT_MATRIX_APPROX_EQ(expected, state.computedState.transform); |
| 386 | } |
| 387 | }; |
| 388 | |
| 389 | auto layerUpdater = TestUtils::createTextureLayerUpdater(renderThread, 100, 100, |
Chris Craik | 243e85b | 2016-03-25 15:26:11 -0700 | [diff] [blame] | 390 | SkMatrix::MakeTrans(5, 5)); |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 391 | |
| 392 | auto node = TestUtils::createNode(0, 0, 200, 200, |
| 393 | [&layerUpdater](RenderProperties& props, RecordingCanvas& canvas) { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 394 | canvas.save(SaveFlags::MatrixClip); |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 395 | canvas.clipRect(50, 50, 150, 150, SkRegion::kIntersect_Op); |
| 396 | canvas.drawLayer(layerUpdater.get()); |
| 397 | canvas.restore(); |
| 398 | }); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 399 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(200, 200), 200, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 400 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 401 | TextureLayerTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 402 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 403 | EXPECT_EQ(1, renderer.getIndex()); |
| 404 | } |
| 405 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 406 | RENDERTHREAD_TEST(FrameBuilder, functor_reject) { |
Chris Craik | 223e3b6 | 2016-03-10 10:27:38 -0800 | [diff] [blame] | 407 | class FunctorTestRenderer : public TestRendererBase { |
| 408 | public: |
| 409 | void onFunctorOp(const FunctorOp& op, const BakedOpState& state) override { |
| 410 | EXPECT_EQ(0, mIndex++); |
| 411 | } |
| 412 | }; |
| 413 | Functor noopFunctor; |
| 414 | |
| 415 | // 1 million pixel tall view, scrolled down 80% |
| 416 | auto scrolledFunctorView = TestUtils::createNode(0, 0, 400, 1000000, |
| 417 | [&noopFunctor](RenderProperties& props, RecordingCanvas& canvas) { |
| 418 | canvas.translate(0, -800000); |
| 419 | canvas.callDrawGLFunction(&noopFunctor); |
| 420 | }); |
| 421 | |
| 422 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(200, 200), 200, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 423 | TestUtils::createSyncedNodeList(scrolledFunctorView), |
| 424 | sLightGeometry, Caches::getInstance()); |
Chris Craik | 223e3b6 | 2016-03-10 10:27:38 -0800 | [diff] [blame] | 425 | FunctorTestRenderer renderer; |
| 426 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
| 427 | EXPECT_EQ(1, renderer.getIndex()) << "Functor should not be rejected"; |
| 428 | } |
| 429 | |
Chris Craik | a204848 | 2016-03-25 14:17:49 -0700 | [diff] [blame] | 430 | RENDERTHREAD_TEST(FrameBuilder, deferColorOp_unbounded) { |
| 431 | class ColorTestRenderer : public TestRendererBase { |
| 432 | public: |
| 433 | void onColorOp(const ColorOp& op, const BakedOpState& state) override { |
| 434 | EXPECT_EQ(0, mIndex++); |
| 435 | EXPECT_EQ(Rect(200, 200), state.computedState.clippedBounds) |
| 436 | << "Color op should be expanded to bounds of surrounding"; |
| 437 | } |
| 438 | }; |
| 439 | |
| 440 | auto unclippedColorView = TestUtils::createNode(0, 0, 10, 10, |
| 441 | [](RenderProperties& props, RecordingCanvas& canvas) { |
| 442 | props.setClipToBounds(false); |
| 443 | canvas.drawColor(SK_ColorWHITE, SkXfermode::Mode::kSrcOver_Mode); |
| 444 | }); |
| 445 | |
| 446 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(200, 200), 200, 200, |
| 447 | TestUtils::createSyncedNodeList(unclippedColorView), |
| 448 | sLightGeometry, Caches::getInstance()); |
| 449 | ColorTestRenderer renderer; |
| 450 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
| 451 | EXPECT_EQ(1, renderer.getIndex()) << "ColorOp should not be rejected"; |
| 452 | } |
| 453 | |
| 454 | TEST(FrameBuilder, renderNode) { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 455 | class RenderNodeTestRenderer : public TestRendererBase { |
| 456 | public: |
| 457 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 458 | switch(mIndex++) { |
| 459 | case 0: |
Chris Craik | 5430ab2 | 2015-12-10 16:28:16 -0800 | [diff] [blame] | 460 | EXPECT_EQ(Rect(200, 200), state.computedState.clippedBounds); |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 461 | EXPECT_EQ(SK_ColorDKGRAY, op.paint->getColor()); |
| 462 | break; |
| 463 | case 1: |
| 464 | EXPECT_EQ(Rect(50, 50, 150, 150), state.computedState.clippedBounds); |
| 465 | EXPECT_EQ(SK_ColorWHITE, op.paint->getColor()); |
| 466 | break; |
| 467 | default: |
| 468 | ADD_FAILURE(); |
| 469 | } |
| 470 | } |
| 471 | }; |
| 472 | |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 473 | auto child = TestUtils::createNode(10, 10, 110, 110, |
| 474 | [](RenderProperties& props, RecordingCanvas& canvas) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 475 | SkPaint paint; |
| 476 | paint.setColor(SK_ColorWHITE); |
| 477 | canvas.drawRect(0, 0, 100, 100, paint); |
| 478 | }); |
| 479 | |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 480 | auto parent = TestUtils::createNode(0, 0, 200, 200, |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 481 | [&child](RenderProperties& props, RecordingCanvas& canvas) { |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 482 | SkPaint paint; |
| 483 | paint.setColor(SK_ColorDKGRAY); |
| 484 | canvas.drawRect(0, 0, 200, 200, paint); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 485 | |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 486 | canvas.save(SaveFlags::MatrixClip); |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 487 | canvas.translate(40, 40); |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 488 | canvas.drawRenderNode(child.get()); |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 489 | canvas.restore(); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 490 | }); |
| 491 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 492 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(200, 200), 200, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 493 | TestUtils::createSyncedNodeList(parent), sLightGeometry, Caches::getInstance()); |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 494 | RenderNodeTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 495 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | 223e3b6 | 2016-03-10 10:27:38 -0800 | [diff] [blame] | 496 | EXPECT_EQ(2, renderer.getIndex()); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 499 | RENDERTHREAD_TEST(FrameBuilder, clipped) { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 500 | class ClippedTestRenderer : public TestRendererBase { |
| 501 | public: |
| 502 | void onBitmapOp(const BitmapOp& op, const BakedOpState& state) override { |
| 503 | EXPECT_EQ(0, mIndex++); |
| 504 | EXPECT_EQ(Rect(10, 20, 30, 40), state.computedState.clippedBounds); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 505 | EXPECT_EQ(Rect(10, 20, 30, 40), state.computedState.clipRect()); |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 506 | EXPECT_TRUE(state.computedState.transform.isIdentity()); |
| 507 | } |
| 508 | }; |
| 509 | |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 510 | auto node = TestUtils::createNode(0, 0, 200, 200, |
| 511 | [](RenderProperties& props, RecordingCanvas& canvas) { |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 512 | SkBitmap bitmap = TestUtils::createSkBitmap(200, 200); |
| 513 | canvas.drawBitmap(bitmap, 0, 0, nullptr); |
| 514 | }); |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 515 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 516 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 517 | SkRect::MakeLTRB(10, 20, 30, 40), // clip to small area, should see in receiver |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 518 | 200, 200, TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 519 | ClippedTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 520 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 523 | RENDERTHREAD_TEST(FrameBuilder, saveLayer_simple) { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 524 | class SaveLayerSimpleTestRenderer : public TestRendererBase { |
| 525 | public: |
| 526 | OffscreenBuffer* startTemporaryLayer(uint32_t width, uint32_t height) override { |
| 527 | EXPECT_EQ(0, mIndex++); |
| 528 | EXPECT_EQ(180u, width); |
| 529 | EXPECT_EQ(180u, height); |
| 530 | return nullptr; |
| 531 | } |
| 532 | void endLayer() override { |
| 533 | EXPECT_EQ(2, mIndex++); |
| 534 | } |
| 535 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 536 | EXPECT_EQ(1, mIndex++); |
| 537 | EXPECT_EQ(Rect(10, 10, 190, 190), op.unmappedBounds); |
Chris Craik | 5430ab2 | 2015-12-10 16:28:16 -0800 | [diff] [blame] | 538 | EXPECT_EQ(Rect(180, 180), state.computedState.clippedBounds); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 539 | EXPECT_EQ(Rect(180, 180), state.computedState.clipRect()); |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 540 | |
| 541 | Matrix4 expectedTransform; |
| 542 | expectedTransform.loadTranslate(-10, -10, 0); |
| 543 | EXPECT_MATRIX_APPROX_EQ(expectedTransform, state.computedState.transform); |
| 544 | } |
| 545 | void onLayerOp(const LayerOp& op, const BakedOpState& state) override { |
| 546 | EXPECT_EQ(3, mIndex++); |
| 547 | EXPECT_EQ(Rect(10, 10, 190, 190), state.computedState.clippedBounds); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 548 | EXPECT_EQ(Rect(200, 200), state.computedState.clipRect()); |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 549 | EXPECT_TRUE(state.computedState.transform.isIdentity()); |
| 550 | } |
| 551 | }; |
| 552 | |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 553 | auto node = TestUtils::createNode(0, 0, 200, 200, |
| 554 | [](RenderProperties& props, RecordingCanvas& canvas) { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 555 | canvas.saveLayerAlpha(10, 10, 190, 190, 128, SaveFlags::ClipToLayer); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 556 | canvas.drawRect(10, 10, 190, 190, SkPaint()); |
| 557 | canvas.restore(); |
| 558 | }); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 559 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(200, 200), 200, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 560 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 561 | SaveLayerSimpleTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 562 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 563 | EXPECT_EQ(4, renderer.getIndex()); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 564 | } |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 565 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 566 | RENDERTHREAD_TEST(FrameBuilder, saveLayer_nested) { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 567 | /* saveLayer1 { rect1, saveLayer2 { rect2 } } will play back as: |
| 568 | * - startTemporaryLayer2, rect2 endLayer2 |
| 569 | * - startTemporaryLayer1, rect1, drawLayer2, endLayer1 |
| 570 | * - startFrame, layerOp1, endFrame |
| 571 | */ |
| 572 | class SaveLayerNestedTestRenderer : public TestRendererBase { |
| 573 | public: |
| 574 | OffscreenBuffer* startTemporaryLayer(uint32_t width, uint32_t height) override { |
| 575 | const int index = mIndex++; |
| 576 | if (index == 0) { |
| 577 | EXPECT_EQ(400u, width); |
| 578 | EXPECT_EQ(400u, height); |
| 579 | return (OffscreenBuffer*) 0x400; |
| 580 | } else if (index == 3) { |
| 581 | EXPECT_EQ(800u, width); |
| 582 | EXPECT_EQ(800u, height); |
| 583 | return (OffscreenBuffer*) 0x800; |
| 584 | } else { ADD_FAILURE(); } |
| 585 | return (OffscreenBuffer*) nullptr; |
| 586 | } |
| 587 | void endLayer() override { |
| 588 | int index = mIndex++; |
| 589 | EXPECT_TRUE(index == 2 || index == 6); |
| 590 | } |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 591 | void startFrame(uint32_t width, uint32_t height, const Rect& repaintRect) override { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 592 | EXPECT_EQ(7, mIndex++); |
| 593 | } |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 594 | void endFrame(const Rect& repaintRect) override { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 595 | EXPECT_EQ(9, mIndex++); |
| 596 | } |
| 597 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 598 | const int index = mIndex++; |
| 599 | if (index == 1) { |
Chris Craik | 5430ab2 | 2015-12-10 16:28:16 -0800 | [diff] [blame] | 600 | EXPECT_EQ(Rect(400, 400), op.unmappedBounds); // inner rect |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 601 | } else if (index == 4) { |
Chris Craik | 5430ab2 | 2015-12-10 16:28:16 -0800 | [diff] [blame] | 602 | EXPECT_EQ(Rect(800, 800), op.unmappedBounds); // outer rect |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 603 | } else { ADD_FAILURE(); } |
| 604 | } |
| 605 | void onLayerOp(const LayerOp& op, const BakedOpState& state) override { |
| 606 | const int index = mIndex++; |
| 607 | if (index == 5) { |
| 608 | EXPECT_EQ((OffscreenBuffer*)0x400, *op.layerHandle); |
Chris Craik | 5430ab2 | 2015-12-10 16:28:16 -0800 | [diff] [blame] | 609 | EXPECT_EQ(Rect(400, 400), op.unmappedBounds); // inner layer |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 610 | } else if (index == 8) { |
| 611 | EXPECT_EQ((OffscreenBuffer*)0x800, *op.layerHandle); |
Chris Craik | 5430ab2 | 2015-12-10 16:28:16 -0800 | [diff] [blame] | 612 | EXPECT_EQ(Rect(800, 800), op.unmappedBounds); // outer layer |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 613 | } else { ADD_FAILURE(); } |
| 614 | } |
| 615 | }; |
| 616 | |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 617 | auto node = TestUtils::createNode(0, 0, 800, 800, |
| 618 | [](RenderProperties& props, RecordingCanvas& canvas) { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 619 | canvas.saveLayerAlpha(0, 0, 800, 800, 128, SaveFlags::ClipToLayer); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 620 | { |
| 621 | canvas.drawRect(0, 0, 800, 800, SkPaint()); |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 622 | canvas.saveLayerAlpha(0, 0, 400, 400, 128, SaveFlags::ClipToLayer); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 623 | { |
| 624 | canvas.drawRect(0, 0, 400, 400, SkPaint()); |
| 625 | } |
| 626 | canvas.restore(); |
| 627 | } |
| 628 | canvas.restore(); |
| 629 | }); |
| 630 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 631 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(800, 800), 800, 800, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 632 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 633 | SaveLayerNestedTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 634 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 635 | EXPECT_EQ(10, renderer.getIndex()); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 636 | } |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 637 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 638 | RENDERTHREAD_TEST(FrameBuilder, saveLayer_contentRejection) { |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 639 | auto node = TestUtils::createNode(0, 0, 200, 200, |
| 640 | [](RenderProperties& props, RecordingCanvas& canvas) { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 641 | canvas.save(SaveFlags::MatrixClip); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 642 | canvas.clipRect(200, 200, 400, 400, SkRegion::kIntersect_Op); |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 643 | canvas.saveLayerAlpha(200, 200, 400, 400, 128, SaveFlags::ClipToLayer); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 644 | |
| 645 | // draw within save layer may still be recorded, but shouldn't be drawn |
| 646 | canvas.drawRect(200, 200, 400, 400, SkPaint()); |
| 647 | |
| 648 | canvas.restore(); |
| 649 | canvas.restore(); |
| 650 | }); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 651 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(200, 200), 200, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 652 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 653 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 654 | FailRenderer renderer; |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 655 | // should see no ops, even within the layer, since the layer should be rejected |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 656 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 659 | RENDERTHREAD_TEST(FrameBuilder, saveLayerUnclipped_simple) { |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 660 | class SaveLayerUnclippedSimpleTestRenderer : public TestRendererBase { |
| 661 | public: |
| 662 | void onCopyToLayerOp(const CopyToLayerOp& op, const BakedOpState& state) override { |
| 663 | EXPECT_EQ(0, mIndex++); |
| 664 | EXPECT_EQ(Rect(10, 10, 190, 190), state.computedState.clippedBounds); |
Chris Craik | 7435eb1 | 2016-01-07 17:41:40 -0800 | [diff] [blame] | 665 | EXPECT_CLIP_RECT(Rect(200, 200), state.computedState.clipState); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 666 | EXPECT_TRUE(state.computedState.transform.isIdentity()); |
| 667 | } |
| 668 | void onSimpleRectsOp(const SimpleRectsOp& op, const BakedOpState& state) override { |
| 669 | EXPECT_EQ(1, mIndex++); |
| 670 | ASSERT_NE(nullptr, op.paint); |
| 671 | ASSERT_EQ(SkXfermode::kClear_Mode, PaintUtils::getXfermodeDirect(op.paint)); |
| 672 | } |
| 673 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 674 | EXPECT_EQ(2, mIndex++); |
| 675 | EXPECT_EQ(Rect(200, 200), op.unmappedBounds); |
| 676 | EXPECT_EQ(Rect(200, 200), state.computedState.clippedBounds); |
| 677 | EXPECT_EQ(Rect(200, 200), state.computedState.clipRect()); |
| 678 | EXPECT_TRUE(state.computedState.transform.isIdentity()); |
| 679 | } |
| 680 | void onCopyFromLayerOp(const CopyFromLayerOp& op, const BakedOpState& state) override { |
| 681 | EXPECT_EQ(3, mIndex++); |
| 682 | EXPECT_EQ(Rect(10, 10, 190, 190), state.computedState.clippedBounds); |
Chris Craik | 7435eb1 | 2016-01-07 17:41:40 -0800 | [diff] [blame] | 683 | EXPECT_CLIP_RECT(Rect(200, 200), state.computedState.clipState); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 684 | EXPECT_TRUE(state.computedState.transform.isIdentity()); |
| 685 | } |
| 686 | }; |
| 687 | |
| 688 | auto node = TestUtils::createNode(0, 0, 200, 200, |
| 689 | [](RenderProperties& props, RecordingCanvas& canvas) { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 690 | canvas.saveLayerAlpha(10, 10, 190, 190, 128, (SaveFlags::Flags)(0)); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 691 | canvas.drawRect(0, 0, 200, 200, SkPaint()); |
| 692 | canvas.restore(); |
| 693 | }); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 694 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(200, 200), 200, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 695 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 696 | SaveLayerUnclippedSimpleTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 697 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 698 | EXPECT_EQ(4, renderer.getIndex()); |
| 699 | } |
| 700 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 701 | RENDERTHREAD_TEST(FrameBuilder, saveLayerUnclipped_mergedClears) { |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 702 | class SaveLayerUnclippedMergedClearsTestRenderer : public TestRendererBase { |
| 703 | public: |
| 704 | void onCopyToLayerOp(const CopyToLayerOp& op, const BakedOpState& state) override { |
| 705 | int index = mIndex++; |
| 706 | EXPECT_GT(4, index); |
| 707 | EXPECT_EQ(5, op.unmappedBounds.getWidth()); |
| 708 | EXPECT_EQ(5, op.unmappedBounds.getHeight()); |
| 709 | if (index == 0) { |
| 710 | EXPECT_EQ(Rect(10, 10), state.computedState.clippedBounds); |
| 711 | } else if (index == 1) { |
| 712 | EXPECT_EQ(Rect(190, 0, 200, 10), state.computedState.clippedBounds); |
| 713 | } else if (index == 2) { |
| 714 | EXPECT_EQ(Rect(0, 190, 10, 200), state.computedState.clippedBounds); |
| 715 | } else if (index == 3) { |
| 716 | EXPECT_EQ(Rect(190, 190, 200, 200), state.computedState.clippedBounds); |
| 717 | } |
| 718 | } |
| 719 | void onSimpleRectsOp(const SimpleRectsOp& op, const BakedOpState& state) override { |
| 720 | EXPECT_EQ(4, mIndex++); |
| 721 | ASSERT_EQ(op.vertexCount, 16u); |
| 722 | for (size_t i = 0; i < op.vertexCount; i++) { |
| 723 | auto v = op.vertices[i]; |
| 724 | EXPECT_TRUE(v.x == 0 || v.x == 10 || v.x == 190 || v.x == 200); |
| 725 | EXPECT_TRUE(v.y == 0 || v.y == 10 || v.y == 190 || v.y == 200); |
| 726 | } |
| 727 | } |
| 728 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 729 | EXPECT_EQ(5, mIndex++); |
| 730 | } |
| 731 | void onCopyFromLayerOp(const CopyFromLayerOp& op, const BakedOpState& state) override { |
| 732 | EXPECT_LT(5, mIndex++); |
| 733 | } |
| 734 | }; |
| 735 | |
| 736 | auto node = TestUtils::createNode(0, 0, 200, 200, |
| 737 | [](RenderProperties& props, RecordingCanvas& canvas) { |
| 738 | |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 739 | int restoreTo = canvas.save(SaveFlags::MatrixClip); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 740 | canvas.scale(2, 2); |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 741 | canvas.saveLayerAlpha(0, 0, 5, 5, 128, SaveFlags::MatrixClip); |
| 742 | canvas.saveLayerAlpha(95, 0, 100, 5, 128, SaveFlags::MatrixClip); |
| 743 | canvas.saveLayerAlpha(0, 95, 5, 100, 128, SaveFlags::MatrixClip); |
| 744 | canvas.saveLayerAlpha(95, 95, 100, 100, 128, SaveFlags::MatrixClip); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 745 | canvas.drawRect(0, 0, 100, 100, SkPaint()); |
| 746 | canvas.restoreToCount(restoreTo); |
| 747 | }); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 748 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(200, 200), 200, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 749 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 750 | SaveLayerUnclippedMergedClearsTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 751 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 752 | EXPECT_EQ(10, renderer.getIndex()) |
| 753 | << "Expect 4 copyTos, 4 copyFroms, 1 clear SimpleRects, and 1 rect."; |
| 754 | } |
| 755 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 756 | RENDERTHREAD_TEST(FrameBuilder, saveLayerUnclipped_clearClip) { |
Chris Craik | 4876de1 | 2016-02-25 16:54:08 -0800 | [diff] [blame] | 757 | class SaveLayerUnclippedClearClipTestRenderer : public TestRendererBase { |
| 758 | public: |
| 759 | void onCopyToLayerOp(const CopyToLayerOp& op, const BakedOpState& state) override { |
| 760 | EXPECT_EQ(0, mIndex++); |
| 761 | } |
| 762 | void onSimpleRectsOp(const SimpleRectsOp& op, const BakedOpState& state) override { |
| 763 | EXPECT_EQ(1, mIndex++); |
| 764 | ASSERT_NE(nullptr, op.paint); |
| 765 | EXPECT_EQ(SkXfermode::kClear_Mode, PaintUtils::getXfermodeDirect(op.paint)); |
| 766 | EXPECT_EQ(Rect(50, 50, 150, 150), state.computedState.clippedBounds) |
| 767 | << "Expect dirty rect as clip"; |
| 768 | ASSERT_NE(nullptr, state.computedState.clipState); |
| 769 | EXPECT_EQ(Rect(50, 50, 150, 150), state.computedState.clipState->rect); |
| 770 | EXPECT_EQ(ClipMode::Rectangle, state.computedState.clipState->mode); |
| 771 | } |
| 772 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 773 | EXPECT_EQ(2, mIndex++); |
| 774 | } |
| 775 | void onCopyFromLayerOp(const CopyFromLayerOp& op, const BakedOpState& state) override { |
| 776 | EXPECT_EQ(3, mIndex++); |
| 777 | } |
| 778 | }; |
| 779 | |
| 780 | auto node = TestUtils::createNode(0, 0, 200, 200, |
| 781 | [](RenderProperties& props, RecordingCanvas& canvas) { |
| 782 | // save smaller than clip, so we get unclipped behavior |
| 783 | canvas.saveLayerAlpha(10, 10, 190, 190, 128, (SaveFlags::Flags)(0)); |
| 784 | canvas.drawRect(0, 0, 200, 200, SkPaint()); |
| 785 | canvas.restore(); |
| 786 | }); |
| 787 | |
| 788 | // draw with partial screen dirty, and assert we see that rect later |
| 789 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeLTRB(50, 50, 150, 150), 200, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 790 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | 4876de1 | 2016-02-25 16:54:08 -0800 | [diff] [blame] | 791 | SaveLayerUnclippedClearClipTestRenderer renderer; |
| 792 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
| 793 | EXPECT_EQ(4, renderer.getIndex()); |
| 794 | } |
| 795 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 796 | RENDERTHREAD_TEST(FrameBuilder, saveLayerUnclipped_reject) { |
Chris Craik | 4876de1 | 2016-02-25 16:54:08 -0800 | [diff] [blame] | 797 | auto node = TestUtils::createNode(0, 0, 200, 200, |
| 798 | [](RenderProperties& props, RecordingCanvas& canvas) { |
| 799 | // unclipped savelayer + rect both in area that won't intersect with dirty |
| 800 | canvas.saveLayerAlpha(100, 100, 200, 200, 128, (SaveFlags::Flags)(0)); |
| 801 | canvas.drawRect(100, 100, 200, 200, SkPaint()); |
| 802 | canvas.restore(); |
| 803 | }); |
| 804 | |
| 805 | // draw with partial screen dirty that doesn't intersect with savelayer |
| 806 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(100, 100), 200, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 807 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | 4876de1 | 2016-02-25 16:54:08 -0800 | [diff] [blame] | 808 | FailRenderer renderer; |
| 809 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
| 810 | } |
| 811 | |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 812 | /* saveLayerUnclipped { saveLayer { saveLayerUnclipped { rect } } } will play back as: |
| 813 | * - startTemporaryLayer, onCopyToLayer, onSimpleRects, onRect, onCopyFromLayer, endLayer |
| 814 | * - startFrame, onCopyToLayer, onSimpleRects, drawLayer, onCopyFromLayer, endframe |
| 815 | */ |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 816 | RENDERTHREAD_TEST(FrameBuilder, saveLayerUnclipped_complex) { |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 817 | class SaveLayerUnclippedComplexTestRenderer : public TestRendererBase { |
| 818 | public: |
| 819 | OffscreenBuffer* startTemporaryLayer(uint32_t width, uint32_t height) { |
| 820 | EXPECT_EQ(0, mIndex++); // savelayer first |
| 821 | return (OffscreenBuffer*)0xabcd; |
| 822 | } |
| 823 | void onCopyToLayerOp(const CopyToLayerOp& op, const BakedOpState& state) override { |
| 824 | int index = mIndex++; |
| 825 | EXPECT_TRUE(index == 1 || index == 7); |
| 826 | } |
| 827 | void onSimpleRectsOp(const SimpleRectsOp& op, const BakedOpState& state) override { |
| 828 | int index = mIndex++; |
| 829 | EXPECT_TRUE(index == 2 || index == 8); |
| 830 | } |
| 831 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 832 | EXPECT_EQ(3, mIndex++); |
| 833 | Matrix4 expected; |
| 834 | expected.loadTranslate(-100, -100, 0); |
| 835 | EXPECT_EQ(Rect(100, 100, 200, 200), state.computedState.clippedBounds); |
| 836 | EXPECT_MATRIX_APPROX_EQ(expected, state.computedState.transform); |
| 837 | } |
| 838 | void onCopyFromLayerOp(const CopyFromLayerOp& op, const BakedOpState& state) override { |
| 839 | int index = mIndex++; |
| 840 | EXPECT_TRUE(index == 4 || index == 10); |
| 841 | } |
| 842 | void endLayer() override { |
| 843 | EXPECT_EQ(5, mIndex++); |
| 844 | } |
| 845 | void startFrame(uint32_t width, uint32_t height, const Rect& repaintRect) override { |
| 846 | EXPECT_EQ(6, mIndex++); |
| 847 | } |
| 848 | void onLayerOp(const LayerOp& op, const BakedOpState& state) override { |
| 849 | EXPECT_EQ(9, mIndex++); |
| 850 | } |
| 851 | void endFrame(const Rect& repaintRect) override { |
| 852 | EXPECT_EQ(11, mIndex++); |
| 853 | } |
| 854 | }; |
| 855 | |
| 856 | auto node = TestUtils::createNode(0, 0, 600, 600, // 500x500 triggers clipping |
| 857 | [](RenderProperties& props, RecordingCanvas& canvas) { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 858 | canvas.saveLayerAlpha(0, 0, 500, 500, 128, (SaveFlags::Flags)0); // unclipped |
| 859 | canvas.saveLayerAlpha(100, 100, 400, 400, 128, SaveFlags::ClipToLayer); // clipped |
| 860 | canvas.saveLayerAlpha(200, 200, 300, 300, 128, (SaveFlags::Flags)0); // unclipped |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 861 | canvas.drawRect(200, 200, 300, 300, SkPaint()); |
| 862 | canvas.restore(); |
| 863 | canvas.restore(); |
| 864 | canvas.restore(); |
| 865 | }); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 866 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(600, 600), 600, 600, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 867 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 868 | SaveLayerUnclippedComplexTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 869 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 870 | EXPECT_EQ(12, renderer.getIndex()); |
| 871 | } |
| 872 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 873 | RENDERTHREAD_TEST(FrameBuilder, hwLayer_simple) { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 874 | class HwLayerSimpleTestRenderer : public TestRendererBase { |
| 875 | public: |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 876 | void startRepaintLayer(OffscreenBuffer* offscreenBuffer, const Rect& repaintRect) override { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 877 | EXPECT_EQ(0, mIndex++); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 878 | EXPECT_EQ(100u, offscreenBuffer->viewportWidth); |
| 879 | EXPECT_EQ(100u, offscreenBuffer->viewportHeight); |
| 880 | EXPECT_EQ(Rect(25, 25, 75, 75), repaintRect); |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 881 | } |
| 882 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 883 | EXPECT_EQ(1, mIndex++); |
| 884 | |
| 885 | EXPECT_TRUE(state.computedState.transform.isIdentity()) |
| 886 | << "Transform should be reset within layer"; |
| 887 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 888 | EXPECT_EQ(Rect(25, 25, 75, 75), state.computedState.clipRect()) |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 889 | << "Damage rect should be used to clip layer content"; |
| 890 | } |
| 891 | void endLayer() override { |
| 892 | EXPECT_EQ(2, mIndex++); |
| 893 | } |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 894 | void startFrame(uint32_t width, uint32_t height, const Rect& repaintRect) override { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 895 | EXPECT_EQ(3, mIndex++); |
| 896 | } |
| 897 | void onLayerOp(const LayerOp& op, const BakedOpState& state) override { |
| 898 | EXPECT_EQ(4, mIndex++); |
| 899 | } |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 900 | void endFrame(const Rect& repaintRect) override { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 901 | EXPECT_EQ(5, mIndex++); |
| 902 | } |
| 903 | }; |
| 904 | |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 905 | auto node = TestUtils::createNode(10, 10, 110, 110, |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 906 | [](RenderProperties& props, RecordingCanvas& canvas) { |
| 907 | props.mutateLayerProperties().setType(LayerType::RenderLayer); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 908 | SkPaint paint; |
| 909 | paint.setColor(SK_ColorWHITE); |
| 910 | canvas.drawRect(0, 0, 100, 100, paint); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 911 | }); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 912 | OffscreenBuffer** layerHandle = node->getLayerHandle(); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 913 | |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 914 | // create RenderNode's layer here in same way prepareTree would |
| 915 | OffscreenBuffer layer(renderThread.renderState(), Caches::getInstance(), 100, 100); |
| 916 | *layerHandle = &layer; |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 917 | |
John Reck | 7db5ffb | 2016-01-15 13:17:09 -0800 | [diff] [blame] | 918 | auto syncedNodeList = TestUtils::createSyncedNodeList(node); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 919 | |
| 920 | // only enqueue partial damage |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 921 | LayerUpdateQueue layerUpdateQueue; // Note: enqueue damage post-sync, so bounds are valid |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 922 | layerUpdateQueue.enqueueLayerWithDamage(node.get(), Rect(25, 25, 75, 75)); |
| 923 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 924 | FrameBuilder frameBuilder(layerUpdateQueue, SkRect::MakeWH(200, 200), 200, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 925 | syncedNodeList, sLightGeometry, Caches::getInstance()); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 926 | HwLayerSimpleTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 927 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 928 | EXPECT_EQ(6, renderer.getIndex()); |
| 929 | |
| 930 | // clean up layer pointer, so we can safely destruct RenderNode |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 931 | *layerHandle = nullptr; |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 932 | } |
| 933 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 934 | RENDERTHREAD_TEST(FrameBuilder, hwLayer_complex) { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 935 | /* parentLayer { greyRect, saveLayer { childLayer { whiteRect } } } will play back as: |
| 936 | * - startRepaintLayer(child), rect(grey), endLayer |
| 937 | * - startTemporaryLayer, drawLayer(child), endLayer |
| 938 | * - startRepaintLayer(parent), rect(white), drawLayer(saveLayer), endLayer |
| 939 | * - startFrame, drawLayer(parent), endLayerb |
| 940 | */ |
| 941 | class HwLayerComplexTestRenderer : public TestRendererBase { |
| 942 | public: |
| 943 | OffscreenBuffer* startTemporaryLayer(uint32_t width, uint32_t height) { |
| 944 | EXPECT_EQ(3, mIndex++); // savelayer first |
| 945 | return (OffscreenBuffer*)0xabcd; |
| 946 | } |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 947 | void startRepaintLayer(OffscreenBuffer* offscreenBuffer, const Rect& repaintRect) override { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 948 | int index = mIndex++; |
| 949 | if (index == 0) { |
| 950 | // starting inner layer |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 951 | EXPECT_EQ(100u, offscreenBuffer->viewportWidth); |
| 952 | EXPECT_EQ(100u, offscreenBuffer->viewportHeight); |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 953 | } else if (index == 6) { |
| 954 | // starting outer layer |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 955 | EXPECT_EQ(200u, offscreenBuffer->viewportWidth); |
| 956 | EXPECT_EQ(200u, offscreenBuffer->viewportHeight); |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 957 | } else { ADD_FAILURE(); } |
| 958 | } |
| 959 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 960 | int index = mIndex++; |
| 961 | if (index == 1) { |
| 962 | // inner layer's rect (white) |
| 963 | EXPECT_EQ(SK_ColorWHITE, op.paint->getColor()); |
| 964 | } else if (index == 7) { |
| 965 | // outer layer's rect (grey) |
| 966 | EXPECT_EQ(SK_ColorDKGRAY, op.paint->getColor()); |
| 967 | } else { ADD_FAILURE(); } |
| 968 | } |
| 969 | void endLayer() override { |
| 970 | int index = mIndex++; |
| 971 | EXPECT_TRUE(index == 2 || index == 5 || index == 9); |
| 972 | } |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 973 | void startFrame(uint32_t width, uint32_t height, const Rect& repaintRect) override { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 974 | EXPECT_EQ(10, mIndex++); |
| 975 | } |
| 976 | void onLayerOp(const LayerOp& op, const BakedOpState& state) override { |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 977 | OffscreenBuffer* layer = *op.layerHandle; |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 978 | int index = mIndex++; |
| 979 | if (index == 4) { |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 980 | EXPECT_EQ(100u, layer->viewportWidth); |
| 981 | EXPECT_EQ(100u, layer->viewportHeight); |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 982 | } else if (index == 8) { |
| 983 | EXPECT_EQ((OffscreenBuffer*)0xabcd, *op.layerHandle); |
| 984 | } else if (index == 11) { |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 985 | EXPECT_EQ(200u, layer->viewportWidth); |
| 986 | EXPECT_EQ(200u, layer->viewportHeight); |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 987 | } else { ADD_FAILURE(); } |
| 988 | } |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 989 | void endFrame(const Rect& repaintRect) override { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 990 | EXPECT_EQ(12, mIndex++); |
| 991 | } |
| 992 | }; |
| 993 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 994 | auto child = TestUtils::createNode(50, 50, 150, 150, |
| 995 | [](RenderProperties& props, RecordingCanvas& canvas) { |
| 996 | props.mutateLayerProperties().setType(LayerType::RenderLayer); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 997 | SkPaint paint; |
| 998 | paint.setColor(SK_ColorWHITE); |
| 999 | canvas.drawRect(0, 0, 100, 100, paint); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 1000 | }); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1001 | OffscreenBuffer childLayer(renderThread.renderState(), Caches::getInstance(), 100, 100); |
| 1002 | *(child->getLayerHandle()) = &childLayer; |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 1003 | |
| 1004 | RenderNode* childPtr = child.get(); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 1005 | auto parent = TestUtils::createNode(0, 0, 200, 200, |
| 1006 | [childPtr](RenderProperties& props, RecordingCanvas& canvas) { |
| 1007 | props.mutateLayerProperties().setType(LayerType::RenderLayer); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 1008 | SkPaint paint; |
| 1009 | paint.setColor(SK_ColorDKGRAY); |
| 1010 | canvas.drawRect(0, 0, 200, 200, paint); |
| 1011 | |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 1012 | canvas.saveLayerAlpha(50, 50, 150, 150, 128, SaveFlags::ClipToLayer); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 1013 | canvas.drawRenderNode(childPtr); |
| 1014 | canvas.restore(); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 1015 | }); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1016 | OffscreenBuffer parentLayer(renderThread.renderState(), Caches::getInstance(), 200, 200); |
| 1017 | *(parent->getLayerHandle()) = &parentLayer; |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 1018 | |
John Reck | 7db5ffb | 2016-01-15 13:17:09 -0800 | [diff] [blame] | 1019 | auto syncedList = TestUtils::createSyncedNodeList(parent); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 1020 | |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1021 | LayerUpdateQueue layerUpdateQueue; // Note: enqueue damage post-sync, so bounds are valid |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 1022 | layerUpdateQueue.enqueueLayerWithDamage(child.get(), Rect(100, 100)); |
| 1023 | layerUpdateQueue.enqueueLayerWithDamage(parent.get(), Rect(200, 200)); |
| 1024 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1025 | FrameBuilder frameBuilder(layerUpdateQueue, SkRect::MakeWH(200, 200), 200, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1026 | syncedList, sLightGeometry, Caches::getInstance()); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 1027 | HwLayerComplexTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1028 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 1029 | EXPECT_EQ(13, renderer.getIndex()); |
| 1030 | |
| 1031 | // clean up layer pointers, so we can safely destruct RenderNodes |
| 1032 | *(child->getLayerHandle()) = nullptr; |
| 1033 | *(parent->getLayerHandle()) = nullptr; |
| 1034 | } |
| 1035 | |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 1036 | static void drawOrderedRect(RecordingCanvas* canvas, uint8_t expectedDrawOrder) { |
| 1037 | SkPaint paint; |
| 1038 | paint.setColor(SkColorSetARGB(256, 0, 0, expectedDrawOrder)); // order put in blue channel |
| 1039 | canvas->drawRect(0, 0, 100, 100, paint); |
| 1040 | } |
| 1041 | static void drawOrderedNode(RecordingCanvas* canvas, uint8_t expectedDrawOrder, float z) { |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 1042 | auto node = TestUtils::createNode(0, 0, 100, 100, |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 1043 | [expectedDrawOrder](RenderProperties& props, RecordingCanvas& canvas) { |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 1044 | drawOrderedRect(&canvas, expectedDrawOrder); |
| 1045 | }); |
| 1046 | node->mutateStagingProperties().setTranslationZ(z); |
| 1047 | node->setPropertyFieldsDirty(RenderNode::TRANSLATION_Z); |
| 1048 | canvas->drawRenderNode(node.get()); // canvas takes reference/sole ownership |
| 1049 | } |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1050 | RENDERTHREAD_TEST(FrameBuilder, zReorder) { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 1051 | class ZReorderTestRenderer : public TestRendererBase { |
| 1052 | public: |
| 1053 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 1054 | int expectedOrder = SkColorGetB(op.paint->getColor()); // extract order from blue channel |
| 1055 | EXPECT_EQ(expectedOrder, mIndex++) << "An op was drawn out of order"; |
| 1056 | } |
| 1057 | }; |
| 1058 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 1059 | auto parent = TestUtils::createNode(0, 0, 100, 100, |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 1060 | [](RenderProperties& props, RecordingCanvas& canvas) { |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 1061 | drawOrderedNode(&canvas, 0, 10.0f); // in reorder=false at this point, so played inorder |
| 1062 | drawOrderedRect(&canvas, 1); |
| 1063 | canvas.insertReorderBarrier(true); |
| 1064 | drawOrderedNode(&canvas, 6, 2.0f); |
| 1065 | drawOrderedRect(&canvas, 3); |
| 1066 | drawOrderedNode(&canvas, 4, 0.0f); |
| 1067 | drawOrderedRect(&canvas, 5); |
| 1068 | drawOrderedNode(&canvas, 2, -2.0f); |
| 1069 | drawOrderedNode(&canvas, 7, 2.0f); |
| 1070 | canvas.insertReorderBarrier(false); |
| 1071 | drawOrderedRect(&canvas, 8); |
| 1072 | drawOrderedNode(&canvas, 9, -10.0f); // in reorder=false at this point, so played inorder |
| 1073 | }); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1074 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(100, 100), 100, 100, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1075 | TestUtils::createSyncedNodeList(parent), sLightGeometry, Caches::getInstance()); |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 1076 | ZReorderTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1077 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 1078 | EXPECT_EQ(10, renderer.getIndex()); |
| 1079 | }; |
| 1080 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1081 | RENDERTHREAD_TEST(FrameBuilder, projectionReorder) { |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 1082 | static const int scrollX = 5; |
| 1083 | static const int scrollY = 10; |
| 1084 | class ProjectionReorderTestRenderer : public TestRendererBase { |
| 1085 | public: |
| 1086 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 1087 | const int index = mIndex++; |
| 1088 | |
| 1089 | Matrix4 expectedMatrix; |
| 1090 | switch (index) { |
| 1091 | case 0: |
| 1092 | EXPECT_EQ(Rect(100, 100), op.unmappedBounds); |
| 1093 | EXPECT_EQ(SK_ColorWHITE, op.paint->getColor()); |
| 1094 | expectedMatrix.loadIdentity(); |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 1095 | EXPECT_EQ(nullptr, state.computedState.localProjectionPathMask); |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 1096 | break; |
| 1097 | case 1: |
| 1098 | EXPECT_EQ(Rect(-10, -10, 60, 60), op.unmappedBounds); |
| 1099 | EXPECT_EQ(SK_ColorDKGRAY, op.paint->getColor()); |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 1100 | expectedMatrix.loadTranslate(50 - scrollX, 50 - scrollY, 0); |
| 1101 | ASSERT_NE(nullptr, state.computedState.localProjectionPathMask); |
| 1102 | EXPECT_EQ(Rect(-35, -30, 45, 50), |
| 1103 | Rect(state.computedState.localProjectionPathMask->getBounds())); |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 1104 | break; |
| 1105 | case 2: |
| 1106 | EXPECT_EQ(Rect(100, 50), op.unmappedBounds); |
| 1107 | EXPECT_EQ(SK_ColorBLUE, op.paint->getColor()); |
| 1108 | expectedMatrix.loadTranslate(-scrollX, 50 - scrollY, 0); |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 1109 | EXPECT_EQ(nullptr, state.computedState.localProjectionPathMask); |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 1110 | break; |
| 1111 | default: |
| 1112 | ADD_FAILURE(); |
| 1113 | } |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 1114 | EXPECT_EQ(expectedMatrix, state.computedState.transform); |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 1115 | } |
| 1116 | }; |
| 1117 | |
| 1118 | /** |
| 1119 | * Construct a tree of nodes, where the root (A) has a receiver background (B), and a child (C) |
| 1120 | * with a projecting child (P) of its own. P would normally draw between B and C's "background" |
| 1121 | * draw, but because it is projected backwards, it's drawn in between B and C. |
| 1122 | * |
| 1123 | * The parent is scrolled by scrollX/scrollY, but this does not affect the background |
| 1124 | * (which isn't affected by scroll). |
| 1125 | */ |
| 1126 | auto receiverBackground = TestUtils::createNode(0, 0, 100, 100, |
| 1127 | [](RenderProperties& properties, RecordingCanvas& canvas) { |
| 1128 | properties.setProjectionReceiver(true); |
| 1129 | // scroll doesn't apply to background, so undone via translationX/Y |
| 1130 | // NOTE: translationX/Y only! no other transform properties may be set for a proj receiver! |
| 1131 | properties.setTranslationX(scrollX); |
| 1132 | properties.setTranslationY(scrollY); |
| 1133 | |
| 1134 | SkPaint paint; |
| 1135 | paint.setColor(SK_ColorWHITE); |
| 1136 | canvas.drawRect(0, 0, 100, 100, paint); |
| 1137 | }); |
| 1138 | auto projectingRipple = TestUtils::createNode(50, 0, 100, 50, |
| 1139 | [](RenderProperties& properties, RecordingCanvas& canvas) { |
| 1140 | properties.setProjectBackwards(true); |
| 1141 | properties.setClipToBounds(false); |
| 1142 | SkPaint paint; |
| 1143 | paint.setColor(SK_ColorDKGRAY); |
| 1144 | canvas.drawRect(-10, -10, 60, 60, paint); |
| 1145 | }); |
| 1146 | auto child = TestUtils::createNode(0, 50, 100, 100, |
| 1147 | [&projectingRipple](RenderProperties& properties, RecordingCanvas& canvas) { |
| 1148 | SkPaint paint; |
| 1149 | paint.setColor(SK_ColorBLUE); |
| 1150 | canvas.drawRect(0, 0, 100, 50, paint); |
| 1151 | canvas.drawRenderNode(projectingRipple.get()); |
| 1152 | }); |
| 1153 | auto parent = TestUtils::createNode(0, 0, 100, 100, |
| 1154 | [&receiverBackground, &child](RenderProperties& properties, RecordingCanvas& canvas) { |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 1155 | // Set a rect outline for the projecting ripple to be masked against. |
| 1156 | properties.mutableOutline().setRoundRect(10, 10, 90, 90, 5, 1.0f); |
| 1157 | |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 1158 | canvas.save(SaveFlags::MatrixClip); |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 1159 | canvas.translate(-scrollX, -scrollY); // Apply scroll (note: bg undoes this internally) |
| 1160 | canvas.drawRenderNode(receiverBackground.get()); |
| 1161 | canvas.drawRenderNode(child.get()); |
| 1162 | canvas.restore(); |
| 1163 | }); |
| 1164 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1165 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(100, 100), 100, 100, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1166 | TestUtils::createSyncedNodeList(parent), sLightGeometry, Caches::getInstance()); |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 1167 | ProjectionReorderTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1168 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 1169 | EXPECT_EQ(3, renderer.getIndex()); |
| 1170 | } |
| 1171 | |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 1172 | RENDERTHREAD_TEST(FrameBuilder, projectionHwLayer) { |
| 1173 | static const int scrollX = 5; |
| 1174 | static const int scrollY = 10; |
| 1175 | class ProjectionHwLayerTestRenderer : public TestRendererBase { |
| 1176 | public: |
| 1177 | void startRepaintLayer(OffscreenBuffer* offscreenBuffer, const Rect& repaintRect) override { |
| 1178 | EXPECT_EQ(0, mIndex++); |
| 1179 | } |
| 1180 | void onArcOp(const ArcOp& op, const BakedOpState& state) override { |
| 1181 | EXPECT_EQ(1, mIndex++); |
| 1182 | ASSERT_EQ(nullptr, state.computedState.localProjectionPathMask); |
| 1183 | } |
| 1184 | void endLayer() override { |
| 1185 | EXPECT_EQ(2, mIndex++); |
| 1186 | } |
| 1187 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 1188 | EXPECT_EQ(3, mIndex++); |
| 1189 | ASSERT_EQ(nullptr, state.computedState.localProjectionPathMask); |
| 1190 | } |
| 1191 | void onOvalOp(const OvalOp& op, const BakedOpState& state) override { |
| 1192 | EXPECT_EQ(4, mIndex++); |
| 1193 | ASSERT_NE(nullptr, state.computedState.localProjectionPathMask); |
| 1194 | Matrix4 expected; |
| 1195 | expected.loadTranslate(100 - scrollX, 100 - scrollY, 0); |
| 1196 | EXPECT_EQ(expected, state.computedState.transform); |
| 1197 | EXPECT_EQ(Rect(-85, -80, 295, 300), |
| 1198 | Rect(state.computedState.localProjectionPathMask->getBounds())); |
| 1199 | } |
| 1200 | void onLayerOp(const LayerOp& op, const BakedOpState& state) override { |
| 1201 | EXPECT_EQ(5, mIndex++); |
| 1202 | ASSERT_EQ(nullptr, state.computedState.localProjectionPathMask); |
| 1203 | } |
| 1204 | }; |
| 1205 | auto receiverBackground = TestUtils::createNode(0, 0, 400, 400, |
| 1206 | [](RenderProperties& properties, RecordingCanvas& canvas) { |
| 1207 | properties.setProjectionReceiver(true); |
| 1208 | // scroll doesn't apply to background, so undone via translationX/Y |
| 1209 | // NOTE: translationX/Y only! no other transform properties may be set for a proj receiver! |
| 1210 | properties.setTranslationX(scrollX); |
| 1211 | properties.setTranslationY(scrollY); |
| 1212 | |
| 1213 | canvas.drawRect(0, 0, 400, 400, SkPaint()); |
| 1214 | }); |
| 1215 | auto projectingRipple = TestUtils::createNode(0, 0, 200, 200, |
| 1216 | [](RenderProperties& properties, RecordingCanvas& canvas) { |
| 1217 | properties.setProjectBackwards(true); |
| 1218 | properties.setClipToBounds(false); |
| 1219 | canvas.drawOval(100, 100, 300, 300, SkPaint()); // drawn mostly out of layer bounds |
| 1220 | }); |
| 1221 | auto child = TestUtils::createNode(100, 100, 300, 300, |
| 1222 | [&projectingRipple](RenderProperties& properties, RecordingCanvas& canvas) { |
| 1223 | properties.mutateLayerProperties().setType(LayerType::RenderLayer); |
| 1224 | canvas.drawRenderNode(projectingRipple.get()); |
| 1225 | canvas.drawArc(0, 0, 200, 200, 0.0f, 280.0f, true, SkPaint()); |
| 1226 | }); |
| 1227 | auto parent = TestUtils::createNode(0, 0, 400, 400, |
| 1228 | [&receiverBackground, &child](RenderProperties& properties, RecordingCanvas& canvas) { |
| 1229 | // Set a rect outline for the projecting ripple to be masked against. |
| 1230 | properties.mutableOutline().setRoundRect(10, 10, 390, 390, 0, 1.0f); |
| 1231 | canvas.translate(-scrollX, -scrollY); // Apply scroll (note: bg undoes this internally) |
| 1232 | canvas.drawRenderNode(receiverBackground.get()); |
| 1233 | canvas.drawRenderNode(child.get()); |
| 1234 | }); |
| 1235 | |
| 1236 | OffscreenBuffer** layerHandle = child->getLayerHandle(); |
| 1237 | |
| 1238 | // create RenderNode's layer here in same way prepareTree would, setting windowTransform |
| 1239 | OffscreenBuffer layer(renderThread.renderState(), Caches::getInstance(), 200, 200); |
| 1240 | Matrix4 windowTransform; |
| 1241 | windowTransform.loadTranslate(100, 100, 0); // total transform of layer's origin |
| 1242 | layer.setWindowTransform(windowTransform); |
| 1243 | *layerHandle = &layer; |
| 1244 | |
| 1245 | auto syncedList = TestUtils::createSyncedNodeList(parent); |
| 1246 | LayerUpdateQueue layerUpdateQueue; // Note: enqueue damage post-sync, so bounds are valid |
| 1247 | layerUpdateQueue.enqueueLayerWithDamage(child.get(), Rect(200, 200)); |
| 1248 | FrameBuilder frameBuilder(layerUpdateQueue, SkRect::MakeWH(400, 400), 400, 400, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1249 | syncedList, sLightGeometry, Caches::getInstance()); |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 1250 | ProjectionHwLayerTestRenderer renderer; |
| 1251 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
| 1252 | EXPECT_EQ(6, renderer.getIndex()); |
| 1253 | |
| 1254 | // clean up layer pointer, so we can safely destruct RenderNode |
| 1255 | *layerHandle = nullptr; |
| 1256 | } |
| 1257 | |
Chris Craik | a748c08 | 2016-03-01 18:48:37 -0800 | [diff] [blame] | 1258 | RENDERTHREAD_TEST(FrameBuilder, projectionChildScroll) { |
| 1259 | static const int scrollX = 500000; |
| 1260 | static const int scrollY = 0; |
| 1261 | class ProjectionChildScrollTestRenderer : public TestRendererBase { |
| 1262 | public: |
| 1263 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 1264 | EXPECT_EQ(0, mIndex++); |
| 1265 | EXPECT_TRUE(state.computedState.transform.isIdentity()); |
| 1266 | } |
| 1267 | void onOvalOp(const OvalOp& op, const BakedOpState& state) override { |
| 1268 | EXPECT_EQ(1, mIndex++); |
| 1269 | ASSERT_NE(nullptr, state.computedState.clipState); |
| 1270 | ASSERT_EQ(ClipMode::Rectangle, state.computedState.clipState->mode); |
| 1271 | ASSERT_EQ(Rect(400, 400), state.computedState.clipState->rect); |
| 1272 | EXPECT_TRUE(state.computedState.transform.isIdentity()); |
| 1273 | } |
| 1274 | }; |
| 1275 | auto receiverBackground = TestUtils::createNode(0, 0, 400, 400, |
| 1276 | [](RenderProperties& properties, RecordingCanvas& canvas) { |
| 1277 | properties.setProjectionReceiver(true); |
| 1278 | canvas.drawRect(0, 0, 400, 400, SkPaint()); |
| 1279 | }); |
| 1280 | auto projectingRipple = TestUtils::createNode(0, 0, 200, 200, |
| 1281 | [](RenderProperties& properties, RecordingCanvas& canvas) { |
| 1282 | // scroll doesn't apply to background, so undone via translationX/Y |
| 1283 | // NOTE: translationX/Y only! no other transform properties may be set for a proj receiver! |
| 1284 | properties.setTranslationX(scrollX); |
| 1285 | properties.setTranslationY(scrollY); |
| 1286 | properties.setProjectBackwards(true); |
| 1287 | properties.setClipToBounds(false); |
| 1288 | canvas.drawOval(0, 0, 200, 200, SkPaint()); |
| 1289 | }); |
| 1290 | auto child = TestUtils::createNode(0, 0, 400, 400, |
| 1291 | [&projectingRipple](RenderProperties& properties, RecordingCanvas& canvas) { |
| 1292 | // Record time clip will be ignored by projectee |
| 1293 | canvas.clipRect(100, 100, 300, 300, SkRegion::kIntersect_Op); |
| 1294 | |
| 1295 | canvas.translate(-scrollX, -scrollY); // Apply scroll (note: bg undoes this internally) |
| 1296 | canvas.drawRenderNode(projectingRipple.get()); |
| 1297 | }); |
| 1298 | auto parent = TestUtils::createNode(0, 0, 400, 400, |
| 1299 | [&receiverBackground, &child](RenderProperties& properties, RecordingCanvas& canvas) { |
| 1300 | canvas.drawRenderNode(receiverBackground.get()); |
| 1301 | canvas.drawRenderNode(child.get()); |
| 1302 | }); |
| 1303 | |
| 1304 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(400, 400), 400, 400, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1305 | TestUtils::createSyncedNodeList(parent), sLightGeometry, Caches::getInstance()); |
Chris Craik | a748c08 | 2016-03-01 18:48:37 -0800 | [diff] [blame] | 1306 | ProjectionChildScrollTestRenderer renderer; |
| 1307 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
| 1308 | EXPECT_EQ(2, renderer.getIndex()); |
| 1309 | } |
| 1310 | |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1311 | // creates a 100x100 shadow casting node with provided translationZ |
| 1312 | static sp<RenderNode> createWhiteRectShadowCaster(float translationZ) { |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 1313 | return TestUtils::createNode(0, 0, 100, 100, |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 1314 | [translationZ](RenderProperties& properties, RecordingCanvas& canvas) { |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 1315 | properties.setTranslationZ(translationZ); |
| 1316 | properties.mutableOutline().setRoundRect(0, 0, 100, 100, 0.0f, 1.0f); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1317 | SkPaint paint; |
| 1318 | paint.setColor(SK_ColorWHITE); |
| 1319 | canvas.drawRect(0, 0, 100, 100, paint); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1320 | }); |
| 1321 | } |
| 1322 | |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 1323 | RENDERTHREAD_TEST(FrameBuilder, shadow) { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 1324 | class ShadowTestRenderer : public TestRendererBase { |
| 1325 | public: |
| 1326 | void onShadowOp(const ShadowOp& op, const BakedOpState& state) override { |
| 1327 | EXPECT_EQ(0, mIndex++); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1328 | EXPECT_FLOAT_EQ(1.0f, op.casterAlpha); |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 1329 | EXPECT_TRUE(op.shadowTask->casterPerimeter.isRect(nullptr)); |
| 1330 | EXPECT_MATRIX_APPROX_EQ(Matrix4::identity(), op.shadowTask->transformXY); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1331 | |
| 1332 | Matrix4 expectedZ; |
| 1333 | expectedZ.loadTranslate(0, 0, 5); |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 1334 | EXPECT_MATRIX_APPROX_EQ(expectedZ, op.shadowTask->transformZ); |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 1335 | } |
| 1336 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 1337 | EXPECT_EQ(1, mIndex++); |
| 1338 | } |
| 1339 | }; |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 1340 | |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 1341 | auto parent = TestUtils::createNode(0, 0, 200, 200, |
| 1342 | [](RenderProperties& props, RecordingCanvas& canvas) { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 1343 | canvas.insertReorderBarrier(true); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1344 | canvas.drawRenderNode(createWhiteRectShadowCaster(5.0f).get()); |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 1345 | }); |
| 1346 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1347 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(200, 200), 200, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1348 | TestUtils::createSyncedNodeList(parent), sLightGeometry, Caches::getInstance()); |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 1349 | ShadowTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1350 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 1351 | EXPECT_EQ(2, renderer.getIndex()); |
| 1352 | } |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 1353 | |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 1354 | RENDERTHREAD_TEST(FrameBuilder, shadowSaveLayer) { |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1355 | class ShadowSaveLayerTestRenderer : public TestRendererBase { |
| 1356 | public: |
| 1357 | OffscreenBuffer* startTemporaryLayer(uint32_t width, uint32_t height) override { |
| 1358 | EXPECT_EQ(0, mIndex++); |
| 1359 | return nullptr; |
| 1360 | } |
| 1361 | void onShadowOp(const ShadowOp& op, const BakedOpState& state) override { |
| 1362 | EXPECT_EQ(1, mIndex++); |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 1363 | EXPECT_FLOAT_EQ(50, op.shadowTask->lightCenter.x); |
| 1364 | EXPECT_FLOAT_EQ(40, op.shadowTask->lightCenter.y); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1365 | } |
| 1366 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 1367 | EXPECT_EQ(2, mIndex++); |
| 1368 | } |
| 1369 | void endLayer() override { |
| 1370 | EXPECT_EQ(3, mIndex++); |
| 1371 | } |
| 1372 | void onLayerOp(const LayerOp& op, const BakedOpState& state) override { |
| 1373 | EXPECT_EQ(4, mIndex++); |
| 1374 | } |
| 1375 | }; |
| 1376 | |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 1377 | auto parent = TestUtils::createNode(0, 0, 200, 200, |
| 1378 | [](RenderProperties& props, RecordingCanvas& canvas) { |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1379 | // save/restore outside of reorderBarrier, so they don't get moved out of place |
| 1380 | canvas.translate(20, 10); |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 1381 | int count = canvas.saveLayerAlpha(30, 50, 130, 150, 128, SaveFlags::ClipToLayer); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1382 | canvas.insertReorderBarrier(true); |
| 1383 | canvas.drawRenderNode(createWhiteRectShadowCaster(5.0f).get()); |
| 1384 | canvas.insertReorderBarrier(false); |
| 1385 | canvas.restoreToCount(count); |
| 1386 | }); |
| 1387 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1388 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(200, 200), 200, 200, |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 1389 | TestUtils::createSyncedNodeList(parent), |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1390 | (FrameBuilder::LightGeometry) {{ 100, 100, 100 }, 50}, Caches::getInstance()); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1391 | ShadowSaveLayerTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1392 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1393 | EXPECT_EQ(5, renderer.getIndex()); |
| 1394 | } |
| 1395 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1396 | RENDERTHREAD_TEST(FrameBuilder, shadowHwLayer) { |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1397 | class ShadowHwLayerTestRenderer : public TestRendererBase { |
| 1398 | public: |
| 1399 | void startRepaintLayer(OffscreenBuffer* offscreenBuffer, const Rect& repaintRect) override { |
| 1400 | EXPECT_EQ(0, mIndex++); |
| 1401 | } |
| 1402 | void onShadowOp(const ShadowOp& op, const BakedOpState& state) override { |
| 1403 | EXPECT_EQ(1, mIndex++); |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 1404 | EXPECT_FLOAT_EQ(50, op.shadowTask->lightCenter.x); |
| 1405 | EXPECT_FLOAT_EQ(40, op.shadowTask->lightCenter.y); |
| 1406 | EXPECT_FLOAT_EQ(30, op.shadowTask->lightRadius); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1407 | } |
| 1408 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 1409 | EXPECT_EQ(2, mIndex++); |
| 1410 | } |
| 1411 | void endLayer() override { |
| 1412 | EXPECT_EQ(3, mIndex++); |
| 1413 | } |
| 1414 | void onLayerOp(const LayerOp& op, const BakedOpState& state) override { |
| 1415 | EXPECT_EQ(4, mIndex++); |
| 1416 | } |
| 1417 | }; |
| 1418 | |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 1419 | auto parent = TestUtils::createNode(50, 60, 150, 160, |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 1420 | [](RenderProperties& props, RecordingCanvas& canvas) { |
| 1421 | props.mutateLayerProperties().setType(LayerType::RenderLayer); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1422 | canvas.insertReorderBarrier(true); |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 1423 | canvas.save(SaveFlags::MatrixClip); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1424 | canvas.translate(20, 10); |
| 1425 | canvas.drawRenderNode(createWhiteRectShadowCaster(5.0f).get()); |
| 1426 | canvas.restore(); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 1427 | }); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1428 | OffscreenBuffer** layerHandle = parent->getLayerHandle(); |
| 1429 | |
| 1430 | // create RenderNode's layer here in same way prepareTree would, setting windowTransform |
| 1431 | OffscreenBuffer layer(renderThread.renderState(), Caches::getInstance(), 100, 100); |
| 1432 | Matrix4 windowTransform; |
| 1433 | windowTransform.loadTranslate(50, 60, 0); // total transform of layer's origin |
| 1434 | layer.setWindowTransform(windowTransform); |
| 1435 | *layerHandle = &layer; |
| 1436 | |
John Reck | 7db5ffb | 2016-01-15 13:17:09 -0800 | [diff] [blame] | 1437 | auto syncedList = TestUtils::createSyncedNodeList(parent); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1438 | LayerUpdateQueue layerUpdateQueue; // Note: enqueue damage post-sync, so bounds are valid |
| 1439 | layerUpdateQueue.enqueueLayerWithDamage(parent.get(), Rect(100, 100)); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1440 | FrameBuilder frameBuilder(layerUpdateQueue, SkRect::MakeWH(200, 200), 200, 200, |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 1441 | syncedList, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1442 | (FrameBuilder::LightGeometry) {{ 100, 100, 100 }, 30}, Caches::getInstance()); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1443 | ShadowHwLayerTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1444 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1445 | EXPECT_EQ(5, renderer.getIndex()); |
| 1446 | |
| 1447 | // clean up layer pointer, so we can safely destruct RenderNode |
| 1448 | *layerHandle = nullptr; |
| 1449 | } |
| 1450 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1451 | RENDERTHREAD_TEST(FrameBuilder, shadowLayering) { |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1452 | class ShadowLayeringTestRenderer : public TestRendererBase { |
| 1453 | public: |
| 1454 | void onShadowOp(const ShadowOp& op, const BakedOpState& state) override { |
| 1455 | int index = mIndex++; |
| 1456 | EXPECT_TRUE(index == 0 || index == 1); |
| 1457 | } |
| 1458 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 1459 | int index = mIndex++; |
| 1460 | EXPECT_TRUE(index == 2 || index == 3); |
| 1461 | } |
| 1462 | }; |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 1463 | auto parent = TestUtils::createNode(0, 0, 200, 200, |
| 1464 | [](RenderProperties& props, RecordingCanvas& canvas) { |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1465 | canvas.insertReorderBarrier(true); |
| 1466 | canvas.drawRenderNode(createWhiteRectShadowCaster(5.0f).get()); |
| 1467 | canvas.drawRenderNode(createWhiteRectShadowCaster(5.0001f).get()); |
| 1468 | }); |
| 1469 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1470 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(200, 200), 200, 200, |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 1471 | TestUtils::createSyncedNodeList(parent), |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1472 | (FrameBuilder::LightGeometry) {{ 100, 100, 100 }, 50}, Caches::getInstance()); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1473 | ShadowLayeringTestRenderer renderer; |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1474 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 1475 | EXPECT_EQ(4, renderer.getIndex()); |
| 1476 | } |
| 1477 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 1478 | static void testProperty(std::function<void(RenderProperties&)> propSetupCallback, |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 1479 | std::function<void(const RectOp&, const BakedOpState&)> opValidateCallback) { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 1480 | class PropertyTestRenderer : public TestRendererBase { |
| 1481 | public: |
| 1482 | PropertyTestRenderer(std::function<void(const RectOp&, const BakedOpState&)> callback) |
| 1483 | : mCallback(callback) {} |
| 1484 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 1485 | EXPECT_EQ(mIndex++, 0); |
| 1486 | mCallback(op, state); |
| 1487 | } |
| 1488 | std::function<void(const RectOp&, const BakedOpState&)> mCallback; |
| 1489 | }; |
| 1490 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 1491 | auto node = TestUtils::createNode(0, 0, 100, 100, |
| 1492 | [propSetupCallback](RenderProperties& props, RecordingCanvas& canvas) { |
| 1493 | propSetupCallback(props); |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 1494 | SkPaint paint; |
| 1495 | paint.setColor(SK_ColorWHITE); |
| 1496 | canvas.drawRect(0, 0, 100, 100, paint); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 1497 | }); |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 1498 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1499 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(100, 100), 200, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1500 | TestUtils::createSyncedNodeList(node), sLightGeometry, Caches::getInstance()); |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 1501 | PropertyTestRenderer renderer(opValidateCallback); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1502 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 1503 | EXPECT_EQ(1, renderer.getIndex()) << "Should have seen one op"; |
| 1504 | } |
| 1505 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1506 | RENDERTHREAD_TEST(FrameBuilder, renderPropOverlappingRenderingAlpha) { |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 1507 | testProperty([](RenderProperties& properties) { |
| 1508 | properties.setAlpha(0.5f); |
| 1509 | properties.setHasOverlappingRendering(false); |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 1510 | }, [](const RectOp& op, const BakedOpState& state) { |
| 1511 | EXPECT_EQ(0.5f, state.alpha) << "Alpha should be applied directly to op"; |
| 1512 | }); |
| 1513 | } |
| 1514 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1515 | RENDERTHREAD_TEST(FrameBuilder, renderPropClipping) { |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 1516 | testProperty([](RenderProperties& properties) { |
| 1517 | properties.setClipToBounds(true); |
| 1518 | properties.setClipBounds(Rect(10, 20, 300, 400)); |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 1519 | }, [](const RectOp& op, const BakedOpState& state) { |
| 1520 | EXPECT_EQ(Rect(10, 20, 100, 100), state.computedState.clippedBounds) |
| 1521 | << "Clip rect should be intersection of node bounds and clip bounds"; |
| 1522 | }); |
| 1523 | } |
| 1524 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1525 | RENDERTHREAD_TEST(FrameBuilder, renderPropRevealClip) { |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 1526 | testProperty([](RenderProperties& properties) { |
| 1527 | properties.mutableRevealClip().set(true, 50, 50, 25); |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 1528 | }, [](const RectOp& op, const BakedOpState& state) { |
| 1529 | ASSERT_NE(nullptr, state.roundRectClipState); |
| 1530 | EXPECT_TRUE(state.roundRectClipState->highPriority); |
| 1531 | EXPECT_EQ(25, state.roundRectClipState->radius); |
| 1532 | EXPECT_EQ(Rect(50, 50, 50, 50), state.roundRectClipState->innerRect); |
| 1533 | }); |
| 1534 | } |
| 1535 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1536 | RENDERTHREAD_TEST(FrameBuilder, renderPropOutlineClip) { |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 1537 | testProperty([](RenderProperties& properties) { |
| 1538 | properties.mutableOutline().setShouldClip(true); |
| 1539 | properties.mutableOutline().setRoundRect(10, 20, 30, 40, 5.0f, 0.5f); |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 1540 | }, [](const RectOp& op, const BakedOpState& state) { |
| 1541 | ASSERT_NE(nullptr, state.roundRectClipState); |
| 1542 | EXPECT_FALSE(state.roundRectClipState->highPriority); |
| 1543 | EXPECT_EQ(5, state.roundRectClipState->radius); |
| 1544 | EXPECT_EQ(Rect(15, 25, 25, 35), state.roundRectClipState->innerRect); |
| 1545 | }); |
| 1546 | } |
| 1547 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1548 | RENDERTHREAD_TEST(FrameBuilder, renderPropTransform) { |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 1549 | testProperty([](RenderProperties& properties) { |
| 1550 | properties.setLeftTopRightBottom(10, 10, 110, 110); |
| 1551 | |
| 1552 | SkMatrix staticMatrix = SkMatrix::MakeScale(1.2f, 1.2f); |
| 1553 | properties.setStaticMatrix(&staticMatrix); |
| 1554 | |
| 1555 | // ignored, since static overrides animation |
| 1556 | SkMatrix animationMatrix = SkMatrix::MakeTrans(15, 15); |
| 1557 | properties.setAnimationMatrix(&animationMatrix); |
| 1558 | |
| 1559 | properties.setTranslationX(10); |
| 1560 | properties.setTranslationY(20); |
| 1561 | properties.setScaleX(0.5f); |
| 1562 | properties.setScaleY(0.7f); |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 1563 | }, [](const RectOp& op, const BakedOpState& state) { |
| 1564 | Matrix4 matrix; |
| 1565 | matrix.loadTranslate(10, 10, 0); // left, top |
| 1566 | matrix.scale(1.2f, 1.2f, 1); // static matrix |
| 1567 | // ignore animation matrix, since static overrides it |
| 1568 | |
| 1569 | // translation xy |
| 1570 | matrix.translate(10, 20); |
| 1571 | |
| 1572 | // scale xy (from default pivot - center) |
| 1573 | matrix.translate(50, 50); |
| 1574 | matrix.scale(0.5f, 0.7f, 1); |
| 1575 | matrix.translate(-50, -50); |
| 1576 | EXPECT_MATRIX_APPROX_EQ(matrix, state.computedState.transform) |
| 1577 | << "Op draw matrix must match expected combination of transformation properties"; |
| 1578 | }); |
| 1579 | } |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 1580 | |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 1581 | struct SaveLayerAlphaData { |
| 1582 | uint32_t layerWidth = 0; |
| 1583 | uint32_t layerHeight = 0; |
| 1584 | Rect rectClippedBounds; |
| 1585 | Matrix4 rectMatrix; |
| 1586 | }; |
| 1587 | /** |
| 1588 | * Constructs a view to hit the temporary layer alpha property implementation: |
| 1589 | * a) 0 < alpha < 1 |
| 1590 | * b) too big for layer (larger than maxTextureSize) |
| 1591 | * c) overlapping rendering content |
| 1592 | * returning observed data about layer size and content clip/transform. |
| 1593 | * |
| 1594 | * Used to validate clipping behavior of temporary layer, where requested layer size is reduced |
| 1595 | * (for efficiency, and to fit in layer size constraints) based on parent clip. |
| 1596 | */ |
| 1597 | void testSaveLayerAlphaClip(SaveLayerAlphaData* outObservedData, |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 1598 | std::function<void(RenderProperties&)> propSetupCallback) { |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 1599 | class SaveLayerAlphaClipTestRenderer : public TestRendererBase { |
| 1600 | public: |
| 1601 | SaveLayerAlphaClipTestRenderer(SaveLayerAlphaData* outData) |
| 1602 | : mOutData(outData) {} |
| 1603 | |
| 1604 | OffscreenBuffer* startTemporaryLayer(uint32_t width, uint32_t height) override { |
| 1605 | EXPECT_EQ(0, mIndex++); |
| 1606 | mOutData->layerWidth = width; |
| 1607 | mOutData->layerHeight = height; |
| 1608 | return nullptr; |
| 1609 | } |
| 1610 | void onRectOp(const RectOp& op, const BakedOpState& state) override { |
| 1611 | EXPECT_EQ(1, mIndex++); |
| 1612 | |
| 1613 | mOutData->rectClippedBounds = state.computedState.clippedBounds; |
| 1614 | mOutData->rectMatrix = state.computedState.transform; |
| 1615 | } |
| 1616 | void endLayer() override { |
| 1617 | EXPECT_EQ(2, mIndex++); |
| 1618 | } |
| 1619 | void onLayerOp(const LayerOp& op, const BakedOpState& state) override { |
| 1620 | EXPECT_EQ(3, mIndex++); |
| 1621 | } |
| 1622 | private: |
| 1623 | SaveLayerAlphaData* mOutData; |
| 1624 | }; |
| 1625 | |
| 1626 | ASSERT_GT(10000, DeviceInfo::get()->maxTextureSize()) |
| 1627 | << "Node must be bigger than max texture size to exercise saveLayer codepath"; |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 1628 | auto node = TestUtils::createNode(0, 0, 10000, 10000, |
| 1629 | [&propSetupCallback](RenderProperties& properties, RecordingCanvas& canvas) { |
| 1630 | properties.setHasOverlappingRendering(true); |
| 1631 | properties.setAlpha(0.5f); // force saveLayer, since too big for HW layer |
| 1632 | // apply other properties |
| 1633 | propSetupCallback(properties); |
| 1634 | |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 1635 | SkPaint paint; |
| 1636 | paint.setColor(SK_ColorWHITE); |
| 1637 | canvas.drawRect(0, 0, 10000, 10000, paint); |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 1638 | }); |
John Reck | 7db5ffb | 2016-01-15 13:17:09 -0800 | [diff] [blame] | 1639 | auto nodes = TestUtils::createSyncedNodeList(node); // sync before querying height |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 1640 | |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 1641 | FrameBuilder frameBuilder(sEmptyLayerUpdateQueue, SkRect::MakeWH(200, 200), 200, 200, |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1642 | nodes, sLightGeometry, Caches::getInstance()); |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 1643 | SaveLayerAlphaClipTestRenderer renderer(outObservedData); |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 1644 | frameBuilder.replayBakedOps<TestDispatcher>(renderer); |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 1645 | |
| 1646 | // assert, since output won't be valid if we haven't seen a save layer triggered |
| 1647 | ASSERT_EQ(4, renderer.getIndex()) << "Test must trigger saveLayer alpha behavior."; |
| 1648 | } |
| 1649 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1650 | RENDERTHREAD_TEST(FrameBuilder, renderPropSaveLayerAlphaClipBig) { |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 1651 | SaveLayerAlphaData observedData; |
| 1652 | testSaveLayerAlphaClip(&observedData, [](RenderProperties& properties) { |
| 1653 | properties.setTranslationX(10); // offset rendering content |
| 1654 | properties.setTranslationY(-2000); // offset rendering content |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 1655 | }); |
| 1656 | EXPECT_EQ(190u, observedData.layerWidth); |
| 1657 | EXPECT_EQ(200u, observedData.layerHeight); |
Chris Craik | 5430ab2 | 2015-12-10 16:28:16 -0800 | [diff] [blame] | 1658 | EXPECT_EQ(Rect(190, 200), observedData.rectClippedBounds) |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 1659 | << "expect content to be clipped to screen area"; |
| 1660 | Matrix4 expected; |
| 1661 | expected.loadTranslate(0, -2000, 0); |
| 1662 | EXPECT_MATRIX_APPROX_EQ(expected, observedData.rectMatrix) |
| 1663 | << "expect content to be translated as part of being clipped"; |
| 1664 | } |
| 1665 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1666 | RENDERTHREAD_TEST(FrameBuilder, renderPropSaveLayerAlphaRotate) { |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 1667 | SaveLayerAlphaData observedData; |
| 1668 | testSaveLayerAlphaClip(&observedData, [](RenderProperties& properties) { |
| 1669 | // Translate and rotate the view so that the only visible part is the top left corner of |
Chris Craik | 8d1f212 | 2015-11-24 16:40:09 -0800 | [diff] [blame] | 1670 | // the view. It will form an isosceles right triangle with a long side length of 200 at the |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 1671 | // bottom of the viewport. |
| 1672 | properties.setTranslationX(100); |
| 1673 | properties.setTranslationY(100); |
| 1674 | properties.setPivotX(0); |
| 1675 | properties.setPivotY(0); |
| 1676 | properties.setRotation(45); |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 1677 | }); |
| 1678 | // ceil(sqrt(2) / 2 * 200) = 142 |
| 1679 | EXPECT_EQ(142u, observedData.layerWidth); |
| 1680 | EXPECT_EQ(142u, observedData.layerHeight); |
Chris Craik | 5430ab2 | 2015-12-10 16:28:16 -0800 | [diff] [blame] | 1681 | EXPECT_EQ(Rect(142, 142), observedData.rectClippedBounds); |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 1682 | EXPECT_MATRIX_APPROX_EQ(Matrix4::identity(), observedData.rectMatrix); |
| 1683 | } |
| 1684 | |
Chris Craik | 3a5811b | 2016-03-22 15:03:08 -0700 | [diff] [blame] | 1685 | RENDERTHREAD_TEST(FrameBuilder, renderPropSaveLayerAlphaScale) { |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 1686 | SaveLayerAlphaData observedData; |
| 1687 | testSaveLayerAlphaClip(&observedData, [](RenderProperties& properties) { |
| 1688 | properties.setPivotX(0); |
| 1689 | properties.setPivotY(0); |
| 1690 | properties.setScaleX(2); |
| 1691 | properties.setScaleY(0.5f); |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 1692 | }); |
| 1693 | EXPECT_EQ(100u, observedData.layerWidth); |
| 1694 | EXPECT_EQ(400u, observedData.layerHeight); |
Chris Craik | 5430ab2 | 2015-12-10 16:28:16 -0800 | [diff] [blame] | 1695 | EXPECT_EQ(Rect(100, 400), observedData.rectClippedBounds); |
Chris Craik | 8ecf41c | 2015-11-16 10:27:59 -0800 | [diff] [blame] | 1696 | EXPECT_MATRIX_APPROX_EQ(Matrix4::identity(), observedData.rectMatrix); |
| 1697 | } |
| 1698 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 1699 | } // namespace uirenderer |
| 1700 | } // namespace android |