blob: 976f775183371aa70536a2b752545cac14341c84 [file] [log] [blame]
Derek Sollenberger1db141f2014-12-16 08:37:20 -05001/*
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>
Ben Wagnera11ee3c2015-08-04 11:14:15 -040021#include <SkPaint.h>
22#include <SkPath.h>
Tom Hudson17c5adf2015-06-09 15:46:04 -040023#include <SkPixelRef.h>
Ben Wagnera11ee3c2015-08-04 11:14:15 -040024#include <SkRect.h>
25#include <SkRRect.h>
Derek Sollenberger1db141f2014-12-16 08:37:20 -050026
Ben Wagner6bbf68d2015-08-19 11:26:06 -040027#include <memory>
28
Derek Sollenberger1db141f2014-12-16 08:37:20 -050029namespace android {
30namespace uirenderer {
31
Tom Hudsonb1476ae2015-03-05 10:30:18 -050032SkiaCanvasProxy::SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls)
Derek Sollenberger1db141f2014-12-16 08:37:20 -050033 : INHERITED(canvas->width(), canvas->height())
Tom Hudsonb1476ae2015-03-05 10:30:18 -050034 , mCanvas(canvas)
35 , mFilterHwuiCalls(filterHwuiCalls) {}
Derek Sollenberger1db141f2014-12-16 08:37:20 -050036
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) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -050043 if (!pts || count == 0) {
44 return;
45 }
46
Derek Sollenberger1db141f2014-12-16 08:37:20 -050047 // convert the SkPoints into floats
Ben Wagnere3a40ea2015-08-19 15:49:37 -040048 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Derek Sollenberger1db141f2014-12-16 08:37:20 -050049 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) {
Tom Hudson17c5adf2015-06-09 15:46:04 -0400106 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 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500125}
126
127void SkiaCanvasProxy::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* srcPtr,
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400128 const SkRect& dst, const SkPaint* paint, SrcRectConstraint) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500129 SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(bitmap.width(), bitmap.height());
Tom Hudson17c5adf2015-06-09 15:46:04 -0400130 // TODO: if bitmap is a subset, do we need to add pixelRefOrigin to src?
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500131 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
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500141void 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) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500144 if (mFilterHwuiCalls) {
145 return;
146 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500147 // convert the SkPoints into floats
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400148 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500149 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(SkCanvas::kMatrixClip_SaveFlag);
163}
164
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500165SkCanvas::SaveLayerStrategy SkiaCanvasProxy::getSaveLayerStrategy(const SaveLayerRec& saveLayerRec) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500166 SkRect rect;
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500167 if (saveLayerRec.fBounds) {
168 rect = *saveLayerRec.fBounds;
169 } else if (!mCanvas->getClipBounds(&rect)) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500170 rect = SkRect::MakeEmpty();
171 }
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500172 mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, saveLayerRec.fPaint,
173 (SkCanvas::SaveFlags) SaveLayerFlagsToSaveFlags(saveLayerRec.fSaveLayerFlags));
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500174 return SkCanvas::kNoLayer_SaveLayerStrategy;
175}
176
177void SkiaCanvasProxy::willRestore() {
178 mCanvas->restore();
179}
180
181void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) {
182 mCanvas->concat(matrix);
183}
184
185void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) {
Chris Craik6daa13c2015-08-19 13:32:12 -0700186 mCanvas->setMatrix(matrix);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500187}
188
189void SkiaCanvasProxy::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
190 const SkPaint& paint) {
191 SkPath path;
192 path.addRRect(outer);
193 path.addRRect(inner);
194 path.setFillType(SkPath::kEvenOdd_FillType);
195 this->drawPath(path, paint);
196}
197
198/**
199 * Utility class that converts the incoming text & paint from the given encoding
200 * into glyphIDs.
201 */
202class GlyphIDConverter {
203public:
204 GlyphIDConverter(const void* text, size_t byteLength, const SkPaint& origPaint) {
205 paint = origPaint;
206 if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) {
207 glyphIDs = (uint16_t*)text;
208 count = byteLength >> 1;
209 } else {
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400210 // ensure space for one glyph per ID given UTF8 encoding.
211 storage.reset(new uint16_t[byteLength]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500212 glyphIDs = storage.get();
213 count = paint.textToGlyphs(text, byteLength, storage.get());
214 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
215 }
216 }
217
218 SkPaint paint;
219 uint16_t* glyphIDs;
220 int count;
221private:
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400222 std::unique_ptr<uint16_t[]> storage;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500223};
224
225void SkiaCanvasProxy::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
226 const SkPaint& origPaint) {
227 // convert to glyphIDs if necessary
228 GlyphIDConverter glyphs(text, byteLength, origPaint);
229
230 // compute the glyph positions
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400231 std::unique_ptr<SkPoint[]> pointStorage(new SkPoint[glyphs.count]);
232 std::unique_ptr<SkScalar[]> glyphWidths(new SkScalar[glyphs.count]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500233 glyphs.paint.getTextWidths(glyphs.glyphIDs, glyphs.count << 1, glyphWidths.get());
234
235 // compute conservative bounds
236 // NOTE: We could call the faster paint.getFontBounds for a less accurate,
237 // but even more conservative bounds if this is too slow.
238 SkRect bounds;
239 glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds);
240
241 // adjust for non-left alignment
242 if (glyphs.paint.getTextAlign() != SkPaint::kLeft_Align) {
243 SkScalar stop = 0;
244 for (int i = 0; i < glyphs.count; i++) {
245 stop += glyphWidths[i];
246 }
247 if (glyphs.paint.getTextAlign() == SkPaint::kCenter_Align) {
248 stop = SkScalarHalf(stop);
249 }
250 if (glyphs.paint.isVerticalText()) {
251 y -= stop;
252 } else {
253 x -= stop;
254 }
255 }
256
257 // setup the first glyph position and adjust bounds if needed
Tom Hudson806a6f02015-02-19 17:11:32 -0500258 int xBaseline = 0;
259 int yBaseline = 0;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500260 if (mCanvas->drawTextAbsolutePos()) {
261 bounds.offset(x,y);
Tom Hudson806a6f02015-02-19 17:11:32 -0500262 xBaseline = x;
263 yBaseline = y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500264 }
Tom Hudson806a6f02015-02-19 17:11:32 -0500265 pointStorage[0].set(xBaseline, yBaseline);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500266
267 // setup the remaining glyph positions
268 if (glyphs.paint.isVerticalText()) {
269 for (int i = 1; i < glyphs.count; i++) {
Tom Hudson806a6f02015-02-19 17:11:32 -0500270 pointStorage[i].set(xBaseline, glyphWidths[i-1] + pointStorage[i-1].fY);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500271 }
272 } else {
273 for (int i = 1; i < glyphs.count; i++) {
Tom Hudson806a6f02015-02-19 17:11:32 -0500274 pointStorage[i].set(glyphWidths[i-1] + pointStorage[i-1].fX, yBaseline);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500275 }
276 }
277
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400278 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500279 mCanvas->drawText(glyphs.glyphIDs, &pointStorage[0].fX, glyphs.count, glyphs.paint,
280 x, y, bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
281}
282
283void SkiaCanvasProxy::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
284 const SkPaint& origPaint) {
285 // convert to glyphIDs if necessary
286 GlyphIDConverter glyphs(text, byteLength, origPaint);
287
288 // convert to relative positions if necessary
289 int x, y;
290 const SkPoint* posArray;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400291 std::unique_ptr<SkPoint[]> pointStorage;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500292 if (mCanvas->drawTextAbsolutePos()) {
293 x = 0;
294 y = 0;
295 posArray = pos;
296 } else {
297 x = pos[0].fX;
298 y = pos[0].fY;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400299 pointStorage.reset(new SkPoint[glyphs.count]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500300 for (int i = 0; i < glyphs.count; i++) {
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400301 pointStorage[i].fX = pos[i].fX - x;
302 pointStorage[i].fY = pos[i].fY - y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500303 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400304 posArray = pointStorage.get();
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500305 }
306
307 // compute conservative bounds
308 // NOTE: We could call the faster paint.getFontBounds for a less accurate,
309 // but even more conservative bounds if this is too slow.
310 SkRect bounds;
311 glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds);
Tom Hudson20c2b3e2015-04-15 13:54:32 -0400312 bounds.offset(x, y);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500313
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400314 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500315 mCanvas->drawText(glyphs.glyphIDs, &posArray[0].fX, glyphs.count, glyphs.paint, x, y,
316 bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
317}
318
319void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
320 SkScalar constY, const SkPaint& paint) {
321 const size_t pointCount = byteLength >> 1;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400322 std::unique_ptr<SkPoint[]> pts(new SkPoint[pointCount]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500323 for (size_t i = 0; i < pointCount; i++) {
324 pts[i].set(xpos[i], constY);
325 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400326 this->onDrawPosText(text, byteLength, pts.get(), paint);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500327}
328
329void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
330 const SkMatrix* matrix, const SkPaint& origPaint) {
331 // convert to glyphIDs if necessary
332 GlyphIDConverter glyphs(text, byteLength, origPaint);
333 mCanvas->drawTextOnPath(glyphs.glyphIDs, glyphs.count, path, 0, 0, glyphs.paint);
334}
335
336void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
337 const SkPaint& paint) {
338 SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextBlob is not supported");
339}
340
341void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
342 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500343 if (mFilterHwuiCalls) {
344 return;
345 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500346 SkPatchUtils::VertexData data;
347
348 SkMatrix matrix;
349 mCanvas->getMatrix(&matrix);
350 SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &matrix);
351
352 // It automatically adjusts lodX and lodY in case it exceeds the number of indices.
353 // If it fails to generate the vertices, then we do not draw.
354 if (SkPatchUtils::getVertexData(&data, cubics, colors, texCoords, lod.width(), lod.height())) {
355 this->drawVertices(SkCanvas::kTriangles_VertexMode, data.fVertexCount, data.fPoints,
356 data.fTexCoords, data.fColors, xmode, data.fIndices, data.fIndexCount,
357 paint);
358 }
359}
360
361void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle) {
362 mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op);
363}
364
365void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkRegion::Op op, ClipEdgeStyle) {
366 SkPath path;
367 path.addRRect(roundRect);
368 mCanvas->clipPath(&path, op);
369}
370
371void SkiaCanvasProxy::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle) {
372 mCanvas->clipPath(&path, op);
373}
374
375void SkiaCanvasProxy::onClipRegion(const SkRegion& region, SkRegion::Op op) {
376 mCanvas->clipRegion(&region, op);
377}
378
379}; // namespace uirenderer
380}; // namespace android