John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 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 | |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 19 | #include "DeferredLayerUpdater.h" |
| 20 | #include "LayerRenderer.h" |
| 21 | |
Ben Wagner | f4cf5d3 | 2016-02-22 15:25:35 -0500 | [diff] [blame^] | 22 | #include <utils/Unicode.h> |
| 23 | |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | #include <signal.h> |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 26 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
| 30 | SkColor 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 | |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 47 | sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater( |
| 48 | renderthread::RenderThread& renderThread, uint32_t width, uint32_t height, |
| 49 | std::function<void(Matrix4*)> transformSetupCallback) { |
| 50 | bool isOpaque = true; |
| 51 | bool forceFilter = true; |
| 52 | GLenum renderTarget = GL_TEXTURE_EXTERNAL_OES; |
| 53 | |
| 54 | Layer* layer = LayerRenderer::createTextureLayer(renderThread.renderState()); |
| 55 | LayerRenderer::updateTextureLayer(layer, width, height, isOpaque, forceFilter, |
| 56 | renderTarget, Matrix4::identity().data); |
| 57 | transformSetupCallback(&(layer->getTransform())); |
| 58 | |
| 59 | sp<DeferredLayerUpdater> layerUpdater = new DeferredLayerUpdater(layer); |
| 60 | return layerUpdater; |
| 61 | } |
| 62 | |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 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; |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 68 | SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 69 | SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I()); |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 70 | while (*text != '\0') { |
Ben Wagner | f4cf5d3 | 2016-02-22 15:25:35 -0500 | [diff] [blame^] | 71 | size_t nextIndex = 0; |
| 72 | int32_t unichar = utf32_from_utf8_at(text, 4, 0, &nextIndex); |
| 73 | text += nextIndex; |
| 74 | |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 75 | glyph_t glyph = autoCache.getCache()->unicharToGlyph(unichar); |
| 76 | autoCache.getCache()->unicharToGlyph(unichar); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 77 | |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 78 | // push glyph and its relative position |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 79 | outGlyphs->push_back(glyph); |
| 80 | outPositions->push_back(totalAdvance); |
| 81 | outPositions->push_back(0); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 82 | |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 83 | // compute bounds |
| 84 | SkGlyph skGlyph = autoCache.getCache()->getUnicharMetrics(unichar); |
| 85 | Rect glyphBounds(skGlyph.fWidth, skGlyph.fHeight); |
| 86 | glyphBounds.translate(totalAdvance + skGlyph.fLeft, skGlyph.fTop); |
| 87 | bounds.unionWith(glyphBounds); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 88 | |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 89 | // advance next character |
| 90 | SkScalar skWidth; |
| 91 | paint.getTextWidths(&glyph, sizeof(glyph), &skWidth, NULL); |
| 92 | totalAdvance += skWidth; |
| 93 | } |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 94 | *outBounds = bounds; |
| 95 | *outTotalAdvance = totalAdvance; |
| 96 | } |
| 97 | |
| 98 | void TestUtils::drawTextToCanvas(TestCanvas* canvas, const char* text, |
| 99 | const SkPaint& paint, float x, float y) { |
| 100 | // drawing text requires GlyphID TextEncoding (which JNI layer would have done) |
| 101 | LOG_ALWAYS_FATAL_IF(paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding, |
| 102 | "must use glyph encoding"); |
| 103 | SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); |
| 104 | SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I()); |
| 105 | |
| 106 | std::vector<glyph_t> glyphs; |
| 107 | std::vector<float> positions; |
| 108 | float totalAdvance; |
| 109 | Rect bounds; |
| 110 | layoutTextUnscaled(paint, text, &glyphs, &positions, &totalAdvance, &bounds); |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 111 | |
| 112 | // apply alignment via x parameter (which JNI layer would have done) |
| 113 | if (paint.getTextAlign() == SkPaint::kCenter_Align) { |
| 114 | x -= totalAdvance / 2; |
| 115 | } else if (paint.getTextAlign() == SkPaint::kRight_Align) { |
| 116 | x -= totalAdvance; |
| 117 | } |
| 118 | |
| 119 | bounds.translate(x, y); |
| 120 | |
| 121 | // Force left alignment, since alignment offset is already baked in |
| 122 | SkPaint alignPaintCopy(paint); |
| 123 | alignPaintCopy.setTextAlign(SkPaint::kLeft_Align); |
| 124 | canvas->drawText(glyphs.data(), positions.data(), glyphs.size(), alignPaintCopy, x, y, |
| 125 | bounds.left, bounds.top, bounds.right, bounds.bottom, totalAdvance); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 128 | void TestUtils::drawTextToCanvas(TestCanvas* canvas, const char* text, |
| 129 | const SkPaint& paint, const SkPath& path) { |
| 130 | // drawing text requires GlyphID TextEncoding (which JNI layer would have done) |
| 131 | LOG_ALWAYS_FATAL_IF(paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding, |
| 132 | "must use glyph encoding"); |
| 133 | SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); |
| 134 | SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I()); |
| 135 | |
| 136 | std::vector<glyph_t> glyphs; |
| 137 | while (*text != '\0') { |
Ben Wagner | f4cf5d3 | 2016-02-22 15:25:35 -0500 | [diff] [blame^] | 138 | size_t nextIndex = 0; |
| 139 | int32_t unichar = utf32_from_utf8_at(text, 4, 0, &nextIndex); |
| 140 | text += nextIndex; |
| 141 | |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 142 | glyphs.push_back(autoCache.getCache()->unicharToGlyph(unichar)); |
| 143 | } |
| 144 | canvas->drawTextOnPath(glyphs.data(), glyphs.size(), path, 0, 0, paint); |
| 145 | } |
| 146 | |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 147 | static void defaultCrashHandler() { |
| 148 | fprintf(stderr, "RenderThread crashed!"); |
| 149 | } |
| 150 | |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 151 | static std::function<void()> gCrashHandler = defaultCrashHandler; |
John Reck | f1dafb5 | 2016-01-15 16:57:26 -0800 | [diff] [blame] | 152 | static sighandler_t gPreviousSignalHandler; |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 153 | |
| 154 | static void signalHandler(int sig) { |
John Reck | f1dafb5 | 2016-01-15 16:57:26 -0800 | [diff] [blame] | 155 | gCrashHandler(); |
| 156 | if (gPreviousSignalHandler) { |
| 157 | gPreviousSignalHandler(sig); |
| 158 | } |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void TestUtils::setRenderThreadCrashHandler(std::function<void()> crashHandler) { |
| 162 | gCrashHandler = crashHandler; |
| 163 | } |
| 164 | |
| 165 | void TestUtils::TestTask::run() { |
John Reck | f1dafb5 | 2016-01-15 16:57:26 -0800 | [diff] [blame] | 166 | gPreviousSignalHandler = signal(SIGABRT, signalHandler); |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 167 | |
| 168 | // RenderState only valid once RenderThread is running, so queried here |
| 169 | RenderState& renderState = renderthread::RenderThread::getInstance().renderState(); |
| 170 | |
| 171 | renderState.onGLContextCreated(); |
| 172 | rtCallback(renderthread::RenderThread::getInstance()); |
Chris Craik | 1bc4ee4 | 2016-02-19 15:57:45 -0800 | [diff] [blame] | 173 | renderState.flush(Caches::FlushMode::Full); |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 174 | renderState.onGLContextDestroyed(); |
John Reck | f1dafb5 | 2016-01-15 16:57:26 -0800 | [diff] [blame] | 175 | |
| 176 | // Restore the previous signal handler |
| 177 | signal(SIGABRT, gPreviousSignalHandler); |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 178 | } |
| 179 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 180 | } /* namespace uirenderer */ |
| 181 | } /* namespace android */ |