Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 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 "SkiaCanvasProxy.h" |
| 18 | |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 19 | #include <memory> |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 20 | |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 21 | #include <log/log.h> |
| 22 | |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 23 | #include <SkLatticeIter.h> |
Ben Wagner | a11ee3c | 2015-08-04 11:14:15 -0400 | [diff] [blame] | 24 | #include <SkPaint.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 25 | #include <SkPatchUtils.h> |
Ben Wagner | a11ee3c | 2015-08-04 11:14:15 -0400 | [diff] [blame] | 26 | #include <SkPath.h> |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 27 | #include <SkPixelRef.h> |
Ben Wagner | a11ee3c | 2015-08-04 11:14:15 -0400 | [diff] [blame] | 28 | #include <SkRRect.h> |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 29 | #include <SkRSXform.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 30 | #include <SkRect.h> |
Matt Sarett | 79fc3b1 | 2016-03-24 11:02:44 -0400 | [diff] [blame] | 31 | #include <SkSurface.h> |
Derek Sollenberger | 09bd6c2 | 2016-11-03 12:54:06 -0400 | [diff] [blame] | 32 | #include <SkTextBlobRunIterator.h> |
Mike Reed | 871cd2d | 2017-03-17 10:15:52 -0400 | [diff] [blame] | 33 | #include <SkVertices.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 34 | #include "hwui/Bitmap.h" |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | namespace uirenderer { |
| 38 | |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 39 | SkiaCanvasProxy::SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls) |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 40 | : INHERITED(canvas->width(), canvas->height()) |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 41 | , mCanvas(canvas) |
| 42 | , mFilterHwuiCalls(filterHwuiCalls) {} |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 43 | |
| 44 | void SkiaCanvasProxy::onDrawPaint(const SkPaint& paint) { |
| 45 | mCanvas->drawPaint(paint); |
| 46 | } |
| 47 | |
| 48 | void SkiaCanvasProxy::onDrawPoints(PointMode pointMode, size_t count, const SkPoint pts[], |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 49 | const SkPaint& paint) { |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 50 | if (!pts || count == 0) { |
| 51 | return; |
| 52 | } |
| 53 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 54 | // convert the SkPoints into floats |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 55 | static_assert(sizeof(SkPoint) == sizeof(float) * 2, "SkPoint is no longer two floats"); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 56 | 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 Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 75 | path.lineTo(pts[i + 1]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 76 | this->drawPath(path, strokedPaint); |
| 77 | path.rewind(); |
| 78 | } |
| 79 | break; |
| 80 | } |
| 81 | default: |
| 82 | LOG_ALWAYS_FATAL("Unknown point type"); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void SkiaCanvasProxy::onDrawOval(const SkRect& rect, const SkPaint& paint) { |
| 87 | mCanvas->drawOval(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint); |
| 88 | } |
| 89 | |
| 90 | void SkiaCanvasProxy::onDrawRect(const SkRect& rect, const SkPaint& paint) { |
| 91 | mCanvas->drawRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint); |
| 92 | } |
| 93 | |
| 94 | void SkiaCanvasProxy::onDrawRRect(const SkRRect& roundRect, const SkPaint& paint) { |
| 95 | if (!roundRect.isComplex()) { |
| 96 | const SkRect& rect = roundRect.rect(); |
| 97 | SkVector radii = roundRect.getSimpleRadii(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 98 | mCanvas->drawRoundRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, radii.fX, radii.fY, |
| 99 | paint); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 100 | } else { |
| 101 | SkPath path; |
| 102 | path.addRRect(roundRect); |
| 103 | mCanvas->drawPath(path, paint); |
| 104 | } |
| 105 | } |
| 106 | |
Yuqian Li | 9969111 | 2017-02-03 15:01:41 -0500 | [diff] [blame] | 107 | void SkiaCanvasProxy::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle, |
| 108 | bool useCenter, const SkPaint& paint) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 109 | mCanvas->drawArc(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, startAngle, sweepAngle, |
| 110 | useCenter, paint); |
Yuqian Li | 9969111 | 2017-02-03 15:01:41 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 113 | void SkiaCanvasProxy::onDrawPath(const SkPath& path, const SkPaint& paint) { |
| 114 | mCanvas->drawPath(path, paint); |
| 115 | } |
| 116 | |
| 117 | void SkiaCanvasProxy::onDrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 118 | const SkPaint* paint) { |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 119 | sk_sp<Bitmap> hwuiBitmap = Bitmap::createFrom(bitmap.info(), *bitmap.pixelRef()); |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 120 | // HWUI doesn't support extractSubset(), so convert any subsetted bitmap into |
| 121 | // a drawBitmapRect(); pass through an un-subsetted bitmap. |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 122 | if (hwuiBitmap && bitmap.dimensions() != hwuiBitmap->info().dimensions()) { |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 123 | SkIPoint origin = bitmap.pixelRefOrigin(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 124 | 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 Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 128 | } else { |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 129 | mCanvas->drawBitmap(*hwuiBitmap, left, top, paint); |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 130 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 131 | } |
| 132 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 133 | void SkiaCanvasProxy::onDrawBitmapRect(const SkBitmap& skBitmap, const SkRect* srcPtr, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 134 | const SkRect& dst, const SkPaint* paint, SrcRectConstraint) { |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 135 | SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(skBitmap.width(), skBitmap.height()); |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 136 | // TODO: if bitmap is a subset, do we need to add pixelRefOrigin to src? |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 137 | 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 Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void SkiaCanvasProxy::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 143 | const SkRect& dst, const SkPaint*) { |
| 144 | // TODO make nine-patch drawing a method on Canvas.h |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 145 | SkDEBUGFAIL("SkiaCanvasProxy::onDrawBitmapNine is not yet supported"); |
| 146 | } |
| 147 | |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 148 | void SkiaCanvasProxy::onDrawImage(const SkImage* image, SkScalar left, SkScalar top, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 149 | const SkPaint* paint) { |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 150 | SkBitmap skiaBitmap; |
Stan Iliev | 0a3ff95 | 2017-07-10 17:04:03 -0400 | [diff] [blame] | 151 | SkPixmap pixmap; |
| 152 | if (image->peekPixels(&pixmap) && skiaBitmap.installPixels(pixmap)) { |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 153 | onDrawBitmap(skiaBitmap, left, top, paint); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void SkiaCanvasProxy::onDrawImageRect(const SkImage* image, const SkRect* srcPtr, const SkRect& dst, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 158 | const SkPaint* paint, SrcRectConstraint constraint) { |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 159 | SkBitmap skiaBitmap; |
Stan Iliev | 0a3ff95 | 2017-07-10 17:04:03 -0400 | [diff] [blame] | 160 | SkPixmap pixmap; |
| 161 | if (image->peekPixels(&pixmap) && skiaBitmap.installPixels(pixmap)) { |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 162 | sk_sp<Bitmap> bitmap = Bitmap::createFrom(skiaBitmap.info(), *skiaBitmap.pixelRef()); |
| 163 | SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(image->width(), image->height()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 164 | mCanvas->drawBitmap(*bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom, dst.fLeft, |
| 165 | dst.fTop, dst.fRight, dst.fBottom, paint); |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | void SkiaCanvasProxy::onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 170 | const SkPaint*) { |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 171 | SkDEBUGFAIL("SkiaCanvasProxy::onDrawImageNine is not yet supported"); |
| 172 | } |
| 173 | |
| 174 | void SkiaCanvasProxy::onDrawImageLattice(const SkImage* image, const Lattice& lattice, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 175 | const SkRect& dst, const SkPaint* paint) { |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 176 | SkLatticeIter iter(lattice, dst); |
| 177 | SkRect srcR, dstR; |
| 178 | while (iter.next(&srcR, &dstR)) { |
Derek Sollenberger | 6c2a9e2 | 2017-08-15 16:23:01 -0400 | [diff] [blame] | 179 | onDrawImageRect(image, &srcR, dstR, paint, SkCanvas::kFast_SrcRectConstraint); |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Mike Reed | 871cd2d | 2017-03-17 10:15:52 -0400 | [diff] [blame] | 183 | void SkiaCanvasProxy::onDrawVerticesObject(const SkVertices* vertices, SkBlendMode bmode, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 184 | const SkPaint& paint) { |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 185 | if (mFilterHwuiCalls) { |
| 186 | return; |
| 187 | } |
Mike Reed | 826deef | 2017-04-04 15:32:04 -0400 | [diff] [blame] | 188 | mCanvas->drawVertices(vertices, bmode, paint); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Matt Sarett | 79fc3b1 | 2016-03-24 11:02:44 -0400 | [diff] [blame] | 191 | sk_sp<SkSurface> SkiaCanvasProxy::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 192 | SkDEBUGFAIL("SkiaCanvasProxy::onNewSurface is not supported"); |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | void SkiaCanvasProxy::willSave() { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 197 | mCanvas->save(android::SaveFlags::MatrixClip); |
| 198 | } |
| 199 | |
| 200 | static 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 Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 207 | return saveFlags; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 208 | } |
| 209 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 210 | SkCanvas::SaveLayerStrategy SkiaCanvasProxy::getSaveLayerStrategy( |
| 211 | const SaveLayerRec& saveLayerRec) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 212 | SkRect rect; |
Leon Scroggins III | 5518e7c | 2016-01-04 09:37:42 -0500 | [diff] [blame] | 213 | if (saveLayerRec.fBounds) { |
| 214 | rect = *saveLayerRec.fBounds; |
| 215 | } else if (!mCanvas->getClipBounds(&rect)) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 216 | rect = SkRect::MakeEmpty(); |
| 217 | } |
Leon Scroggins III | 5518e7c | 2016-01-04 09:37:42 -0500 | [diff] [blame] | 218 | mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, saveLayerRec.fPaint, |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 219 | saveFlags(saveLayerRec.fSaveLayerFlags)); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 220 | return SkCanvas::kNoLayer_SaveLayerStrategy; |
| 221 | } |
| 222 | |
| 223 | void SkiaCanvasProxy::willRestore() { |
| 224 | mCanvas->restore(); |
| 225 | } |
| 226 | |
| 227 | void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) { |
| 228 | mCanvas->concat(matrix); |
| 229 | } |
| 230 | |
| 231 | void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) { |
Chris Craik | 6daa13c | 2015-08-19 13:32:12 -0700 | [diff] [blame] | 232 | mCanvas->setMatrix(matrix); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void SkiaCanvasProxy::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 236 | const SkPaint& paint) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 237 | 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 | */ |
| 248 | class GlyphIDConverter { |
| 249 | public: |
| 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 Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 256 | // ensure space for one glyph per ID given UTF8 encoding. |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 257 | storage.reset(new uint16_t[byteLength]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 258 | 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 Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 267 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 268 | private: |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 269 | std::unique_ptr<uint16_t[]> storage; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | void SkiaCanvasProxy::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 273 | const SkPaint& origPaint) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 274 | // convert to glyphIDs if necessary |
| 275 | GlyphIDConverter glyphs(text, byteLength, origPaint); |
| 276 | |
| 277 | // compute the glyph positions |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 278 | std::unique_ptr<SkScalar[]> glyphWidths(new SkScalar[glyphs.count]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 279 | 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 Hudson | 806a6f0 | 2015-02-19 17:11:32 -0500 | [diff] [blame] | 304 | int xBaseline = 0; |
| 305 | int yBaseline = 0; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 306 | if (mCanvas->drawTextAbsolutePos()) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 307 | bounds.offset(x, y); |
Tom Hudson | 806a6f0 | 2015-02-19 17:11:32 -0500 | [diff] [blame] | 308 | xBaseline = x; |
| 309 | yBaseline = y; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 310 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 311 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 312 | 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 Iliev | 0b58d99 | 2017-03-30 18:22:27 -0400 | [diff] [blame] | 315 | 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 Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 324 | yPosition += glyphWidths[i - 1]; |
Stan Iliev | 0b58d99 | 2017-03-30 18:22:27 -0400 | [diff] [blame] | 325 | positions[posIndex++] = yPosition; |
| 326 | } |
| 327 | } else { |
| 328 | float xPosition = xBaseline; |
| 329 | for (int i = 1; i < glyphs.count; i++) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 330 | xPosition += glyphWidths[i - 1]; |
Stan Iliev | 0b58d99 | 2017-03-30 18:22:27 -0400 | [diff] [blame] | 331 | 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 Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 337 | bounds.fRight, bounds.fBottom, 0); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void SkiaCanvasProxy::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 341 | const SkPaint& origPaint) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 342 | // convert to glyphIDs if necessary |
| 343 | GlyphIDConverter glyphs(text, byteLength, origPaint); |
| 344 | |
| 345 | // convert to relative positions if necessary |
| 346 | int x, y; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 347 | if (mCanvas->drawTextAbsolutePos()) { |
| 348 | x = 0; |
| 349 | y = 0; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 350 | } else { |
| 351 | x = pos[0].fX; |
| 352 | y = pos[0].fY; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 353 | } |
| 354 | |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 355 | // 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 Iliev | d84d2ee | 2016-07-25 13:32:25 -0400 | [diff] [blame] | 359 | SkRect bounds = SkRect::MakeEmpty(); |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 360 | for (int i = 0; i < glyphs.count; i++) { |
Stan Iliev | d84d2ee | 2016-07-25 13:32:25 -0400 | [diff] [blame] | 361 | SkRect glyphBounds = SkRect::MakeEmpty(); |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 362 | 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 Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 366 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 367 | 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 Iliev | 0b58d99 | 2017-03-30 18:22:27 -0400 | [diff] [blame] | 370 | if (mCanvas->drawTextAbsolutePos()) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 371 | memcpy(positions, pos, 2 * glyphs.count * sizeof(float)); |
Stan Iliev | 0b58d99 | 2017-03-30 18:22:27 -0400 | [diff] [blame] | 372 | } 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 Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 380 | bounds.fRight, bounds.fBottom, 0); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 384 | SkScalar constY, const SkPaint& paint) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 385 | const size_t pointCount = byteLength >> 1; |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 386 | std::unique_ptr<SkPoint[]> pts(new SkPoint[pointCount]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 387 | for (size_t i = 0; i < pointCount; i++) { |
| 388 | pts[i].set(xpos[i], constY); |
| 389 | } |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 390 | this->onDrawPosText(text, byteLength, pts.get(), paint); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 394 | const SkMatrix* matrix, const SkPaint& origPaint) { |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 395 | SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextOnPath is not supported"); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 396 | } |
| 397 | |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 398 | void SkiaCanvasProxy::onDrawTextRSXform(const void* text, size_t byteLength, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 399 | const SkRSXform xform[], const SkRect* cullRect, |
| 400 | const SkPaint& paint) { |
| 401 | GlyphIDConverter glyphs(text, byteLength, paint); // Just get count |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 402 | 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 Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 409 | this->onDrawText((char*)text + (byteLength / glyphs.count * i), byteLength / glyphs.count, |
| 410 | 0, 0, paint); |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 411 | } |
| 412 | mCanvas->setMatrix(origM); |
| 413 | } |
| 414 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 415 | void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 416 | const SkPaint& paint) { |
Derek Sollenberger | 09bd6c2 | 2016-11-03 12:54:06 -0400 | [diff] [blame] | 417 | SkPaint runPaint = paint; |
| 418 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 419 | 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 Sollenberger | 09bd6c2 | 2016-11-03 12:54:06 -0400 | [diff] [blame] | 426 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 427 | 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 Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 457 | const SkPoint texCoords[4], SkBlendMode bmode, |
| 458 | const SkPaint& paint) { |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 459 | if (mFilterHwuiCalls) { |
| 460 | return; |
| 461 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 462 | SkMatrix matrix; |
| 463 | mCanvas->getMatrix(&matrix); |
| 464 | SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &matrix); |
| 465 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 466 | mCanvas->drawVertices( |
| 467 | SkPatchUtils::MakeVertices(cubics, colors, texCoords, lod.width(), lod.height()).get(), |
| 468 | bmode, paint); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 469 | } |
| 470 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 471 | void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 472 | mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op); |
| 473 | } |
| 474 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 475 | void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkClipOp op, ClipEdgeStyle) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 476 | SkPath path; |
| 477 | path.addRRect(roundRect); |
| 478 | mCanvas->clipPath(&path, op); |
| 479 | } |
| 480 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 481 | void SkiaCanvasProxy::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 482 | mCanvas->clipPath(&path, op); |
| 483 | } |
| 484 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 485 | }; // namespace uirenderer |
| 486 | }; // namespace android |