The Android Open Source Project | 9066cfe | 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 | 9066cfe | 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 | |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 24 | #include <ui/GraphicBuffer.h> |
| 25 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include "LayerDim.h" |
| 27 | #include "SurfaceFlinger.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include "DisplayHardware/DisplayHardware.h" |
| 29 | |
| 30 | namespace android { |
| 31 | // --------------------------------------------------------------------------- |
| 32 | |
Mathias Agopian | 9cc8852 | 2009-06-18 18:48:39 -0700 | [diff] [blame] | 33 | bool LayerDim::sUseTexture; |
| 34 | GLuint LayerDim::sTexId; |
| 35 | EGLImageKHR LayerDim::sImage; |
| 36 | int32_t LayerDim::sWidth; |
| 37 | int32_t LayerDim::sHeight; |
| 38 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | // --------------------------------------------------------------------------- |
| 40 | |
| 41 | LayerDim::LayerDim(SurfaceFlinger* flinger, DisplayID display, |
Mathias Agopian | 593c05c | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 42 | const sp<Client>& client) |
| 43 | : LayerBaseClient(flinger, display, client) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
| 47 | void LayerDim::initDimmer(SurfaceFlinger* flinger, uint32_t w, uint32_t h) |
| 48 | { |
Mathias Agopian | 9cc8852 | 2009-06-18 18:48:39 -0700 | [diff] [blame] | 49 | sTexId = -1; |
| 50 | sImage = EGL_NO_IMAGE_KHR; |
| 51 | sWidth = w; |
| 52 | sHeight = h; |
| 53 | sUseTexture = false; |
| 54 | |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 55 | #if defined(DIM_WITH_TEXTURE) && defined(EGL_ANDROID_image_native_buffer) |
| 56 | |
Mathias Agopian | 9cc8852 | 2009-06-18 18:48:39 -0700 | [diff] [blame] | 57 | #warning "using a texture to implement LayerDim" |
| 58 | |
| 59 | /* On some h/w like msm7K, it is faster to use a texture because the |
| 60 | * software renderer will defer to copybit, for this to work we need to |
| 61 | * use an EGLImage texture so copybit can actually make use of it. |
| 62 | * This burns a full-screen worth of graphic memory. |
| 63 | */ |
| 64 | |
| 65 | const DisplayHardware& hw(flinger->graphicPlane(0).displayHardware()); |
| 66 | uint32_t flags = hw.getFlags(); |
| 67 | |
| 68 | if (LIKELY(flags & DisplayHardware::DIRECT_TEXTURE)) { |
Mathias Agopian | 6950e42 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 69 | sp<GraphicBuffer> buffer = new GraphicBuffer(w, h, PIXEL_FORMAT_RGB_565, |
| 70 | GraphicBuffer::USAGE_SW_WRITE_OFTEN | |
| 71 | GraphicBuffer::USAGE_HW_TEXTURE); |
Mathias Agopian | 5cec474 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 72 | |
Mathias Agopian | 9cc8852 | 2009-06-18 18:48:39 -0700 | [diff] [blame] | 73 | android_native_buffer_t* clientBuf = buffer->getNativeBuffer(); |
| 74 | |
| 75 | glGenTextures(1, &sTexId); |
| 76 | glBindTexture(GL_TEXTURE_2D, sTexId); |
| 77 | |
| 78 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 79 | sImage = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, |
| 80 | EGL_NATIVE_BUFFER_ANDROID, (EGLClientBuffer)clientBuf, 0); |
| 81 | if (sImage == EGL_NO_IMAGE_KHR) { |
| 82 | LOGE("eglCreateImageKHR() failed. err=0x%4x", eglGetError()); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)sImage); |
| 87 | GLint error = glGetError(); |
| 88 | if (error != GL_NO_ERROR) { |
| 89 | eglDestroyImageKHR(dpy, sImage); |
| 90 | LOGE("glEGLImageTargetTexture2DOES() failed. err=0x%4x", error); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | // initialize the texture with zeros |
| 95 | GGLSurface t; |
Mathias Agopian | 2ec8458 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 96 | buffer->lock(&t, GRALLOC_USAGE_SW_WRITE_OFTEN); |
Mathias Agopian | dafb08d | 2009-06-25 16:21:32 -0700 | [diff] [blame] | 97 | memset(t.data, 0, t.stride * t.height * 2); |
Mathias Agopian | 9cc8852 | 2009-06-18 18:48:39 -0700 | [diff] [blame] | 98 | buffer->unlock(); |
| 99 | sUseTexture = true; |
| 100 | } |
| 101 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | LayerDim::~LayerDim() |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | void LayerDim::onDraw(const Region& clip) const |
| 109 | { |
| 110 | const State& s(drawingState()); |
Mathias Agopian | 6158b1b | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 111 | Region::const_iterator it = clip.begin(); |
| 112 | Region::const_iterator const end = clip.end(); |
| 113 | if (s.alpha>0 && (it != end)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
Mathias Agopian | 781953d | 2010-06-25 18:02:21 -0700 | [diff] [blame^] | 115 | const GLfloat alpha = s.alpha/255.0f; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 116 | const uint32_t fbHeight = hw.getHeight(); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 117 | glDisable(GL_DITHER); |
| 118 | glEnable(GL_BLEND); |
| 119 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
Mathias Agopian | 781953d | 2010-06-25 18:02:21 -0700 | [diff] [blame^] | 120 | glColor4f(0, 0, 0, alpha); |
| 121 | |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 122 | #if defined(GL_OES_texture_external) |
Mathias Agopian | 781953d | 2010-06-25 18:02:21 -0700 | [diff] [blame^] | 123 | if (GLExtensions::getInstance().haveTextureExternal()) { |
| 124 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 125 | } |
Mathias Agopian | f8b4b44 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 126 | #endif |
Mathias Agopian | 9042b45 | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 127 | #if defined(DIM_WITH_TEXTURE) && defined(EGL_ANDROID_image_native_buffer) |
Mathias Agopian | 9cc8852 | 2009-06-18 18:48:39 -0700 | [diff] [blame] | 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 Agopian | 6158b1b | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 158 | while (it != end) { |
| 159 | const Rect& r = *it++; |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 160 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
Mathias Agopian | 9cc8852 | 2009-06-18 18:48:39 -0700 | [diff] [blame] | 165 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // --------------------------------------------------------------------------- |
| 169 | |
| 170 | }; // namespace android |