TestUtils.cpp revision 7dc370bfe3223dfbcb368e0006c688dfd668ab01
1/*
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
19#include "hwui/Paint.h"
20#include "DeferredLayerUpdater.h"
21#include "LayerRenderer.h"
22
23#include <renderthread/EglManager.h>
24#include <utils/Unicode.h>
25
26namespace 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
46sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
47        renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
48        const SkMatrix& transform) {
49    Layer* layer = LayerRenderer::createTextureLayer(renderThread.renderState());
50    layer->getTransform().load(transform);
51
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
57    bool isOpaque = true;
58    bool forceFilter = true;
59    GLenum renderTarget = GL_TEXTURE_EXTERNAL_OES;
60    LayerRenderer::updateTextureLayer(layer, width, height, isOpaque, forceFilter,
61    renderTarget, Matrix4::identity().data);
62
63    return layerUpdater;
64}
65
66void 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;
71    SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry);
72    SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I());
73    while (*text != '\0') {
74        size_t nextIndex = 0;
75        int32_t unichar = utf32_from_utf8_at(text, 4, 0, &nextIndex);
76        text += nextIndex;
77
78        glyph_t glyph = autoCache.getCache()->unicharToGlyph(unichar);
79        autoCache.getCache()->unicharToGlyph(unichar);
80
81        // push glyph and its relative position
82        outGlyphs->push_back(glyph);
83        outPositions->push_back(totalAdvance);
84        outPositions->push_back(0);
85
86        // 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);
91
92        // advance next character
93        SkScalar skWidth;
94        paint.getTextWidths(&glyph, sizeof(glyph), &skWidth, NULL);
95        totalAdvance += skWidth;
96    }
97    *outBounds = bounds;
98    *outTotalAdvance = totalAdvance;
99}
100
101
102void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text,
103        const SkPaint& paint, float x, float y) {
104    auto utf16 = asciiToUtf16(text);
105    canvas->drawText(utf16.get(), 0, strlen(text), strlen(text), x, y, 0, paint, nullptr);
106}
107
108void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text,
109        const SkPaint& paint, const SkPath& path) {
110    auto utf16 = asciiToUtf16(text);
111    canvas->drawTextOnPath(utf16.get(), strlen(text), 0, path, 0, 0, paint, nullptr);
112}
113
114void TestUtils::TestTask::run() {
115    // RenderState only valid once RenderThread is running, so queried here
116    renderthread::RenderThread& renderThread = renderthread::RenderThread::getInstance();
117    bool hasEglContext = renderThread.eglManager().hasEglContext();
118    RenderState& renderState = renderThread.renderState();
119    if (!hasEglContext) {
120        renderState.onGLContextCreated();
121    }
122
123    rtCallback(renderThread);
124    if (!hasEglContext) {
125        renderState.flush(Caches::FlushMode::Full);
126        renderState.onGLContextDestroyed();
127    }
128}
129
130std::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;
137}
138
139} /* namespace uirenderer */
140} /* namespace android */
141