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 | |
Kevin Lubick | 09a7709 | 2023-10-31 14:58:26 +0000 | [diff] [blame] | 22 | #include <hwui/MinikinSkia.h> |
| 23 | #include <hwui/Typeface.h> |
Seigo Nonaka | 7c93e86 | 2017-10-25 16:34:48 -0700 | [diff] [blame] | 24 | #include <minikin/Layout.h> |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 25 | #include <pipeline/skia/SkiaOpenGLPipeline.h> |
| 26 | #include <pipeline/skia/SkiaVulkanPipeline.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 27 | #include <renderthread/EglManager.h> |
Greg Daniel | 98c78dad | 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 | |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 31 | #include "SkCanvas.h" |
Mike Reed | 3ad4f8a | 2018-05-02 15:46:56 -0400 | [diff] [blame] | 32 | #include "SkColorData.h" |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 33 | #include "SkMatrix.h" |
| 34 | #include "SkPath.h" |
| 35 | #include "SkPixmap.h" |
| 36 | #include "SkRect.h" |
| 37 | #include "SkSurface.h" |
Leon Scroggins III | 4f354e9 | 2018-05-11 13:19:37 -0400 | [diff] [blame] | 38 | #include "SkUnPreMultiply.h" |
Leon Scroggins III | ee708fa | 2016-12-12 15:31:39 -0500 | [diff] [blame] | 39 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 40 | namespace android { |
| 41 | namespace uirenderer { |
| 42 | |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 43 | std::unordered_map<int, TestUtils::CallCounts> TestUtils::sMockFunctorCounts{}; |
| 44 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 45 | SkColor TestUtils::interpolateColor(float fraction, SkColor start, SkColor end) { |
| 46 | int startA = (start >> 24) & 0xff; |
| 47 | int startR = (start >> 16) & 0xff; |
| 48 | int startG = (start >> 8) & 0xff; |
| 49 | int startB = start & 0xff; |
| 50 | |
| 51 | int endA = (end >> 24) & 0xff; |
| 52 | int endR = (end >> 16) & 0xff; |
| 53 | int endG = (end >> 8) & 0xff; |
| 54 | int endB = end & 0xff; |
| 55 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 56 | return (int)((startA + (int)(fraction * (endA - startA))) << 24) | |
| 57 | (int)((startR + (int)(fraction * (endR - startR))) << 16) | |
| 58 | (int)((startG + (int)(fraction * (endG - startG))) << 8) | |
| 59 | (int)((startB + (int)(fraction * (endB - startB)))); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 62 | sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater( |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 63 | renderthread::RenderThread& renderThread) { |
| 64 | android::uirenderer::renderthread::IRenderPipeline* pipeline; |
John Reck | 18f442e | 2018-04-09 16:56:34 -0700 | [diff] [blame] | 65 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) { |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 66 | pipeline = new skiapipeline::SkiaOpenGLPipeline(renderThread); |
| 67 | } else { |
| 68 | pipeline = new skiapipeline::SkiaVulkanPipeline(renderThread); |
| 69 | } |
| 70 | sp<DeferredLayerUpdater> layerUpdater = pipeline->createTextureLayer(); |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 71 | layerUpdater->apply(); |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 72 | delete pipeline; |
| 73 | return layerUpdater; |
| 74 | } |
| 75 | |
| 76 | sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater( |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 77 | renderthread::RenderThread& renderThread, uint32_t width, uint32_t height, |
Chris Craik | 243e85b | 2016-03-25 15:26:11 -0700 | [diff] [blame] | 78 | const SkMatrix& transform) { |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 79 | sp<DeferredLayerUpdater> layerUpdater = createTextureLayerUpdater(renderThread); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 80 | layerUpdater->backingLayer()->getTransform() = transform; |
Chris Craik | 243e85b | 2016-03-25 15:26:11 -0700 | [diff] [blame] | 81 | layerUpdater->setSize(width, height); |
| 82 | layerUpdater->setTransform(&transform); |
| 83 | |
| 84 | // updateLayer so it's ready to draw |
ramindani | 3952ed6 | 2021-08-12 15:55:12 +0000 | [diff] [blame] | 85 | layerUpdater->updateLayer(true, nullptr, 0, SkRect::MakeEmpty()); |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 86 | return layerUpdater; |
| 87 | } |
| 88 | |
Mike Reed | f6d86ac | 2019-01-18 14:13:23 -0500 | [diff] [blame] | 89 | void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint, float x, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 90 | float y) { |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 91 | auto utf16 = asciiToUtf16(text); |
Seigo Nonaka | 3a4217f | 2018-05-02 12:56:16 -0700 | [diff] [blame] | 92 | uint32_t length = strlen(text); |
Mike Reed | f6d86ac | 2019-01-18 14:13:23 -0500 | [diff] [blame] | 93 | |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 94 | canvas->drawText(utf16.get(), length, // text buffer |
| 95 | 0, length, // draw range |
| 96 | 0, length, // context range |
Mike Reed | f6d86ac | 2019-01-18 14:13:23 -0500 | [diff] [blame] | 97 | x, y, minikin::Bidi::LTR, paint, nullptr, nullptr /* measured text */); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Mike Reed | f6d86ac | 2019-01-18 14:13:23 -0500 | [diff] [blame] | 100 | void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 101 | const SkPath& path) { |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 102 | auto utf16 = asciiToUtf16(text); |
Mike Reed | f6d86ac | 2019-01-18 14:13:23 -0500 | [diff] [blame] | 103 | canvas->drawTextOnPath(utf16.get(), strlen(text), minikin::Bidi::LTR, path, 0, 0, paint, |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 104 | nullptr); |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 105 | } |
| 106 | |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 107 | void TestUtils::TestTask::run() { |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 108 | // RenderState only valid once RenderThread is running, so queried here |
sergeyv | 7dc370b | 2016-06-17 11:21:11 -0700 | [diff] [blame] | 109 | renderthread::RenderThread& renderThread = renderthread::RenderThread::getInstance(); |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 110 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) { |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 111 | renderThread.requireVkContext(); |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 112 | } else { |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 113 | renderThread.requireGlContext(); |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 114 | } |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 115 | |
sergeyv | 7dc370b | 2016-06-17 11:21:11 -0700 | [diff] [blame] | 116 | rtCallback(renderThread); |
Derek Sollenberger | 56ad6ec | 2016-07-22 12:13:32 -0400 | [diff] [blame] | 117 | |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 118 | renderThread.destroyRenderingContext(); |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 121 | std::unique_ptr<uint16_t[]> TestUtils::asciiToUtf16(const char* str) { |
| 122 | const int length = strlen(str); |
| 123 | std::unique_ptr<uint16_t[]> utf16(new uint16_t[length]); |
| 124 | for (int i = 0; i < length; i++) { |
| 125 | utf16.get()[i] = str[i]; |
| 126 | } |
| 127 | return utf16; |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 130 | SkColor TestUtils::getColor(const sk_sp<SkSurface>& surface, int x, int y) { |
| 131 | SkPixmap pixmap; |
| 132 | if (!surface->peekPixels(&pixmap)) { |
| 133 | return 0; |
| 134 | } |
| 135 | switch (pixmap.colorType()) { |
| 136 | case kGray_8_SkColorType: { |
| 137 | const uint8_t* addr = pixmap.addr8(x, y); |
| 138 | return SkColorSetRGB(*addr, *addr, *addr); |
| 139 | } |
| 140 | case kAlpha_8_SkColorType: { |
| 141 | const uint8_t* addr = pixmap.addr8(x, y); |
| 142 | return SkColorSetA(0, addr[0]); |
| 143 | } |
| 144 | case kRGB_565_SkColorType: { |
| 145 | const uint16_t* addr = pixmap.addr16(x, y); |
| 146 | return SkPixel16ToColor(addr[0]); |
| 147 | } |
| 148 | case kARGB_4444_SkColorType: { |
| 149 | const uint16_t* addr = pixmap.addr16(x, y); |
| 150 | SkPMColor c = SkPixel4444ToPixel32(addr[0]); |
| 151 | return SkUnPreMultiply::PMColorToColor(c); |
| 152 | } |
| 153 | case kBGRA_8888_SkColorType: { |
| 154 | const uint32_t* addr = pixmap.addr32(x, y); |
| 155 | SkPMColor c = SkSwizzle_BGRA_to_PMColor(addr[0]); |
| 156 | return SkUnPreMultiply::PMColorToColor(c); |
| 157 | } |
| 158 | case kRGBA_8888_SkColorType: { |
| 159 | const uint32_t* addr = pixmap.addr32(x, y); |
| 160 | SkPMColor c = SkSwizzle_RGBA_to_PMColor(addr[0]); |
| 161 | return SkUnPreMultiply::PMColorToColor(c); |
| 162 | } |
| 163 | default: |
| 164 | return 0; |
| 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 169 | SkRect TestUtils::getClipBounds(const SkCanvas* canvas) { |
Mike Reed | 5e43898 | 2017-01-25 08:23:25 -0500 | [diff] [blame] | 170 | return SkRect::Make(canvas->getDeviceClipBounds()); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | SkRect TestUtils::getLocalClipBounds(const SkCanvas* canvas) { |
| 174 | SkMatrix invertedTotalMatrix; |
| 175 | if (!canvas->getTotalMatrix().invert(&invertedTotalMatrix)) { |
| 176 | return SkRect::MakeEmpty(); |
| 177 | } |
| 178 | SkRect outlineInDeviceCoord = TestUtils::getClipBounds(canvas); |
| 179 | SkRect outlineInLocalCoord; |
| 180 | invertedTotalMatrix.mapRect(&outlineInLocalCoord, outlineInDeviceCoord); |
| 181 | return outlineInLocalCoord; |
| 182 | } |
| 183 | |
Kevin Lubick | 09a7709 | 2023-10-31 14:58:26 +0000 | [diff] [blame] | 184 | SkFont TestUtils::defaultFont() { |
| 185 | const std::shared_ptr<minikin::MinikinFont>& minikinFont = |
| 186 | Typeface::resolveDefault(nullptr)->fFontCollection->getFamilyAt(0)->getFont(0)->baseTypeface(); |
| 187 | SkTypeface* skTypeface = reinterpret_cast<const MinikinFontSkia*>(minikinFont.get())->GetSkTypeface(); |
| 188 | LOG_ALWAYS_FATAL_IF(skTypeface == nullptr); |
| 189 | return SkFont(sk_ref_sp(skTypeface)); |
| 190 | } |
| 191 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 192 | } /* namespace uirenderer */ |
| 193 | } /* namespace android */ |