2015-03-23 19:51:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkAndroidSDKCanvas.h"
|
|
|
|
|
|
|
|
#include "SkColorFilter.h"
|
2015-08-05 20:57:49 +00:00
|
|
|
#include "SkPaint.h"
|
2015-03-23 19:51:20 +00:00
|
|
|
#include "SkPathEffect.h"
|
|
|
|
#include "SkShader.h"
|
2015-08-05 20:57:49 +00:00
|
|
|
#include "SkTLazy.h"
|
2015-03-23 19:51:20 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/** Discard SkShaders not exposed by the Android Java API. */
|
|
|
|
|
|
|
|
void CheckShader(SkPaint* paint) {
|
|
|
|
SkShader* shader = paint->getShader();
|
|
|
|
if (!shader) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-19 18:46:38 +00:00
|
|
|
if (shader->isABitmap()) {
|
2015-03-23 19:51:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
if (shader->asACompose(nullptr)) {
|
2015-03-23 19:51:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
SkShader::GradientType gtype = shader->asAGradient(nullptr);
|
2015-03-23 19:51:20 +00:00
|
|
|
if (gtype == SkShader::kLinear_GradientType ||
|
|
|
|
gtype == SkShader::kRadial_GradientType ||
|
|
|
|
gtype == SkShader::kSweep_GradientType) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
paint->setShader(nullptr);
|
2015-03-23 19:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Filter(SkPaint* paint) {
|
|
|
|
|
|
|
|
uint32_t flags = paint->getFlags();
|
|
|
|
flags &= ~SkPaint::kLCDRenderText_Flag;
|
|
|
|
paint->setFlags(flags);
|
|
|
|
|
|
|
|
// Android doesn't support Xfermodes above kLighten_Mode
|
|
|
|
SkXfermode::Mode mode;
|
|
|
|
SkXfermode::AsMode(paint->getXfermode(), &mode);
|
|
|
|
if (mode > SkXfermode::kLighten_Mode) {
|
2015-08-27 14:41:13 +00:00
|
|
|
paint->setXfermode(nullptr);
|
2015-03-23 19:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Force bilinear scaling or none
|
|
|
|
if (paint->getFilterQuality() != kNone_SkFilterQuality) {
|
|
|
|
paint->setFilterQuality(kLow_SkFilterQuality);
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckShader(paint);
|
|
|
|
|
|
|
|
// Android SDK only supports mode & matrix color filters
|
|
|
|
// (and, again, no modes above kLighten_Mode).
|
|
|
|
SkColorFilter* cf = paint->getColorFilter();
|
|
|
|
if (cf) {
|
|
|
|
SkColor color;
|
|
|
|
SkXfermode::Mode mode;
|
|
|
|
SkScalar srcColorMatrix[20];
|
|
|
|
bool isMode = cf->asColorMode(&color, &mode);
|
|
|
|
if (isMode && mode > SkXfermode::kLighten_Mode) {
|
|
|
|
paint->setColorFilter(
|
|
|
|
SkColorFilter::CreateModeFilter(color, SkXfermode::kSrcOver_Mode));
|
|
|
|
} else if (!isMode && !cf->asColorMatrix(srcColorMatrix)) {
|
2015-08-27 14:41:13 +00:00
|
|
|
paint->setColorFilter(nullptr);
|
2015-03-23 19:51:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
|
|
|
|
SkPathEffect* pe = paint->getPathEffect();
|
|
|
|
if (pe && !pe->exposedInAndroidJavaAPI()) {
|
2015-08-27 14:41:13 +00:00
|
|
|
paint->setPathEffect(nullptr);
|
2015-03-23 19:51:20 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// TODO: Android doesn't support all the flags that can be passed to
|
|
|
|
// blur filters; we need plumbing to get them out.
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
paint->setImageFilter(nullptr);
|
|
|
|
paint->setLooper(nullptr);
|
2015-03-23 19:51:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#define FILTER(p) \
|
|
|
|
SkPaint filteredPaint(p); \
|
|
|
|
Filter(&filteredPaint);
|
|
|
|
|
|
|
|
#define FILTER_PTR(p) \
|
|
|
|
SkTLazy<SkPaint> lazyPaint; \
|
|
|
|
SkPaint* filteredPaint = (SkPaint*) p; \
|
|
|
|
if (p) { \
|
|
|
|
filteredPaint = lazyPaint.set(*p); \
|
|
|
|
Filter(filteredPaint); \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
SkAndroidSDKCanvas::SkAndroidSDKCanvas() : fProxyTarget(nullptr) { }
|
2015-03-23 19:51:20 +00:00
|
|
|
|
|
|
|
void SkAndroidSDKCanvas::reset(SkCanvas* newTarget) { fProxyTarget = newTarget; }
|
|
|
|
|
|
|
|
void SkAndroidSDKCanvas::onDrawPaint(const SkPaint& paint) {
|
|
|
|
FILTER(paint);
|
|
|
|
fProxyTarget->drawPaint(filteredPaint);
|
|
|
|
}
|
|
|
|
void SkAndroidSDKCanvas::onDrawPoints(PointMode pMode,
|
|
|
|
size_t count,
|
|
|
|
const SkPoint pts[],
|
|
|
|
const SkPaint& paint) {
|
|
|
|
FILTER(paint);
|
|
|
|
fProxyTarget->drawPoints(pMode, count, pts, filteredPaint);
|
|
|
|
}
|
|
|
|
void SkAndroidSDKCanvas::onDrawOval(const SkRect& r, const SkPaint& paint) {
|
|
|
|
FILTER(paint);
|
|
|
|
fProxyTarget->drawOval(r, filteredPaint);
|
|
|
|
}
|
|
|
|
void SkAndroidSDKCanvas::onDrawRect(const SkRect& r, const SkPaint& paint) {
|
|
|
|
FILTER(paint);
|
|
|
|
fProxyTarget->drawRect(r, filteredPaint);
|
|
|
|
}
|
|
|
|
void SkAndroidSDKCanvas::onDrawRRect(const SkRRect& r, const SkPaint& paint) {
|
|
|
|
FILTER(paint);
|
|
|
|
fProxyTarget->drawRRect(r, filteredPaint);
|
|
|
|
}
|
|
|
|
void SkAndroidSDKCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
|
|
|
|
FILTER(paint);
|
|
|
|
fProxyTarget->drawPath(path, filteredPaint);
|
|
|
|
}
|
|
|
|
void SkAndroidSDKCanvas::onDrawBitmap(const SkBitmap& bitmap,
|
|
|
|
SkScalar left,
|
|
|
|
SkScalar top,
|
|
|
|
const SkPaint* paint) {
|
|
|
|
FILTER_PTR(paint);
|
|
|
|
fProxyTarget->drawBitmap(bitmap, left, top, filteredPaint);
|
|
|
|
}
|
|
|
|
void SkAndroidSDKCanvas::onDrawBitmapRect(const SkBitmap& bitmap,
|
|
|
|
const SkRect* src,
|
|
|
|
const SkRect& dst,
|
|
|
|
const SkPaint* paint,
|
2015-07-31 21:29:26 +00:00
|
|
|
SkCanvas::SrcRectConstraint constraint) {
|
2015-03-23 19:51:20 +00:00
|
|
|
FILTER_PTR(paint);
|
2015-08-06 17:02:53 +00:00
|
|
|
fProxyTarget->legacy_drawBitmapRect(bitmap, src, dst, filteredPaint, constraint);
|
2015-03-23 19:51:20 +00:00
|
|
|
}
|
|
|
|
void SkAndroidSDKCanvas::onDrawBitmapNine(const SkBitmap& bitmap,
|
|
|
|
const SkIRect& center,
|
|
|
|
const SkRect& dst,
|
|
|
|
const SkPaint* paint) {
|
|
|
|
FILTER_PTR(paint);
|
|
|
|
fProxyTarget->drawBitmapNine(bitmap, center, dst, filteredPaint);
|
|
|
|
}
|
|
|
|
void SkAndroidSDKCanvas::onDrawVertices(VertexMode vMode,
|
|
|
|
int vertexCount,
|
|
|
|
const SkPoint vertices[],
|
|
|
|
const SkPoint texs[], const SkColor colors[], SkXfermode* xMode,
|
|
|
|
const uint16_t indices[], int indexCount,
|
|
|
|
const SkPaint& paint) {
|
|
|
|
FILTER(paint);
|
|
|
|
fProxyTarget->drawVertices(vMode, vertexCount, vertices, texs, colors,
|
|
|
|
xMode, indices, indexCount, filteredPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkAndroidSDKCanvas::onDrawDRRect(const SkRRect& outer,
|
|
|
|
const SkRRect& inner,
|
|
|
|
const SkPaint& paint) {
|
|
|
|
FILTER(paint);
|
|
|
|
fProxyTarget->drawDRRect(outer, inner, filteredPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkAndroidSDKCanvas::onDrawText(const void* text,
|
|
|
|
size_t byteLength,
|
|
|
|
SkScalar x,
|
|
|
|
SkScalar y,
|
|
|
|
const SkPaint& paint) {
|
|
|
|
FILTER(paint);
|
|
|
|
fProxyTarget->drawText(text, byteLength, x, y, filteredPaint);
|
|
|
|
}
|
|
|
|
void SkAndroidSDKCanvas::onDrawPosText(const void* text,
|
|
|
|
size_t byteLength,
|
|
|
|
const SkPoint pos[],
|
|
|
|
const SkPaint& paint) {
|
|
|
|
FILTER(paint);
|
|
|
|
fProxyTarget->drawPosText(text, byteLength, pos, filteredPaint);
|
|
|
|
}
|
|
|
|
void SkAndroidSDKCanvas::onDrawPosTextH(const void* text,
|
|
|
|
size_t byteLength,
|
|
|
|
const SkScalar xpos[],
|
|
|
|
SkScalar constY,
|
|
|
|
const SkPaint& paint) {
|
|
|
|
FILTER(paint);
|
|
|
|
fProxyTarget->drawPosTextH(text, byteLength, xpos, constY, filteredPaint);
|
|
|
|
}
|
|
|
|
void SkAndroidSDKCanvas::onDrawTextOnPath(const void* text,
|
|
|
|
size_t byteLength,
|
|
|
|
const SkPath& path,
|
|
|
|
const SkMatrix* matrix,
|
|
|
|
const SkPaint& paint) {
|
|
|
|
FILTER(paint);
|
|
|
|
fProxyTarget->drawTextOnPath(text, byteLength, path, matrix, filteredPaint);
|
|
|
|
}
|
|
|
|
void SkAndroidSDKCanvas::onDrawTextBlob(const SkTextBlob* blob,
|
|
|
|
SkScalar x,
|
|
|
|
SkScalar y,
|
|
|
|
const SkPaint& paint) {
|
|
|
|
FILTER(paint);
|
|
|
|
fProxyTarget->drawTextBlob(blob, x, y, filteredPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkAndroidSDKCanvas::onDrawPatch(const SkPoint cubics[12],
|
|
|
|
const SkColor colors[4],
|
|
|
|
const SkPoint texCoords[4],
|
|
|
|
SkXfermode* xmode,
|
|
|
|
const SkPaint& paint) {
|
|
|
|
FILTER(paint);
|
|
|
|
fProxyTarget->drawPatch(cubics, colors, texCoords, xmode, filteredPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SkAndroidSDKCanvas::onDrawImage(const SkImage* image,
|
|
|
|
SkScalar x,
|
|
|
|
SkScalar y,
|
|
|
|
const SkPaint* paint) {
|
|
|
|
FILTER_PTR(paint);
|
|
|
|
fProxyTarget->drawImage(image, x, y, filteredPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkAndroidSDKCanvas::onDrawImageRect(const SkImage* image,
|
2015-08-03 13:03:06 +00:00
|
|
|
const SkRect* in,
|
|
|
|
const SkRect& out,
|
2015-08-03 15:41:53 +00:00
|
|
|
const SkPaint* paint,
|
2015-08-03 13:03:06 +00:00
|
|
|
SrcRectConstraint constraint) {
|
2015-03-23 19:51:20 +00:00
|
|
|
FILTER_PTR(paint);
|
2015-08-06 17:02:53 +00:00
|
|
|
fProxyTarget->legacy_drawImageRect(image, in, out, filteredPaint, constraint);
|
2015-03-23 19:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkAndroidSDKCanvas::onDrawPicture(const SkPicture* picture,
|
2015-10-01 15:02:22 +00:00
|
|
|
const SkMatrix* matrix,
|
|
|
|
const SkPaint* paint) {
|
2015-03-23 19:51:20 +00:00
|
|
|
FILTER_PTR(paint);
|
|
|
|
fProxyTarget->drawPicture(picture, matrix, filteredPaint);
|
|
|
|
}
|
|
|
|
|
2015-10-01 15:02:22 +00:00
|
|
|
void SkAndroidSDKCanvas::onDrawAtlas(const SkImage* atlas,
|
|
|
|
const SkRSXform xform[],
|
|
|
|
const SkRect tex[],
|
|
|
|
const SkColor colors[],
|
|
|
|
int count,
|
|
|
|
SkXfermode::Mode mode,
|
|
|
|
const SkRect* cullRect,
|
|
|
|
const SkPaint* paint) {
|
|
|
|
FILTER_PTR(paint);
|
|
|
|
fProxyTarget->drawAtlas(atlas, xform, tex, colors, count, mode, cullRect,
|
|
|
|
filteredPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkAndroidSDKCanvas::onDrawImageNine(const SkImage* image,
|
|
|
|
const SkIRect& center,
|
|
|
|
const SkRect& dst,
|
|
|
|
const SkPaint* paint) {
|
|
|
|
FILTER_PTR(paint);
|
|
|
|
fProxyTarget->drawImageNine(image, center, dst, filteredPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-07 17:22:31 +00:00
|
|
|
void SkAndroidSDKCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
|
|
|
|
fProxyTarget->drawDrawable(drawable, matrix);
|
2015-03-23 19:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkISize SkAndroidSDKCanvas::getBaseLayerSize() const {
|
|
|
|
return fProxyTarget->getBaseLayerSize();
|
|
|
|
}
|
|
|
|
bool SkAndroidSDKCanvas::getClipBounds(SkRect* rect) const {
|
|
|
|
return fProxyTarget->getClipBounds(rect);
|
|
|
|
}
|
|
|
|
bool SkAndroidSDKCanvas::getClipDeviceBounds(SkIRect* rect) const {
|
|
|
|
return fProxyTarget->getClipDeviceBounds(rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkAndroidSDKCanvas::isClipEmpty() const { return fProxyTarget->isClipEmpty(); }
|
|
|
|
bool SkAndroidSDKCanvas::isClipRect() const { return fProxyTarget->isClipRect(); }
|
|
|
|
|
|
|
|
SkSurface* SkAndroidSDKCanvas::onNewSurface(const SkImageInfo& info,
|
|
|
|
const SkSurfaceProps& props) {
|
|
|
|
return fProxyTarget->newSurface(info, &props);
|
|
|
|
}
|
|
|
|
|
2015-05-26 21:12:24 +00:00
|
|
|
bool SkAndroidSDKCanvas::onPeekPixels(SkPixmap* pmap) {
|
|
|
|
SkASSERT(pmap);
|
|
|
|
SkImageInfo info;
|
|
|
|
size_t rowBytes;
|
|
|
|
const void* addr = fProxyTarget->peekPixels(&info, &rowBytes);
|
|
|
|
if (addr) {
|
|
|
|
pmap->reset(info, addr, rowBytes);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-03-23 19:51:20 +00:00
|
|
|
}
|
|
|
|
|
2015-05-26 21:12:24 +00:00
|
|
|
bool SkAndroidSDKCanvas::onAccessTopLayerPixels(SkPixmap* pmap) {
|
|
|
|
SkASSERT(pmap);
|
|
|
|
SkImageInfo info;
|
|
|
|
size_t rowBytes;
|
2015-08-27 14:41:13 +00:00
|
|
|
const void* addr = fProxyTarget->accessTopLayerPixels(&info, &rowBytes, nullptr);
|
2015-05-26 21:12:24 +00:00
|
|
|
if (addr) {
|
|
|
|
pmap->reset(info, addr, rowBytes);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-03-23 19:51:20 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 13:52:58 +00:00
|
|
|
void SkAndroidSDKCanvas::willSave() {
|
|
|
|
fProxyTarget->save();
|
|
|
|
}
|
|
|
|
|
2015-12-18 15:38:26 +00:00
|
|
|
SkCanvas::SaveLayerStrategy SkAndroidSDKCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
|
|
|
|
fProxyTarget->saveLayer(rec);
|
2015-04-08 13:52:58 +00:00
|
|
|
return SkCanvas::kNoLayer_SaveLayerStrategy;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkAndroidSDKCanvas::willRestore() {
|
|
|
|
fProxyTarget->restore();
|
|
|
|
}
|
|
|
|
|
2015-03-23 19:51:20 +00:00
|
|
|
void SkAndroidSDKCanvas::didRestore() { }
|
2015-04-08 13:52:58 +00:00
|
|
|
|
2015-03-23 19:51:20 +00:00
|
|
|
void SkAndroidSDKCanvas::didConcat(const SkMatrix& m) {
|
|
|
|
fProxyTarget->concat(m);
|
|
|
|
}
|
2015-04-08 13:52:58 +00:00
|
|
|
|
|
|
|
void SkAndroidSDKCanvas::didSetMatrix(const SkMatrix& m) {
|
|
|
|
fProxyTarget->setMatrix(m);
|
|
|
|
}
|
2015-03-23 19:51:20 +00:00
|
|
|
|
|
|
|
void SkAndroidSDKCanvas::onClipRect(const SkRect& rect,
|
|
|
|
SkRegion::Op op,
|
|
|
|
ClipEdgeStyle style) {
|
|
|
|
fProxyTarget->clipRect(rect, op, style);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkAndroidSDKCanvas::onClipRRect(const SkRRect& rrect,
|
|
|
|
SkRegion::Op op,
|
|
|
|
ClipEdgeStyle style) {
|
|
|
|
fProxyTarget->clipRRect(rrect, op, style);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkAndroidSDKCanvas::onClipPath(const SkPath& path,
|
|
|
|
SkRegion::Op op,
|
|
|
|
ClipEdgeStyle style) {
|
|
|
|
fProxyTarget->clipPath(path, op, style);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkAndroidSDKCanvas::onClipRegion(const SkRegion& region, SkRegion::Op op) {
|
|
|
|
fProxyTarget->clipRegion(region, op);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkAndroidSDKCanvas::onDiscard() { fProxyTarget->discard(); }
|
|
|
|
|
|
|
|
|