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"
23afc221499d943386256feb9db46c119ff834bf79Yuqian Li#include "hwui/MinikinUtils.h"
24b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
25b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craiknamespace android {
26b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craiknamespace uirenderer {
27b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
28b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris CraikRecordingCanvas::RecordingCanvas(size_t width, size_t height)
29b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        : mState(*this)
30b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        , mResourceCache(ResourceCache::getInstance()) {
316f485569fa3d6047dcffd068aebf361e3598783cDerek Sollenberger    resetRecording(width, height);
32b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
33b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
34b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris CraikRecordingCanvas::~RecordingCanvas() {
35003cc3dec8e2a92e51086fbcd5ee1bb236efa701Chris Craik    LOG_ALWAYS_FATAL_IF(mDisplayList,
36b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            "Destroyed a RecordingCanvas during a record!");
37b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
38b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
39c0e7a90f1f5f98e85dbeda021fac0dff79725933Stan Ilievvoid RecordingCanvas::resetRecording(int width, int height, RenderNode* node) {
40003cc3dec8e2a92e51086fbcd5ee1bb236efa701Chris Craik    LOG_ALWAYS_FATAL_IF(mDisplayList,
41b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            "prepareDirty called a second time during a recording!");
42003cc3dec8e2a92e51086fbcd5ee1bb236efa701Chris Craik    mDisplayList = new DisplayList();
43b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
44e4db79de127cfe961195f52907af8451026eaa20Chris Craik    mState.initializeRecordingSaveStack(width, height);
45b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
46161f54b2d4160b8d3f3da9eba5746da5162e4821Chris Craik    mDeferredBarrierType = DeferredBarrierType::InOrder;
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
130e4f6d968f51ba3bb867e8e124dca97b7e9c3fea5Chris Craik    Rect unmappedBounds(left, top, right, bottom);
131e4f6d968f51ba3bb867e8e124dca97b7e9c3fea5Chris Craik    unmappedBounds.roundOut();
1326fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
1336fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    // determine clipped bounds relative to previous viewport.
134b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    Rect visibleBounds = unmappedBounds;
1356fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    previous.transform->mapRect(visibleBounds);
1366fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
137b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    if (CC_UNLIKELY(!clippedLayer
138b87eadda1818034ce03d85f30388384d1ac65916Chris Craik            && previous.transform->rectToRect()
139b87eadda1818034ce03d85f30388384d1ac65916Chris Craik            && visibleBounds.contains(previous.getRenderTargetClip()))) {
140b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        // unlikely case where an unclipped savelayer is recorded with a clip it can use,
141b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        // as none of its unaffected/unclipped area is visible
142b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        clippedLayer = true;
143eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita        flags |= SaveFlags::ClipToLayer;
144b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    }
1456fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
1466fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    visibleBounds.doIntersect(previous.getRenderTargetClip());
1476fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    visibleBounds.snapToPixelBoundaries();
148b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    visibleBounds.doIntersect(Rect(previous.getViewportWidth(), previous.getViewportHeight()));
1496fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
1506fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    // Map visible bounds back to layer space, and intersect with parameter bounds
1516fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    Rect layerBounds = visibleBounds;
1523c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik    if (CC_LIKELY(!layerBounds.isEmpty())) {
1533c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik        // if non-empty, can safely map by the inverse transform
1543c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik        Matrix4 inverse;
1553c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik        inverse.loadInverse(*previous.transform);
1563c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik        inverse.mapRect(layerBounds);
1573c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik        layerBounds.doIntersect(unmappedBounds);
1583c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik    }
1596fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
1606fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    int saveValue = mState.save((int) flags);
1616fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    Snapshot& snapshot = *mState.writableSnapshot();
1626fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
163b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    // layerBounds is in original bounds space, but clipped by current recording clip
1643c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik    if (!layerBounds.isEmpty() && !unmappedBounds.isEmpty()) {
165b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        if (CC_LIKELY(clippedLayer)) {
1663c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik            auto previousClip = getRecordedClip(); // capture before new snapshot clip has changed
1673c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik            if (addOp(alloc().create_trivial<BeginLayerOp>(
1683c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                    unmappedBounds,
1693c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                    *previous.transform, // transform to *draw* with
1703c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                    previousClip, // clip to *draw* with
1713c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                    refPaint(paint))) >= 0) {
1723c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                snapshot.flags |= Snapshot::kFlagIsLayer | Snapshot::kFlagIsFboLayer;
1733c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                snapshot.initializeViewport(unmappedBounds.getWidth(), unmappedBounds.getHeight());
1743c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                snapshot.transform->loadTranslate(-unmappedBounds.left, -unmappedBounds.top, 0.0f);
1753c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik
1763c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                Rect clip = layerBounds;
1773c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                clip.translate(-unmappedBounds.left, -unmappedBounds.top);
1783c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                snapshot.resetClip(clip.left, clip.top, clip.right, clip.bottom);
1793c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                snapshot.roundRectClipState = nullptr;
1803c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                return saveValue;
1813c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik            }
1823c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik        } else {
1833c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik            if (addOp(alloc().create_trivial<BeginUnclippedLayerOp>(
1843c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                    unmappedBounds,
1853c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                    *mState.currentSnapshot()->transform,
1863c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                    getRecordedClip(),
1873c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                    refPaint(paint))) >= 0) {
1883c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                snapshot.flags |= Snapshot::kFlagIsLayer;
1893c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik                return saveValue;
1903c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik            }
191b87eadda1818034ce03d85f30388384d1ac65916Chris Craik        }
1926fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    }
1936fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik
1943c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik    // Layer not needed, so skip recording it...
195b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    if (CC_LIKELY(clippedLayer)) {
1963c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik        // ... and set empty clip to reject inner content, if possible
1973c53ec51efd4bbc3f06cc63dd8efe186e3fb168fChris Craik        snapshot.resetClip(0, 0, 0, 0);
198b87eadda1818034ce03d85f30388384d1ac65916Chris Craik    }
1996fe991e5e76f9af9dab960100d5768d96d5f4daaChris Craik    return saveValue;
200b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
201b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
202b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// Matrix
203b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::rotate(float degrees) {
204b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    if (degrees == 0) return;
205b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
206b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mState.rotate(degrees);
207b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
208b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
209b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::scale(float sx, float sy) {
210b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    if (sx == 1 && sy == 1) return;
211b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
212b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mState.scale(sx, sy);
213b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
214b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
215b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::skew(float sx, float sy) {
216b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mState.skew(sx, sy);
217b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
218b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
219b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::translate(float dx, float dy) {
220b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    if (dx == 0 && dy == 0) return;
221b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
222b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    mState.translate(dx, dy, 0);
223b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
224b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
225b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// Clip
226b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikbool RecordingCanvas::getClipBounds(SkRect* outRect) const {
227e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik    *outRect = mState.getLocalClipBounds().toSkRect();
228b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    return !(outRect->isEmpty());
229b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
230b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikbool RecordingCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
231b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    return mState.quickRejectConservative(left, top, right, bottom);
232b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
233b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikbool RecordingCanvas::quickRejectPath(const SkPath& path) const {
234b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    SkRect bounds = path.getBounds();
235b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    return mState.quickRejectConservative(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
236b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
2376e49c9f007c879f05b035c40c0ba543c00f9d0d0Mike Reedbool RecordingCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
238b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    return mState.clipRect(left, top, right, bottom, op);
239b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
2406e49c9f007c879f05b035c40c0ba543c00f9d0d0Mike Reedbool RecordingCanvas::clipPath(const SkPath* path, SkClipOp op) {
241b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    return mState.clipPath(path, op);
242b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
243b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
244b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
245b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// android/graphics/Canvas draw operations
246b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// ----------------------------------------------------------------------------
247260ab726486317496bc12a57d599ea96dcde3284Mike Reedvoid RecordingCanvas::drawColor(int color, SkBlendMode mode) {
248a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik    addOp(alloc().create_trivial<ColorOp>(
249a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            getRecordedClip(),
250a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            color,
251a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            mode));
252b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
253b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
254b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawPaint(const SkPaint& paint) {
2554c3980b6e43cc7c0541f54b8e7e2d9d6108be622Chris Craik    SkRect bounds;
2564c3980b6e43cc7c0541f54b8e7e2d9d6108be622Chris Craik    if (getClipBounds(&bounds)) {
2574c3980b6e43cc7c0541f54b8e7e2d9d6108be622Chris Craik        drawRect(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, paint);
2584c3980b6e43cc7c0541f54b8e7e2d9d6108be622Chris Craik    }
259b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
260b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
261386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikstatic Rect calcBoundsOfPoints(const float* points, int floatCount) {
262386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    Rect unmappedBounds(points[0], points[1], points[0], points[1]);
263386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    for (int i = 2; i < floatCount; i += 2) {
264386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        unmappedBounds.expandToCover(points[i], points[i + 1]);
265386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    }
266386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    return unmappedBounds;
267386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
268386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
269b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// Geometry
270386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikvoid RecordingCanvas::drawPoints(const float* points, int floatCount, const SkPaint& paint) {
271792fb3252f6460acfd82c4e5d7536ca6b787a0e0Derek Sollenberger    if (CC_UNLIKELY(floatCount < 2 || paint.nothingToDraw())) return;
272386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    floatCount &= ~0x1; // round down to nearest two
273386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
2747df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<PointsOp>(
275386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            calcBoundsOfPoints(points, floatCount),
276386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            *mState.currentSnapshot()->transform,
277e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
278386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            refPaint(&paint), refBuffer<float>(points, floatCount), floatCount));
279b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
280a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik
281a1717271caac5e8ea3808c331d4141ac01a42134Chris Craikvoid RecordingCanvas::drawLines(const float* points, int floatCount, const SkPaint& paint) {
282792fb3252f6460acfd82c4e5d7536ca6b787a0e0Derek Sollenberger    if (CC_UNLIKELY(floatCount < 4 || paint.nothingToDraw())) return;
283a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik    floatCount &= ~0x3; // round down to nearest four
284a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik
2857df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<LinesOp>(
286386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            calcBoundsOfPoints(points, floatCount),
287a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik            *mState.currentSnapshot()->transform,
288e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
289a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik            refPaint(&paint), refBuffer<float>(points, floatCount), floatCount));
290b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
291a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik
292b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawRect(float left, float top, float right, float bottom, const SkPaint& paint) {
293792fb3252f6460acfd82c4e5d7536ca6b787a0e0Derek Sollenberger    if (CC_UNLIKELY(paint.nothingToDraw())) return;
294814ee6a9218aa339a4757b2c0ba1ad268f8dbc8aChris Craik
2957df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<RectOp>(
296b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            Rect(left, top, right, bottom),
297b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            *(mState.currentSnapshot()->transform),
298e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
299b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            refPaint(&paint)));
300b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
301b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
302b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawSimpleRects(const float* rects, int vertexCount, const SkPaint* paint) {
303b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    if (rects == nullptr) return;
304b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
3057a89600bac7ab889a5ba8a994c57d677de0e45d5Chris Craik    Vertex* rectData = (Vertex*) mDisplayList->allocator.create_trivial_array<Vertex>(vertexCount);
306b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    Vertex* vertex = rectData;
307b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
308b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    float left = FLT_MAX;
309b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    float top = FLT_MAX;
310b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    float right = FLT_MIN;
311b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    float bottom = FLT_MIN;
312b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    for (int index = 0; index < vertexCount; index += 4) {
313b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        float l = rects[index + 0];
314b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        float t = rects[index + 1];
315b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        float r = rects[index + 2];
316b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        float b = rects[index + 3];
317b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
318b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        Vertex::set(vertex++, l, t);
319b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        Vertex::set(vertex++, r, t);
320b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        Vertex::set(vertex++, l, b);
321b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        Vertex::set(vertex++, r, b);
322b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
323b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        left = std::min(left, l);
324b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        top = std::min(top, t);
325b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        right = std::max(right, r);
326b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        bottom = std::max(bottom, b);
327b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
3287df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<SimpleRectsOp>(
329b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            Rect(left, top, right, bottom),
330b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            *(mState.currentSnapshot()->transform),
331e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
332b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            refPaint(paint), rectData, vertexCount));
333b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
334b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
335b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
336792fb3252f6460acfd82c4e5d7536ca6b787a0e0Derek Sollenberger    if (CC_UNLIKELY(paint.nothingToDraw())) return;
337814ee6a9218aa339a4757b2c0ba1ad268f8dbc8aChris Craik
338b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    if (paint.getStyle() == SkPaint::kFill_Style
339b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            && (!paint.isAntiAlias() || mState.currentTransform()->isSimple())) {
340b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        int count = 0;
341b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        Vector<float> rects;
342b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        SkRegion::Iterator it(region);
343b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        while (!it.done()) {
344b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            const SkIRect& r = it.rect();
345b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            rects.push(r.fLeft);
346b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            rects.push(r.fTop);
347b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            rects.push(r.fRight);
348b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            rects.push(r.fBottom);
349b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            count += 4;
350b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            it.next();
351b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        }
352b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        drawSimpleRects(rects.array(), count, &paint);
353b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    } else {
354b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        SkRegion::Iterator it(region);
355b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        while (!it.done()) {
356b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            const SkIRect& r = it.rect();
357b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            drawRect(r.fLeft, r.fTop, r.fRight, r.fBottom, paint);
358b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            it.next();
359b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        }
360b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
361b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
362814ee6a9218aa339a4757b2c0ba1ad268f8dbc8aChris Craik
363b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawRoundRect(float left, float top, float right, float bottom,
364b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float rx, float ry, const SkPaint& paint) {
365792fb3252f6460acfd82c4e5d7536ca6b787a0e0Derek Sollenberger    if (CC_UNLIKELY(paint.nothingToDraw())) return;
366814ee6a9218aa339a4757b2c0ba1ad268f8dbc8aChris Craik
3672dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik    if (CC_LIKELY(MathUtils::isPositive(rx) || MathUtils::isPositive(ry))) {
3682dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik        addOp(alloc().create_trivial<RoundRectOp>(
3692dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik                Rect(left, top, right, bottom),
3702dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik                *(mState.currentSnapshot()->transform),
3712dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik                getRecordedClip(),
3722dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik                refPaint(&paint), rx, ry));
3732dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik    } else {
3742dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik        drawRect(left, top, right, bottom, paint);
3752dbb4c46ee648c64bb977b6839374d73b5a605d9Chris Craik    }
376b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
377386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
378268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craikvoid RecordingCanvas::drawRoundRect(
379268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik        CanvasPropertyPrimitive* left, CanvasPropertyPrimitive* top,
380268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik        CanvasPropertyPrimitive* right, CanvasPropertyPrimitive* bottom,
381268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik        CanvasPropertyPrimitive* rx, CanvasPropertyPrimitive* ry,
382268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik        CanvasPropertyPaint* paint) {
383268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(left);
384268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(top);
385268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(right);
386268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(bottom);
387268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(rx);
388268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(ry);
389268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(paint);
390268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    refBitmapsInShader(paint->value.getShader());
3917df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<RoundRectPropsOp>(
392268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            *(mState.currentSnapshot()->transform),
393e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
394268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            &paint->value,
395268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            &left->value, &top->value, &right->value, &bottom->value,
396268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            &rx->value, &ry->value));
397268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik}
398268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik
399b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
400268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    // TODO: move to Canvas.h
401792fb3252f6460acfd82c4e5d7536ca6b787a0e0Derek Sollenberger    if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
402814ee6a9218aa339a4757b2c0ba1ad268f8dbc8aChris Craik
403386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    drawOval(x - radius, y - radius, x + radius, y + radius, paint);
404b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
405386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
406268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craikvoid RecordingCanvas::drawCircle(
407268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik        CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y,
408268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik        CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint) {
409268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(x);
410268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(y);
411268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(radius);
412268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    mDisplayList->ref(paint);
413268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik    refBitmapsInShader(paint->value.getShader());
4147df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<CirclePropsOp>(
415268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            *(mState.currentSnapshot()->transform),
416e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
417268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            &paint->value,
418268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik            &x->value, &y->value, &radius->value));
419268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik}
420268a9c0f29c16a64d5819c7dbe8b0633baedab83Chris Craik
421b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
422792fb3252f6460acfd82c4e5d7536ca6b787a0e0Derek Sollenberger    if (CC_UNLIKELY(paint.nothingToDraw())) return;
423814ee6a9218aa339a4757b2c0ba1ad268f8dbc8aChris Craik
4247df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<OvalOp>(
425386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            Rect(left, top, right, bottom),
426386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            *(mState.currentSnapshot()->transform),
427e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
428386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            refPaint(&paint)));
429b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
430386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
431b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawArc(float left, float top, float right, float bottom,
432386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
433792fb3252f6460acfd82c4e5d7536ca6b787a0e0Derek Sollenberger    if (CC_UNLIKELY(paint.nothingToDraw())) return;
434814ee6a9218aa339a4757b2c0ba1ad268f8dbc8aChris Craik
435caa24184735a607e87077c73262a42acdea7b8fbChris Craik    if (fabs(sweepAngle) >= 360.0f) {
436caa24184735a607e87077c73262a42acdea7b8fbChris Craik        drawOval(left, top, right, bottom, paint);
437caa24184735a607e87077c73262a42acdea7b8fbChris Craik    } else {
438caa24184735a607e87077c73262a42acdea7b8fbChris Craik        addOp(alloc().create_trivial<ArcOp>(
439caa24184735a607e87077c73262a42acdea7b8fbChris Craik                Rect(left, top, right, bottom),
440caa24184735a607e87077c73262a42acdea7b8fbChris Craik                *(mState.currentSnapshot()->transform),
441caa24184735a607e87077c73262a42acdea7b8fbChris Craik                getRecordedClip(),
442caa24184735a607e87077c73262a42acdea7b8fbChris Craik                refPaint(&paint),
443caa24184735a607e87077c73262a42acdea7b8fbChris Craik                startAngle, sweepAngle, useCenter));
444caa24184735a607e87077c73262a42acdea7b8fbChris Craik    }
445b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
446386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
447b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
448792fb3252f6460acfd82c4e5d7536ca6b787a0e0Derek Sollenberger    if (CC_UNLIKELY(paint.nothingToDraw())) return;
449814ee6a9218aa339a4757b2c0ba1ad268f8dbc8aChris Craik
4507df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<PathOp>(
451386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            Rect(path.getBounds()),
452386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            *(mState.currentSnapshot()->transform),
453e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
454386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            refPaint(&paint), refPath(&path)));
455b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
456b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
457766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liuvoid RecordingCanvas::drawVectorDrawable(VectorDrawableRoot* tree) {
458766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu    mDisplayList->ref(tree);
459718cd3eb70703c43f29ca37907bbf0e153d8cca0Doris Liu    mDisplayList->vectorDrawables.push_back(tree);
4607df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<VectorDrawableOp>(
461766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu            tree,
4621d8e194661085f9a18ab1b3cd12f9e19d3a86be5Doris Liu            Rect(tree->stagingProperties()->getBounds()),
463766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu            *(mState.currentSnapshot()->transform),
464766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu            getRecordedClip()));
465766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu}
466766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu
467b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// Bitmap-based
468aed7f58fb05a25ce2112829e77c0eb5dd268e8a7sergeyvvoid RecordingCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) {
469eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    save(SaveFlags::Matrix);
470b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    translate(left, top);
471ec4a4b13eae2241d1613890c1c1c096bed891845sergeyv    drawBitmap(bitmap, paint);
472b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    restore();
473b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
474b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
475ec4a4b13eae2241d1613890c1c1c096bed891845sergeyvvoid RecordingCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix,
476b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik                            const SkPaint* paint) {
477b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    if (matrix.isIdentity()) {
478ec4a4b13eae2241d1613890c1c1c096bed891845sergeyv        drawBitmap(bitmap, paint);
479b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    } else if (!(matrix.getType() & ~(SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask))
480b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            && MathUtils::isPositive(matrix.getScaleX())
481b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            && MathUtils::isPositive(matrix.getScaleY())) {
482b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        // SkMatrix::isScaleTranslate() not available in L
483b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        SkRect src;
484b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        SkRect dst;
485b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        bitmap.getBounds(&src);
486b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        matrix.mapRect(&dst, src);
487ec4a4b13eae2241d1613890c1c1c096bed891845sergeyv        drawBitmap(bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom,
488b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik                   dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint);
489b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    } else {
490eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita        save(SaveFlags::Matrix);
491b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        concat(matrix);
492ec4a4b13eae2241d1613890c1c1c096bed891845sergeyv        drawBitmap(bitmap, paint);
493b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        restore();
494b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
495b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
496386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
497ec4a4b13eae2241d1613890c1c1c096bed891845sergeyvvoid RecordingCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop,
498b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float srcRight, float srcBottom, float dstLeft, float dstTop,
499b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float dstRight, float dstBottom, const SkPaint* paint) {
500b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    if (srcLeft == 0 && srcTop == 0
501b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            && srcRight == bitmap.width()
502b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            && srcBottom == bitmap.height()
503b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            && (srcBottom - srcTop == dstBottom - dstTop)
504b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            && (srcRight - srcLeft == dstRight - dstLeft)) {
505b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        // transform simple rect to rect drawing case into position bitmap ops, since they merge
506eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita        save(SaveFlags::Matrix);
507b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        translate(dstLeft, dstTop);
508ec4a4b13eae2241d1613890c1c1c096bed891845sergeyv        drawBitmap(bitmap, paint);
509b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik        restore();
510b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    } else {
5117df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck        addOp(alloc().create_trivial<BitmapRectOp>(
512f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                Rect(dstLeft, dstTop, dstRight, dstBottom),
513f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                *(mState.currentSnapshot()->transform),
514e4db79de127cfe961195f52907af8451026eaa20Chris Craik                getRecordedClip(),
515f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                refPaint(paint), refBitmap(bitmap),
516f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                Rect(srcLeft, srcTop, srcRight, srcBottom)));
517b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
518b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
519386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
520ec4a4b13eae2241d1613890c1c1c096bed891845sergeyvvoid RecordingCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
521b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            const float* vertices, const int* colors, const SkPaint* paint) {
522f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    int vertexCount = (meshWidth + 1) * (meshHeight + 1);
5237df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<BitmapMeshOp>(
524f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            calcBoundsOfPoints(vertices, vertexCount * 2),
525f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            *(mState.currentSnapshot()->transform),
526e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
527f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            refPaint(paint), refBitmap(bitmap), meshWidth, meshHeight,
528f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            refBuffer<float>(vertices, vertexCount * 2), // 2 floats per vertex
529f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            refBuffer<int>(colors, vertexCount))); // 1 color per vertex
530b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
531386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
532ec4a4b13eae2241d1613890c1c1c096bed891845sergeyvvoid RecordingCanvas::drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& patch,
533b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float dstLeft, float dstTop, float dstRight, float dstBottom,
534b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            const SkPaint* paint) {
5357df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<PatchOp>(
536f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            Rect(dstLeft, dstTop, dstRight, dstBottom),
537f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            *(mState.currentSnapshot()->transform),
538e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
539f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            refPaint(paint), refBitmap(bitmap), refPatch(&patch)));
540b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
541b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
542b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik// Text
543dccca44ffda4836b56a21da95a046c9708ffd49csergeyvvoid RecordingCanvas::drawGlyphs(const uint16_t* glyphs, const float* positions, int glyphCount,
544b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            const SkPaint& paint, float x, float y, float boundsLeft, float boundsTop,
545b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            float boundsRight, float boundsBottom, float totalAdvance) {
546792fb3252f6460acfd82c4e5d7536ca6b787a0e0Derek Sollenberger    if (!glyphs || !positions || glyphCount <= 0 || paint.nothingToDraw()) return;
547a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik    glyphs = refBuffer<glyph_t>(glyphs, glyphCount);
548a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik    positions = refBuffer<float>(positions, glyphCount * 2);
549a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik
55015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    // TODO: either must account for text shadow in bounds, or record separate ops for text shadows
5517df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<TextOp>(
552a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik            Rect(boundsLeft, boundsTop, boundsRight, boundsBottom),
553a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik            *(mState.currentSnapshot()->transform),
554e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
555a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik            refPaint(&paint), glyphs, positions, glyphCount, x, y));
556a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik    drawTextDecorations(x, y, totalAdvance, paint);
557b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
558a1717271caac5e8ea3808c331d4141ac01a42134Chris Craik
559afc221499d943386256feb9db46c119ff834bf79Yuqian Livoid RecordingCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
560afc221499d943386256feb9db46c119ff834bf79Yuqian Li        const SkPaint& paint, const SkPath& path, size_t start, size_t end) {
561afc221499d943386256feb9db46c119ff834bf79Yuqian Li    uint16_t glyphs[1];
562afc221499d943386256feb9db46c119ff834bf79Yuqian Li    for (size_t i = start; i < end; i++) {
563afc221499d943386256feb9db46c119ff834bf79Yuqian Li        glyphs[0] = layout.getGlyphId(i);
564afc221499d943386256feb9db46c119ff834bf79Yuqian Li        float x = hOffset + layout.getX(i);
565afc221499d943386256feb9db46c119ff834bf79Yuqian Li        float y = vOffset + layout.getY(i);
566792fb3252f6460acfd82c4e5d7536ca6b787a0e0Derek Sollenberger        if (paint.nothingToDraw()) return;
567afc221499d943386256feb9db46c119ff834bf79Yuqian Li        const uint16_t* tempGlyphs = refBuffer<glyph_t>(glyphs, 1);
568afc221499d943386256feb9db46c119ff834bf79Yuqian Li        addOp(alloc().create_trivial<TextOnPathOp>(
569afc221499d943386256feb9db46c119ff834bf79Yuqian Li                *(mState.currentSnapshot()->transform),
570afc221499d943386256feb9db46c119ff834bf79Yuqian Li                getRecordedClip(),
571afc221499d943386256feb9db46c119ff834bf79Yuqian Li                refPaint(&paint), tempGlyphs, 1, refPath(&path), x, y));
572afc221499d943386256feb9db46c119ff834bf79Yuqian Li    }
573b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
574b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
575ec4a4b13eae2241d1613890c1c1c096bed891845sergeyvvoid RecordingCanvas::drawBitmap(Bitmap& bitmap, const SkPaint* paint) {
5767df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    addOp(alloc().create_trivial<BitmapOp>(
577ec4a4b13eae2241d1613890c1c1c096bed891845sergeyv            Rect(bitmap.width(), bitmap.height()),
578b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            *(mState.currentSnapshot()->transform),
579e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
580ec4a4b13eae2241d1613890c1c1c096bed891845sergeyv            refPaint(paint), refBitmap(bitmap)));
581b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
582e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik
583b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craikvoid RecordingCanvas::drawRenderNode(RenderNode* renderNode) {
58454fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik    auto&& stagingProps = renderNode->stagingProperties();
5857df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck    RenderNodeOp* op = alloc().create_trivial<RenderNodeOp>(
58654fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Rect(stagingProps.getWidth(), stagingProps.getHeight()),
587b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            *(mState.currentSnapshot()->transform),
588e4db79de127cfe961195f52907af8451026eaa20Chris Craik            getRecordedClip(),
589b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik            renderNode);
590b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    int opIndex = addOp(op);
5911367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik    if (CC_LIKELY(opIndex >= 0)) {
5921367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik        int childIndex = mDisplayList->addChild(op);
593b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
5941367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik        // update the chunk's child indices
5951367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik        DisplayList::Chunk& chunk = mDisplayList->chunks.back();
5961367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik        chunk.endChildIndex = childIndex + 1;
597b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
5981367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik        if (renderNode->stagingProperties().isProjectionReceiver()) {
5991367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik            // use staging property, since recording on UI thread
6001367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik            mDisplayList->projectionReceiveIndex = opIndex;
6011367d2550ebce40f45b16dc651bc3d8d22930801Chris Craik        }
602b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik    }
603b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik}
604b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
605d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craikvoid RecordingCanvas::drawLayer(DeferredLayerUpdater* layerHandle) {
606d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik    // We ref the DeferredLayerUpdater due to its thread-safe ref-counting semantics.
607d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik    mDisplayList->ref(layerHandle);
608