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