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 namespace android {
24 namespace uirenderer {
25 
interpolateColor(float fraction,SkColor start,SkColor end)26 SkColor 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 
createTextureLayerUpdater(renderthread::RenderThread & renderThread,uint32_t width,uint32_t height,const SkMatrix & transform)43 sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
44         renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
45         const SkMatrix& transform) {
46     Layer* layer = LayerRenderer::createTextureLayer(renderThread.renderState());
47     layer->getTransform().load(transform);
48 
49     sp<DeferredLayerUpdater> layerUpdater = new DeferredLayerUpdater(layer);
50     layerUpdater->setSize(width, height);
51     layerUpdater->setTransform(&transform);
52 
53     // updateLayer so it's ready to draw
54     bool isOpaque = true;
55     bool forceFilter = true;
56     GLenum renderTarget = GL_TEXTURE_EXTERNAL_OES;
57     LayerRenderer::updateTextureLayer(layer, width, height, isOpaque, forceFilter,
58     renderTarget, Matrix4::identity().data);
59 
60     return layerUpdater;
61 }
62 
layoutTextUnscaled(const SkPaint & paint,const char * text,std::vector<glyph_t> * outGlyphs,std::vector<float> * outPositions,float * outTotalAdvance,Rect * outBounds)63 void TestUtils::layoutTextUnscaled(const SkPaint& paint, const char* text,
64         std::vector<glyph_t>* outGlyphs, std::vector<float>* outPositions,
65         float* outTotalAdvance, Rect* outBounds) {
66     Rect bounds;
67     float totalAdvance = 0;
68     SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry);
69     SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I());
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         outGlyphs->push_back(glyph);
77         outPositions->push_back(totalAdvance);
78         outPositions->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     *outBounds = bounds;
92     *outTotalAdvance = totalAdvance;
93 }
94 
95 
drawUtf8ToCanvas(Canvas * canvas,const char * text,const SkPaint & paint,float x,float y)96 void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text,
97         const SkPaint& paint, float x, float y) {
98     auto utf16 = asciiToUtf16(text);
99     canvas->drawText(utf16.get(), 0, strlen(text), strlen(text), x, y, 0, paint, nullptr);
100 }
101 
drawUtf8ToCanvas(Canvas * canvas,const char * text,const SkPaint & paint,const SkPath & path)102 void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text,
103         const SkPaint& paint, const SkPath& path) {
104     auto utf16 = asciiToUtf16(text);
105     canvas->drawTextOnPath(utf16.get(), strlen(text), 0, path, 0, 0, paint, nullptr);
106 }
107 
run()108 void TestUtils::TestTask::run() {
109     // RenderState only valid once RenderThread is running, so queried here
110     RenderState& renderState = renderthread::RenderThread::getInstance().renderState();
111 
112     renderState.onGLContextCreated();
113     rtCallback(renderthread::RenderThread::getInstance());
114     renderState.flush(Caches::FlushMode::Full);
115     renderState.onGLContextDestroyed();
116 }
117 
asciiToUtf16(const char * str)118 std::unique_ptr<uint16_t[]> TestUtils::asciiToUtf16(const char* str) {
119     const int length = strlen(str);
120     std::unique_ptr<uint16_t[]> utf16(new uint16_t[length]);
121     for (int i = 0; i < length; i++) {
122         utf16.get()[i] = str[i];
123     }
124     return utf16;
125 }
126 
127 } /* namespace uirenderer */
128 } /* namespace android */
129