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