John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "TestUtils.h" |
| 18 | |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 19 | #include "hwui/Paint.h" |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 20 | #include "DeferredLayerUpdater.h" |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 21 | |
sergeyv | 7dc370b | 2016-06-17 11:21:11 -0700 | [diff] [blame] | 22 | #include <renderthread/EglManager.h> |
Derek Sollenberger | 56ad6ec | 2016-07-22 12:13:32 -0400 | [diff] [blame] | 23 | #include <renderthread/OpenGLPipeline.h> |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 24 | #include <pipeline/skia/SkiaOpenGLPipeline.h> |
| 25 | #include <pipeline/skia/SkiaVulkanPipeline.h> |
| 26 | #include <renderthread/VulkanManager.h> |
Ben Wagner | f4cf5d3 | 2016-02-22 15:25:35 -0500 | [diff] [blame] | 27 | #include <utils/Unicode.h> |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 28 | #include <SkClipStack.h> |
Ben Wagner | f4cf5d3 | 2016-02-22 15:25:35 -0500 | [diff] [blame] | 29 | |
Leon Scroggins III | ee708fa | 2016-12-12 15:31:39 -0500 | [diff] [blame] | 30 | #include <SkGlyphCache.h> |
| 31 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 32 | namespace android { |
| 33 | namespace uirenderer { |
| 34 | |
| 35 | SkColor TestUtils::interpolateColor(float fraction, SkColor start, SkColor end) { |
| 36 | int startA = (start >> 24) & 0xff; |
| 37 | int startR = (start >> 16) & 0xff; |
| 38 | int startG = (start >> 8) & 0xff; |
| 39 | int startB = start & 0xff; |
| 40 | |
| 41 | int endA = (end >> 24) & 0xff; |
| 42 | int endR = (end >> 16) & 0xff; |
| 43 | int endG = (end >> 8) & 0xff; |
| 44 | int endB = end & 0xff; |
| 45 | |
| 46 | return (int)((startA + (int)(fraction * (endA - startA))) << 24) |
| 47 | | (int)((startR + (int)(fraction * (endR - startR))) << 16) |
| 48 | | (int)((startG + (int)(fraction * (endG - startG))) << 8) |
| 49 | | (int)((startB + (int)(fraction * (endB - startB)))); |
| 50 | } |
| 51 | |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 52 | sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater( |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 53 | renderthread::RenderThread& renderThread) { |
| 54 | android::uirenderer::renderthread::IRenderPipeline* pipeline; |
| 55 | if (Properties::getRenderPipelineType() == RenderPipelineType::OpenGL) { |
| 56 | pipeline = new renderthread::OpenGLPipeline(renderThread); |
| 57 | } else if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) { |
| 58 | pipeline = new skiapipeline::SkiaOpenGLPipeline(renderThread); |
| 59 | } else { |
| 60 | pipeline = new skiapipeline::SkiaVulkanPipeline(renderThread); |
| 61 | } |
| 62 | sp<DeferredLayerUpdater> layerUpdater = pipeline->createTextureLayer(); |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame^] | 63 | layerUpdater->apply(); |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 64 | delete pipeline; |
| 65 | return layerUpdater; |
| 66 | } |
| 67 | |
| 68 | sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater( |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 69 | renderthread::RenderThread& renderThread, uint32_t width, uint32_t height, |
Chris Craik | 243e85b | 2016-03-25 15:26:11 -0700 | [diff] [blame] | 70 | const SkMatrix& transform) { |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 71 | sp<DeferredLayerUpdater> layerUpdater = createTextureLayerUpdater(renderThread); |
Derek Sollenberger | 56ad6ec | 2016-07-22 12:13:32 -0400 | [diff] [blame] | 72 | layerUpdater->backingLayer()->getTransform().load(transform); |
Chris Craik | 243e85b | 2016-03-25 15:26:11 -0700 | [diff] [blame] | 73 | layerUpdater->setSize(width, height); |
| 74 | layerUpdater->setTransform(&transform); |
| 75 | |
| 76 | // updateLayer so it's ready to draw |
Derek Sollenberger | 56ad6ec | 2016-07-22 12:13:32 -0400 | [diff] [blame] | 77 | layerUpdater->updateLayer(true, GL_TEXTURE_EXTERNAL_OES, Matrix4::identity().data); |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 78 | return layerUpdater; |
| 79 | } |
| 80 | |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 81 | void TestUtils::layoutTextUnscaled(const SkPaint& paint, const char* text, |
| 82 | std::vector<glyph_t>* outGlyphs, std::vector<float>* outPositions, |
| 83 | float* outTotalAdvance, Rect* outBounds) { |
| 84 | Rect bounds; |
| 85 | float totalAdvance = 0; |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 86 | SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 87 | SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I()); |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 88 | while (*text != '\0') { |
Ben Wagner | f4cf5d3 | 2016-02-22 15:25:35 -0500 | [diff] [blame] | 89 | size_t nextIndex = 0; |
| 90 | int32_t unichar = utf32_from_utf8_at(text, 4, 0, &nextIndex); |
| 91 | text += nextIndex; |
| 92 | |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 93 | glyph_t glyph = autoCache.getCache()->unicharToGlyph(unichar); |
| 94 | autoCache.getCache()->unicharToGlyph(unichar); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 95 | |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 96 | // push glyph and its relative position |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 97 | outGlyphs->push_back(glyph); |
| 98 | outPositions->push_back(totalAdvance); |
| 99 | outPositions->push_back(0); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 100 | |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 101 | // compute bounds |
| 102 | SkGlyph skGlyph = autoCache.getCache()->getUnicharMetrics(unichar); |
| 103 | Rect glyphBounds(skGlyph.fWidth, skGlyph.fHeight); |
| 104 | glyphBounds.translate(totalAdvance + skGlyph.fLeft, skGlyph.fTop); |
| 105 | bounds.unionWith(glyphBounds); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 106 | |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 107 | // advance next character |
| 108 | SkScalar skWidth; |
| 109 | paint.getTextWidths(&glyph, sizeof(glyph), &skWidth, NULL); |
| 110 | totalAdvance += skWidth; |
| 111 | } |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 112 | *outBounds = bounds; |
| 113 | *outTotalAdvance = totalAdvance; |
| 114 | } |
| 115 | |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 116 | |
| 117 | void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 118 | const SkPaint& paint, float x, float y) { |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 119 | auto utf16 = asciiToUtf16(text); |
| 120 | canvas->drawText(utf16.get(), 0, strlen(text), strlen(text), x, y, 0, paint, nullptr); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 123 | void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 124 | const SkPaint& paint, const SkPath& path) { |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 125 | auto utf16 = asciiToUtf16(text); |
| 126 | canvas->drawTextOnPath(utf16.get(), strlen(text), 0, path, 0, 0, paint, nullptr); |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 127 | } |
| 128 | |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 129 | void TestUtils::TestTask::run() { |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 130 | // RenderState only valid once RenderThread is running, so queried here |
sergeyv | 7dc370b | 2016-06-17 11:21:11 -0700 | [diff] [blame] | 131 | renderthread::RenderThread& renderThread = renderthread::RenderThread::getInstance(); |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 132 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) { |
| 133 | renderThread.vulkanManager().initialize(); |
| 134 | } else { |
| 135 | renderThread.eglManager().initialize(); |
| 136 | } |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 137 | |
sergeyv | 7dc370b | 2016-06-17 11:21:11 -0700 | [diff] [blame] | 138 | rtCallback(renderThread); |
Derek Sollenberger | 56ad6ec | 2016-07-22 12:13:32 -0400 | [diff] [blame] | 139 | |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 140 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) { |
| 141 | renderThread.vulkanManager().destroy(); |
| 142 | } else { |
| 143 | renderThread.renderState().flush(Caches::FlushMode::Full); |
| 144 | renderThread.eglManager().destroy(); |
| 145 | } |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 148 | std::unique_ptr<uint16_t[]> TestUtils::asciiToUtf16(const char* str) { |
| 149 | const int length = strlen(str); |
| 150 | std::unique_ptr<uint16_t[]> utf16(new uint16_t[length]); |
| 151 | for (int i = 0; i < length; i++) { |
| 152 | utf16.get()[i] = str[i]; |
| 153 | } |
| 154 | return utf16; |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 157 | SkColor TestUtils::getColor(const sk_sp<SkSurface>& surface, int x, int y) { |
| 158 | SkPixmap pixmap; |
| 159 | if (!surface->peekPixels(&pixmap)) { |
| 160 | return 0; |
| 161 | } |
| 162 | switch (pixmap.colorType()) { |
| 163 | case kGray_8_SkColorType: { |
| 164 | const uint8_t* addr = pixmap.addr8(x, y); |
| 165 | return SkColorSetRGB(*addr, *addr, *addr); |
| 166 | } |
| 167 | case kAlpha_8_SkColorType: { |
| 168 | const uint8_t* addr = pixmap.addr8(x, y); |
| 169 | return SkColorSetA(0, addr[0]); |
| 170 | } |
| 171 | case kRGB_565_SkColorType: { |
| 172 | const uint16_t* addr = pixmap.addr16(x, y); |
| 173 | return SkPixel16ToColor(addr[0]); |
| 174 | } |
| 175 | case kARGB_4444_SkColorType: { |
| 176 | const uint16_t* addr = pixmap.addr16(x, y); |
| 177 | SkPMColor c = SkPixel4444ToPixel32(addr[0]); |
| 178 | return SkUnPreMultiply::PMColorToColor(c); |
| 179 | } |
| 180 | case kBGRA_8888_SkColorType: { |
| 181 | const uint32_t* addr = pixmap.addr32(x, y); |
| 182 | SkPMColor c = SkSwizzle_BGRA_to_PMColor(addr[0]); |
| 183 | return SkUnPreMultiply::PMColorToColor(c); |
| 184 | } |
| 185 | case kRGBA_8888_SkColorType: { |
| 186 | const uint32_t* addr = pixmap.addr32(x, y); |
| 187 | SkPMColor c = SkSwizzle_RGBA_to_PMColor(addr[0]); |
| 188 | return SkUnPreMultiply::PMColorToColor(c); |
| 189 | } |
| 190 | default: |
| 191 | return 0; |
| 192 | } |
| 193 | return 0; |
| 194 | } |
| 195 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 196 | SkRect TestUtils::getClipBounds(const SkCanvas* canvas) { |
Mike Reed | 5e43898 | 2017-01-25 08:23:25 -0500 | [diff] [blame] | 197 | return SkRect::Make(canvas->getDeviceClipBounds()); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | SkRect TestUtils::getLocalClipBounds(const SkCanvas* canvas) { |
| 201 | SkMatrix invertedTotalMatrix; |
| 202 | if (!canvas->getTotalMatrix().invert(&invertedTotalMatrix)) { |
| 203 | return SkRect::MakeEmpty(); |
| 204 | } |
| 205 | SkRect outlineInDeviceCoord = TestUtils::getClipBounds(canvas); |
| 206 | SkRect outlineInLocalCoord; |
| 207 | invertedTotalMatrix.mapRect(&outlineInLocalCoord, outlineInDeviceCoord); |
| 208 | return outlineInLocalCoord; |
| 209 | } |
| 210 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 211 | } /* namespace uirenderer */ |
| 212 | } /* namespace android */ |