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 | |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 19 | #include "hwui/Bitmap.h" |
| 20 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 21 | #include <cutils/log.h> |
| 22 | #include <SkPatchUtils.h> |
Ben Wagner | a11ee3c | 2015-08-04 11:14:15 -0400 | [diff] [blame] | 23 | #include <SkPaint.h> |
| 24 | #include <SkPath.h> |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 25 | #include <SkPixelRef.h> |
Ben Wagner | a11ee3c | 2015-08-04 11:14:15 -0400 | [diff] [blame] | 26 | #include <SkRect.h> |
| 27 | #include <SkRRect.h> |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 28 | #include <SkRSXform.h> |
Matt Sarett | 79fc3b1 | 2016-03-24 11:02:44 -0400 | [diff] [blame] | 29 | #include <SkSurface.h> |
Derek Sollenberger | 09bd6c2 | 2016-11-03 12:54:06 -0400 | [diff] [blame] | 30 | #include <SkTextBlobRunIterator.h> |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 31 | |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 32 | #include <memory> |
| 33 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 34 | namespace android { |
| 35 | namespace uirenderer { |
| 36 | |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 37 | SkiaCanvasProxy::SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls) |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 38 | : INHERITED(canvas->width(), canvas->height()) |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 39 | , mCanvas(canvas) |
| 40 | , mFilterHwuiCalls(filterHwuiCalls) {} |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 41 | |
| 42 | void SkiaCanvasProxy::onDrawPaint(const SkPaint& paint) { |
| 43 | mCanvas->drawPaint(paint); |
| 44 | } |
| 45 | |
| 46 | void SkiaCanvasProxy::onDrawPoints(PointMode pointMode, size_t count, const SkPoint pts[], |
| 47 | const SkPaint& paint) { |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 48 | if (!pts || count == 0) { |
| 49 | return; |
| 50 | } |
| 51 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 52 | // convert the SkPoints into floats |
Ben Wagner | e3a40ea | 2015-08-19 15:49:37 -0400 | [diff] [blame] | 53 | 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] | 54 | const size_t floatCount = count << 1; |
| 55 | const float* floatArray = &pts[0].fX; |
| 56 | |
| 57 | switch (pointMode) { |
| 58 | case kPoints_PointMode: { |
| 59 | mCanvas->drawPoints(floatArray, floatCount, paint); |
| 60 | break; |
| 61 | } |
| 62 | case kLines_PointMode: { |
| 63 | mCanvas->drawLines(floatArray, floatCount, paint); |
| 64 | break; |
| 65 | } |
| 66 | case kPolygon_PointMode: { |
| 67 | SkPaint strokedPaint(paint); |
| 68 | strokedPaint.setStyle(SkPaint::kStroke_Style); |
| 69 | |
| 70 | SkPath path; |
| 71 | for (size_t i = 0; i < count - 1; i++) { |
| 72 | path.moveTo(pts[i]); |
| 73 | path.lineTo(pts[i+1]); |
| 74 | this->drawPath(path, strokedPaint); |
| 75 | path.rewind(); |
| 76 | } |
| 77 | break; |
| 78 | } |
| 79 | default: |
| 80 | LOG_ALWAYS_FATAL("Unknown point type"); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void SkiaCanvasProxy::onDrawOval(const SkRect& rect, const SkPaint& paint) { |
| 85 | mCanvas->drawOval(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint); |
| 86 | } |
| 87 | |
| 88 | void SkiaCanvasProxy::onDrawRect(const SkRect& rect, const SkPaint& paint) { |
| 89 | mCanvas->drawRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint); |
| 90 | } |
| 91 | |
| 92 | void SkiaCanvasProxy::onDrawRRect(const SkRRect& roundRect, const SkPaint& paint) { |
| 93 | if (!roundRect.isComplex()) { |
| 94 | const SkRect& rect = roundRect.rect(); |
| 95 | SkVector radii = roundRect.getSimpleRadii(); |
| 96 | mCanvas->drawRoundRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, |
| 97 | radii.fX, radii.fY, paint); |
| 98 | } else { |
| 99 | SkPath path; |
| 100 | path.addRRect(roundRect); |
| 101 | mCanvas->drawPath(path, paint); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void SkiaCanvasProxy::onDrawPath(const SkPath& path, const SkPaint& paint) { |
| 106 | mCanvas->drawPath(path, paint); |
| 107 | } |
| 108 | |
| 109 | void SkiaCanvasProxy::onDrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, |
| 110 | const SkPaint* paint) { |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 111 | sk_sp<Bitmap> hwuiBitmap = Bitmap::createFrom(bitmap.info(), *bitmap.pixelRef()); |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 112 | // HWUI doesn't support extractSubset(), so convert any subsetted bitmap into |
| 113 | // a drawBitmapRect(); pass through an un-subsetted bitmap. |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 114 | if (hwuiBitmap && bitmap.dimensions() != hwuiBitmap->info().dimensions()) { |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 115 | SkIPoint origin = bitmap.pixelRefOrigin(); |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 116 | mCanvas->drawBitmap(*hwuiBitmap, origin.fX, origin.fY, |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 117 | origin.fX + bitmap.dimensions().width(), |
| 118 | origin.fY + bitmap.dimensions().height(), |
| 119 | left, top, |
| 120 | left + bitmap.dimensions().width(), |
| 121 | top + bitmap.dimensions().height(), |
| 122 | paint); |
| 123 | } else { |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 124 | mCanvas->drawBitmap(*hwuiBitmap, left, top, paint); |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 125 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 126 | } |
| 127 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 128 | void SkiaCanvasProxy::onDrawBitmapRect(const SkBitmap& skBitmap, const SkRect* srcPtr, |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 129 | const SkRect& dst, const SkPaint* paint, SrcRectConstraint) { |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 130 | SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(skBitmap.width(), skBitmap.height()); |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 131 | // 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] | 132 | Bitmap* bitmap = reinterpret_cast<Bitmap*>(skBitmap.pixelRef()); |
| 133 | mCanvas->drawBitmap(*bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom, |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 134 | dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint); |
| 135 | } |
| 136 | |
| 137 | void SkiaCanvasProxy::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 138 | const SkRect& dst, const SkPaint*) { |
| 139 | //TODO make nine-patch drawing a method on Canvas.h |
| 140 | SkDEBUGFAIL("SkiaCanvasProxy::onDrawBitmapNine is not yet supported"); |
| 141 | } |
| 142 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 143 | void SkiaCanvasProxy::onDrawVertices(VertexMode mode, int vertexCount, const SkPoint vertices[], |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame] | 144 | const SkPoint texs[], const SkColor colors[], SkBlendMode, const uint16_t indices[], |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 145 | int indexCount, const SkPaint& paint) { |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame] | 146 | // TODO: should we pass through blendmode |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 147 | if (mFilterHwuiCalls) { |
| 148 | return; |
| 149 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 150 | // convert the SkPoints into floats |
Ben Wagner | e3a40ea | 2015-08-19 15:49:37 -0400 | [diff] [blame] | 151 | 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] | 152 | const int floatCount = vertexCount << 1; |
| 153 | const float* vArray = &vertices[0].fX; |
| 154 | const float* tArray = (texs) ? &texs[0].fX : NULL; |
| 155 | const int* cArray = (colors) ? (int*)colors : NULL; |
| 156 | mCanvas->drawVertices(mode, floatCount, vArray, tArray, cArray, indices, indexCount, paint); |
| 157 | } |
| 158 | |
Matt Sarett | 79fc3b1 | 2016-03-24 11:02:44 -0400 | [diff] [blame] | 159 | sk_sp<SkSurface> SkiaCanvasProxy::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 160 | SkDEBUGFAIL("SkiaCanvasProxy::onNewSurface is not supported"); |
| 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | void SkiaCanvasProxy::willSave() { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 165 | mCanvas->save(android::SaveFlags::MatrixClip); |
| 166 | } |
| 167 | |
| 168 | static inline SaveFlags::Flags saveFlags(SkCanvas::SaveLayerFlags layerFlags) { |
| 169 | SaveFlags::Flags saveFlags = 0; |
| 170 | |
| 171 | if (!(layerFlags & SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag)) { |
| 172 | saveFlags |= SaveFlags::ClipToLayer; |
| 173 | } |
| 174 | |
| 175 | if (!(layerFlags & SkCanvas::kIsOpaque_SaveLayerFlag)) { |
| 176 | saveFlags |= SaveFlags::HasAlphaLayer; |
| 177 | } |
| 178 | |
| 179 | return saveFlags; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 180 | } |
| 181 | |
Leon Scroggins III | 5518e7c | 2016-01-04 09:37:42 -0500 | [diff] [blame] | 182 | SkCanvas::SaveLayerStrategy SkiaCanvasProxy::getSaveLayerStrategy(const SaveLayerRec& saveLayerRec) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 183 | SkRect rect; |
Leon Scroggins III | 5518e7c | 2016-01-04 09:37:42 -0500 | [diff] [blame] | 184 | if (saveLayerRec.fBounds) { |
| 185 | rect = *saveLayerRec.fBounds; |
| 186 | } else if (!mCanvas->getClipBounds(&rect)) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 187 | rect = SkRect::MakeEmpty(); |
| 188 | } |
Leon Scroggins III | 5518e7c | 2016-01-04 09:37:42 -0500 | [diff] [blame] | 189 | mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, saveLayerRec.fPaint, |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 190 | saveFlags(saveLayerRec.fSaveLayerFlags)); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 191 | return SkCanvas::kNoLayer_SaveLayerStrategy; |
| 192 | } |
| 193 | |
| 194 | void SkiaCanvasProxy::willRestore() { |
| 195 | mCanvas->restore(); |
| 196 | } |
| 197 | |
| 198 | void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) { |
| 199 | mCanvas->concat(matrix); |
| 200 | } |
| 201 | |
| 202 | void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) { |
Chris Craik | 6daa13c | 2015-08-19 13:32:12 -0700 | [diff] [blame] | 203 | mCanvas->setMatrix(matrix); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void SkiaCanvasProxy::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, |
| 207 | const SkPaint& paint) { |
| 208 | SkPath path; |
| 209 | path.addRRect(outer); |
| 210 | path.addRRect(inner); |
| 211 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 212 | this->drawPath(path, paint); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Utility class that converts the incoming text & paint from the given encoding |
| 217 | * into glyphIDs. |
| 218 | */ |
| 219 | class GlyphIDConverter { |
| 220 | public: |
| 221 | GlyphIDConverter(const void* text, size_t byteLength, const SkPaint& origPaint) { |
| 222 | paint = origPaint; |
| 223 | if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) { |
| 224 | glyphIDs = (uint16_t*)text; |
| 225 | count = byteLength >> 1; |
| 226 | } else { |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 227 | // ensure space for one glyph per ID given UTF8 encoding. |
| 228 | storage.reset(new uint16_t[byteLength]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 229 | glyphIDs = storage.get(); |
| 230 | count = paint.textToGlyphs(text, byteLength, storage.get()); |
| 231 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | SkPaint paint; |
| 236 | uint16_t* glyphIDs; |
| 237 | int count; |
| 238 | private: |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 239 | std::unique_ptr<uint16_t[]> storage; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | void SkiaCanvasProxy::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
| 243 | const SkPaint& origPaint) { |
| 244 | // convert to glyphIDs if necessary |
| 245 | GlyphIDConverter glyphs(text, byteLength, origPaint); |
| 246 | |
| 247 | // compute the glyph positions |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 248 | std::unique_ptr<SkPoint[]> pointStorage(new SkPoint[glyphs.count]); |
| 249 | std::unique_ptr<SkScalar[]> glyphWidths(new SkScalar[glyphs.count]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 250 | glyphs.paint.getTextWidths(glyphs.glyphIDs, glyphs.count << 1, glyphWidths.get()); |
| 251 | |
| 252 | // compute conservative bounds |
| 253 | // NOTE: We could call the faster paint.getFontBounds for a less accurate, |
| 254 | // but even more conservative bounds if this is too slow. |
| 255 | SkRect bounds; |
| 256 | glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds); |
| 257 | |
| 258 | // adjust for non-left alignment |
| 259 | if (glyphs.paint.getTextAlign() != SkPaint::kLeft_Align) { |
| 260 | SkScalar stop = 0; |
| 261 | for (int i = 0; i < glyphs.count; i++) { |
| 262 | stop += glyphWidths[i]; |
| 263 | } |
| 264 | if (glyphs.paint.getTextAlign() == SkPaint::kCenter_Align) { |
| 265 | stop = SkScalarHalf(stop); |
| 266 | } |
| 267 | if (glyphs.paint.isVerticalText()) { |
| 268 | y -= stop; |
| 269 | } else { |
| 270 | x -= stop; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // setup the first glyph position and adjust bounds if needed |
Tom Hudson | 806a6f0 | 2015-02-19 17:11:32 -0500 | [diff] [blame] | 275 | int xBaseline = 0; |
| 276 | int yBaseline = 0; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 277 | if (mCanvas->drawTextAbsolutePos()) { |
| 278 | bounds.offset(x,y); |
Tom Hudson | 806a6f0 | 2015-02-19 17:11:32 -0500 | [diff] [blame] | 279 | xBaseline = x; |
| 280 | yBaseline = y; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 281 | } |
Tom Hudson | 806a6f0 | 2015-02-19 17:11:32 -0500 | [diff] [blame] | 282 | pointStorage[0].set(xBaseline, yBaseline); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 283 | |
| 284 | // setup the remaining glyph positions |
| 285 | if (glyphs.paint.isVerticalText()) { |
| 286 | for (int i = 1; i < glyphs.count; i++) { |
Tom Hudson | 806a6f0 | 2015-02-19 17:11:32 -0500 | [diff] [blame] | 287 | pointStorage[i].set(xBaseline, glyphWidths[i-1] + pointStorage[i-1].fY); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 288 | } |
| 289 | } else { |
| 290 | for (int i = 1; i < glyphs.count; i++) { |
Tom Hudson | 806a6f0 | 2015-02-19 17:11:32 -0500 | [diff] [blame] | 291 | pointStorage[i].set(glyphWidths[i-1] + pointStorage[i-1].fX, yBaseline); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
Ben Wagner | e3a40ea | 2015-08-19 15:49:37 -0400 | [diff] [blame] | 295 | static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats"); |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 296 | mCanvas->drawGlyphs(glyphs.glyphIDs, &pointStorage[0].fX, glyphs.count, glyphs.paint, |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 297 | x, y, bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0); |
| 298 | } |
| 299 | |
| 300 | void SkiaCanvasProxy::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
| 301 | const SkPaint& origPaint) { |
| 302 | // convert to glyphIDs if necessary |
| 303 | GlyphIDConverter glyphs(text, byteLength, origPaint); |
| 304 | |
| 305 | // convert to relative positions if necessary |
| 306 | int x, y; |
| 307 | const SkPoint* posArray; |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 308 | std::unique_ptr<SkPoint[]> pointStorage; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 309 | if (mCanvas->drawTextAbsolutePos()) { |
| 310 | x = 0; |
| 311 | y = 0; |
| 312 | posArray = pos; |
| 313 | } else { |
| 314 | x = pos[0].fX; |
| 315 | y = pos[0].fY; |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 316 | pointStorage.reset(new SkPoint[glyphs.count]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 317 | for (int i = 0; i < glyphs.count; i++) { |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 318 | pointStorage[i].fX = pos[i].fX - x; |
| 319 | pointStorage[i].fY = pos[i].fY - y; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 320 | } |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 321 | posArray = pointStorage.get(); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 322 | } |
| 323 | |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 324 | // Compute conservative bounds. If the content has already been processed |
| 325 | // by Minikin then it had already computed these bounds. Unfortunately, |
| 326 | // there is no way to capture those bounds as part of the Skia drawPosText |
| 327 | // API so we need to do that computation again here. |
Stan Iliev | d84d2ee | 2016-07-25 13:32:25 -0400 | [diff] [blame] | 328 | SkRect bounds = SkRect::MakeEmpty(); |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 329 | for (int i = 0; i < glyphs.count; i++) { |
Stan Iliev | d84d2ee | 2016-07-25 13:32:25 -0400 | [diff] [blame] | 330 | SkRect glyphBounds = SkRect::MakeEmpty(); |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 331 | glyphs.paint.measureText(&glyphs.glyphIDs[i], sizeof(uint16_t), &glyphBounds); |
| 332 | glyphBounds.offset(pos[i].fX, pos[i].fY); |
| 333 | bounds.join(glyphBounds); |
| 334 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 335 | |
Ben Wagner | e3a40ea | 2015-08-19 15:49:37 -0400 | [diff] [blame] | 336 | static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats"); |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 337 | mCanvas->drawGlyphs(glyphs.glyphIDs, &posArray[0].fX, glyphs.count, glyphs.paint, x, y, |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 338 | bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0); |
| 339 | } |
| 340 | |
| 341 | void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], |
| 342 | SkScalar constY, const SkPaint& paint) { |
| 343 | const size_t pointCount = byteLength >> 1; |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 344 | std::unique_ptr<SkPoint[]> pts(new SkPoint[pointCount]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 345 | for (size_t i = 0; i < pointCount; i++) { |
| 346 | pts[i].set(xpos[i], constY); |
| 347 | } |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 348 | this->onDrawPosText(text, byteLength, pts.get(), paint); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, |
| 352 | const SkMatrix* matrix, const SkPaint& origPaint) { |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 353 | SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextOnPath is not supported"); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 354 | } |
| 355 | |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 356 | void SkiaCanvasProxy::onDrawTextRSXform(const void* text, size_t byteLength, |
| 357 | const SkRSXform xform[], const SkRect* cullRect, const SkPaint& paint) { |
| 358 | GlyphIDConverter glyphs(text, byteLength, paint); // Just get count |
| 359 | SkMatrix localM, currM, origM; |
| 360 | mCanvas->getMatrix(&currM); |
| 361 | origM = currM; |
| 362 | for (int i = 0; i < glyphs.count; i++) { |
| 363 | localM.setRSXform(*xform++); |
| 364 | currM.setConcat(origM, localM); |
| 365 | mCanvas->setMatrix(currM); |
| 366 | this->onDrawText((char*)text + (byteLength / glyphs.count * i), |
| 367 | byteLength / glyphs.count, 0, 0, paint); |
| 368 | } |
| 369 | mCanvas->setMatrix(origM); |
| 370 | } |
| 371 | |
| 372 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 373 | void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 374 | const SkPaint& paint) { |
Derek Sollenberger | 09bd6c2 | 2016-11-03 12:54:06 -0400 | [diff] [blame] | 375 | SkPaint runPaint = paint; |
| 376 | |
| 377 | SkTextBlobRunIterator it(blob); |
| 378 | for (;!it.done(); it.next()) { |
| 379 | size_t textLen = it.glyphCount() * sizeof(uint16_t); |
| 380 | const SkPoint& offset = it.offset(); |
| 381 | // applyFontToPaint() always overwrites the exact same attributes, |
| 382 | // so it is safe to not re-seed the paint for this reason. |
| 383 | it.applyFontToPaint(&runPaint); |
| 384 | |
| 385 | switch (it.positioning()) { |
| 386 | case SkTextBlob::kDefault_Positioning: |
| 387 | this->drawText(it.glyphs(), textLen, x + offset.x(), y + offset.y(), runPaint); |
| 388 | break; |
| 389 | case SkTextBlob::kHorizontal_Positioning: { |
| 390 | std::unique_ptr<SkPoint[]> pts(new SkPoint[it.glyphCount()]); |
| 391 | for (size_t i = 0; i < it.glyphCount(); i++) { |
| 392 | pts[i].set(x + offset.x() + it.pos()[i], y + offset.y()); |
| 393 | } |
| 394 | this->drawPosText(it.glyphs(), textLen, pts.get(), runPaint); |
| 395 | break; |
| 396 | } |
| 397 | case SkTextBlob::kFull_Positioning: { |
| 398 | std::unique_ptr<SkPoint[]> pts(new SkPoint[it.glyphCount()]); |
| 399 | for (size_t i = 0; i < it.glyphCount(); i++) { |
| 400 | const size_t xIndex = i*2; |
| 401 | const size_t yIndex = xIndex + 1; |
| 402 | pts[i].set(x + offset.x() + it.pos()[xIndex], y + offset.y() + it.pos()[yIndex]); |
| 403 | } |
| 404 | this->drawPosText(it.glyphs(), textLen, pts.get(), runPaint); |
| 405 | break; |
| 406 | } |
| 407 | default: |
| 408 | SkFAIL("unhandled positioning mode"); |
| 409 | } |
| 410 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame] | 414 | const SkPoint texCoords[4], SkBlendMode bmode, const SkPaint& paint) { |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 415 | if (mFilterHwuiCalls) { |
| 416 | return; |
| 417 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 418 | SkPatchUtils::VertexData data; |
| 419 | |
| 420 | SkMatrix matrix; |
| 421 | mCanvas->getMatrix(&matrix); |
| 422 | SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &matrix); |
| 423 | |
| 424 | // It automatically adjusts lodX and lodY in case it exceeds the number of indices. |
| 425 | // If it fails to generate the vertices, then we do not draw. |
| 426 | if (SkPatchUtils::getVertexData(&data, cubics, colors, texCoords, lod.width(), lod.height())) { |
| 427 | this->drawVertices(SkCanvas::kTriangles_VertexMode, data.fVertexCount, data.fPoints, |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame] | 428 | data.fTexCoords, data.fColors, bmode, data.fIndices, data.fIndexCount, |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 429 | paint); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle) { |
| 434 | mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op); |
| 435 | } |
| 436 | |
| 437 | void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkRegion::Op op, ClipEdgeStyle) { |
| 438 | SkPath path; |
| 439 | path.addRRect(roundRect); |
| 440 | mCanvas->clipPath(&path, op); |
| 441 | } |
| 442 | |
| 443 | void SkiaCanvasProxy::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle) { |
| 444 | mCanvas->clipPath(&path, op); |
| 445 | } |
| 446 | |
| 447 | void SkiaCanvasProxy::onClipRegion(const SkRegion& region, SkRegion::Op op) { |
| 448 | mCanvas->clipRegion(®ion, op); |
| 449 | } |
| 450 | |
| 451 | }; // namespace uirenderer |
| 452 | }; // namespace android |