1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (C) 2012-2014, The Linux Foundation. All rights reserved.
4 *
5 * Not a Contribution, Apache license notifications and license are retained
6 * for attribution purposes only.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 *      http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20#ifndef HWC_COPYBIT_H
21#define HWC_COPYBIT_H
22#include "hwc_utils.h"
23
24#define NUM_RENDER_BUFFERS 3
25//These scaling factors are specific for MDP3. Normally scaling factor
26//is only 4, but copybit will create temp buffer to let it run through
27//twice
28#define MAX_SCALE_FACTOR 16
29#define MIN_SCALE_FACTOR 0.0625
30#define MAX_LAYERS_FOR_ABC 2
31#define INVALID_DIMENSION -1
32#define NO_UPDATING_LAYER -2
33namespace qhwc {
34
35class CopyBit {
36public:
37    CopyBit(hwc_context_t *ctx, const int& dpy);
38    ~CopyBit();
39    // API to get copybit engine(non static)
40    struct copybit_device_t *getCopyBitDevice();
41    //Sets up members and prepares copybit if conditions are met
42    bool prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
43                                                                   int dpy);
44    //Draws layer if the layer is set for copybit in prepare
45    bool draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
46                                                        int dpy, int* fd);
47    // resets the values
48    void reset();
49
50    private_handle_t * getCurrentRenderBuffer();
51
52    void setReleaseFd(int fd);
53
54    void setReleaseFdSync(int fd);
55
56    bool prepareOverlap(hwc_context_t *ctx, hwc_display_contents_1_t *list);
57
58    int drawOverlap(hwc_context_t *ctx, hwc_display_contents_1_t *list);
59
60private:
61    /* cached data */
62    struct LayerCache {
63      int layerCount;
64      buffer_handle_t hnd[MAX_NUM_APP_LAYERS];
65      hwc_rect_t displayFrame[MAX_NUM_APP_LAYERS];
66      bool drop[MAX_NUM_APP_LAYERS];
67      /* c'tor */
68      LayerCache();
69      /* clear caching info*/
70      void reset();
71      void updateCounts(hwc_context_t *ctx, hwc_display_contents_1_t *list,
72              int dpy);
73    };
74    /* framebuffer cache*/
75    struct FbCache {
76      hwc_rect_t  FbdirtyRect[NUM_RENDER_BUFFERS];
77      hwc_rect_t  FbdisplayRect[NUM_RENDER_BUFFERS];
78      int FbIndex;
79      FbCache();
80      void reset();
81      void insertAndUpdateFbCache(hwc_rect_t dirtyRect,
82                               hwc_rect_t displayRect);
83      int getUnchangedFbDRCount(hwc_rect_t dirtyRect,
84                             hwc_rect_t displayRect);
85    };
86
87    // holds the copybit device
88    struct copybit_device_t *mEngine;
89    bool drawUsingAppBufferComposition(hwc_context_t *ctx,
90                                hwc_display_contents_1_t *list,
91                                int dpy, int *fd);
92    // Helper functions for copybit composition
93    int  drawLayerUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
94                          private_handle_t *renderBuffer, bool isFG);
95    // Helper function to draw copybit layer for PTOR comp
96    int drawRectUsingCopybit(hwc_context_t *dev, hwc_layer_1_t *layer,
97                          private_handle_t *renderBuffer, hwc_rect_t overlap,
98                          hwc_rect_t destRect);
99    int fillColorUsingCopybit(hwc_layer_1_t *layer,
100                          private_handle_t *renderBuffer);
101    bool canUseCopybitForYUV (hwc_context_t *ctx);
102    bool canUseCopybitForRGB (hwc_context_t *ctx,
103                                     hwc_display_contents_1_t *list, int dpy);
104    bool validateParams (hwc_context_t *ctx,
105                                const hwc_display_contents_1_t *list);
106    //Flags if this feature is on.
107    bool mIsModeOn;
108    // flag that indicates whether CopyBit composition is enabled for this cycle
109    bool mCopyBitDraw;
110
111    unsigned int getRGBRenderingArea (const hwc_context_t *ctx,
112                                         const hwc_display_contents_1_t *list);
113
114    void getLayerResolution(const hwc_layer_1_t* layer,
115                                   unsigned int &width, unsigned int& height);
116
117    int allocRenderBuffers(int w, int h, int f);
118
119    void freeRenderBuffers();
120
121    int clear (private_handle_t* hnd, hwc_rect_t& rect);
122
123    private_handle_t* mRenderBuffer[NUM_RENDER_BUFFERS];
124
125    // Index of the current intermediate render buffer
126    int mCurRenderBufferIndex;
127
128    // Release FDs of the intermediate render buffer
129    int mRelFd[NUM_RENDER_BUFFERS];
130
131    //Dynamic composition threshold for deciding copybit usage.
132    double mDynThreshold;
133    bool mSwapRectEnable;
134    int mAlignedWidth;
135    int mAlignedHeight;
136    int mSwapRect;
137    LayerCache mLayerCache;
138    FbCache mFbCache;
139    hwc_rect_t mDirtyRect;
140    bool prepareSwapRect(hwc_context_t *ctx, hwc_display_contents_1_t *list,
141                  int dpy);
142    bool isLayerChanging(hwc_context_t *ctx,
143                            hwc_display_contents_1_t *list, int k);
144    bool isSmartBlitPossible(const hwc_display_contents_1_t *list);
145};
146
147}; //namespace qhwc
148
149#endif //HWC_COPYBIT_H
150