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