TestUtils.cpp revision 2a0732996dec1d03c575b98ea9abff75534f51d5
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 22#include <utils/Unicode.h> 23 24namespace android { 25namespace uirenderer { 26 27SkColor TestUtils::interpolateColor(float fraction, SkColor start, SkColor end) { 28 int startA = (start >> 24) & 0xff; 29 int startR = (start >> 16) & 0xff; 30 int startG = (start >> 8) & 0xff; 31 int startB = start & 0xff; 32 33 int endA = (end >> 24) & 0xff; 34 int endR = (end >> 16) & 0xff; 35 int endG = (end >> 8) & 0xff; 36 int endB = end & 0xff; 37 38 return (int)((startA + (int)(fraction * (endA - startA))) << 24) 39 | (int)((startR + (int)(fraction * (endR - startR))) << 16) 40 | (int)((startG + (int)(fraction * (endG - startG))) << 8) 41 | (int)((startB + (int)(fraction * (endB - startB)))); 42} 43 44sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater( 45 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height, 46 std::function<void(Matrix4*)> transformSetupCallback) { 47 bool isOpaque = true; 48 bool forceFilter = true; 49 GLenum renderTarget = GL_TEXTURE_EXTERNAL_OES; 50 51 Layer* layer = LayerRenderer::createTextureLayer(renderThread.renderState()); 52 LayerRenderer::updateTextureLayer(layer, width, height, isOpaque, forceFilter, 53 renderTarget, Matrix4::identity().data); 54 transformSetupCallback(&(layer->getTransform())); 55 56 sp<DeferredLayerUpdater> layerUpdater = new DeferredLayerUpdater(layer); 57 return layerUpdater; 58} 59 60void TestUtils::layoutTextUnscaled(const SkPaint& paint, const char* text, 61 std::vector<glyph_t>* outGlyphs, std::vector<float>* outPositions, 62 float* outTotalAdvance, Rect* outBounds) { 63 Rect bounds; 64 float totalAdvance = 0; 65 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); 66 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I()); 67 while (*text != '\0') { 68 size_t nextIndex = 0; 69 int32_t unichar = utf32_from_utf8_at(text, 4, 0, &nextIndex); 70 text += nextIndex; 71 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 95void TestUtils::drawUtf8ToCanvas(TestCanvas* canvas, const char* text, 96 const SkPaint& paint, float x, float y) { 97 // drawing text requires GlyphID TextEncoding (which JNI layer would have done) 98 LOG_ALWAYS_FATAL_IF(paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding, 99 "must use glyph encoding"); 100 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); 101 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I()); 102 103 std::vector<glyph_t> glyphs; 104 std::vector<float> positions; 105 float totalAdvance; 106 Rect bounds; 107 layoutTextUnscaled(paint, text, &glyphs, &positions, &totalAdvance, &bounds); 108 109 // apply alignment via x parameter (which JNI layer would have done) 110 if (paint.getTextAlign() == SkPaint::kCenter_Align) { 111 x -= totalAdvance / 2; 112 } else if (paint.getTextAlign() == SkPaint::kRight_Align) { 113 x -= totalAdvance; 114 } 115 116 bounds.translate(x, y); 117 118 // Force left alignment, since alignment offset is already baked in 119 SkPaint alignPaintCopy(paint); 120 alignPaintCopy.setTextAlign(SkPaint::kLeft_Align); 121 canvas->drawGlyphs(glyphs.data(), positions.data(), glyphs.size(), alignPaintCopy, x, y, 122 bounds.left, bounds.top, bounds.right, bounds.bottom, totalAdvance); 123} 124 125void TestUtils::drawUtf8ToCanvas(TestCanvas* canvas, const char* text, 126 const SkPaint& paint, const SkPath& path) { 127 // drawing text requires GlyphID TextEncoding (which JNI layer would have done) 128 LOG_ALWAYS_FATAL_IF(paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding, 129 "must use glyph encoding"); 130 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); 131 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I()); 132 133 std::vector<glyph_t> glyphs; 134 while (*text != '\0') { 135 size_t nextIndex = 0; 136 int32_t unichar = utf32_from_utf8_at(text, 4, 0, &nextIndex); 137 text += nextIndex; 138 139 glyphs.push_back(autoCache.getCache()->unicharToGlyph(unichar)); 140 } 141 canvas->drawGlyphsOnPath(glyphs.data(), glyphs.size(), path, 0, 0, paint); 142} 143 144void TestUtils::TestTask::run() { 145 // RenderState only valid once RenderThread is running, so queried here 146 RenderState& renderState = renderthread::RenderThread::getInstance().renderState(); 147 148 renderState.onGLContextCreated(); 149 rtCallback(renderthread::RenderThread::getInstance()); 150 renderState.flush(Caches::FlushMode::Full); 151 renderState.onGLContextDestroyed(); 152} 153 154std::unique_ptr<uint16_t[]> TestUtils::utf8ToUtf16(const char* str) { 155 const size_t strLen = strlen(str); 156 const ssize_t utf16Len = utf8_to_utf16_length((uint8_t*) str, strLen); 157 std::unique_ptr<uint16_t[]> dst(new uint16_t[utf16Len + 1]); 158 utf8_to_utf16((uint8_t*) str, strLen, (char16_t*) dst.get()); 159 return dst; 160} 161 162} /* namespace uirenderer */ 163} /* namespace android */ 164