TestUtils.cpp revision 52771272f4f018f4fc6846224bf047497e784af1
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
22#include <renderthread/EglManager.h>
23#include <renderthread/OpenGLPipeline.h>
24#include <utils/Unicode.h>
25#include <SkClipStack.h>
26
27namespace android {
28namespace uirenderer {
29
30SkColor TestUtils::interpolateColor(float fraction, SkColor start, SkColor end) {
31    int startA = (start >> 24) & 0xff;
32    int startR = (start >> 16) & 0xff;
33    int startG = (start >> 8) & 0xff;
34    int startB = start & 0xff;
35
36    int endA = (end >> 24) & 0xff;
37    int endR = (end >> 16) & 0xff;
38    int endG = (end >> 8) & 0xff;
39    int endB = end & 0xff;
40
41    return (int)((startA + (int)(fraction * (endA - startA))) << 24)
42            | (int)((startR + (int)(fraction * (endR - startR))) << 16)
43            | (int)((startG + (int)(fraction * (endG - startG))) << 8)
44            | (int)((startB + (int)(fraction * (endB - startB))));
45}
46
47sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
48        renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
49        const SkMatrix& transform) {
50    renderthread::OpenGLPipeline pipeline(renderThread);
51    sp<DeferredLayerUpdater> layerUpdater = pipeline.createTextureLayer();
52    layerUpdater->backingLayer()->getTransform().load(transform);
53    layerUpdater->setSize(width, height);
54    layerUpdater->setTransform(&transform);
55
56    // updateLayer so it's ready to draw
57    layerUpdater->updateLayer(true, GL_TEXTURE_EXTERNAL_OES, Matrix4::identity().data);
58    return layerUpdater;
59}
60
61void TestUtils::layoutTextUnscaled(const SkPaint& paint, const char* text,
62        std::vector<glyph_t>* outGlyphs, std::vector<float>* outPositions,
63        float* outTotalAdvance, Rect* outBounds) {
64    Rect bounds;
65    float totalAdvance = 0;
66    SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry);
67    SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I());
68    while (*text != '\0') {
69        size_t nextIndex = 0;
70        int32_t unichar = utf32_from_utf8_at(text, 4, 0, &nextIndex);
71        text += nextIndex;
72
73        glyph_t glyph = autoCache.getCache()->unicharToGlyph(unichar);
74        autoCache.getCache()->unicharToGlyph(unichar);
75
76        // push glyph and its relative position
77        outGlyphs->push_back(glyph);
78        outPositions->push_back(totalAdvance);
79        outPositions->push_back(0);
80
81        // compute bounds
82        SkGlyph skGlyph = autoCache.getCache()->getUnicharMetrics(unichar);
83        Rect glyphBounds(skGlyph.fWidth, skGlyph.fHeight);
84        glyphBounds.translate(totalAdvance + skGlyph.fLeft, skGlyph.fTop);
85        bounds.unionWith(glyphBounds);
86
87        // advance next character
88        SkScalar skWidth;
89        paint.getTextWidths(&glyph, sizeof(glyph), &skWidth, NULL);
90        totalAdvance += skWidth;
91    }
92    *outBounds = bounds;
93    *outTotalAdvance = totalAdvance;
94}
95
96
97void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text,
98        const SkPaint& paint, float x, float y) {
99    auto utf16 = asciiToUtf16(text);
100    canvas->drawText(utf16.get(), 0, strlen(text), strlen(text), x, y, 0, paint, nullptr);
101}
102
103void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text,
104        const SkPaint& paint, const SkPath& path) {
105    auto utf16 = asciiToUtf16(text);
106    canvas->drawTextOnPath(utf16.get(), strlen(text), 0, path, 0, 0, paint, nullptr);
107}
108
109void TestUtils::TestTask::run() {
110    // RenderState only valid once RenderThread is running, so queried here
111    renderthread::RenderThread& renderThread = renderthread::RenderThread::getInstance();
112    renderThread.eglManager().initialize();
113
114    rtCallback(renderThread);
115
116    renderThread.renderState().flush(Caches::FlushMode::Full);
117    renderThread.eglManager().destroy();
118}
119
120std::unique_ptr<uint16_t[]> TestUtils::asciiToUtf16(const char* str) {
121    const int length = strlen(str);
122    std::unique_ptr<uint16_t[]> utf16(new uint16_t[length]);
123    for (int i = 0; i < length; i++) {
124        utf16.get()[i] = str[i];
125    }
126    return utf16;
127}
128
129SkColor TestUtils::getColor(const sk_sp<SkSurface>& surface, int x, int y) {
130    SkPixmap pixmap;
131    if (!surface->peekPixels(&pixmap)) {
132        return 0;
133    }
134    switch (pixmap.colorType()) {
135        case kGray_8_SkColorType: {
136            const uint8_t* addr = pixmap.addr8(x, y);
137            return SkColorSetRGB(*addr, *addr, *addr);
138        }
139        case kAlpha_8_SkColorType: {
140            const uint8_t* addr = pixmap.addr8(x, y);
141            return SkColorSetA(0, addr[0]);
142        }
143        case kRGB_565_SkColorType: {
144            const uint16_t* addr = pixmap.addr16(x, y);
145            return SkPixel16ToColor(addr[0]);
146        }
147        case kARGB_4444_SkColorType: {
148            const uint16_t* addr = pixmap.addr16(x, y);
149            SkPMColor c = SkPixel4444ToPixel32(addr[0]);
150            return SkUnPreMultiply::PMColorToColor(c);
151        }
152        case kBGRA_8888_SkColorType: {
153            const uint32_t* addr = pixmap.addr32(x, y);
154            SkPMColor c = SkSwizzle_BGRA_to_PMColor(addr[0]);
155            return SkUnPreMultiply::PMColorToColor(c);
156        }
157        case kRGBA_8888_SkColorType: {
158            const uint32_t* addr = pixmap.addr32(x, y);
159            SkPMColor c = SkSwizzle_RGBA_to_PMColor(addr[0]);
160            return SkUnPreMultiply::PMColorToColor(c);
161        }
162        default:
163            return 0;
164    }
165    return 0;
166}
167
168SkRect TestUtils::getClipBounds(const SkCanvas* canvas) {
169    SkClipStack::BoundsType boundType;
170    SkRect clipBounds;
171    canvas->getClipStack()->getBounds(&clipBounds, &boundType);
172    return clipBounds;
173}
174
175SkRect TestUtils::getLocalClipBounds(const SkCanvas* canvas) {
176    SkMatrix invertedTotalMatrix;
177    if (!canvas->getTotalMatrix().invert(&invertedTotalMatrix)) {
178        return SkRect::MakeEmpty();
179    }
180    SkRect outlineInDeviceCoord = TestUtils::getClipBounds(canvas);
181    SkRect outlineInLocalCoord;
182    invertedTotalMatrix.mapRect(&outlineInLocalCoord, outlineInDeviceCoord);
183    return outlineInLocalCoord;
184}
185
186} /* namespace uirenderer */
187} /* namespace android */
188