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.
|
|
|
|
*/
|
|
|
|
|
2016-07-15 10:28:16 -04:00
|
|
|
#include "SkiaCanvas.h"
|
|
|
|
|
2015-07-30 10:00:39 -04:00
|
|
|
#include "CanvasProperty.h"
|
2016-10-25 18:36:39 -04:00
|
|
|
#include "NinePatchUtils.h"
|
2016-07-15 10:28:16 -04:00
|
|
|
#include "VectorDrawable.h"
|
2016-10-14 16:30:21 -07:00
|
|
|
#include "hwui/Bitmap.h"
|
2016-07-18 13:07:42 -04:00
|
|
|
#include "hwui/MinikinUtils.h"
|
2016-10-17 16:26:15 -04:00
|
|
|
#include "pipeline/skia/AnimatedDrawables.h"
|
2014-06-23 14:13:53 -04:00
|
|
|
|
2018-01-14 16:52:17 -05:00
|
|
|
#include <SkAnimatedImage.h>
|
2017-04-13 09:33:18 -04:00
|
|
|
#include <SkCanvasStateUtils.h>
|
2017-07-26 13:53:27 -04:00
|
|
|
#include <SkColorFilter.h>
|
2017-03-29 16:25:10 -04:00
|
|
|
#include <SkColorSpaceXformCanvas.h>
|
2015-01-20 07:51:14 -08:00
|
|
|
#include <SkDeque.h>
|
|
|
|
#include <SkDrawFilter.h>
|
2017-11-03 10:12:19 -07:00
|
|
|
#include <SkDrawable.h>
|
2015-01-20 07:51:14 -08:00
|
|
|
#include <SkGraphics.h>
|
2015-07-30 10:00:39 -04:00
|
|
|
#include <SkImage.h>
|
2016-09-20 10:34:11 -04:00
|
|
|
#include <SkImagePriv.h>
|
2018-01-14 16:52:17 -05:00
|
|
|
#include <SkPicture.h>
|
2016-07-18 13:07:42 -04:00
|
|
|
#include <SkRSXform.h>
|
2015-01-20 07:51:14 -08:00
|
|
|
#include <SkShader.h>
|
|
|
|
#include <SkTemplates.h>
|
2016-10-24 10:40:39 -04:00
|
|
|
#include <SkTextBlob.h>
|
2014-06-23 14:13:53 -04:00
|
|
|
|
2015-08-07 12:13:48 -04:00
|
|
|
#include <memory>
|
|
|
|
|
2014-06-23 14:13:53 -04:00
|
|
|
namespace android {
|
|
|
|
|
2016-10-24 10:40:39 -04:00
|
|
|
using uirenderer::PaintUtils;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-08-04 08:35:10 -04:00
|
|
|
Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
|
|
|
|
return new SkiaCanvas(skiaCanvas);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2016-10-24 10:40:39 -04:00
|
|
|
SkiaCanvas::SkiaCanvas() {}
|
|
|
|
|
2017-08-04 08:35:10 -04:00
|
|
|
SkiaCanvas::SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {}
|
2016-10-24 10:40:39 -04:00
|
|
|
|
2015-04-22 09:04:45 -07:00
|
|
|
SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
|
2017-04-04 19:38:50 -07:00
|
|
|
sk_sp<SkColorSpace> cs = bitmap.refColorSpace();
|
2017-04-13 12:18:47 -04:00
|
|
|
mCanvasOwned =
|
|
|
|
std::unique_ptr<SkCanvas>(new SkCanvas(bitmap, SkCanvas::ColorBehavior::kLegacy));
|
2017-08-04 08:35:10 -04:00
|
|
|
if (cs.get() == nullptr || cs->isSRGB()) {
|
2017-11-03 10:12:19 -07:00
|
|
|
if (!uirenderer::Properties::isSkiaEnabled()) {
|
|
|
|
mCanvasWrapper =
|
|
|
|
SkCreateColorSpaceXformCanvas(mCanvasOwned.get(), SkColorSpace::MakeSRGB());
|
2017-08-04 08:35:10 -04:00
|
|
|
mCanvas = mCanvasWrapper.get();
|
|
|
|
} else {
|
|
|
|
mCanvas = mCanvasOwned.get();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/** The wrapper is needed if we are drawing into a non-sRGB destination, since
|
|
|
|
* we need to transform all colors (not just bitmaps via filters) into the
|
|
|
|
* destination's colorspace.
|
|
|
|
*/
|
|
|
|
mCanvasWrapper = SkCreateColorSpaceXformCanvas(mCanvasOwned.get(), std::move(cs));
|
|
|
|
mCanvas = mCanvasWrapper.get();
|
|
|
|
}
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2016-10-17 16:26:15 -04:00
|
|
|
SkiaCanvas::~SkiaCanvas() {}
|
|
|
|
|
2016-07-15 10:28:16 -04:00
|
|
|
void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
|
2016-11-18 17:21:09 -05:00
|
|
|
if (mCanvas != skiaCanvas) {
|
|
|
|
mCanvas = skiaCanvas;
|
|
|
|
mCanvasOwned.reset();
|
2017-08-04 08:35:10 -04:00
|
|
|
mCanvasWrapper.reset();
|
2016-11-18 17:21:09 -05:00
|
|
|
}
|
2016-07-15 10:28:16 -04:00
|
|
|
mSaveStack.reset(nullptr);
|
|
|
|
}
|
|
|
|
|
2014-06-23 14:13:53 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas state operations: Replace Bitmap
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-04-22 09:04:45 -07:00
|
|
|
void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
|
2017-04-04 19:38:50 -07:00
|
|
|
sk_sp<SkColorSpace> cs = bitmap.refColorSpace();
|
2017-04-13 12:18:47 -04:00
|
|
|
std::unique_ptr<SkCanvas> newCanvas =
|
|
|
|
std::unique_ptr<SkCanvas>(new SkCanvas(bitmap, SkCanvas::ColorBehavior::kLegacy));
|
2017-08-04 08:35:10 -04:00
|
|
|
std::unique_ptr<SkCanvas> newCanvasWrapper;
|
|
|
|
if (cs.get() != nullptr && !cs->isSRGB()) {
|
|
|
|
newCanvasWrapper = SkCreateColorSpaceXformCanvas(newCanvas.get(), std::move(cs));
|
2017-11-03 10:12:19 -07:00
|
|
|
} else if (!uirenderer::Properties::isSkiaEnabled()) {
|
2017-08-04 08:35:10 -04:00
|
|
|
newCanvasWrapper = SkCreateColorSpaceXformCanvas(newCanvas.get(), SkColorSpace::MakeSRGB());
|
|
|
|
}
|
2017-03-14 22:36:14 +00:00
|
|
|
|
|
|
|
// deletes the previously owned canvas (if any)
|
2017-03-29 16:25:10 -04:00
|
|
|
mCanvasOwned = std::move(newCanvas);
|
|
|
|
mCanvasWrapper = std::move(newCanvasWrapper);
|
2017-08-04 08:35:10 -04:00
|
|
|
mCanvas = mCanvasWrapper ? mCanvasWrapper.get() : mCanvasOwned.get();
|
2017-03-14 22:36:14 +00:00
|
|
|
|
2014-06-23 14:13:53 -04:00
|
|
|
// clean up the old save stack
|
2016-10-24 10:40:39 -04:00
|
|
|
mSaveStack.reset(nullptr);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// 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() {
|
2016-10-24 10:40:39 -04:00
|
|
|
const auto* rec = this->currentSaveRec();
|
|
|
|
if (!rec) {
|
2014-06-23 14:13:53 -04:00
|
|
|
// Fast path - no record for this frame.
|
|
|
|
mCanvas->restore();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-21 10:43:01 -05:00
|
|
|
bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
|
2017-11-03 10:12:19 -07:00
|
|
|
bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
|
2014-06-23 14:13:53 -04:00
|
|
|
|
|
|
|
SkMatrix savedMatrix;
|
|
|
|
if (preserveMatrix) {
|
|
|
|
savedMatrix = mCanvas->getTotalMatrix();
|
|
|
|
}
|
|
|
|
|
2016-10-24 10:40:39 -04:00
|
|
|
const size_t clipIndex = rec->clipIndex;
|
2014-06-23 14:13:53 -04:00
|
|
|
|
|
|
|
mCanvas->restore();
|
2016-10-24 10:40:39 -04:00
|
|
|
mSaveStack->pop_back();
|
2014-06-23 14:13:53 -04:00
|
|
|
|
|
|
|
if (preserveMatrix) {
|
|
|
|
mCanvas->setMatrix(savedMatrix);
|
|
|
|
}
|
|
|
|
|
2016-10-24 10:40:39 -04:00
|
|
|
if (preserveClip) {
|
|
|
|
this->applyPersistentClips(clipIndex);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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::ClipToLayer)) {
|
|
|
|
layerFlags |= SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
return layerFlags;
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, const SkPaint* paint,
|
|
|
|
SaveFlags::Flags flags) {
|
2015-12-21 10:43:01 -05:00
|
|
|
const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
|
2017-01-09 16:11:59 -05:00
|
|
|
const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags));
|
2015-12-21 10:43:01 -05:00
|
|
|
|
2016-12-14 11:18:34 -05:00
|
|
|
return mCanvas->saveLayer(rec);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, int alpha,
|
|
|
|
SaveFlags::Flags flags) {
|
2015-12-21 10:43:01 -05:00
|
|
|
if (static_cast<unsigned>(alpha) < 0xFF) {
|
2016-04-27 17:03:38 -04:00
|
|
|
SkPaint alphaPaint;
|
|
|
|
alphaPaint.setAlpha(alpha);
|
|
|
|
return this->saveLayer(left, top, right, bottom, &alphaPaint, flags);
|
2015-12-21 10:43:01 -05:00
|
|
|
}
|
2016-04-27 17:03:38 -04:00
|
|
|
return this->saveLayer(left, top, right, bottom, nullptr, flags);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2016-10-24 10:40:39 -04:00
|
|
|
class SkiaCanvas::Clip {
|
|
|
|
public:
|
2016-12-02 15:36:59 -05:00
|
|
|
Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
|
2017-11-03 10:12:19 -07:00
|
|
|
: mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
|
2016-12-02 15:36:59 -05:00
|
|
|
Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
|
2017-11-03 10:12:19 -07:00
|
|
|
: mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
|
2016-12-02 15:36:59 -05:00
|
|
|
Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
|
2017-11-03 10:12:19 -07:00
|
|
|
: mType(Type::Path), mOp(op), mMatrix(m), mPath(&path) {}
|
2016-10-24 10:40:39 -04:00
|
|
|
|
|
|
|
void apply(SkCanvas* canvas) const {
|
|
|
|
canvas->setMatrix(mMatrix);
|
|
|
|
switch (mType) {
|
2017-11-03 10:12:19 -07:00
|
|
|
case Type::Rect:
|
|
|
|
canvas->clipRect(mRRect.rect(), mOp);
|
|
|
|
break;
|
|
|
|
case Type::RRect:
|
|
|
|
canvas->clipRRect(mRRect, mOp);
|
|
|
|
break;
|
|
|
|
case Type::Path:
|
|
|
|
canvas->clipPath(*mPath.get(), mOp);
|
|
|
|
break;
|
2016-10-24 10:40:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum class Type {
|
|
|
|
Rect,
|
|
|
|
RRect,
|
|
|
|
Path,
|
|
|
|
};
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
Type mType;
|
|
|
|
SkClipOp mOp;
|
|
|
|
SkMatrix mMatrix;
|
2016-10-24 10:40:39 -04:00
|
|
|
|
|
|
|
// These are logically a union (tracked separately due to non-POD path).
|
|
|
|
SkTLazy<SkPath> mPath;
|
2017-11-03 10:12:19 -07:00
|
|
|
SkRRect mRRect;
|
2016-10-24 10:40:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
|
2017-11-03 10:12:19 -07:00
|
|
|
const SaveRec* rec = mSaveStack ? static_cast<const SaveRec*>(mSaveStack->back()) : nullptr;
|
2016-10-24 10:40:39 -04:00
|
|
|
int currentSaveCount = mCanvas->getSaveCount();
|
|
|
|
SkASSERT(!rec || currentSaveCount >= rec->saveCount);
|
|
|
|
|
|
|
|
return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-24 10:40:39 -04:00
|
|
|
if (!mSaveStack) {
|
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;
|
2016-10-24 10:40:39 -04:00
|
|
|
rec->clipIndex = mClipStack.size();
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2016-10-24 10:40:39 -04:00
|
|
|
template <typename T>
|
2016-12-02 15:36:59 -05:00
|
|
|
void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
|
2016-10-24 10:40:39 -04:00
|
|
|
// Only need tracking when in a partial save frame which
|
|
|
|
// doesn't restore the clip.
|
|
|
|
const SaveRec* rec = this->currentSaveRec();
|
|
|
|
if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
|
|
|
|
mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 10:40:39 -04:00
|
|
|
// Applies and optionally removes all clips >= index.
|
|
|
|
void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
|
|
|
|
SkASSERT(clipStartIndex <= mClipStack.size());
|
|
|
|
const auto begin = mClipStack.cbegin() + clipStartIndex;
|
|
|
|
const auto end = mClipStack.cend();
|
2014-06-23 14:13:53 -04:00
|
|
|
|
2016-10-24 10:40:39 -04:00
|
|
|
// Clip application mutates the CTM.
|
|
|
|
const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
|
2014-06-23 14:13:53 -04:00
|
|
|
|
2016-10-24 10:40:39 -04:00
|
|
|
for (auto clip = begin; clip != end; ++clip) {
|
2016-11-18 17:21:09 -05:00
|
|
|
clip->apply(mCanvas);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2016-10-24 10:40:39 -04:00
|
|
|
mCanvas->setMatrix(saveMatrix);
|
|
|
|
|
|
|
|
// If the current/post-restore save rec is also persisting clips, we
|
|
|
|
// leave them on the stack to be reapplied part of the next restore().
|
|
|
|
// Otherwise we're done and just pop them.
|
|
|
|
const auto* rec = this->currentSaveRec();
|
|
|
|
if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
|
|
|
|
mClipStack.erase(begin, end);
|
|
|
|
}
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// 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;
|
2017-01-25 08:23:25 -05:00
|
|
|
if (!mCanvas->getDeviceClipBounds(&ibounds)) {
|
2014-06-23 14:13:53 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-12-02 15:36:59 -05:00
|
|
|
bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
|
2014-06-23 14:13:53 -04:00
|
|
|
SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
|
2016-10-24 10:40:39 -04:00
|
|
|
this->recordClip(rect, op);
|
2014-06-23 14:13:53 -04:00
|
|
|
mCanvas->clipRect(rect, op);
|
2015-06-23 15:42:12 -07:00
|
|
|
return !mCanvas->isClipEmpty();
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2016-12-02 15:36:59 -05:00
|
|
|
bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
|
2017-04-17 11:27:36 -04:00
|
|
|
this->recordClip(*path, op);
|
|
|
|
mCanvas->clipPath(*path, 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);
|
|
|
|
}
|
|
|
|
|
2017-04-13 09:33:18 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas state operations: Capture
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
SkCanvasState* SkiaCanvas::captureCanvasState() const {
|
|
|
|
SkCanvas* canvas = mCanvas;
|
|
|
|
if (mCanvasOwned) {
|
|
|
|
// Important to use the underlying SkCanvas, not the wrapper.
|
|
|
|
canvas = mCanvasOwned.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Workarounds for http://crbug.com/271096: SW draw only supports
|
|
|
|
// translate & scale transforms, and a simple rectangular clip.
|
|
|
|
// (This also avoids significant wasted time in calling
|
|
|
|
// SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
|
2017-11-03 10:12:19 -07:00
|
|
|
if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
|
|
|
|
~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
|
|
|
|
return nullptr;
|
2017-04-13 09:33:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return SkCanvasStateUtils::CaptureCanvasState(canvas);
|
|
|
|
}
|
|
|
|
|
2014-06-23 14:13:53 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas draw operations
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2016-10-07 15:59:20 -04:00
|
|
|
void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
|
2014-06-23 14:13:53 -04:00
|
|
|
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) {
|
2017-03-03 14:02:09 -05:00
|
|
|
if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
|
2014-06-23 14:13:53 -04:00
|
|
|
// convert the floats into SkPoints
|
2017-11-03 10:12:19 -07:00
|
|
|
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) {
|
2017-03-03 14:02:09 -05:00
|
|
|
if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
|
2014-06-23 14:13:53 -04:00
|
|
|
this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const SkPaint& paint) {
|
2017-03-03 14:02:09 -05:00
|
|
|
if (CC_UNLIKELY(paint.nothingToDraw())) return;
|
2017-04-04 09:15:37 -04:00
|
|
|
mCanvas->drawRect({left, top, right, bottom}, paint);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2015-07-10 09:58:41 -04:00
|
|
|
void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
|
2017-03-03 14:02:09 -05:00
|
|
|
if (CC_UNLIKELY(paint.nothingToDraw())) return;
|
2016-10-24 10:40:39 -04:00
|
|
|
mCanvas->drawRegion(region, paint);
|
2015-07-10 09:58:41 -04:00
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
|
|
|
|
const SkPaint& paint) {
|
2017-03-03 14:02:09 -05:00
|
|
|
if (CC_UNLIKELY(paint.nothingToDraw())) return;
|
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) {
|
2017-03-03 14:02:09 -05:00
|
|
|
if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
|
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) {
|
2017-03-03 14:02:09 -05:00
|
|
|
if (CC_UNLIKELY(paint.nothingToDraw())) return;
|
2014-06-23 14:13:53 -04:00
|
|
|
SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
|
|
|
|
mCanvas->drawOval(oval, paint);
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
|
|
|
|
float sweepAngle, bool useCenter, const SkPaint& paint) {
|
2017-03-03 14:02:09 -05:00
|
|
|
if (CC_UNLIKELY(paint.nothingToDraw())) return;
|
2014-06-23 14:13:53 -04:00
|
|
|
SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
|
2017-11-06 13:55:59 -05:00
|
|
|
if (fabs(sweepAngle) >= 360.0f) {
|
|
|
|
mCanvas->drawOval(arc, paint);
|
|
|
|
} else {
|
|
|
|
mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
|
|
|
|
}
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2014-07-21 15:22:10 -04:00
|
|
|
void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
|
2017-03-03 14:02:09 -05:00
|
|
|
if (CC_UNLIKELY(paint.nothingToDraw())) return;
|
2017-08-15 16:42:05 -04:00
|
|
|
if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-09 13:08:27 -05:00
|
|
|
mCanvas->drawPath(path, paint);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2017-04-04 15:32:04 -04:00
|
|
|
void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) {
|
|
|
|
mCanvas->drawVertices(vertices, mode, paint);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas draw operations: Bitmaps
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2017-08-04 08:35:10 -04:00
|
|
|
const SkPaint* SkiaCanvas::addFilter(const SkPaint* origPaint, SkPaint* tmpPaint,
|
2017-11-03 10:12:19 -07:00
|
|
|
sk_sp<SkColorFilter> colorSpaceFilter) {
|
2017-08-04 08:35:10 -04:00
|
|
|
/* We don't apply the colorSpace filter if this canvas is already wrapped with
|
|
|
|
* a SkColorSpaceXformCanvas since it already takes care of converting the
|
|
|
|
* contents of the bitmap into the appropriate colorspace. The mCanvasWrapper
|
|
|
|
* should only be used if this canvas is backed by a surface/bitmap that is known
|
|
|
|
* to have a non-sRGB colorspace.
|
|
|
|
*/
|
|
|
|
if (!mCanvasWrapper && colorSpaceFilter) {
|
2017-07-26 13:53:27 -04:00
|
|
|
if (origPaint) {
|
|
|
|
*tmpPaint = *origPaint;
|
|
|
|
}
|
2017-08-04 08:35:10 -04:00
|
|
|
|
|
|
|
if (tmpPaint->getColorFilter()) {
|
2017-11-03 10:12:19 -07:00
|
|
|
tmpPaint->setColorFilter(
|
|
|
|
SkColorFilter::MakeComposeFilter(tmpPaint->refColorFilter(), colorSpaceFilter));
|
2017-08-04 08:35:10 -04:00
|
|
|
LOG_ALWAYS_FATAL_IF(!tmpPaint->getColorFilter());
|
|
|
|
} else {
|
|
|
|
tmpPaint->setColorFilter(colorSpaceFilter);
|
|
|
|
}
|
|
|
|
|
2017-07-26 13:53:27 -04:00
|
|
|
return tmpPaint;
|
|
|
|
} else {
|
|
|
|
return origPaint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 16:30:21 -07:00
|
|
|
void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) {
|
2017-07-26 13:53:27 -04:00
|
|
|
SkPaint tmpPaint;
|
|
|
|
sk_sp<SkColorFilter> colorFilter;
|
|
|
|
sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
|
|
|
|
mCanvas->drawImage(image, left, top, addFilter(paint, &tmpPaint, colorFilter));
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2017-07-26 13:53:27 -04:00
|
|
|
void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
|
2016-11-18 17:21:09 -05:00
|
|
|
SkAutoCanvasRestore acr(mCanvas, true);
|
2014-12-08 17:03:30 -05:00
|
|
|
mCanvas->concat(matrix);
|
2017-07-26 13:53:27 -04:00
|
|
|
|
|
|
|
SkPaint tmpPaint;
|
|
|
|
sk_sp<SkColorFilter> colorFilter;
|
|
|
|
sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
|
|
|
|
mCanvas->drawImage(image, 0, 0, addFilter(paint, &tmpPaint, colorFilter));
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
|
|
|
|
float srcBottom, float dstLeft, float dstTop, 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);
|
2017-07-26 13:53:27 -04:00
|
|
|
|
|
|
|
SkPaint tmpPaint;
|
|
|
|
sk_sp<SkColorFilter> colorFilter;
|
|
|
|
sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
|
2017-08-15 16:23:01 -04:00
|
|
|
mCanvas->drawImageRect(image, srcRect, dstRect, addFilter(paint, &tmpPaint, colorFilter),
|
2017-11-03 10:12:19 -07:00
|
|
|
SkCanvas::kFast_SrcRectConstraint);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2017-07-26 13:53:27 -04:00
|
|
|
void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
|
2017-11-03 10:12:19 -07: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;
|
2017-03-17 10:15:52 -04:00
|
|
|
uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
|
|
|
|
if (colors) {
|
|
|
|
flags |= SkVertices::kHasColors_BuilderFlag;
|
|
|
|
}
|
2017-04-04 15:32:04 -04:00
|
|
|
SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
|
2017-03-17 10:15:52 -04:00
|
|
|
memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
|
|
|
|
if (colors) {
|
|
|
|
memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
|
|
|
|
}
|
|
|
|
SkPoint* texs = builder.texCoords();
|
|
|
|
uint16_t* indices = builder.indices();
|
2014-06-23 14:13:53 -04:00
|
|
|
|
|
|
|
// cons up texture coordinates and indices
|
|
|
|
{
|
2017-07-26 13:53:27 -04:00
|
|
|
const SkScalar w = SkIntToScalar(bitmap.width());
|
|
|
|
const SkScalar h = SkIntToScalar(bitmap.height());
|
2014-06-23 14:13:53 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
// double-check that we have legal indices
|
2014-06-23 14:13:53 -04:00
|
|
|
#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;
|
|
|
|
}
|
2016-10-24 10:40:39 -04:00
|
|
|
|
2017-07-26 13:53:27 -04:00
|
|
|
sk_sp<SkColorFilter> colorFilter;
|
|
|
|
sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
|
2017-11-03 10:12:19 -07:00
|
|
|
sk_sp<SkShader> shader =
|
|
|
|
image->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
|
|
|
|
if (colorFilter) {
|
2017-07-26 13:53:27 -04:00
|
|
|
shader = shader->makeWithColorFilter(colorFilter);
|
|
|
|
}
|
|
|
|
tmpPaint.setShader(shader);
|
2014-06-23 14:13:53 -04:00
|
|
|
|
2017-03-17 10:15:52 -04:00
|
|
|
mCanvas->drawVertices(builder.detach(), SkBlendMode::kModulate, tmpPaint);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
|
|
|
|
float dstTop, float dstRight, float dstBottom,
|
|
|
|
const SkPaint* paint) {
|
2016-10-24 10:40:39 -04:00
|
|
|
SkCanvas::Lattice lattice;
|
2017-07-26 13:53:27 -04:00
|
|
|
NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
|
2016-10-24 10:40:39 -04:00
|
|
|
|
2017-12-04 14:48:27 -05:00
|
|
|
lattice.fRectTypes = nullptr;
|
|
|
|
lattice.fColors = nullptr;
|
2016-10-24 10:40:39 -04:00
|
|
|
int numFlags = 0;
|
2016-10-17 16:26:15 -04:00
|
|
|
if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
|
2016-10-24 10:40:39 -04:00
|
|
|
// We can expect the framework to give us a color for every distinct rect.
|
|
|
|
// Skia requires a flag for every rect.
|
|
|
|
numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
|
|
|
|
}
|
|
|
|
|
2017-12-04 14:48:27 -05:00
|
|
|
SkAutoSTMalloc<25, SkCanvas::Lattice::RectType> flags(numFlags);
|
|
|
|
SkAutoSTMalloc<25, SkColor> colors(numFlags);
|
2016-10-24 10:40:39 -04:00
|
|
|
if (numFlags > 0) {
|
2017-12-04 14:48:27 -05:00
|
|
|
NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk, colors.get());
|
2016-10-24 10:40:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
lattice.fBounds = nullptr;
|
|
|
|
SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
|
2017-07-26 13:53:27 -04:00
|
|
|
|
|
|
|
SkPaint tmpPaint;
|
|
|
|
sk_sp<SkColorFilter> colorFilter;
|
|
|
|
sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
|
|
|
|
mCanvas->drawImageLattice(image.get(), lattice, dst, addFilter(paint, &tmpPaint, colorFilter));
|
2015-07-10 13:56:39 -04:00
|
|
|
}
|
|
|
|
|
2018-01-22 10:25:26 -05:00
|
|
|
double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
|
|
|
|
return imgDrawable->drawStaging(mCanvas);
|
2018-01-14 16:52:17 -05:00
|
|
|
}
|
|
|
|
|
2016-02-04 22:17:11 +00:00
|
|
|
void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
|
2016-03-02 15:16:28 -08:00
|
|
|
vectorDrawable->drawStaging(this);
|
2016-02-04 22:17:11 +00:00
|
|
|
}
|
|
|
|
|
2014-06-23 14:13:53 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas draw operations: Text
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2017-03-30 18:22:27 -04:00
|
|
|
void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const SkPaint& paint, float x,
|
2017-11-03 10:12:19 -07:00
|
|
|
float y, float boundsLeft, float boundsTop, float boundsRight,
|
|
|
|
float boundsBottom, float totalAdvance) {
|
2017-03-30 18:22:27 -04:00
|
|
|
if (count <= 0 || paint.nothingToDraw()) return;
|
2016-10-24 10:40:39 -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);
|
2017-11-29 13:15:45 -05:00
|
|
|
SkASSERT(paintCopy.getTextEncoding() == SkPaint::kGlyphID_TextEncoding);
|
2018-02-05 18:04:11 -05:00
|
|
|
// Stroke with a hairline is drawn on HW with a fill style for compatibility with Android O and
|
|
|
|
// older.
|
|
|
|
if (!mCanvasOwned && sApiLevel <= 27 && paintCopy.getStrokeWidth() <= 0
|
|
|
|
&& paintCopy.getStyle() == SkPaint::kStroke_Style) {
|
|
|
|
paintCopy.setStyle(SkPaint::kFill_Style);
|
|
|
|
}
|
2016-10-24 10:40:39 -04:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
SkRect bounds =
|
|
|
|
SkRect::MakeLTRB(boundsLeft + x, boundsTop + y, boundsRight + x, boundsBottom + y);
|
2016-10-24 10:40:39 -04:00
|
|
|
|
|
|
|
SkTextBlobBuilder builder;
|
|
|
|
const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(paintCopy, count, &bounds);
|
2017-03-30 18:22:27 -04:00
|
|
|
glyphFunc(buffer.glyphs, buffer.pos);
|
2016-10-24 10:40:39 -04:00
|
|
|
|
|
|
|
sk_sp<SkTextBlob> textBlob(builder.make());
|
|
|
|
mCanvas->drawTextBlob(textBlob, 0, 0, paintCopy);
|
|
|
|
drawTextDecorations(x, y, totalAdvance, paintCopy);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2016-07-18 13:07:42 -04:00
|
|
|
void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
|
2017-11-03 10:12:19 -07:00
|
|
|
const SkPaint& paint, const SkPath& path, size_t start,
|
|
|
|
size_t end) {
|
2018-03-14 15:03:13 -04:00
|
|
|
// Set align to left for drawing, as we don't want individual
|
|
|
|
// glyphs centered or right-aligned; the offsets take care of
|
|
|
|
// that portion of the alignment.
|
|
|
|
SkPaint paintCopy(paint);
|
|
|
|
paintCopy.setTextAlign(SkPaint::kLeft_Align);
|
|
|
|
SkASSERT(paintCopy.getTextEncoding() == SkPaint::kGlyphID_TextEncoding);
|
|
|
|
|
2016-07-18 13:07:42 -04:00
|
|
|
const int N = end - start;
|
2016-11-09 11:55:59 -05:00
|
|
|
SkAutoSTMalloc<1024, uint8_t> storage(N * (sizeof(uint16_t) + sizeof(SkRSXform)));
|
2016-07-18 13:07:42 -04:00
|
|
|
SkRSXform* xform = (SkRSXform*)storage.get();
|
|
|
|
uint16_t* glyphs = (uint16_t*)(xform + N);
|
|
|
|
SkPathMeasure meas(path, false);
|
|
|
|
|
|
|
|
for (size_t i = start; i < end; i++) {
|
|
|
|
glyphs[i - start] = layout.getGlyphId(i);
|
2018-01-10 16:06:04 -05:00
|
|
|
float halfWidth = layout.getCharAdvance(i) * 0.5f;
|
|
|
|
float x = hOffset + layout.getX(i) + halfWidth;
|
2016-07-18 13:07:42 -04:00
|
|
|
float y = vOffset + layout.getY(i);
|
|
|
|
|
|
|
|
SkPoint pos;
|
|
|
|
SkVector tan;
|
|
|
|
if (!meas.getPosTan(x, &pos, &tan)) {
|
|
|
|
pos.set(x, y);
|
|
|
|
tan.set(1, 0);
|
|
|
|
}
|
|
|
|
xform[i - start].fSCos = tan.x();
|
|
|
|
xform[i - start].fSSin = tan.y();
|
2018-01-10 16:06:04 -05:00
|
|
|
xform[i - start].fTx = pos.x() - tan.y() * y - halfWidth * tan.x();
|
|
|
|
xform[i - start].fTy = pos.y() + tan.x() * y - halfWidth * tan.y();
|
2016-07-18 13:07:42 -04:00
|
|
|
}
|
|
|
|
|
2018-03-14 15:03:13 -04:00
|
|
|
this->asSkCanvas()->drawTextRSXform(glyphs, sizeof(uint16_t) * N, xform, nullptr, paintCopy);
|
2014-06-23 14:13:53 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 10:00:39 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas draw operations: Animations
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
|
2017-11-03 10:12:19 -07:00
|
|
|
uirenderer::CanvasPropertyPrimitive* top,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* right,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* bottom,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* rx,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* ry,
|
|
|
|
uirenderer::CanvasPropertyPaint* paint) {
|
2016-10-17 16:26:15 -04:00
|
|
|
sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
|
2017-11-03 10:12:19 -07:00
|
|
|
new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
|
|
|
|
paint));
|
2015-07-30 10:00:39 -04:00
|
|
|
mCanvas->drawDrawable(drawable.get());
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* y,
|
|
|
|
uirenderer::CanvasPropertyPrimitive* radius,
|
|
|
|
uirenderer::CanvasPropertyPaint* paint) {
|
|
|
|
sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
|
|
|
|
new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
|
2015-07-30 10:00:39 -04:00
|
|
|
mCanvas->drawDrawable(drawable.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Canvas draw operations: View System
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2016-10-24 10:40:39 -04:00
|
|
|
void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
|
2016-07-15 10:28:16 -04:00
|
|
|
LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
|
|
|
|
}
|
2015-07-30 10:00:39 -04:00
|
|
|
|
2016-07-15 10:28:16 -04:00
|
|
|
void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
|
|
|
|
LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
|
|
|
|
}
|
2015-07-30 10:00:39 -04:00
|
|
|
|
2016-04-14 10:38:54 -07:00
|
|
|
void SkiaCanvas::callDrawGLFunction(Functor* functor,
|
2017-11-03 10:12:19 -07:00
|
|
|
uirenderer::GlFunctorLifecycleListener* listener) {
|
2016-07-15 10:28:16 -04:00
|
|
|
LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw GL Content");
|
|
|
|
}
|
2015-07-30 10:00:39 -04:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
} // namespace android
|