• 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 "SkCanvas.h"
30 #include "SkColorData.h"
31 #include "SkMatrix.h"
32 #include "SkPath.h"
33 #include "SkPixmap.h"
34 #include "SkRect.h"
35 #include "SkSurface.h"
36 #include "SkUnPreMultiply.h"
37 
38 namespace android {
39 namespace uirenderer {
40 
41 std::unordered_map<int, TestUtils::CallCounts> TestUtils::sMockFunctorCounts{};
42 
interpolateColor(float fraction,SkColor start,SkColor end)43 SkColor TestUtils::interpolateColor(float fraction, SkColor start, SkColor end) {
44     int startA = (start >> 24) & 0xff;
45     int startR = (start >> 16) & 0xff;
46     int startG = (start >> 8) & 0xff;
47     int startB = start & 0xff;
48 
49     int endA = (end >> 24) & 0xff;
50     int endR = (end >> 16) & 0xff;
51     int endG = (end >> 8) & 0xff;
52     int endB = end & 0xff;
53 
54     return (int)((startA + (int)(fraction * (endA - startA))) << 24) |
55            (int)((startR + (int)(fraction * (endR - startR))) << 16) |
56            (int)((startG + (int)(fraction * (endG - startG))) << 8) |
57            (int)((startB + (int)(fraction * (endB - startB))));
58 }
59 
createTextureLayerUpdater(renderthread::RenderThread & renderThread)60 sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
61         renderthread::RenderThread& renderThread) {
62     android::uirenderer::renderthread::IRenderPipeline* pipeline;
63     if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
64         pipeline = new skiapipeline::SkiaOpenGLPipeline(renderThread);
65     } else {
66         pipeline = new skiapipeline::SkiaVulkanPipeline(renderThread);
67     }
68     sp<DeferredLayerUpdater> layerUpdater = pipeline->createTextureLayer();
69     layerUpdater->apply();
70     delete pipeline;
71     return layerUpdater;
72 }
73 
createTextureLayerUpdater(renderthread::RenderThread & renderThread,uint32_t width,uint32_t height,const SkMatrix & transform)74 sp<DeferredLayerUpdater> TestUtils::createTextureLayerUpdater(
75         renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
76         const SkMatrix& transform) {
77     sp<DeferredLayerUpdater> layerUpdater = createTextureLayerUpdater(renderThread);
78     layerUpdater->backingLayer()->getTransform() = transform;
79     layerUpdater->setSize(width, height);
80     layerUpdater->setTransform(&transform);
81 
82     // updateLayer so it's ready to draw
83     layerUpdater->updateLayer(true, nullptr, 0, SkRect::MakeEmpty());
84     return layerUpdater;
85 }
86 
drawUtf8ToCanvas(Canvas * canvas,const char * text,const Paint & paint,float x,float y)87 void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint, float x,
88                                  float y) {
89     auto utf16 = asciiToUtf16(text);
90     uint32_t length = strlen(text);
91 
92     canvas->drawText(utf16.get(), length,  // text buffer
93                      0, length,            // draw range
94                      0, length,            // context range
95                      x, y, minikin::Bidi::LTR, paint, nullptr, nullptr /* measured text */);
96 }
97 
drawUtf8ToCanvas(Canvas * canvas,const char * text,const Paint & paint,const SkPath & path)98 void TestUtils::drawUtf8ToCanvas(Canvas* canvas, const char* text, const Paint& paint,
99                                  const SkPath& path) {
100     auto utf16 = asciiToUtf16(text);
101     canvas->drawTextOnPath(utf16.get(), strlen(text), minikin::Bidi::LTR, path, 0, 0, paint,
102                            nullptr);
103 }
104 
run()105 void TestUtils::TestTask::run() {
106     // RenderState only valid once RenderThread is running, so queried here
107     renderthread::RenderThread& renderThread = renderthread::RenderThread::getInstance();
108     if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
109         renderThread.requireVkContext();
110     } else {
111         renderThread.requireGlContext();
112     }
113 
114     rtCallback(renderThread);
115 
116     renderThread.destroyRenderingContext();
117 }
118 
asciiToUtf16(const char * str)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;
126 }
127 
getColor(const sk_sp<SkSurface> & surface,int x,int y)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 
getClipBounds(const SkCanvas * canvas)167 SkRect TestUtils::getClipBounds(const SkCanvas* canvas) {
168     return SkRect::Make(canvas->getDeviceClipBounds());
169 }
170 
getLocalClipBounds(const SkCanvas * canvas)171 SkRect TestUtils::getLocalClipBounds(const SkCanvas* canvas) {
172     SkMatrix invertedTotalMatrix;
173     if (!canvas->getTotalMatrix().invert(&invertedTotalMatrix)) {
174         return SkRect::MakeEmpty();
175     }
176     SkRect outlineInDeviceCoord = TestUtils::getClipBounds(canvas);
177     SkRect outlineInLocalCoord;
178     invertedTotalMatrix.mapRect(&outlineInLocalCoord, outlineInDeviceCoord);
179     return outlineInLocalCoord;
180 }
181 
182 } /* namespace uirenderer */
183 } /* namespace android */
184