SkiaCanvasProxy.cpp revision 6e49c9f007c879f05b035c40c0ba543c00f9d0d0
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 19#include "hwui/Bitmap.h" 20 21#include <cutils/log.h> 22#include <SkPatchUtils.h> 23#include <SkPaint.h> 24#include <SkPath.h> 25#include <SkPixelRef.h> 26#include <SkRect.h> 27#include <SkRRect.h> 28#include <SkRSXform.h> 29#include <SkSurface.h> 30#include <SkTextBlobRunIterator.h> 31 32#include <memory> 33 34namespace android { 35namespace uirenderer { 36 37SkiaCanvasProxy::SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls) 38 : INHERITED(canvas->width(), canvas->height()) 39 , mCanvas(canvas) 40 , mFilterHwuiCalls(filterHwuiCalls) {} 41 42void SkiaCanvasProxy::onDrawPaint(const SkPaint& paint) { 43 mCanvas->drawPaint(paint); 44} 45 46void SkiaCanvasProxy::onDrawPoints(PointMode pointMode, size_t count, const SkPoint pts[], 47 const SkPaint& paint) { 48 if (!pts || count == 0) { 49 return; 50 } 51 52 // convert the SkPoints into floats 53 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats"); 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 84void SkiaCanvasProxy::onDrawOval(const SkRect& rect, const SkPaint& paint) { 85 mCanvas->drawOval(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint); 86} 87 88void SkiaCanvasProxy::onDrawRect(const SkRect& rect, const SkPaint& paint) { 89 mCanvas->drawRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint); 90} 91 92void 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 105void SkiaCanvasProxy::onDrawPath(const SkPath& path, const SkPaint& paint) { 106 mCanvas->drawPath(path, paint); 107} 108 109void SkiaCanvasProxy::onDrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, 110 const SkPaint* paint) { 111 sk_sp<Bitmap> hwuiBitmap = Bitmap::createFrom(bitmap.info(), *bitmap.pixelRef()); 112 // HWUI doesn't support extractSubset(), so convert any subsetted bitmap into 113 // a drawBitmapRect(); pass through an un-subsetted bitmap. 114 if (hwuiBitmap && bitmap.dimensions() != hwuiBitmap->info().dimensions()) { 115 SkIPoint origin = bitmap.pixelRefOrigin(); 116 mCanvas->drawBitmap(*hwuiBitmap, origin.fX, origin.fY, 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 { 124 mCanvas->drawBitmap(*hwuiBitmap, left, top, paint); 125 } 126} 127 128void SkiaCanvasProxy::onDrawBitmapRect(const SkBitmap& skBitmap, const SkRect* srcPtr, 129 const SkRect& dst, const SkPaint* paint, SrcRectConstraint) { 130 SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(skBitmap.width(), skBitmap.height()); 131 // TODO: if bitmap is a subset, do we need to add pixelRefOrigin to src? 132 Bitmap* bitmap = reinterpret_cast<Bitmap*>(skBitmap.pixelRef()); 133 mCanvas->drawBitmap(*bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom, 134 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint); 135} 136 137void 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 143void SkiaCanvasProxy::onDrawVertices(VertexMode mode, int vertexCount, const SkPoint vertices[], 144 const SkPoint texs[], const SkColor colors[], SkBlendMode, const uint16_t indices[], 145 int indexCount, const SkPaint& paint) { 146 // TODO: should we pass through blendmode 147 if (mFilterHwuiCalls) { 148 return; 149 } 150 // convert the SkPoints into floats 151 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats"); 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 159sk_sp<SkSurface> SkiaCanvasProxy::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) { 160 SkDEBUGFAIL("SkiaCanvasProxy::onNewSurface is not supported"); 161 return NULL; 162} 163 164void SkiaCanvasProxy::willSave() { 165 mCanvas->save(android::SaveFlags::MatrixClip); 166} 167 168static 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; 180} 181 182SkCanvas::SaveLayerStrategy SkiaCanvasProxy::getSaveLayerStrategy(const SaveLayerRec& saveLayerRec) { 183 SkRect rect; 184 if (saveLayerRec.fBounds) { 185 rect = *saveLayerRec.fBounds; 186 } else if (!mCanvas->getClipBounds(&rect)) { 187 rect = SkRect::MakeEmpty(); 188 } 189 mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, saveLayerRec.fPaint, 190 saveFlags(saveLayerRec.fSaveLayerFlags)); 191 return SkCanvas::kNoLayer_SaveLayerStrategy; 192} 193 194void SkiaCanvasProxy::willRestore() { 195 mCanvas->restore(); 196} 197 198void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) { 199 mCanvas->concat(matrix); 200} 201 202void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) { 203 mCanvas->setMatrix(matrix); 204} 205 206void 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 */ 219class GlyphIDConverter { 220public: 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 { 227 // ensure space for one glyph per ID given UTF8 encoding. 228 storage.reset(new uint16_t[byteLength]); 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; 238private: 239 std::unique_ptr<uint16_t[]> storage; 240}; 241 242void 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 248 std::unique_ptr<SkPoint[]> pointStorage(new SkPoint[glyphs.count]); 249 std::unique_ptr<SkScalar[]> glyphWidths(new SkScalar[glyphs.count]); 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 275 int xBaseline = 0; 276 int yBaseline = 0; 277 if (mCanvas->drawTextAbsolutePos()) { 278 bounds.offset(x,y); 279 xBaseline = x; 280 yBaseline = y; 281 } 282 pointStorage[0].set(xBaseline, yBaseline); 283 284 // setup the remaining glyph positions 285 if (glyphs.paint.isVerticalText()) { 286 for (int i = 1; i < glyphs.count; i++) { 287 pointStorage[i].set(xBaseline, glyphWidths[i-1] + pointStorage[i-1].fY); 288 } 289 } else { 290 for (int i = 1; i < glyphs.count; i++) { 291 pointStorage[i].set(glyphWidths[i-1] + pointStorage[i-1].fX, yBaseline); 292 } 293 } 294 295 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats"); 296 mCanvas->drawGlyphs(glyphs.glyphIDs, &pointStorage[0].fX, glyphs.count, glyphs.paint, 297 x, y, bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0); 298} 299 300void 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; 308 std::unique_ptr<SkPoint[]> pointStorage; 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; 316 pointStorage.reset(new SkPoint[glyphs.count]); 317 for (int i = 0; i < glyphs.count; i++) { 318 pointStorage[i].fX = pos[i].fX - x; 319 pointStorage[i].fY = pos[i].fY - y; 320 } 321 posArray = pointStorage.get(); 322 } 323 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. 328 SkRect bounds = SkRect::MakeEmpty(); 329 for (int i = 0; i < glyphs.count; i++) { 330 SkRect glyphBounds = SkRect::MakeEmpty(); 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 } 335 336 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats"); 337 mCanvas->drawGlyphs(glyphs.glyphIDs, &posArray[0].fX, glyphs.count, glyphs.paint, x, y, 338 bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0); 339} 340 341void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], 342 SkScalar constY, const SkPaint& paint) { 343 const size_t pointCount = byteLength >> 1; 344 std::unique_ptr<SkPoint[]> pts(new SkPoint[pointCount]); 345 for (size_t i = 0; i < pointCount; i++) { 346 pts[i].set(xpos[i], constY); 347 } 348 this->onDrawPosText(text, byteLength, pts.get(), paint); 349} 350 351void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, 352 const SkMatrix* matrix, const SkPaint& origPaint) { 353 SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextOnPath is not supported"); 354} 355 356void 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 373void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, 374 const SkPaint& paint) { 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 } 411} 412 413void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], 414 const SkPoint texCoords[4], SkBlendMode bmode, const SkPaint& paint) { 415 if (mFilterHwuiCalls) { 416 return; 417 } 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, 428 data.fTexCoords, data.fColors, bmode, data.fIndices, data.fIndexCount, 429 paint); 430 } 431} 432 433void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle) { 434 mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op); 435} 436 437void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkClipOp op, ClipEdgeStyle) { 438 SkPath path; 439 path.addRRect(roundRect); 440 mCanvas->clipPath(&path, op); 441} 442 443void SkiaCanvasProxy::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle) { 444 mCanvas->clipPath(&path, op); 445} 446 447void SkiaCanvasProxy::onClipRegion(const SkRegion& region, SkClipOp op) { 448 mCanvas->clipRegion(®ion, op); 449} 450 451}; // namespace uirenderer 452}; // namespace android 453