blob: 0916d72a518446caf55d365f194c40fa45674090 [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
Derek Sollenberger79abbf22016-03-24 11:07:19 -040019#include "hwui/Paint.h"
Chris Craikd2dfd8f2015-12-16 14:27:20 -080020#include "DeferredLayerUpdater.h"
Chris Craikd2dfd8f2015-12-16 14:27:20 -080021
sergeyv7dc370b2016-06-17 11:21:11 -070022#include <renderthread/EglManager.h>
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -040023#include <renderthread/OpenGLPipeline.h>
Greg Daniel98c78dad2017-01-04 14:45:56 -050024#include <pipeline/skia/SkiaOpenGLPipeline.h>
25#include <pipeline/skia/SkiaVulkanPipeline.h>
26#include <renderthread/VulkanManager.h>
Ben Wagnerf4cf5d32016-02-22 15:25:35 -050027#include <utils/Unicode.h>
Stan Iliev52771272016-11-17 09:54:38 -050028#include <SkClipStack.h>
Ben Wagnerf4cf5d32016-02-22 15:25:35 -050029
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
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 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;
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();
63 delete pipeline;
64 return layerUpdater;
65}
66
67sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
Chris Craikd2dfd8f2015-12-16 14:27:20 -080068 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
Chris Craik243e85b2016-03-25 15:26:11 -070069 const SkMatrix& transform) {
Greg Daniel98c78dad2017-01-04 14:45:56 -050070 sp<DeferredLayerUpdater> layerUpdater = createTextureLayerUpdater(renderThread);
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -040071 layerUpdater->backingLayer()->getTransform().load(transform);
Chris Craik243e85b2016-03-25 15:26:11 -070072 layerUpdater->setSize(width, height);
73 layerUpdater->setTransform(&transform);
74
75 // updateLayer so it's ready to draw
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -040076 layerUpdater->updateLayer(true, GL_TEXTURE_EXTERNAL_OES, Matrix4::identity().data);
Chris Craikd2dfd8f2015-12-16 14:27:20 -080077 return layerUpdater;
78}
79
Chris Craike8c3c812016-02-05 20:10:50 -080080void TestUtils::layoutTextUnscaled(const SkPaint& paint, const char* text,
81 std::vector<glyph_t>* outGlyphs, std::vector<float>* outPositions,
82 float* outTotalAdvance, Rect* outBounds) {
83 Rect bounds;
84 float totalAdvance = 0;
Chris Craik42a54072015-11-24 11:41:54 -080085 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry);
Chris Craikd7448e62015-12-15 10:34:36 -080086 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I());
Chris Craik42a54072015-11-24 11:41:54 -080087 while (*text != '\0') {
Ben Wagnerf4cf5d32016-02-22 15:25:35 -050088 size_t nextIndex = 0;
89 int32_t unichar = utf32_from_utf8_at(text, 4, 0, &nextIndex);
90 text += nextIndex;
91
Chris Craik42a54072015-11-24 11:41:54 -080092 glyph_t glyph = autoCache.getCache()->unicharToGlyph(unichar);
93 autoCache.getCache()->unicharToGlyph(unichar);
Chris Craika1717272015-11-19 13:02:43 -080094
Chris Craik42a54072015-11-24 11:41:54 -080095 // push glyph and its relative position
Chris Craike8c3c812016-02-05 20:10:50 -080096 outGlyphs->push_back(glyph);
97 outPositions->push_back(totalAdvance);
98 outPositions->push_back(0);
Chris Craika1717272015-11-19 13:02:43 -080099
Chris Craik42a54072015-11-24 11:41:54 -0800100 // compute bounds
101 SkGlyph skGlyph = autoCache.getCache()->getUnicharMetrics(unichar);
102 Rect glyphBounds(skGlyph.fWidth, skGlyph.fHeight);
103 glyphBounds.translate(totalAdvance + skGlyph.fLeft, skGlyph.fTop);
104 bounds.unionWith(glyphBounds);
Chris Craika1717272015-11-19 13:02:43 -0800105
Chris Craik42a54072015-11-24 11:41:54 -0800106 // advance next character
107 SkScalar skWidth;
108 paint.getTextWidths(&glyph, sizeof(glyph), &skWidth, NULL);
109 totalAdvance += skWidth;
110 }
Chris Craike8c3c812016-02-05 20:10:50 -0800111 *outBounds = bounds;
112 *outTotalAdvance = totalAdvance;
113}
114
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400115
116void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text,
Chris Craike8c3c812016-02-05 20:10:50 -0800117 const SkPaint& paint, float x, float y) {
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400118 auto utf16 = asciiToUtf16(text);
119 canvas->drawText(utf16.get(), 0, strlen(text), strlen(text), x, y, 0, paint, nullptr);
Chris Craika1717272015-11-19 13:02:43 -0800120}
121
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400122void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text,
Chris Craikd7448e62015-12-15 10:34:36 -0800123 const SkPaint& paint, const SkPath& path) {
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400124 auto utf16 = asciiToUtf16(text);
125 canvas->drawTextOnPath(utf16.get(), strlen(text), 0, path, 0, 0, paint, nullptr);
Chris Craikd7448e62015-12-15 10:34:36 -0800126}
127
John Recke5da4ef2016-01-14 12:34:46 -0800128void TestUtils::TestTask::run() {
John Recke5da4ef2016-01-14 12:34:46 -0800129 // RenderState only valid once RenderThread is running, so queried here
sergeyv7dc370b2016-06-17 11:21:11 -0700130 renderthread::RenderThread& renderThread = renderthread::RenderThread::getInstance();
Greg Daniel98c78dad2017-01-04 14:45:56 -0500131 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
132 renderThread.vulkanManager().initialize();
133 } else {
134 renderThread.eglManager().initialize();
135 }
John Recke5da4ef2016-01-14 12:34:46 -0800136
sergeyv7dc370b2016-06-17 11:21:11 -0700137 rtCallback(renderThread);
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -0400138
Greg Daniel98c78dad2017-01-04 14:45:56 -0500139 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
140 renderThread.vulkanManager().destroy();
141 } else {
142 renderThread.renderState().flush(Caches::FlushMode::Full);
143 renderThread.eglManager().destroy();
144 }
John Recke5da4ef2016-01-14 12:34:46 -0800145}
146
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400147std::unique_ptr<uint16_t[]> TestUtils::asciiToUtf16(const char* str) {
148 const int length = strlen(str);
149 std::unique_ptr<uint16_t[]> utf16(new uint16_t[length]);
150 for (int i = 0; i < length; i++) {
151 utf16.get()[i] = str[i];
152 }
153 return utf16;
sergeyvdccca442016-03-21 15:38:21 -0700154}
155
Stan Iliev021693b2016-10-17 16:26:15 -0400156SkColor TestUtils::getColor(const sk_sp<SkSurface>& surface, int x, int y) {
157 SkPixmap pixmap;
158 if (!surface->peekPixels(&pixmap)) {
159 return 0;
160 }
161 switch (pixmap.colorType()) {
162 case kGray_8_SkColorType: {
163 const uint8_t* addr = pixmap.addr8(x, y);
164 return SkColorSetRGB(*addr, *addr, *addr);
165 }
166 case kAlpha_8_SkColorType: {
167 const uint8_t* addr = pixmap.addr8(x, y);
168 return SkColorSetA(0, addr[0]);
169 }
170 case kRGB_565_SkColorType: {
171 const uint16_t* addr = pixmap.addr16(x, y);
172 return SkPixel16ToColor(addr[0]);
173 }
174 case kARGB_4444_SkColorType: {
175 const uint16_t* addr = pixmap.addr16(x, y);
176 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
177 return SkUnPreMultiply::PMColorToColor(c);
178 }
179 case kBGRA_8888_SkColorType: {
180 const uint32_t* addr = pixmap.addr32(x, y);
181 SkPMColor c = SkSwizzle_BGRA_to_PMColor(addr[0]);
182 return SkUnPreMultiply::PMColorToColor(c);
183 }
184 case kRGBA_8888_SkColorType: {
185 const uint32_t* addr = pixmap.addr32(x, y);
186 SkPMColor c = SkSwizzle_RGBA_to_PMColor(addr[0]);
187 return SkUnPreMultiply::PMColorToColor(c);
188 }
189 default:
190 return 0;
191 }
192 return 0;
193}
194
Stan Iliev52771272016-11-17 09:54:38 -0500195SkRect TestUtils::getClipBounds(const SkCanvas* canvas) {
Mike Reed5e438982017-01-25 08:23:25 -0500196 return SkRect::Make(canvas->getDeviceClipBounds());
Stan Iliev52771272016-11-17 09:54:38 -0500197}
198
199SkRect TestUtils::getLocalClipBounds(const SkCanvas* canvas) {
200 SkMatrix invertedTotalMatrix;
201 if (!canvas->getTotalMatrix().invert(&invertedTotalMatrix)) {
202 return SkRect::MakeEmpty();
203 }
204 SkRect outlineInDeviceCoord = TestUtils::getClipBounds(canvas);
205 SkRect outlineInLocalCoord;
206 invertedTotalMatrix.mapRect(&outlineInLocalCoord, outlineInDeviceCoord);
207 return outlineInLocalCoord;
208}
209
John Reck16c9d6a2015-11-17 15:51:08 -0800210} /* namespace uirenderer */
211} /* namespace android */