1/*
2 * Copyright (C) 2007 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#define LOG_TAG "SurfaceFlinger"
18
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <utils/Errors.h>
24#include <utils/Log.h>
25
26#include "LayerDim.h"
27#include "SurfaceFlinger.h"
28#include "VRamHeap.h"
29#include "DisplayHardware/DisplayHardware.h"
30
31namespace android {
32// ---------------------------------------------------------------------------
33
34const uint32_t LayerDim::typeInfo = LayerBaseClient::typeInfo | 0x10;
35const char* const LayerDim::typeID = "LayerDim";
36sp<MemoryDealer> LayerDim::mDimmerDealer;
37LayerBitmap LayerDim::mDimmerBitmap;
38
39// ---------------------------------------------------------------------------
40
41LayerDim::LayerDim(SurfaceFlinger* flinger, DisplayID display,
42        Client* client, int32_t i)
43     : LayerBaseClient(flinger, display, client, i)
44{
45}
46
47void LayerDim::initDimmer(SurfaceFlinger* flinger, uint32_t w, uint32_t h)
48{
49    // must only be called once.
50    mDimmerDealer = flinger->getSurfaceHeapManager()
51            ->createHeap(ISurfaceComposer::eHardware);
52    if (mDimmerDealer != 0) {
53        mDimmerBitmap.init(mDimmerDealer);
54        mDimmerBitmap.setBits(w, h, 1, PIXEL_FORMAT_RGB_565);
55        mDimmerBitmap.clear();
56    }
57}
58
59LayerDim::~LayerDim()
60{
61}
62
63void LayerDim::onDraw(const Region& clip) const
64{
65    const State& s(drawingState());
66
67    Region::iterator iterator(clip);
68    if (s.alpha>0 && iterator) {
69        const DisplayHardware& hw(graphicPlane(0).displayHardware());
70
71        status_t err = NO_ERROR;
72        const int can_use_copybit = canUseCopybit();
73        if (can_use_copybit)  {
74            // StopWatch watch("copybit");
75            copybit_image_t dst;
76            hw.getDisplaySurface(&dst);
77            const copybit_rect_t& drect
78                = reinterpret_cast<const copybit_rect_t&>(mTransformedBounds);
79
80            copybit_image_t src;
81            mDimmerBitmap.getBitmapSurface(&src);
82            const copybit_rect_t& srect(drect);
83
84            copybit_device_t* copybit = mFlinger->getBlitEngine();
85            copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0);
86            copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, s.alpha);
87            copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_ENABLE);
88            region_iterator it(clip);
89            err = copybit->stretch(copybit, &dst, &src, &drect, &srect, &it);
90        }
91
92        if (!can_use_copybit || err) {
93            const GGLfixed alpha = (s.alpha << 16)/255;
94            const uint32_t fbHeight = hw.getHeight();
95            glDisable(GL_TEXTURE_2D);
96            glDisable(GL_DITHER);
97            glEnable(GL_BLEND);
98            glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
99            glColor4x(0, 0, 0, alpha);
100            glVertexPointer(2, GL_FIXED, 0, mVertices);
101            Rect r;
102            while (iterator.iterate(&r)) {
103                const GLint sy = fbHeight - (r.top + r.height());
104                glScissor(r.left, sy, r.width(), r.height());
105                glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
106            }
107        }
108    }
109}
110
111// ---------------------------------------------------------------------------
112
113}; // namespace android
114