2014-06-23 14:13:53 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Canvas.h"
|
2015-07-30 10:00:39 -04:00
|
|
|
#include "CanvasProperty.h"
|
|
|
|
#include "Layer.h"
|
|
|
|
#include "RenderNode.h"
|
2014-06-23 14:13:53 -04:00
|
|
|
|
2015-01-20 07:51:14 -08:00
|
|
|
#include <SkCanvas.h>
|
|
|
|
#include <SkClipStack.h>
|
2015-07-30 10:00:39 -04:00
|
|
|
#include <SkDrawable.h>
|
2015-01-20 07:51:14 -08:00
|
|
|
#include <SkDevice.h>
|
|
|
|
#include <SkDeque.h>
|
|
|
|
#include <SkDrawFilter.h>
|
|
|
|
#include <SkGraphics.h>
|
2015-07-30 10:00:39 -04:00
|
|
|
#include <SkImage.h>
|
2015-01-20 07:51:14 -08:00
|
|
|
#include <SkShader.h>
|
|
|
|
#include <SkTArray.h>
|
2015-12-21 10:43:01 -05:00
|
|
|
#include <SkTLazy.h>
|
2015-01-20 07:51:14 -08:00
|
|
|
#include <SkTemplates.h>
|
2014-06-23 14:13:53 -04:00
|
|
|
|
2016-02-04 22:17:11 +00:00
|
|
|
#include "VectorDrawable.h"
|
|
|
|
|
2015-08-07 12:13:48 -04:00
|
|
|
#include <memory>
|
|
|
|
|
2014-06-23 14:13:53 -04:00
|
|
|
namespace android {
|
|
|
|
|
|
|
|
// Holds an SkCanvas reference plus additional native data.
|
|
|
|
class SkiaCanvas : public Canvas {
|
|
|
|
public:
|
2015-04-22 09:04:45 -07:00
|
|
|
explicit SkiaCanvas(const SkBitmap& bitmap);
|
2014-06-23 14:13:53 -04:00
|
|
|
|
2014-12-17 11:30:31 -05:00
|
|
|
/**
|
|
|
|
* Create a new SkiaCanvas.
|
|
|
|
*
|
|
|
|
* @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must
|
|
|
|
* not be NULL. This constructor will ref() the SkCanvas, and unref()
|
|
|
|
* it in its destructor.
|
|
|
|
*/
|
|
|
|
explicit SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {
|
2014-06-23 14:13:53 -04:00
|
|
|
SkASSERT(canvas);
|
2014-12-17 11:30:31 -05:00
|
|
|
canvas->ref();
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2015-01-29 11:19:31 -05:00
|
|
|
virtual SkCanvas* asSkCanvas() override {
|
2014-06-23 14:13:53 -04:00
|
|
|
return mCanvas.get();
|
|
|
|
}
|
|
|
|
|
2015-07-30 10:00:39 -04:00
|
|
|
virtual void resetRecording(int width, int height) override {
|
|
|
|
LOG_ALWAYS_FATAL("SkiaCanvas cannot be reset as a recording canvas");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual uirenderer::DisplayList* finishRecording() override {
|
|
|
|
LOG_ALWAYS_FATAL("SkiaCanvas does not produce a DisplayList");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
virtual void insertReorderBarrier(bool enableReorder) override {
|
|
|
|
LOG_ALWAYS_FATAL("SkiaCanvas does not support reordering barriers");
|
|
|
|
}
|
|
|
|
|
2015-04-22 09:04:45 -07:00
|
|
|
virtual void setBitmap(const SkBitmap& bitmap) override;
|
2014-06-23 14:13:53 -04:00
|
|
|
|
2015-01-29 11:19:31 -05:00
|
|
|
virtual bool isOpaque() override;
|
|
|
|
virtual int width() override;
|
|
|
|
virtual int height() override;
|
2014-06-23 14:13:53 -04:00
|
|
|
|
2015-07-13 13:24:29 -04:00
|
|
|
virtual void setHighContrastText(bool highContrastText) override {
|
|
|
|
mHighContrastText = highContrastText;
|
|
|
|
}
|
|
|
|
virtual bool isHighContrastText() override { return mHighContrastText; }
|
|
|
|
|
2015-01-29 11:19:31 -05:00
|
|
|
virtual int getSaveCount() const override;
|
2015-12-21 10:43:01 -05:00
|
|
|
virtual int save(SaveFlags::Flags flags) override;
|
2015-01-29 11:19:31 -05:00
|
|
|
virtual void restore() override;
|
|
|
|
virtual void restoreToCount(int saveCount) override;
|
2014-06-23 14:13:53 -04:00
|
|
|
|
|
|
|
virtual int saveLayer(float left, float top, float right, float bottom,
|
2015-12-21 10:43:01 -05:00
|
|
|
const SkPaint* paint, SaveFlags::Flags flags) override;
|
2014-06-23 14:13:53 -04:00
|
|
|
virtual int saveLayerAlpha(float left, float top, float right, float bottom,
|
2015-12-21 10:43:01 -05:00
|
|
|
int alpha, SaveFlags::Flags flags) override;
|
2015-01-29 11:19:31 -05:00
|
|
|
|
|
|
|
virtual void getMatrix(SkMatrix* outMatrix) const override;
|
|
|
|
virtual void setMatrix(const SkMatrix& matrix) override;
|
|
|
|
virtual void concat(const SkMatrix& matrix) override;
|
|
|
|
virtual void rotate(float degrees) override;
|
|
|
|
virtual void scale(float sx, float sy) override;
|
|
|
|
virtual void skew(float sx, float sy) override;
|
|
|
|
virtual void translate(float dx, float dy) override;
|
|
|
|
|
|
|
|
virtual bool getClipBounds(SkRect* outRect) const override;
|
|
|
|
virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
|
|
|
|
virtual bool quickRejectPath(const SkPath& path) const override;
|
|
|
|
virtual bool clipRect(float left, float top, float right, float bottom,
|
|
|
|
SkRegion::Op op) override;
|
|
|
|
virtual bool clipPath(const SkPath* path, SkRegion::Op op) override;
|
|
|
|
virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) override;
|
|
|
|
|
|
|
|
virtual SkDrawFilter* getDrawFilter() override;
|
|
|
|
virtual void setDrawFilter(SkDrawFilter* drawFilter) override;
|
|
|
|
|
|
|
|
virtual void drawColor(int color, SkXfermode::Mode mode) override;
|
|
|
|
virtual void drawPaint(const SkPaint& paint) override;
|
|
|
|
|
|
|
|
virtual void drawPoint(float x, float y, const SkPaint& paint) override;
|
|
|
|
virtual void drawPoints(const float* points, int count, const SkPaint& paint) override;
|
2014-06-23 14:13:53 -04:00
|
|
|
virtual void drawLine(float startX, float startY, float stopX, float stopY,
|
2015-01-29 11:19:31 -05:00
|
|
|
const SkPaint& paint) override;
|
|
|
|
virtual void drawLines(const float* points, int count, const SkPaint& paint) override;
|
|
|
|
virtual void drawRect(float left, float top, float right, float bottom,
|
|
|
|
const SkPaint& paint) override;
|
2015-07-10 09:58:41 -04:00
|
|
|
virtual void drawRegion(const SkRegion& region, const SkPaint& paint) override;
|
2014-06-23 14:13:53 -04:00
|
|
|
virtual void drawRoundRect(float left, float top, float right, float bottom,
|
2015-01-29 11:19:31 -05:00
|
|
|
float rx, float ry, const SkPaint& paint) override;
|
|
|
|
virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override;
|
|
|
|
virtual void drawOval(float left, float top, float right, float bottom,
|
|
|
|
const SkPaint& paint) override;
|
2014-06-23 14:13:53 -04:00
|
|
|
virtual void drawArc(float left, float top, float right, float bottom,
|
2015-01-29 11:19:31 -05:00
|
|
|
float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override;
|
|
|
|
virtual void drawPath(const SkPath& path, const SkPaint& paint) override;
|
2014-06-23 14:13:53 -04:00
|
|
|
virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
|
|
|
|
const float* verts, const float* tex, const int* colors,
|
2015-01-29 11:19:31 -05:00
|
|
|
const uint16_t* indices, int indexCount, const SkPaint& paint) override;
|
2014-06-23 14:13:53 -04:00
|
|
|
|
2015-01-29 11:19:31 -05:00
|
|
|
virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
|
|
|
|
const SkPaint* paint) override;
|
|
|
|
virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
|
|
|
|
const SkPaint* paint) override;
|
2014-06-23 14:13:53 -04:00
|
|
|
virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
|
|
|
|
float srcRight, float srcBottom, float dstLeft, float dstTop,
|
2015-01-29 11:19:31 -05:00
|
|
|
float dstRight, float dstBottom, const SkPaint* paint) override;
|
2014-06-23 14:13:53 -04:00
|
|
|
virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
|
2015-01-29 11:19:31 -05:00
|
|
|
const float* vertices, const int* colors, const SkPaint* paint) override;
|
2015-07-10 13:56:39 -04:00
|
|
|
virtual void drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& chunk,
|
|
|
|
float dstLeft, float dstTop, float dstRight, float dstBottom,
|
|
|
|
const SkPaint* paint) override;
|
2014-06-23 14:13:53 -04:00
|
|
|
|
2014-07-21 15:22:10 -04:00
|
|
|
virtual void drawText(const uint16_t* text, const float* positions, int count,
|
|
|
|
const SkPaint& paint, float x, float y,
|
2014-12-09 15:03:44 -05:00
|
|
|
float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
|
2015-01-29 11:19:31 -05:00
|
|
|
float totalAdvance) override;
|
2014-06-23 14:13:53 -04:00
|
|
|
virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
|
2015-01-29 11:19:31 -05:00
|
|
|
float hOffset, float vOffset, const SkPaint& paint) override;
|
2014-07-21 15:22:10 -04:00
|
|
|
|
2015-01-29 11:19:31 -05:00
|
|
|
virtual bool drawTextAbsolutePos() const override { return true; }
|
2016-02-04 22:17:11 +00:00
|
|
|
virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override;
|
2014-06-23 14:13:53 -04:00
|
|
|
|
2015-07-30 10:00:39 -04:00
|
|
|
virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) override;
|
|
|
|
virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* y, uirenderer::CanvasPropertyPrimitive* radius,
|
|
|
|
uirenderer::CanvasPropertyPaint* paint) override;
|
|
|
|
|
|
|
|
virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override;
|
|
|
|
virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override;
|
|
|
|
virtual void callDrawGLFunction(Functor* functor) override;
|
|
|
|
|
2014-06-23 14:13:53 -04:00
|
|
|
private:
|
|
|
|
struct SaveRec {
|
2015-12-21 10:43:01 -05:00
|
|
|
int saveCount;
|
|
|
|
SaveFlags::Flags saveFlags;
|
2014-06-23 14:13:53 -04:00
|
|
|
};
|
|
|
|
|
2015-07-13 13:24:29 -04:00
|
|
|
bool mHighContrastText = false;
|
|
|
|
|
2015-12-21 10:43:01 -05:00
|
|
|
void recordPartialSave(SaveFlags::Flags flags);
|
2014-06-23 14:13:53 -04:00
|
|
|
void saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, int frameSaveCount);
|
|
|
|
void applyClips(const SkTArray<SkClipStack::Element>& clips);
|
|
|
|
|
2014-07-21 15:22:10 -04:00
|
|
|
void drawPoints(const float* points, int count, const SkPaint& paint,
|
2014-06-23 14:13:53 -04:00
|
|
|
SkCanvas::PointMode mode);
|
|
|
|
|
|
|
|
SkAutoTUnref<SkCanvas> mCanvas;
|
2015-08-07 12:13:48 -04:00
|
|
|
std::unique_ptr<SkDeque> mSaveStack; // lazily allocated, tracks partial saves.
|
2014-06-23 14:13:53 -04:00
|
|
|
};
|
|
|
|
|
2015-04-22 09:04:45 -07:00
|
|
|
Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
|
2014-06-23 14:13:53 -04:00
|
|
|
return new SkiaCanvas(bitmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
|
|
|
|
return new SkiaCanvas(skiaCanvas);
|
|
|
|
}
|
|
|
|
|
2015-04-22 09:04:45 -07:00
|
|
|
SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
|
|
|
|
mCanvas.reset(new SkCanvas(bitmap));
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas state operations: Replace Bitmap
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class ClipCopier : public SkCanvas::ClipVisitor {
|
|
|
|
public:
|
|
|
|
ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {}
|
|
|
|
|
|
|
|
virtual void clipRect(const SkRect& rect, SkRegion::Op op, bool antialias) {
|
|
|
|
m_dstCanvas->clipRect(rect, op, antialias);
|
|
|
|
}
|
|
|
|
virtual void clipRRect(const SkRRect& rrect, SkRegion::Op op, bool antialias) {
|
|
|
|
m_dstCanvas->clipRRect(rrect, op, antialias);
|
|
|
|
}
|
|
|
|
virtual void clipPath(const SkPath& path, SkRegion::Op op, bool antialias) {
|
|
|
|
m_dstCanvas->clipPath(path, op, antialias);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkCanvas* m_dstCanvas;
|
|
|
|
};
|
|
|
|
|
2015-04-22 09:04:45 -07:00
|
|
|
void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
|
|
|
|
SkCanvas* newCanvas = new SkCanvas(bitmap);
|
2014-06-23 14:13:53 -04:00
|
|
|
|
2015-04-22 09:04:45 -07:00
|
|
|
if (!bitmap.isNull()) {
|
2014-06-23 14:13:53 -04:00
|
|
|
// Copy the canvas matrix & clip state.
|
|
|
|
newCanvas->setMatrix(mCanvas->getTotalMatrix());
|
2015-07-31 10:38:40 -04:00
|
|
|
|
|
|
|
ClipCopier copier(newCanvas);
|
|
|
|
mCanvas->replayClips(&copier);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// unrefs the existing canvas
|
|
|
|
mCanvas.reset(newCanvas);
|
|
|
|
|
|
|
|
// clean up the old save stack
|
|
|
|
mSaveStack.reset(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas state operations
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool SkiaCanvas::isOpaque() {
|
2015-07-31 10:38:40 -04:00
|
|
|
return mCanvas->imageInfo().isOpaque();
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int SkiaCanvas::width() {
|
2015-07-31 10:38:40 -04:00
|
|
|
return mCanvas->imageInfo().width();
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int SkiaCanvas::height() {
|
2015-07-31 10:38:40 -04:00
|
|
|
return mCanvas->imageInfo().height();
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas state operations: Save (layer)
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
int SkiaCanvas::getSaveCount() const {
|
|
|
|
return mCanvas->getSaveCount();
|
|
|
|
}
|
|
|
|
|
2015-12-21 10:43:01 -05:00
|
|
|
int SkiaCanvas::save(SaveFlags::Flags flags) {
|
2014-06-23 14:13:53 -04:00
|
|
|
int count = mCanvas->save();
|
|
|
|
recordPartialSave(flags);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
Fix persistent Canvas clip handling
Partial Canvas save semantics (clip or matrix persisting after restore)
are currently emulated in the native canvas wrapper (SkiaCanvas.cpp).
Persistent clips (save w/ MATRIX_SAVE_FLAG only) in particular are
emulated using the SkCanvas clip stack. There are two problems with
the current implementation:
1) The canvas save count is used to identify the clip stack topmost
frame, on the assumption that it is the same as the actual clip stack
save count. But with the introduction of lazy SkCanvas saves in Skia,
the two values can diverge: the clip stack save count only reflects
*committed* saves, while the canvas save count includes both committed
and pending saves. This means that we can no longer compare canvas and
clip stack save counts directly.
While we're looking at addressing the save count discrepancy in Skia
proper, we can also refactor the partial save emulation to no longer
rely on the two values being synchronized: instead of using the canvas
save count to locate the top clip stack frame, simply use the clip
stack save count for the same purpose - getClipStack()->getSaveCount()
always points to the correct top clip stack frame.
With this patch:
* we use SkCanvas::getSaveCount() to track *canvas* save frames which
require persistent matrix/clip handling (mSaveRecStack)
* we use SkClipStack::getSaveCount() to extract the clips from the
top clip stack frame
Also, since we're no longer mixing/comparing the two save counts, we
don't have to decrement the canvas value anymore (to make it zero-based
like its clip stack counterpart).
2) When iterating over clip stack elements, we currently start at
kTopIterStart and advance using next(). This is incorrect as next()
moves up the stack, so we only iterate over the topmost element =>
if there are multiple (non-consolidated) clip elements in the top
frame, we only get to see one.
We need to iterate using prev() instead.
Change-Id: Ic2d8cad684018925e749b9172fbf7c6202d9fb62
2015-11-04 14:36:02 -05:00
|
|
|
// The SkiaCanvas::restore operation layers on the capability to preserve
|
|
|
|
// either (or both) the matrix and/or clip state after a SkCanvas::restore
|
|
|
|
// operation. It does this by explicitly saving off the clip & matrix state
|
|
|
|
// when requested and playing it back after the SkCanvas::restore.
|
2014-06-23 14:13:53 -04:00
|
|
|
void SkiaCanvas::restore() {
|
|
|
|
const SaveRec* rec = (NULL == mSaveStack.get())
|
|
|
|
? NULL
|
|
|
|
: static_cast<SaveRec*>(mSaveStack->back());
|
Fix persistent Canvas clip handling
Partial Canvas save semantics (clip or matrix persisting after restore)
are currently emulated in the native canvas wrapper (SkiaCanvas.cpp).
Persistent clips (save w/ MATRIX_SAVE_FLAG only) in particular are
emulated using the SkCanvas clip stack. There are two problems with
the current implementation:
1) The canvas save count is used to identify the clip stack topmost
frame, on the assumption that it is the same as the actual clip stack
save count. But with the introduction of lazy SkCanvas saves in Skia,
the two values can diverge: the clip stack save count only reflects
*committed* saves, while the canvas save count includes both committed
and pending saves. This means that we can no longer compare canvas and
clip stack save counts directly.
While we're looking at addressing the save count discrepancy in Skia
proper, we can also refactor the partial save emulation to no longer
rely on the two values being synchronized: instead of using the canvas
save count to locate the top clip stack frame, simply use the clip
stack save count for the same purpose - getClipStack()->getSaveCount()
always points to the correct top clip stack frame.
With this patch:
* we use SkCanvas::getSaveCount() to track *canvas* save frames which
require persistent matrix/clip handling (mSaveRecStack)
* we use SkClipStack::getSaveCount() to extract the clips from the
top clip stack frame
Also, since we're no longer mixing/comparing the two save counts, we
don't have to decrement the canvas value anymore (to make it zero-based
like its clip stack counterpart).
2) When iterating over clip stack elements, we currently start at
kTopIterStart and advance using next(). This is incorrect as next()
moves up the stack, so we only iterate over the topmost element =>
if there are multiple (non-consolidated) clip elements in the top
frame, we only get to see one.
We need to iterate using prev() instead.
Change-Id: Ic2d8cad684018925e749b9172fbf7c6202d9fb62
2015-11-04 14:36:02 -05:00
|
|
|
int currentSaveCount = mCanvas->getSaveCount();
|
2014-06-23 14:13:53 -04:00
|
|
|
SkASSERT(NULL == rec || currentSaveCount >= rec->saveCount);
|
|
|
|
|
|
|
|
if (NULL == rec || rec->saveCount != currentSaveCount) {
|
|
|
|
// Fast path - no record for this frame.
|
|
|
|
mCanvas->restore();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-21 10:43:01 -05:00
|
|
|
bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
|
|
|
|
bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
|
2014-06-23 14:13:53 -04:00
|
|
|
|
|
|
|
SkMatrix savedMatrix;
|
|
|
|
if (preserveMatrix) {
|
|
|
|
savedMatrix = mCanvas->getTotalMatrix();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkTArray<SkClipStack::Element> savedClips;
|
Fix persistent Canvas clip handling
Partial Canvas save semantics (clip or matrix persisting after restore)
are currently emulated in the native canvas wrapper (SkiaCanvas.cpp).
Persistent clips (save w/ MATRIX_SAVE_FLAG only) in particular are
emulated using the SkCanvas clip stack. There are two problems with
the current implementation:
1) The canvas save count is used to identify the clip stack topmost
frame, on the assumption that it is the same as the actual clip stack
save count. But with the introduction of lazy SkCanvas saves in Skia,
the two values can diverge: the clip stack save count only reflects
*committed* saves, while the canvas save count includes both committed
and pending saves. This means that we can no longer compare canvas and
clip stack save counts directly.
While we're looking at addressing the save count discrepancy in Skia
proper, we can also refactor the partial save emulation to no longer
rely on the two values being synchronized: instead of using the canvas
save count to locate the top clip stack frame, simply use the clip
stack save count for the same purpose - getClipStack()->getSaveCount()
always points to the correct top clip stack frame.
With this patch:
* we use SkCanvas::getSaveCount() to track *canvas* save frames which
require persistent matrix/clip handling (mSaveRecStack)
* we use SkClipStack::getSaveCount() to extract the clips from the
top clip stack frame
Also, since we're no longer mixing/comparing the two save counts, we
don't have to decrement the canvas value anymore (to make it zero-based
like its clip stack counterpart).
2) When iterating over clip stack elements, we currently start at
kTopIterStart and advance using next(). This is incorrect as next()
moves up the stack, so we only iterate over the topmost element =>
if there are multiple (non-consolidated) clip elements in the top
frame, we only get to see one.
We need to iterate using prev() instead.
Change-Id: Ic2d8cad684018925e749b9172fbf7c6202d9fb62
2015-11-04 14:36:02 -05:00
|
|
|
int topClipStackFrame = mCanvas->getClipStack()->getSaveCount();
|
2014-06-23 14:13:53 -04:00
|
|
|
if (preserveClip) {
|
Fix persistent Canvas clip handling
Partial Canvas save semantics (clip or matrix persisting after restore)
are currently emulated in the native canvas wrapper (SkiaCanvas.cpp).
Persistent clips (save w/ MATRIX_SAVE_FLAG only) in particular are
emulated using the SkCanvas clip stack. There are two problems with
the current implementation:
1) The canvas save count is used to identify the clip stack topmost
frame, on the assumption that it is the same as the actual clip stack
save count. But with the introduction of lazy SkCanvas saves in Skia,
the two values can diverge: the clip stack save count only reflects
*committed* saves, while the canvas save count includes both committed
and pending saves. This means that we can no longer compare canvas and
clip stack save counts directly.
While we're looking at addressing the save count discrepancy in Skia
proper, we can also refactor the partial save emulation to no longer
rely on the two values being synchronized: instead of using the canvas
save count to locate the top clip stack frame, simply use the clip
stack save count for the same purpose - getClipStack()->getSaveCount()
always points to the correct top clip stack frame.
With this patch:
* we use SkCanvas::getSaveCount() to track *canvas* save frames which
require persistent matrix/clip handling (mSaveRecStack)
* we use SkClipStack::getSaveCount() to extract the clips from the
top clip stack frame
Also, since we're no longer mixing/comparing the two save counts, we
don't have to decrement the canvas value anymore (to make it zero-based
like its clip stack counterpart).
2) When iterating over clip stack elements, we currently start at
kTopIterStart and advance using next(). This is incorrect as next()
moves up the stack, so we only iterate over the topmost element =>
if there are multiple (non-consolidated) clip elements in the top
frame, we only get to see one.
We need to iterate using prev() instead.
Change-Id: Ic2d8cad684018925e749b9172fbf7c6202d9fb62
2015-11-04 14:36:02 -05:00
|
|
|
saveClipsForFrame(savedClips, topClipStackFrame);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mCanvas->restore();
|
|
|
|
|
|
|
|
if (preserveMatrix) {
|
|
|
|
mCanvas->setMatrix(savedMatrix);
|
|
|
|
}
|
|
|
|
|
Fix persistent Canvas clip handling
Partial Canvas save semantics (clip or matrix persisting after restore)
are currently emulated in the native canvas wrapper (SkiaCanvas.cpp).
Persistent clips (save w/ MATRIX_SAVE_FLAG only) in particular are
emulated using the SkCanvas clip stack. There are two problems with
the current implementation:
1) The canvas save count is used to identify the clip stack topmost
frame, on the assumption that it is the same as the actual clip stack
save count. But with the introduction of lazy SkCanvas saves in Skia,
the two values can diverge: the clip stack save count only reflects
*committed* saves, while the canvas save count includes both committed
and pending saves. This means that we can no longer compare canvas and
clip stack save counts directly.
While we're looking at addressing the save count discrepancy in Skia
proper, we can also refactor the partial save emulation to no longer
rely on the two values being synchronized: instead of using the canvas
save count to locate the top clip stack frame, simply use the clip
stack save count for the same purpose - getClipStack()->getSaveCount()
always points to the correct top clip stack frame.
With this patch:
* we use SkCanvas::getSaveCount() to track *canvas* save frames which
require persistent matrix/clip handling (mSaveRecStack)
* we use SkClipStack::getSaveCount() to extract the clips from the
top clip stack frame
Also, since we're no longer mixing/comparing the two save counts, we
don't have to decrement the canvas value anymore (to make it zero-based
like its clip stack counterpart).
2) When iterating over clip stack elements, we currently start at
kTopIterStart and advance using next(). This is incorrect as next()
moves up the stack, so we only iterate over the topmost element =>
if there are multiple (non-consolidated) clip elements in the top
frame, we only get to see one.
We need to iterate using prev() instead.
Change-Id: Ic2d8cad684018925e749b9172fbf7c6202d9fb62
2015-11-04 14:36:02 -05:00
|
|
|
if (preserveClip && !savedClips.empty() &&
|
|
|
|
topClipStackFrame != mCanvas->getClipStack()->getSaveCount()) {
|
|
|
|
// Only reapply the saved clips if the top clip stack frame was actually
|
|
|
|
// popped by restore(). If it wasn't, it means it doesn't belong to the
|
|
|
|
// restored canvas frame (SkCanvas lazy save/restore kicked in).
|
2014-06-23 14:13:53 -04:00
|
|
|
applyClips(savedClips);
|
|
|
|
}
|
|
|
|
|
|
|
|
mSaveStack->pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkiaCanvas::restoreToCount(int restoreCount) {
|
|
|
|
while (mCanvas->getSaveCount() > restoreCount) {
|
|
|
|
this->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-21 10:43:01 -05:00
|
|
|
static inline SkCanvas::SaveLayerFlags layerFlags(SaveFlags::Flags flags) {
|
|
|
|
SkCanvas::SaveLayerFlags layerFlags = 0;
|
|
|
|
|
|
|
|
if (!(flags & SaveFlags::HasAlphaLayer)) {
|
|
|
|
layerFlags |= SkCanvas::kIsOpaque_SaveLayerFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(flags & SaveFlags::ClipToLayer)) {
|
|
|
|
layerFlags |= SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
return layerFlags;
|
|
|
|
}
|
|
|
|
|
2014-06-23 14:13:53 -04:00
|
|
|
int SkiaCanvas::saveLayer(float left, float top, float right, float bottom,
|
2015-12-21 10:43:01 -05:00
|
|
|
const SkPaint* paint, SaveFlags::Flags flags) {
|
|
|
|
const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
|
|
|
|
const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags));
|
|
|
|
|
|
|
|
int count = mCanvas->saveLayer(rec);
|
2014-06-23 14:13:53 -04:00
|
|
|
recordPartialSave(flags);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom,
|
2015-12-21 10:43:01 -05:00
|
|
|
int alpha, SaveFlags::Flags flags) {
|
|
|
|
SkTLazy<SkPaint> alphaPaint;
|
|
|
|
if (static_cast<unsigned>(alpha) < 0xFF) {
|
|
|
|
alphaPaint.init()->setAlpha(alpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->saveLayer(left, top, right, bottom, alphaPaint.getMaybeNull(),
|
|
|
|
flags);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-12-21 10:43:01 -05:00
|
|
|
void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
|
2014-06-23 14:13:53 -04:00
|
|
|
// A partial save is a save operation which doesn't capture the full canvas state.
|
2015-12-21 10:43:01 -05:00
|
|
|
// (either SaveFlags::Matrix or SaveFlags::Clip is missing).
|
2014-06-23 14:13:53 -04:00
|
|
|
|
|
|
|
// Mask-out non canvas state bits.
|
2015-12-21 10:43:01 -05:00
|
|
|
flags &= SaveFlags::MatrixClip;
|
2014-06-23 14:13:53 -04:00
|
|
|
|
2015-12-21 10:43:01 -05:00
|
|
|
if (flags == SaveFlags::MatrixClip) {
|
2014-06-23 14:13:53 -04:00
|
|
|
// not a partial save.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL == mSaveStack.get()) {
|
2015-08-19 12:45:09 -04:00
|
|
|
mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
|
Fix persistent Canvas clip handling
Partial Canvas save semantics (clip or matrix persisting after restore)
are currently emulated in the native canvas wrapper (SkiaCanvas.cpp).
Persistent clips (save w/ MATRIX_SAVE_FLAG only) in particular are
emulated using the SkCanvas clip stack. There are two problems with
the current implementation:
1) The canvas save count is used to identify the clip stack topmost
frame, on the assumption that it is the same as the actual clip stack
save count. But with the introduction of lazy SkCanvas saves in Skia,
the two values can diverge: the clip stack save count only reflects
*committed* saves, while the canvas save count includes both committed
and pending saves. This means that we can no longer compare canvas and
clip stack save counts directly.
While we're looking at addressing the save count discrepancy in Skia
proper, we can also refactor the partial save emulation to no longer
rely on the two values being synchronized: instead of using the canvas
save count to locate the top clip stack frame, simply use the clip
stack save count for the same purpose - getClipStack()->getSaveCount()
always points to the correct top clip stack frame.
With this patch:
* we use SkCanvas::getSaveCount() to track *canvas* save frames which
require persistent matrix/clip handling (mSaveRecStack)
* we use SkClipStack::getSaveCount() to extract the clips from the
top clip stack frame
Also, since we're no longer mixing/comparing the two save counts, we
don't have to decrement the canvas value anymore (to make it zero-based
like its clip stack counterpart).
2) When iterating over clip stack elements, we currently start at
kTopIterStart and advance using next(). This is incorrect as next()
moves up the stack, so we only iterate over the topmost element =>
if there are multiple (non-consolidated) clip elements in the top
frame, we only get to see one.
We need to iterate using prev() instead.
Change-Id: Ic2d8cad684018925e749b9172fbf7c6202d9fb62
2015-11-04 14:36:02 -05:00
|
|
|
rec->saveCount = mCanvas->getSaveCount();
|
2014-06-23 14:13:53 -04:00
|
|
|
rec->saveFlags = flags;
|
|
|
|
}
|
|
|
|
|
Fix persistent Canvas clip handling
Partial Canvas save semantics (clip or matrix persisting after restore)
are currently emulated in the native canvas wrapper (SkiaCanvas.cpp).
Persistent clips (save w/ MATRIX_SAVE_FLAG only) in particular are
emulated using the SkCanvas clip stack. There are two problems with
the current implementation:
1) The canvas save count is used to identify the clip stack topmost
frame, on the assumption that it is the same as the actual clip stack
save count. But with the introduction of lazy SkCanvas saves in Skia,
the two values can diverge: the clip stack save count only reflects
*committed* saves, while the canvas save count includes both committed
and pending saves. This means that we can no longer compare canvas and
clip stack save counts directly.
While we're looking at addressing the save count discrepancy in Skia
proper, we can also refactor the partial save emulation to no longer
rely on the two values being synchronized: instead of using the canvas
save count to locate the top clip stack frame, simply use the clip
stack save count for the same purpose - getClipStack()->getSaveCount()
always points to the correct top clip stack frame.
With this patch:
* we use SkCanvas::getSaveCount() to track *canvas* save frames which
require persistent matrix/clip handling (mSaveRecStack)
* we use SkClipStack::getSaveCount() to extract the clips from the
top clip stack frame
Also, since we're no longer mixing/comparing the two save counts, we
don't have to decrement the canvas value anymore (to make it zero-based
like its clip stack counterpart).
2) When iterating over clip stack elements, we currently start at
kTopIterStart and advance using next(). This is incorrect as next()
moves up the stack, so we only iterate over the topmost element =>
if there are multiple (non-consolidated) clip elements in the top
frame, we only get to see one.
We need to iterate using prev() instead.
Change-Id: Ic2d8cad684018925e749b9172fbf7c6202d9fb62
2015-11-04 14:36:02 -05:00
|
|
|
void SkiaCanvas::saveClipsForFrame(SkTArray<SkClipStack::Element>& clips,
|
|
|
|
int saveCountToBackup) {
|
|
|
|
// Each SkClipStack::Element stores the index of the canvas save
|
|
|
|
// with which it is associated. Backup only those Elements that
|
|
|
|
// are associated with 'saveCountToBackup'
|
2014-06-23 14:13:53 -04:00
|
|
|
SkClipStack::Iter clipIterator(*mCanvas->getClipStack(),
|
|
|
|
SkClipStack::Iter::kTop_IterStart);
|
Fix persistent Canvas clip handling
Partial Canvas save semantics (clip or matrix persisting after restore)
are currently emulated in the native canvas wrapper (SkiaCanvas.cpp).
Persistent clips (save w/ MATRIX_SAVE_FLAG only) in particular are
emulated using the SkCanvas clip stack. There are two problems with
the current implementation:
1) The canvas save count is used to identify the clip stack topmost
frame, on the assumption that it is the same as the actual clip stack
save count. But with the introduction of lazy SkCanvas saves in Skia,
the two values can diverge: the clip stack save count only reflects
*committed* saves, while the canvas save count includes both committed
and pending saves. This means that we can no longer compare canvas and
clip stack save counts directly.
While we're looking at addressing the save count discrepancy in Skia
proper, we can also refactor the partial save emulation to no longer
rely on the two values being synchronized: instead of using the canvas
save count to locate the top clip stack frame, simply use the clip
stack save count for the same purpose - getClipStack()->getSaveCount()
always points to the correct top clip stack frame.
With this patch:
* we use SkCanvas::getSaveCount() to track *canvas* save frames which
require persistent matrix/clip handling (mSaveRecStack)
* we use SkClipStack::getSaveCount() to extract the clips from the
top clip stack frame
Also, since we're no longer mixing/comparing the two save counts, we
don't have to decrement the canvas value anymore (to make it zero-based
like its clip stack counterpart).
2) When iterating over clip stack elements, we currently start at
kTopIterStart and advance using next(). This is incorrect as next()
moves up the stack, so we only iterate over the topmost element =>
if there are multiple (non-consolidated) clip elements in the top
frame, we only get to see one.
We need to iterate using prev() instead.
Change-Id: Ic2d8cad684018925e749b9172fbf7c6202d9fb62
2015-11-04 14:36:02 -05:00
|
|
|
while (const SkClipStack::Element* elem = clipIterator.prev()) {
|
|
|
|
if (elem->getSaveCount() < saveCountToBackup) {
|
|
|
|
// done with the target save count.
|
2014-06-23 14:13:53 -04:00
|
|
|
break;
|
|
|
|
}
|
Fix persistent Canvas clip handling
Partial Canvas save semantics (clip or matrix persisting after restore)
are currently emulated in the native canvas wrapper (SkiaCanvas.cpp).
Persistent clips (save w/ MATRIX_SAVE_FLAG only) in particular are
emulated using the SkCanvas clip stack. There are two problems with
the current implementation:
1) The canvas save count is used to identify the clip stack topmost
frame, on the assumption that it is the same as the actual clip stack
save count. But with the introduction of lazy SkCanvas saves in Skia,
the two values can diverge: the clip stack save count only reflects
*committed* saves, while the canvas save count includes both committed
and pending saves. This means that we can no longer compare canvas and
clip stack save counts directly.
While we're looking at addressing the save count discrepancy in Skia
proper, we can also refactor the partial save emulation to no longer
rely on the two values being synchronized: instead of using the canvas
save count to locate the top clip stack frame, simply use the clip
stack save count for the same purpose - getClipStack()->getSaveCount()
always points to the correct top clip stack frame.
With this patch:
* we use SkCanvas::getSaveCount() to track *canvas* save frames which
require persistent matrix/clip handling (mSaveRecStack)
* we use SkClipStack::getSaveCount() to extract the clips from the
top clip stack frame
Also, since we're no longer mixing/comparing the two save counts, we
don't have to decrement the canvas value anymore (to make it zero-based
like its clip stack counterpart).
2) When iterating over clip stack elements, we currently start at
kTopIterStart and advance using next(). This is incorrect as next()
moves up the stack, so we only iterate over the topmost element =>
if there are multiple (non-consolidated) clip elements in the top
frame, we only get to see one.
We need to iterate using prev() instead.
Change-Id: Ic2d8cad684018925e749b9172fbf7c6202d9fb62
2015-11-04 14:36:02 -05:00
|
|
|
SkASSERT(elem->getSaveCount() == saveCountToBackup);
|
2014-06-23 14:13:53 -04:00
|
|
|
clips.push_back(*elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkiaCanvas::applyClips(const SkTArray<SkClipStack::Element>& clips) {
|
|
|
|
ClipCopier clipCopier(mCanvas);
|
|
|
|
|
|
|
|
// The clip stack stores clips in device space.
|
|
|
|
SkMatrix origMatrix = mCanvas->getTotalMatrix();
|
|
|
|
mCanvas->resetMatrix();
|
|
|
|
|
|
|
|
// We pushed the clips in reverse order.
|
|
|
|
for (int i = clips.count() - 1; i >= 0; --i) {
|
|
|
|
clips[i].replay(&clipCopier);
|
|
|
|
}
|
|
|
|
|
|
|
|
mCanvas->setMatrix(origMatrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas state operations: Matrix
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
|
|
|
|
*outMatrix = mCanvas->getTotalMatrix();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
|
|
|
|
mCanvas->setMatrix(matrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkiaCanvas::concat(const SkMatrix& matrix) {
|
|
|
|
mCanvas->concat(matrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkiaCanvas::rotate(float degrees) {
|
|
|
|
mCanvas->rotate(degrees);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkiaCanvas::scale(float sx, float sy) {
|
|
|
|
mCanvas->scale(sx, sy);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkiaCanvas::skew(float sx, float sy) {
|
|
|
|
mCanvas->skew(sx, sy);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkiaCanvas::translate(float dx, float dy) {
|
|
|
|
mCanvas->translate(dx, dy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas state operations: Clips
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// This function is a mirror of SkCanvas::getClipBounds except that it does
|
|
|
|
// not outset the edge of the clip to account for anti-aliasing. There is
|
|
|
|
// a skia bug to investigate pushing this logic into back into skia.
|
|
|
|
// (see https://code.google.com/p/skia/issues/detail?id=1303)
|
|
|
|
bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
|
|
|
|
SkIRect ibounds;
|
|
|
|
if (!mCanvas->getClipDeviceBounds(&ibounds)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkMatrix inverse;
|
|
|
|
// if we can't invert the CTM, we can't return local clip bounds
|
|
|
|
if (!mCanvas->getTotalMatrix().invert(&inverse)) {
|
|
|
|
if (outRect) {
|
|
|
|
outRect->setEmpty();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL != outRect) {
|
|
|
|
SkRect r = SkRect::Make(ibounds);
|
|
|
|
inverse.mapRect(outRect, r);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
|
|
|
|
SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
|
|
|
|
return mCanvas->quickReject(bounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
|
|
|
|
return mCanvas->quickReject(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
|
|
|
|
SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
|
|
|
|
mCanvas->clipRect(rect, op);
|
2015-06-23 15:42:12 -07:00
|
|
|
return !mCanvas->isClipEmpty();
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SkiaCanvas::clipPath(const SkPath* path, SkRegion::Op op) {
|
|
|
|
mCanvas->clipPath(*path, op);
|
2015-06-23 15:42:12 -07:00
|
|
|
return !mCanvas->isClipEmpty();
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SkiaCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) {
|
|
|
|
SkPath rgnPath;
|
|
|
|
if (region->getBoundaryPath(&rgnPath)) {
|
|
|
|
// The region is specified in device space.
|
|
|
|
SkMatrix savedMatrix = mCanvas->getTotalMatrix();
|
|
|
|
mCanvas->resetMatrix();
|
|
|
|
mCanvas->clipPath(rgnPath, op);
|
|
|
|
mCanvas->setMatrix(savedMatrix);
|
|
|
|
} else {
|
|
|
|
mCanvas->clipRect(SkRect::MakeEmpty(), op);
|
|
|
|
}
|
2015-06-23 15:42:12 -07:00
|
|
|
return !mCanvas->isClipEmpty();
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas state operations: Filters
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2014-07-21 15:22:10 -04:00
|
|
|
SkDrawFilter* SkiaCanvas::getDrawFilter() {
|
|
|
|
return mCanvas->getDrawFilter();
|
|
|
|
}
|
|
|
|
|
2014-06-23 14:13:53 -04:00
|
|
|
void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
|
|
|
|
mCanvas->setDrawFilter(drawFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas draw operations
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void SkiaCanvas::drawColor(int color, SkXfermode::Mode mode) {
|
|
|
|
mCanvas->drawColor(color, mode);
|
|
|
|
}
|
|
|
|
|
2014-07-21 15:22:10 -04:00
|
|
|
void SkiaCanvas::drawPaint(const SkPaint& paint) {
|
2014-06-23 14:13:53 -04:00
|
|
|
mCanvas->drawPaint(paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas draw operations: Geometry
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2014-07-21 15:22:10 -04:00
|
|
|
void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
|
2014-06-23 14:13:53 -04:00
|
|
|
SkCanvas::PointMode mode) {
|
|
|
|
// convert the floats into SkPoints
|
|
|
|
count >>= 1; // now it is the number of points
|
2015-08-19 11:26:06 -04:00
|
|
|
std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
|
2014-06-23 14:13:53 -04:00
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
pts[i].set(points[0], points[1]);
|
|
|
|
points += 2;
|
|
|
|
}
|
2015-08-19 11:26:06 -04:00
|
|
|
mCanvas->drawPoints(mode, count, pts.get(), paint);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-21 15:22:10 -04:00
|
|
|
void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
|
2014-06-23 14:13:53 -04:00
|
|
|
mCanvas->drawPoint(x, y, paint);
|
|
|
|
}
|
|
|
|
|
2014-07-21 15:22:10 -04:00
|
|
|
void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
|
2014-06-23 14:13:53 -04:00
|
|
|
this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
|
2014-07-21 15:22:10 -04:00
|
|
|
const SkPaint& paint) {
|
2014-06-23 14:13:53 -04:00
|
|
|
mCanvas->drawLine(startX, startY, stopX, stopY, paint);
|
|
|
|
}
|
|
|
|
|
2014-07-21 15:22:10 -04:00
|
|
|
void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
|
2014-06-23 14:13:53 -04:00
|
|
|
this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkiaCanvas::drawRect(float left, float top, float right, float bottom,
|
2014-07-21 15:22:10 -04:00
|
|
|
const SkPaint& paint) {
|
2014-06-23 14:13:53 -04:00
|
|
|
mCanvas->drawRectCoords(left, top, right, bottom, paint);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-10 09:58:41 -04:00
|
|
|
void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
|
|
|
|
SkRegion::Iterator it(region);
|
|
|
|
while (!it.done()) {
|
|
|
|
mCanvas->drawRect(SkRect::Make(it.rect()), paint);
|
|
|
|
it.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-23 14:13:53 -04:00
|
|
|
void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom,
|
2014-07-21 15:22:10 -04:00
|
|
|
float rx, float ry, const SkPaint& paint) {
|
2014-06-23 14:13:53 -04:00
|
|
|
SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
|
|
|
|
mCanvas->drawRoundRect(rect, rx, ry, paint);
|
|
|
|
}
|
|
|
|
|
2014-07-21 15:22:10 -04:00
|
|
|
void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
|
2014-06-23 14:13:53 -04:00
|
|
|
mCanvas->drawCircle(x, y, radius, paint);
|
|
|
|
}
|
|
|
|
|
2014-07-21 15:22:10 -04:00
|
|
|
void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
|
2014-06-23 14:13:53 -04:00
|
|
|
SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
|
|
|
|
mCanvas->drawOval(oval, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkiaCanvas::drawArc(float left, float top, float right, float bottom,
|
2014-07-21 15:22:10 -04:00
|
|
|
float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
|
2014-06-23 14:13:53 -04:00
|
|
|
SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
|
|
|
|
mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
|
|
|
|
}
|
|
|
|
|
2014-07-21 15:22:10 -04:00
|
|
|
void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
|
2014-06-23 14:13:53 -04:00
|
|
|
mCanvas->drawPath(path, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
|
|
|
|
const float* verts, const float* texs, const int* colors,
|
2014-07-21 15:22:10 -04:00
|
|
|
const uint16_t* indices, int indexCount, const SkPaint& paint) {
|
2014-06-23 14:13:53 -04:00
|
|
|
#ifndef SK_SCALAR_IS_FLOAT
|
|
|
|
SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
|
|
|
|
#endif
|
|
|
|
const int ptCount = vertexCount >> 1;
|
|
|
|
mCanvas->drawVertices(vertexMode, ptCount, (SkPoint*)verts, (SkPoint*)texs,
|
|
|
|
(SkColor*)colors, NULL, indices, indexCount, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas draw operations: Bitmaps
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2014-07-21 15:22:10 -04:00
|
|
|
void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) {
|
2014-06-23 14:13:53 -04:00
|
|
|
mCanvas->drawBitmap(bitmap, left, top, paint);
|
|
|
|
}
|
|
|
|
|
2014-07-21 15:22:10 -04:00
|
|
|
void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
|
2014-12-08 17:03:30 -05:00
|
|
|
SkAutoCanvasRestore acr(mCanvas, true);
|
|
|
|
mCanvas->concat(matrix);
|
|
|
|
mCanvas->drawBitmap(bitmap, 0, 0, paint);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
|
|
|
|
float srcRight, float srcBottom, float dstLeft, float dstTop,
|
2014-07-21 15:22:10 -04:00
|
|
|
float dstRight, float dstBottom, const SkPaint* paint) {
|
2014-06-23 14:13:53 -04:00
|
|
|
SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
|
|
|
|
SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
|
2015-07-31 10:38:40 -04:00
|
|
|
mCanvas->drawBitmapRect(bitmap, srcRect, dstRect, paint);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkiaCanvas::drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
|
2014-07-21 15:22:10 -04:00
|
|
|
const float* vertices, const int* colors, const SkPaint* paint) {
|
2014-06-23 14:13:53 -04:00
|
|
|
|
|
|
|
const int ptCount = (meshWidth + 1) * (meshHeight + 1);
|
|
|
|
const int indexCount = meshWidth * meshHeight * 6;
|
|
|
|
|
|
|
|
/* Our temp storage holds 2 or 3 arrays.
|
|
|
|
texture points [ptCount * sizeof(SkPoint)]
|
|
|
|
optionally vertex points [ptCount * sizeof(SkPoint)] if we need a
|
|
|
|
copy to convert from float to fixed
|
|
|
|
indices [ptCount * sizeof(uint16_t)]
|
|
|
|
*/
|
|
|
|
ssize_t storageSize = ptCount * sizeof(SkPoint); // texs[]
|
|
|
|
storageSize += indexCount * sizeof(uint16_t); // indices[]
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef SK_SCALAR_IS_FLOAT
|
|
|
|
SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
|
|
|
|
#endif
|
2015-08-19 11:26:06 -04:00
|
|
|
std::unique_ptr<char[]> storage(new char[storageSize]);
|
2014-06-23 14:13:53 -04:00
|
|
|
SkPoint* texs = (SkPoint*)storage.get();
|
|
|
|
uint16_t* indices = (uint16_t*)(texs + ptCount);
|
|
|
|
|
|
|
|
// cons up texture coordinates and indices
|
|
|
|
{
|
|
|
|
const SkScalar w = SkIntToScalar(bitmap.width());
|
|
|
|
const SkScalar h = SkIntToScalar(bitmap.height());
|
|
|
|
const SkScalar dx = w / meshWidth;
|
|
|
|
const SkScalar dy = h / meshHeight;
|
|
|
|
|
|
|
|
SkPoint* texsPtr = texs;
|
|
|
|
SkScalar y = 0;
|
|
|
|
for (int i = 0; i <= meshHeight; i++) {
|
|
|
|
if (i == meshHeight) {
|
|
|
|
y = h; // to ensure numerically we hit h exactly
|
|
|
|
}
|
|
|
|
SkScalar x = 0;
|
|
|
|
for (int j = 0; j < meshWidth; j++) {
|
|
|
|
texsPtr->set(x, y);
|
|
|
|
texsPtr += 1;
|
|
|
|
x += dx;
|
|
|
|
}
|
|
|
|
texsPtr->set(w, y);
|
|
|
|
texsPtr += 1;
|
|
|
|
y += dy;
|
|
|
|
}
|
|
|
|
SkASSERT(texsPtr - texs == ptCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
// cons up indices
|
|
|
|
{
|
|
|
|
uint16_t* indexPtr = indices;
|
|
|
|
int index = 0;
|
|
|
|
for (int i = 0; i < meshHeight; i++) {
|
|
|
|
for (int j = 0; j < meshWidth; j++) {
|
|
|
|
// lower-left triangle
|
|
|
|
*indexPtr++ = index;
|
|
|
|
*indexPtr++ = index + meshWidth + 1;
|
|
|
|
*indexPtr++ = index + meshWidth + 2;
|
|
|
|
// upper-right triangle
|
|
|
|
*indexPtr++ = index;
|
|
|
|
*indexPtr++ = index + meshWidth + 2;
|
|
|
|
*indexPtr++ = index + 1;
|
|
|
|
// bump to the next cell
|
|
|
|
index += 1;
|
|
|
|
}
|
|
|
|
// bump to the next row
|
|
|
|
index += 1;
|
|
|
|
}
|
|
|
|
SkASSERT(indexPtr - indices == indexCount);
|
|
|
|
SkASSERT((char*)indexPtr - (char*)storage.get() == storageSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// double-check that we have legal indices
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
{
|
|
|
|
for (int i = 0; i < indexCount; i++) {
|
|
|
|
SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// cons-up a shader for the bitmap
|
2014-07-21 15:22:10 -04:00
|
|
|
SkPaint tmpPaint;
|
2014-06-23 14:13:53 -04:00
|
|
|
if (paint) {
|
|
|
|
tmpPaint = *paint;
|
|
|
|
}
|
|
|
|
SkShader* shader = SkShader::CreateBitmapShader(bitmap,
|
|
|
|
SkShader::kClamp_TileMode,
|
|
|
|
SkShader::kClamp_TileMode);
|
|
|
|
SkSafeUnref(tmpPaint.setShader(shader));
|
|
|
|
|
|
|
|
mCanvas->drawVertices(SkCanvas::kTriangles_VertexMode, ptCount, (SkPoint*)vertices,
|
|
|
|
texs, (const SkColor*)colors, NULL, indices,
|
|
|
|
indexCount, tmpPaint);
|
|
|
|
}
|
|
|
|
|
2015-07-10 13:56:39 -04:00
|
|
|
void SkiaCanvas::drawNinePatch(const SkBitmap& bitmap, const Res_png_9patch& chunk,
|
|
|
|
float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) {
|
|
|
|
SkRect bounds = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
|
|
|
|
NinePatch::Draw(mCanvas, bounds, bitmap, chunk, paint, nullptr);
|
|
|
|
}
|
|
|
|
|
2016-02-04 22:17:11 +00:00
|
|
|
void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
|
|
|
|
const SkBitmap& bitmap = vectorDrawable->getBitmapUpdateIfDirty();
|
|
|
|
SkRect bounds = vectorDrawable->getBounds();
|
|
|
|
drawBitmap(bitmap, 0, 0, bitmap.width(), bitmap.height(),
|
|
|
|
bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom,
|
|
|
|
vectorDrawable->getPaint());
|
|
|
|
}
|
|
|
|
|
2014-06-23 14:13:53 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas draw operations: Text
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2014-07-21 15:22:10 -04:00
|
|
|
void SkiaCanvas::drawText(const uint16_t* text, const float* positions, int count,
|
|
|
|
const SkPaint& paint, float x, float y,
|
2014-12-09 15:03:44 -05:00
|
|
|
float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
|
|
|
|
float totalAdvance) {
|
2014-07-21 15:22:10 -04:00
|
|
|
// Set align to left for drawing, as we don't want individual
|
|
|
|
// glyphs centered or right-aligned; the offset above takes
|
|
|
|
// care of all alignment.
|
|
|
|
SkPaint paintCopy(paint);
|
|
|
|
paintCopy.setTextAlign(SkPaint::kLeft_Align);
|
2014-06-23 14:13:53 -04:00
|
|
|
|
2015-08-19 15:49:37 -04:00
|
|
|
static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
|
2014-07-21 15:22:10 -04:00
|
|
|
mCanvas->drawPosText(text, count << 1, reinterpret_cast<const SkPoint*>(positions), paintCopy);
|
2015-11-19 13:02:43 -08:00
|
|
|
drawTextDecorations(x, y, totalAdvance, paint);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkiaCanvas::drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
|
2014-07-21 15:22:10 -04:00
|
|
|
float hOffset, float vOffset, const SkPaint& paint) {
|
2015-04-14 11:34:39 -04:00
|
|
|
mCanvas->drawTextOnPathHV(glyphs, count << 1, path, hOffset, vOffset, paint);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 10:00:39 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas draw operations: Animations
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class AnimatedRoundRect : public SkDrawable {
|
|
|
|
public:
|
|
|
|
AnimatedRoundRect(uirenderer::CanvasPropertyPrimitive* left,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* p) :
|
|
|
|
mLeft(left), mTop(top), mRight(right), mBottom(bottom), mRx(rx), mRy(ry), mPaint(p) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual SkRect onGetBounds() override {
|
|
|
|
return SkRect::MakeLTRB(mLeft->value, mTop->value, mRight->value, mBottom->value);
|
|
|
|
}
|
|
|
|
virtual void onDraw(SkCanvas* canvas) override {
|
|
|
|
SkRect rect = SkRect::MakeLTRB(mLeft->value, mTop->value, mRight->value, mBottom->value);
|
|
|
|
canvas->drawRoundRect(rect, mRx->value, mRy->value, mPaint->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
sp<uirenderer::CanvasPropertyPrimitive> mLeft;
|
|
|
|
sp<uirenderer::CanvasPropertyPrimitive> mTop;
|
|
|
|
sp<uirenderer::CanvasPropertyPrimitive> mRight;
|
|
|
|
sp<uirenderer::CanvasPropertyPrimitive> mBottom;
|
|
|
|
sp<uirenderer::CanvasPropertyPrimitive> mRx;
|
|
|
|
sp<uirenderer::CanvasPropertyPrimitive> mRy;
|
|
|
|
sp<uirenderer::CanvasPropertyPaint> mPaint;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AnimatedCircle : public SkDrawable {
|
|
|
|
public:
|
|
|
|
AnimatedCircle(uirenderer::CanvasPropertyPrimitive* x, uirenderer::CanvasPropertyPrimitive* y,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* radius, uirenderer::CanvasPropertyPaint* paint) :
|
|
|
|
mX(x), mY(y), mRadius(radius), mPaint(paint) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual SkRect onGetBounds() override {
|
|
|
|
const float x = mX->value;
|
|
|
|
const float y = mY->value;
|
|
|
|
const float radius = mRadius->value;
|
|
|
|
return SkRect::MakeLTRB(x - radius, y - radius, x + radius, y + radius);
|
|
|
|
}
|
|
|
|
virtual void onDraw(SkCanvas* canvas) override {
|
|
|
|
canvas->drawCircle(mX->value, mY->value, mRadius->value, mPaint->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
sp<uirenderer::CanvasPropertyPrimitive> mX;
|
|
|
|
sp<uirenderer::CanvasPropertyPrimitive> mY;
|
|
|
|
sp<uirenderer::CanvasPropertyPrimitive> mRadius;
|
|
|
|
sp<uirenderer::CanvasPropertyPaint> mPaint;
|
|
|
|
};
|
|
|
|
|
|
|
|
void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) {
|
|
|
|
SkAutoTUnref<AnimatedRoundRect> drawable(
|
|
|
|
new AnimatedRoundRect(left, top, right, bottom, rx, ry, paint));
|
|
|
|
mCanvas->drawDrawable(drawable.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x, uirenderer::CanvasPropertyPrimitive* y,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* radius, uirenderer::CanvasPropertyPaint* paint) {
|
|
|
|
SkAutoTUnref<AnimatedCircle> drawable(new AnimatedCircle(x, y, radius, paint));
|
|
|
|
mCanvas->drawDrawable(drawable.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas draw operations: View System
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layer) { }
|
|
|
|
|
|
|
|
void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) { }
|
|
|
|
|
|
|
|
void SkiaCanvas::callDrawGLFunction(Functor* functor) { }
|
|
|
|
|
2014-06-23 14:13:53 -04:00
|
|
|
} // namespace android
|