TestUtils.cpp revision d2dfd8f128b632ed99418ab2b32949c939a9a369
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 "DeferredLayerUpdater.h"
20#include "LayerRenderer.h"
21
22namespace android {
23namespace uirenderer {
24
25SkColor TestUtils::interpolateColor(float fraction, SkColor start, SkColor end) {
26    int startA = (start >> 24) & 0xff;
27    int startR = (start >> 16) & 0xff;
28    int startG = (start >> 8) & 0xff;
29    int startB = start & 0xff;
30
31    int endA = (end >> 24) & 0xff;
32    int endR = (end >> 16) & 0xff;
33    int endG = (end >> 8) & 0xff;
34    int endB = end & 0xff;
35
36    return (int)((startA + (int)(fraction * (endA - startA))) << 24)
37            | (int)((startR + (int)(fraction * (endR - startR))) << 16)
38            | (int)((startG + (int)(fraction * (endG - startG))) << 8)
39            | (int)((startB + (int)(fraction * (endB - startB))));
40}
41
42sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
43        renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
44        std::function<void(Matrix4*)> transformSetupCallback) {
45    bool isOpaque = true;
46    bool forceFilter = true;
47    GLenum renderTarget = GL_TEXTURE_EXTERNAL_OES;
48
49    Layer* layer = LayerRenderer::createTextureLayer(renderThread.renderState());
50    LayerRenderer::updateTextureLayer(layer, width, height, isOpaque, forceFilter,
51            renderTarget, Matrix4::identity().data);
52    transformSetupCallback(&(layer->getTransform()));
53
54    sp<DeferredLayerUpdater> layerUpdater = new DeferredLayerUpdater(layer);
55    return layerUpdater;
56}
57
58void TestUtils::drawTextToCanvas(TestCanvas* canvas, const char* text,
59        const SkPaint& paint, float x, float y) {
60    // drawing text requires GlyphID TextEncoding (which JNI layer would have done)
61    LOG_ALWAYS_FATAL_IF(paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding,
62            "must use glyph encoding");
63    SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry);
64    SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I());
65
66    float totalAdvance = 0;
67    std::vector<glyph_t> glyphs;
68    std::vector<float> positions;
69    Rect bounds;
70    while (*text != '\0') {
71        SkUnichar unichar = SkUTF8_NextUnichar(&text);
72        glyph_t glyph = autoCache.getCache()->unicharToGlyph(unichar);
73        autoCache.getCache()->unicharToGlyph(unichar);
74
75        // push glyph and its relative position
76        glyphs.push_back(glyph);
77        positions.push_back(totalAdvance);
78        positions.push_back(0);
79
80        // compute bounds
81        SkGlyph skGlyph = autoCache.getCache()->getUnicharMetrics(unichar);
82        Rect glyphBounds(skGlyph.fWidth, skGlyph.fHeight);
83        glyphBounds.translate(totalAdvance + skGlyph.fLeft, skGlyph.fTop);
84        bounds.unionWith(glyphBounds);
85
86        // advance next character
87        SkScalar skWidth;
88        paint.getTextWidths(&glyph, sizeof(glyph), &skWidth, NULL);
89        totalAdvance += skWidth;
90    }
91
92    // apply alignment via x parameter (which JNI layer would have done)
93    if (paint.getTextAlign() == SkPaint::kCenter_Align) {
94        x -= totalAdvance / 2;
95    } else if (paint.getTextAlign() == SkPaint::kRight_Align) {
96        x -= totalAdvance;
97    }
98
99    bounds.translate(x, y);
100
101    // Force left alignment, since alignment offset is already baked in
102    SkPaint alignPaintCopy(paint);
103    alignPaintCopy.setTextAlign(SkPaint::kLeft_Align);
104    canvas->drawText(glyphs.data(), positions.data(), glyphs.size(), alignPaintCopy, x, y,
105                bounds.left, bounds.top, bounds.right, bounds.bottom, totalAdvance);
106}
107
108void TestUtils::drawTextToCanvas(TestCanvas* canvas, const char* text,
109        const SkPaint& paint, const SkPath& path) {
110    // drawing text requires GlyphID TextEncoding (which JNI layer would have done)
111    LOG_ALWAYS_FATAL_IF(paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding,
112            "must use glyph encoding");
113    SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry);
114    SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I());
115
116    std::vector<glyph_t> glyphs;
117    while (*text != '\0') {
118        SkUnichar unichar = SkUTF8_NextUnichar(&text);
119        glyphs.push_back(autoCache.getCache()->unicharToGlyph(unichar));
120    }
121    canvas->drawTextOnPath(glyphs.data(), glyphs.size(), path, 0, 0, paint);
122}
123
124} /* namespace uirenderer */
125} /* namespace android */
126