1c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik/*
2c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik * Copyright (C) 2013 The Android Open Source Project
3c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik *
4c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik * Licensed under the Apache License, Version 2.0 (the "License");
5c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik * you may not use this file except in compliance with the License.
6c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik * You may obtain a copy of the License at
7c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik *
8c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik *      http://www.apache.org/licenses/LICENSE-2.0
9c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik *
10c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik * Unless required by applicable law or agreed to in writing, software
11c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik * distributed under the License is distributed on an "AS IS" BASIS,
12c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik * See the License for the specific language governing permissions and
14c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik * limitations under the License.
15c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik */
16c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
17c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik#include <utils/Trace.h>
1828ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik#include <ui/Rect.h>
1928ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik#include <ui/Region.h>
20c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
21cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy#include "Caches.h"
22c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik#include "Debug.h"
23527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik#include "DeferredDisplayList.h"
24c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik#include "DisplayListOp.h"
25c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik#include "OpenGLRenderer.h"
262507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik#include "Properties.h"
27deeda3d337aed1eee218b89a7aba5992ced371f0Chris Craik#include "utils/MathUtils.h"
28c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
29c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik#if DEBUG_DEFER
30c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik    #define DEFER_LOGD(...) ALOGD(__VA_ARGS__)
31c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik#else
32c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik    #define DEFER_LOGD(...)
33c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik#endif
34c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
35c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craiknamespace android {
36c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craiknamespace uirenderer {
37c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
381ed30c907ffbb71d1bc448ee57c66b0d16f600efChris Craik// Depth of the save stack at the beginning of batch playback at flush time
391ed30c907ffbb71d1bc448ee57c66b0d16f600efChris Craik#define FLUSH_SAVE_STACK_DEPTH 2
401ed30c907ffbb71d1bc448ee57c66b0d16f600efChris Craik
41527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik#define DEBUG_COLOR_BARRIER          0x1f000000
42527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik#define DEBUG_COLOR_MERGEDBATCH      0x5f7f7fff
43527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik#define DEBUG_COLOR_MERGEDBATCH_SOLO 0x5f7fff7f
44527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
45b45c6aa665624013ef3b207fffcfe265041f6bffChris Craikstatic bool avoidOverdraw() {
46b45c6aa665624013ef3b207fffcfe265041f6bffChris Craik    // Don't avoid overdraw when visualizing it, since that makes it harder to
47b45c6aa665624013ef3b207fffcfe265041f6bffChris Craik    // debug where it's coming from, and when the problem occurs.
48b45c6aa665624013ef3b207fffcfe265041f6bffChris Craik    return !Properties::debugOverdraw;
49b45c6aa665624013ef3b207fffcfe265041f6bffChris Craik};
50b45c6aa665624013ef3b207fffcfe265041f6bffChris Craik
51ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik/////////////////////////////////////////////////////////////////////////////////
52ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik// Operation Batches
53ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik/////////////////////////////////////////////////////////////////////////////////
54ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
55527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craikclass Batch {
56c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craikpublic:
57107843de4507b3511006cb9c77b8d0364374385aTom Hudson    virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) = 0;
58527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    virtual ~Batch() {}
5928ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    virtual bool purelyDrawBatch() { return false; }
6064bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe    virtual bool coversBounds(const Rect& bounds) { return false; }
61527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik};
62527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
63527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craikclass DrawBatch : public Batch {
64527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craikpublic:
6528ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    DrawBatch(const DeferInfo& deferInfo) : mAllOpsOpaque(true),
6628ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik            mBatchId(deferInfo.batchId), mMergeId(deferInfo.mergeId) {
67527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik        mOps.clear();
68527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    }
69c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
70527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    virtual ~DrawBatch() { mOps.clear(); }
71c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
72c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    virtual void add(DrawOp* op, const DeferredDisplayState* state, bool opaqueOverBounds) {
73c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik        // NOTE: ignore empty bounds special case, since we don't merge across those ops
74c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        mBounds.unionWith(state->mBounds);
7528ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik        mAllOpsOpaque &= opaqueOverBounds;
76272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck        mOps.push_back(OpStatePair(op, state));
77c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik    }
78c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
79c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    bool intersects(const Rect& rect) {
80c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik        if (!rect.intersects(mBounds)) return false;
81ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
82c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik        for (unsigned int i = 0; i < mOps.size(); i++) {
83c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik            if (rect.intersects(mOps[i].state->mBounds)) {
84c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik#if DEBUG_DEFER
85c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik                DEFER_LOGD("op intersects with op %p with bounds %f %f %f %f:", mOps[i].op,
86c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik                        mOps[i].state->mBounds.left, mOps[i].state->mBounds.top,
87c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik                        mOps[i].state->mBounds.right, mOps[i].state->mBounds.bottom);
88c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik                mOps[i].op->output(2);
89c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik#endif
90c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik                return true;
91c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik            }
92c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik        }
93c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik        return false;
94c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik    }
95c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
96d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) override {
9741541825bc90dac740e424cdd41a8c997e15cdb7Chris Craik        DEFER_LOGD("%d  replaying DrawBatch %p, with %d ops (batch id %x, merge id %p)",
9841541825bc90dac740e424cdd41a8c997e15cdb7Chris Craik                index, this, mOps.size(), getBatchId(), getMergeId());
99ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
100ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        for (unsigned int i = 0; i < mOps.size(); i++) {
101c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik            DrawOp* op = mOps[i].op;
102c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik            const DeferredDisplayState* state = mOps[i].state;
103c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik            renderer.restoreDisplayState(*state);
104ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
105ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
106d90144db52c7297879b950cbbc85137ed123ab5bChris Craik            renderer.eventMark(op->name());
107ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik#endif
108107843de4507b3511006cb9c77b8d0364374385aTom Hudson            op->applyDraw(renderer, dirty);
109527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
110527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik#if DEBUG_MERGE_BEHAVIOR
111c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik            const Rect& bounds = state->mBounds;
112527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik            int batchColor = 0x1f000000;
113527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik            if (getBatchId() & 0x1) batchColor |= 0x0000ff;
114527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik            if (getBatchId() & 0x2) batchColor |= 0x00ff00;
115527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik            if (getBatchId() & 0x4) batchColor |= 0xff0000;
116527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik            renderer.drawScreenSpaceColorRect(bounds.left, bounds.top, bounds.right, bounds.bottom,
117527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik                    batchColor);
118527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik#endif
119ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        }
120ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    }
121ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
122d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual bool purelyDrawBatch() override { return true; }
12328ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
124d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual bool coversBounds(const Rect& bounds) override {
12528ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik        if (CC_LIKELY(!mAllOpsOpaque || !mBounds.contains(bounds) || count() == 1)) return false;
12628ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
12728ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik        Region uncovered(android::Rect(bounds.left, bounds.top, bounds.right, bounds.bottom));
12828ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik        for (unsigned int i = 0; i < mOps.size(); i++) {
129c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik            const Rect &r = mOps[i].state->mBounds;
13028ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik            uncovered.subtractSelf(android::Rect(r.left, r.top, r.right, r.bottom));
13128ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik        }
13228ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik        return uncovered.isEmpty();
13328ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    }
13428ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
135527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    inline int getBatchId() const { return mBatchId; }
136527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    inline mergeid_t getMergeId() const { return mMergeId; }
137ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    inline int count() const { return mOps.size(); }
138527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
139527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craikprotected:
140272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck    std::vector<OpStatePair> mOps;
14128ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    Rect mBounds; // union of bounds of contained ops
142527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craikprivate:
14328ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    bool mAllOpsOpaque;
144527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    int mBatchId;
145527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    mergeid_t mMergeId;
146c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik};
147c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
148527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craikclass MergingDrawBatch : public DrawBatch {
149ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikpublic:
1500e87f00f8cb79635aa70b9a2dfa02bf19dc7473dChris Craik    MergingDrawBatch(DeferInfo& deferInfo, int width, int height) :
1510e87f00f8cb79635aa70b9a2dfa02bf19dc7473dChris Craik            DrawBatch(deferInfo), mClipRect(width, height),
1520e87f00f8cb79635aa70b9a2dfa02bf19dc7473dChris Craik            mClipSideFlags(kClipSide_None) {}
153a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik
154a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik    /*
155a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik     * Helper for determining if a new op can merge with a MergingDrawBatch based on their bounds
156a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik     * and clip side flags. Positive bounds delta means new bounds fit in old.
157a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik     */
158a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik    static inline bool checkSide(const int currentFlags, const int newFlags, const int side,
159a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik            float boundsDelta) {
160a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik        bool currentClipExists = currentFlags & side;
161a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik        bool newClipExists = newFlags & side;
162a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik
163a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik        // if current is clipped, we must be able to fit new bounds in current
164a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik        if (boundsDelta > 0 && currentClipExists) return false;
165a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik
166a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik        // if new is clipped, we must be able to fit current bounds in new
167a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik        if (boundsDelta < 0 && newClipExists) return false;
168a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik
169a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik        return true;
170a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik    }
171ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
172527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    /*
173527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     * Checks if a (mergeable) op can be merged into this batch
174527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     *
175527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     * If true, the op's multiDraw must be guaranteed to handle both ops simultaneously, so it is
176527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     * important to consider all paint attributes used in the draw calls in deciding both a) if an
177a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik     * op tries to merge at all, and b) if the op can merge with another set of ops
178527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     *
179527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     * False positives can lead to information from the paints of subsequent merged operations being
180527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     * dropped, so we make simplifying qualifications on the ops that can merge, per op type.
181527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     */
182c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    bool canMergeWith(const DrawOp* op, const DeferredDisplayState* state) {
183527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik        bool isTextBatch = getBatchId() == DeferredDisplayList::kOpBatch_Text ||
184527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik                getBatchId() == DeferredDisplayList::kOpBatch_ColorText;
185527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
186527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik        // Overlapping other operations is only allowed for text without shadow. For other ops,
187527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik        // multiDraw isn't guaranteed to overdraw correctly
188c29a0a4664a4b9871fadd668b632469a0db240b9Derek Sollenberger        if (!isTextBatch || op->hasTextShadow()) {
189c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik            if (intersects(state->mBounds)) return false;
190527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik        }
191c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        const DeferredDisplayState* lhs = state;
192c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        const DeferredDisplayState* rhs = mOps[0].state;
193527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
194deeda3d337aed1eee218b89a7aba5992ced371f0Chris Craik        if (!MathUtils::areEqual(lhs->mAlpha, rhs->mAlpha)) return false;
195deeda3d337aed1eee218b89a7aba5992ced371f0Chris Craik
196deeda3d337aed1eee218b89a7aba5992ced371f0Chris Craik        // Identical round rect clip state means both ops will clip in the same way, or not at all.
197deeda3d337aed1eee218b89a7aba5992ced371f0Chris Craik        // As the state objects are const, we can compare their pointers to determine mergeability
198deeda3d337aed1eee218b89a7aba5992ced371f0Chris Craik        if (lhs->mRoundRectClipState != rhs->mRoundRectClipState) return false;
199fca52b7583d1e5f5ff8ed06554875d2a30ef56faChris Craik        if (lhs->mProjectionPathMask != rhs->mProjectionPathMask) return false;
200527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
201a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik        /* Clipping compatibility check
202a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik         *
203a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik         * Exploits the fact that if a op or batch is clipped on a side, its bounds will equal its
204a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik         * clip for that side.
205a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik         */
206a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik        const int currentFlags = mClipSideFlags;
207c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        const int newFlags = state->mClipSideFlags;
208a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik        if (currentFlags != kClipSide_None || newFlags != kClipSide_None) {
209c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik            const Rect& opBounds = state->mBounds;
210a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik            float boundsDelta = mBounds.left - opBounds.left;
211a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik            if (!checkSide(currentFlags, newFlags, kClipSide_Left, boundsDelta)) return false;
212a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik            boundsDelta = mBounds.top - opBounds.top;
213a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik            if (!checkSide(currentFlags, newFlags, kClipSide_Top, boundsDelta)) return false;
214a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik
215a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik            // right and bottom delta calculation reversed to account for direction
216a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik            boundsDelta = opBounds.right - mBounds.right;
217a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik            if (!checkSide(currentFlags, newFlags, kClipSide_Right, boundsDelta)) return false;
218a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik            boundsDelta = opBounds.bottom - mBounds.bottom;
219a02c4ed885d97e516f844ddb0a96083f1b45b4cbChris Craik            if (!checkSide(currentFlags, newFlags, kClipSide_Bottom, boundsDelta)) return false;
22028ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik        }
22128ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
222527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik        // if paints are equal, then modifiers + paint attribs don't need to be compared
223c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        if (op->mPaint == mOps[0].op->mPaint) return true;
224527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
225bf6f0f260886a04a1680c7f9917124a751322ca4Chris Craik        if (PaintUtils::getAlphaDirect(op->mPaint)
226bf6f0f260886a04a1680c7f9917124a751322ca4Chris Craik                != PaintUtils::getAlphaDirect(mOps[0].op->mPaint)) {
227bf6f0f260886a04a1680c7f9917124a751322ca4Chris Craik            return false;
228bf6f0f260886a04a1680c7f9917124a751322ca4Chris Craik        }
229527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
23076d3a1b8d035d27bc80b0f2fc480a903bd001514Derek Sollenberger        if (op->mPaint && mOps[0].op->mPaint &&
23176d3a1b8d035d27bc80b0f2fc480a903bd001514Derek Sollenberger            op->mPaint->getColorFilter() != mOps[0].op->mPaint->getColorFilter()) {
23276d3a1b8d035d27bc80b0f2fc480a903bd001514Derek Sollenberger            return false;
23376d3a1b8d035d27bc80b0f2fc480a903bd001514Derek Sollenberger        }
23476d3a1b8d035d27bc80b0f2fc480a903bd001514Derek Sollenberger
235d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III        if (op->mPaint && mOps[0].op->mPaint &&
236d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III            op->mPaint->getShader() != mOps[0].op->mPaint->getShader()) {
237d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III            return false;
238d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III        }
239d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III
240527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik        return true;
241527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    }
242527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
243d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void add(DrawOp* op, const DeferredDisplayState* state,
244d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik            bool opaqueOverBounds) override {
245c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        DrawBatch::add(op, state, opaqueOverBounds);
24628ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
247c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        const int newClipSideFlags = state->mClipSideFlags;
24828ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik        mClipSideFlags |= newClipSideFlags;
249c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        if (newClipSideFlags & kClipSide_Left) mClipRect.left = state->mClip.left;
250c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        if (newClipSideFlags & kClipSide_Top) mClipRect.top = state->mClip.top;
251c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        if (newClipSideFlags & kClipSide_Right) mClipRect.right = state->mClip.right;
252c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        if (newClipSideFlags & kClipSide_Bottom) mClipRect.bottom = state->mClip.bottom;
25328ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    }
25428ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
255d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) override {
25628ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik        DEFER_LOGD("%d  replaying MergingDrawBatch %p, with %d ops,"
25728ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik                " clip flags %x (batch id %x, merge id %p)",
25828ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik                index, this, mOps.size(), mClipSideFlags, getBatchId(), getMergeId());
259527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik        if (mOps.size() == 1) {
260107843de4507b3511006cb9c77b8d0364374385aTom Hudson            DrawBatch::replay(renderer, dirty, -1);
261107843de4507b3511006cb9c77b8d0364374385aTom Hudson            return;
262527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik        }
263527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
26428ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik        // clipping in the merged case is done ahead of time since all ops share the clip (if any)
265d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        renderer.setupMergedMultiDraw(mClipSideFlags ? &mClipRect : nullptr);
26628ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
267c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        DrawOp* op = mOps[0].op;
268d965bc5823d878a3fd056b8a95fb4eb578ed3fe4Chris Craik#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
269d965bc5823d878a3fd056b8a95fb4eb578ed3fe4Chris Craik        renderer.eventMark("multiDraw");
270d965bc5823d878a3fd056b8a95fb4eb578ed3fe4Chris Craik        renderer.eventMark(op->name());
271d965bc5823d878a3fd056b8a95fb4eb578ed3fe4Chris Craik#endif
272107843de4507b3511006cb9c77b8d0364374385aTom Hudson        op->multiDraw(renderer, dirty, mOps, mBounds);
273527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
274527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik#if DEBUG_MERGE_BEHAVIOR
275527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik        renderer.drawScreenSpaceColorRect(mBounds.left, mBounds.top, mBounds.right, mBounds.bottom,
276527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik                DEBUG_COLOR_MERGEDBATCH);
277527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik#endif
278ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    }
27928ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
28028ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craikprivate:
28128ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    /*
28228ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik     * Contains the effective clip rect shared by all merged ops. Initialized to the layer viewport,
28328ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik     * it will shrink if an op must be clipped on a certain side. The clipped sides are reflected in
28428ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik     * mClipSideFlags.
28528ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik     */
28628ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    Rect mClipRect;
28728ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    int mClipSideFlags;
288527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik};
289ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
290527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craikclass StateOpBatch : public Batch {
291527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craikpublic:
292527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    // creates a single operation batch
293c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    StateOpBatch(const StateOp* op, const DeferredDisplayState* state) : mOp(op), mState(state) {}
294527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
295d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) override {
296ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        DEFER_LOGD("replaying state op batch %p", this);
297c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        renderer.restoreDisplayState(*mState);
298ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
299ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        // use invalid save count because it won't be used at flush time - RestoreToCountOp is the
300ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        // only one to use it, and we don't use that class at flush time, instead calling
301ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        // renderer.restoreToCount directly
302ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        int saveCount = -1;
303ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        mOp->applyState(renderer, saveCount);
304ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    }
305ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
306ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikprivate:
3077273daace9303f4662444111c40bb83d3ead4a92Chris Craik    const StateOp* mOp;
308c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    const DeferredDisplayState* mState;
309ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik};
310ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
311527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craikclass RestoreToCountBatch : public Batch {
312ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikpublic:
31364bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe    RestoreToCountBatch(const StateOp* op, const DeferredDisplayState* state, int restoreCount) :
31464bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe            mState(state), mRestoreCount(restoreCount) {}
315ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
316d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) override {
317ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        DEFER_LOGD("batch %p restoring to count %d", this, mRestoreCount);
3187273daace9303f4662444111c40bb83d3ead4a92Chris Craik
319c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        renderer.restoreDisplayState(*mState);
320ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        renderer.restoreToCount(mRestoreCount);
321ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    }
322ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
323ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikprivate:
3247273daace9303f4662444111c40bb83d3ead4a92Chris Craik    // we use the state storage for the RestoreToCountOp, but don't replay the op itself
325c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    const DeferredDisplayState* mState;
326c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik
327ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    /*
328ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik     * The count used here represents the flush() time saveCount. This is as opposed to the
329ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik     * DisplayList record time, or defer() time values (which are RestoreToCountOp's mCount, and
330ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik     * (saveCount + mCount) respectively). Since the count is different from the original
331ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik     * RestoreToCountOp, we don't store a pointer to the op, as elsewhere.
332ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik     */
333ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    const int mRestoreCount;
334ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik};
335ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
336527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik#if DEBUG_MERGE_BEHAVIOR
337527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craikclass BarrierDebugBatch : public Batch {
338107843de4507b3511006cb9c77b8d0364374385aTom Hudson    virtual void replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
339527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik        renderer.drawScreenSpaceColorRect(0, 0, 10000, 10000, DEBUG_COLOR_BARRIER);
340527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    }
341527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik};
342527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik#endif
343527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
344ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik/////////////////////////////////////////////////////////////////////////////////
345ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik// DeferredDisplayList
346ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik/////////////////////////////////////////////////////////////////////////////////
347ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
348ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikvoid DeferredDisplayList::resetBatchingState() {
349c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik    for (int i = 0; i < kOpBatch_Count; i++) {
350d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        mBatchLookup[i] = nullptr;
351527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik        mMergingBatches[i].clear();
352527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    }
353527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik#if DEBUG_MERGE_BEHAVIOR
354527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    if (mBatches.size() != 0) {
355527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik        mBatches.add(new BarrierDebugBatch());
356c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik    }
357527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik#endif
358527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    mEarliestBatchIndex = mBatches.size();
359ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik}
360ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
361ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikvoid DeferredDisplayList::clear() {
362ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    resetBatchingState();
363ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    mComplexClipStackStart = -1;
364ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
365c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik    for (unsigned int i = 0; i < mBatches.size(); i++) {
366c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik        delete mBatches[i];
367c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik    }
368c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik    mBatches.clear();
369ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    mSaveStack.clear();
370527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    mEarliestBatchIndex = 0;
37128ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    mEarliestUnclearedIndex = 0;
372c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik}
373c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
374ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik/////////////////////////////////////////////////////////////////////////////////
375ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik// Operation adding
376ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik/////////////////////////////////////////////////////////////////////////////////
377ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
378ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikint DeferredDisplayList::getStateOpDeferFlags() const {
379ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    // For both clipOp and save(Layer)Op, we don't want to save drawing info, and only want to save
380ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    // the clip if we aren't recording a complex clip (and can thus trust it to be a rect)
381ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    return recordingComplexClip() ? 0 : kStateDeferFlag_Clip;
382ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik}
383ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
384ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikint DeferredDisplayList::getDrawOpDeferFlags() const {
385ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    return kStateDeferFlag_Draw | getStateOpDeferFlags();
386ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik}
387ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
388ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik/**
389ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * When an clipping operation occurs that could cause a complex clip, record the operation and all
390ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * subsequent clipOps, save/restores (if the clip flag is set). During a flush, instead of loading
391ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * the clip from deferred state, we play back all of the relevant state operations that generated
392ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * the complex clip.
393ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik *
394ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * Note that we don't need to record the associated restore operation, since operations at defer
395ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * time record whether they should store the renderer's current clip
396ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik */
397ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikvoid DeferredDisplayList::addClip(OpenGLRenderer& renderer, ClipOp* op) {
398ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    if (recordingComplexClip() || op->canCauseComplexClip() || !renderer.hasRectToRectTransform()) {
399ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        DEFER_LOGD("%p Received complex clip operation %p", this, op);
400ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
401ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        // NOTE: defer clip op before setting mComplexClipStackStart so previous clip is recorded
402ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        storeStateOpBarrier(renderer, op);
403ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
404ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        if (!recordingComplexClip()) {
405ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik            mComplexClipStackStart = renderer.getSaveCount() - 1;
406ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik            DEFER_LOGD("    Starting complex clip region, start is %d", mComplexClipStackStart);
407c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik        }
408ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    }
409ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik}
410ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
411ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik/**
412ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * For now, we record save layer operations as barriers in the batch list, preventing drawing
413ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * operations from reordering around the saveLayer and it's associated restore()
414ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik *
415ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * In the future, we should send saveLayer commands (if they can be played out of order) and their
416ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * contained drawing operations to a seperate list of batches, so that they may draw at the
417ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * beginning of the frame. This would avoid targetting and removing an FBO in the middle of a frame.
418ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik *
419ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * saveLayer operations should be pulled to the beginning of the frame if the canvas doesn't have a
420eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita * complex clip, and if the flags (SaveFlags::Clip & SaveFlags::ClipToLayer) are set.
421ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik */
422ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikvoid DeferredDisplayList::addSaveLayer(OpenGLRenderer& renderer,
423ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        SaveLayerOp* op, int newSaveCount) {
424ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    DEFER_LOGD("%p adding saveLayerOp %p, flags %x, new count %d",
425ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik            this, op, op->getFlags(), newSaveCount);
426ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
427ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    storeStateOpBarrier(renderer, op);
428272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck    mSaveStack.push_back(newSaveCount);
429ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik}
430ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
431ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik/**
432ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * Takes save op and it's return value - the new save count - and stores it into the stream as a
433ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * barrier if it's needed to properly modify a complex clip
434ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik */
435ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikvoid DeferredDisplayList::addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount) {
436ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    int saveFlags = op->getFlags();
437ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    DEFER_LOGD("%p adding saveOp %p, flags %x, new count %d", this, op, saveFlags, newSaveCount);
438ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
439eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    if (recordingComplexClip() && (saveFlags & SaveFlags::Clip)) {
440ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        // store and replay the save operation, as it may be needed to correctly playback the clip
441ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        DEFER_LOGD("    adding save barrier with new save count %d", newSaveCount);
442ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        storeStateOpBarrier(renderer, op);
443272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck        mSaveStack.push_back(newSaveCount);
444ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    }
445ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik}
446ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
447ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik/**
448ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * saveLayer() commands must be associated with a restoreToCount batch that will clean up and draw
449ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * the layer in the deferred list
450ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik *
451ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * other save() commands which occur as children of a snapshot with complex clip will be deferred,
452ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * and must be restored
453ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik *
454ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * Either will act as a barrier to draw operation reordering, as we want to play back layer
455ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik * save/restore and complex canvas modifications (including save/restore) in order.
456ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik */
4577273daace9303f4662444111c40bb83d3ead4a92Chris Craikvoid DeferredDisplayList::addRestoreToCount(OpenGLRenderer& renderer, StateOp* op,
4587273daace9303f4662444111c40bb83d3ead4a92Chris Craik        int newSaveCount) {
459ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    DEFER_LOGD("%p addRestoreToCount %d", this, newSaveCount);
460ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
461ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    if (recordingComplexClip() && newSaveCount <= mComplexClipStackStart) {
462ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        mComplexClipStackStart = -1;
463ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        resetBatchingState();
464ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    }
465ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
466272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck    if (mSaveStack.empty() || newSaveCount > mSaveStack.back()) {
467ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik        return;
468ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    }
469ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
470272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck    while (!mSaveStack.empty() && mSaveStack.back() >= newSaveCount) mSaveStack.pop_back();
471ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
4721ed30c907ffbb71d1bc448ee57c66b0d16f600efChris Craik    storeRestoreToCountBarrier(renderer, op, mSaveStack.size() + FLUSH_SAVE_STACK_DEPTH);
473ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik}
474ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
475ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikvoid DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) {
476c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    /* 1: op calculates local bounds */
477c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    DeferredDisplayState* const state = createState();
4783b20251a355c88193c439f928a84ae69483fb488John Reck    if (op->getLocalBounds(state->mBounds)) {
479c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        if (state->mBounds.isEmpty()) {
480c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik            // valid empty bounds, don't bother deferring
481c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik            tryRecycleState(state);
482c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik            return;
483c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        }
484c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    } else {
485c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik        state->mBounds.setEmpty();
486c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    }
487c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik
488