blob: a4890ede8faa5345cc6b99e39dba6842da029d77 [file] [log] [blame]
John Reck16c9d6a2015-11-17 15:51:08 -08001/*
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 Craikd2dfd8f2015-12-16 14:27:20 -080019#include "DeferredLayerUpdater.h"
John Reck1bcacfd2017-11-03 10:12:19 -070020#include "hwui/Paint.h"
Chris Craikd2dfd8f2015-12-16 14:27:20 -080021
Seigo Nonaka7c93e862017-10-25 16:34:48 -070022#include <minikin/Layout.h>
Greg Daniel98c78dad2017-01-04 14:45:56 -050023#include <pipeline/skia/SkiaOpenGLPipeline.h>
24#include <pipeline/skia/SkiaVulkanPipeline.h>
John Reck1bcacfd2017-11-03 10:12:19 -070025#include <renderthread/EglManager.h>
Greg Daniel98c78dad2017-01-04 14:45:56 -050026#include <renderthread/VulkanManager.h>
Ben Wagnerf4cf5d32016-02-22 15:25:35 -050027#include <utils/Unicode.h>
28
Kevin Lubick1175dc02022-02-28 12:41:27 -050029#include "SkCanvas.h"
Mike Reed3ad4f8a2018-05-02 15:46:56 -040030#include "SkColorData.h"
Kevin Lubick1175dc02022-02-28 12:41:27 -050031#include "SkMatrix.h"
32#include "SkPath.h"
33#include "SkPixmap.h"
34#include "SkRect.h"
35#include "SkSurface.h"
Leon Scroggins III4f354e92018-05-11 13:19:37 -040036#include "SkUnPreMultiply.h"
Leon Scroggins IIIee708fa2016-12-12 15:31:39 -050037
John Reck16c9d6a2015-11-17 15:51:08 -080038namespace android {
39namespace uirenderer {
40
John Reck283bb462018-12-13 16:40:14 -080041std::unordered_map<int, TestUtils::CallCounts> TestUtils::sMockFunctorCounts{};
42
John Reck16c9d6a2015-11-17 15:51:08 -080043SkColor TestUtils::interpolateColor(float fraction, SkColor start, SkColor end) {
44 int startA = (start >> 24) & 0xff;
45 int startR = (start >> 16) & 0xff;
46 int startG = (start >> 8) & 0xff;
47 int startB = start & 0xff;
48
49 int endA = (end >> 24) & 0xff;
50 int endR = (end >> 16) & 0xff;
51 int endG = (end >> 8) & 0xff;
52 int endB = end & 0xff;
53
John Reck1bcacfd2017-11-03 10:12:19 -070054 return (int)((startA + (int)(fraction * (endA - startA))) << 24) |
55 (int)((startR + (int)(fraction * (endR - startR))) << 16) |
56 (int)((startG + (int)(fraction * (endG - startG))) << 8) |
57 (int)((startB + (int)(fraction * (endB - startB))));
John Reck16c9d6a2015-11-17 15:51:08 -080058}
59
Chris Craikd2dfd8f2015-12-16 14:27:20 -080060sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
Greg Daniel98c78dad2017-01-04 14:45:56 -050061 renderthread::RenderThread& renderThread) {
62 android::uirenderer::renderthread::IRenderPipeline* pipeline;
John Reck18f442e2018-04-09 16:56:34 -070063 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
Greg Daniel98c78dad2017-01-04 14:45:56 -050064 pipeline = new skiapipeline::SkiaOpenGLPipeline(renderThread);
65 } else {
66 pipeline = new skiapipeline::SkiaVulkanPipeline(renderThread);
67 }
68 sp<DeferredLayerUpdater> layerUpdater = pipeline->createTextureLayer();
sergeyv3e9999b2017-01-19 15:37:02 -080069 layerUpdater->apply();
Greg Daniel98c78dad2017-01-04 14:45:56 -050070 delete pipeline;
71 return layerUpdater;
72}
73
74sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
Chris Craikd2dfd8f2015-12-16 14:27:20 -080075 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
Chris Craik243e85b2016-03-25 15:26:11 -070076 const SkMatrix& transform) {
Greg Daniel98c78dad2017-01-04 14:45:56 -050077 sp<DeferredLayerUpdater> layerUpdater = createTextureLayerUpdater(renderThread);
Stan Iliev564ca3e2018-09-04 22:00:00 +000078 layerUpdater->backingLayer()->getTransform() = transform;
Chris Craik243e85b2016-03-25 15:26:11 -070079 layerUpdater->setSize(width, height);
80 layerUpdater->setTransform(&transform);
81
82 // updateLayer so it's ready to draw
ramindani3952ed62021-08-12 15:55:12 +000083 layerUpdater->updateLayer(true, nullptr, 0, SkRect::MakeEmpty());
Chris Craikd2dfd8f2015-12-16 14:27:20 -080084 return layerUpdater;
85}
86
Mike Reedf6d86ac2019-01-18 14:13:23 -050087void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint, float x,
John Reck1bcacfd2017-11-03 10:12:19 -070088 float y) {
Derek Sollenberger79abbf22016-03-24 11:07:19 -040089 auto utf16 = asciiToUtf16(text);
Seigo Nonaka3a4217f2018-05-02 12:56:16 -070090 uint32_t length = strlen(text);
Mike Reedf6d86ac2019-01-18 14:13:23 -050091
John Reck283bb462018-12-13 16:40:14 -080092 canvas->drawText(utf16.get(), length, // text buffer
93 0, length, // draw range
94 0, length, // context range
Mike Reedf6d86ac2019-01-18 14:13:23 -050095 x, y, minikin::Bidi::LTR, paint, nullptr, nullptr /* measured text */);
Chris Craika1717272015-11-19 13:02:43 -080096}
97
Mike Reedf6d86ac2019-01-18 14:13:23 -050098void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint,
John Reck1bcacfd2017-11-03 10:12:19 -070099 const SkPath& path) {
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400100 auto utf16 = asciiToUtf16(text);
Mike Reedf6d86ac2019-01-18 14:13:23 -0500101 canvas->drawTextOnPath(utf16.get(), strlen(text), minikin::Bidi::LTR, path, 0, 0, paint,
John Reck283bb462018-12-13 16:40:14 -0800102 nullptr);
Chris Craikd7448e62015-12-15 10:34:36 -0800103}
104
John Recke5da4ef2016-01-14 12:34:46 -0800105void TestUtils::TestTask::run() {
John Recke5da4ef2016-01-14 12:34:46 -0800106 // RenderState only valid once RenderThread is running, so queried here
sergeyv7dc370b2016-06-17 11:21:11 -0700107 renderthread::RenderThread& renderThread = renderthread::RenderThread::getInstance();
Greg Daniel98c78dad2017-01-04 14:45:56 -0500108 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
Stan Iliev981afe72019-02-13 14:24:33 -0500109 renderThread.requireVkContext();
Greg Daniel98c78dad2017-01-04 14:45:56 -0500110 } else {
John Reck1e510712018-04-23 08:15:03 -0700111 renderThread.requireGlContext();
Greg Daniel98c78dad2017-01-04 14:45:56 -0500112 }
John Recke5da4ef2016-01-14 12:34:46 -0800113
sergeyv7dc370b2016-06-17 11:21:11 -0700114 rtCallback(renderThread);
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -0400115
John Reck283bb462018-12-13 16:40:14 -0800116 renderThread.destroyRenderingContext();
John Recke5da4ef2016-01-14 12:34:46 -0800117}
118
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400119std::unique_ptr<uint16_t[]> TestUtils::asciiToUtf16(const char* str) {
120 const int length = strlen(str);
121 std::unique_ptr<uint16_t[]> utf16(new uint16_t[length]);
122 for (int i = 0; i < length; i++) {
123 utf16.get()[i] = str[i];
124 }
125 return utf16;
sergeyvdccca442016-03-21 15:38:21 -0700126}
127
Stan Iliev021693b2016-10-17 16:26:15 -0400128SkColor TestUtils::getColor(const sk_sp<SkSurface>& surface, int x, int y) {
129 SkPixmap pixmap;
130 if (!surface->peekPixels(&pixmap)) {
131 return 0;
132 }
133 switch (pixmap.colorType()) {
134 case kGray_8_SkColorType: {
135 const uint8_t* addr = pixmap.addr8(x, y);
136 return SkColorSetRGB(*addr, *addr, *addr);
137 }
138 case kAlpha_8_SkColorType: {
139 const uint8_t* addr = pixmap.addr8(x, y);
140 return SkColorSetA(0, addr[0]);
141 }
142 case kRGB_565_SkColorType: {
143 const uint16_t* addr = pixmap.addr16(x, y);
144 return SkPixel16ToColor(addr[0]);
145 }
146 case kARGB_4444_SkColorType: {
147 const uint16_t* addr = pixmap.addr16(x, y);
148 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
149 return SkUnPreMultiply::PMColorToColor(c);
150 }
151 case kBGRA_8888_SkColorType: {
152 const uint32_t* addr = pixmap.addr32(x, y);
153 SkPMColor c = SkSwizzle_BGRA_to_PMColor(addr[0]);
154 return SkUnPreMultiply::PMColorToColor(c);
155 }
156 case kRGBA_8888_SkColorType: {
157 const uint32_t* addr = pixmap.addr32(x, y);
158 SkPMColor c = SkSwizzle_RGBA_to_PMColor(addr[0]);
159 return SkUnPreMultiply::PMColorToColor(c);
160 }
161 default:
162 return 0;
163 }
164 return 0;
165}
166
Stan Iliev52771272016-11-17 09:54:38 -0500167SkRect TestUtils::getClipBounds(const SkCanvas* canvas) {
Mike Reed5e438982017-01-25 08:23:25 -0500168 return SkRect::Make(canvas->getDeviceClipBounds());
Stan Iliev52771272016-11-17 09:54:38 -0500169}
170
171SkRect TestUtils::getLocalClipBounds(const SkCanvas* canvas) {
172 SkMatrix invertedTotalMatrix;
173 if (!canvas->getTotalMatrix().invert(&invertedTotalMatrix)) {
174 return SkRect::MakeEmpty();
175 }
176 SkRect outlineInDeviceCoord = TestUtils::getClipBounds(canvas);
177 SkRect outlineInLocalCoord;
178 invertedTotalMatrix.mapRect(&outlineInLocalCoord, outlineInDeviceCoord);
179 return outlineInLocalCoord;
180}
181
John Reck16c9d6a2015-11-17 15:51:08 -0800182} /* namespace uirenderer */
183} /* namespace android */