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 | |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 19 | #include "hwui/Paint.h" |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 20 | #include "DeferredLayerUpdater.h" |
| 21 | #include "LayerRenderer.h" |
| 22 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 23 | namespace android { |
| 24 | namespace uirenderer { |
| 25 | |
| 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 | |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 43 | sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater( |
| 44 | renderthread::RenderThread& renderThread, uint32_t width, uint32_t height, |
Chris Craik | 243e85b | 2016-03-25 15:26:11 -0700 | [diff] [blame^] | 45 | const SkMatrix& transform) { |
| 46 | Layer* layer = LayerRenderer::createTextureLayer(renderThread.renderState()); |
| 47 | |
| 48 | sp<DeferredLayerUpdater> layerUpdater = new DeferredLayerUpdater(layer); |
| 49 | layerUpdater->setSize(width, height); |
| 50 | layerUpdater->setTransform(&transform); |
| 51 | |
| 52 | // updateLayer so it's ready to draw |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 53 | bool isOpaque = true; |
| 54 | bool forceFilter = true; |
| 55 | GLenum renderTarget = GL_TEXTURE_EXTERNAL_OES; |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 56 | LayerRenderer::updateTextureLayer(layer, width, height, isOpaque, forceFilter, |
Chris Craik | 243e85b | 2016-03-25 15:26:11 -0700 | [diff] [blame^] | 57 | renderTarget, Matrix4::identity().data); |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 58 | |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 59 | return layerUpdater; |
| 60 | } |
| 61 | |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 62 | void TestUtils::layoutTextUnscaled(const SkPaint& paint, const char* text, |
| 63 | std::vector<glyph_t>* outGlyphs, std::vector<float>* outPositions, |
| 64 | float* outTotalAdvance, Rect* outBounds) { |
| 65 | Rect bounds; |
| 66 | float totalAdvance = 0; |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 67 | SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 68 | SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I()); |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 69 | while (*text != '\0') { |
| 70 | SkUnichar unichar = SkUTF8_NextUnichar(&text); |
| 71 | glyph_t glyph = autoCache.getCache()->unicharToGlyph(unichar); |
| 72 | autoCache.getCache()->unicharToGlyph(unichar); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 73 | |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 74 | // push glyph and its relative position |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 75 | outGlyphs->push_back(glyph); |
| 76 | outPositions->push_back(totalAdvance); |
| 77 | outPositions->push_back(0); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 78 | |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 79 | // compute bounds |
| 80 | SkGlyph skGlyph = autoCache.getCache()->getUnicharMetrics(unichar); |
| 81 | Rect glyphBounds(skGlyph.fWidth, skGlyph.fHeight); |
| 82 | glyphBounds.translate(totalAdvance + skGlyph.fLeft, skGlyph.fTop); |
| 83 | bounds.unionWith(glyphBounds); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 84 | |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 85 | // advance next character |
| 86 | SkScalar skWidth; |
| 87 | paint.getTextWidths(&glyph, sizeof(glyph), &skWidth, NULL); |
| 88 | totalAdvance += skWidth; |
| 89 | } |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 90 | *outBounds = bounds; |
| 91 | *outTotalAdvance = totalAdvance; |
| 92 | } |
| 93 | |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 94 | |
| 95 | void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 96 | const SkPaint& paint, float x, float y) { |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 97 | auto utf16 = asciiToUtf16(text); |
| 98 | canvas->drawText(utf16.get(), 0, strlen(text), strlen(text), x, y, 0, paint, nullptr); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 101 | void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 102 | const SkPaint& paint, const SkPath& path) { |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 103 | auto utf16 = asciiToUtf16(text); |
| 104 | canvas->drawTextOnPath(utf16.get(), strlen(text), 0, path, 0, 0, paint, nullptr); |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 105 | } |
| 106 | |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 107 | void TestUtils::TestTask::run() { |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 108 | // RenderState only valid once RenderThread is running, so queried here |
| 109 | RenderState& renderState = renderthread::RenderThread::getInstance().renderState(); |
| 110 | |
| 111 | renderState.onGLContextCreated(); |
| 112 | rtCallback(renderthread::RenderThread::getInstance()); |
Chris Craik | 1bc4ee4 | 2016-02-19 15:57:45 -0800 | [diff] [blame] | 113 | renderState.flush(Caches::FlushMode::Full); |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 114 | renderState.onGLContextDestroyed(); |
| 115 | } |
| 116 | |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 117 | std::unique_ptr<uint16_t[]> TestUtils::asciiToUtf16(const char* str) { |
| 118 | const int length = strlen(str); |
| 119 | std::unique_ptr<uint16_t[]> utf16(new uint16_t[length]); |
| 120 | for (int i = 0; i < length; i++) { |
| 121 | utf16.get()[i] = str[i]; |
| 122 | } |
| 123 | return utf16; |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 124 | } |
| 125 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 126 | } /* namespace uirenderer */ |
| 127 | } /* namespace android */ |