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