The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include "DisplayHardware/DisplayHardware.h" |
| 27 | |
| 28 | namespace android { |
| 29 | // --------------------------------------------------------------------------- |
| 30 | |
| 31 | const uint32_t LayerDim::typeInfo = LayerBaseClient::typeInfo | 0x10; |
| 32 | const char* const LayerDim::typeID = "LayerDim"; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
Mathias Agopian | 945ebbf | 2009-06-18 18:48:39 -0700 | [diff] [blame^] | 34 | bool LayerDim::sUseTexture; |
| 35 | GLuint LayerDim::sTexId; |
| 36 | EGLImageKHR LayerDim::sImage; |
| 37 | int32_t LayerDim::sWidth; |
| 38 | int32_t LayerDim::sHeight; |
| 39 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | // --------------------------------------------------------------------------- |
| 41 | |
| 42 | LayerDim::LayerDim(SurfaceFlinger* flinger, DisplayID display, |
| 43 | Client* client, int32_t i) |
Mathias Agopian | 945ebbf | 2009-06-18 18:48:39 -0700 | [diff] [blame^] | 44 | : LayerBaseClient(flinger, display, client, i) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
| 48 | void LayerDim::initDimmer(SurfaceFlinger* flinger, uint32_t w, uint32_t h) |
| 49 | { |
Mathias Agopian | 945ebbf | 2009-06-18 18:48:39 -0700 | [diff] [blame^] | 50 | 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 | |
| 66 | const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware()); |
| 67 | uint32_t flags = hw.getFlags(); |
| 68 | |
| 69 | if (LIKELY(flags & DisplayHardware::DIRECT_TEXTURE)) { |
| 70 | // TODO: api to pass the usage flags |
| 71 | sp<Buffer> buffer = new Buffer(w, h, PIXEL_FORMAT_RGB_565); |
| 72 | android_native_buffer_t* clientBuf = buffer->getNativeBuffer(); |
| 73 | |
| 74 | glGenTextures(1, &sTexId); |
| 75 | glBindTexture(GL_TEXTURE_2D, sTexId); |
| 76 | |
| 77 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 78 | sImage = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, |
| 79 | EGL_NATIVE_BUFFER_ANDROID, (EGLClientBuffer)clientBuf, 0); |
| 80 | if (sImage == EGL_NO_IMAGE_KHR) { |
| 81 | LOGE("eglCreateImageKHR() failed. err=0x%4x", eglGetError()); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)sImage); |
| 86 | GLint error = glGetError(); |
| 87 | if (error != GL_NO_ERROR) { |
| 88 | eglDestroyImageKHR(dpy, sImage); |
| 89 | LOGE("glEGLImageTargetTexture2DOES() failed. err=0x%4x", error); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | // initialize the texture with zeros |
| 94 | GGLSurface t; |
| 95 | buffer->lock(&t, GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN); |
| 96 | memset(t.data, 0, t.width * t.stride * 2); |
| 97 | buffer->unlock(); |
| 98 | sUseTexture = true; |
| 99 | } |
| 100 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | LayerDim::~LayerDim() |
| 104 | { |
| 105 | } |
| 106 | |
| 107 | void LayerDim::onDraw(const Region& clip) const |
| 108 | { |
| 109 | const State& s(drawingState()); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 110 | Region::const_iterator it = clip.begin(); |
| 111 | Region::const_iterator const end = clip.end(); |
| 112 | if (s.alpha>0 && (it != end)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 114 | const GGLfixed alpha = (s.alpha << 16)/255; |
| 115 | const uint32_t fbHeight = hw.getHeight(); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 116 | glDisable(GL_DITHER); |
| 117 | glEnable(GL_BLEND); |
| 118 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 119 | glColor4x(0, 0, 0, alpha); |
Mathias Agopian | 945ebbf | 2009-06-18 18:48:39 -0700 | [diff] [blame^] | 120 | |
| 121 | #ifdef DIM_WITH_TEXTURE |
| 122 | if (sUseTexture) { |
| 123 | glBindTexture(GL_TEXTURE_2D, sTexId); |
| 124 | glEnable(GL_TEXTURE_2D); |
| 125 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 126 | const GLshort texCoords[4][2] = { |
| 127 | { 0, 0 }, |
| 128 | { 0, 1 }, |
| 129 | { 1, 1 }, |
| 130 | { 1, 0 } |
| 131 | }; |
| 132 | glMatrixMode(GL_TEXTURE); |
| 133 | glLoadIdentity(); |
| 134 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 135 | glTexCoordPointer(2, GL_SHORT, 0, texCoords); |
| 136 | } else |
| 137 | #endif |
| 138 | { |
| 139 | glDisable(GL_TEXTURE_2D); |
| 140 | } |
| 141 | |
| 142 | GLshort w = sWidth; |
| 143 | GLshort h = sHeight; |
| 144 | const GLshort vertices[4][2] = { |
| 145 | { 0, 0 }, |
| 146 | { 0, h }, |
| 147 | { w, h }, |
| 148 | { w, 0 } |
| 149 | }; |
| 150 | glVertexPointer(2, GL_SHORT, 0, vertices); |
| 151 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 152 | while (it != end) { |
| 153 | const Rect& r = *it++; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 154 | const GLint sy = fbHeight - (r.top + r.height()); |
| 155 | glScissor(r.left, sy, r.width(), r.height()); |
| 156 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
Mathias Agopian | 945ebbf | 2009-06-18 18:48:39 -0700 | [diff] [blame^] | 159 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // --------------------------------------------------------------------------- |
| 163 | |
| 164 | }; // namespace android |