blob: 5e6d77474e5b7db4227d0338374d660fc873dbdc [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
48 SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(float)*2, SkPoint_is_no_longer_2_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) {
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,
128 const SkRect& dst, const SkPaint* paint, DrawBitmapRectFlags) {
129 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
141void SkiaCanvasProxy::onDrawSprite(const SkBitmap& bitmap, int left, int top,
142 const SkPaint* paint) {
Tom Hudson17c5adf2015-06-09 15:46:04 -0400143 // TODO: if bitmap is a subset, do we need to add pixelRefOrigin to src?
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500144 mCanvas->save(SkCanvas::kMatrixClip_SaveFlag);
Tom Hudsonac7b6d32015-06-30 11:26:13 -0400145 mCanvas->setLocalMatrix(SkMatrix::I());
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500146 mCanvas->drawBitmap(bitmap, left, top, paint);
147 mCanvas->restore();
148}
149
150void SkiaCanvasProxy::onDrawVertices(VertexMode mode, int vertexCount, const SkPoint vertices[],
151 const SkPoint texs[], const SkColor colors[], SkXfermode*, const uint16_t indices[],
152 int indexCount, const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500153 if (mFilterHwuiCalls) {
154 return;
155 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500156 // convert the SkPoints into floats
157 SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(float)*2, SkPoint_is_no_longer_2_floats);
158 const int floatCount = vertexCount << 1;
159 const float* vArray = &vertices[0].fX;
160 const float* tArray = (texs) ? &texs[0].fX : NULL;
161 const int* cArray = (colors) ? (int*)colors : NULL;
162 mCanvas->drawVertices(mode, floatCount, vArray, tArray, cArray, indices, indexCount, paint);
163}
164
165SkSurface* SkiaCanvasProxy::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) {
166 SkDEBUGFAIL("SkiaCanvasProxy::onNewSurface is not supported");
167 return NULL;
168}
169
170void SkiaCanvasProxy::willSave() {
171 mCanvas->save(SkCanvas::kMatrixClip_SaveFlag);
172}
173
174SkCanvas::SaveLayerStrategy SkiaCanvasProxy::willSaveLayer(const SkRect* rectPtr,
175 const SkPaint* paint, SaveFlags flags) {
176 SkRect rect;
177 if (rectPtr) {
178 rect = *rectPtr;
179 } else if(!mCanvas->getClipBounds(&rect)) {
180 rect = SkRect::MakeEmpty();
181 }
182 mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint, flags);
183 return SkCanvas::kNoLayer_SaveLayerStrategy;
184}
185
186void SkiaCanvasProxy::willRestore() {
187 mCanvas->restore();
188}
189
190void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) {
191 mCanvas->concat(matrix);
192}
193
194void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) {
Tom Hudsonac7b6d32015-06-30 11:26:13 -0400195 // SkCanvas setMatrix() is relative to the Canvas origin, but OpenGLRenderer's
196 // setMatrix() is relative to device origin; call setLocalMatrix() instead.
197 mCanvas->setLocalMatrix(matrix);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500198}
199
200void SkiaCanvasProxy::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
201 const SkPaint& paint) {
202 SkPath path;
203 path.addRRect(outer);
204 path.addRRect(inner);
205 path.setFillType(SkPath::kEvenOdd_FillType);
206 this->drawPath(path, paint);
207}
208
209/**
210 * Utility class that converts the incoming text & paint from the given encoding
211 * into glyphIDs.
212 */
213class GlyphIDConverter {
214public:
215 GlyphIDConverter(const void* text, size_t byteLength, const SkPaint& origPaint) {
216 paint = origPaint;
217 if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) {
218 glyphIDs = (uint16_t*)text;
219 count = byteLength >> 1;
220 } else {
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400221 // ensure space for one glyph per ID given UTF8 encoding.
222 storage.reset(new uint16_t[byteLength]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500223 glyphIDs = storage.get();
224 count = paint.textToGlyphs(text, byteLength, storage.get());
225 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
226 }
227 }
228
229 SkPaint paint;
230 uint16_t* glyphIDs;
231 int count;
232private:
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400233 std::unique_ptr<uint16_t[]> storage;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500234};
235
236void SkiaCanvasProxy::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
237 const SkPaint& origPaint) {
238 // convert to glyphIDs if necessary
239 GlyphIDConverter glyphs(text, byteLength, origPaint);
240
241 // compute the glyph positions
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400242 std::unique_ptr<SkPoint[]> pointStorage(new SkPoint[glyphs.count]);
243 std::unique_ptr<SkScalar[]> glyphWidths(new SkScalar[glyphs.count]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500244 glyphs.paint.getTextWidths(glyphs.glyphIDs, glyphs.count << 1, glyphWidths.get());
245
246 // compute conservative bounds
247 // NOTE: We could call the faster paint.getFontBounds for a less accurate,
248 // but even more conservative bounds if this is too slow.
249 SkRect bounds;
250 glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds);
251
252 // adjust for non-left alignment
253 if (glyphs.paint.getTextAlign() != SkPaint::kLeft_Align) {
254 SkScalar stop = 0;
255 for (int i = 0; i < glyphs.count; i++) {
256 stop += glyphWidths[i];
257 }
258 if (glyphs.paint.getTextAlign() == SkPaint::kCenter_Align) {
259 stop = SkScalarHalf(stop);
260 }
261 if (glyphs.paint.isVerticalText()) {
262 y -= stop;
263 } else {
264 x -= stop;
265 }
266 }
267
268 // setup the first glyph position and adjust bounds if needed
Tom Hudson806a6f02015-02-19 17:11:32 -0500269 int xBaseline = 0;
270 int yBaseline = 0;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500271 if (mCanvas->drawTextAbsolutePos()) {
272 bounds.offset(x,y);
Tom Hudson806a6f02015-02-19 17:11:32 -0500273 xBaseline = x;
274 yBaseline = y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500275 }
Tom Hudson806a6f02015-02-19 17:11:32 -0500276 pointStorage[0].set(xBaseline, yBaseline);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500277
278 // setup the remaining glyph positions
279 if (glyphs.paint.isVerticalText()) {
280 for (int i = 1; i < glyphs.count; i++) {
Tom Hudson806a6f02015-02-19 17:11:32 -0500281 pointStorage[i].set(xBaseline, glyphWidths[i-1] + pointStorage[i-1].fY);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500282 }
283 } else {
284 for (int i = 1; i < glyphs.count; i++) {
Tom Hudson806a6f02015-02-19 17:11:32 -0500285 pointStorage[i].set(glyphWidths[i-1] + pointStorage[i-1].fX, yBaseline);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500286 }
287 }
288
289 SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(float)*2, SkPoint_is_no_longer_2_floats);
290 mCanvas->drawText(glyphs.glyphIDs, &pointStorage[0].fX, glyphs.count, glyphs.paint,
291 x, y, bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
292}
293
294void SkiaCanvasProxy::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
295 const SkPaint& origPaint) {
296 // convert to glyphIDs if necessary
297 GlyphIDConverter glyphs(text, byteLength, origPaint);
298
299 // convert to relative positions if necessary
300 int x, y;
301 const SkPoint* posArray;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400302 std::unique_ptr<SkPoint[]> pointStorage;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500303 if (mCanvas->drawTextAbsolutePos()) {
304 x = 0;
305 y = 0;
306 posArray = pos;
307 } else {
308 x = pos[0].fX;
309 y = pos[0].fY;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400310 pointStorage.reset(new SkPoint[glyphs.count]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500311 for (int i = 0; i < glyphs.count; i++) {
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400312 pointStorage[i].fX = pos[i].fX - x;
313 pointStorage[i].fY = pos[i].fY - y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500314 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400315 posArray = pointStorage.get();
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500316 }
317
318 // compute conservative bounds
319 // NOTE: We could call the faster paint.getFontBounds for a less accurate,
320 // but even more conservative bounds if this is too slow.
321 SkRect bounds;
322 glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds);
Tom Hudson20c2b3e2015-04-15 13:54:32 -0400323 bounds.offset(x, y);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500324
325 SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(float)*2, SkPoint_is_no_longer_2_floats);
326 mCanvas->drawText(glyphs.glyphIDs, &posArray[0].fX, glyphs.count, glyphs.paint, x, y,
327 bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
328}
329
330void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
331 SkScalar constY, const SkPaint& paint) {
332 const size_t pointCount = byteLength >> 1;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400333 std::unique_ptr<SkPoint[]> pts(new SkPoint[pointCount]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500334 for (size_t i = 0; i < pointCount; i++) {
335 pts[i].set(xpos[i], constY);
336 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400337 this->onDrawPosText(text, byteLength, pts.get(), paint);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500338}
339
340void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
341 const SkMatrix* matrix, const SkPaint& origPaint) {
342 // convert to glyphIDs if necessary
343 GlyphIDConverter glyphs(text, byteLength, origPaint);
344 mCanvas->drawTextOnPath(glyphs.glyphIDs, glyphs.count, path, 0, 0, glyphs.paint);
345}
346
347void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
348 const SkPaint& paint) {
349 SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextBlob is not supported");
350}
351
352void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
353 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500354 if (mFilterHwuiCalls) {
355 return;
356 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500357 SkPatchUtils::VertexData data;
358
359 SkMatrix matrix;
360 mCanvas->getMatrix(&matrix);
361 SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &matrix);
362
363 // It automatically adjusts lodX and lodY in case it exceeds the number of indices.
364 // If it fails to generate the vertices, then we do not draw.
365 if (SkPatchUtils::getVertexData(&data, cubics, colors, texCoords, lod.width(), lod.height())) {
366 this->drawVertices(SkCanvas::kTriangles_VertexMode, data.fVertexCount, data.fPoints,
367 data.fTexCoords, data.fColors, xmode, data.fIndices, data.fIndexCount,
368 paint);
369 }
370}
371
372void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle) {
373 mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op);
374}
375
376void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkRegion::Op op, ClipEdgeStyle) {
377 SkPath path;
378 path.addRRect(roundRect);
379 mCanvas->clipPath(&path, op);
380}
381
382void SkiaCanvasProxy::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle) {
383 mCanvas->clipPath(&path, op);
384}
385
386void SkiaCanvasProxy::onClipRegion(const SkRegion& region, SkRegion::Op op) {
387 mCanvas->clipRegion(&region, op);
388}
389
390}; // namespace uirenderer
391}; // namespace android