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 "hwui/Paint.h"
21
22 #include <SkClipStack.h>
23 #include <minikin/Layout.h>
24 #include <pipeline/skia/SkiaOpenGLPipeline.h>
25 #include <pipeline/skia/SkiaVulkanPipeline.h>
26 #include <renderthread/EglManager.h>
27 #include <renderthread/OpenGLPipeline.h>
28 #include <renderthread/VulkanManager.h>
29 #include <utils/Unicode.h>
30
31 #include <SkGlyphCache.h>
32
33 namespace android {
34 namespace uirenderer {
35
interpolateColor(float fraction,SkColor start,SkColor end)36 SkColor TestUtils::interpolateColor(float fraction, SkColor start, SkColor end) {
37 int startA = (start >> 24) & 0xff;
38 int startR = (start >> 16) & 0xff;
39 int startG = (start >> 8) & 0xff;
40 int startB = start & 0xff;
41
42 int endA = (end >> 24) & 0xff;
43 int endR = (end >> 16) & 0xff;
44 int endG = (end >> 8) & 0xff;
45 int endB = end & 0xff;
46
47 return (int)((startA + (int)(fraction * (endA - startA))) << 24) |
48 (int)((startR + (int)(fraction * (endR - startR))) << 16) |
49 (int)((startG + (int)(fraction * (endG - startG))) << 8) |
50 (int)((startB + (int)(fraction * (endB - startB))));
51 }
52
createTextureLayerUpdater(renderthread::RenderThread & renderThread)53 sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
54 renderthread::RenderThread& renderThread) {
55 android::uirenderer::renderthread::IRenderPipeline* pipeline;
56 if (Properties::getRenderPipelineType() == RenderPipelineType::OpenGL) {
57 pipeline = new renderthread::OpenGLPipeline(renderThread);
58 } else if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
59 pipeline = new skiapipeline::SkiaOpenGLPipeline(renderThread);
60 } else {
61 pipeline = new skiapipeline::SkiaVulkanPipeline(renderThread);
62 }
63 sp<DeferredLayerUpdater> layerUpdater = pipeline->createTextureLayer();
64 layerUpdater->apply();
65 delete pipeline;
66 return layerUpdater;
67 }
68
createTextureLayerUpdater(renderthread::RenderThread & renderThread,uint32_t width,uint32_t height,const SkMatrix & transform)69 sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
70 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
71 const SkMatrix& transform) {
72 sp<DeferredLayerUpdater> layerUpdater = createTextureLayerUpdater(renderThread);
73 layerUpdater->backingLayer()->getTransform().load(transform);
74 layerUpdater->setSize(width, height);
75 layerUpdater->setTransform(&transform);
76
77 // updateLayer so it's ready to draw
78 layerUpdater->updateLayer(true, Matrix4::identity().data, HAL_DATASPACE_UNKNOWN);
79 if (layerUpdater->backingLayer()->getApi() == Layer::Api::OpenGL) {
80 static_cast<GlLayer*>(layerUpdater->backingLayer())
81 ->setRenderTarget(GL_TEXTURE_EXTERNAL_OES);
82 }
83 return layerUpdater;
84 }
85
layoutTextUnscaled(const SkPaint & paint,const char * text,std::vector<glyph_t> * outGlyphs,std::vector<float> * outPositions,float * outTotalAdvance,Rect * outBounds)86 void TestUtils::layoutTextUnscaled(const SkPaint& paint, const char* text,
87 std::vector<glyph_t>* outGlyphs,
88 std::vector<float>* outPositions, float* outTotalAdvance,
89 Rect* outBounds) {
90 Rect bounds;
91 float totalAdvance = 0;
92 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry);
93 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I());
94 while (*text != '\0') {
95 size_t nextIndex = 0;
96 int32_t unichar = utf32_from_utf8_at(text, 4, 0, &nextIndex);
97 text += nextIndex;
98
99 glyph_t glyph = autoCache.getCache()->unicharToGlyph(unichar);
100 autoCache.getCache()->unicharToGlyph(unichar);
101
102 // push glyph and its relative position
103 outGlyphs->push_back(glyph);
104 outPositions->push_back(totalAdvance);
105 outPositions->push_back(0);
106
107 // compute bounds
108 SkGlyph skGlyph = autoCache.getCache()->getUnicharMetrics(unichar);
109 Rect glyphBounds(skGlyph.fWidth, skGlyph.fHeight);
110 glyphBounds.translate(totalAdvance + skGlyph.fLeft, skGlyph.fTop);
111 bounds.unionWith(glyphBounds);
112
113 // advance next character
114 SkScalar skWidth;
115 paint.getTextWidths(&glyph, sizeof(glyph), &skWidth, NULL);
116 totalAdvance += skWidth;
117 }
118 *outBounds = bounds;
119 *outTotalAdvance = totalAdvance;
120 }
121
drawUtf8ToCanvas(Canvas * canvas,const char * text,const SkPaint & paint,float x,float y)122 void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, const SkPaint& paint, float x,
123 float y) {
124 auto utf16 = asciiToUtf16(text);
125 SkPaint glyphPaint(paint);
126 glyphPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
127 canvas->drawText(utf16.get(), 0, strlen(text), strlen(text), x, y, minikin::Bidi::LTR,
128 glyphPaint, nullptr, nullptr /* measured text */);
129 }
130
drawUtf8ToCanvas(Canvas * canvas,const char * text,const SkPaint & paint,const SkPath & path)131 void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, const SkPaint& paint,
132 const SkPath& path) {
133 auto utf16 = asciiToUtf16(text);
134 SkPaint glyphPaint(paint);
135 glyphPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
136 canvas->drawTextOnPath(utf16.get(), strlen(text), minikin::Bidi::LTR, path, 0, 0, glyphPaint,
137 nullptr);
138 }
139
run()140 void TestUtils::TestTask::run() {
141 // RenderState only valid once RenderThread is running, so queried here
142 renderthread::RenderThread& renderThread = renderthread::RenderThread::getInstance();
143 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
144 renderThread.vulkanManager().initialize();
145 } else {
146 renderThread.eglManager().initialize();
147 }
148
149 rtCallback(renderThread);
150
151 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
152 renderThread.vulkanManager().destroy();
153 } else {
154 renderThread.renderState().flush(Caches::FlushMode::Full);
155 renderThread.eglManager().destroy();
156 }
157 }
158
asciiToUtf16(const char * str)159 std::unique_ptr<uint16_t[]> TestUtils::asciiToUtf16(const char* str) {
160 const int length = strlen(str);
161 std::unique_ptr<uint16_t[]> utf16(new uint16_t[length]);
162 for (int i = 0; i < length; i++) {
163 utf16.get()[i] = str[i];
164 }
165 return utf16;
166 }
167
getColor(const sk_sp<SkSurface> & surface,int x,int y)168 SkColor TestUtils::getColor(const sk_sp<SkSurface>& surface, int x, int y) {
169 SkPixmap pixmap;
170 if (!surface->peekPixels(&pixmap)) {
171 return 0;
172 }
173 switch (pixmap.colorType()) {
174 case kGray_8_SkColorType: {
175 const uint8_t* addr = pixmap.addr8(x, y);
176 return SkColorSetRGB(*addr, *addr, *addr);
177 }
178 case kAlpha_8_SkColorType: {
179 const uint8_t* addr = pixmap.addr8(x, y);
180 return SkColorSetA(0, addr[0]);
181 }
182 case kRGB_565_SkColorType: {
183 const uint16_t* addr = pixmap.addr16(x, y);
184 return SkPixel16ToColor(addr[0]);
185 }
186 case kARGB_4444_SkColorType: {
187 const uint16_t* addr = pixmap.addr16(x, y);
188 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
189 return SkUnPreMultiply::PMColorToColor(c);
190 }
191 case kBGRA_8888_SkColorType: {
192 const uint32_t* addr = pixmap.addr32(x, y);
193 SkPMColor c = SkSwizzle_BGRA_to_PMColor(addr[0]);
194 return SkUnPreMultiply::PMColorToColor(c);
195 }
196 case kRGBA_8888_SkColorType: {
197 const uint32_t* addr = pixmap.addr32(x, y);
198 SkPMColor c = SkSwizzle_RGBA_to_PMColor(addr[0]);
199 return SkUnPreMultiply::PMColorToColor(c);
200 }
201 default:
202 return 0;
203 }
204 return 0;
205 }
206
getClipBounds(const SkCanvas * canvas)207 SkRect TestUtils::getClipBounds(const SkCanvas* canvas) {
208 return SkRect::Make(canvas->getDeviceClipBounds());
209 }
210
getLocalClipBounds(const SkCanvas * canvas)211 SkRect TestUtils::getLocalClipBounds(const SkCanvas* canvas) {
212 SkMatrix invertedTotalMatrix;
213 if (!canvas->getTotalMatrix().invert(&invertedTotalMatrix)) {
214 return SkRect::MakeEmpty();
215 }
216 SkRect outlineInDeviceCoord = TestUtils::getClipBounds(canvas);
217 SkRect outlineInLocalCoord;
218 invertedTotalMatrix.mapRect(&outlineInLocalCoord, outlineInDeviceCoord);
219 return outlineInLocalCoord;
220 }
221
222 } /* namespace uirenderer */
223 } /* namespace android */
224