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" |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 21 | |
sergeyv | 7dc370b | 2016-06-17 11:21:11 -0700 | [diff] [blame] | 22 | #include <renderthread/EglManager.h> |
Derek Sollenberger | 56ad6ec | 2016-07-22 12:13:32 -0400 | [diff] [blame] | 23 | #include <renderthread/OpenGLPipeline.h> |
Ben Wagner | f4cf5d3 | 2016-02-22 15:25:35 -0500 | [diff] [blame] | 24 | #include <utils/Unicode.h> |
| 25 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | |
| 29 | SkColor TestUtils::interpolateColor(float fraction, SkColor start, SkColor end) { |
| 30 | int startA = (start >> 24) & 0xff; |
| 31 | int startR = (start >> 16) & 0xff; |
| 32 | int startG = (start >> 8) & 0xff; |
| 33 | int startB = start & 0xff; |
| 34 | |
| 35 | int endA = (end >> 24) & 0xff; |
| 36 | int endR = (end >> 16) & 0xff; |
| 37 | int endG = (end >> 8) & 0xff; |
| 38 | int endB = end & 0xff; |
| 39 | |
| 40 | return (int)((startA + (int)(fraction * (endA - startA))) << 24) |
| 41 | | (int)((startR + (int)(fraction * (endR - startR))) << 16) |
| 42 | | (int)((startG + (int)(fraction * (endG - startG))) << 8) |
| 43 | | (int)((startB + (int)(fraction * (endB - startB)))); |
| 44 | } |
| 45 | |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 46 | sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater( |
| 47 | renderthread::RenderThread& renderThread, uint32_t width, uint32_t height, |
Chris Craik | 243e85b | 2016-03-25 15:26:11 -0700 | [diff] [blame] | 48 | const SkMatrix& transform) { |
Derek Sollenberger | 56ad6ec | 2016-07-22 12:13:32 -0400 | [diff] [blame] | 49 | renderthread::OpenGLPipeline pipeline(renderThread); |
| 50 | sp<DeferredLayerUpdater> layerUpdater = pipeline.createTextureLayer(); |
| 51 | layerUpdater->backingLayer()->getTransform().load(transform); |
Chris Craik | 243e85b | 2016-03-25 15:26:11 -0700 | [diff] [blame] | 52 | layerUpdater->setSize(width, height); |
| 53 | layerUpdater->setTransform(&transform); |
| 54 | |
| 55 | // updateLayer so it's ready to draw |
Derek Sollenberger | 56ad6ec | 2016-07-22 12:13:32 -0400 | [diff] [blame] | 56 | layerUpdater->updateLayer(true, GL_TEXTURE_EXTERNAL_OES, Matrix4::identity().data); |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 57 | return layerUpdater; |
| 58 | } |
| 59 | |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 60 | void 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; |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 65 | SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 66 | SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I()); |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 67 | while (*text != '\0') { |
Ben Wagner | f4cf5d3 | 2016-02-22 15:25:35 -0500 | [diff] [blame] | 68 | size_t nextIndex = 0; |
| 69 | int32_t unichar = utf32_from_utf8_at(text, 4, 0, &nextIndex); |
| 70 | text += nextIndex; |
| 71 | |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 72 | glyph_t glyph = autoCache.getCache()->unicharToGlyph(unichar); |
| 73 | autoCache.getCache()->unicharToGlyph(unichar); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 74 | |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 75 | // push glyph and its relative position |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 76 | outGlyphs->push_back(glyph); |
| 77 | outPositions->push_back(totalAdvance); |
| 78 | outPositions->push_back(0); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 79 | |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 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); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 85 | |
Chris Craik | 42a5407 | 2015-11-24 11:41:54 -0800 | [diff] [blame] | 86 | // advance next character |
| 87 | SkScalar skWidth; |
| 88 | paint.getTextWidths(&glyph, sizeof(glyph), &skWidth, NULL); |
| 89 | totalAdvance += skWidth; |
| 90 | } |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 91 | *outBounds = bounds; |
| 92 | *outTotalAdvance = totalAdvance; |
| 93 | } |
| 94 | |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 95 | |
| 96 | void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 97 | const SkPaint& paint, float x, float y) { |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 98 | auto utf16 = asciiToUtf16(text); |
| 99 | 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] | 100 | } |
| 101 | |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 102 | void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 103 | const SkPaint& paint, const SkPath& path) { |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 104 | auto utf16 = asciiToUtf16(text); |
| 105 | canvas->drawTextOnPath(utf16.get(), strlen(text), 0, path, 0, 0, paint, nullptr); |
Chris Craik | d7448e6 | 2015-12-15 10:34:36 -0800 | [diff] [blame] | 106 | } |
| 107 | |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 108 | void TestUtils::TestTask::run() { |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 109 | // RenderState only valid once RenderThread is running, so queried here |
sergeyv | 7dc370b | 2016-06-17 11:21:11 -0700 | [diff] [blame] | 110 | renderthread::RenderThread& renderThread = renderthread::RenderThread::getInstance(); |
Derek Sollenberger | 56ad6ec | 2016-07-22 12:13:32 -0400 | [diff] [blame] | 111 | renderThread.eglManager().initialize(); |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 112 | |
sergeyv | 7dc370b | 2016-06-17 11:21:11 -0700 | [diff] [blame] | 113 | rtCallback(renderThread); |
Derek Sollenberger | 56ad6ec | 2016-07-22 12:13:32 -0400 | [diff] [blame] | 114 | |
| 115 | renderThread.renderState().flush(Caches::FlushMode::Full); |
| 116 | renderThread.eglManager().destroy(); |
John Reck | e5da4ef | 2016-01-14 12:34:46 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Derek Sollenberger | 79abbf2 | 2016-03-24 11:07:19 -0400 | [diff] [blame] | 119 | std::unique_ptr<uint16_t[]> TestUtils::asciiToUtf16(const char* str) { |
| 120 | const int length = strlen(str); |
| 121 | std::unique_ptr<uint16_t[]> utf16(new uint16_t[length]); |
| 122 | for (int i = 0; i < length; i++) { |
| 123 | utf16.get()[i] = str[i]; |
| 124 | } |
| 125 | return utf16; |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame^] | 128 | SkColor TestUtils::getColor(const sk_sp<SkSurface>& surface, int x, int y) { |
| 129 | SkPixmap pixmap; |
| 130 | if (!surface->peekPixels(&pixmap)) { |
| 131 | return 0; |
| 132 | } |
| 133 | switch (pixmap.colorType()) { |
| 134 | case kGray_8_SkColorType: { |
| 135 | const uint8_t* addr = pixmap.addr8(x, y); |
| 136 | return SkColorSetRGB(*addr, *addr, *addr); |
| 137 | } |
| 138 | case kAlpha_8_SkColorType: { |
| 139 | const uint8_t* addr = pixmap.addr8(x, y); |
| 140 | return SkColorSetA(0, addr[0]); |
| 141 | } |
| 142 | case kRGB_565_SkColorType: { |
| 143 | const uint16_t* addr = pixmap.addr16(x, y); |
| 144 | return SkPixel16ToColor(addr[0]); |
| 145 | } |
| 146 | case kARGB_4444_SkColorType: { |
| 147 | const uint16_t* addr = pixmap.addr16(x, y); |
| 148 | SkPMColor c = SkPixel4444ToPixel32(addr[0]); |
| 149 | return SkUnPreMultiply::PMColorToColor(c); |
| 150 | } |
| 151 | case kBGRA_8888_SkColorType: { |
| 152 | const uint32_t* addr = pixmap.addr32(x, y); |
| 153 | SkPMColor c = SkSwizzle_BGRA_to_PMColor(addr[0]); |
| 154 | return SkUnPreMultiply::PMColorToColor(c); |
| 155 | } |
| 156 | case kRGBA_8888_SkColorType: { |
| 157 | const uint32_t* addr = pixmap.addr32(x, y); |
| 158 | SkPMColor c = SkSwizzle_RGBA_to_PMColor(addr[0]); |
| 159 | return SkUnPreMultiply::PMColorToColor(c); |
| 160 | } |
| 161 | default: |
| 162 | return 0; |
| 163 | } |
| 164 | return 0; |
| 165 | } |
| 166 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 167 | } /* namespace uirenderer */ |
| 168 | } /* namespace android */ |