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