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 "Canvas.h"
18 
19 #include "MinikinUtils.h"
20 #include "Paint.h"
21 #include "Properties.h"
22 #include "RenderNode.h"
23 #include "Typeface.h"
24 #include "pipeline/skia/SkiaRecordingCanvas.h"
25 
26 #include "hwui/PaintFilter.h"
27 
28 #include <SkFontMetrics.h>
29 
30 namespace android {
31 
create_recording_canvas(int width,int height,uirenderer::RenderNode * renderNode)32 Canvas* Canvas::create_recording_canvas(int width, int height, uirenderer::RenderNode* renderNode) {
33     return new uirenderer::skiapipeline::SkiaRecordingCanvas(renderNode, width, height);
34 }
35 
drawStroke(SkScalar left,SkScalar right,SkScalar top,SkScalar thickness,const SkPaint & paint,Canvas * canvas)36 static inline void drawStroke(SkScalar left, SkScalar right, SkScalar top, SkScalar thickness,
37                               const SkPaint& paint, Canvas* canvas) {
38     const SkScalar strokeWidth = fmax(thickness, 1.0f);
39     const SkScalar bottom = top + strokeWidth;
40     canvas->drawRect(left, top, right, bottom, paint);
41 }
42 
drawTextDecorations(float x,float y,float length,const Paint & paint)43 void Canvas::drawTextDecorations(float x, float y, float length, const Paint& paint) {
44     // paint has already been filtered by our caller, so we can ignore any filter
45     const bool strikeThru = paint.isStrikeThru();
46     const bool underline = paint.isUnderline();
47     if (strikeThru || underline) {
48         const SkScalar left = x;
49         const SkScalar right = x + length;
50         const float textSize = paint.getSkFont().getSize();
51         if (underline) {
52             SkFontMetrics metrics;
53             paint.getSkFont().getMetrics(&metrics);
54             SkScalar position;
55             if (!metrics.hasUnderlinePosition(&position)) {
56                 position = textSize * Paint::kStdUnderline_Top;
57             }
58             SkScalar thickness;
59             if (!metrics.hasUnderlineThickness(&thickness)) {
60                 thickness = textSize * Paint::kStdUnderline_Thickness;
61             }
62             const SkScalar top = y + position;
63             drawStroke(left, right, top, thickness, paint, this);
64         }
65         if (strikeThru) {
66             const float position = textSize * Paint::kStdStrikeThru_Top;
67             const SkScalar thickness = textSize * Paint::kStdStrikeThru_Thickness;
68             const SkScalar top = y + position;
69             drawStroke(left, right, top, thickness, paint, this);
70         }
71     }
72 }
73 
simplifyPaint(int color,Paint * paint)74 static void simplifyPaint(int color, Paint* paint) {
75     paint->setColor(color);
76     paint->setShader(nullptr);
77     paint->setColorFilter(nullptr);
78     paint->setLooper(nullptr);
79     paint->setStrokeWidth(4 + 0.04 * paint->getSkFont().getSize());
80     paint->setStrokeJoin(SkPaint::kRound_Join);
81     paint->setLooper(nullptr);
82 }
83 
84 class DrawTextFunctor {
85 public:
DrawTextFunctor(const minikin::Layout & layout,Canvas * canvas,const Paint & paint,float x,float y,minikin::MinikinRect & bounds,float totalAdvance)86     DrawTextFunctor(const minikin::Layout& layout, Canvas* canvas, const Paint& paint, float x,
87                     float y, minikin::MinikinRect& bounds, float totalAdvance)
88             : layout(layout)
89             , canvas(canvas)
90             , paint(paint)
91             , x(x)
92             , y(y)
93             , bounds(bounds)
94             , totalAdvance(totalAdvance) {}
95 
operator ()(size_t start,size_t end)96     void operator()(size_t start, size_t end) {
97         auto glyphFunc = [&](uint16_t* text, float* positions) {
98             if (canvas->drawTextAbsolutePos()) {
99                 for (size_t i = start, textIndex = 0, posIndex = 0; i < end; i++) {
100                     text[textIndex++] = layout.getGlyphId(i);
101                     positions[posIndex++] = x + layout.getX(i);
102                     positions[posIndex++] = y + layout.getY(i);
103                 }
104             } else {
105                 for (size_t i = start, textIndex = 0, posIndex = 0; i < end; i++) {
106                     text[textIndex++] = layout.getGlyphId(i);
107                     positions[posIndex++] = layout.getX(i);
108                     positions[posIndex++] = layout.getY(i);
109                 }
110             }
111         };
112 
113         size_t glyphCount = end - start;
114 
115         if (CC_UNLIKELY(canvas->isHighContrastText() && paint.getAlpha() != 0)) {
116             // high contrast draw path
117             int color = paint.getColor();
118             int channelSum = SkColorGetR(color) + SkColorGetG(color) + SkColorGetB(color);
119             bool darken = channelSum < (128 * 3);
120 
121             // outline
122             Paint outlinePaint(paint);
123             simplifyPaint(darken ? SK_ColorWHITE : SK_ColorBLACK, &outlinePaint);
124             outlinePaint.setStyle(SkPaint::kStrokeAndFill_Style);
125             canvas->drawGlyphs(glyphFunc, glyphCount, outlinePaint, x, y, bounds.mLeft, bounds.mTop,
126                                bounds.mRight, bounds.mBottom, totalAdvance);
127 
128             // inner
129             Paint innerPaint(paint);
130             simplifyPaint(darken ? SK_ColorBLACK : SK_ColorWHITE, &innerPaint);
131             innerPaint.setStyle(SkPaint::kFill_Style);
132             canvas->drawGlyphs(glyphFunc, glyphCount, innerPaint, x, y, bounds.mLeft, bounds.mTop,
133                                bounds.mRight, bounds.mBottom, totalAdvance);
134         } else {
135             // standard draw path
136             canvas->drawGlyphs(glyphFunc, glyphCount, paint, x, y, bounds.mLeft, bounds.mTop,
137                                bounds.mRight, bounds.mBottom, totalAdvance);
138         }
139     }
140 
141 private:
142     const minikin::Layout& layout;
143     Canvas* canvas;
144     const Paint& paint;
145     float x;
146     float y;
147     minikin::MinikinRect& bounds;
148     float totalAdvance;
149 };
150 
drawText(const uint16_t * text,int textSize,int start,int count,int contextStart,int contextCount,float x,float y,minikin::Bidi bidiFlags,const Paint & origPaint,const Typeface * typeface,minikin::MeasuredText * mt)151 void Canvas::drawText(const uint16_t* text, int textSize, int start, int count, int contextStart,
152                       int contextCount, float x, float y, minikin::Bidi bidiFlags,
153                       const Paint& origPaint, const Typeface* typeface, minikin::MeasuredText* mt) {
154     // minikin may modify the original paint
155     Paint paint(origPaint);
156 
157     minikin::Layout layout = MinikinUtils::doLayout(&paint, bidiFlags, typeface, text, textSize,
158                                                     start, count, contextStart, contextCount, mt);
159 
160     x += MinikinUtils::xOffsetForTextAlign(&paint, layout);
161 
162     minikin::MinikinRect bounds;
163     layout.getBounds(&bounds);
164     if (!drawTextAbsolutePos()) {
165         bounds.offset(x, y);
166     }
167 
168     // Set align to left for drawing, as we don't want individual
169     // glyphs centered or right-aligned; the offset above takes
170     // care of all alignment.
171     paint.setTextAlign(Paint::kLeft_Align);
172 
173     DrawTextFunctor f(layout, this, paint, x, y, bounds, layout.getAdvance());
174     MinikinUtils::forFontRun(layout, &paint, f);
175 }
176 
drawDoubleRoundRectXY(float outerLeft,float outerTop,float outerRight,float outerBottom,float outerRx,float outerRy,float innerLeft,float innerTop,float innerRight,float innerBottom,float innerRx,float innerRy,const SkPaint & paint)177 void Canvas::drawDoubleRoundRectXY(float outerLeft, float outerTop, float outerRight,
178                             float outerBottom, float outerRx, float outerRy, float innerLeft,
179                             float innerTop, float innerRight, float innerBottom, float innerRx,
180                             float innerRy, const SkPaint& paint) {
181     if (CC_UNLIKELY(paint.nothingToDraw())) return;
182     SkRect outer = SkRect::MakeLTRB(outerLeft, outerTop, outerRight, outerBottom);
183     SkRect inner = SkRect::MakeLTRB(innerLeft, innerTop, innerRight, innerBottom);
184 
185     SkRRect outerRRect;
186     outerRRect.setRectXY(outer, outerRx, outerRy);
187 
188     SkRRect innerRRect;
189     innerRRect.setRectXY(inner, innerRx, innerRy);
190     drawDoubleRoundRect(outerRRect, innerRRect, paint);
191 }
192 
drawDoubleRoundRectRadii(float outerLeft,float outerTop,float outerRight,float outerBottom,const float * outerRadii,float innerLeft,float innerTop,float innerRight,float innerBottom,const float * innerRadii,const SkPaint & paint)193 void Canvas::drawDoubleRoundRectRadii(float outerLeft, float outerTop, float outerRight,
194                             float outerBottom, const float* outerRadii, float innerLeft,
195                             float innerTop, float innerRight, float innerBottom,
196                             const float* innerRadii, const SkPaint& paint) {
197     static_assert(sizeof(SkVector) == sizeof(float) * 2);
198     if (CC_UNLIKELY(paint.nothingToDraw())) return;
199     SkRect outer = SkRect::MakeLTRB(outerLeft, outerTop, outerRight, outerBottom);
200     SkRect inner = SkRect::MakeLTRB(innerLeft, innerTop, innerRight, innerBottom);
201 
202     SkRRect outerRRect;
203     const SkVector* outerSkVector = reinterpret_cast<const SkVector*>(outerRadii);
204     outerRRect.setRectRadii(outer, outerSkVector);
205 
206     SkRRect innerRRect;
207     const SkVector* innerSkVector = reinterpret_cast<const SkVector*>(innerRadii);
208     innerRRect.setRectRadii(inner, innerSkVector);
209     drawDoubleRoundRect(outerRRect, innerRRect, paint);
210 }
211 
212 class DrawTextOnPathFunctor {
213 public:
DrawTextOnPathFunctor(const minikin::Layout & layout,Canvas * canvas,float hOffset,float vOffset,const Paint & paint,const SkPath & path)214     DrawTextOnPathFunctor(const minikin::Layout& layout, Canvas* canvas, float hOffset,
215                           float vOffset, const Paint& paint, const SkPath& path)
216             : layout(layout)
217             , canvas(canvas)
218             , hOffset(hOffset)
219             , vOffset(vOffset)
220             , paint(paint)
221             , path(path) {}
222 
operator ()(size_t start,size_t end)223     void operator()(size_t start, size_t end) {
224         canvas->drawLayoutOnPath(layout, hOffset, vOffset, paint, path, start, end);
225     }
226 
227 private:
228     const minikin::Layout& layout;
229     Canvas* canvas;
230     float hOffset;
231     float vOffset;
232     const Paint& paint;
233     const SkPath& path;
234 };
235 
drawTextOnPath(const uint16_t * text,int count,minikin::Bidi bidiFlags,const SkPath & path,float hOffset,float vOffset,const Paint & paint,const Typeface * typeface)236 void Canvas::drawTextOnPath(const uint16_t* text, int count, minikin::Bidi bidiFlags,
237                             const SkPath& path, float hOffset, float vOffset, const Paint& paint,
238                             const Typeface* typeface) {
239     Paint paintCopy(paint);
240     minikin::Layout layout =
241             MinikinUtils::doLayout(&paintCopy, bidiFlags, typeface, text, count,  // text buffer
242                                    0, count,                                      // draw range
243                                    0, count,                                      // context range
244                                    nullptr);
245     hOffset += MinikinUtils::hOffsetForTextAlign(&paintCopy, layout, path);
246 
247     // Set align to left for drawing, as we don't want individual
248     // glyphs centered or right-aligned; the offset above takes
249     // care of all alignment.
250     paintCopy.setTextAlign(Paint::kLeft_Align);
251 
252     DrawTextOnPathFunctor f(layout, this, hOffset, vOffset, paintCopy, path);
253     MinikinUtils::forFontRun(layout, &paintCopy, f);
254 }
255 
256 int Canvas::sApiLevel = 1;
257 
setCompatibilityVersion(int apiLevel)258 void Canvas::setCompatibilityVersion(int apiLevel) {
259     sApiLevel = apiLevel;
260 }
261 
262 }  // namespace android
263