TestUtils.cpp revision df55ced813357bfa20fe2685da8a6684e65bcbd1
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 <utils/Unicode.h> 24 25namespace android { 26namespace uirenderer { 27 28SkColor TestUtils::interpolateColor(float fraction, SkColor start, SkColor end) { 29 int startA = (start >> 24) & 0xff; 30 int startR = (start >> 16) & 0xff; 31 int startG = (start >> 8) & 0xff; 32 int startB = start & 0xff; 33 34 int endA = (end >> 24) & 0xff; 35 int endR = (end >> 16) & 0xff; 36 int endG = (end >> 8) & 0xff; 37 int endB = end & 0xff; 38 39 return (int)((startA + (int)(fraction * (endA - startA))) << 24) 40 | (int)((startR + (int)(fraction * (endR - startR))) << 16) 41 | (int)((startG + (int)(fraction * (endG - startG))) << 8) 42 | (int)((startB + (int)(fraction * (endB - startB)))); 43} 44 45sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater( 46 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height, 47 const SkMatrix& transform) { 48 Layer* layer = LayerRenderer::createTextureLayer(renderThread.renderState()); 49 layer->getTransform().load(transform); 50 51 sp<DeferredLayerUpdater> layerUpdater = new DeferredLayerUpdater(layer); 52 layerUpdater->setSize(width, height); 53 layerUpdater->setTransform(&transform); 54 55 // updateLayer so it's ready to draw 56 bool isOpaque = true; 57 bool forceFilter = true; 58 GLenum renderTarget = GL_TEXTURE_EXTERNAL_OES; 59 LayerRenderer::updateTextureLayer(layer, width, height, isOpaque, forceFilter, 60 renderTarget, Matrix4::identity().data); 61 62 return layerUpdater; 63} 64 65void TestUtils::layoutTextUnscaled(const SkPaint& paint, const char* text, 66 std::vector<glyph_t>* outGlyphs, std::vector<float>* outPositions, 67 float* outTotalAdvance, Rect* outBounds) { 68 Rect bounds; 69 float totalAdvance = 0; 70 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); 71 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I()); 72 while (*text != '\0') { 73 size_t nextIndex = 0; 74 int32_t unichar = utf32_from_utf8_at(text, 4, 0, &nextIndex); 75 text += nextIndex; 76 77 glyph_t glyph = autoCache.getCache()->unicharToGlyph(unichar); 78 autoCache.getCache()->unicharToGlyph(unichar); 79 80 // push glyph and its relative position 81 outGlyphs->push_back(glyph); 82 outPositions->push_back(totalAdvance); 83 outPositions->push_back(0); 84 85 // compute bounds 86 SkGlyph skGlyph = autoCache.getCache()->getUnicharMetrics(unichar); 87 Rect glyphBounds(skGlyph.fWidth, skGlyph.fHeight); 88 glyphBounds.translate(totalAdvance + skGlyph.fLeft, skGlyph.fTop); 89 bounds.unionWith(glyphBounds); 90 91 // advance next character 92 SkScalar skWidth; 93 paint.getTextWidths(&glyph, sizeof(glyph), &skWidth, NULL); 94 totalAdvance += skWidth; 95 } 96 *outBounds = bounds; 97 *outTotalAdvance = totalAdvance; 98} 99 100 101void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, 102 const SkPaint& paint, float x, float y) { 103 auto utf16 = asciiToUtf16(text); 104 canvas->drawText(utf16.get(), 0, strlen(text), strlen(text), x, y, 0, paint, nullptr); 105} 106 107void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, 108 const SkPaint& paint, const SkPath& path) { 109 auto utf16 = asciiToUtf16(text); 110 canvas->drawTextOnPath(utf16.get(), strlen(text), 0, path, 0, 0, paint, nullptr); 111} 112 113void TestUtils::TestTask::run() { 114 // RenderState only valid once RenderThread is running, so queried here 115 RenderState& renderState = renderthread::RenderThread::getInstance().renderState(); 116 117 renderState.onGLContextCreated(); 118 rtCallback(renderthread::RenderThread::getInstance()); 119 renderState.flush(Caches::FlushMode::Full); 120 renderState.onGLContextDestroyed(); 121} 122 123std::unique_ptr<uint16_t[]> TestUtils::asciiToUtf16(const char* str) { 124 const int length = strlen(str); 125 std::unique_ptr<uint16_t[]> utf16(new uint16_t[length]); 126 for (int i = 0; i < length; i++) { 127 utf16.get()[i] = str[i]; 128 } 129 return utf16; 130} 131 132} /* namespace uirenderer */ 133} /* namespace android */ 134