18872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger/*
28872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * Copyright (C) 2014 The Android Open Source Project
38872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger *
48872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * Licensed under the Apache License, Version 2.0 (the "License");
58872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * you may not use this file except in compliance with the License.
68872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * You may obtain a copy of the License at
78872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger *
88872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger *      http://www.apache.org/licenses/LICENSE-2.0
98872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger *
108872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * Unless required by applicable law or agreed to in writing, software
118872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * distributed under the License is distributed on an "AS IS" BASIS,
128872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * See the License for the specific language governing permissions and
148872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * limitations under the License.
158872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger */
168872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
176f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger#include "CanvasProperty.h"
186f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger#include "Layer.h"
196f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger#include "RenderNode.h"
20dccca44ffda4836b56a21da95a046c9708ffd49csergeyv#include "hwui/Canvas.h"
218872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
22849911a9c4315fc552faa38516c842b2541b1909John Reck#include <SkCanvas.h>
23849911a9c4315fc552faa38516c842b2541b1909John Reck#include <SkClipStack.h>
246f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger#include <SkDrawable.h>
25849911a9c4315fc552faa38516c842b2541b1909John Reck#include <SkDevice.h>
26849911a9c4315fc552faa38516c842b2541b1909John Reck#include <SkDeque.h>
27849911a9c4315fc552faa38516c842b2541b1909John Reck#include <SkDrawFilter.h>
28849911a9c4315fc552faa38516c842b2541b1909John Reck#include <SkGraphics.h>
296f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger#include <SkImage.h>
30849911a9c4315fc552faa38516c842b2541b1909John Reck#include <SkShader.h>
31849911a9c4315fc552faa38516c842b2541b1909John Reck#include <SkTArray.h>
32eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita#include <SkTLazy.h>
33849911a9c4315fc552faa38516c842b2541b1909John Reck#include <SkTemplates.h>
348872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
35766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu#include "VectorDrawable.h"
36766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu
3760126efd7d905ca24822765c6dafac17fef278abBen Wagner#include <memory>
3860126efd7d905ca24822765c6dafac17fef278abBen Wagner
398872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergernamespace android {
408872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
418872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// Holds an SkCanvas reference plus additional native data.
428872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerclass SkiaCanvas : public Canvas {
438872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerpublic:
44c1b33d665c8caf5760f68c45c6ca0baa649b832aJohn Reck    explicit SkiaCanvas(const SkBitmap& bitmap);
458872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4618981294a58c411a4a33ebb22caf9c75ba639691Leon Scroggins III    /**
4718981294a58c411a4a33ebb22caf9c75ba639691Leon Scroggins III     *  Create a new SkiaCanvas.
4818981294a58c411a4a33ebb22caf9c75ba639691Leon Scroggins III     *
4918981294a58c411a4a33ebb22caf9c75ba639691Leon Scroggins III     *  @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must
5018981294a58c411a4a33ebb22caf9c75ba639691Leon Scroggins III     *      not be NULL. This constructor will ref() the SkCanvas, and unref()
5118981294a58c411a4a33ebb22caf9c75ba639691Leon Scroggins III     *      it in its destructor.
5218981294a58c411a4a33ebb22caf9c75ba639691Leon Scroggins III     */
5318981294a58c411a4a33ebb22caf9c75ba639691Leon Scroggins III    explicit SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {
548872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        SkASSERT(canvas);
5518981294a58c411a4a33ebb22caf9c75ba639691Leon Scroggins III        canvas->ref();
568872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
578872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
58b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual SkCanvas* asSkCanvas() override {
598872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        return mCanvas.get();
608872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
618872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
626f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    virtual void resetRecording(int width, int height) override {
636f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger        LOG_ALWAYS_FATAL("SkiaCanvas cannot be reset as a recording canvas");
646f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    }
656f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger
666f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    virtual uirenderer::DisplayList* finishRecording() override {
676f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger        LOG_ALWAYS_FATAL("SkiaCanvas does not produce a DisplayList");
686f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger        return nullptr;
696f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    }
706f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    virtual void insertReorderBarrier(bool enableReorder) override {
716f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger        LOG_ALWAYS_FATAL("SkiaCanvas does not support reordering barriers");
726f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    }
736f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger
74c1b33d665c8caf5760f68c45c6ca0baa649b832aJohn Reck    virtual void setBitmap(const SkBitmap& bitmap) override;
758872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
76b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual bool isOpaque() override;
77b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual int width() override;
78b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual int height() override;
798872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
806578a989566e585eee053095dc80e2552e125db2Derek Sollenberger    virtual void setHighContrastText(bool highContrastText) override {
816578a989566e585eee053095dc80e2552e125db2Derek Sollenberger        mHighContrastText = highContrastText;
826578a989566e585eee053095dc80e2552e125db2Derek Sollenberger    }
836578a989566e585eee053095dc80e2552e125db2Derek Sollenberger    virtual bool isHighContrastText() override { return mHighContrastText; }
846578a989566e585eee053095dc80e2552e125db2Derek Sollenberger
85b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual int getSaveCount() const override;
86eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    virtual int save(SaveFlags::Flags flags) override;
87b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void restore() override;
88b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void restoreToCount(int saveCount) override;
898872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
908872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    virtual int saveLayer(float left, float top, float right, float bottom,
91eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita                const SkPaint* paint, SaveFlags::Flags flags) override;
928872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    virtual int saveLayerAlpha(float left, float top, float right, float bottom,
93eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita            int alpha, SaveFlags::Flags flags) override;
94b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger
95b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void getMatrix(SkMatrix* outMatrix) const override;
96b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void setMatrix(const SkMatrix& matrix) override;
97b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void concat(const SkMatrix& matrix) override;
98b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void rotate(float degrees) override;
99b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void scale(float sx, float sy) override;
100b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void skew(float sx, float sy) override;
101b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void translate(float dx, float dy) override;
102b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger
103b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual bool getClipBounds(SkRect* outRect) const override;
104b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
105b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual bool quickRejectPath(const SkPath& path) const override;
106b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual bool clipRect(float left, float top, float right, float bottom,
107b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger            SkRegion::Op op) override;
108b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual bool clipPath(const SkPath* path, SkRegion::Op op) override;
109b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) override;
110b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger
111b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual SkDrawFilter* getDrawFilter() override;
112b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void setDrawFilter(SkDrawFilter* drawFilter) override;
113b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger
114b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void drawColor(int color, SkXfermode::Mode mode) override;
115b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void drawPaint(const SkPaint& paint) override;
116b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger
117b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void drawPoint(float x, float y, const SkPaint& paint) override;
118b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void drawPoints(const float* points, int count, const SkPaint& paint) override;
1198872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    virtual void drawLine(float startX, float startY, float stopX, float stopY,
120b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger            const SkPaint& paint) override;
121b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void drawLines(const float* points, int count, const SkPaint& paint) override;
122b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void drawRect(float left, float top, float right, float bottom,
123b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger            const SkPaint& paint) override;
12494394b3fb048d5349a77b57950ab7f6b6e92ce34Derek Sollenberger    virtual void drawRegion(const SkRegion& region, const SkPaint& paint) override;
1258872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    virtual void drawRoundRect(float left, float top, float right, float bottom,
126b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger            float rx, float ry, const SkPaint& paint) override;
127b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override;
128b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void drawOval(float left, float top, float right, float bottom,
129b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger            const SkPaint& paint) override;
1308872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    virtual void drawArc(float left, float top, float right, float bottom,
131b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger            float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override;
132b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void drawPath(const SkPath& path, const SkPaint& paint) override;
1338872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
1348872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            const float* verts, const float* tex, const int* colors,
135b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger            const uint16_t* indices, int indexCount, const SkPaint& paint) override;
1368872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
137b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
138b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger            const SkPaint* paint) override;
139b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
140b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger            const SkPaint* paint) override;
1418872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
1428872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            float srcRight, float srcBottom, float dstLeft, float dstTop,
143b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger            float dstRight, float dstBottom, const SkPaint* paint) override;
1448872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
145b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger            const float* vertices, const int* colors, const SkPaint* paint) override;
146edca320a2b42011f98c308fdf25fc0494c6a5454Derek Sollenberger    virtual void drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& chunk,
147edca320a2b42011f98c308fdf25fc0494c6a5454Derek Sollenberger            float dstLeft, float dstTop, float dstRight, float dstBottom,
148edca320a2b42011f98c308fdf25fc0494c6a5454Derek Sollenberger            const SkPaint* paint) override;
1498872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
150b3d50e007523d1ada0866d384c391c72ac1a3577Derek Sollenberger    virtual bool drawTextAbsolutePos() const  override { return true; }
151766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu    virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override;
1528872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1536f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
1546f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger            uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
1556f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger            uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
1566f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger            uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) override;
1576f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x,
1586f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger            uirenderer::CanvasPropertyPrimitive* y, uirenderer::CanvasPropertyPrimitive* radius,
1596f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger            uirenderer::CanvasPropertyPaint* paint) override;
1606f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger
1616f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override;
1626f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override;
163cd1c3eba69d044b551cededad75474038f919890John Reck    virtual void callDrawGLFunction(Functor* functor,
164cd1c3eba69d044b551cededad75474038f919890John Reck            uirenderer::GlFunctorLifecycleListener* listener) override;
1656f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger
16679abbf22d4f672208327546661e694d837f564a9Derek Sollenbergerprotected:
16779abbf22d4f672208327546661e694d837f564a9Derek Sollenberger    virtual void drawGlyphs(const uint16_t* text, const float* positions, int count,
16879abbf22d4f672208327546661e694d837f564a9Derek Sollenberger            const SkPaint& paint, float x, float y,
16979abbf22d4f672208327546661e694d837f564a9Derek Sollenberger            float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
17079abbf22d4f672208327546661e694d837f564a9Derek Sollenberger            float totalAdvance) override;
17179abbf22d4f672208327546661e694d837f564a9Derek Sollenberger    virtual void drawGlyphsOnPath(const uint16_t* glyphs, int count, const SkPath& path,
17279abbf22d4f672208327546661e694d837f564a9Derek Sollenberger            float hOffset, float vOffset, const SkPaint& paint) override;
17379abbf22d4f672208327546661e694d837f564a9Derek Sollenberger
1748872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerprivate:
1758872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    struct SaveRec {
176eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita        int              saveCount;
177eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita        SaveFlags::Flags saveFlags;
1788872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    };
1798872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1806578a989566e585eee053095dc80e2552e125db2Derek Sollenberger    bool mHighContrastText = false;
1816578a989566e585eee053095dc80e2552e125db2Derek Sollenberger
182eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    void recordPartialSave(SaveFlags::Flags flags);
1838872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    void saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, int frameSaveCount);
1848872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    void applyClips(const SkTArray<SkClipStack::Element>& clips);
1858872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
186acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenberger    void drawPoints(const float* points, int count, const SkPaint& paint,
1878872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                    SkCanvas::PointMode mode);
1888872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1898872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkAutoTUnref<SkCanvas> mCanvas;
19060126efd7d905ca24822765c6dafac17fef278abBen Wagner    std::unique_ptr<SkDeque> mSaveStack; // lazily allocated, tracks partial saves.
1918872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger};
1928872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
193c1b33d665c8caf5760f68c45c6ca0baa649b832aJohn ReckCanvas* Canvas::create_canvas(const SkBitmap& bitmap) {
1948872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return new SkiaCanvas(bitmap);
1958872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
1968872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1978872b38ef403cc2c44aca07d392f5e9426fd7f54Derek SollenbergerCanvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
1988872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return new SkiaCanvas(skiaCanvas);
1998872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2008872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
201c1b33d665c8caf5760f68c45c6ca0baa649b832aJohn ReckSkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
202c1b33d665c8caf5760f68c45c6ca0baa649b832aJohn Reck    mCanvas.reset(new SkCanvas(bitmap));
2038872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2048872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2058872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
2068872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// Canvas state operations: Replace Bitmap
2078872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
2088872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2098872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerclass ClipCopier : public SkCanvas::ClipVisitor {
2108872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerpublic:
2118872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {}
2128872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2138872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    virtual void clipRect(const SkRect& rect, SkRegion::Op op, bool antialias) {
2148872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        m_dstCanvas->clipRect(rect, op, antialias);
2158872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
2168872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    virtual void clipRRect(const SkRRect& rrect, SkRegion::Op op, bool antialias) {
2178872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        m_dstCanvas->clipRRect(rrect, op, antialias);
2188872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
2198872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    virtual void clipPath(const SkPath& path, SkRegion::Op op, bool antialias) {
2208872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        m_dstCanvas->clipPath(path, op, antialias);
2218872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
2228872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2238872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerprivate:
2248872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkCanvas* m_dstCanvas;
2258872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger};
2268872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
227c1b33d665c8caf5760f68c45c6ca0baa649b832aJohn Reckvoid SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
228c1b33d665c8caf5760f68c45c6ca0baa649b832aJohn Reck    SkCanvas* newCanvas = new SkCanvas(bitmap);
2298872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
230c1b33d665c8caf5760f68c45c6ca0baa649b832aJohn Reck    if (!bitmap.isNull()) {
2318872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        // Copy the canvas matrix & clip state.
2328872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        newCanvas->setMatrix(mCanvas->getTotalMatrix());
233f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III
234f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III        ClipCopier copier(newCanvas);
235f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III        mCanvas->replayClips(&copier);
2368872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
2378872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2388872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    // unrefs the existing canvas
2398872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas.reset(newCanvas);
2408872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2418872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    // clean up the old save stack
2428872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mSaveStack.reset(NULL);
2438872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2448872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2458872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
2468872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// Canvas state operations
2478872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
2488872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2498872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerbool SkiaCanvas::isOpaque() {
250f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III    return mCanvas->imageInfo().isOpaque();
2518872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2528872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2538872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerint SkiaCanvas::width() {
254f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III    return mCanvas->imageInfo().width();
2558872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2568872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2578872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerint SkiaCanvas::height() {
258f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III    return mCanvas->imageInfo().height();
2598872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2608872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2618872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
2628872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// Canvas state operations: Save (layer)
2638872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
2648872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2658872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerint SkiaCanvas::getSaveCount() const {
2668872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return mCanvas->getSaveCount();
2678872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2688872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
269eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malitaint SkiaCanvas::save(SaveFlags::Flags flags) {
2708872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    int count = mCanvas->save();
2718872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    recordPartialSave(flags);
2728872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return count;
2738872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2748872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2755e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita// The SkiaCanvas::restore operation layers on the capability to preserve
2765e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita// either (or both) the matrix and/or clip state after a SkCanvas::restore
2775e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita// operation. It does this by explicitly saving off the clip & matrix state
2785e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita// when requested and playing it back after the SkCanvas::restore.
2798872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::restore() {
2808872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const SaveRec* rec = (NULL == mSaveStack.get())
2818872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            ? NULL
2828872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            : static_cast<SaveRec*>(mSaveStack->back());
2835e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita    int currentSaveCount = mCanvas->getSaveCount();
2848872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkASSERT(NULL == rec || currentSaveCount >= rec->saveCount);
2858872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2868872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (NULL == rec || rec->saveCount != currentSaveCount) {
2878872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        // Fast path - no record for this frame.
2888872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        mCanvas->restore();
2898872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        return;
2908872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
2918872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
292eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
293eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    bool preserveClip   = !(rec->saveFlags & SaveFlags::Clip);
2948872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2958872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkMatrix savedMatrix;
2968872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (preserveMatrix) {
2978872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        savedMatrix = mCanvas->getTotalMatrix();
2988872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
2998872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3008872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkTArray<SkClipStack::Element> savedClips;
3015e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita    int topClipStackFrame = mCanvas->getClipStack()->getSaveCount();
3028872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (preserveClip) {
3035e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita        saveClipsForFrame(savedClips, topClipStackFrame);
3048872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
3058872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3068872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->restore();
3078872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3088872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (preserveMatrix) {
3098872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        mCanvas->setMatrix(savedMatrix);
3108872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
3118872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3125e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita    if (preserveClip && !savedClips.empty() &&
3135e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita        topClipStackFrame != mCanvas->getClipStack()->getSaveCount()) {
3145e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita        // Only reapply the saved clips if the top clip stack frame was actually
3155e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita        // popped by restore().  If it wasn't, it means it doesn't belong to the
3165e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita        // restored canvas frame (SkCanvas lazy save/restore kicked in).
3178872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        applyClips(savedClips);
3188872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
3198872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3208872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mSaveStack->pop_back();
3218872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
3228872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3238872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::restoreToCount(int restoreCount) {
3248872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    while (mCanvas->getSaveCount() > restoreCount) {
3258872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        this->restore();
3268872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
3278872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
3288872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
329eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malitastatic inline SkCanvas::SaveLayerFlags layerFlags(SaveFlags::Flags flags) {
330eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    SkCanvas::SaveLayerFlags layerFlags = 0;
331eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita
332eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    if (!(flags & SaveFlags::HasAlphaLayer)) {
333eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita        layerFlags |= SkCanvas::kIsOpaque_SaveLayerFlag;
334eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    }
335eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita
336eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    if (!(flags & SaveFlags::ClipToLayer)) {
337eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita        layerFlags |= SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
338eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    }
339eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita
340eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    return layerFlags;
341eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita}
342eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita
3438872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerint SkiaCanvas::saveLayer(float left, float top, float right, float bottom,
344eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita            const SkPaint* paint, SaveFlags::Flags flags) {
345eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
346eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags));
347eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita
348eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    int count = mCanvas->saveLayer(rec);
3498872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    recordPartialSave(flags);
3508872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return count;
3518872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
3528872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3538872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerint SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom,
354eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita        int alpha, SaveFlags::Flags flags) {
355eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    SkTLazy<SkPaint> alphaPaint;
356eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    if (static_cast<unsigned>(alpha) < 0xFF) {
357eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita        alphaPaint.init()->setAlpha(alpha);
358eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    }
359eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita
360eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    return this->saveLayer(left, top, right, bottom, alphaPaint.getMaybeNull(),
361eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita                           flags);
3628872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
3638872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3648872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
3658872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
3668872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
3678872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
368eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malitavoid SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
3698872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    // A partial save is a save operation which doesn't capture the full canvas state.
370eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
3718872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3728872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    // Mask-out non canvas state bits.
373eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    flags &= SaveFlags::MatrixClip;
3748872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
375eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    if (flags == SaveFlags::MatrixClip) {
3768872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        // not a partial save.
3778872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        return;
3788872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
3798872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3808872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (NULL == mSaveStack.get()) {
381d1cbc1608906302130158acc4c72c82c89b49e10Ben Wagner        mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
3828872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
3838872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3848872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
3855e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita    rec->saveCount = mCanvas->getSaveCount();
3868872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    rec->saveFlags = flags;
3878872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
3888872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3895e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malitavoid SkiaCanvas::saveClipsForFrame(SkTArray<SkClipStack::Element>& clips,
3905e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita                                   int saveCountToBackup) {
3915e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita    // Each SkClipStack::Element stores the index of the canvas save
3925e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita    // with which it is associated. Backup only those Elements that
3935e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita    // are associated with 'saveCountToBackup'
3948872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkClipStack::Iter clipIterator(*mCanvas->getClipStack(),
3958872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                                   SkClipStack::Iter::kTop_IterStart);
3965e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita    while (const SkClipStack::Element* elem = clipIterator.prev()) {
3975e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita        if (elem->getSaveCount() < saveCountToBackup) {
3985e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita            // done with the target save count.
3998872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            break;
4008872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        }
4015e27140f48a1ec0ae3890dca84bfa91bd32ecb3bFlorin Malita        SkASSERT(elem->getSaveCount() == saveCountToBackup);
4028872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        clips.push_back(*elem);
4038872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
4048872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4058872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4068872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::applyClips(const SkTArray<SkClipStack::Element>& clips) {
4078872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    ClipCopier clipCopier(mCanvas);
4088872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4098872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    // The clip stack stores clips in device space.
4108872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkMatrix origMatrix = mCanvas->getTotalMatrix();
4118872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->resetMatrix();
4128872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4138872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    // We pushed the clips in reverse order.
4148872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    for (int i = clips.count() - 1; i >= 0; --i) {
4158872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        clips[i].replay(&clipCopier);
4168872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
4178872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4188872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->setMatrix(origMatrix);
4198872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4208872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4218872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
4228872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// Canvas state operations: Matrix
4238872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
4248872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4258872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
4268872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    *outMatrix = mCanvas->getTotalMatrix();
4278872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4288872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4298872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::setMatrix(const SkMatrix& matrix) {
4308872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->setMatrix(matrix);
4318872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4328872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4338872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::concat(const SkMatrix& matrix) {
4348872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->concat(matrix);
4358872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4368872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4378872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::rotate(float degrees) {
4388872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->rotate(degrees);
4398872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4408872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4418872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::scale(float sx, float sy) {
4428872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->scale(sx, sy);
4438872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4448872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4458872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::skew(float sx, float sy) {
4468872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->skew(sx, sy);
4478872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4488872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4498872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::translate(float dx, float dy) {
4508872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->translate(dx, dy);
4518872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4528872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4538872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
4548872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// Canvas state operations: Clips
4558872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
4568872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4578872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// This function is a mirror of SkCanvas::getClipBounds except that it does
4588872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// not outset the edge of the clip to account for anti-aliasing. There is
4598872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// a skia bug to investigate pushing this logic into back into skia.
4608872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// (see https://code.google.com/p/skia/issues/detail?id=1303)
4618872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerbool SkiaCanvas::getClipBounds(SkRect* outRect) const {
4628872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkIRect ibounds;
4638872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (!mCanvas->getClipDeviceBounds(&ibounds)) {
4648872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        return false;
4658872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
4668872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4678872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkMatrix inverse;
4688872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    // if we can't invert the CTM, we can't return local clip bounds
4698872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (!mCanvas->getTotalMatrix().invert(&inverse)) {
4708872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        if (outRect) {
4718872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            outRect->setEmpty();
4728872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        }
4738872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        return false;
4748872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
4758872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4768872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (NULL != outRect) {
4778872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        SkRect r = SkRect::Make(ibounds);
4788872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        inverse.mapRect(outRect, r);
4798872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
4808872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return true;
4818872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4828872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4838872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerbool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
4848872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
4858872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return mCanvas->quickReject(bounds);
4868872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4878872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4888872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerbool SkiaCanvas::quickRejectPath(const SkPath& path) const {
4898872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return mCanvas->quickReject(path);
4908872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4918872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4928872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerbool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
4938872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
4948872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->clipRect(rect, op);
4955ec6a2878b2ad933c5da6fe2341c854155acc24cChris Craik    return !mCanvas->isClipEmpty();
4968872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4978872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4988872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerbool SkiaCanvas::clipPath(const SkPath* path, SkRegion::Op op) {
4998872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->clipPath(*path, op);
5005ec6a2878b2ad933c5da6fe2341c854155acc24cChris Craik    return !mCanvas->isClipEmpty();
5018872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5028872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5038872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerbool SkiaCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) {
5048872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkPath rgnPath;
5058872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (region->getBoundaryPath(&rgnPath)) {
5068872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        // The region is specified in device space.
5078872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        SkMatrix savedMatrix = mCanvas->getTotalMatrix();
5088872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        mCanvas->resetMatrix();
5098872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        mCanvas->clipPath(rgnPath, op);
5108872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        mCanvas->setMatrix(savedMatrix);
5118872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    } else {
5128872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        mCanvas->clipRect(SkRect::MakeEmpty(), op);
5138872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
5145ec6a2878b2ad933c5da6fe2341c854155acc24cChris Craik    return !mCanvas->isClipEmpty();
5158872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5168872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5178872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
5188872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// Canvas state operations: Filters
5198872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
5208872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
521acb4099deff72c2c631f7110a405a3331d3e8b27Derek SollenbergerSkDrawFilter* SkiaCanvas::getDrawFilter() {
522acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenberger    return mCanvas->getDrawFilter();
523acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenberger}
524acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenberger
5258872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
5268872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->setDrawFilter(drawFilter);
5278872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5288872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5298872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
5308872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// Canvas draw operations
5318872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
5328872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5338872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::drawColor(int color, SkXfermode::Mode mode) {
5348872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->drawColor(color, mode);
5358872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5368872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
537acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenbergervoid SkiaCanvas::drawPaint(const SkPaint& paint) {
5388872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->drawPaint(paint);
5398872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5408872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5418872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
5428872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// Canvas draw operations: Geometry
5438872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
5448872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
545acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenbergervoid SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
5468872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                            SkCanvas::PointMode mode) {
5478872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    // convert the floats into SkPoints
5488872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    count >>= 1;    // now it is the number of points
5496bbf68d05a9deb335fc693b4a64651aea1b4e9e0Ben Wagner    std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
5508872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    for (int i = 0; i < count; i++) {
5518872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        pts[i].set(points[0], points[1]);
5528872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        points += 2;
5538872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
5546bbf68d05a9deb335fc693b4a64651aea1b4e9e0Ben Wagner    mCanvas->drawPoints(mode, count, pts.get(), paint);
5558872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5568872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5578872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
558acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenbergervoid SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
5598872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->drawPoint(x, y, paint);
5608872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5618872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
562acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenbergervoid SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
5638872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
5648872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5658872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5668872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
567acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenberger                          const SkPaint& paint) {
5688872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->drawLine(startX, startY, stopX, stopY, paint);
5698872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5708872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
571acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenbergervoid SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
5728872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
5738872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5748872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5758872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::drawRect(float left, float top, float right, float bottom,
576acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenberger        const SkPaint& paint) {
5778872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->drawRectCoords(left, top, right, bottom, paint);
5788872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5798872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5808872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
58194394b3fb048d5349a77b57950ab7f6b6e92ce34Derek Sollenbergervoid SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
58294394b3fb048d5349a77b57950ab7f6b6e92ce34Derek Sollenberger    SkRegion::Iterator it(region);
58394394b3fb048d5349a77b57950ab7f6b6e92ce34Derek Sollenberger    while (!it.done()) {
58494394b3fb048d5349a77b57950ab7f6b6e92ce34Derek Sollenberger        mCanvas->drawRect(SkRect::Make(it.rect()), paint);
58594394b3fb048d5349a77b57950ab7f6b6e92ce34Derek Sollenberger        it.next();
58694394b3fb048d5349a77b57950ab7f6b6e92ce34Derek Sollenberger    }
58794394b3fb048d5349a77b57950ab7f6b6e92ce34Derek Sollenberger}
58894394b3fb048d5349a77b57950ab7f6b6e92ce34Derek Sollenberger
5898872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom,
590acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenberger        float rx, float ry, const SkPaint& paint) {
5918872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
5928872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->drawRoundRect(rect, rx, ry, paint);
5938872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5948872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
595acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenbergervoid SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
5968872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->drawCircle(x, y, radius, paint);
5978872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5988872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
599acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenbergervoid SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
6008872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
6018872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->drawOval(oval, paint);
6028872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
6038872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
6048872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::drawArc(float left, float top, float right, float bottom,
605acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenberger        float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
6068872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
6078872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
6088872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
6098872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
610acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenbergervoid SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
6118872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->drawPath(path, paint);
6128872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
6138872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
6148872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
6158872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                              const float* verts, const float* texs, const int* colors,
616acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenberger                              const uint16_t* indices, int indexCount, const SkPaint& paint) {
6178872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger#ifndef SK_SCALAR_IS_FLOAT
6188872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
6198872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger#endif
6208872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const int ptCount = vertexCount >> 1;
6218872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->drawVertices(vertexMode, ptCount, (SkPoint*)verts, (SkPoint*)texs,
6228872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                          (SkColor*)colors, NULL, indices, indexCount, paint);
6238872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
6248872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
6258872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
6268872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// Canvas draw operations: Bitmaps
6278872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
6288872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
629acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenbergervoid SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) {
6308872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->drawBitmap(bitmap, left, top, paint);
6318872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
6328872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
633acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenbergervoid SkiaCanvas::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
63470ffbf9e8288296ef7009c8297bcb6da6a7f73b2Mike Reed    SkAutoCanvasRestore acr(mCanvas, true);
63570ffbf9e8288296ef7009c8297bcb6da6a7f73b2Mike Reed    mCanvas->concat(matrix);
63670ffbf9e8288296ef7009c8297bcb6da6a7f73b2Mike Reed    mCanvas->drawBitmap(bitmap, 0, 0, paint);
6378872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
6388872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
6398872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
6408872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                            float srcRight, float srcBottom, float dstLeft, float dstTop,
641acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenberger                            float dstRight, float dstBottom, const SkPaint* paint) {
6428872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
6438872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
644f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III    mCanvas->drawBitmapRect(bitmap, srcRect, dstRect, paint);
6458872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
6468872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
6478872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergervoid SkiaCanvas::drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
648acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenberger        const float* vertices, const int* colors, const SkPaint* paint) {
6498872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
6508872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const int ptCount = (meshWidth + 1) * (meshHeight + 1);
6518872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const int indexCount = meshWidth * meshHeight * 6;
6528872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
6538872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    /*  Our temp storage holds 2 or 3 arrays.
6548872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        texture points [ptCount * sizeof(SkPoint)]
6558872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        optionally vertex points [ptCount * sizeof(SkPoint)] if we need a
6568872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            copy to convert from float to fixed
6578872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        indices [ptCount * sizeof(uint16_t)]
6588872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    */
6598872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    ssize_t storageSize = ptCount * sizeof(SkPoint); // texs[]
6608872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    storageSize += indexCount * sizeof(uint16_t);  // indices[]
6618872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
6628872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
6638872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger#ifndef SK_SCALAR_IS_FLOAT
6648872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
6658872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger#endif
6666bbf68d05a9deb335fc693b4a64651aea1b4e9e0Ben Wagner    std::unique_ptr<char[]> storage(new char[storageSize]);
6678872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkPoint* texs = (SkPoint*)storage.get();
6688872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    uint16_t* indices = (uint16_t*)(texs + ptCount);
6698872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
6708872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    // cons up texture coordinates and indices
6718872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    {
6728872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        const SkScalar w = SkIntToScalar(bitmap.width());
6738872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        const SkScalar h = SkIntToScalar(bitmap.height());
6748872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        const SkScalar dx = w / meshWidth;
6758872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        const SkScalar dy = h / meshHeight;
6768872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
6778872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        SkPoint* texsPtr = texs;
6788872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        SkScalar y = 0;
6798872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        for (int i = 0; i <= meshHeight; i++) {
6808872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            if (i == meshHeight) {
6818872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                y = h;  // to ensure numerically we hit h exactly
6828872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            }
6838872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            SkScalar x = 0;
6848872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            for (int j = 0; j < meshWidth; j++) {
6858872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                texsPtr->set(x, y);
6868872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                texsPtr += 1;
6878872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                x += dx;
6888872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            }
6898872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            texsPtr->set(w, y);
6908872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            texsPtr += 1;
6918872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            y += dy;
6928872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        }
6938872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        SkASSERT(texsPtr - texs == ptCount);
6948872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
6958872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
6968872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    // cons up indices
6978872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    {
6988872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        uint16_t* indexPtr = indices;
6998872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        int index = 0;
7008872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        for (int i = 0; i < meshHeight; i++) {
7018872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            for (int j = 0; j < meshWidth; j++) {
7028872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                // lower-left triangle
7038872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                *indexPtr++ = index;
7048872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                *indexPtr++ = index + meshWidth + 1;
7058872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                *indexPtr++ = index + meshWidth + 2;
7068872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                // upper-right triangle
7078872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                *indexPtr++ = index;
7088872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                *indexPtr++ = index + meshWidth + 2;
7098872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                *indexPtr++ = index + 1;
7108872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                // bump to the next cell
7118872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                index += 1;
7128872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            }
7138872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            // bump to the next row
7148872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            index += 1;
7158872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        }
7168872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        SkASSERT(indexPtr - indices == indexCount);
7178872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        SkASSERT((char*)indexPtr - (char*)storage.get() == storageSize);
7188872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
7198872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
7208872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    // double-check that we have legal indices
7218872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger#ifdef SK_DEBUG
7228872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    {
7238872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        for (int i = 0; i < indexCount; i++) {
7248872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
7258872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        }
7268872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
7278872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger#endif
7288872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
7298872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    // cons-up a shader for the bitmap
730acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenberger    SkPaint tmpPaint;
7318872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (paint) {
7328872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        tmpPaint = *paint;
7338872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
7348872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkShader* shader = SkShader::CreateBitmapShader(bitmap,
7358872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                                                    SkShader::kClamp_TileMode,
7368872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                                                    SkShader::kClamp_TileMode);
7378872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkSafeUnref(tmpPaint.setShader(shader));
7388872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
7398872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    mCanvas->drawVertices(SkCanvas::kTriangles_VertexMode, ptCount, (SkPoint*)vertices,
7408872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                         texs, (const SkColor*)colors, NULL, indices,
7418872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                         indexCount, tmpPaint);
7428872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
7438872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
744edca320a2b42011f98c308fdf25fc0494c6a5454Derek Sollenbergervoid SkiaCanvas::drawNinePatch(const SkBitmap& bitmap, const Res_png_9patch& chunk,
745edca320a2b42011f98c308fdf25fc0494c6a5454Derek Sollenberger        float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) {
746edca320a2b42011f98c308fdf25fc0494c6a5454Derek Sollenberger    SkRect bounds = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
747edca320a2b42011f98c308fdf25fc0494c6a5454Derek Sollenberger    NinePatch::Draw(mCanvas, bounds, bitmap, chunk, paint, nullptr);
748edca320a2b42011f98c308fdf25fc0494c6a5454Derek Sollenberger}
749edca320a2b42011f98c308fdf25fc0494c6a5454Derek Sollenberger
750766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liuvoid SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
7511d8e194661085f9a18ab1b3cd12f9e19d3a86be5Doris Liu    vectorDrawable->drawStaging(this);
752766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu}
753766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu
7548872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
7558872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// Canvas draw operations: Text
7568872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// ----------------------------------------------------------------------------
7578872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
758dccca44ffda4836b56a21da95a046c9708ffd49csergeyvvoid SkiaCanvas::drawGlyphs(const uint16_t* text, const float* positions, int count,
759acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenberger        const SkPaint& paint, float x, float y,
7608dfaa4904205772cdceee63ef3989bcdedf1a914Tom Hudson        float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
7618dfaa4904205772cdceee63ef3989bcdedf1a914Tom Hudson        float totalAdvance) {
762e3a40ea488c7cfa396d5901255719a6ddab791d4Ben Wagner    static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
76379abbf22d4f672208327546661e694d837f564a9Derek Sollenberger    mCanvas->drawPosText(text, count << 1, reinterpret_cast<const SkPoint*>(positions), paint);
764a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik    drawTextDecorations(x, y, totalAdvance, paint);
7658872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
7668872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
767dccca44ffda4836b56a21da95a046c9708ffd49csergeyvvoid SkiaCanvas::drawGlyphsOnPath(const uint16_t* glyphs, int count, const SkPath& path,
768acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenberger        float hOffset, float vOffset, const SkPaint& paint) {
76934e79c1e570673148e3e0bbd91df3180a00eeff1Tom Hudson    mCanvas->drawTextOnPathHV(glyphs, count << 1, path, hOffset, vOffset, paint);
7708872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
7718872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
7726f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger// ----------------------------------------------------------------------------
7736f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger// Canvas draw operations: Animations
7746f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger// ----------------------------------------------------------------------------
7756f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger
7766f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenbergerclass AnimatedRoundRect : public SkDrawable {
7776f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger public:
7786f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    AnimatedRoundRect(uirenderer::CanvasPropertyPrimitive* left,
7796f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger            uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
7806f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger            uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
7816f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger            uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* p) :
7826f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger            mLeft(left), mTop(top), mRight(right), mBottom(bottom), mRx(rx), mRy(ry), mPaint(p) {}
7836f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger
7846f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger protected:
7856f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger     virtual SkRect onGetBounds() override {
7866f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger         return SkRect::MakeLTRB(mLeft->value, mTop->value, mRight->value, mBottom->value);
7876f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger     }
7886f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger     virtual void onDraw(SkCanvas* canvas) override {
7896f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger         SkRect rect = SkRect::MakeLTRB(mLeft->value, mTop->value, mRight->value, mBottom->value);
7906f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger         canvas->drawRoundRect(rect, mRx->value, mRy->value, mPaint->value);
7916f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger     }
7926f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger
7936f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger private:
7946f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    sp<uirenderer::CanvasPropertyPrimitive> mLeft;
7956f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    sp<uirenderer::CanvasPropertyPrimitive> mTop;
7966f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    sp<uirenderer::CanvasPropertyPrimitive> mRight;
7976f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    sp<uirenderer::CanvasPropertyPrimitive> mBottom;
7986f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    sp<uirenderer::CanvasPropertyPrimitive> mRx;
7996f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    sp<uirenderer::CanvasPropertyPrimitive> mRy;
8006f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    sp<uirenderer::CanvasPropertyPaint> mPaint;
8016f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger};
8026f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger
8036f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenbergerclass AnimatedCircle : public SkDrawable {
8046f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger public:
8056f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    AnimatedCircle(uirenderer::CanvasPropertyPrimitive* x, uirenderer::CanvasPropertyPrimitive* y,
8066f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger            uirenderer::CanvasPropertyPrimitive* radius, uirenderer::CanvasPropertyPaint* paint) :
8076f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger            mX(x), mY(y), mRadius(radius), mPaint(paint) {}
8086f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger
8096f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger protected:
8106f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger     virtual SkRect onGetBounds() override {
8116f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger         const float x = mX->value;
8126f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger         const float y = mY->value;
8136f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger         const float radius = mRadius->value;
8146f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger         return SkRect::MakeLTRB(x - radius, y - radius, x + radius, y + radius);
8156f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger     }
8166f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger     virtual void onDraw(SkCanvas* canvas) override {
8176f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger         canvas->drawCircle(mX->value, mY->value, mRadius->value, mPaint->value);
8186f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger     }
8196f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger
8206f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger private:
8216f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    sp<uirenderer::CanvasPropertyPrimitive> mX;
8226f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    sp<uirenderer::CanvasPropertyPrimitive> mY;
8236f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    sp<uirenderer::CanvasPropertyPrimitive> mRadius;
8246f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    sp<uirenderer::CanvasPropertyPaint> mPaint;
8256f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger};
8266f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger
8276f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenbergervoid SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
8286f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger        uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
8296f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger        uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
8306f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger        uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) {
8316f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    SkAutoTUnref<AnimatedRoundRect> drawable(
8326f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger            new AnimatedRoundRect(left, top, right, bottom, rx, ry, paint));
8336f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    mCanvas->drawDrawable(drawable.get());
8346f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger}
8356f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger
8366f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenbergervoid SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x, uirenderer::CanvasPropertyPrimitive* y,
8376f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger        uirenderer::CanvasPropertyPrimitive* radius, uirenderer::CanvasPropertyPaint* paint) {
8386f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    SkAutoTUnref<AnimatedCircle> drawable(new AnimatedCircle(x, y, radius, paint));
8396f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    mCanvas->drawDrawable(drawable.get());
8406f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger}
8416f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger
8426f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger// ----------------------------------------------------------------------------
8436f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger// Canvas draw operations: View System
8446f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger// ----------------------------------------------------------------------------
8456f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger
8466f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenbergervoid SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layer) { }
8476f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger
8486f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenbergervoid SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) { }
8496f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger
850cd1c3eba69d044b551cededad75474038f919890John Reckvoid SkiaCanvas::callDrawGLFunction(Functor* functor,
851cd1c3eba69d044b551cededad75474038f919890John Reck        uirenderer::GlFunctorLifecycleListener* listener) { }
8526f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger
8538872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger} // namespace android
854