| 1 | /* |
| 2 | Copyright (c) 2012, Broadcom Europe Ltd |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above copyright |
| 10 | notice, this list of conditions and the following disclaimer in the |
| 11 | documentation and/or other materials provided with the distribution. |
| 12 | * Neither the name of the copyright holder nor the |
| 13 | names of its contributors may be used to endorse or promote products |
| 14 | derived from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #define EGL_EGLEXT_PROTOTYPES /* we want the prototypes so the compiler will check that the signatures match */ |
| 29 | |
| 30 | #include "interface/khronos/common/khrn_client_mangle.h" |
| 31 | |
| 32 | #include "interface/khronos/common/khrn_int_common.h" |
| 33 | #include "interface/khronos/include/EGL/egl.h" |
| 34 | #include "interface/khronos/include/EGL/eglext.h" |
| 35 | #include "interface/khronos/common/khrn_client.h" |
| 36 | #include "interface/khronos/common/khrn_client_rpc.h" |
| 37 | #include "interface/khronos/egl/egl_client_config.h" |
| 38 | #ifdef RPC_DIRECT |
| 39 | #include "interface/khronos/egl/egl_int_impl.h" |
| 40 | #endif |
| 41 | #include <assert.h> |
| 42 | |
| 43 | static bool lock_surface_check_attribs(const EGLint *attrib_list, bool *preserve_pixels, uint32_t *lock_usage_hint) |
| 44 | { |
| 45 | if (!attrib_list) |
| 46 | return EGL_TRUE; |
| 47 | |
| 48 | while (1) { |
| 49 | int name = *attrib_list++; |
| 50 | if (name == EGL_NONE) |
| 51 | return EGL_TRUE; |
| 52 | else { |
| 53 | int value = *attrib_list++; |
| 54 | switch (name) { |
| 55 | case EGL_MAP_PRESERVE_PIXELS_KHR: |
| 56 | *preserve_pixels = value ? true : false; |
| 57 | break; |
| 58 | case EGL_LOCK_USAGE_HINT_KHR: |
| 59 | if (value & ~(EGL_READ_SURFACE_BIT_KHR | EGL_WRITE_SURFACE_BIT_KHR)) |
| 60 | return EGL_FALSE; |
| 61 | |
| 62 | *lock_usage_hint = value; |
| 63 | break; |
| 64 | default: |
| 65 | return EGL_FALSE; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | EGLAPI EGLBoolean EGLAPIENTRY eglLockSurfaceKHR (EGLDisplay dpy, EGLSurface surf, const EGLint *attrib_list) |
| 72 | { |
| 73 | CLIENT_THREAD_STATE_T *thread = CLIENT_GET_THREAD_STATE(); |
| 74 | EGLBoolean result; |
| 75 | |
| 76 | CLIENT_LOCK(); |
| 77 | |
| 78 | { |
| 79 | CLIENT_PROCESS_STATE_T *process = client_egl_get_process_state(thread, dpy, EGL_TRUE); |
| 80 | |
| 81 | if (!process) |
| 82 | result = 0; |
| 83 | else { |
| 84 | EGL_SURFACE_T *surface = client_egl_get_surface(thread, process, surf); |
| 85 | |
| 86 | if (surface) { |
| 87 | bool preserve_pixels = false; |
| 88 | uint32_t lock_usage_hint = EGL_READ_SURFACE_BIT_KHR | EGL_WRITE_SURFACE_BIT_KHR; /* we completely ignore this */ |
| 89 | |
| 90 | assert(!surface->is_locked); |
| 91 | |
| 92 | if (!lock_surface_check_attribs(attrib_list, &preserve_pixels, &lock_usage_hint)) { |
| 93 | thread->error = EGL_BAD_ATTRIBUTE; |
| 94 | result = EGL_FALSE; |
| 95 | } else if (!egl_config_is_lockable((int)(intptr_t)surface->config - 1)) { |
| 96 | /* Only lockable surfaces can be locked (obviously) */ |
| 97 | thread->error = EGL_BAD_ACCESS; |
| 98 | result = EGL_FALSE; |
| 99 | } else if (surface->context_binding_count) { |
| 100 | /* Cannot lock a surface if it is bound to a context */ |
| 101 | thread->error = EGL_BAD_ACCESS; |
| 102 | result = EGL_FALSE; |
| 103 | } else if (preserve_pixels) { |
| 104 | /* TODO: we don't need to support this. What error should we return? */ |
| 105 | thread->error = EGL_BAD_ATTRIBUTE; |
| 106 | return EGL_FALSE; |
| 107 | } else { |
| 108 | /* Don't allocate the buffer here. This happens during "mapping", in eglQuerySurface. */ |
| 109 | surface->mapped_buffer = 0; |
| 110 | surface->is_locked = true; |
| 111 | thread->error = EGL_SUCCESS; |
| 112 | result = EGL_TRUE; |
| 113 | } |
| 114 | } else |
| 115 | result = EGL_FALSE; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | CLIENT_UNLOCK(); |
| 120 | return result; |
| 121 | } |
| 122 | |
| 123 | EGLAPI EGLBoolean EGLAPIENTRY eglUnlockSurfaceKHR (EGLDisplay dpy, EGLSurface surf) |
| 124 | { |
| 125 | CLIENT_THREAD_STATE_T *thread = CLIENT_GET_THREAD_STATE(); |
| 126 | EGLBoolean result; |
| 127 | |
| 128 | CLIENT_LOCK(); |
| 129 | |
| 130 | { |
| 131 | CLIENT_PROCESS_STATE_T *process = client_egl_get_process_state(thread, dpy, EGL_TRUE); |
| 132 | |
| 133 | if (!process) |
| 134 | result = 0; |
| 135 | else { |
| 136 | EGL_SURFACE_T *surface = client_egl_get_locked_surface(thread, process, surf); |
| 137 | |
| 138 | if (!surface) { |
| 139 | result = EGL_FALSE; |
| 140 | } else if (!surface->is_locked) { |
| 141 | thread->error = EGL_BAD_ACCESS; |
| 142 | result = EGL_FALSE; |
| 143 | } else { |
| 144 | assert(surface->is_locked); |
| 145 | if (surface->mapped_buffer) { |
| 146 | KHRN_IMAGE_FORMAT_T format = egl_config_get_mapped_format((int)(intptr_t)surface->config - 1); |
| 147 | uint32_t stride = khrn_image_get_stride(format, surface->width); |
| 148 | int lines, offset, height; |
| 149 | |
| 150 | lines = KHDISPATCH_WORKSPACE_SIZE / stride; |
| 151 | offset = 0; |
| 152 | height = surface->height; |
| 153 | |
| 154 | while (height > 0) { |
| 155 | int batch = _min(lines, height); |
| 156 | #ifndef RPC_DIRECT |
| 157 | uint32_t len = batch * stride; |
| 158 | #endif |
| 159 | |
| 160 | RPC_CALL7_IN_BULK(eglIntSetColorData_impl, |
| 161 | thread, |
| 162 | EGLINTSETCOLORDATA_ID, |
| 163 | surface->serverbuffer, |
| 164 | format, |
| 165 | surface->width, |
| 166 | batch, |
| 167 | stride, |
| 168 | offset, |
| 169 | (const char *)surface->mapped_buffer + offset * stride, |
| 170 | len); |
| 171 | |
| 172 | offset += batch; |
| 173 | height -= batch; |
| 174 | } |
| 175 | |
| 176 | khrn_platform_free(surface->mapped_buffer); |
| 177 | } |
| 178 | |
| 179 | surface->mapped_buffer = 0; |
| 180 | surface->is_locked = false; |
| 181 | thread->error = EGL_SUCCESS; |
| 182 | result = EGL_TRUE; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | CLIENT_UNLOCK(); |
| 188 | return result; |
| 189 | } |
| 190 | |
| 191 | |