blob: 06e2d6c00ef5891649cedde8c3344cddfd1b6c62 [file] [log] [blame]
Derek Sollenberger1db141f2014-12-16 08:37:20 -05001/*
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 "SkiaCanvasProxy.h"
18
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070019#include <memory>
sergeyvaed7f582016-10-14 16:30:21 -070020
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070021#include <log/log.h>
22
Stan Iliev770e0b52017-01-05 16:53:14 -050023#include <SkLatticeIter.h>
Ben Wagnera11ee3c2015-08-04 11:14:15 -040024#include <SkPaint.h>
John Reck1bcacfd2017-11-03 10:12:19 -070025#include <SkPatchUtils.h>
Ben Wagnera11ee3c2015-08-04 11:14:15 -040026#include <SkPath.h>
Tom Hudson17c5adf2015-06-09 15:46:04 -040027#include <SkPixelRef.h>
Ben Wagnera11ee3c2015-08-04 11:14:15 -040028#include <SkRRect.h>
Yuqian Liafc221492016-07-18 13:07:42 -040029#include <SkRSXform.h>
John Reck1bcacfd2017-11-03 10:12:19 -070030#include <SkRect.h>
Matt Sarett79fc3b12016-03-24 11:02:44 -040031#include <SkSurface.h>
Derek Sollenberger09bd6c22016-11-03 12:54:06 -040032#include <SkTextBlobRunIterator.h>
Mike Reed871cd2d2017-03-17 10:15:52 -040033#include <SkVertices.h>
John Reck1bcacfd2017-11-03 10:12:19 -070034#include "hwui/Bitmap.h"
Derek Sollenberger1db141f2014-12-16 08:37:20 -050035
36namespace android {
37namespace uirenderer {
38
Tom Hudsonb1476ae2015-03-05 10:30:18 -050039SkiaCanvasProxy::SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls)
Derek Sollenberger1db141f2014-12-16 08:37:20 -050040 : INHERITED(canvas->width(), canvas->height())
Tom Hudsonb1476ae2015-03-05 10:30:18 -050041 , mCanvas(canvas)
42 , mFilterHwuiCalls(filterHwuiCalls) {}
Derek Sollenberger1db141f2014-12-16 08:37:20 -050043
44void SkiaCanvasProxy::onDrawPaint(const SkPaint& paint) {
45 mCanvas->drawPaint(paint);
46}
47
48void SkiaCanvasProxy::onDrawPoints(PointMode pointMode, size_t count, const SkPoint pts[],
John Reck1bcacfd2017-11-03 10:12:19 -070049 const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -050050 if (!pts || count == 0) {
51 return;
52 }
53
Derek Sollenberger1db141f2014-12-16 08:37:20 -050054 // convert the SkPoints into floats
John Reck1bcacfd2017-11-03 10:12:19 -070055 static_assert(sizeof(SkPoint) == sizeof(float) * 2, "SkPoint is no longer two floats");
Derek Sollenberger1db141f2014-12-16 08:37:20 -050056 const size_t floatCount = count << 1;
57 const float* floatArray = &pts[0].fX;
58
59 switch (pointMode) {
60 case kPoints_PointMode: {
61 mCanvas->drawPoints(floatArray, floatCount, paint);
62 break;
63 }
64 case kLines_PointMode: {
65 mCanvas->drawLines(floatArray, floatCount, paint);
66 break;
67 }
68 case kPolygon_PointMode: {
69 SkPaint strokedPaint(paint);
70 strokedPaint.setStyle(SkPaint::kStroke_Style);
71
72 SkPath path;
73 for (size_t i = 0; i < count - 1; i++) {
74 path.moveTo(pts[i]);
John Reck1bcacfd2017-11-03 10:12:19 -070075 path.lineTo(pts[i + 1]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -050076 this->drawPath(path, strokedPaint);
77 path.rewind();
78 }
79 break;
80 }
81 default:
82 LOG_ALWAYS_FATAL("Unknown point type");
83 }
84}
85
86void SkiaCanvasProxy::onDrawOval(const SkRect& rect, const SkPaint& paint) {
87 mCanvas->drawOval(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
88}
89
90void SkiaCanvasProxy::onDrawRect(const SkRect& rect, const SkPaint& paint) {
91 mCanvas->drawRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
92}
93
94void SkiaCanvasProxy::onDrawRRect(const SkRRect& roundRect, const SkPaint& paint) {
95 if (!roundRect.isComplex()) {
96 const SkRect& rect = roundRect.rect();
97 SkVector radii = roundRect.getSimpleRadii();
John Reck1bcacfd2017-11-03 10:12:19 -070098 mCanvas->drawRoundRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, radii.fX, radii.fY,
99 paint);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500100 } else {
101 SkPath path;
102 path.addRRect(roundRect);
103 mCanvas->drawPath(path, paint);
104 }
105}
106
Yuqian Li99691112017-02-03 15:01:41 -0500107void SkiaCanvasProxy::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
108 bool useCenter, const SkPaint& paint) {
John Reck1bcacfd2017-11-03 10:12:19 -0700109 mCanvas->drawArc(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, startAngle, sweepAngle,
110 useCenter, paint);
Yuqian Li99691112017-02-03 15:01:41 -0500111}
112
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500113void SkiaCanvasProxy::onDrawPath(const SkPath& path, const SkPaint& paint) {
114 mCanvas->drawPath(path, paint);
115}
116
117void SkiaCanvasProxy::onDrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
John Reck1bcacfd2017-11-03 10:12:19 -0700118 const SkPaint* paint) {
sergeyvfc9999502016-10-17 13:07:38 -0700119 sk_sp<Bitmap> hwuiBitmap = Bitmap::createFrom(bitmap.info(), *bitmap.pixelRef());
Tom Hudson17c5adf2015-06-09 15:46:04 -0400120 // HWUI doesn't support extractSubset(), so convert any subsetted bitmap into
121 // a drawBitmapRect(); pass through an un-subsetted bitmap.
sergeyvfc9999502016-10-17 13:07:38 -0700122 if (hwuiBitmap && bitmap.dimensions() != hwuiBitmap->info().dimensions()) {
Tom Hudson17c5adf2015-06-09 15:46:04 -0400123 SkIPoint origin = bitmap.pixelRefOrigin();
John Reck1bcacfd2017-11-03 10:12:19 -0700124 mCanvas->drawBitmap(
125 *hwuiBitmap, origin.fX, origin.fY, origin.fX + bitmap.dimensions().width(),
126 origin.fY + bitmap.dimensions().height(), left, top,
127 left + bitmap.dimensions().width(), top + bitmap.dimensions().height(), paint);
Tom Hudson17c5adf2015-06-09 15:46:04 -0400128 } else {
sergeyvaed7f582016-10-14 16:30:21 -0700129 mCanvas->drawBitmap(*hwuiBitmap, left, top, paint);
Tom Hudson17c5adf2015-06-09 15:46:04 -0400130 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500131}
132
sergeyvfc9999502016-10-17 13:07:38 -0700133void SkiaCanvasProxy::onDrawBitmapRect(const SkBitmap& skBitmap, const SkRect* srcPtr,
John Reck1bcacfd2017-11-03 10:12:19 -0700134 const SkRect& dst, const SkPaint* paint, SrcRectConstraint) {
sergeyvfc9999502016-10-17 13:07:38 -0700135 SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(skBitmap.width(), skBitmap.height());
Tom Hudson17c5adf2015-06-09 15:46:04 -0400136 // TODO: if bitmap is a subset, do we need to add pixelRefOrigin to src?
John Reck1bcacfd2017-11-03 10:12:19 -0700137 Bitmap* bitmap = reinterpret_cast<Bitmap*>(skBitmap.pixelRef());
138 mCanvas->drawBitmap(*bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom, dst.fLeft, dst.fTop,
139 dst.fRight, dst.fBottom, paint);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500140}
141
142void SkiaCanvasProxy::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
John Reck1bcacfd2017-11-03 10:12:19 -0700143 const SkRect& dst, const SkPaint*) {
144 // TODO make nine-patch drawing a method on Canvas.h
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500145 SkDEBUGFAIL("SkiaCanvasProxy::onDrawBitmapNine is not yet supported");
146}
147
Stan Iliev770e0b52017-01-05 16:53:14 -0500148void SkiaCanvasProxy::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
John Reck1bcacfd2017-11-03 10:12:19 -0700149 const SkPaint* paint) {
Stan Iliev770e0b52017-01-05 16:53:14 -0500150 SkBitmap skiaBitmap;
Stan Iliev0a3ff952017-07-10 17:04:03 -0400151 SkPixmap pixmap;
152 if (image->peekPixels(&pixmap) && skiaBitmap.installPixels(pixmap)) {
Stan Iliev770e0b52017-01-05 16:53:14 -0500153 onDrawBitmap(skiaBitmap, left, top, paint);
154 }
155}
156
157void SkiaCanvasProxy::onDrawImageRect(const SkImage* image, const SkRect* srcPtr, const SkRect& dst,
John Reck1bcacfd2017-11-03 10:12:19 -0700158 const SkPaint* paint, SrcRectConstraint constraint) {
Stan Iliev770e0b52017-01-05 16:53:14 -0500159 SkBitmap skiaBitmap;
Stan Iliev0a3ff952017-07-10 17:04:03 -0400160 SkPixmap pixmap;
161 if (image->peekPixels(&pixmap) && skiaBitmap.installPixels(pixmap)) {
Stan Iliev770e0b52017-01-05 16:53:14 -0500162 sk_sp<Bitmap> bitmap = Bitmap::createFrom(skiaBitmap.info(), *skiaBitmap.pixelRef());
163 SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(image->width(), image->height());
John Reck1bcacfd2017-11-03 10:12:19 -0700164 mCanvas->drawBitmap(*bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom, dst.fLeft,
165 dst.fTop, dst.fRight, dst.fBottom, paint);
Stan Iliev770e0b52017-01-05 16:53:14 -0500166 }
167}
168
169void SkiaCanvasProxy::onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
John Reck1bcacfd2017-11-03 10:12:19 -0700170 const SkPaint*) {
Stan Iliev770e0b52017-01-05 16:53:14 -0500171 SkDEBUGFAIL("SkiaCanvasProxy::onDrawImageNine is not yet supported");
172}
173
174void SkiaCanvasProxy::onDrawImageLattice(const SkImage* image, const Lattice& lattice,
John Reck1bcacfd2017-11-03 10:12:19 -0700175 const SkRect& dst, const SkPaint* paint) {
Stan Iliev770e0b52017-01-05 16:53:14 -0500176 SkLatticeIter iter(lattice, dst);
177 SkRect srcR, dstR;
178 while (iter.next(&srcR, &dstR)) {
Derek Sollenberger6c2a9e22017-08-15 16:23:01 -0400179 onDrawImageRect(image, &srcR, dstR, paint, SkCanvas::kFast_SrcRectConstraint);
Stan Iliev770e0b52017-01-05 16:53:14 -0500180 }
181}
182
Mike Reed871cd2d2017-03-17 10:15:52 -0400183void SkiaCanvasProxy::onDrawVerticesObject(const SkVertices* vertices, SkBlendMode bmode,
John Reck1bcacfd2017-11-03 10:12:19 -0700184 const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500185 if (mFilterHwuiCalls) {
186 return;
187 }
Mike Reed826deef2017-04-04 15:32:04 -0400188 mCanvas->drawVertices(vertices, bmode, paint);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500189}
190
Matt Sarett79fc3b12016-03-24 11:02:44 -0400191sk_sp<SkSurface> SkiaCanvasProxy::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500192 SkDEBUGFAIL("SkiaCanvasProxy::onNewSurface is not supported");
193 return NULL;
194}
195
196void SkiaCanvasProxy::willSave() {
Florin Malitaeecff562015-12-21 10:43:01 -0500197 mCanvas->save(android::SaveFlags::MatrixClip);
198}
199
200static inline SaveFlags::Flags saveFlags(SkCanvas::SaveLayerFlags layerFlags) {
201 SaveFlags::Flags saveFlags = 0;
202
203 if (!(layerFlags & SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag)) {
204 saveFlags |= SaveFlags::ClipToLayer;
205 }
206
207 if (!(layerFlags & SkCanvas::kIsOpaque_SaveLayerFlag)) {
208 saveFlags |= SaveFlags::HasAlphaLayer;
209 }
210
211 return saveFlags;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500212}
213
John Reck1bcacfd2017-11-03 10:12:19 -0700214SkCanvas::SaveLayerStrategy SkiaCanvasProxy::getSaveLayerStrategy(
215 const SaveLayerRec& saveLayerRec) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500216 SkRect rect;
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500217 if (saveLayerRec.fBounds) {
218 rect = *saveLayerRec.fBounds;
219 } else if (!mCanvas->getClipBounds(&rect)) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500220 rect = SkRect::MakeEmpty();
221 }
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500222 mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, saveLayerRec.fPaint,
Florin Malitaeecff562015-12-21 10:43:01 -0500223 saveFlags(saveLayerRec.fSaveLayerFlags));
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500224 return SkCanvas::kNoLayer_SaveLayerStrategy;
225}
226
227void SkiaCanvasProxy::willRestore() {
228 mCanvas->restore();
229}
230
231void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) {
232 mCanvas->concat(matrix);
233}
234
235void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) {
Chris Craik6daa13c2015-08-19 13:32:12 -0700236 mCanvas->setMatrix(matrix);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500237}
238
239void SkiaCanvasProxy::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
John Reck1bcacfd2017-11-03 10:12:19 -0700240 const SkPaint& paint) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500241 SkPath path;
242 path.addRRect(outer);
243 path.addRRect(inner);
244 path.setFillType(SkPath::kEvenOdd_FillType);
245 this->drawPath(path, paint);
246}
247
248/**
249 * Utility class that converts the incoming text & paint from the given encoding
250 * into glyphIDs.
251 */
252class GlyphIDConverter {
253public:
254 GlyphIDConverter(const void* text, size_t byteLength, const SkPaint& origPaint) {
255 paint = origPaint;
256 if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) {
257 glyphIDs = (uint16_t*)text;
258 count = byteLength >> 1;
259 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700260 // ensure space for one glyph per ID given UTF8 encoding.
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400261 storage.reset(new uint16_t[byteLength]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500262 glyphIDs = storage.get();
263 count = paint.textToGlyphs(text, byteLength, storage.get());
264 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
265 }
266 }
267
268 SkPaint paint;
269 uint16_t* glyphIDs;
270 int count;
John Reck1bcacfd2017-11-03 10:12:19 -0700271
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500272private:
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400273 std::unique_ptr<uint16_t[]> storage;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500274};
275
276void SkiaCanvasProxy::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
John Reck1bcacfd2017-11-03 10:12:19 -0700277 const SkPaint& origPaint) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500278 // convert to glyphIDs if necessary
279 GlyphIDConverter glyphs(text, byteLength, origPaint);
280
281 // compute the glyph positions
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400282 std::unique_ptr<SkScalar[]> glyphWidths(new SkScalar[glyphs.count]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500283 glyphs.paint.getTextWidths(glyphs.glyphIDs, glyphs.count << 1, glyphWidths.get());
284
285 // compute conservative bounds
286 // NOTE: We could call the faster paint.getFontBounds for a less accurate,
287 // but even more conservative bounds if this is too slow.
288 SkRect bounds;
289 glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds);
290
291 // adjust for non-left alignment
292 if (glyphs.paint.getTextAlign() != SkPaint::kLeft_Align) {
293 SkScalar stop = 0;
294 for (int i = 0; i < glyphs.count; i++) {
295 stop += glyphWidths[i];
296 }
297 if (glyphs.paint.getTextAlign() == SkPaint::kCenter_Align) {
298 stop = SkScalarHalf(stop);
299 }
300 if (glyphs.paint.isVerticalText()) {
301 y -= stop;
302 } else {
303 x -= stop;
304 }
305 }
306
307 // setup the first glyph position and adjust bounds if needed
Tom Hudson806a6f02015-02-19 17:11:32 -0500308 int xBaseline = 0;
309 int yBaseline = 0;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500310 if (mCanvas->drawTextAbsolutePos()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700311 bounds.offset(x, y);
Tom Hudson806a6f02015-02-19 17:11:32 -0500312 xBaseline = x;
313 yBaseline = y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500314 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500315
John Reck1bcacfd2017-11-03 10:12:19 -0700316 static_assert(sizeof(SkPoint) == sizeof(float) * 2, "SkPoint is no longer two floats");
317 auto glyphFunc = [&](uint16_t* text, float* positions) {
318 memcpy(text, glyphs.glyphIDs, glyphs.count * sizeof(uint16_t));
Stan Iliev0b58d992017-03-30 18:22:27 -0400319 size_t posIndex = 0;
320 // setup the first glyph position
321 positions[posIndex++] = xBaseline;
322 positions[posIndex++] = yBaseline;
323 // setup the remaining glyph positions
324 if (glyphs.paint.isVerticalText()) {
325 float yPosition = yBaseline;
326 for (int i = 1; i < glyphs.count; i++) {
327 positions[posIndex++] = xBaseline;
John Reck1bcacfd2017-11-03 10:12:19 -0700328 yPosition += glyphWidths[i - 1];
Stan Iliev0b58d992017-03-30 18:22:27 -0400329 positions[posIndex++] = yPosition;
330 }
331 } else {
332 float xPosition = xBaseline;
333 for (int i = 1; i < glyphs.count; i++) {
John Reck1bcacfd2017-11-03 10:12:19 -0700334 xPosition += glyphWidths[i - 1];
Stan Iliev0b58d992017-03-30 18:22:27 -0400335 positions[posIndex++] = xPosition;
336 positions[posIndex++] = yBaseline;
337 }
338 }
339 };
340 mCanvas->drawGlyphs(glyphFunc, glyphs.count, glyphs.paint, x, y, bounds.fLeft, bounds.fTop,
John Reck1bcacfd2017-11-03 10:12:19 -0700341 bounds.fRight, bounds.fBottom, 0);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500342}
343
344void SkiaCanvasProxy::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
John Reck1bcacfd2017-11-03 10:12:19 -0700345 const SkPaint& origPaint) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500346 // convert to glyphIDs if necessary
347 GlyphIDConverter glyphs(text, byteLength, origPaint);
348
349 // convert to relative positions if necessary
350 int x, y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500351 if (mCanvas->drawTextAbsolutePos()) {
352 x = 0;
353 y = 0;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500354 } else {
355 x = pos[0].fX;
356 y = pos[0].fY;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500357 }
358
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400359 // Compute conservative bounds. If the content has already been processed
360 // by Minikin then it had already computed these bounds. Unfortunately,
361 // there is no way to capture those bounds as part of the Skia drawPosText
362 // API so we need to do that computation again here.
Stan Ilievd84d2ee2016-07-25 13:32:25 -0400363 SkRect bounds = SkRect::MakeEmpty();
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400364 for (int i = 0; i < glyphs.count; i++) {
Stan Ilievd84d2ee2016-07-25 13:32:25 -0400365 SkRect glyphBounds = SkRect::MakeEmpty();
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400366 glyphs.paint.measureText(&glyphs.glyphIDs[i], sizeof(uint16_t), &glyphBounds);
367 glyphBounds.offset(pos[i].fX, pos[i].fY);
368 bounds.join(glyphBounds);
369 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500370
John Reck1bcacfd2017-11-03 10:12:19 -0700371 static_assert(sizeof(SkPoint) == sizeof(float) * 2, "SkPoint is no longer two floats");
372 auto glyphFunc = [&](uint16_t* text, float* positions) {
373 memcpy(text, glyphs.glyphIDs, glyphs.count * sizeof(uint16_t));
Stan Iliev0b58d992017-03-30 18:22:27 -0400374 if (mCanvas->drawTextAbsolutePos()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700375 memcpy(positions, pos, 2 * glyphs.count * sizeof(float));
Stan Iliev0b58d992017-03-30 18:22:27 -0400376 } else {
377 for (int i = 0, posIndex = 0; i < glyphs.count; i++) {
378 positions[posIndex++] = pos[i].fX - x;
379 positions[posIndex++] = pos[i].fY - y;
380 }
381 }
382 };
383 mCanvas->drawGlyphs(glyphFunc, glyphs.count, glyphs.paint, x, y, bounds.fLeft, bounds.fTop,
John Reck1bcacfd2017-11-03 10:12:19 -0700384 bounds.fRight, bounds.fBottom, 0);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500385}
386
387void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
John Reck1bcacfd2017-11-03 10:12:19 -0700388 SkScalar constY, const SkPaint& paint) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500389 const size_t pointCount = byteLength >> 1;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400390 std::unique_ptr<SkPoint[]> pts(new SkPoint[pointCount]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500391 for (size_t i = 0; i < pointCount; i++) {
392 pts[i].set(xpos[i], constY);
393 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400394 this->onDrawPosText(text, byteLength, pts.get(), paint);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500395}
396
397void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
John Reck1bcacfd2017-11-03 10:12:19 -0700398 const SkMatrix* matrix, const SkPaint& origPaint) {
Yuqian Liafc221492016-07-18 13:07:42 -0400399 SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextOnPath is not supported");
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500400}
401
Yuqian Liafc221492016-07-18 13:07:42 -0400402void SkiaCanvasProxy::onDrawTextRSXform(const void* text, size_t byteLength,
John Reck1bcacfd2017-11-03 10:12:19 -0700403 const SkRSXform xform[], const SkRect* cullRect,
404 const SkPaint& paint) {
405 GlyphIDConverter glyphs(text, byteLength, paint); // Just get count
Yuqian Liafc221492016-07-18 13:07:42 -0400406 SkMatrix localM, currM, origM;
407 mCanvas->getMatrix(&currM);
408 origM = currM;
409 for (int i = 0; i < glyphs.count; i++) {
410 localM.setRSXform(*xform++);
411 currM.setConcat(origM, localM);
412 mCanvas->setMatrix(currM);
John Reck1bcacfd2017-11-03 10:12:19 -0700413 this->onDrawText((char*)text + (byteLength / glyphs.count * i), byteLength / glyphs.count,
414 0, 0, paint);
Yuqian Liafc221492016-07-18 13:07:42 -0400415 }
416 mCanvas->setMatrix(origM);
417}
418
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500419void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
John Reck1bcacfd2017-11-03 10:12:19 -0700420 const SkPaint& paint) {
Derek Sollenberger09bd6c22016-11-03 12:54:06 -0400421 SkPaint runPaint = paint;
422
John Reck1bcacfd2017-11-03 10:12:19 -0700423 SkTextBlobRunIterator it(blob);
424 for (; !it.done(); it.next()) {
425 size_t textLen = it.glyphCount() * sizeof(uint16_t);
426 const SkPoint& offset = it.offset();
427 // applyFontToPaint() always overwrites the exact same attributes,
428 // so it is safe to not re-seed the paint for this reason.
429 it.applyFontToPaint(&runPaint);
Derek Sollenberger09bd6c22016-11-03 12:54:06 -0400430
John Reck1bcacfd2017-11-03 10:12:19 -0700431 switch (it.positioning()) {
432 case SkTextBlob::kDefault_Positioning:
433 this->drawText(it.glyphs(), textLen, x + offset.x(), y + offset.y(), runPaint);
434 break;
435 case SkTextBlob::kHorizontal_Positioning: {
436 std::unique_ptr<SkPoint[]> pts(new SkPoint[it.glyphCount()]);
437 for (size_t i = 0; i < it.glyphCount(); i++) {
438 pts[i].set(x + offset.x() + it.pos()[i], y + offset.y());
439 }
440 this->drawPosText(it.glyphs(), textLen, pts.get(), runPaint);
441 break;
442 }
443 case SkTextBlob::kFull_Positioning: {
444 std::unique_ptr<SkPoint[]> pts(new SkPoint[it.glyphCount()]);
445 for (size_t i = 0; i < it.glyphCount(); i++) {
446 const size_t xIndex = i * 2;
447 const size_t yIndex = xIndex + 1;
448 pts[i].set(x + offset.x() + it.pos()[xIndex],
449 y + offset.y() + it.pos()[yIndex]);
450 }
451 this->drawPosText(it.glyphs(), textLen, pts.get(), runPaint);
452 break;
453 }
454 default:
455 SK_ABORT("unhandled positioning mode");
456 }
457 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500458}
459
460void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
John Reck1bcacfd2017-11-03 10:12:19 -0700461 const SkPoint texCoords[4], SkBlendMode bmode,
462 const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500463 if (mFilterHwuiCalls) {
464 return;
465 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500466 SkMatrix matrix;
467 mCanvas->getMatrix(&matrix);
468 SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &matrix);
469
John Reck1bcacfd2017-11-03 10:12:19 -0700470 mCanvas->drawVertices(
471 SkPatchUtils::MakeVertices(cubics, colors, texCoords, lod.width(), lod.height()).get(),
472 bmode, paint);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500473}
474
Mike Reed6e49c9f2016-12-02 15:36:59 -0500475void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500476 mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op);
477}
478
Mike Reed6e49c9f2016-12-02 15:36:59 -0500479void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkClipOp op, ClipEdgeStyle) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500480 SkPath path;
481 path.addRRect(roundRect);
482 mCanvas->clipPath(&path, op);
483}
484
Mike Reed6e49c9f2016-12-02 15:36:59 -0500485void SkiaCanvasProxy::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500486 mCanvas->clipPath(&path, op);
487}
488
John Reck1bcacfd2017-11-03 10:12:19 -0700489}; // namespace uirenderer
490}; // namespace android