blob: 6ebb49f64ca2419b1da0b178a8533b33aea3301a [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
21#include <utils/Errors.h>
22#include <utils/Log.h>
23
Mathias Agopian52212712009-08-11 22:34:02 -070024#include "BufferAllocator.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include "LayerDim.h"
26#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include "DisplayHardware/DisplayHardware.h"
28
29namespace android {
30// ---------------------------------------------------------------------------
31
32const uint32_t LayerDim::typeInfo = LayerBaseClient::typeInfo | 0x10;
33const char* const LayerDim::typeID = "LayerDim";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034
Mathias Agopian945ebbf2009-06-18 18:48:39 -070035bool LayerDim::sUseTexture;
36GLuint LayerDim::sTexId;
37EGLImageKHR LayerDim::sImage;
38int32_t LayerDim::sWidth;
39int32_t LayerDim::sHeight;
40
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041// ---------------------------------------------------------------------------
42
43LayerDim::LayerDim(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -070044 const sp<Client>& client, int32_t i)
Mathias Agopian945ebbf2009-06-18 18:48:39 -070045 : LayerBaseClient(flinger, display, client, i)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046{
47}
48
49void LayerDim::initDimmer(SurfaceFlinger* flinger, uint32_t w, uint32_t h)
50{
Mathias Agopian945ebbf2009-06-18 18:48:39 -070051 sTexId = -1;
52 sImage = EGL_NO_IMAGE_KHR;
53 sWidth = w;
54 sHeight = h;
55 sUseTexture = false;
56
57#ifdef DIM_WITH_TEXTURE
58
59#warning "using a texture to implement LayerDim"
60
61 /* On some h/w like msm7K, it is faster to use a texture because the
62 * software renderer will defer to copybit, for this to work we need to
63 * use an EGLImage texture so copybit can actually make use of it.
64 * This burns a full-screen worth of graphic memory.
65 */
66
67 const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware());
68 uint32_t flags = hw.getFlags();
69
70 if (LIKELY(flags & DisplayHardware::DIRECT_TEXTURE)) {
71 // TODO: api to pass the usage flags
Mathias Agopian52212712009-08-11 22:34:02 -070072 sp<Buffer> buffer = new Buffer(w, h, PIXEL_FORMAT_RGB_565,
73 BufferAllocator::USAGE_SW_WRITE_OFTEN |
74 BufferAllocator::USAGE_HW_TEXTURE |
75 BufferAllocator::USAGE_HW_2D);
76
Mathias Agopian945ebbf2009-06-18 18:48:39 -070077 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
78
79 glGenTextures(1, &sTexId);
80 glBindTexture(GL_TEXTURE_2D, sTexId);
81
82 EGLDisplay dpy = eglGetCurrentDisplay();
83 sImage = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
84 EGL_NATIVE_BUFFER_ANDROID, (EGLClientBuffer)clientBuf, 0);
85 if (sImage == EGL_NO_IMAGE_KHR) {
86 LOGE("eglCreateImageKHR() failed. err=0x%4x", eglGetError());
87 return;
88 }
89
90 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)sImage);
91 GLint error = glGetError();
92 if (error != GL_NO_ERROR) {
93 eglDestroyImageKHR(dpy, sImage);
94 LOGE("glEGLImageTargetTexture2DOES() failed. err=0x%4x", error);
95 return;
96 }
97
98 // initialize the texture with zeros
99 GGLSurface t;
100 buffer->lock(&t, GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN);
Mathias Agopian958b3ca2009-06-25 16:21:32 -0700101 memset(t.data, 0, t.stride * t.height * 2);
Mathias Agopian945ebbf2009-06-18 18:48:39 -0700102 buffer->unlock();
103 sUseTexture = true;
104 }
105#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106}
107
108LayerDim::~LayerDim()
109{
110}
111
112void LayerDim::onDraw(const Region& clip) const
113{
114 const State& s(drawingState());
Mathias Agopian20f68782009-05-11 00:03:41 -0700115 Region::const_iterator it = clip.begin();
116 Region::const_iterator const end = clip.end();
117 if (s.alpha>0 && (it != end)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700119 const GGLfixed alpha = (s.alpha << 16)/255;
120 const uint32_t fbHeight = hw.getHeight();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700121 glDisable(GL_DITHER);
122 glEnable(GL_BLEND);
123 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
124 glColor4x(0, 0, 0, alpha);
Mathias Agopian945ebbf2009-06-18 18:48:39 -0700125
126#ifdef DIM_WITH_TEXTURE
127 if (sUseTexture) {
128 glBindTexture(GL_TEXTURE_2D, sTexId);
129 glEnable(GL_TEXTURE_2D);
130 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
131 const GLshort texCoords[4][2] = {
132 { 0, 0 },
133 { 0, 1 },
134 { 1, 1 },
135 { 1, 0 }
136 };
137 glMatrixMode(GL_TEXTURE);
138 glLoadIdentity();
139 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
140 glTexCoordPointer(2, GL_SHORT, 0, texCoords);
141 } else
142#endif
143 {
144 glDisable(GL_TEXTURE_2D);
145 }
146
147 GLshort w = sWidth;
148 GLshort h = sHeight;
149 const GLshort vertices[4][2] = {
150 { 0, 0 },
151 { 0, h },
152 { w, h },
153 { w, 0 }
154 };
155 glVertexPointer(2, GL_SHORT, 0, vertices);
156
Mathias Agopian20f68782009-05-11 00:03:41 -0700157 while (it != end) {
158 const Rect& r = *it++;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700159 const GLint sy = fbHeight - (r.top + r.height());
160 glScissor(r.left, sy, r.width(), r.height());
161 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 }
163 }
Mathias Agopian945ebbf2009-06-18 18:48:39 -0700164 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165}
166
167// ---------------------------------------------------------------------------
168
169}; // namespace android