Home | History | Annotate | Download | only in hwui
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #pragma once
     18 
     19 #include "ClipArea.h"
     20 #include "Rect.h"
     21 #include "utils/Macros.h"
     22 
     23 #include <unordered_map>
     24 #include <vector>
     25 
     26 struct SkRect;
     27 
     28 namespace android {
     29 namespace uirenderer {
     30 
     31 class BakedOpState;
     32 struct BeginLayerOp;
     33 class BatchBase;
     34 class LinearAllocator;
     35 struct MergedBakedOpList;
     36 class MergingOpBatch;
     37 class OffscreenBuffer;
     38 class OpBatch;
     39 class RenderNode;
     40 
     41 typedef int batchid_t;
     42 typedef const void* mergeid_t;
     43 
     44 namespace OpBatchType {
     45 enum {
     46     Bitmap,
     47     MergedPatch,
     48     AlphaVertices,
     49     Vertices,
     50     AlphaMaskTexture,
     51     Text,
     52     ColorText,
     53     Shadow,
     54     TextureLayer,
     55     Functor,
     56     CopyToLayer,
     57     CopyFromLayer,
     58 
     59     Count  // must be last
     60 };
     61 }
     62 
     63 typedef void (*BakedOpReceiver)(void*, const BakedOpState&);
     64 typedef void (*MergedOpReceiver)(void*, const MergedBakedOpList& opList);
     65 
     66 /**
     67  * Stores the deferred render operations and state used to compute ordering
     68  * for a single FBO/layer.
     69  */
     70 class LayerBuilder {
     71     // Prevent copy/assign because users may stash pointer to offscreenBuffer and viewportClip
     72     PREVENT_COPY_AND_ASSIGN(LayerBuilder);
     73 
     74 public:
     75     // Create LayerBuilder for Fbo0
     76     LayerBuilder(uint32_t width, uint32_t height, const Rect& repaintRect)
     77             : LayerBuilder(width, height, repaintRect, nullptr, nullptr){};
     78 
     79     // Create LayerBuilder for an offscreen layer, where beginLayerOp is present for a
     80     // saveLayer, renderNode is present for a HW layer.
     81     LayerBuilder(uint32_t width, uint32_t height, const Rect& repaintRect,
     82                  const BeginLayerOp* beginLayerOp, RenderNode* renderNode);
     83 
     84     // iterate back toward target to see if anything drawn since should overlap the new op
     85     // if no target, merging ops still iterate to find similar batch to insert after
     86     void locateInsertIndex(int batchId, const Rect& clippedBounds, BatchBase** targetBatch,
     87                            size_t* insertBatchIndex) const;
     88 
     89     void deferUnmergeableOp(LinearAllocator& allocator, BakedOpState* op, batchid_t batchId);
     90 
     91     // insertion point of a new batch, will hopefully be immediately after similar batch
     92     // (generally, should be similar shader)
     93     void deferMergeableOp(LinearAllocator& allocator, BakedOpState* op, batchid_t batchId,
     94                           mergeid_t mergeId);
     95 
     96     void replayBakedOpsImpl(void* arg, BakedOpReceiver* receivers, MergedOpReceiver*) const;
     97 
     98     void deferLayerClear(const Rect& dstRect);
     99 
    100     bool empty() const { return mBatches.empty(); }
    101 
    102     void clear();
    103 
    104     void dump() const;
    105 
    106     const uint32_t width;
    107     const uint32_t height;
    108     const Rect repaintRect;
    109     const ClipRect repaintClip;
    110     OffscreenBuffer* offscreenBuffer;
    111     const BeginLayerOp* beginLayerOp;
    112     const RenderNode* renderNode;
    113 
    114     // list of deferred CopyFromLayer ops, to be deferred upon encountering EndUnclippedLayerOps
    115     std::vector<BakedOpState*> activeUnclippedSaveLayers;
    116 
    117 private:
    118     void onDeferOp(LinearAllocator& allocator, const BakedOpState* bakedState);
    119     void flushLayerClears(LinearAllocator& allocator);
    120 
    121     std::vector<BatchBase*> mBatches;
    122 
    123     /**
    124      * Maps the mergeid_t returned by an op's getMergeId() to the most recently seen
    125      * MergingDrawBatch of that id. These ids are unique per draw type and guaranteed to not
    126      * collide, which avoids the need to resolve mergeid collisions.
    127      */
    128     std::unordered_map<mergeid_t, MergingOpBatch*> mMergingBatchLookup[OpBatchType::Count];
    129 
    130     // Maps batch ids to the most recent *non-merging* batch of that id
    131     OpBatch* mBatchLookup[OpBatchType::Count] = {nullptr};
    132 
    133     std::vector<Rect> mClearRects;
    134 };
    135 
    136 };  // namespace uirenderer
    137 };  // namespace android
    138