blob: 4114a42bd5b168153a3e4fd3078a740175fbf99e [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
24#include "LayerDim.h"
25#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include "DisplayHardware/DisplayHardware.h"
27
28namespace android {
29// ---------------------------------------------------------------------------
30
31const uint32_t LayerDim::typeInfo = LayerBaseClient::typeInfo | 0x10;
32const char* const LayerDim::typeID = "LayerDim";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
Mathias Agopian945ebbf2009-06-18 18:48:39 -070034bool LayerDim::sUseTexture;
35GLuint LayerDim::sTexId;
36EGLImageKHR LayerDim::sImage;
37int32_t LayerDim::sWidth;
38int32_t LayerDim::sHeight;
39
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040// ---------------------------------------------------------------------------
41
42LayerDim::LayerDim(SurfaceFlinger* flinger, DisplayID display,
Mathias Agopianf9d93272009-06-19 17:00:27 -070043 const sp<Client>& client, int32_t i)
Mathias Agopian945ebbf2009-06-18 18:48:39 -070044 : LayerBaseClient(flinger, display, client, i)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045{
46}
47
48void LayerDim::initDimmer(SurfaceFlinger* flinger, uint32_t w, uint32_t h)
49{
Mathias Agopian945ebbf2009-06-18 18:48:39 -070050 sTexId = -1;
51 sImage = EGL_NO_IMAGE_KHR;
52 sWidth = w;
53 sHeight = h;
54 sUseTexture = false;
55
56#ifdef DIM_WITH_TEXTURE
57
58#warning "using a texture to implement LayerDim"
59
60 /* On some h/w like msm7K, it is faster to use a texture because the
61 * software renderer will defer to copybit, for this to work we need to
62 * use an EGLImage texture so copybit can actually make use of it.
63 * This burns a full-screen worth of graphic memory.
64 */
65
Mathias Agopian958b3ca2009-06-25 16:21:32 -070066 // copybit supports 4x scaling, so we only need to allocate 1/16 of the
67 // buffer.
68 // FIXME: we have to add 1px because the mdp fails
69 w = w/4 + 1;
70 h = h/4 + 1;
71
Mathias Agopian945ebbf2009-06-18 18:48:39 -070072 const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware());
73 uint32_t flags = hw.getFlags();
74
75 if (LIKELY(flags & DisplayHardware::DIRECT_TEXTURE)) {
76 // TODO: api to pass the usage flags
77 sp<Buffer> buffer = new Buffer(w, h, PIXEL_FORMAT_RGB_565);
78 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
79
80 glGenTextures(1, &sTexId);
81 glBindTexture(GL_TEXTURE_2D, sTexId);
82
83 EGLDisplay dpy = eglGetCurrentDisplay();
84 sImage = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
85 EGL_NATIVE_BUFFER_ANDROID, (EGLClientBuffer)clientBuf, 0);
86 if (sImage == EGL_NO_IMAGE_KHR) {
87 LOGE("eglCreateImageKHR() failed. err=0x%4x", eglGetError());
88 return;
89 }
90
91 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)sImage);
92 GLint error = glGetError();
93 if (error != GL_NO_ERROR) {
94 eglDestroyImageKHR(dpy, sImage);
95 LOGE("glEGLImageTargetTexture2DOES() failed. err=0x%4x", error);
96 return;
97 }
98
99 // initialize the texture with zeros
100 GGLSurface t;
101 buffer->lock(&t, GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN);
Mathias Agopian958b3ca2009-06-25 16:21:32 -0700102 memset(t.data, 0, t.stride * t.height * 2);
Mathias Agopian945ebbf2009-06-18 18:48:39 -0700103 buffer->unlock();
104 sUseTexture = true;
105 }
106#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107}
108
109LayerDim::~LayerDim()
110{
111}
112
113void LayerDim::onDraw(const Region& clip) const
114{
115 const State& s(drawingState());
Mathias Agopian20f68782009-05-11 00:03:41 -0700116 Region::const_iterator it = clip.begin();
117 Region::const_iterator const end = clip.end();
118 if (s.alpha>0 && (it != end)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 const DisplayHardware& hw(graphicPlane(0).displayHardware());
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700120 const GGLfixed alpha = (s.alpha << 16)/255;
121 const uint32_t fbHeight = hw.getHeight();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700122 glDisable(GL_DITHER);
123 glEnable(GL_BLEND);
124 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
125 glColor4x(0, 0, 0, alpha);
Mathias Agopian945ebbf2009-06-18 18:48:39 -0700126
127#ifdef DIM_WITH_TEXTURE
128 if (sUseTexture) {
129 glBindTexture(GL_TEXTURE_2D, sTexId);
130 glEnable(GL_TEXTURE_2D);
131 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
132 const GLshort texCoords[4][2] = {
133 { 0, 0 },
134 { 0, 1 },
135 { 1, 1 },
136 { 1, 0 }
137 };
138 glMatrixMode(GL_TEXTURE);
139 glLoadIdentity();
140 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
141 glTexCoordPointer(2, GL_SHORT, 0, texCoords);
142 } else
143#endif
144 {
145 glDisable(GL_TEXTURE_2D);
146 }
147
148 GLshort w = sWidth;
149 GLshort h = sHeight;
150 const GLshort vertices[4][2] = {
151 { 0, 0 },
152 { 0, h },
153 { w, h },
154 { w, 0 }
155 };
156 glVertexPointer(2, GL_SHORT, 0, vertices);
157
Mathias Agopian20f68782009-05-11 00:03:41 -0700158 while (it != end) {
159 const Rect& r = *it++;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700160 const GLint sy = fbHeight - (r.top + r.height());
161 glScissor(r.left, sy, r.width(), r.height());
162 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 }
164 }
Mathias Agopian945ebbf2009-06-18 18:48:39 -0700165 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166}
167
168// ---------------------------------------------------------------------------
169
170}; // namespace android