blob: e1c9319ea203ca51785cd53d724ab8c02c166100 [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
John Reck16c9d6a2015-11-17 15:51:08 -080023namespace android {
24namespace uirenderer {
25
26SkColor TestUtils::interpolateColor(float fraction, SkColor start, SkColor end) {
27 int startA = (start >> 24) & 0xff;
28 int startR = (start >> 16) & 0xff;
29 int startG = (start >> 8) & 0xff;
30 int startB = start & 0xff;
31
32 int endA = (end >> 24) & 0xff;
33 int endR = (end >> 16) & 0xff;
34 int endG = (end >> 8) & 0xff;
35 int endB = end & 0xff;
36
37 return (int)((startA + (int)(fraction * (endA - startA))) << 24)
38 | (int)((startR + (int)(fraction * (endR - startR))) << 16)
39 | (int)((startG + (int)(fraction * (endG - startG))) << 8)
40 | (int)((startB + (int)(fraction * (endB - startB))));
41}
42
Chris Craikd2dfd8f2015-12-16 14:27:20 -080043sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
44 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
45 std::function<void(Matrix4*)> transformSetupCallback) {
46 bool isOpaque = true;
47 bool forceFilter = true;
48 GLenum renderTarget = GL_TEXTURE_EXTERNAL_OES;
49
50 Layer* layer = LayerRenderer::createTextureLayer(renderThread.renderState());
51 LayerRenderer::updateTextureLayer(layer, width, height, isOpaque, forceFilter,
52 renderTarget, Matrix4::identity().data);
53 transformSetupCallback(&(layer->getTransform()));
54
55 sp<DeferredLayerUpdater> layerUpdater = new DeferredLayerUpdater(layer);
56 return layerUpdater;
57}
58
Chris Craike8c3c812016-02-05 20:10:50 -080059void TestUtils::layoutTextUnscaled(const SkPaint& paint, const char* text,
60 std::vector<glyph_t>* outGlyphs, std::vector<float>* outPositions,
61 float* outTotalAdvance, Rect* outBounds) {
62 Rect bounds;
63 float totalAdvance = 0;
Chris Craik42a54072015-11-24 11:41:54 -080064 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry);
Chris Craikd7448e62015-12-15 10:34:36 -080065 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I());
Chris Craik42a54072015-11-24 11:41:54 -080066 while (*text != '\0') {
67 SkUnichar unichar = SkUTF8_NextUnichar(&text);
68 glyph_t glyph = autoCache.getCache()->unicharToGlyph(unichar);
69 autoCache.getCache()->unicharToGlyph(unichar);
Chris Craika1717272015-11-19 13:02:43 -080070
Chris Craik42a54072015-11-24 11:41:54 -080071 // push glyph and its relative position
Chris Craike8c3c812016-02-05 20:10:50 -080072 outGlyphs->push_back(glyph);
73 outPositions->push_back(totalAdvance);
74 outPositions->push_back(0);
Chris Craika1717272015-11-19 13:02:43 -080075
Chris Craik42a54072015-11-24 11:41:54 -080076 // compute bounds
77 SkGlyph skGlyph = autoCache.getCache()->getUnicharMetrics(unichar);
78 Rect glyphBounds(skGlyph.fWidth, skGlyph.fHeight);
79 glyphBounds.translate(totalAdvance + skGlyph.fLeft, skGlyph.fTop);
80 bounds.unionWith(glyphBounds);
Chris Craika1717272015-11-19 13:02:43 -080081
Chris Craik42a54072015-11-24 11:41:54 -080082 // advance next character
83 SkScalar skWidth;
84 paint.getTextWidths(&glyph, sizeof(glyph), &skWidth, NULL);
85 totalAdvance += skWidth;
86 }
Chris Craike8c3c812016-02-05 20:10:50 -080087 *outBounds = bounds;
88 *outTotalAdvance = totalAdvance;
89}
90
Derek Sollenberger79abbf22016-03-24 11:07:19 -040091
92void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text,
Chris Craike8c3c812016-02-05 20:10:50 -080093 const SkPaint& paint, float x, float y) {
Derek Sollenberger79abbf22016-03-24 11:07:19 -040094 auto utf16 = asciiToUtf16(text);
95 canvas->drawText(utf16.get(), 0, strlen(text), strlen(text), x, y, 0, paint, nullptr);
Chris Craika1717272015-11-19 13:02:43 -080096}
97
Derek Sollenberger79abbf22016-03-24 11:07:19 -040098void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text,
Chris Craikd7448e62015-12-15 10:34:36 -080099 const SkPaint& paint, const SkPath& path) {
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400100 auto utf16 = asciiToUtf16(text);
101 canvas->drawTextOnPath(utf16.get(), strlen(text), 0, path, 0, 0, paint, nullptr);
Chris Craikd7448e62015-12-15 10:34:36 -0800102}
103
John Recke5da4ef2016-01-14 12:34:46 -0800104void TestUtils::TestTask::run() {
John Recke5da4ef2016-01-14 12:34:46 -0800105 // RenderState only valid once RenderThread is running, so queried here
106 RenderState& renderState = renderthread::RenderThread::getInstance().renderState();
107
108 renderState.onGLContextCreated();
109 rtCallback(renderthread::RenderThread::getInstance());
Chris Craik1bc4ee42016-02-19 15:57:45 -0800110 renderState.flush(Caches::FlushMode::Full);
John Recke5da4ef2016-01-14 12:34:46 -0800111 renderState.onGLContextDestroyed();
112}
113
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400114std::unique_ptr<uint16_t[]> TestUtils::asciiToUtf16(const char* str) {
115 const int length = strlen(str);
116 std::unique_ptr<uint16_t[]> utf16(new uint16_t[length]);
117 for (int i = 0; i < length; i++) {
118 utf16.get()[i] = str[i];
119 }
120 return utf16;
sergeyvdccca442016-03-21 15:38:21 -0700121}
122
John Reck16c9d6a2015-11-17 15:51:08 -0800123} /* namespace uirenderer */
124} /* namespace android */