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