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