TestUtils.cpp revision afbd0f1fef46ef0ddf633dfde0de724db3da1405
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::layoutTextUnscaled(const SkPaint& paint, const char* text, 59 std::vector<glyph_t>* outGlyphs, std::vector<float>* outPositions, 60 float* outTotalAdvance, Rect* outBounds) { 61 Rect bounds; 62 float totalAdvance = 0; 63 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); 64 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I()); 65 while (*text != '\0') { 66 SkUnichar unichar = SkUTF8_NextUnichar(&text); 67 glyph_t glyph = autoCache.getCache()->unicharToGlyph(unichar); 68 autoCache.getCache()->unicharToGlyph(unichar); 69 70 // push glyph and its relative position 71 outGlyphs->push_back(glyph); 72 outPositions->push_back(totalAdvance); 73 outPositions->push_back(0); 74 75 // compute bounds 76 SkGlyph skGlyph = autoCache.getCache()->getUnicharMetrics(unichar); 77 Rect glyphBounds(skGlyph.fWidth, skGlyph.fHeight); 78 glyphBounds.translate(totalAdvance + skGlyph.fLeft, skGlyph.fTop); 79 bounds.unionWith(glyphBounds); 80 81 // advance next character 82 SkScalar skWidth; 83 paint.getTextWidths(&glyph, sizeof(glyph), &skWidth, NULL); 84 totalAdvance += skWidth; 85 } 86 *outBounds = bounds; 87 *outTotalAdvance = totalAdvance; 88} 89 90void TestUtils::drawTextToCanvas(TestCanvas* canvas, const char* text, 91 const SkPaint& paint, float x, float y) { 92 // drawing text requires GlyphID TextEncoding (which JNI layer would have done) 93 LOG_ALWAYS_FATAL_IF(paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding, 94 "must use glyph encoding"); 95 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); 96 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I()); 97 98 std::vector<glyph_t> glyphs; 99 std::vector<float> positions; 100 float totalAdvance; 101 Rect bounds; 102 layoutTextUnscaled(paint, text, &glyphs, &positions, &totalAdvance, &bounds); 103 104 // apply alignment via x parameter (which JNI layer would have done) 105 if (paint.getTextAlign() == SkPaint::kCenter_Align) { 106 x -= totalAdvance / 2; 107 } else if (paint.getTextAlign() == SkPaint::kRight_Align) { 108 x -= totalAdvance; 109 } 110 111 bounds.translate(x, y); 112 113 // Force left alignment, since alignment offset is already baked in 114 SkPaint alignPaintCopy(paint); 115 alignPaintCopy.setTextAlign(SkPaint::kLeft_Align); 116 canvas->drawText(glyphs.data(), positions.data(), glyphs.size(), alignPaintCopy, x, y, 117 bounds.left, bounds.top, bounds.right, bounds.bottom, totalAdvance); 118} 119 120void TestUtils::drawTextToCanvas(TestCanvas* canvas, const char* text, 121 const SkPaint& paint, const SkPath& path) { 122 // drawing text requires GlyphID TextEncoding (which JNI layer would have done) 123 LOG_ALWAYS_FATAL_IF(paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding, 124 "must use glyph encoding"); 125 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); 126 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I()); 127 128 std::vector<glyph_t> glyphs; 129 while (*text != '\0') { 130 SkUnichar unichar = SkUTF8_NextUnichar(&text); 131 glyphs.push_back(autoCache.getCache()->unicharToGlyph(unichar)); 132 } 133 canvas->drawTextOnPath(glyphs.data(), glyphs.size(), path, 0, 0, paint); 134} 135 136void TestUtils::TestTask::run() { 137 // RenderState only valid once RenderThread is running, so queried here 138 RenderState& renderState = renderthread::RenderThread::getInstance().renderState(); 139 140 renderState.onGLContextCreated(); 141 rtCallback(renderthread::RenderThread::getInstance()); 142 renderState.flush(Caches::FlushMode::Full); 143 renderState.onGLContextDestroyed(); 144} 145 146} /* namespace uirenderer */ 147} /* namespace android */ 148