Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 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 "tests/common/TestUtils.h" |
| 20 | #include <GrRectanizer.h> |
| 21 | #include "pipeline/skia/VectorDrawableAtlas.h" |
| 22 | |
| 23 | using namespace android; |
| 24 | using namespace android::uirenderer; |
| 25 | using namespace android::uirenderer::renderthread; |
| 26 | using namespace android::uirenderer::skiapipeline; |
| 27 | |
| 28 | RENDERTHREAD_SKIA_PIPELINE_TEST(VectorDrawableAtlas, addGetRemove) { |
| 29 | VectorDrawableAtlas atlas(100*100); |
| 30 | atlas.prepareForDraw(renderThread.getGrContext()); |
| 31 | //create 150 rects 10x10, which won't fit in the atlas (atlas can fit no more than 100 rects) |
| 32 | const int MAX_RECTS = 150; |
| 33 | AtlasEntry VDRects[MAX_RECTS]; |
| 34 | |
| 35 | sk_sp<SkSurface> atlasSurface; |
| 36 | |
| 37 | //check we are able to allocate new rects |
| 38 | //check that rects in the atlas do not intersect |
| 39 | for (uint32_t i = 0; i < MAX_RECTS; i++) { |
| 40 | VDRects[i] = atlas.requestNewEntry(10, 10, renderThread.getGrContext()); |
| 41 | if (0 == i) { |
| 42 | atlasSurface = VDRects[0].surface; |
| 43 | } |
| 44 | ASSERT_TRUE(VDRects[i].key != INVALID_ATLAS_KEY); |
| 45 | ASSERT_TRUE(VDRects[i].surface.get() != nullptr); |
| 46 | ASSERT_TRUE(VDRects[i].rect.width() == 10 && VDRects[i].rect.height() == 10); |
| 47 | |
| 48 | //nothing in the atlas should intersect |
| 49 | if (atlasSurface.get() == VDRects[i].surface.get()) { |
| 50 | for (uint32_t j = 0; j < i; j++) { |
| 51 | if (atlasSurface.get() == VDRects[j].surface.get()) { |
| 52 | ASSERT_FALSE(VDRects[i].rect.intersect(VDRects[j].rect)); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | //first 1/3 rects should all be in the same surface |
| 59 | for (uint32_t i = 1; i < MAX_RECTS/3; i++) { |
| 60 | ASSERT_NE(VDRects[i].key, VDRects[0].key); |
| 61 | ASSERT_EQ(VDRects[i].surface.get(), atlasSurface.get()); |
| 62 | } |
| 63 | |
| 64 | //first rect is using atlas and last is a standalone surface |
| 65 | ASSERT_NE(VDRects[0].surface.get(), VDRects[MAX_RECTS-1].surface.get()); |
| 66 | |
| 67 | //check getEntry returns the same surfaces that we had created |
| 68 | for (uint32_t i = 0; i < MAX_RECTS; i++) { |
| 69 | auto VDRect = atlas.getEntry(VDRects[i].key); |
| 70 | ASSERT_TRUE(VDRect.key != INVALID_ATLAS_KEY); |
| 71 | ASSERT_EQ(VDRects[i].key, VDRect.key); |
| 72 | ASSERT_EQ(VDRects[i].surface.get(), VDRect.surface.get()); |
| 73 | ASSERT_EQ(VDRects[i].rect, VDRect.rect); |
| 74 | atlas.releaseEntry(VDRect.key); |
| 75 | } |
| 76 | |
| 77 | //check that any new rects will be allocated in the atlas, even that rectanizer is full. |
| 78 | //rects in the atlas should not intersect. |
| 79 | for (uint32_t i = 0; i < MAX_RECTS/3; i++) { |
| 80 | VDRects[i] = atlas.requestNewEntry(10, 10, renderThread.getGrContext()); |
| 81 | ASSERT_TRUE(VDRects[i].key != INVALID_ATLAS_KEY); |
| 82 | ASSERT_EQ(VDRects[i].surface.get(), atlasSurface.get()); |
| 83 | ASSERT_TRUE(VDRects[i].rect.width() == 10 && VDRects[i].rect.height() == 10); |
| 84 | for (uint32_t j = 0; j < i; j++) { |
| 85 | ASSERT_FALSE(VDRects[i].rect.intersect(VDRects[j].rect)); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | |
| 91 | RENDERTHREAD_SKIA_PIPELINE_TEST(VectorDrawableAtlas, disallowSharedSurface) { |
| 92 | VectorDrawableAtlas atlas(100*100); |
| 93 | //don't allow to use a shared surface |
| 94 | atlas.setStorageMode(VectorDrawableAtlas::StorageMode::disallowSharedSurface); |
| 95 | atlas.prepareForDraw(renderThread.getGrContext()); |
| 96 | //create 150 rects 10x10, which won't fit in the atlas (atlas can fit no more than 100 rects) |
| 97 | const int MAX_RECTS = 150; |
| 98 | AtlasEntry VDRects[MAX_RECTS]; |
| 99 | |
| 100 | //check we are able to allocate new rects |
| 101 | //check that rects in the atlas use unique surfaces |
| 102 | for (uint32_t i = 0; i < MAX_RECTS; i++) { |
| 103 | VDRects[i] = atlas.requestNewEntry(10, 10, renderThread.getGrContext()); |
| 104 | ASSERT_TRUE(VDRects[i].key != INVALID_ATLAS_KEY); |
| 105 | ASSERT_TRUE(VDRects[i].surface.get() != nullptr); |
| 106 | ASSERT_TRUE(VDRects[i].rect.width() == 10 && VDRects[i].rect.height() == 10); |
| 107 | |
| 108 | //nothing in the atlas should use the same surface |
| 109 | for (uint32_t j = 0; j < i; j++) { |
| 110 | ASSERT_NE(VDRects[i].surface.get(), VDRects[j].surface.get()); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | RENDERTHREAD_SKIA_PIPELINE_TEST(VectorDrawableAtlas, repack) { |
| 116 | VectorDrawableAtlas atlas(100*100); |
| 117 | ASSERT_FALSE(atlas.isFragmented()); |
| 118 | atlas.prepareForDraw(renderThread.getGrContext()); |
| 119 | ASSERT_FALSE(atlas.isFragmented()); |
| 120 | //create 150 rects 10x10, which won't fit in the atlas (atlas can fit no more than 100 rects) |
| 121 | const int MAX_RECTS = 150; |
| 122 | AtlasEntry VDRects[MAX_RECTS]; |
| 123 | |
| 124 | sk_sp<SkSurface> atlasSurface; |
| 125 | |
| 126 | //fill the atlas with check we are able to allocate new rects |
| 127 | for (uint32_t i = 0; i < MAX_RECTS; i++) { |
| 128 | VDRects[i] = atlas.requestNewEntry(10, 10, renderThread.getGrContext()); |
| 129 | if (0 == i) { |
| 130 | atlasSurface = VDRects[0].surface; |
| 131 | } |
| 132 | ASSERT_TRUE(VDRects[i].key != INVALID_ATLAS_KEY); |
| 133 | } |
| 134 | |
| 135 | ASSERT_FALSE(atlas.isFragmented()); |
| 136 | |
| 137 | //first 1/3 rects should all be in the same surface |
| 138 | for (uint32_t i = 1; i < MAX_RECTS/3; i++) { |
| 139 | ASSERT_NE(VDRects[i].key, VDRects[0].key); |
| 140 | ASSERT_EQ(VDRects[i].surface.get(), atlasSurface.get()); |
| 141 | } |
| 142 | |
| 143 | //release all entries |
| 144 | for (uint32_t i = 0; i < MAX_RECTS; i++) { |
| 145 | auto VDRect = atlas.getEntry(VDRects[i].key); |
| 146 | ASSERT_TRUE(VDRect.key != INVALID_ATLAS_KEY); |
| 147 | atlas.releaseEntry(VDRect.key); |
| 148 | } |
| 149 | |
| 150 | ASSERT_FALSE(atlas.isFragmented()); |
| 151 | |
| 152 | //allocate 4x4 rects, which will fragment the atlas badly, because each entry occupies a 10x10 |
| 153 | //area |
| 154 | for (uint32_t i = 0; i < 4*MAX_RECTS; i++) { |
| 155 | AtlasEntry entry = atlas.requestNewEntry(4, 4, renderThread.getGrContext()); |
| 156 | ASSERT_TRUE(entry.key != INVALID_ATLAS_KEY); |
| 157 | } |
| 158 | |
| 159 | ASSERT_TRUE(atlas.isFragmented()); |
| 160 | |
| 161 | atlas.repackIfNeeded(renderThread.getGrContext()); |
| 162 | |
| 163 | ASSERT_FALSE(atlas.isFragmented()); |
| 164 | } |