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#ifndef ANDROID_HWUI_DEFERRED_DISPLAY_LIST_H
18c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik#define ANDROID_HWUI_DEFERRED_DISPLAY_LIST_H
19c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
201673035f051aa5da7e98bf7ad6acc5ae4e27a5dfSene Gales#include <unordered_map>
211673035f051aa5da7e98bf7ad6acc5ae4e27a5dfSene Gales
22c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik#include <utils/Errors.h>
23c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik#include <utils/LinearAllocator.h>
24c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
25c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik#include "Matrix.h"
26c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik#include "OpenGLRenderer.h"
27c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik#include "Rect.h"
28527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
29272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck#include <vector>
30272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck
31527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craikclass SkBitmap;
32c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
33c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craiknamespace android {
34c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craiknamespace uirenderer {
35c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
36ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikclass ClipOp;
37c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craikclass DrawOp;
38ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikclass SaveOp;
39ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikclass SaveLayerOp;
40ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craikclass StateOp;
41c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik
42c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craikclass DeferredDisplayState;
43c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
44527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craikclass Batch;
45527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craikclass DrawBatch;
46527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craikclass MergingDrawBatch;
47527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
487f6d6b0370df4b5a9e0f45bffc31ea6caeeb509dRomain Guytypedef const void* mergeid_t;
49527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
50c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craikclass DeferredDisplayState {
51c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craikpublic:
52c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    // global op bounds, mapped by mMatrix to be in screen space coordinates, clipped
53c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    Rect mBounds;
54c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik
55c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    // the below are set and used by the OpenGLRenderer at record and deferred playback
56c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    bool mClipValid;
57c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    Rect mClip;
58c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    int mClipSideFlags; // specifies which sides of the bounds are clipped, unclipped if cleared
59c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    mat4 mMatrix;
60c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    float mAlpha;
61deeda3d337aed1eee218b89a7aba5992ced371f0Chris Craik    const RoundRectClipState* mRoundRectClipState;
62fca52b7583d1e5f5ff8ed06554875d2a30ef56faChris Craik    const ProjectionPathMask* mProjectionPathMask;
63c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik};
64c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik
65c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craikclass OpStatePair {
66c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craikpublic:
67c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    OpStatePair()
68e84a208317e0ed388fcdad1e6743c7849acb51b0Chris Craik            : op(nullptr), state(nullptr) {}
69c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    OpStatePair(DrawOp* newOp, const DeferredDisplayState* newState)
70c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik            : op(newOp), state(newState) {}
71c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    OpStatePair(const OpStatePair& other)
72c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik            : op(other.op), state(other.state) {}
73c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    DrawOp* op;
74c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    const DeferredDisplayState* state;
75c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik};
76c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik
77c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craikclass DeferredDisplayList {
78edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe    friend struct DeferStateStruct; // used to give access to allocator
79c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craikpublic:
80b45c6aa665624013ef3b207fffcfe265041f6bffChris Craik    DeferredDisplayList(const Rect& bounds)
81b45c6aa665624013ef3b207fffcfe265041f6bffChris Craik            : mBounds(bounds) {
8228ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik        clear();
8328ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    }
84c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik    ~DeferredDisplayList() { clear(); }
85c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
86c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik    enum OpBatchId {
87527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik        kOpBatch_None = 0, // Don't batch
88c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik        kOpBatch_Bitmap,
89c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik        kOpBatch_Patch,
90c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik        kOpBatch_AlphaVertices,
91c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik        kOpBatch_Vertices,
92c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik        kOpBatch_AlphaMaskTexture,
93c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik        kOpBatch_Text,
94c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik        kOpBatch_ColorText,
95c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
96c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik        kOpBatch_Count, // Add other batch ids before this
97c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik    };
98c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
99272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck    bool isEmpty() { return mBatches.empty(); }
100c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
101c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik    /**
102c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik     * Plays back all of the draw ops recorded into batches to the renderer.
103c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik     * Adjusts the state of the renderer as necessary, and restores it when complete
104c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik     */
105107843de4507b3511006cb9c77b8d0364374385aTom Hudson    void flush(OpenGLRenderer& renderer, Rect& dirty);
106ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
107ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    void addClip(OpenGLRenderer& renderer, ClipOp* op);
108ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    void addSaveLayer(OpenGLRenderer& renderer, SaveLayerOp* op, int newSaveCount);
109ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    void addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount);
1107273daace9303f4662444111c40bb83d3ead4a92Chris Craik    void addRestoreToCount(OpenGLRenderer& renderer, StateOp* op, int newSaveCount);
111c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
112c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik    /**
113c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik     * Add a draw op into the DeferredDisplayList, reordering as needed (for performance) if
1145e49b307eb99269db2db257760508b8efd7bb97dChris Craik     * disallowReorder is false, respecting draw order when overlaps occur.
115c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik     */
116ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    void addDrawOp(OpenGLRenderer& renderer, DrawOp* op);
117c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
118c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craikprivate:
119f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik    DeferredDisplayList(const DeferredDisplayList& other); // disallow copy
120f57776b2d195f0937906eb88b777bb55ccc36967Chris Craik
121c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    DeferredDisplayState* createState() {
1227df9ff2a08fd4bbd9b2e734a357cffcf64675df9John Reck        return mAllocator.create_trivial<DeferredDisplayState>();
123c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    }
124c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik
125c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    void tryRecycleState(DeferredDisplayState* state) {
126b5bc454870c8b7df88a633b18c4c6499361c3a08John Reck        mAllocator.rewindIfLastAlloc(state);
127c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    }
128c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik
129d90144db52c7297879b950cbbc85137ed123ab5bChris Craik    /**
130ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik     * Resets the batching back-pointers, creating a barrier in the operation stream so that no ops
131ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik     * added in the future will be inserted into a batch that already exist.
132ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik     */
133ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    void resetBatchingState();
134ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
1351206b9bba91f7ed899c5c87427cce725fe5aadfcChris Craik    void clear();
1361206b9bba91f7ed899c5c87427cce725fe5aadfcChris Craik
137ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    void storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op);
1387273daace9303f4662444111c40bb83d3ead4a92Chris Craik    void storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op, int newSaveCount);
139ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
140ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    bool recordingComplexClip() const { return mComplexClipStackStart >= 0; }
141ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
142ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    int getStateOpDeferFlags() const;
143ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    int getDrawOpDeferFlags() const;
144ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik
145f70119cd776f871a82c94be8522dce02e04c73a8Chris Craik    void discardDrawingBatches(const unsigned int maxIndex);
14628ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
14728ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    // layer space bounds of rendering
14828ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    Rect mBounds;
14928ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
150d90144db52c7297879b950cbbc85137ed123ab5bChris Craik    /**
151d90144db52c7297879b950cbbc85137ed123ab5bChris Craik     * At defer time, stores the *defer time* savecount of save/saveLayer ops that were deferred, so
152d90144db52c7297879b950cbbc85137ed123ab5bChris Craik     * that when an associated restoreToCount is deferred, it can be recorded as a
153d90144db52c7297879b950cbbc85137ed123ab5bChris Craik     * RestoreToCountBatch
154ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik     */
155272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck    std::vector<int> mSaveStack;
156ff78583d8a73ca35ce65b5d2592570ff6fb9901bChris Craik    int mComplexClipStackStart;
157c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
158272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck    std::vector<Batch*> mBatches;
159527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
160527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    // Maps batch ids to the most recent *non-merging* batch of that id
161527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    Batch* mBatchLookup[kOpBatch_Count];
162527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
163527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    // Points to the index after the most recent barrier
164527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    int mEarliestBatchIndex;
165527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
16628ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    // Points to the first index that may contain a pure drawing batch
16728ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    int mEarliestUnclearedIndex;
16828ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
169527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    /**
170527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     * Maps the mergeid_t returned by an op's getMergeId() to the most recently seen
171527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     * MergingDrawBatch of that id. These ids are unique per draw type and guaranteed to not
172527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     * collide, which avoids the need to resolve mergeid collisions.
173527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik     */
1741673035f051aa5da7e98bf7ad6acc5ae4e27a5dfSene Gales    std::unordered_map<mergeid_t, DrawBatch*> mMergingBatches[kOpBatch_Count];
175c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik
176c1c5f0870282b56dafe5a4d756e4b9e6884655a7Chris Craik    LinearAllocator mAllocator;
177c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik};
178c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
17928ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik/**
18028ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik * Struct containing information that instructs the defer
18128ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik */
18228ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craikstruct DeferInfo {
18328ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craikpublic:
18428ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    DeferInfo() :
18528ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik            batchId(DeferredDisplayList::kOpBatch_None),
18628ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik            mergeId((mergeid_t) -1),
18728ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik            mergeable(false),
18828ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik            opaqueOverBounds(false) {
18928ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    };
19028ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
19128ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    int batchId;
19228ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    mergeid_t mergeId;
19328ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    bool mergeable;
19428ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik    bool opaqueOverBounds; // opaque over bounds in DeferredDisplayState - can skip ops below
19528ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik};
19628ce94a4ffc7576f40776d212f1ada79fafaa061Chris Craik
197c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik}; // namespace uirenderer
198c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik}; // namespace android
199c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik
200c3566d06421c8acc0aafb18f7e307e5725ce87e1Chris Craik#endif // ANDROID_HWUI_DEFERRED_DISPLAY_LIST_H
201