blob: 4a0d6ee199782cac2e491f7de32a80cd9e6a873c [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 Daniel98c78da2017-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>
27#include <renderthread/OpenGLPipeline.h>
Greg Daniel98c78da2017-01-04 14:45:56 -050028#include <renderthread/VulkanManager.h>
Ben Wagnerf4cf5d32016-02-22 15:25:35 -050029#include <utils/Unicode.h>
30
Leon Scroggins IIIee708fa2016-12-12 15:31:39 -050031#include <SkGlyphCache.h>
32
John Reck16c9d6a2015-11-17 15:51:08 -080033namespace android {
34namespace uirenderer {
35
36SkColor 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 Reck1bcacfd2017-11-03 10:12:19 -070047 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 Reck16c9d6a2015-11-17 15:51:08 -080051}
52
Chris Craikd2dfd8f2015-12-16 14:27:20 -080053sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
Greg Daniel98c78da2017-01-04 14:45:56 -050054 renderthread::RenderThread& renderThread) {
55 android::uirenderer::renderthread::IRenderPipeline* pipeline;
56 if (Properties::getRenderPipelineType() == RenderPipelineType::OpenGL) {
57 pipeline = new renderthread::OpenGLPipeline(renderThread);
58 } else if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
59 pipeline = new skiapipeline::SkiaOpenGLPipeline(renderThread);
60 } else {
61 pipeline = new skiapipeline::SkiaVulkanPipeline(renderThread);
62 }
63 sp<DeferredLayerUpdater> layerUpdater = pipeline->createTextureLayer();
sergeyv3e9999b2017-01-19 15:37:02 -080064 layerUpdater->apply();
Greg Daniel98c78da2017-01-04 14:45:56 -050065 delete pipeline;
66 return layerUpdater;
67}
68
69sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
Chris Craikd2dfd8f2015-12-16 14:27:20 -080070 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
Chris Craik243e85b2016-03-25 15:26:11 -070071 const SkMatrix& transform) {
Greg Daniel98c78da2017-01-04 14:45:56 -050072 sp<DeferredLayerUpdater> layerUpdater = createTextureLayerUpdater(renderThread);
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -040073 layerUpdater->backingLayer()->getTransform().load(transform);
Chris Craik243e85b2016-03-25 15:26:11 -070074 layerUpdater->setSize(width, height);
75 layerUpdater->setTransform(&transform);
76
77 // updateLayer so it's ready to draw
Chris Craik09df8872017-02-14 12:37:49 -080078 layerUpdater->updateLayer(true, Matrix4::identity().data);
79 if (layerUpdater->backingLayer()->getApi() == Layer::Api::OpenGL) {
John Reck1bcacfd2017-11-03 10:12:19 -070080 static_cast<GlLayer*>(layerUpdater->backingLayer())
81 ->setRenderTarget(GL_TEXTURE_EXTERNAL_OES);
Chris Craik09df8872017-02-14 12:37:49 -080082 }
Chris Craikd2dfd8f2015-12-16 14:27:20 -080083 return layerUpdater;
84}
85
Chris Craike8c3c812016-02-05 20:10:50 -080086void TestUtils::layoutTextUnscaled(const SkPaint& paint, const char* text,
John Reck1bcacfd2017-11-03 10:12:19 -070087 std::vector<glyph_t>* outGlyphs,
88 std::vector<float>* outPositions, float* outTotalAdvance,
89 Rect* outBounds) {
Chris Craike8c3c812016-02-05 20:10:50 -080090 Rect bounds;
91 float totalAdvance = 0;
Chris Craik42a54072015-11-24 11:41:54 -080092 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry);
Chris Craikd7448e62015-12-15 10:34:36 -080093 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I());
Chris Craik42a54072015-11-24 11:41:54 -080094 while (*text != '\0') {
Ben Wagnerf4cf5d32016-02-22 15:25:35 -050095 size_t nextIndex = 0;
96 int32_t unichar = utf32_from_utf8_at(text, 4, 0, &nextIndex);
97 text += nextIndex;
98
Chris Craik42a54072015-11-24 11:41:54 -080099 glyph_t glyph = autoCache.getCache()->unicharToGlyph(unichar);
100 autoCache.getCache()->unicharToGlyph(unichar);
Chris Craika1717272015-11-19 13:02:43 -0800101
Chris Craik42a54072015-11-24 11:41:54 -0800102 // push glyph and its relative position
Chris Craike8c3c812016-02-05 20:10:50 -0800103 outGlyphs->push_back(glyph);
104 outPositions->push_back(totalAdvance);
105 outPositions->push_back(0);
Chris Craika1717272015-11-19 13:02:43 -0800106
Chris Craik42a54072015-11-24 11:41:54 -0800107 // compute bounds
108 SkGlyph skGlyph = autoCache.getCache()->getUnicharMetrics(unichar);
109 Rect glyphBounds(skGlyph.fWidth, skGlyph.fHeight);
110 glyphBounds.translate(totalAdvance + skGlyph.fLeft, skGlyph.fTop);
111 bounds.unionWith(glyphBounds);
Chris Craika1717272015-11-19 13:02:43 -0800112
Chris Craik42a54072015-11-24 11:41:54 -0800113 // advance next character
114 SkScalar skWidth;
115 paint.getTextWidths(&glyph, sizeof(glyph), &skWidth, NULL);
116 totalAdvance += skWidth;
117 }
Chris Craike8c3c812016-02-05 20:10:50 -0800118 *outBounds = bounds;
119 *outTotalAdvance = totalAdvance;
120}
121
John Reck1bcacfd2017-11-03 10:12:19 -0700122void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, const SkPaint& paint, float x,
123 float y) {
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400124 auto utf16 = asciiToUtf16(text);
Stan Ilievec083812018-01-02 13:43:39 -0500125 SkPaint glyphPaint(paint);
126 glyphPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
127 canvas->drawText(utf16.get(), 0, strlen(text), strlen(text), x, y, minikin::Bidi::LTR,
128 glyphPaint, nullptr);
Chris Craika1717272015-11-19 13:02:43 -0800129}
130
John Reck1bcacfd2017-11-03 10:12:19 -0700131void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, const SkPaint& paint,
132 const SkPath& path) {
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400133 auto utf16 = asciiToUtf16(text);
Stan Ilievec083812018-01-02 13:43:39 -0500134 SkPaint glyphPaint(paint);
135 glyphPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
136 canvas->drawTextOnPath(utf16.get(), strlen(text), minikin::Bidi::LTR, path, 0, 0, glyphPaint,
137 nullptr);
Chris Craikd7448e62015-12-15 10:34:36 -0800138}
139
John Recke5da4ef2016-01-14 12:34:46 -0800140void TestUtils::TestTask::run() {
John Recke5da4ef2016-01-14 12:34:46 -0800141 // RenderState only valid once RenderThread is running, so queried here
sergeyv7dc370b2016-06-17 11:21:11 -0700142 renderthread::RenderThread& renderThread = renderthread::RenderThread::getInstance();
Greg Daniel98c78da2017-01-04 14:45:56 -0500143 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
144 renderThread.vulkanManager().initialize();
145 } else {
146 renderThread.eglManager().initialize();
147 }
John Recke5da4ef2016-01-14 12:34:46 -0800148
sergeyv7dc370b2016-06-17 11:21:11 -0700149 rtCallback(renderThread);
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -0400150
Greg Daniel98c78da2017-01-04 14:45:56 -0500151 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
152 renderThread.vulkanManager().destroy();
153 } else {
154 renderThread.renderState().flush(Caches::FlushMode::Full);
155 renderThread.eglManager().destroy();
156 }
John Recke5da4ef2016-01-14 12:34:46 -0800157}
158
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400159std::unique_ptr<uint16_t[]> TestUtils::asciiToUtf16(const char* str) {
160 const int length = strlen(str);
161 std::unique_ptr<uint16_t[]> utf16(new uint16_t[length]);
162 for (int i = 0; i < length; i++) {
163 utf16.get()[i] = str[i];
164 }
165 return utf16;
sergeyvdccca442016-03-21 15:38:21 -0700166}
167
Stan Iliev021693b2016-10-17 16:26:15 -0400168SkColor TestUtils::getColor(const sk_sp<SkSurface>& surface, int x, int y) {
169 SkPixmap pixmap;
170 if (!surface->peekPixels(&pixmap)) {
171 return 0;
172 }
173 switch (pixmap.colorType()) {
174 case kGray_8_SkColorType: {
175 const uint8_t* addr = pixmap.addr8(x, y);
176 return SkColorSetRGB(*addr, *addr, *addr);
177 }
178 case kAlpha_8_SkColorType: {
179 const uint8_t* addr = pixmap.addr8(x, y);
180 return SkColorSetA(0, addr[0]);
181 }
182 case kRGB_565_SkColorType: {
183 const uint16_t* addr = pixmap.addr16(x, y);
184 return SkPixel16ToColor(addr[0]);
185 }
186 case kARGB_4444_SkColorType: {
187 const uint16_t* addr = pixmap.addr16(x, y);
188 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
189 return SkUnPreMultiply::PMColorToColor(c);
190 }
191 case kBGRA_8888_SkColorType: {
192 const uint32_t* addr = pixmap.addr32(x, y);
193 SkPMColor c = SkSwizzle_BGRA_to_PMColor(addr[0]);
194 return SkUnPreMultiply::PMColorToColor(c);
195 }
196 case kRGBA_8888_SkColorType: {
197 const uint32_t* addr = pixmap.addr32(x, y);
198 SkPMColor c = SkSwizzle_RGBA_to_PMColor(addr[0]);
199 return SkUnPreMultiply::PMColorToColor(c);
200 }
201 default:
202 return 0;
203 }
204 return 0;
205}
206
Stan Iliev52771272016-11-17 09:54:38 -0500207SkRect TestUtils::getClipBounds(const SkCanvas* canvas) {
Mike Reed5e438982017-01-25 08:23:25 -0500208 return SkRect::Make(canvas->getDeviceClipBounds());
Stan Iliev52771272016-11-17 09:54:38 -0500209}
210
211SkRect TestUtils::getLocalClipBounds(const SkCanvas* canvas) {
212 SkMatrix invertedTotalMatrix;
213 if (!canvas->getTotalMatrix().invert(&invertedTotalMatrix)) {
214 return SkRect::MakeEmpty();
215 }
216 SkRect outlineInDeviceCoord = TestUtils::getClipBounds(canvas);
217 SkRect outlineInLocalCoord;
218 invertedTotalMatrix.mapRect(&outlineInLocalCoord, outlineInDeviceCoord);
219 return outlineInLocalCoord;
220}
221
John Reck16c9d6a2015-11-17 15:51:08 -0800222} /* namespace uirenderer */
223} /* namespace android */