David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2011, 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 | |
| 17 | #include "header.h" |
| 18 | |
| 19 | namespace android |
| 20 | { |
| 21 | bool capture; // capture after each glDraw* |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 22 | } |
| 23 | |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 24 | void Debug_glDrawArrays(GLenum mode, GLint first, GLsizei count) |
| 25 | { |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 26 | DbgContext * const dbg = getDbgContextThreadSpecific(); |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 27 | glesv2debugger::Message msg, cmd; |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 28 | msg.set_context_id(reinterpret_cast<int>(dbg)); |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 29 | msg.set_type(glesv2debugger::Message_Type_BeforeCall); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 30 | bool expectResponse = dbg->expectResponse.Bit(glesv2debugger::Message_Function_glDrawArrays); |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 31 | msg.set_expect_response(expectResponse); |
| 32 | msg.set_function(glesv2debugger::Message_Function_glDrawArrays); |
| 33 | msg.set_arg0(mode); |
| 34 | msg.set_arg1(first); |
| 35 | msg.set_arg2(count); |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 36 | |
| 37 | msg.set_arg7(dbg->maxAttrib); // indicate capturing vertex data |
| 38 | if (dbg->hasNonVBOAttribs) { |
| 39 | std::string * const data = msg.mutable_data(); |
| 40 | for (unsigned i = 0; i < count; i++) |
| 41 | dbg->Fetch(i + first, data); |
| 42 | } |
| 43 | |
| 44 | void * pixels = NULL; |
| 45 | GLint readFormat = 0, readType = 0; |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 46 | int viewport[4] = {}; |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 47 | if (!expectResponse) { |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 48 | cmd.set_function(glesv2debugger::Message_Function_CONTINUE); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 49 | cmd.set_expect_response(false); |
| 50 | } |
| 51 | glesv2debugger::Message_Function oldCmd = cmd.function(); |
David Li | 940c3f8 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 52 | Send(msg, cmd); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 53 | expectResponse = cmd.expect_response(); |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 54 | while (true) { |
| 55 | msg.Clear(); |
| 56 | nsecs_t c0 = systemTime(timeMode); |
| 57 | switch (cmd.function()) { |
| 58 | case glesv2debugger::Message_Function_CONTINUE: |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 59 | dbg->hooks->gl.glDrawArrays(mode, first, count); |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 60 | msg.set_time((systemTime(timeMode) - c0) * 1e-6f); |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 61 | msg.set_context_id(reinterpret_cast<int>(dbg)); |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 62 | msg.set_function(glesv2debugger::Message_Function_glDrawArrays); |
| 63 | msg.set_type(glesv2debugger::Message_Type_AfterCall); |
| 64 | msg.set_expect_response(expectResponse); |
David Li | 940c3f8 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 65 | if (!expectResponse) |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 66 | cmd.set_function(glesv2debugger::Message_Function_SKIP); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 67 | if (!expectResponse) { |
| 68 | cmd.set_function(glesv2debugger::Message_Function_SKIP); |
| 69 | cmd.set_expect_response(false); |
| 70 | } |
| 71 | oldCmd = cmd.function(); |
David Li | 940c3f8 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 72 | Send(msg, cmd); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 73 | expectResponse = cmd.expect_response(); |
| 74 | // TODO: pack glReadPixels data with vertex data instead of |
| 75 | // relying on sperate call for transport, this would allow |
| 76 | // auto generated message loop using EXTEND_Debug macro |
David Li | 940c3f8 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 77 | if (capture) { |
| 78 | dbg->hooks->gl.glGetIntegerv(GL_VIEWPORT, viewport); |
| 79 | dbg->hooks->gl.glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &readFormat); |
| 80 | dbg->hooks->gl.glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &readType); |
David Li | 6a5ca48 | 2011-03-21 10:02:30 -0700 | [diff] [blame] | 81 | // LOGD("glDrawArrays CAPTURE: x=%d y=%d width=%d height=%d format=0x%.4X type=0x%.4X", |
| 82 | // viewport[0], viewport[1], viewport[2], viewport[3], readFormat, readType); |
David Li | f9bc124 | 2011-03-22 18:42:03 -0700 | [diff] [blame] | 83 | pixels = dbg->GetReadPixelsBuffer(viewport[2] * viewport[3] * |
| 84 | GetBytesPerPixel(readFormat, readType)); |
David Li | 940c3f8 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 85 | Debug_glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], |
| 86 | readFormat, readType, pixels); |
David Li | 940c3f8 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 87 | } |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 88 | break; |
| 89 | case glesv2debugger::Message_Function_SKIP: |
| 90 | return; |
David Li | 940c3f8 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 91 | case glesv2debugger::Message_Function_SETPROP: |
David Li | fbfc703 | 2011-03-21 16:25:58 -0700 | [diff] [blame] | 92 | SetProp(dbg, cmd); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 93 | expectResponse = cmd.expect_response(); |
| 94 | if (!expectResponse) // SETPROP is "out of band" |
| 95 | cmd.set_function(oldCmd); |
| 96 | else |
| 97 | Receive(cmd); |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 98 | break; |
| 99 | default: |
David Li | 291f5fe | 2011-03-21 17:42:50 -0700 | [diff] [blame] | 100 | GenerateCall(dbg, cmd, msg, NULL); |
| 101 | msg.set_expect_response(expectResponse); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 102 | if (!expectResponse) { |
David Li | 291f5fe | 2011-03-21 17:42:50 -0700 | [diff] [blame] | 103 | cmd.set_function(cmd.SKIP); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 104 | cmd.set_expect_response(expectResponse); |
| 105 | } |
| 106 | oldCmd = cmd.function(); |
David Li | 291f5fe | 2011-03-21 17:42:50 -0700 | [diff] [blame] | 107 | Send(msg, cmd); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 108 | expectResponse = cmd.expect_response(); |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 109 | break; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 114 | template<typename T> |
| 115 | static inline void FetchIndexed(const unsigned count, const T * indices, |
| 116 | std::string * const data, const DbgContext * const ctx) |
| 117 | { |
| 118 | for (unsigned i = 0; i < count; i++) { |
| 119 | if (!ctx->indexBuffer) |
| 120 | data->append((const char *)(indices + i), sizeof(*indices)); |
| 121 | if (ctx->hasNonVBOAttribs) |
| 122 | ctx->Fetch(indices[i], data); |
| 123 | } |
| 124 | } |
| 125 | |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 126 | void Debug_glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid* indices) |
| 127 | { |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 128 | DbgContext * const dbg = getDbgContextThreadSpecific(); |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 129 | glesv2debugger::Message msg, cmd; |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 130 | msg.set_context_id(reinterpret_cast<int>(dbg)); |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 131 | msg.set_type(glesv2debugger::Message_Type_BeforeCall); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 132 | bool expectResponse = dbg->expectResponse.Bit(glesv2debugger::Message_Function_glDrawElements); |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 133 | msg.set_expect_response(expectResponse); |
| 134 | msg.set_function(glesv2debugger::Message_Function_glDrawElements); |
| 135 | msg.set_arg0(mode); |
| 136 | msg.set_arg1(count); |
| 137 | msg.set_arg2(type); |
| 138 | msg.set_arg3(reinterpret_cast<int>(indices)); |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 139 | |
| 140 | msg.set_arg7(dbg->maxAttrib); // indicate capturing vertex data |
| 141 | std::string * const data = msg.mutable_data(); |
| 142 | if (GL_UNSIGNED_BYTE == type) { |
| 143 | if (dbg->indexBuffer) |
David Li | f9bc124 | 2011-03-22 18:42:03 -0700 | [diff] [blame] | 144 | FetchIndexed(count, (unsigned char *)dbg->indexBuffer->data + |
| 145 | (unsigned long)indices, data, dbg); |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 146 | else |
| 147 | FetchIndexed(count, (unsigned char *)indices, data, dbg); |
| 148 | } else if (GL_UNSIGNED_SHORT == type) { |
| 149 | if (dbg->indexBuffer) |
David Li | f9bc124 | 2011-03-22 18:42:03 -0700 | [diff] [blame] | 150 | FetchIndexed(count, (unsigned short *)((char *)dbg->indexBuffer->data + |
| 151 | (unsigned long)indices), data, dbg); |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 152 | else |
| 153 | FetchIndexed(count, (unsigned short *)indices, data, dbg); |
| 154 | } else |
| 155 | assert(0); |
| 156 | |
| 157 | void * pixels = NULL; |
| 158 | GLint readFormat = 0, readType = 0; |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 159 | int viewport[4] = {}; |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 160 | if (!expectResponse) { |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 161 | cmd.set_function(glesv2debugger::Message_Function_CONTINUE); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 162 | cmd.set_expect_response(false); |
| 163 | } |
| 164 | glesv2debugger::Message_Function oldCmd = cmd.function(); |
David Li | 940c3f8 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 165 | Send(msg, cmd); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 166 | expectResponse = cmd.expect_response(); |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 167 | while (true) { |
| 168 | msg.Clear(); |
| 169 | nsecs_t c0 = systemTime(timeMode); |
| 170 | switch (cmd.function()) { |
| 171 | case glesv2debugger::Message_Function_CONTINUE: |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 172 | dbg->hooks->gl.glDrawElements(mode, count, type, indices); |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 173 | msg.set_time((systemTime(timeMode) - c0) * 1e-6f); |
David Li | 5c425f2 | 2011-03-10 16:40:37 -0800 | [diff] [blame] | 174 | msg.set_context_id(reinterpret_cast<int>(dbg)); |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 175 | msg.set_function(glesv2debugger::Message_Function_glDrawElements); |
| 176 | msg.set_type(glesv2debugger::Message_Type_AfterCall); |
| 177 | msg.set_expect_response(expectResponse); |
David Li | 940c3f8 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 178 | if (!expectResponse) |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 179 | cmd.set_function(glesv2debugger::Message_Function_SKIP); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 180 | if (!expectResponse) { |
| 181 | cmd.set_function(glesv2debugger::Message_Function_SKIP); |
| 182 | cmd.set_expect_response(false); |
| 183 | } |
| 184 | oldCmd = cmd.function(); |
David Li | 940c3f8 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 185 | Send(msg, cmd); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 186 | expectResponse = cmd.expect_response(); |
| 187 | // TODO: pack glReadPixels data with vertex data instead of |
| 188 | // relying on sperate call for transport, this would allow |
| 189 | // auto generated message loop using EXTEND_Debug macro |
David Li | 940c3f8 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 190 | if (capture) { |
| 191 | dbg->hooks->gl.glGetIntegerv(GL_VIEWPORT, viewport); |
| 192 | dbg->hooks->gl.glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &readFormat); |
| 193 | dbg->hooks->gl.glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &readType); |
David Li | 6a5ca48 | 2011-03-21 10:02:30 -0700 | [diff] [blame] | 194 | // LOGD("glDrawArrays CAPTURE: x=%d y=%d width=%d height=%d format=0x%.4X type=0x%.4X", |
| 195 | // viewport[0], viewport[1], viewport[2], viewport[3], readFormat, readType); |
David Li | f9bc124 | 2011-03-22 18:42:03 -0700 | [diff] [blame] | 196 | pixels = dbg->GetReadPixelsBuffer(viewport[2] * viewport[3] * |
| 197 | GetBytesPerPixel(readFormat, readType)); |
David Li | 940c3f8 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 198 | Debug_glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], |
| 199 | readFormat, readType, pixels); |
David Li | 940c3f8 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 200 | } |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 201 | break; |
| 202 | case glesv2debugger::Message_Function_SKIP: |
| 203 | return; |
David Li | 940c3f8 | 2011-03-10 19:07:42 -0800 | [diff] [blame] | 204 | case glesv2debugger::Message_Function_SETPROP: |
David Li | fbfc703 | 2011-03-21 16:25:58 -0700 | [diff] [blame] | 205 | SetProp(dbg, cmd); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 206 | expectResponse = cmd.expect_response(); |
| 207 | if (!expectResponse) // SETPROP is "out of band" |
| 208 | cmd.set_function(oldCmd); |
| 209 | else |
| 210 | Receive(cmd); |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 211 | break; |
| 212 | default: |
David Li | 291f5fe | 2011-03-21 17:42:50 -0700 | [diff] [blame] | 213 | GenerateCall(dbg, cmd, msg, NULL); |
| 214 | msg.set_expect_response(expectResponse); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 215 | if (!expectResponse) { |
David Li | 291f5fe | 2011-03-21 17:42:50 -0700 | [diff] [blame] | 216 | cmd.set_function(cmd.SKIP); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 217 | cmd.set_expect_response(expectResponse); |
| 218 | } |
| 219 | oldCmd = cmd.function(); |
David Li | 291f5fe | 2011-03-21 17:42:50 -0700 | [diff] [blame] | 220 | Send(msg, cmd); |
David Li | e2ad4d0 | 2011-04-08 18:41:00 -0700 | [diff] [blame^] | 221 | expectResponse = cmd.expect_response(); |
David Li | 55c94cc | 2011-03-04 17:50:48 -0800 | [diff] [blame] | 222 | break; |
| 223 | } |
| 224 | } |
| 225 | } |