blob: fc009d871620a0b81e85596d12fee0a4b24860a3 [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
Florin Malitaeecff562015-12-21 10:43:01 -0500207 return saveFlags;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500208}
209
John Reck1bcacfd2017-11-03 10:12:19 -0700210SkCanvas::SaveLayerStrategy SkiaCanvasProxy::getSaveLayerStrategy(
211 const SaveLayerRec& saveLayerRec) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500212 SkRect rect;
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500213 if (saveLayerRec.fBounds) {
214 rect = *saveLayerRec.fBounds;
215 } else if (!mCanvas->getClipBounds(&rect)) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500216 rect = SkRect::MakeEmpty();
217 }
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500218 mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, saveLayerRec.fPaint,
Florin Malitaeecff562015-12-21 10:43:01 -0500219 saveFlags(saveLayerRec.fSaveLayerFlags));
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500220 return SkCanvas::kNoLayer_SaveLayerStrategy;
221}
222
223void SkiaCanvasProxy::willRestore() {
224 mCanvas->restore();
225}
226
227void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) {
228 mCanvas->concat(matrix);
229}
230
231void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) {
Chris Craik6daa13c2015-08-19 13:32:12 -0700232 mCanvas->setMatrix(matrix);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500233}
234
235void SkiaCanvasProxy::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
John Reck1bcacfd2017-11-03 10:12:19 -0700236 const SkPaint& paint) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500237 SkPath path;
238 path.addRRect(outer);
239 path.addRRect(inner);
240 path.setFillType(SkPath::kEvenOdd_FillType);
241 this->drawPath(path, paint);
242}
243
244/**
245 * Utility class that converts the incoming text & paint from the given encoding
246 * into glyphIDs.
247 */
248class GlyphIDConverter {
249public:
250 GlyphIDConverter(const void* text, size_t byteLength, const SkPaint& origPaint) {
251 paint = origPaint;
252 if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) {
253 glyphIDs = (uint16_t*)text;
254 count = byteLength >> 1;
255 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700256 // ensure space for one glyph per ID given UTF8 encoding.
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400257 storage.reset(new uint16_t[byteLength]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500258 glyphIDs = storage.get();
259 count = paint.textToGlyphs(text, byteLength, storage.get());
260 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
261 }
262 }
263
264 SkPaint paint;
265 uint16_t* glyphIDs;
266 int count;
John Reck1bcacfd2017-11-03 10:12:19 -0700267
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500268private:
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400269 std::unique_ptr<uint16_t[]> storage;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500270};
271
272void SkiaCanvasProxy::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
John Reck1bcacfd2017-11-03 10:12:19 -0700273 const SkPaint& origPaint) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500274 // convert to glyphIDs if necessary
275 GlyphIDConverter glyphs(text, byteLength, origPaint);
276
277 // compute the glyph positions
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400278 std::unique_ptr<SkScalar[]> glyphWidths(new SkScalar[glyphs.count]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500279 glyphs.paint.getTextWidths(glyphs.glyphIDs, glyphs.count << 1, glyphWidths.get());
280
281 // compute conservative bounds
282 // NOTE: We could call the faster paint.getFontBounds for a less accurate,
283 // but even more conservative bounds if this is too slow.
284 SkRect bounds;
285 glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds);
286
287 // adjust for non-left alignment
288 if (glyphs.paint.getTextAlign() != SkPaint::kLeft_Align) {
289 SkScalar stop = 0;
290 for (int i = 0; i < glyphs.count; i++) {
291 stop += glyphWidths[i];
292 }
293 if (glyphs.paint.getTextAlign() == SkPaint::kCenter_Align) {
294 stop = SkScalarHalf(stop);
295 }
296 if (glyphs.paint.isVerticalText()) {
297 y -= stop;
298 } else {
299 x -= stop;
300 }
301 }
302
303 // setup the first glyph position and adjust bounds if needed
Tom Hudson806a6f02015-02-19 17:11:32 -0500304 int xBaseline = 0;
305 int yBaseline = 0;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500306 if (mCanvas->drawTextAbsolutePos()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700307 bounds.offset(x, y);
Tom Hudson806a6f02015-02-19 17:11:32 -0500308 xBaseline = x;
309 yBaseline = y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500310 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500311
John Reck1bcacfd2017-11-03 10:12:19 -0700312 static_assert(sizeof(SkPoint) == sizeof(float) * 2, "SkPoint is no longer two floats");
313 auto glyphFunc = [&](uint16_t* text, float* positions) {
314 memcpy(text, glyphs.glyphIDs, glyphs.count * sizeof(uint16_t));
Stan Iliev0b58d992017-03-30 18:22:27 -0400315 size_t posIndex = 0;
316 // setup the first glyph position
317 positions[posIndex++] = xBaseline;
318 positions[posIndex++] = yBaseline;
319 // setup the remaining glyph positions
320 if (glyphs.paint.isVerticalText()) {
321 float yPosition = yBaseline;
322 for (int i = 1; i < glyphs.count; i++) {
323 positions[posIndex++] = xBaseline;
John Reck1bcacfd2017-11-03 10:12:19 -0700324 yPosition += glyphWidths[i - 1];
Stan Iliev0b58d992017-03-30 18:22:27 -0400325 positions[posIndex++] = yPosition;
326 }
327 } else {
328 float xPosition = xBaseline;
329 for (int i = 1; i < glyphs.count; i++) {
John Reck1bcacfd2017-11-03 10:12:19 -0700330 xPosition += glyphWidths[i - 1];
Stan Iliev0b58d992017-03-30 18:22:27 -0400331 positions[posIndex++] = xPosition;
332 positions[posIndex++] = yBaseline;
333 }
334 }
335 };
336 mCanvas->drawGlyphs(glyphFunc, glyphs.count, glyphs.paint, x, y, bounds.fLeft, bounds.fTop,
John Reck1bcacfd2017-11-03 10:12:19 -0700337 bounds.fRight, bounds.fBottom, 0);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500338}
339
340void SkiaCanvasProxy::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
John Reck1bcacfd2017-11-03 10:12:19 -0700341 const SkPaint& origPaint) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500342 // convert to glyphIDs if necessary
343 GlyphIDConverter glyphs(text, byteLength, origPaint);
344
345 // convert to relative positions if necessary
346 int x, y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500347 if (mCanvas->drawTextAbsolutePos()) {
348 x = 0;
349 y = 0;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500350 } else {
351 x = pos[0].fX;
352 y = pos[0].fY;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500353 }
354
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400355 // Compute conservative bounds. If the content has already been processed
356 // by Minikin then it had already computed these bounds. Unfortunately,
357 // there is no way to capture those bounds as part of the Skia drawPosText
358 // API so we need to do that computation again here.
Stan Ilievd84d2ee2016-07-25 13:32:25 -0400359 SkRect bounds = SkRect::MakeEmpty();
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400360 for (int i = 0; i < glyphs.count; i++) {
Stan Ilievd84d2ee2016-07-25 13:32:25 -0400361 SkRect glyphBounds = SkRect::MakeEmpty();
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400362 glyphs.paint.measureText(&glyphs.glyphIDs[i], sizeof(uint16_t), &glyphBounds);
363 glyphBounds.offset(pos[i].fX, pos[i].fY);
364 bounds.join(glyphBounds);
365 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500366
John Reck1bcacfd2017-11-03 10:12:19 -0700367 static_assert(sizeof(SkPoint) == sizeof(float) * 2, "SkPoint is no longer two floats");
368 auto glyphFunc = [&](uint16_t* text, float* positions) {
369 memcpy(text, glyphs.glyphIDs, glyphs.count * sizeof(uint16_t));
Stan Iliev0b58d992017-03-30 18:22:27 -0400370 if (mCanvas->drawTextAbsolutePos()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700371 memcpy(positions, pos, 2 * glyphs.count * sizeof(float));
Stan Iliev0b58d992017-03-30 18:22:27 -0400372 } else {
373 for (int i = 0, posIndex = 0; i < glyphs.count; i++) {
374 positions[posIndex++] = pos[i].fX - x;
375 positions[posIndex++] = pos[i].fY - y;
376 }
377 }
378 };
379 mCanvas->drawGlyphs(glyphFunc, glyphs.count, glyphs.paint, x, y, bounds.fLeft, bounds.fTop,
John Reck1bcacfd2017-11-03 10:12:19 -0700380 bounds.fRight, bounds.fBottom, 0);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500381}
382
383void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
John Reck1bcacfd2017-11-03 10:12:19 -0700384 SkScalar constY, const SkPaint& paint) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500385 const size_t pointCount = byteLength >> 1;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400386 std::unique_ptr<SkPoint[]> pts(new SkPoint[pointCount]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500387 for (size_t i = 0; i < pointCount; i++) {
388 pts[i].set(xpos[i], constY);
389 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400390 this->onDrawPosText(text, byteLength, pts.get(), paint);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500391}
392
393void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
John Reck1bcacfd2017-11-03 10:12:19 -0700394 const SkMatrix* matrix, const SkPaint& origPaint) {
Yuqian Liafc221492016-07-18 13:07:42 -0400395 SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextOnPath is not supported");
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500396}
397
Yuqian Liafc221492016-07-18 13:07:42 -0400398void SkiaCanvasProxy::onDrawTextRSXform(const void* text, size_t byteLength,
John Reck1bcacfd2017-11-03 10:12:19 -0700399 const SkRSXform xform[], const SkRect* cullRect,
400 const SkPaint& paint) {
401 GlyphIDConverter glyphs(text, byteLength, paint); // Just get count
Yuqian Liafc221492016-07-18 13:07:42 -0400402 SkMatrix localM, currM, origM;
403 mCanvas->getMatrix(&currM);
404 origM = currM;
405 for (int i = 0; i < glyphs.count; i++) {
406 localM.setRSXform(*xform++);
407 currM.setConcat(origM, localM);
408 mCanvas->setMatrix(currM);
John Reck1bcacfd2017-11-03 10:12:19 -0700409 this->onDrawText((char*)text + (byteLength / glyphs.count * i), byteLength / glyphs.count,
410 0, 0, paint);
Yuqian Liafc221492016-07-18 13:07:42 -0400411 }
412 mCanvas->setMatrix(origM);
413}
414
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500415void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
John Reck1bcacfd2017-11-03 10:12:19 -0700416 const SkPaint& paint) {
Derek Sollenberger09bd6c22016-11-03 12:54:06 -0400417 SkPaint runPaint = paint;
418
John Reck1bcacfd2017-11-03 10:12:19 -0700419 SkTextBlobRunIterator it(blob);
420 for (; !it.done(); it.next()) {
421 size_t textLen = it.glyphCount() * sizeof(uint16_t);
422 const SkPoint& offset = it.offset();
423 // applyFontToPaint() always overwrites the exact same attributes,
424 // so it is safe to not re-seed the paint for this reason.
425 it.applyFontToPaint(&runPaint);
Derek Sollenberger09bd6c22016-11-03 12:54:06 -0400426
John Reck1bcacfd2017-11-03 10:12:19 -0700427 switch (it.positioning()) {
428 case SkTextBlob::kDefault_Positioning:
429 this->drawText(it.glyphs(), textLen, x + offset.x(), y + offset.y(), runPaint);
430 break;
431 case SkTextBlob::kHorizontal_Positioning: {
432 std::unique_ptr<SkPoint[]> pts(new SkPoint[it.glyphCount()]);
433 for (size_t i = 0; i < it.glyphCount(); i++) {
434 pts[i].set(x + offset.x() + it.pos()[i], y + offset.y());
435 }
436 this->drawPosText(it.glyphs(), textLen, pts.get(), runPaint);
437 break;
438 }
439 case SkTextBlob::kFull_Positioning: {
440 std::unique_ptr<SkPoint[]> pts(new SkPoint[it.glyphCount()]);
441 for (size_t i = 0; i < it.glyphCount(); i++) {
442 const size_t xIndex = i * 2;
443 const size_t yIndex = xIndex + 1;
444 pts[i].set(x + offset.x() + it.pos()[xIndex],
445 y + offset.y() + it.pos()[yIndex]);
446 }
447 this->drawPosText(it.glyphs(), textLen, pts.get(), runPaint);
448 break;
449 }
450 default:
451 SK_ABORT("unhandled positioning mode");
452 }
453 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500454}
455
456void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
John Reck1bcacfd2017-11-03 10:12:19 -0700457 const SkPoint texCoords[4], SkBlendMode bmode,
458 const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500459 if (mFilterHwuiCalls) {
460 return;
461 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500462 SkMatrix matrix;
463 mCanvas->getMatrix(&matrix);
464 SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &matrix);
465
John Reck1bcacfd2017-11-03 10:12:19 -0700466 mCanvas->drawVertices(
467 SkPatchUtils::MakeVertices(cubics, colors, texCoords, lod.width(), lod.height()).get(),
468 bmode, paint);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500469}
470
Mike Reed6e49c9f2016-12-02 15:36:59 -0500471void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500472 mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op);
473}
474
Mike Reed6e49c9f2016-12-02 15:36:59 -0500475void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkClipOp op, ClipEdgeStyle) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500476 SkPath path;
477 path.addRRect(roundRect);
478 mCanvas->clipPath(&path, op);
479}
480
Mike Reed6e49c9f2016-12-02 15:36:59 -0500481void SkiaCanvasProxy::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500482 mCanvas->clipPath(&path, op);
483}
484
John Reck1bcacfd2017-11-03 10:12:19 -0700485}; // namespace uirenderer
486}; // namespace android