1b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik/*
2b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * Copyright (C) 2015 The Android Open Source Project
3b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik *
4b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * Licensed under the Apache License, Version 2.0 (the "License");
5b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * you may not use this file except in compliance with the License.
6b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * You may obtain a copy of the License at
7b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik *
8b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik *      http://www.apache.org/licenses/LICENSE-2.0
9b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik *
10b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * Unless required by applicable law or agreed to in writing, software
11b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * distributed under the License is distributed on an "AS IS" BASIS,
12b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * See the License for the specific language governing permissions and
14b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik * limitations under the License.
15b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik */
16b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
17b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "RecordingCanvas.h"
18b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
19d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik#include "DeferredLayerUpdater.h"
20b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "RecordedOp.h"
21b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "RenderNode.h"
22766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu#include "VectorDrawable.h"
23b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
24b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craiknamespace android {
25b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craiknamespace uirenderer {
26b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
27b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris CraikRecordingCanvas::RecordingCanvas(size_t width, size_t height)
28b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        : mState(*this)
29b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        , mResourceCache(ResourceCache::getInstance()) {
306f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    resetRecording(width, height);
31b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
32b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
33b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris CraikRecordingCanvas::~RecordingCanvas() {
34003cc3dec8e2a92e51086fbcd5ee1bb236efa701Chris Craik    LOG_ALWAYS_FATAL_IF(mDisplayList,
35b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            "Destroyed a RecordingCanvas during a record!");
36b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
37b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
386f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenbergervoid RecordingCanvas::resetRecording(int width, int height) {
39003cc3dec8e2a92e51086fbcd5ee1bb236efa701Chris Craik    LOG_ALWAYS_FATAL_IF(mDisplayList,
40b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            "prepareDirty called a second time during a recording!");
41003cc3dec8e2a92e51086fbcd5ee1bb236efa701Chris Craik    mDisplayList = new DisplayList();
42b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
43e4db79de127cfe961195f52907af8451026eaa20Chris Craik    mState.initializeRecordingSaveStack(width, height);
44b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
45161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    mDeferredBarrierType = DeferredBarrierType::InOrder;
46b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mState.setDirtyClip(false);
47b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
48b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
49003cc3dec8e2a92e51086fbcd5ee1bb236efa701Chris CraikDisplayList* RecordingCanvas::finishRecording() {
50b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    restoreToCount(1);
51b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mPaintMap.clear();
52b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mRegionMap.clear();
53b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mPathMap.clear();
54003cc3dec8e2a92e51086fbcd5ee1bb236efa701Chris Craik    DisplayList* displayList = mDisplayList;
55003cc3dec8e2a92e51086fbcd5ee1bb236efa701Chris Craik    mDisplayList = nullptr;
56b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mSkiaCanvasProxy.reset(nullptr);
57003cc3dec8e2a92e51086fbcd5ee1bb236efa701Chris Craik    return displayList;
58b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
59b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
60d645640180c25c2711e99aa82ec629155f8e91baChris Craikvoid RecordingCanvas::insertReorderBarrier(bool enableReorder) {
61d645640180c25c2711e99aa82ec629155f8e91baChris Craik    if (enableReorder) {
62d645640180c25c2711e99aa82ec629155f8e91baChris Craik        mDeferredBarrierType = DeferredBarrierType::OutOfOrder;
63d645640180c25c2711e99aa82ec629155f8e91baChris Craik        mDeferredBarrierClip = getRecordedClip();
64d645640180c25c2711e99aa82ec629155f8e91baChris Craik    } else {
65d645640180c25c2711e99aa82ec629155f8e91baChris Craik        mDeferredBarrierType = DeferredBarrierType::InOrder;
66d645640180c25c2711e99aa82ec629155f8e91baChris Craik        mDeferredBarrierClip = nullptr;
67d645640180c25c2711e99aa82ec629155f8e91baChris Craik    }
68d645640180c25c2711e99aa82ec629155f8e91baChris Craik}
69d645640180c25c2711e99aa82ec629155f8e91baChris Craik
70b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris CraikSkCanvas* RecordingCanvas::asSkCanvas() {
71003cc3dec8e2a92e51086fbcd5ee1bb236efa701Chris Craik    LOG_ALWAYS_FATAL_IF(!mDisplayList,
72b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            "attempting to get an SkCanvas when we are not recording!");
73b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    if (!mSkiaCanvasProxy) {
74b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        mSkiaCanvasProxy.reset(new SkiaCanvasProxy(this));
75b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
76b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
77b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    // SkCanvas instances default to identity transform, but should inherit
78b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    // the state of this Canvas; if this code was in the SkiaCanvasProxy
79b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    // constructor, we couldn't cache mSkiaCanvasProxy.
80b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    SkMatrix parentTransform;
81b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    getMatrix(&parentTransform);
82b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mSkiaCanvasProxy.get()->setMatrix(parentTransform);
83b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
84b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    return mSkiaCanvasProxy.get();
85b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
86b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
87b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
886fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik// CanvasStateClient implementation
896fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik// ----------------------------------------------------------------------------
906fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
916fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craikvoid RecordingCanvas::onViewportInitialized() {
926fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik}
936fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
946fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craikvoid RecordingCanvas::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
956fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    if (removed.flags & Snapshot::kFlagIsFboLayer) {
967df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck        addOp(alloc().create_trivial<EndLayerOp>());
97b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    } else if (removed.flags & Snapshot::kFlagIsLayer) {
987df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck        addOp(alloc().create_trivial<EndUnclippedLayerOp>());
996fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    }
1006fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik}
1016fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
1026fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik// ----------------------------------------------------------------------------
103b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// android/graphics/Canvas state operations
104b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
105b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// Save (layer)
106eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malitaint RecordingCanvas::save(SaveFlags::Flags flags) {
107b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    return mState.save((int) flags);
108b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
109b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
110b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::RecordingCanvas::restore() {
111b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mState.restore();
112b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
113b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
114b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::restoreToCount(int saveCount) {
115b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mState.restoreToCount(saveCount);
116b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
117b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
118b87eadda1818034ce03d85f30388384d1ac65916Chris Craikint RecordingCanvas::saveLayer(float left, float top, float right, float bottom,
119eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita        const SkPaint* paint, SaveFlags::Flags flags) {
1206fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    // force matrix/clip isolation for layer
121eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    flags |= SaveFlags::MatrixClip;
122eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    bool clippedLayer = flags & SaveFlags::ClipToLayer;
1236fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
1246fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    const Snapshot& previous = *mState.currentSnapshot();
1256fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
1266fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    // initialize the snapshot as though it almost represents an FBO layer so deferred draw
1276fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    // operations will be able to store and restore the current clip and transform info, and
1286fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    // quick rejection will be correct (for display lists)
1296fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
130b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    const Rect unmappedBounds(left, top, right, bottom);
1316fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
1326fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    // determine clipped bounds relative to previous viewport.
133b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    Rect visibleBounds = unmappedBounds;
1346fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    previous.transform->mapRect(visibleBounds);
1356fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
136b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    if (CC_UNLIKELY(!clippedLayer
137b87eadda1818034ce03d85f30388384d1ac65916Chris Craik            && previous.transform->rectToRect()
138b87eadda1818034ce03d85f30388384d1ac65916Chris Craik            && visibleBounds.contains(previous.getRenderTargetClip()))) {
139b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        // unlikely case where an unclipped savelayer is recorded with a clip it can use,
140b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        // as none of its unaffected/unclipped area is visible
141b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        clippedLayer = true;
142eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita        flags |= SaveFlags::ClipToLayer;
143b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    }
1446fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
1456fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    visibleBounds.doIntersect(previous.getRenderTargetClip());
1466fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    visibleBounds.snapToPixelBoundaries();
147b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    visibleBounds.doIntersect(Rect(previous.getViewportWidth(), previous.getViewportHeight()));
1486fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
1496fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    // Map visible bounds back to layer space, and intersect with parameter bounds
1506fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    Rect layerBounds = visibleBounds;
1516fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    Matrix4 inverse;
1526fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    inverse.loadInverse(*previous.transform);
1536fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    inverse.mapRect(layerBounds);
154b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    layerBounds.doIntersect(unmappedBounds);
1556fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
1566fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    int saveValue = mState.save((int) flags);
1576fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    Snapshot& snapshot = *mState.writableSnapshot();
1586fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
159b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    // layerBounds is in original bounds space, but clipped by current recording clip
160b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    if (layerBounds.isEmpty() || unmappedBounds.isEmpty()) {
1616fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik        // Don't bother recording layer, since it's been rejected
162b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        if (CC_LIKELY(clippedLayer)) {
163b87eadda1818034ce03d85f30388384d1ac65916Chris Craik            snapshot.resetClip(0, 0, 0, 0);
164b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        }
1656fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik        return saveValue;
1666fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    }
1676fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
168b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    if (CC_LIKELY(clippedLayer)) {
169b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        auto previousClip = getRecordedClip(); // note: done before new snapshot's clip has changed
170b87eadda1818034ce03d85f30388384d1ac65916Chris Craik
171b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        snapshot.flags |= Snapshot::kFlagIsLayer | Snapshot::kFlagIsFboLayer;
172b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        snapshot.initializeViewport(unmappedBounds.getWidth(), unmappedBounds.getHeight());
173b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        snapshot.transform->loadTranslate(-unmappedBounds.left, -unmappedBounds.top, 0.0f);
174b87eadda1818034ce03d85f30388384d1ac65916Chris Craik
175b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        Rect clip = layerBounds;
176b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        clip.translate(-unmappedBounds.left, -unmappedBounds.top);
177b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        snapshot.resetClip(clip.left, clip.top, clip.right, clip.bottom);
178b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        snapshot.roundRectClipState = nullptr;
179b87eadda1818034ce03d85f30388384d1ac65916Chris Craik
1807df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck        addOp(alloc().create_trivial<BeginLayerOp>(
181b87eadda1818034ce03d85f30388384d1ac65916Chris Craik                unmappedBounds,
182b87eadda1818034ce03d85f30388384d1ac65916Chris Craik                *previous.transform, // transform to *draw* with
183b87eadda1818034ce03d85f30388384d1ac65916Chris Craik                previousClip, // clip to *draw* with
184b87eadda1818034ce03d85f30388384d1ac65916Chris Craik                refPaint(paint)));
185b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    } else {
186b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        snapshot.flags |= Snapshot::kFlagIsLayer;
187b87eadda1818034ce03d85f30388384d1ac65916Chris Craik
1887df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck        addOp(alloc().create_trivial<BeginUnclippedLayerOp>(
189b87eadda1818034ce03d85f30388384d1ac65916Chris Craik                unmappedBounds,
190b87eadda1818034ce03d85f30388384d1ac65916Chris Craik                *mState.currentSnapshot()->transform,
191b87eadda1818034ce03d85f30388384d1ac65916Chris Craik                getRecordedClip(),
192b87eadda1818034ce03d85f30388384d1ac65916Chris Craik                refPaint(paint)));
193b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    }
1946fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
1956fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    return saveValue;
196b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
197b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
198b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// Matrix
199b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::rotate(float degrees) {
200b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    if (degrees == 0) return;
201b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
202b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mState.rotate(degrees);
203b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
204b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
205b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::scale(float sx, float sy) {
206b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    if (sx == 1 && sy == 1) return;
207b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
208b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mState.scale(sx, sy);
209b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
210b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
211b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::skew(float sx, float sy) {
212b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mState.skew(sx, sy);
213b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
214b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
215b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::translate(float dx, float dy) {
216b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    if (dx == 0 && dy == 0) return;
217b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
218b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mState.translate(dx, dy, 0);
219b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
220b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
221b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// Clip
222b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikbool RecordingCanvas::getClipBounds(SkRect* outRect) const {
223e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik    *outRect = mState.getLocalClipBounds().toSkRect();
224b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    return !(outRect->isEmpty());
225b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
226b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikbool RecordingCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
227b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    return mState.quickRejectConservative(left, top, right, bottom);
228b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
229b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikbool RecordingCanvas::quickRejectPath(const SkPath& path) const {
230b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    SkRect bounds = path.getBounds();
231b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    return mState.quickRejectConservative(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
232b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
233b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikbool RecordingCanvas::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
234b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    return mState.clipRect(left, top, right, bottom, op);
235b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
236b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikbool RecordingCanvas::clipPath(const SkPath* path, SkRegion::Op op) {
237b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    return mState.clipPath(path, op);
238b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
239b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikbool RecordingCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) {
240b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    return mState.clipRegion(region, op);
241b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
242b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
243b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
244b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// android/graphics/Canvas draw operations
245b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
246b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawColor(int color, SkXfermode::Mode mode) {
247a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik    addOp(alloc().create_trivial<ColorOp>(
248a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            getRecordedClip(),
249a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            color,
250a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            mode));
251b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
252b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
253b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawPaint(const SkPaint& paint) {
2544c3980b6e43cc7c0541f54b8e7e2d9d6108be622Chris Craik    SkRect bounds;
2554c3980b6e43cc7c0541f54b8e7e2d9d6108be622Chris Craik    if (getClipBounds(&bounds)) {
2564c3980b6e43cc7c0541f54b8e7e2d9d6108be622Chris Craik        drawRect(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, paint);
2574c3980b6e43cc7c0541f54b8e7e2d9d6108be622Chris Craik    }
258b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
259b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
260386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikstatic Rect calcBoundsOfPoints(const float* points, int floatCount) {
261386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    Rect unmappedBounds(points[0], points[1], points[0], points[1]);
262386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    for (int i = 2; i < floatCount; i += 2) {
263386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        unmappedBounds.expandToCover(points[i], points[i + 1]);
264386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    }
265386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    return unmappedBounds;
266386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
267386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
268b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// Geometry
269386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikvoid RecordingCanvas::drawPoints(const float* points, int floatCount, const SkPaint& paint) {
270386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    if (floatCount < 2) return;
271386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    floatCount &= ~0x1; // round down to nearest two
272386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
2737df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<PointsOp>(
274386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            calcBoundsOfPoints(points, floatCount),
275386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            *mState.currentSnapshot()->transform,
276e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
277386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            refPaint(&paint), refBuffer<float>(points, floatCount), floatCount));
278b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
279a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik
280a1717271caac5e8ea3808c331d4141ac01a42134Chris Craikvoid RecordingCanvas::drawLines(const float* points, int floatCount, const SkPaint& paint) {
281a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik    if (floatCount < 4) return;
282a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik    floatCount &= ~0x3; // round down to nearest four
283a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik
2847df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<LinesOp>(
285386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            calcBoundsOfPoints(points, floatCount),
286a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik            *mState.currentSnapshot()->transform,
287e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
288a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik            refPaint(&paint), refBuffer<float>(points, floatCount), floatCount));
289b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
290a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik
291b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawRect(float left, float top, float right, float bottom, const SkPaint& paint) {
2927df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<RectOp>(
293b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            Rect(left, top, right, bottom),
294b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            *(mState.currentSnapshot()->transform),
295e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
296b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            refPaint(&paint)));
297b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
298b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
299b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawSimpleRects(const float* rects, int vertexCount, const SkPaint* paint) {
300b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    if (rects == nullptr) return;
301b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
3027a89600bac7ab889a5ba8a994c57d677de0e45d5Chris Craik    Vertex* rectData = (Vertex*) mDisplayList->allocator.create_trivial_array<Vertex>(vertexCount);
303b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    Vertex* vertex = rectData;
304b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
305b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    float left = FLT_MAX;
306b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    float top = FLT_MAX;
307b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    float right = FLT_MIN;
308b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    float bottom = FLT_MIN;
309b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    for (int index = 0; index < vertexCount; index += 4) {
310b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        float l = rects[index + 0];
311b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        float t = rects[index + 1];
312b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        float r = rects[index + 2];
313b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        float b = rects[index + 3];
314b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
315b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        Vertex::set(vertex++, l, t);
316b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        Vertex::set(vertex++, r, t);
317b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        Vertex::set(vertex++, l, b);
318b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        Vertex::set(vertex++, r, b);
319b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
320b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        left = std::min(left, l);
321b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        top = std::min(top, t);
322b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        right = std::max(right, r);
323b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        bottom = std::max(bottom, b);
324b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
3257df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<SimpleRectsOp>(
326b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            Rect(left, top, right, bottom),
327b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            *(mState.currentSnapshot()->transform),
328e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
329b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            refPaint(paint), rectData, vertexCount));
330b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
331b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
332b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
333b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    if (paint.getStyle() == SkPaint::kFill_Style
334b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            && (!paint.isAntiAlias() || mState.currentTransform()->isSimple())) {
335b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        int count = 0;
336b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        Vector<float> rects;
337b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        SkRegion::Iterator it(region);
338b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        while (!it.done()) {
339b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            const SkIRect& r = it.rect();
340b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            rects.push(r.fLeft);
341b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            rects.push(r.fTop);
342b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            rects.push(r.fRight);
343b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            rects.push(r.fBottom);
344b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            count += 4;
345b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            it.next();
346b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        }
347b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        drawSimpleRects(rects.array(), count, &paint);
348b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    } else {
349b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        SkRegion::Iterator it(region);
350b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        while (!it.done()) {
351b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            const SkIRect& r = it.rect();
352b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            drawRect(r.fLeft, r.fTop, r.fRight, r.fBottom, paint);
353b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            it.next();
354b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        }
355b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
356b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
357b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawRoundRect(float left, float top, float right, float bottom,
358b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float rx, float ry, const SkPaint& paint) {
3592dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik    if (CC_LIKELY(MathUtils::isPositive(rx) || MathUtils::isPositive(ry))) {
3602dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik        addOp(alloc().create_trivial<RoundRectOp>(
3612dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik                Rect(left, top, right, bottom),
3622dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik                *(mState.currentSnapshot()->transform),
3632dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik                getRecordedClip(),
3642dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik                refPaint(&paint), rx, ry));
3652dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik    } else {
3662dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik        drawRect(left, top, right, bottom, paint);
3672dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik    }
368b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
369386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
370268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craikvoid RecordingCanvas::drawRoundRect(
371268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik        CanvasPropertyPrimitive* left, CanvasPropertyPrimitive* top,
372268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik        CanvasPropertyPrimitive* right, CanvasPropertyPrimitive* bottom,
373268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik        CanvasPropertyPrimitive* rx, CanvasPropertyPrimitive* ry,
374268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik        CanvasPropertyPaint* paint) {
375268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(left);
376268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(top);
377268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(right);
378268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(bottom);
379268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(rx);
380268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(ry);
381268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(paint);
382268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    refBitmapsInShader(paint->value.getShader());
3837df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<RoundRectPropsOp>(
384268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            *(mState.currentSnapshot()->transform),
385e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
386268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            &paint->value,
387268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            &left->value, &top->value, &right->value, &bottom->value,
388268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            &rx->value, &ry->value));
389268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik}
390268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik
391b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
392268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    // TODO: move to Canvas.h
393386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    if (radius <= 0) return;
394386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    drawOval(x - radius, y - radius, x + radius, y + radius, paint);
395b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
396386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
397268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craikvoid RecordingCanvas::drawCircle(
398268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik        CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y,
399268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik        CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint) {
400268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(x);
401268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(y);
402268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(radius);
403268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(paint);
404268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    refBitmapsInShader(paint->value.getShader());
4057df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<CirclePropsOp>(
406268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            *(mState.currentSnapshot()->transform),
407e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
408268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            &paint->value,
409268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            &x->value, &y->value, &radius->value));
410268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik}
411268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik
412b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
4137df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<OvalOp>(
414386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            Rect(left, top, right, bottom),
415386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            *(mState.currentSnapshot()->transform),
416e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
417386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            refPaint(&paint)));
418b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
419386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
420b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawArc(float left, float top, float right, float bottom,
421386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
422caa24184735a607e87077c73262a42acdea7b8fbChris Craik    if (fabs(sweepAngle) >= 360.0f) {
423caa24184735a607e87077c73262a42acdea7b8fbChris Craik        drawOval(left, top, right, bottom, paint);
424caa24184735a607e87077c73262a42acdea7b8fbChris Craik    } else {
425caa24184735a607e87077c73262a42acdea7b8fbChris Craik        addOp(alloc().create_trivial<ArcOp>(
426caa24184735a607e87077c73262a42acdea7b8fbChris Craik                Rect(left, top, right, bottom),
427caa24184735a607e87077c73262a42acdea7b8fbChris Craik                *(mState.currentSnapshot()->transform),
428caa24184735a607e87077c73262a42acdea7b8fbChris Craik                getRecordedClip(),
429caa24184735a607e87077c73262a42acdea7b8fbChris Craik                refPaint(&paint),
430caa24184735a607e87077c73262a42acdea7b8fbChris Craik                startAngle, sweepAngle, useCenter));
431caa24184735a607e87077c73262a42acdea7b8fbChris Craik    }
432b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
433386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
434b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
4357df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<PathOp>(
436386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            Rect(path.getBounds()),
437386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            *(mState.currentSnapshot()->transform),
438e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
439386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            refPaint(&paint), refPath(&path)));
440b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
441b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
442766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liuvoid RecordingCanvas::drawVectorDrawable(VectorDrawableRoot* tree) {
4431d8e194661085f9a18ab1b3cd12f9e19d3a86be5Doris Liu    mDisplayList->pushStagingFunctors.push_back(tree->getFunctor());
444766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu    mDisplayList->ref(tree);
4457df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<VectorDrawableOp>(
446766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu            tree,
4471d8e194661085f9a18ab1b3cd12f9e19d3a86be5Doris Liu            Rect(tree->stagingProperties()->getBounds()),
448766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu            *(mState.currentSnapshot()->transform),
449766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu            getRecordedClip()));
450766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu}
451766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu
452b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// Bitmap-based
453b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) {
454eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    save(SaveFlags::Matrix);
455b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    translate(left, top);
456b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    drawBitmap(&bitmap, paint);
457b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    restore();
458b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
459b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
460b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
461b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik                            const SkPaint* paint) {
462b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    if (matrix.isIdentity()) {
463b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        drawBitmap(&bitmap, paint);
464b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    } else if (!(matrix.getType() & ~(SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask))
465b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            && MathUtils::isPositive(matrix.getScaleX())
466b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            && MathUtils::isPositive(matrix.getScaleY())) {
467b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        // SkMatrix::isScaleTranslate() not available in L
468b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        SkRect src;
469b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        SkRect dst;
470b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        bitmap.getBounds(&src);
471b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        matrix.mapRect(&dst, src);
472b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        drawBitmap(bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom,
473b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik                   dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint);
474b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    } else {
475eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita        save(SaveFlags::Matrix);
476b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        concat(matrix);
477b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        drawBitmap(&bitmap, paint);
478b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        restore();
479b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
480b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
481386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
482b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
483b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float srcRight, float srcBottom, float dstLeft, float dstTop,
484b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float dstRight, float dstBottom, const SkPaint* paint) {
485b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    if (srcLeft == 0 && srcTop == 0
486b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            && srcRight == bitmap.width()
487b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            && srcBottom == bitmap.height()
488b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            && (srcBottom - srcTop == dstBottom - dstTop)
489b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            && (srcRight - srcLeft == dstRight - dstLeft)) {
490b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        // transform simple rect to rect drawing case into position bitmap ops, since they merge
491eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita        save(SaveFlags::Matrix);
492b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        translate(dstLeft, dstTop);
493b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        drawBitmap(&bitmap, paint);
494b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        restore();
495b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    } else {
4967df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck        addOp(alloc().create_trivial<BitmapRectOp>(
497f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                Rect(dstLeft, dstTop, dstRight, dstBottom),
498f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                *(mState.currentSnapshot()->transform),
499e4db79de127cfe961195f52907af8451026eaa20Chris Craik                getRecordedClip(),
500f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                refPaint(paint), refBitmap(bitmap),
501f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                Rect(srcLeft, srcTop, srcRight, srcBottom)));
502b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
503b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
504386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
505b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
506b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            const float* vertices, const int* colors, const SkPaint* paint) {
507f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    int vertexCount = (meshWidth + 1) * (meshHeight + 1);
5087df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<BitmapMeshOp>(
509f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            calcBoundsOfPoints(vertices, vertexCount * 2),
510f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            *(mState.currentSnapshot()->transform),
511e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
512f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            refPaint(paint), refBitmap(bitmap), meshWidth, meshHeight,
513f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            refBuffer<float>(vertices, vertexCount * 2), // 2 floats per vertex
514f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            refBuffer<int>(colors, vertexCount))); // 1 color per vertex
515b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
516386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
517f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craikvoid RecordingCanvas::drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& patch,
518b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float dstLeft, float dstTop, float dstRight, float dstBottom,
519b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            const SkPaint* paint) {
5207df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<PatchOp>(
521f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            Rect(dstLeft, dstTop, dstRight, dstBottom),
522f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            *(mState.currentSnapshot()->transform),
523e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
524f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            refPaint(paint), refBitmap(bitmap), refPatch(&patch)));
525b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
526b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
527b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// Text
528dccca44ffda4836b56a21da95a046c9708ffd49csergeyvvoid RecordingCanvas::drawGlyphs(const uint16_t* glyphs, const float* positions, int glyphCount,
529b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            const SkPaint& paint, float x, float y, float boundsLeft, float boundsTop,
530b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float boundsRight, float boundsBottom, float totalAdvance) {
531a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik    if (!glyphs || !positions || glyphCount <= 0 || PaintUtils::paintWillNotDrawText(paint)) return;
532a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik    glyphs = refBuffer<glyph_t>(glyphs, glyphCount);
533a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik    positions = refBuffer<float>(positions, glyphCount * 2);
534a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik
53515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    // TODO: either must account for text shadow in bounds, or record separate ops for text shadows
5367df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<TextOp>(
537a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik            Rect(boundsLeft, boundsTop, boundsRight, boundsBottom),
538a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik            *(mState.currentSnapshot()->transform),
539e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
540a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik            refPaint(&paint), glyphs, positions, glyphCount, x, y));
541a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik    drawTextDecorations(x, y, totalAdvance, paint);
542b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
543a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik
544dccca44ffda4836b56a21da95a046c9708ffd49csergeyvvoid RecordingCanvas::drawGlyphsOnPath(const uint16_t* glyphs, int glyphCount, const SkPath& path,
545b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float hOffset, float vOffset, const SkPaint& paint) {
546d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    if (!glyphs || glyphCount <= 0 || PaintUtils::paintWillNotDrawText(paint)) return;
547d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    glyphs = refBuffer<glyph_t>(glyphs, glyphCount);
5487df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<TextOnPathOp>(
549d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik            *(mState.currentSnapshot()->transform),
5504c3980b6e43cc7c0541f54b8e7e2d9d6108be622Chris Craik            getRecordedClip(),
551d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik            refPaint(&paint), glyphs, glyphCount, refPath(&path), hOffset, vOffset));
552b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
553b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
554b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
5557df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<BitmapOp>(
5565430ab220b231a96b71c3e030d0303d9ce008b05Chris Craik            Rect(bitmap->width(), bitmap->height()),
557b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            *(mState.currentSnapshot()->transform),
558e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
559b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            refPaint(paint), refBitmap(*bitmap)));
560b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
561e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik
562b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawRenderNode(RenderNode* renderNode) {
56354fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik    auto&& stagingProps = renderNode->stagingProperties();
5647df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    RenderNodeOp* op = alloc().create_trivial<RenderNodeOp>(
56554fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Rect(stagingProps.getWidth(), stagingProps.getHeight()),
566b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            *(mState.currentSnapshot()->transform),
567e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
568b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            renderNode);
569b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    int opIndex = addOp(op);
5701367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik    if (CC_LIKELY(opIndex >= 0)) {
5711367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik        int childIndex = mDisplayList->addChild(op);
572b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
5731367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik        // update the chunk's child indices
5741367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik        DisplayList::Chunk& chunk = mDisplayList->chunks.back();
5751367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik        chunk.endChildIndex = childIndex + 1;
576b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
5771367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik        if (renderNode->stagingProperties().isProjectionReceiver()) {
5781367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik            // use staging property, since recording on UI thread
5791367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik            mDisplayList->projectionReceiveIndex = opIndex;
5801367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik        }
581b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
582b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
583b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
584d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craikvoid RecordingCanvas::drawLayer(DeferredLayerUpdater* layerHandle) {
585d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik    // We ref the DeferredLayerUpdater due to its thread-safe ref-counting semantics.
586d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik    mDisplayList->ref(layerHandle);
587d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik
588417ed6d4337e5409d52f58cc93677c8715193f32John Reck    // Note that the backing layer has *not* yet been updated, so don't trust
589417ed6d4337e5409d52f58cc93677c8715193f32John Reck    // its width, height, transform, etc...!
5907df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<TextureLayerOp>(
591417ed6d4337e5409d52f58cc93677c8715193f32John Reck            Rect(layerHandle->getWidth(), layerHandle->getHeight()),
592aafb01d8ade0def3f51b74ae3bbc610c4ab33044Chris Craik            *(mState.currentSnapshot()->transform),
593e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
594417ed6d4337e5409d52f58cc93677c8715193f32John Reck            layerHandle->backingLayer()));
595d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik}
596d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik
597cd1c3eba69d044b551cededad75474038f919890John Reckvoid RecordingCanvas::callDrawGLFunction(Functor* functor,
598cd1c3eba69d044b551cededad75474038f919890John Reck        GlFunctorLifecycleListener* listener) {
599cd1c3eba69d044b551cededad75474038f919890John Reck    mDisplayList->functors.push_back({functor, listener});
600cd1c3eba69d044b551cededad75474038f919890John Reck    mDisplayList->ref(listener);
6017df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<FunctorOp>(