blob: 930067a9b2cc53135cc97054dcb30d47f5aabdf7 [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"
21#include "LayerRenderer.h"
22
sergeyv7dc370b2016-06-17 11:21:11 -070023#include <renderthread/EglManager.h>
Ben Wagnerf4cf5d32016-02-22 15:25:35 -050024#include <utils/Unicode.h>
25
John Reck16c9d6a2015-11-17 15:51:08 -080026namespace android {
27namespace uirenderer {
28
29SkColor TestUtils::interpolateColor(float fraction, SkColor start, SkColor end) {
30 int startA = (start >> 24) & 0xff;
31 int startR = (start >> 16) & 0xff;
32 int startG = (start >> 8) & 0xff;
33 int startB = start & 0xff;
34
35 int endA = (end >> 24) & 0xff;
36 int endR = (end >> 16) & 0xff;
37 int endG = (end >> 8) & 0xff;
38 int endB = end & 0xff;
39
40 return (int)((startA + (int)(fraction * (endA - startA))) << 24)
41 | (int)((startR + (int)(fraction * (endR - startR))) << 16)
42 | (int)((startG + (int)(fraction * (endG - startG))) << 8)
43 | (int)((startB + (int)(fraction * (endB - startB))));
44}
45
Chris Craikd2dfd8f2015-12-16 14:27:20 -080046sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
47 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
Chris Craik243e85b2016-03-25 15:26:11 -070048 const SkMatrix& transform) {
49 Layer* layer = LayerRenderer::createTextureLayer(renderThread.renderState());
Chris Craikaafb01d2016-03-25 18:34:11 -070050 layer->getTransform().load(transform);
Chris Craik243e85b2016-03-25 15:26:11 -070051
52 sp<DeferredLayerUpdater> layerUpdater = new DeferredLayerUpdater(layer);
53 layerUpdater->setSize(width, height);
54 layerUpdater->setTransform(&transform);
55
56 // updateLayer so it's ready to draw
Chris Craikd2dfd8f2015-12-16 14:27:20 -080057 bool isOpaque = true;
58 bool forceFilter = true;
59 GLenum renderTarget = GL_TEXTURE_EXTERNAL_OES;
Chris Craikd2dfd8f2015-12-16 14:27:20 -080060 LayerRenderer::updateTextureLayer(layer, width, height, isOpaque, forceFilter,
Chris Craik243e85b2016-03-25 15:26:11 -070061 renderTarget, Matrix4::identity().data);
Chris Craikd2dfd8f2015-12-16 14:27:20 -080062
Chris Craikd2dfd8f2015-12-16 14:27:20 -080063 return layerUpdater;
64}
65
Chris Craike8c3c812016-02-05 20:10:50 -080066void TestUtils::layoutTextUnscaled(const SkPaint& paint, const char* text,
67 std::vector<glyph_t>* outGlyphs, std::vector<float>* outPositions,
68 float* outTotalAdvance, Rect* outBounds) {
69 Rect bounds;
70 float totalAdvance = 0;
Chris Craik42a54072015-11-24 11:41:54 -080071 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry);
Chris Craikd7448e62015-12-15 10:34:36 -080072 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I());
Chris Craik42a54072015-11-24 11:41:54 -080073 while (*text != '\0') {
Ben Wagnerf4cf5d32016-02-22 15:25:35 -050074 size_t nextIndex = 0;
75 int32_t unichar = utf32_from_utf8_at(text, 4, 0, &nextIndex);
76 text += nextIndex;
77
Chris Craik42a54072015-11-24 11:41:54 -080078 glyph_t glyph = autoCache.getCache()->unicharToGlyph(unichar);
79 autoCache.getCache()->unicharToGlyph(unichar);
Chris Craika1717272015-11-19 13:02:43 -080080
Chris Craik42a54072015-11-24 11:41:54 -080081 // push glyph and its relative position
Chris Craike8c3c812016-02-05 20:10:50 -080082 outGlyphs->push_back(glyph);
83 outPositions->push_back(totalAdvance);
84 outPositions->push_back(0);
Chris Craika1717272015-11-19 13:02:43 -080085
Chris Craik42a54072015-11-24 11:41:54 -080086 // compute bounds
87 SkGlyph skGlyph = autoCache.getCache()->getUnicharMetrics(unichar);
88 Rect glyphBounds(skGlyph.fWidth, skGlyph.fHeight);
89 glyphBounds.translate(totalAdvance + skGlyph.fLeft, skGlyph.fTop);
90 bounds.unionWith(glyphBounds);
Chris Craika1717272015-11-19 13:02:43 -080091
Chris Craik42a54072015-11-24 11:41:54 -080092 // advance next character
93 SkScalar skWidth;
94 paint.getTextWidths(&glyph, sizeof(glyph), &skWidth, NULL);
95 totalAdvance += skWidth;
96 }
Chris Craike8c3c812016-02-05 20:10:50 -080097 *outBounds = bounds;
98 *outTotalAdvance = totalAdvance;
99}
100
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400101
102void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text,
Chris Craike8c3c812016-02-05 20:10:50 -0800103 const SkPaint& paint, float x, float y) {
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400104 auto utf16 = asciiToUtf16(text);
105 canvas->drawText(utf16.get(), 0, strlen(text), strlen(text), x, y, 0, paint, nullptr);
Chris Craika1717272015-11-19 13:02:43 -0800106}
107
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400108void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text,
Chris Craikd7448e62015-12-15 10:34:36 -0800109 const SkPaint& paint, const SkPath& path) {
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400110 auto utf16 = asciiToUtf16(text);
111 canvas->drawTextOnPath(utf16.get(), strlen(text), 0, path, 0, 0, paint, nullptr);
Chris Craikd7448e62015-12-15 10:34:36 -0800112}
113
John Recke5da4ef2016-01-14 12:34:46 -0800114void TestUtils::TestTask::run() {
John Recke5da4ef2016-01-14 12:34:46 -0800115 // RenderState only valid once RenderThread is running, so queried here
sergeyv7dc370b2016-06-17 11:21:11 -0700116 renderthread::RenderThread& renderThread = renderthread::RenderThread::getInstance();
117 bool hasEglContext = renderThread.eglManager().hasEglContext();
118 RenderState& renderState = renderThread.renderState();
119 if (!hasEglContext) {
120 renderState.onGLContextCreated();
121 }
John Recke5da4ef2016-01-14 12:34:46 -0800122
sergeyv7dc370b2016-06-17 11:21:11 -0700123 rtCallback(renderThread);
124 if (!hasEglContext) {
125 renderState.flush(Caches::FlushMode::Full);
126 renderState.onGLContextDestroyed();
127 }
John Recke5da4ef2016-01-14 12:34:46 -0800128}
129
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400130std::unique_ptr<uint16_t[]> TestUtils::asciiToUtf16(const char* str) {
131 const int length = strlen(str);
132 std::unique_ptr<uint16_t[]> utf16(new uint16_t[length]);
133 for (int i = 0; i < length; i++) {
134 utf16.get()[i] = str[i];
135 }
136 return utf16;
sergeyvdccca442016-03-21 15:38:21 -0700137}
138
John Reck16c9d6a2015-11-17 15:51:08 -0800139} /* namespace uirenderer */
140} /* namespace android */