blob: 574dfffa2a0586bc40bc6592c4c6bd50c5eeb380 [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
John Reck1bcacfd2017-11-03 10:12:19 -070022#include <SkClipStack.h>
Seigo Nonaka7c93e862017-10-25 16:34:48 -070023#include <minikin/Layout.h>
Greg Daniel98c78dad2017-01-04 14:45:56 -050024#include <pipeline/skia/SkiaOpenGLPipeline.h>
25#include <pipeline/skia/SkiaVulkanPipeline.h>
John Reck1bcacfd2017-11-03 10:12:19 -070026#include <renderthread/EglManager.h>
Greg Daniel98c78dad2017-01-04 14:45:56 -050027#include <renderthread/VulkanManager.h>
Ben Wagnerf4cf5d32016-02-22 15:25:35 -050028#include <utils/Unicode.h>
29
Leon Scroggins IIIee708fa2016-12-12 15:31:39 -050030#include <SkGlyphCache.h>
31
John Reck16c9d6a2015-11-17 15:51:08 -080032namespace android {
33namespace uirenderer {
34
35SkColor 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
John Reck1bcacfd2017-11-03 10:12:19 -070046 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))));
John Reck16c9d6a2015-11-17 15:51:08 -080050}
51
Chris Craikd2dfd8f2015-12-16 14:27:20 -080052sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
Greg Daniel98c78dad2017-01-04 14:45:56 -050053 renderthread::RenderThread& renderThread) {
54 android::uirenderer::renderthread::IRenderPipeline* pipeline;
John Reck18f442e2018-04-09 16:56:34 -070055 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
Greg Daniel98c78dad2017-01-04 14:45:56 -050056 pipeline = new skiapipeline::SkiaOpenGLPipeline(renderThread);
57 } else {
58 pipeline = new skiapipeline::SkiaVulkanPipeline(renderThread);
59 }
60 sp<DeferredLayerUpdater> layerUpdater = pipeline->createTextureLayer();
sergeyv3e9999b2017-01-19 15:37:02 -080061 layerUpdater->apply();
Greg Daniel98c78dad2017-01-04 14:45:56 -050062 delete pipeline;
63 return layerUpdater;
64}
65
66sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
Chris Craikd2dfd8f2015-12-16 14:27:20 -080067 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
Chris Craik243e85b2016-03-25 15:26:11 -070068 const SkMatrix& transform) {
Greg Daniel98c78dad2017-01-04 14:45:56 -050069 sp<DeferredLayerUpdater> layerUpdater = createTextureLayerUpdater(renderThread);
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -040070 layerUpdater->backingLayer()->getTransform().load(transform);
Chris Craik243e85b2016-03-25 15:26:11 -070071 layerUpdater->setSize(width, height);
72 layerUpdater->setTransform(&transform);
73
74 // updateLayer so it's ready to draw
Derek Sollenbergerbe3876c2018-04-20 16:13:31 -040075 layerUpdater->updateLayer(true, Matrix4::identity().data, HAL_DATASPACE_UNKNOWN);
Chris Craik09df8872017-02-14 12:37:49 -080076 if (layerUpdater->backingLayer()->getApi() == Layer::Api::OpenGL) {
John Reck1bcacfd2017-11-03 10:12:19 -070077 static_cast<GlLayer*>(layerUpdater->backingLayer())
78 ->setRenderTarget(GL_TEXTURE_EXTERNAL_OES);
Chris Craik09df8872017-02-14 12:37:49 -080079 }
Chris Craikd2dfd8f2015-12-16 14:27:20 -080080 return layerUpdater;
81}
82
Chris Craike8c3c812016-02-05 20:10:50 -080083void TestUtils::layoutTextUnscaled(const SkPaint& paint, const char* text,
John Reck1bcacfd2017-11-03 10:12:19 -070084 std::vector<glyph_t>* outGlyphs,
85 std::vector<float>* outPositions, float* outTotalAdvance,
86 Rect* outBounds) {
Chris Craike8c3c812016-02-05 20:10:50 -080087 Rect bounds;
88 float totalAdvance = 0;
Chris Craik42a54072015-11-24 11:41:54 -080089 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry);
Chris Craikd7448e62015-12-15 10:34:36 -080090 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I());
Chris Craik42a54072015-11-24 11:41:54 -080091 while (*text != '\0') {
Ben Wagnerf4cf5d32016-02-22 15:25:35 -050092 size_t nextIndex = 0;
93 int32_t unichar = utf32_from_utf8_at(text, 4, 0, &nextIndex);
94 text += nextIndex;
95
Derek Sollenberger086994b2018-03-09 11:46:51 -050096 glyph_t glyph = autoCache->unicharToGlyph(unichar);
97 autoCache->unicharToGlyph(unichar);
Chris Craika1717272015-11-19 13:02:43 -080098
Chris Craik42a54072015-11-24 11:41:54 -080099 // push glyph and its relative position
Chris Craike8c3c812016-02-05 20:10:50 -0800100 outGlyphs->push_back(glyph);
101 outPositions->push_back(totalAdvance);
102 outPositions->push_back(0);
Chris Craika1717272015-11-19 13:02:43 -0800103
Chris Craik42a54072015-11-24 11:41:54 -0800104 // compute bounds
Derek Sollenberger086994b2018-03-09 11:46:51 -0500105 SkGlyph skGlyph = autoCache->getUnicharMetrics(unichar);
Chris Craik42a54072015-11-24 11:41:54 -0800106 Rect glyphBounds(skGlyph.fWidth, skGlyph.fHeight);
107 glyphBounds.translate(totalAdvance + skGlyph.fLeft, skGlyph.fTop);
108 bounds.unionWith(glyphBounds);
Chris Craika1717272015-11-19 13:02:43 -0800109
Chris Craik42a54072015-11-24 11:41:54 -0800110 // advance next character
111 SkScalar skWidth;
112 paint.getTextWidths(&glyph, sizeof(glyph), &skWidth, NULL);
113 totalAdvance += skWidth;
114 }
Chris Craike8c3c812016-02-05 20:10:50 -0800115 *outBounds = bounds;
116 *outTotalAdvance = totalAdvance;
117}
118
John Reck1bcacfd2017-11-03 10:12:19 -0700119void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, const SkPaint& paint, float x,
120 float y) {
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400121 auto utf16 = asciiToUtf16(text);
Seigo Nonaka3a4217f2018-05-02 12:56:16 -0700122 uint32_t length = strlen(text);
Stan Ilievec083812018-01-02 13:43:39 -0500123 SkPaint glyphPaint(paint);
124 glyphPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
Seigo Nonaka3a4217f2018-05-02 12:56:16 -0700125 canvas->drawText(
126 utf16.get(), length, // text buffer
127 0, length, // draw range
128 0, length, // context range
129 x, y, minikin::Bidi::LTR,
Seigo Nonaka83143d02018-03-14 17:08:28 -0700130 glyphPaint, nullptr, nullptr /* measured text */);
Chris Craika1717272015-11-19 13:02:43 -0800131}
132
John Reck1bcacfd2017-11-03 10:12:19 -0700133void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, const SkPaint& paint,
134 const SkPath& path) {
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400135 auto utf16 = asciiToUtf16(text);
Stan Ilievec083812018-01-02 13:43:39 -0500136 SkPaint glyphPaint(paint);
137 glyphPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
138 canvas->drawTextOnPath(utf16.get(), strlen(text), minikin::Bidi::LTR, path, 0, 0, glyphPaint,
139 nullptr);
Chris Craikd7448e62015-12-15 10:34:36 -0800140}
141
John Recke5da4ef2016-01-14 12:34:46 -0800142void TestUtils::TestTask::run() {
John Recke5da4ef2016-01-14 12:34:46 -0800143 // RenderState only valid once RenderThread is running, so queried here
sergeyv7dc370b2016-06-17 11:21:11 -0700144 renderthread::RenderThread& renderThread = renderthread::RenderThread::getInstance();
Greg Daniel98c78dad2017-01-04 14:45:56 -0500145 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
146 renderThread.vulkanManager().initialize();
147 } else {
John Reck1e510712018-04-23 08:15:03 -0700148 renderThread.requireGlContext();
Greg Daniel98c78dad2017-01-04 14:45:56 -0500149 }
John Recke5da4ef2016-01-14 12:34:46 -0800150
sergeyv7dc370b2016-06-17 11:21:11 -0700151 rtCallback(renderThread);
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -0400152
Greg Daniel98c78dad2017-01-04 14:45:56 -0500153 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
154 renderThread.vulkanManager().destroy();
155 } else {
156 renderThread.renderState().flush(Caches::FlushMode::Full);
John Reck1e510712018-04-23 08:15:03 -0700157 renderThread.destroyGlContext();
Greg Daniel98c78dad2017-01-04 14:45:56 -0500158 }
John Recke5da4ef2016-01-14 12:34:46 -0800159}
160
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400161std::unique_ptr<uint16_t[]> TestUtils::asciiToUtf16(const char* str) {
162 const int length = strlen(str);
163 std::unique_ptr<uint16_t[]> utf16(new uint16_t[length]);
164 for (int i = 0; i < length; i++) {
165 utf16.get()[i] = str[i];
166 }
167 return utf16;
sergeyvdccca442016-03-21 15:38:21 -0700168}
169
Stan Iliev021693b2016-10-17 16:26:15 -0400170SkColor TestUtils::getColor(const sk_sp<SkSurface>& surface, int x, int y) {
171 SkPixmap pixmap;
172 if (!surface->peekPixels(&pixmap)) {
173 return 0;
174 }
175 switch (pixmap.colorType()) {
176 case kGray_8_SkColorType: {
177 const uint8_t* addr = pixmap.addr8(x, y);
178 return SkColorSetRGB(*addr, *addr, *addr);
179 }
180 case kAlpha_8_SkColorType: {
181 const uint8_t* addr = pixmap.addr8(x, y);
182 return SkColorSetA(0, addr[0]);
183 }
184 case kRGB_565_SkColorType: {
185 const uint16_t* addr = pixmap.addr16(x, y);
186 return SkPixel16ToColor(addr[0]);
187 }
188 case kARGB_4444_SkColorType: {
189 const uint16_t* addr = pixmap.addr16(x, y);
190 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
191 return SkUnPreMultiply::PMColorToColor(c);
192 }
193 case kBGRA_8888_SkColorType: {
194 const uint32_t* addr = pixmap.addr32(x, y);
195 SkPMColor c = SkSwizzle_BGRA_to_PMColor(addr[0]);
196 return SkUnPreMultiply::PMColorToColor(c);
197 }
198 case kRGBA_8888_SkColorType: {
199 const uint32_t* addr = pixmap.addr32(x, y);
200 SkPMColor c = SkSwizzle_RGBA_to_PMColor(addr[0]);
201 return SkUnPreMultiply::PMColorToColor(c);
202 }
203 default:
204 return 0;
205 }
206 return 0;
207}
208
Stan Iliev52771272016-11-17 09:54:38 -0500209SkRect TestUtils::getClipBounds(const SkCanvas* canvas) {
Mike Reed5e438982017-01-25 08:23:25 -0500210 return SkRect::Make(canvas->getDeviceClipBounds());
Stan Iliev52771272016-11-17 09:54:38 -0500211}
212
213SkRect TestUtils::getLocalClipBounds(const SkCanvas* canvas) {
214 SkMatrix invertedTotalMatrix;
215 if (!canvas->getTotalMatrix().invert(&invertedTotalMatrix)) {
216 return SkRect::MakeEmpty();
217 }
218 SkRect outlineInDeviceCoord = TestUtils::getClipBounds(canvas);
219 SkRect outlineInLocalCoord;
220 invertedTotalMatrix.mapRect(&outlineInLocalCoord, outlineInDeviceCoord);
221 return outlineInLocalCoord;
222}
223
John Reck16c9d6a2015-11-17 15:51:08 -0800224} /* namespace uirenderer */
225} /* namespace android */