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 | |
Mark Salyzyn | db15537 | 2017-01-11 08:30:03 -0800 | [diff] [blame] | 23 | #include "hwui/Bitmap.h" |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 24 | #include <SkLatticeIter.h> |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 25 | #include <SkPatchUtils.h> |
Ben Wagner | a11ee3c | 2015-08-04 11:14:15 -0400 | [diff] [blame] | 26 | #include <SkPaint.h> |
| 27 | #include <SkPath.h> |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 28 | #include <SkPixelRef.h> |
Ben Wagner | a11ee3c | 2015-08-04 11:14:15 -0400 | [diff] [blame] | 29 | #include <SkRect.h> |
| 30 | #include <SkRRect.h> |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 31 | #include <SkRSXform.h> |
Matt Sarett | 79fc3b1 | 2016-03-24 11:02:44 -0400 | [diff] [blame] | 32 | #include <SkSurface.h> |
Derek Sollenberger | 09bd6c2 | 2016-11-03 12:54:06 -0400 | [diff] [blame] | 33 | #include <SkTextBlobRunIterator.h> |
Mike Reed | 871cd2d | 2017-03-17 10:15:52 -0400 | [diff] [blame] | 34 | #include <SkVertices.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[], |
| 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 |
Ben Wagner | e3a40ea | 2015-08-19 15:49:37 -0400 | [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]); |
| 75 | path.lineTo(pts[i+1]); |
| 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(); |
| 98 | mCanvas->drawRoundRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, |
| 99 | radii.fX, radii.fY, paint); |
| 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) { |
| 109 | mCanvas->drawArc(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, |
| 110 | startAngle, sweepAngle, useCenter, paint); |
| 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, |
| 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(); |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 124 | mCanvas->drawBitmap(*hwuiBitmap, origin.fX, origin.fY, |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 125 | origin.fX + bitmap.dimensions().width(), |
| 126 | origin.fY + bitmap.dimensions().height(), |
| 127 | left, top, |
| 128 | left + bitmap.dimensions().width(), |
| 129 | top + bitmap.dimensions().height(), |
| 130 | paint); |
| 131 | } else { |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 132 | mCanvas->drawBitmap(*hwuiBitmap, left, top, paint); |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 133 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 134 | } |
| 135 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 136 | void SkiaCanvasProxy::onDrawBitmapRect(const SkBitmap& skBitmap, const SkRect* srcPtr, |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 137 | const SkRect& dst, const SkPaint* paint, SrcRectConstraint) { |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 138 | SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(skBitmap.width(), skBitmap.height()); |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 139 | // TODO: if bitmap is a subset, do we need to add pixelRefOrigin to src? |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 140 | Bitmap* bitmap = reinterpret_cast<Bitmap*>(skBitmap.pixelRef()); |
| 141 | mCanvas->drawBitmap(*bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom, |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 142 | dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint); |
| 143 | } |
| 144 | |
| 145 | void SkiaCanvasProxy::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 146 | const SkRect& dst, const SkPaint*) { |
| 147 | //TODO make nine-patch drawing a method on Canvas.h |
| 148 | SkDEBUGFAIL("SkiaCanvasProxy::onDrawBitmapNine is not yet supported"); |
| 149 | } |
| 150 | |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 151 | void SkiaCanvasProxy::onDrawImage(const SkImage* image, SkScalar left, SkScalar top, |
| 152 | const SkPaint* paint) { |
| 153 | SkBitmap skiaBitmap; |
| 154 | if (image->asLegacyBitmap(&skiaBitmap, SkImage::kRO_LegacyBitmapMode)) { |
| 155 | onDrawBitmap(skiaBitmap, left, top, paint); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void SkiaCanvasProxy::onDrawImageRect(const SkImage* image, const SkRect* srcPtr, const SkRect& dst, |
| 160 | const SkPaint* paint, SrcRectConstraint constraint) { |
| 161 | SkBitmap skiaBitmap; |
| 162 | if (image->asLegacyBitmap(&skiaBitmap, SkImage::kRO_LegacyBitmapMode)) { |
| 163 | sk_sp<Bitmap> bitmap = Bitmap::createFrom(skiaBitmap.info(), *skiaBitmap.pixelRef()); |
| 164 | SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(image->width(), image->height()); |
| 165 | mCanvas->drawBitmap(*bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom, |
| 166 | dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void SkiaCanvasProxy::onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst, |
| 171 | const SkPaint*) { |
| 172 | SkDEBUGFAIL("SkiaCanvasProxy::onDrawImageNine is not yet supported"); |
| 173 | } |
| 174 | |
| 175 | void SkiaCanvasProxy::onDrawImageLattice(const SkImage* image, const Lattice& lattice, |
| 176 | const SkRect& dst, const SkPaint* paint) { |
| 177 | SkLatticeIter iter(lattice, dst); |
| 178 | SkRect srcR, dstR; |
| 179 | while (iter.next(&srcR, &dstR)) { |
| 180 | onDrawImageRect(image, &srcR, dstR, paint, SkCanvas::kStrict_SrcRectConstraint); |
| 181 | } |
| 182 | } |
| 183 | |
Mike Reed | 871cd2d | 2017-03-17 10:15:52 -0400 | [diff] [blame] | 184 | void SkiaCanvasProxy::onDrawVerticesObject(const SkVertices* vertices, SkBlendMode bmode, |
| 185 | const SkPaint& paint) { |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame] | 186 | // TODO: should we pass through blendmode |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 187 | if (mFilterHwuiCalls) { |
| 188 | return; |
| 189 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 190 | // convert the SkPoints into floats |
Ben Wagner | e3a40ea | 2015-08-19 15:49:37 -0400 | [diff] [blame] | 191 | static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats"); |
Mike Reed | 871cd2d | 2017-03-17 10:15:52 -0400 | [diff] [blame] | 192 | const int floatCount = vertices->vertexCount() << 1; |
| 193 | const float* vArray = (const float*)vertices->positions(); |
| 194 | const float* tArray = (const float*)vertices->texCoords(); |
| 195 | const int* cArray = (const int*)vertices->colors(); |
| 196 | mCanvas->drawVertices(vertices->mode(), floatCount, vArray, tArray, cArray, |
| 197 | vertices->indices(), vertices->indexCount(), paint); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Matt Sarett | 79fc3b1 | 2016-03-24 11:02:44 -0400 | [diff] [blame] | 200 | sk_sp<SkSurface> SkiaCanvasProxy::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 201 | SkDEBUGFAIL("SkiaCanvasProxy::onNewSurface is not supported"); |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | void SkiaCanvasProxy::willSave() { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 206 | mCanvas->save(android::SaveFlags::MatrixClip); |
| 207 | } |
| 208 | |
| 209 | static inline SaveFlags::Flags saveFlags(SkCanvas::SaveLayerFlags layerFlags) { |
| 210 | SaveFlags::Flags saveFlags = 0; |
| 211 | |
| 212 | if (!(layerFlags & SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag)) { |
| 213 | saveFlags |= SaveFlags::ClipToLayer; |
| 214 | } |
| 215 | |
| 216 | if (!(layerFlags & SkCanvas::kIsOpaque_SaveLayerFlag)) { |
| 217 | saveFlags |= SaveFlags::HasAlphaLayer; |
| 218 | } |
| 219 | |
| 220 | return saveFlags; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 221 | } |
| 222 | |
Leon Scroggins III | 5518e7c | 2016-01-04 09:37:42 -0500 | [diff] [blame] | 223 | SkCanvas::SaveLayerStrategy SkiaCanvasProxy::getSaveLayerStrategy(const SaveLayerRec& saveLayerRec) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 224 | SkRect rect; |
Leon Scroggins III | 5518e7c | 2016-01-04 09:37:42 -0500 | [diff] [blame] | 225 | if (saveLayerRec.fBounds) { |
| 226 | rect = *saveLayerRec.fBounds; |
| 227 | } else if (!mCanvas->getClipBounds(&rect)) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 228 | rect = SkRect::MakeEmpty(); |
| 229 | } |
Leon Scroggins III | 5518e7c | 2016-01-04 09:37:42 -0500 | [diff] [blame] | 230 | mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, saveLayerRec.fPaint, |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 231 | saveFlags(saveLayerRec.fSaveLayerFlags)); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 232 | return SkCanvas::kNoLayer_SaveLayerStrategy; |
| 233 | } |
| 234 | |
| 235 | void SkiaCanvasProxy::willRestore() { |
| 236 | mCanvas->restore(); |
| 237 | } |
| 238 | |
| 239 | void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) { |
| 240 | mCanvas->concat(matrix); |
| 241 | } |
| 242 | |
| 243 | void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) { |
Chris Craik | 6daa13c | 2015-08-19 13:32:12 -0700 | [diff] [blame] | 244 | mCanvas->setMatrix(matrix); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void SkiaCanvasProxy::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, |
| 248 | const SkPaint& paint) { |
| 249 | SkPath path; |
| 250 | path.addRRect(outer); |
| 251 | path.addRRect(inner); |
| 252 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 253 | this->drawPath(path, paint); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Utility class that converts the incoming text & paint from the given encoding |
| 258 | * into glyphIDs. |
| 259 | */ |
| 260 | class GlyphIDConverter { |
| 261 | public: |
| 262 | GlyphIDConverter(const void* text, size_t byteLength, const SkPaint& origPaint) { |
| 263 | paint = origPaint; |
| 264 | if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) { |
| 265 | glyphIDs = (uint16_t*)text; |
| 266 | count = byteLength >> 1; |
| 267 | } else { |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 268 | // ensure space for one glyph per ID given UTF8 encoding. |
| 269 | storage.reset(new uint16_t[byteLength]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 270 | glyphIDs = storage.get(); |
| 271 | count = paint.textToGlyphs(text, byteLength, storage.get()); |
| 272 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | SkPaint paint; |
| 277 | uint16_t* glyphIDs; |
| 278 | int count; |
| 279 | private: |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 280 | std::unique_ptr<uint16_t[]> storage; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | void SkiaCanvasProxy::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
| 284 | const SkPaint& origPaint) { |
| 285 | // convert to glyphIDs if necessary |
| 286 | GlyphIDConverter glyphs(text, byteLength, origPaint); |
| 287 | |
| 288 | // compute the glyph positions |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 289 | std::unique_ptr<SkScalar[]> glyphWidths(new SkScalar[glyphs.count]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 290 | glyphs.paint.getTextWidths(glyphs.glyphIDs, glyphs.count << 1, glyphWidths.get()); |
| 291 | |
| 292 | // compute conservative bounds |
| 293 | // NOTE: We could call the faster paint.getFontBounds for a less accurate, |
| 294 | // but even more conservative bounds if this is too slow. |
| 295 | SkRect bounds; |
| 296 | glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds); |
| 297 | |
| 298 | // adjust for non-left alignment |
| 299 | if (glyphs.paint.getTextAlign() != SkPaint::kLeft_Align) { |
| 300 | SkScalar stop = 0; |
| 301 | for (int i = 0; i < glyphs.count; i++) { |
| 302 | stop += glyphWidths[i]; |
| 303 | } |
| 304 | if (glyphs.paint.getTextAlign() == SkPaint::kCenter_Align) { |
| 305 | stop = SkScalarHalf(stop); |
| 306 | } |
| 307 | if (glyphs.paint.isVerticalText()) { |
| 308 | y -= stop; |
| 309 | } else { |
| 310 | x -= stop; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // setup the first glyph position and adjust bounds if needed |
Tom Hudson | 806a6f0 | 2015-02-19 17:11:32 -0500 | [diff] [blame] | 315 | int xBaseline = 0; |
| 316 | int yBaseline = 0; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 317 | if (mCanvas->drawTextAbsolutePos()) { |
| 318 | bounds.offset(x,y); |
Tom Hudson | 806a6f0 | 2015-02-19 17:11:32 -0500 | [diff] [blame] | 319 | xBaseline = x; |
| 320 | yBaseline = y; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 321 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 322 | |
Ben Wagner | e3a40ea | 2015-08-19 15:49:37 -0400 | [diff] [blame] | 323 | static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats"); |
Stan Iliev | 0b58d99 | 2017-03-30 18:22:27 -0400 | [diff] [blame^] | 324 | auto glyphFunc = [&] (uint16_t* text, float* positions) { |
| 325 | memcpy(text, glyphs.glyphIDs, glyphs.count*sizeof(uint16_t)); |
| 326 | size_t posIndex = 0; |
| 327 | // setup the first glyph position |
| 328 | positions[posIndex++] = xBaseline; |
| 329 | positions[posIndex++] = yBaseline; |
| 330 | // setup the remaining glyph positions |
| 331 | if (glyphs.paint.isVerticalText()) { |
| 332 | float yPosition = yBaseline; |
| 333 | for (int i = 1; i < glyphs.count; i++) { |
| 334 | positions[posIndex++] = xBaseline; |
| 335 | yPosition += glyphWidths[i-1]; |
| 336 | positions[posIndex++] = yPosition; |
| 337 | } |
| 338 | } else { |
| 339 | float xPosition = xBaseline; |
| 340 | for (int i = 1; i < glyphs.count; i++) { |
| 341 | xPosition += glyphWidths[i-1]; |
| 342 | positions[posIndex++] = xPosition; |
| 343 | positions[posIndex++] = yBaseline; |
| 344 | } |
| 345 | } |
| 346 | }; |
| 347 | mCanvas->drawGlyphs(glyphFunc, glyphs.count, glyphs.paint, x, y, bounds.fLeft, bounds.fTop, |
| 348 | bounds.fRight, bounds.fBottom, 0); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void SkiaCanvasProxy::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
| 352 | const SkPaint& origPaint) { |
| 353 | // convert to glyphIDs if necessary |
| 354 | GlyphIDConverter glyphs(text, byteLength, origPaint); |
| 355 | |
| 356 | // convert to relative positions if necessary |
| 357 | int x, y; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 358 | if (mCanvas->drawTextAbsolutePos()) { |
| 359 | x = 0; |
| 360 | y = 0; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 361 | } else { |
| 362 | x = pos[0].fX; |
| 363 | y = pos[0].fY; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 364 | } |
| 365 | |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 366 | // Compute conservative bounds. If the content has already been processed |
| 367 | // by Minikin then it had already computed these bounds. Unfortunately, |
| 368 | // there is no way to capture those bounds as part of the Skia drawPosText |
| 369 | // API so we need to do that computation again here. |
Stan Iliev | d84d2ee | 2016-07-25 13:32:25 -0400 | [diff] [blame] | 370 | SkRect bounds = SkRect::MakeEmpty(); |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 371 | for (int i = 0; i < glyphs.count; i++) { |
Stan Iliev | d84d2ee | 2016-07-25 13:32:25 -0400 | [diff] [blame] | 372 | SkRect glyphBounds = SkRect::MakeEmpty(); |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 373 | glyphs.paint.measureText(&glyphs.glyphIDs[i], sizeof(uint16_t), &glyphBounds); |
| 374 | glyphBounds.offset(pos[i].fX, pos[i].fY); |
| 375 | bounds.join(glyphBounds); |
| 376 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 377 | |
Ben Wagner | e3a40ea | 2015-08-19 15:49:37 -0400 | [diff] [blame] | 378 | static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats"); |
Stan Iliev | 0b58d99 | 2017-03-30 18:22:27 -0400 | [diff] [blame^] | 379 | auto glyphFunc = [&] (uint16_t* text, float* positions) { |
| 380 | memcpy(text, glyphs.glyphIDs, glyphs.count*sizeof(uint16_t)); |
| 381 | if (mCanvas->drawTextAbsolutePos()) { |
| 382 | memcpy(positions, pos, 2*glyphs.count*sizeof(float)); |
| 383 | } else { |
| 384 | for (int i = 0, posIndex = 0; i < glyphs.count; i++) { |
| 385 | positions[posIndex++] = pos[i].fX - x; |
| 386 | positions[posIndex++] = pos[i].fY - y; |
| 387 | } |
| 388 | } |
| 389 | }; |
| 390 | mCanvas->drawGlyphs(glyphFunc, glyphs.count, glyphs.paint, x, y, bounds.fLeft, bounds.fTop, |
| 391 | bounds.fRight, bounds.fBottom, 0); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], |
| 395 | SkScalar constY, const SkPaint& paint) { |
| 396 | const size_t pointCount = byteLength >> 1; |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 397 | std::unique_ptr<SkPoint[]> pts(new SkPoint[pointCount]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 398 | for (size_t i = 0; i < pointCount; i++) { |
| 399 | pts[i].set(xpos[i], constY); |
| 400 | } |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 401 | this->onDrawPosText(text, byteLength, pts.get(), paint); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, |
| 405 | const SkMatrix* matrix, const SkPaint& origPaint) { |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 406 | SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextOnPath is not supported"); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 407 | } |
| 408 | |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 409 | void SkiaCanvasProxy::onDrawTextRSXform(const void* text, size_t byteLength, |
| 410 | const SkRSXform xform[], const SkRect* cullRect, const SkPaint& paint) { |
| 411 | GlyphIDConverter glyphs(text, byteLength, paint); // Just get count |
| 412 | SkMatrix localM, currM, origM; |
| 413 | mCanvas->getMatrix(&currM); |
| 414 | origM = currM; |
| 415 | for (int i = 0; i < glyphs.count; i++) { |
| 416 | localM.setRSXform(*xform++); |
| 417 | currM.setConcat(origM, localM); |
| 418 | mCanvas->setMatrix(currM); |
| 419 | this->onDrawText((char*)text + (byteLength / glyphs.count * i), |
| 420 | byteLength / glyphs.count, 0, 0, paint); |
| 421 | } |
| 422 | mCanvas->setMatrix(origM); |
| 423 | } |
| 424 | |
| 425 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 426 | void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 427 | const SkPaint& paint) { |
Derek Sollenberger | 09bd6c2 | 2016-11-03 12:54:06 -0400 | [diff] [blame] | 428 | SkPaint runPaint = paint; |
| 429 | |
| 430 | SkTextBlobRunIterator it(blob); |
| 431 | for (;!it.done(); it.next()) { |
| 432 | size_t textLen = it.glyphCount() * sizeof(uint16_t); |
| 433 | const SkPoint& offset = it.offset(); |
| 434 | // applyFontToPaint() always overwrites the exact same attributes, |
| 435 | // so it is safe to not re-seed the paint for this reason. |
| 436 | it.applyFontToPaint(&runPaint); |
| 437 | |
| 438 | switch (it.positioning()) { |
| 439 | case SkTextBlob::kDefault_Positioning: |
| 440 | this->drawText(it.glyphs(), textLen, x + offset.x(), y + offset.y(), runPaint); |
| 441 | break; |
| 442 | case SkTextBlob::kHorizontal_Positioning: { |
| 443 | std::unique_ptr<SkPoint[]> pts(new SkPoint[it.glyphCount()]); |
| 444 | for (size_t i = 0; i < it.glyphCount(); i++) { |
| 445 | pts[i].set(x + offset.x() + it.pos()[i], y + offset.y()); |
| 446 | } |
| 447 | this->drawPosText(it.glyphs(), textLen, pts.get(), runPaint); |
| 448 | break; |
| 449 | } |
| 450 | case SkTextBlob::kFull_Positioning: { |
| 451 | std::unique_ptr<SkPoint[]> pts(new SkPoint[it.glyphCount()]); |
| 452 | for (size_t i = 0; i < it.glyphCount(); i++) { |
| 453 | const size_t xIndex = i*2; |
| 454 | const size_t yIndex = xIndex + 1; |
| 455 | pts[i].set(x + offset.x() + it.pos()[xIndex], y + offset.y() + it.pos()[yIndex]); |
| 456 | } |
| 457 | this->drawPosText(it.glyphs(), textLen, pts.get(), runPaint); |
| 458 | break; |
| 459 | } |
| 460 | default: |
| 461 | SkFAIL("unhandled positioning mode"); |
| 462 | } |
| 463 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame] | 467 | const SkPoint texCoords[4], SkBlendMode bmode, const SkPaint& paint) { |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 468 | if (mFilterHwuiCalls) { |
| 469 | return; |
| 470 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 471 | SkPatchUtils::VertexData data; |
| 472 | |
| 473 | SkMatrix matrix; |
| 474 | mCanvas->getMatrix(&matrix); |
| 475 | SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &matrix); |
| 476 | |
| 477 | // It automatically adjusts lodX and lodY in case it exceeds the number of indices. |
| 478 | // If it fails to generate the vertices, then we do not draw. |
| 479 | if (SkPatchUtils::getVertexData(&data, cubics, colors, texCoords, lod.width(), lod.height())) { |
| 480 | this->drawVertices(SkCanvas::kTriangles_VertexMode, data.fVertexCount, data.fPoints, |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame] | 481 | data.fTexCoords, data.fColors, bmode, data.fIndices, data.fIndexCount, |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 482 | paint); |
| 483 | } |
| 484 | } |
| 485 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 486 | void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 487 | mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op); |
| 488 | } |
| 489 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 490 | void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkClipOp op, ClipEdgeStyle) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 491 | SkPath path; |
| 492 | path.addRRect(roundRect); |
| 493 | mCanvas->clipPath(&path, op); |
| 494 | } |
| 495 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 496 | void SkiaCanvasProxy::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 497 | mCanvas->clipPath(&path, op); |
| 498 | } |
| 499 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 500 | }; // namespace uirenderer |
| 501 | }; // namespace android |