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 <hwui/MinikinSkia.h>
23 #include <hwui/Typeface.h>
24 #include <minikin/Layout.h>
25 #include <pipeline/skia/SkiaOpenGLPipeline.h>
26 #include <pipeline/skia/SkiaVulkanPipeline.h>
27 #include <renderthread/EglManager.h>
28 #include <renderthread/VulkanManager.h>
29 #include <utils/Unicode.h>
30
31 #include "SkCanvas.h"
32 #include "SkColorData.h"
33 #include "SkMatrix.h"
34 #include "SkPath.h"
35 #include "SkPixmap.h"
36 #include "SkRect.h"
37 #include "SkSurface.h"
38 #include "SkUnPreMultiply.h"
39
40 namespace android {
41 namespace uirenderer {
42
43 std::unordered_map<int, TestUtils::CallCounts> TestUtils::sMockFunctorCounts{};
44
interpolateColor(float fraction,SkColor start,SkColor end)45 SkColor TestUtils::interpolateColor(float fraction, SkColor start, SkColor end) {
46 int startA = (start >> 24) & 0xff;
47 int startR = (start >> 16) & 0xff;
48 int startG = (start >> 8) & 0xff;
49 int startB = start & 0xff;
50
51 int endA = (end >> 24) & 0xff;
52 int endR = (end >> 16) & 0xff;
53 int endG = (end >> 8) & 0xff;
54 int endB = end & 0xff;
55
56 return (int)((startA + (int)(fraction * (endA - startA))) << 24) |
57 (int)((startR + (int)(fraction * (endR - startR))) << 16) |
58 (int)((startG + (int)(fraction * (endG - startG))) << 8) |
59 (int)((startB + (int)(fraction * (endB - startB))));
60 }
61
createTextureLayerUpdater(renderthread::RenderThread & renderThread)62 sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
63 renderthread::RenderThread& renderThread) {
64 android::uirenderer::renderthread::IRenderPipeline* pipeline;
65 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
66 pipeline = new skiapipeline::SkiaOpenGLPipeline(renderThread);
67 } else {
68 pipeline = new skiapipeline::SkiaVulkanPipeline(renderThread);
69 }
70 sp<DeferredLayerUpdater> layerUpdater = pipeline->createTextureLayer();
71 layerUpdater->apply();
72 delete pipeline;
73 return layerUpdater;
74 }
75
createTextureLayerUpdater(renderthread::RenderThread & renderThread,uint32_t width,uint32_t height,const SkMatrix & transform)76 sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
77 renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
78 const SkMatrix& transform) {
79 sp<DeferredLayerUpdater> layerUpdater = createTextureLayerUpdater(renderThread);
80 layerUpdater->backingLayer()->getTransform() = transform;
81 layerUpdater->setSize(width, height);
82 layerUpdater->setTransform(&transform);
83
84 // updateLayer so it's ready to draw
85 layerUpdater->updateLayer(true, nullptr, 0, SkRect::MakeEmpty());
86 return layerUpdater;
87 }
88
drawUtf8ToCanvas(Canvas * canvas,const char * text,const Paint & paint,float x,float y)89 void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint, float x,
90 float y) {
91 auto utf16 = asciiToUtf16(text);
92 uint32_t length = strlen(text);
93
94 canvas->drawText(utf16.get(), length, // text buffer
95 0, length, // draw range
96 0, length, // context range
97 x, y, minikin::Bidi::LTR, paint, nullptr, nullptr /* measured text */);
98 }
99
drawUtf8ToCanvas(Canvas * canvas,const char * text,const Paint & paint,const SkPath & path)100 void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint,
101 const SkPath& path) {
102 auto utf16 = asciiToUtf16(text);
103 canvas->drawTextOnPath(utf16.get(), strlen(text), minikin::Bidi::LTR, path, 0, 0, paint,
104 nullptr);
105 }
106
run()107 void TestUtils::TestTask::run() {
108 // RenderState only valid once RenderThread is running, so queried here
109 renderthread::RenderThread& renderThread = renderthread::RenderThread::getInstance();
110 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
111 renderThread.requireVkContext();
112 } else {
113 renderThread.requireGlContext();
114 }
115
116 rtCallback(renderThread);
117
118 renderThread.destroyRenderingContext();
119 }
120
asciiToUtf16(const char * str)121 std::unique_ptr<uint16_t[]> TestUtils::asciiToUtf16(const char* str) {
122 const int length = strlen(str);
123 std::unique_ptr<uint16_t[]> utf16(new uint16_t[length]);
124 for (int i = 0; i < length; i++) {
125 utf16.get()[i] = str[i];
126 }
127 return utf16;
128 }
129
getColor(const sk_sp<SkSurface> & surface,int x,int y)130 SkColor TestUtils::getColor(const sk_sp<SkSurface>& surface, int x, int y) {
131 SkPixmap pixmap;
132 if (!surface->peekPixels(&pixmap)) {
133 return 0;
134 }
135 switch (pixmap.colorType()) {
136 case kGray_8_SkColorType: {
137 const uint8_t* addr = pixmap.addr8(x, y);
138 return SkColorSetRGB(*addr, *addr, *addr);
139 }
140 case kAlpha_8_SkColorType: {
141 const uint8_t* addr = pixmap.addr8(x, y);
142 return SkColorSetA(0, addr[0]);
143 }
144 case kRGB_565_SkColorType: {
145 const uint16_t* addr = pixmap.addr16(x, y);
146 return SkPixel16ToColor(addr[0]);
147 }
148 case kARGB_4444_SkColorType: {
149 const uint16_t* addr = pixmap.addr16(x, y);
150 SkPMColor c = SkPixel4444ToPixel32(addr[0]);
151 return SkUnPreMultiply::PMColorToColor(c);
152 }
153 case kBGRA_8888_SkColorType: {
154 const uint32_t* addr = pixmap.addr32(x, y);
155 SkPMColor c = SkSwizzle_BGRA_to_PMColor(addr[0]);
156 return SkUnPreMultiply::PMColorToColor(c);
157 }
158 case kRGBA_8888_SkColorType: {
159 const uint32_t* addr = pixmap.addr32(x, y);
160 SkPMColor c = SkSwizzle_RGBA_to_PMColor(addr[0]);
161 return SkUnPreMultiply::PMColorToColor(c);
162 }
163 default:
164 return 0;
165 }
166 return 0;
167 }
168
getClipBounds(const SkCanvas * canvas)169 SkRect TestUtils::getClipBounds(const SkCanvas* canvas) {
170 return SkRect::Make(canvas->getDeviceClipBounds());
171 }
172
getLocalClipBounds(const SkCanvas * canvas)173 SkRect TestUtils::getLocalClipBounds(const SkCanvas* canvas) {
174 SkMatrix invertedTotalMatrix;
175 if (!canvas->getTotalMatrix().invert(&invertedTotalMatrix)) {
176 return SkRect::MakeEmpty();
177 }
178 SkRect outlineInDeviceCoord = TestUtils::getClipBounds(canvas);
179 SkRect outlineInLocalCoord;
180 invertedTotalMatrix.mapRect(&outlineInLocalCoord, outlineInDeviceCoord);
181 return outlineInLocalCoord;
182 }
183
defaultFont()184 SkFont TestUtils::defaultFont() {
185 const std::shared_ptr<minikin::MinikinFont>& minikinFont =
186 Typeface::resolveDefault(nullptr)->fFontCollection->getFamilyAt(0)->getFont(0)->baseTypeface();
187 SkTypeface* skTypeface = reinterpret_cast<const MinikinFontSkia*>(minikinFont.get())->GetSkTypeface();
188 LOG_ALWAYS_FATAL_IF(skTypeface == nullptr);
189 return SkFont(sk_ref_sp(skTypeface));
190 }
191
192 } /* namespace uirenderer */
193 } /* namespace android */
194