bsalomon@google.com | 7491372 | 2011-10-27 20:44:19 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
tomhudson@google.com | 6bf38b5 | 2012-02-14 15:11:59 +0000 | [diff] [blame] | 9 | #include "gl/SkNullGLContext.h" |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 10 | #include "gl/GrGLInterface.h" |
| 11 | #include "GrGLDefines.h" |
| 12 | #include "GrGLNoOpInterface.h" |
| 13 | #include "SkTDArray.h" |
| 14 | #include "SkTLS.h" |
| 15 | |
bsalomon | 3740972 | 2015-02-11 14:19:18 -0800 | [diff] [blame] | 16 | static SkNullGLContext::ContextState* current_context(); |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 17 | |
| 18 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
| 19 | |
| 20 | class BufferObj { |
| 21 | public: |
mtklein | 2766c00 | 2015-06-26 11:45:03 -0700 | [diff] [blame] | 22 | |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 23 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 24 | BufferObj(GrGLuint id) : fID(id), fDataPtr(nullptr), fSize(0), fMapped(false) {} |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 25 | ~BufferObj() { delete[] fDataPtr; } |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 26 | |
| 27 | void allocate(GrGLsizeiptr size, const GrGLchar* dataPtr) { |
| 28 | if (fDataPtr) { |
| 29 | SkASSERT(0 != fSize); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 30 | delete[] fDataPtr; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | fSize = size; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 34 | fDataPtr = new char[size]; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | GrGLuint id() const { return fID; } |
| 38 | GrGLchar* dataPtr() { return fDataPtr; } |
| 39 | GrGLsizeiptr size() const { return fSize; } |
| 40 | |
| 41 | void setMapped(bool mapped) { fMapped = mapped; } |
| 42 | bool mapped() const { return fMapped; } |
| 43 | |
| 44 | private: |
| 45 | GrGLuint fID; |
| 46 | GrGLchar* fDataPtr; |
| 47 | GrGLsizeiptr fSize; // size in bytes |
| 48 | bool fMapped; |
| 49 | }; |
| 50 | |
| 51 | // This class maintains a sparsely populated array of buffer pointers. |
| 52 | class BufferManager { |
| 53 | public: |
mtklein | 2766c00 | 2015-06-26 11:45:03 -0700 | [diff] [blame] | 54 | |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 55 | |
| 56 | BufferManager() : fFreeListHead(kFreeListEnd) {} |
| 57 | |
| 58 | ~BufferManager() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 59 | // nullptr out the entries that are really free list links rather than ptrs before deleting. |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 60 | intptr_t curr = fFreeListHead; |
| 61 | while (kFreeListEnd != curr) { |
| 62 | intptr_t next = reinterpret_cast<intptr_t>(fBuffers[SkToS32(curr)]); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 63 | fBuffers[SkToS32(curr)] = nullptr; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 64 | curr = next; |
| 65 | } |
| 66 | |
| 67 | fBuffers.deleteAll(); |
| 68 | } |
| 69 | |
| 70 | BufferObj* lookUp(GrGLuint id) { |
| 71 | BufferObj* buffer = fBuffers[id]; |
| 72 | SkASSERT(buffer && buffer->id() == id); |
| 73 | return buffer; |
| 74 | } |
| 75 | |
| 76 | BufferObj* create() { |
| 77 | GrGLuint id; |
| 78 | BufferObj* buffer; |
| 79 | |
| 80 | if (kFreeListEnd == fFreeListHead) { |
| 81 | // no free slots - create a new one |
| 82 | id = fBuffers.count(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 83 | buffer = new BufferObj(id); |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 84 | *fBuffers.append() = buffer; |
| 85 | } else { |
| 86 | // grab the head of the free list and advance the head to the next free slot. |
| 87 | id = static_cast<GrGLuint>(fFreeListHead); |
| 88 | fFreeListHead = reinterpret_cast<intptr_t>(fBuffers[id]); |
| 89 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 90 | buffer = new BufferObj(id); |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 91 | fBuffers[id] = buffer; |
| 92 | } |
| 93 | |
| 94 | return buffer; |
| 95 | } |
| 96 | |
| 97 | void free(BufferObj* buffer) { |
| 98 | SkASSERT(fBuffers.count() > 0); |
| 99 | |
| 100 | GrGLuint id = buffer->id(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 101 | delete buffer; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 102 | |
| 103 | fBuffers[id] = reinterpret_cast<BufferObj*>(fFreeListHead); |
| 104 | fFreeListHead = id; |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | static const intptr_t kFreeListEnd = -1; |
| 109 | // Index of the first entry of fBuffers in the free list. Free slots in fBuffers are indices to |
| 110 | // the next free slot. The last free slot has a value of kFreeListEnd. |
| 111 | intptr_t fFreeListHead; |
| 112 | SkTDArray<BufferObj*> fBuffers; |
| 113 | }; |
| 114 | |
| 115 | /** |
| 116 | * The state object for the null interface. |
| 117 | */ |
bsalomon | 3740972 | 2015-02-11 14:19:18 -0800 | [diff] [blame] | 118 | class SkNullGLContext::ContextState : public SkRefCnt { |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 119 | public: |
mtklein | 2766c00 | 2015-06-26 11:45:03 -0700 | [diff] [blame] | 120 | |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 121 | |
| 122 | BufferManager fBufferManager; |
| 123 | GrGLuint fCurrArrayBuffer; |
| 124 | GrGLuint fCurrElementArrayBuffer; |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 125 | GrGLuint fCurrPixelPackBuffer; |
| 126 | GrGLuint fCurrPixelUnpackBuffer; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 127 | GrGLuint fCurrProgramID; |
| 128 | GrGLuint fCurrShaderID; |
| 129 | |
| 130 | |
| 131 | ContextState() |
| 132 | : fCurrArrayBuffer(0) |
| 133 | , fCurrElementArrayBuffer(0) |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 134 | , fCurrPixelPackBuffer(0) |
| 135 | , fCurrPixelUnpackBuffer(0) |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 136 | , fCurrProgramID(0) |
| 137 | , fCurrShaderID(0) {} |
| 138 | |
bsalomon | 3740972 | 2015-02-11 14:19:18 -0800 | [diff] [blame] | 139 | static ContextState* Get() { return current_context(); } |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | typedef SkNullGLContext::ContextState State; |
| 143 | |
| 144 | // Functions not declared in GrGLBogusInterface.h (not common with the Debug GL interface). |
| 145 | |
| 146 | namespace { // added to suppress 'no previous prototype' warning |
| 147 | |
| 148 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLActiveTexture(GrGLenum texture) {} |
| 149 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLAttachShader(GrGLuint program, GrGLuint shader) {} |
| 150 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLBeginQuery(GrGLenum target, GrGLuint id) {} |
| 151 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLBindAttribLocation(GrGLuint program, GrGLuint index, const char* name) {} |
| 152 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLBindTexture(GrGLenum target, GrGLuint texture) {} |
| 153 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLBindVertexArray(GrGLuint id) {} |
| 154 | |
| 155 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLGenBuffers(GrGLsizei n, GrGLuint* ids) { |
| 156 | State* state = State::Get(); |
| 157 | for (int i = 0; i < n; ++i) { |
| 158 | BufferObj* buffer = state->fBufferManager.create(); |
| 159 | ids[i] = buffer->id(); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLGenerateMipmap(GrGLenum target) {} |
| 164 | |
| 165 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLBufferData(GrGLenum target, |
| 166 | GrGLsizeiptr size, |
| 167 | const GrGLvoid* data, |
| 168 | GrGLenum usage) { |
| 169 | State* state = State::Get(); |
| 170 | GrGLuint id = 0; |
| 171 | |
| 172 | switch (target) { |
| 173 | case GR_GL_ARRAY_BUFFER: |
| 174 | id = state->fCurrArrayBuffer; |
| 175 | break; |
| 176 | case GR_GL_ELEMENT_ARRAY_BUFFER: |
| 177 | id = state->fCurrElementArrayBuffer; |
| 178 | break; |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 179 | case GR_GL_PIXEL_PACK_BUFFER: |
| 180 | id = state->fCurrPixelPackBuffer; |
| 181 | break; |
| 182 | case GR_GL_PIXEL_UNPACK_BUFFER: |
| 183 | id = state->fCurrPixelUnpackBuffer; |
| 184 | break; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 185 | default: |
| 186 | SkFAIL("Unexpected target to nullGLBufferData"); |
| 187 | break; |
| 188 | } |
| 189 | |
| 190 | if (id > 0) { |
| 191 | BufferObj* buffer = state->fBufferManager.lookUp(id); |
| 192 | buffer->allocate(size, (const GrGLchar*) data); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLPixelStorei(GrGLenum pname, GrGLint param) {} |
| 197 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLReadPixels(GrGLint x, GrGLint y, GrGLsizei width, GrGLsizei height, GrGLenum format, GrGLenum type, GrGLvoid* pixels) {} |
| 198 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLUseProgram(GrGLuint program) {} |
| 199 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLViewport(GrGLint x, GrGLint y, GrGLsizei width, GrGLsizei height) {} |
| 200 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLBindFramebuffer(GrGLenum target, GrGLuint framebuffer) {} |
| 201 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLBindRenderbuffer(GrGLenum target, GrGLuint renderbuffer) {} |
| 202 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLDeleteFramebuffers(GrGLsizei n, const GrGLuint *framebuffers) {} |
| 203 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLDeleteRenderbuffers(GrGLsizei n, const GrGLuint *renderbuffers) {} |
| 204 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLFramebufferRenderbuffer(GrGLenum target, GrGLenum attachment, GrGLenum renderbuffertarget, GrGLuint renderbuffer) {} |
| 205 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLFramebufferTexture2D(GrGLenum target, GrGLenum attachment, GrGLenum textarget, GrGLuint texture, GrGLint level) {} |
| 206 | |
| 207 | GrGLuint GR_GL_FUNCTION_TYPE nullGLCreateProgram() { |
| 208 | return ++State::Get()->fCurrProgramID; |
| 209 | } |
| 210 | |
| 211 | GrGLuint GR_GL_FUNCTION_TYPE nullGLCreateShader(GrGLenum type) { |
| 212 | return ++State::Get()->fCurrShaderID; |
| 213 | } |
| 214 | |
| 215 | // same delete used for shaders and programs |
| 216 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLDelete(GrGLuint program) { |
| 217 | } |
| 218 | |
| 219 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLBindBuffer(GrGLenum target, GrGLuint buffer) { |
| 220 | State* state = State::Get(); |
| 221 | switch (target) { |
| 222 | case GR_GL_ARRAY_BUFFER: |
| 223 | state->fCurrArrayBuffer = buffer; |
| 224 | break; |
| 225 | case GR_GL_ELEMENT_ARRAY_BUFFER: |
| 226 | state->fCurrElementArrayBuffer = buffer; |
| 227 | break; |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 228 | case GR_GL_PIXEL_PACK_BUFFER: |
| 229 | state->fCurrPixelPackBuffer = buffer; |
| 230 | break; |
| 231 | case GR_GL_PIXEL_UNPACK_BUFFER: |
| 232 | state->fCurrPixelUnpackBuffer = buffer; |
| 233 | break; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | // deleting a bound buffer has the side effect of binding 0 |
| 238 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLDeleteBuffers(GrGLsizei n, const GrGLuint* ids) { |
| 239 | State* state = State::Get(); |
| 240 | for (int i = 0; i < n; ++i) { |
| 241 | if (ids[i] == state->fCurrArrayBuffer) { |
| 242 | state->fCurrArrayBuffer = 0; |
| 243 | } |
| 244 | if (ids[i] == state->fCurrElementArrayBuffer) { |
| 245 | state->fCurrElementArrayBuffer = 0; |
| 246 | } |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 247 | if (ids[i] == state->fCurrPixelPackBuffer) { |
| 248 | state->fCurrPixelPackBuffer = 0; |
| 249 | } |
| 250 | if (ids[i] == state->fCurrPixelUnpackBuffer) { |
| 251 | state->fCurrPixelUnpackBuffer = 0; |
| 252 | } |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 253 | |
| 254 | BufferObj* buffer = state->fBufferManager.lookUp(ids[i]); |
| 255 | state->fBufferManager.free(buffer); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | GrGLvoid* GR_GL_FUNCTION_TYPE nullGLMapBufferRange(GrGLenum target, GrGLintptr offset, |
| 260 | GrGLsizeiptr length, GrGLbitfield access) { |
| 261 | State* state = State::Get(); |
| 262 | GrGLuint id = 0; |
| 263 | switch (target) { |
| 264 | case GR_GL_ARRAY_BUFFER: |
| 265 | id = state->fCurrArrayBuffer; |
| 266 | break; |
| 267 | case GR_GL_ELEMENT_ARRAY_BUFFER: |
| 268 | id = state->fCurrElementArrayBuffer; |
| 269 | break; |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 270 | case GR_GL_PIXEL_PACK_BUFFER: |
| 271 | id = state->fCurrPixelPackBuffer; |
| 272 | break; |
| 273 | case GR_GL_PIXEL_UNPACK_BUFFER: |
| 274 | id = state->fCurrPixelUnpackBuffer; |
| 275 | break; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | if (id > 0) { |
| 279 | // We just ignore the offset and length here. |
| 280 | BufferObj* buffer = state->fBufferManager.lookUp(id); |
| 281 | SkASSERT(!buffer->mapped()); |
| 282 | buffer->setMapped(true); |
| 283 | return buffer->dataPtr(); |
| 284 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 285 | return nullptr; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | GrGLvoid* GR_GL_FUNCTION_TYPE nullGLMapBuffer(GrGLenum target, GrGLenum access) { |
| 289 | State* state = State::Get(); |
| 290 | GrGLuint id = 0; |
| 291 | switch (target) { |
| 292 | case GR_GL_ARRAY_BUFFER: |
| 293 | id = state->fCurrArrayBuffer; |
| 294 | break; |
| 295 | case GR_GL_ELEMENT_ARRAY_BUFFER: |
| 296 | id = state->fCurrElementArrayBuffer; |
| 297 | break; |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 298 | case GR_GL_PIXEL_PACK_BUFFER: |
| 299 | id = state->fCurrPixelPackBuffer; |
| 300 | break; |
| 301 | case GR_GL_PIXEL_UNPACK_BUFFER: |
| 302 | id = state->fCurrPixelUnpackBuffer; |
| 303 | break; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | if (id > 0) { |
| 307 | BufferObj* buffer = state->fBufferManager.lookUp(id); |
| 308 | SkASSERT(!buffer->mapped()); |
| 309 | buffer->setMapped(true); |
| 310 | return buffer->dataPtr(); |
| 311 | } |
| 312 | |
| 313 | SkASSERT(false); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 314 | return nullptr; // no buffer bound to target |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLFlushMappedBufferRange(GrGLenum target, |
| 318 | GrGLintptr offset, |
| 319 | GrGLsizeiptr length) {} |
| 320 | |
| 321 | |
| 322 | GrGLboolean GR_GL_FUNCTION_TYPE nullGLUnmapBuffer(GrGLenum target) { |
| 323 | State* state = State::Get(); |
| 324 | GrGLuint id = 0; |
| 325 | switch (target) { |
| 326 | case GR_GL_ARRAY_BUFFER: |
| 327 | id = state->fCurrArrayBuffer; |
| 328 | break; |
| 329 | case GR_GL_ELEMENT_ARRAY_BUFFER: |
| 330 | id = state->fCurrElementArrayBuffer; |
| 331 | break; |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 332 | case GR_GL_PIXEL_PACK_BUFFER: |
| 333 | id = state->fCurrPixelPackBuffer; |
| 334 | break; |
| 335 | case GR_GL_PIXEL_UNPACK_BUFFER: |
| 336 | id = state->fCurrPixelUnpackBuffer; |
| 337 | break; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 338 | } |
| 339 | if (id > 0) { |
| 340 | BufferObj* buffer = state->fBufferManager.lookUp(id); |
| 341 | SkASSERT(buffer->mapped()); |
| 342 | buffer->setMapped(false); |
| 343 | return GR_GL_TRUE; |
| 344 | } |
| 345 | |
| 346 | GrAlwaysAssert(false); |
| 347 | return GR_GL_FALSE; // GR_GL_INVALID_OPERATION; |
| 348 | } |
| 349 | |
| 350 | GrGLvoid GR_GL_FUNCTION_TYPE nullGLGetBufferParameteriv(GrGLenum target, GrGLenum pname, GrGLint* params) { |
| 351 | State* state = State::Get(); |
| 352 | switch (pname) { |
| 353 | case GR_GL_BUFFER_MAPPED: { |
| 354 | *params = GR_GL_FALSE; |
| 355 | GrGLuint id = 0; |
| 356 | switch (target) { |
| 357 | case GR_GL_ARRAY_BUFFER: |
| 358 | id = state->fCurrArrayBuffer; |
| 359 | break; |
| 360 | case GR_GL_ELEMENT_ARRAY_BUFFER: |
| 361 | id = state->fCurrElementArrayBuffer; |
| 362 | break; |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 363 | case GR_GL_PIXEL_PACK_BUFFER: |
| 364 | id = state->fCurrPixelPackBuffer; |
| 365 | break; |
| 366 | case GR_GL_PIXEL_UNPACK_BUFFER: |
| 367 | id = state->fCurrPixelUnpackBuffer; |
| 368 | break; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 369 | } |
| 370 | if (id > 0) { |
| 371 | BufferObj* buffer = state->fBufferManager.lookUp(id); |
| 372 | if (buffer->mapped()) { |
| 373 | *params = GR_GL_TRUE; |
| 374 | } |
| 375 | } |
| 376 | break; } |
| 377 | default: |
| 378 | SkFAIL("Unexpected pname to GetBufferParamateriv"); |
| 379 | break; |
| 380 | } |
| 381 | }; |
| 382 | |
bsalomon | 3740972 | 2015-02-11 14:19:18 -0800 | [diff] [blame] | 383 | class NullInterface : public GrGLInterface { |
| 384 | public: |
| 385 | NullInterface(State* state) : fState(SkRef(state)) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 386 | ~NullInterface() override { |
bsalomon | 3740972 | 2015-02-11 14:19:18 -0800 | [diff] [blame] | 387 | fState->unref(); |
| 388 | } |
| 389 | State* fState; |
| 390 | }; |
| 391 | |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 392 | } // end anonymous namespace |
| 393 | |
bsalomon | 3740972 | 2015-02-11 14:19:18 -0800 | [diff] [blame] | 394 | static GrGLInterface* create_null_interface(State* state) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 395 | GrGLInterface* interface = new NullInterface(state); |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 396 | |
| 397 | interface->fStandard = kGL_GrGLStandard; |
| 398 | |
| 399 | GrGLInterface::Functions* functions = &interface->fFunctions; |
| 400 | functions->fActiveTexture = nullGLActiveTexture; |
| 401 | functions->fAttachShader = nullGLAttachShader; |
| 402 | functions->fBeginQuery = nullGLBeginQuery; |
| 403 | functions->fBindAttribLocation = nullGLBindAttribLocation; |
| 404 | functions->fBindBuffer = nullGLBindBuffer; |
| 405 | functions->fBindFragDataLocation = noOpGLBindFragDataLocation; |
| 406 | functions->fBindTexture = nullGLBindTexture; |
| 407 | functions->fBindVertexArray = nullGLBindVertexArray; |
| 408 | functions->fBlendColor = noOpGLBlendColor; |
cdalton | bae6f6c | 2015-04-22 10:39:03 -0700 | [diff] [blame] | 409 | functions->fBlendEquation = noOpGLBlendEquation; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 410 | functions->fBlendFunc = noOpGLBlendFunc; |
| 411 | functions->fBufferData = nullGLBufferData; |
| 412 | functions->fBufferSubData = noOpGLBufferSubData; |
| 413 | functions->fClear = noOpGLClear; |
| 414 | functions->fClearColor = noOpGLClearColor; |
| 415 | functions->fClearStencil = noOpGLClearStencil; |
| 416 | functions->fColorMask = noOpGLColorMask; |
| 417 | functions->fCompileShader = noOpGLCompileShader; |
| 418 | functions->fCompressedTexImage2D = noOpGLCompressedTexImage2D; |
| 419 | functions->fCompressedTexSubImage2D = noOpGLCompressedTexSubImage2D; |
| 420 | functions->fCopyTexSubImage2D = noOpGLCopyTexSubImage2D; |
| 421 | functions->fCreateProgram = nullGLCreateProgram; |
| 422 | functions->fCreateShader = nullGLCreateShader; |
| 423 | functions->fCullFace = noOpGLCullFace; |
| 424 | functions->fDeleteBuffers = nullGLDeleteBuffers; |
| 425 | functions->fDeleteProgram = nullGLDelete; |
| 426 | functions->fDeleteQueries = noOpGLDeleteIds; |
| 427 | functions->fDeleteShader = nullGLDelete; |
| 428 | functions->fDeleteTextures = noOpGLDeleteIds; |
| 429 | functions->fDeleteVertexArrays = noOpGLDeleteIds; |
| 430 | functions->fDepthMask = noOpGLDepthMask; |
| 431 | functions->fDisable = noOpGLDisable; |
| 432 | functions->fDisableVertexAttribArray = noOpGLDisableVertexAttribArray; |
| 433 | functions->fDrawArrays = noOpGLDrawArrays; |
cdalton | 626e1ff | 2015-06-12 13:56:46 -0700 | [diff] [blame] | 434 | functions->fDrawArraysInstanced = noOpGLDrawArraysInstanced; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 435 | functions->fDrawBuffer = noOpGLDrawBuffer; |
| 436 | functions->fDrawBuffers = noOpGLDrawBuffers; |
| 437 | functions->fDrawElements = noOpGLDrawElements; |
cdalton | 626e1ff | 2015-06-12 13:56:46 -0700 | [diff] [blame] | 438 | functions->fDrawElementsInstanced = noOpGLDrawElementsInstanced; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 439 | functions->fEnable = noOpGLEnable; |
| 440 | functions->fEnableVertexAttribArray = noOpGLEnableVertexAttribArray; |
| 441 | functions->fEndQuery = noOpGLEndQuery; |
| 442 | functions->fFinish = noOpGLFinish; |
| 443 | functions->fFlush = noOpGLFlush; |
| 444 | functions->fFlushMappedBufferRange = nullGLFlushMappedBufferRange; |
| 445 | functions->fFrontFace = noOpGLFrontFace; |
| 446 | functions->fGenBuffers = nullGLGenBuffers; |
| 447 | functions->fGenerateMipmap = nullGLGenerateMipmap; |
| 448 | functions->fGenQueries = noOpGLGenIds; |
| 449 | functions->fGenTextures = noOpGLGenIds; |
| 450 | functions->fGenVertexArrays = noOpGLGenIds; |
| 451 | functions->fGetBufferParameteriv = nullGLGetBufferParameteriv; |
| 452 | functions->fGetError = noOpGLGetError; |
| 453 | functions->fGetIntegerv = noOpGLGetIntegerv; |
| 454 | functions->fGetQueryObjecti64v = noOpGLGetQueryObjecti64v; |
| 455 | functions->fGetQueryObjectiv = noOpGLGetQueryObjectiv; |
| 456 | functions->fGetQueryObjectui64v = noOpGLGetQueryObjectui64v; |
| 457 | functions->fGetQueryObjectuiv = noOpGLGetQueryObjectuiv; |
| 458 | functions->fGetQueryiv = noOpGLGetQueryiv; |
| 459 | functions->fGetProgramInfoLog = noOpGLGetInfoLog; |
| 460 | functions->fGetProgramiv = noOpGLGetShaderOrProgramiv; |
| 461 | functions->fGetShaderInfoLog = noOpGLGetInfoLog; |
| 462 | functions->fGetShaderiv = noOpGLGetShaderOrProgramiv; |
| 463 | functions->fGetString = noOpGLGetString; |
| 464 | functions->fGetStringi = noOpGLGetStringi; |
| 465 | functions->fGetTexLevelParameteriv = noOpGLGetTexLevelParameteriv; |
| 466 | functions->fGetUniformLocation = noOpGLGetUniformLocation; |
| 467 | functions->fInsertEventMarker = noOpGLInsertEventMarker; |
| 468 | functions->fLineWidth = noOpGLLineWidth; |
| 469 | functions->fLinkProgram = noOpGLLinkProgram; |
| 470 | functions->fMapBuffer = nullGLMapBuffer; |
| 471 | functions->fMapBufferRange = nullGLMapBufferRange; |
| 472 | functions->fPixelStorei = nullGLPixelStorei; |
| 473 | functions->fPopGroupMarker = noOpGLPopGroupMarker; |
| 474 | functions->fPushGroupMarker = noOpGLPushGroupMarker; |
| 475 | functions->fQueryCounter = noOpGLQueryCounter; |
| 476 | functions->fReadBuffer = noOpGLReadBuffer; |
| 477 | functions->fReadPixels = nullGLReadPixels; |
| 478 | functions->fScissor = noOpGLScissor; |
| 479 | functions->fShaderSource = noOpGLShaderSource; |
| 480 | functions->fStencilFunc = noOpGLStencilFunc; |
| 481 | functions->fStencilFuncSeparate = noOpGLStencilFuncSeparate; |
| 482 | functions->fStencilMask = noOpGLStencilMask; |
| 483 | functions->fStencilMaskSeparate = noOpGLStencilMaskSeparate; |
| 484 | functions->fStencilOp = noOpGLStencilOp; |
| 485 | functions->fStencilOpSeparate = noOpGLStencilOpSeparate; |
| 486 | functions->fTexImage2D = noOpGLTexImage2D; |
| 487 | functions->fTexParameteri = noOpGLTexParameteri; |
| 488 | functions->fTexParameteriv = noOpGLTexParameteriv; |
| 489 | functions->fTexSubImage2D = noOpGLTexSubImage2D; |
| 490 | functions->fTexStorage2D = noOpGLTexStorage2D; |
| 491 | functions->fDiscardFramebuffer = noOpGLDiscardFramebuffer; |
| 492 | functions->fUniform1f = noOpGLUniform1f; |
| 493 | functions->fUniform1i = noOpGLUniform1i; |
| 494 | functions->fUniform1fv = noOpGLUniform1fv; |
| 495 | functions->fUniform1iv = noOpGLUniform1iv; |
| 496 | functions->fUniform2f = noOpGLUniform2f; |
| 497 | functions->fUniform2i = noOpGLUniform2i; |
| 498 | functions->fUniform2fv = noOpGLUniform2fv; |
| 499 | functions->fUniform2iv = noOpGLUniform2iv; |
| 500 | functions->fUniform3f = noOpGLUniform3f; |
| 501 | functions->fUniform3i = noOpGLUniform3i; |
| 502 | functions->fUniform3fv = noOpGLUniform3fv; |
| 503 | functions->fUniform3iv = noOpGLUniform3iv; |
| 504 | functions->fUniform4f = noOpGLUniform4f; |
| 505 | functions->fUniform4i = noOpGLUniform4i; |
| 506 | functions->fUniform4fv = noOpGLUniform4fv; |
| 507 | functions->fUniform4iv = noOpGLUniform4iv; |
| 508 | functions->fUniformMatrix2fv = noOpGLUniformMatrix2fv; |
| 509 | functions->fUniformMatrix3fv = noOpGLUniformMatrix3fv; |
| 510 | functions->fUniformMatrix4fv = noOpGLUniformMatrix4fv; |
| 511 | functions->fUnmapBuffer = nullGLUnmapBuffer; |
| 512 | functions->fUseProgram = nullGLUseProgram; |
| 513 | functions->fVertexAttrib1f = noOpGLVertexAttrib1f; |
| 514 | functions->fVertexAttrib2fv = noOpGLVertexAttrib2fv; |
| 515 | functions->fVertexAttrib3fv = noOpGLVertexAttrib3fv; |
| 516 | functions->fVertexAttrib4fv = noOpGLVertexAttrib4fv; |
| 517 | functions->fVertexAttribPointer = noOpGLVertexAttribPointer; |
cdalton | 626e1ff | 2015-06-12 13:56:46 -0700 | [diff] [blame] | 518 | functions->fVertexAttribDivisor = noOpGLVertexAttribDivisor; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 519 | functions->fViewport = nullGLViewport; |
| 520 | functions->fBindFramebuffer = nullGLBindFramebuffer; |
| 521 | functions->fBindRenderbuffer = nullGLBindRenderbuffer; |
| 522 | functions->fCheckFramebufferStatus = noOpGLCheckFramebufferStatus; |
| 523 | functions->fDeleteFramebuffers = nullGLDeleteFramebuffers; |
| 524 | functions->fDeleteRenderbuffers = nullGLDeleteRenderbuffers; |
| 525 | functions->fFramebufferRenderbuffer = nullGLFramebufferRenderbuffer; |
| 526 | functions->fFramebufferTexture2D = nullGLFramebufferTexture2D; |
| 527 | functions->fGenFramebuffers = noOpGLGenIds; |
| 528 | functions->fGenRenderbuffers = noOpGLGenIds; |
| 529 | functions->fGetFramebufferAttachmentParameteriv = noOpGLGetFramebufferAttachmentParameteriv; |
| 530 | functions->fGetRenderbufferParameteriv = noOpGLGetRenderbufferParameteriv; |
| 531 | functions->fRenderbufferStorage = noOpGLRenderbufferStorage; |
| 532 | functions->fRenderbufferStorageMultisample = noOpGLRenderbufferStorageMultisample; |
| 533 | functions->fBlitFramebuffer = noOpGLBlitFramebuffer; |
| 534 | functions->fResolveMultisampleFramebuffer = noOpGLResolveMultisampleFramebuffer; |
| 535 | functions->fMatrixLoadf = noOpGLMatrixLoadf; |
| 536 | functions->fMatrixLoadIdentity = noOpGLMatrixLoadIdentity; |
| 537 | functions->fBindFragDataLocationIndexed = noOpGLBindFragDataLocationIndexed; |
| 538 | |
| 539 | interface->fExtensions.init(kGL_GrGLStandard, functions->fGetString, functions->fGetStringi, |
bsalomon | b1a32ad | 2015-11-16 06:48:44 -0800 | [diff] [blame] | 540 | functions->fGetIntegerv, nullptr, GR_EGL_NO_DISPLAY); |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 541 | return interface; |
| 542 | } |
| 543 | |
| 544 | ////////////////////////////////////////////////////////////////////////////// |
| 545 | |
| 546 | static void* create_tls() { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 547 | State** current = new State*; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 548 | *current = nullptr; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 549 | return current; |
| 550 | } |
| 551 | |
| 552 | static void delete_tls(void* ctx) { |
bsalomon | 3740972 | 2015-02-11 14:19:18 -0800 | [diff] [blame] | 553 | State** current = static_cast<State**>(ctx); |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 554 | if (*current) { |
| 555 | (*current)->unref(); |
| 556 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 557 | delete current; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 558 | } |
| 559 | |
bsalomon | 3740972 | 2015-02-11 14:19:18 -0800 | [diff] [blame] | 560 | static State* current_context() { |
| 561 | return *static_cast<State**>(SkTLS::Get(create_tls, delete_tls)); |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 562 | } |
| 563 | |
bsalomon | 3740972 | 2015-02-11 14:19:18 -0800 | [diff] [blame] | 564 | static void set_current_context(State* state) { |
| 565 | State** current = static_cast<State**>(SkTLS::Get(create_tls, delete_tls)); |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 566 | if (*current) { |
| 567 | (*current)->unref(); |
| 568 | } |
bsalomon | 3740972 | 2015-02-11 14:19:18 -0800 | [diff] [blame] | 569 | *current = state; |
| 570 | if (state) { |
| 571 | state->ref(); |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
bsalomon | 3740972 | 2015-02-11 14:19:18 -0800 | [diff] [blame] | 575 | #if GR_GL_PER_GL_FUNC_CALLBACK |
| 576 | static void set_current_context_from_interface(const GrGLInterface* interface) { |
| 577 | set_current_context(reinterpret_cast<State*>(interface->fCallbackData)); |
| 578 | } |
| 579 | #endif |
| 580 | |
rmistry | 0f515bd | 2015-12-22 10:22:26 -0800 | [diff] [blame^] | 581 | SkNullGLContext* SkNullGLContext::Create(GrGLStandard forcedGpuAPI) { |
| 582 | if (kGLES_GrGLStandard == forcedGpuAPI) { |
| 583 | return nullptr; |
| 584 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 585 | SkNullGLContext* ctx = new SkNullGLContext; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 586 | if (!ctx->isValid()) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 587 | delete ctx; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 588 | return nullptr; |
bsalomon | bb0502e | 2015-02-11 11:11:11 -0800 | [diff] [blame] | 589 | } |
| 590 | return ctx; |
| 591 | } |
bsalomon@google.com | 7491372 | 2011-10-27 20:44:19 +0000 | [diff] [blame] | 592 | |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 593 | SkNullGLContext::SkNullGLContext() { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 594 | fState = new ContextState; |
bsalomon | 3740972 | 2015-02-11 14:19:18 -0800 | [diff] [blame] | 595 | GrGLInterface* interface = create_null_interface(fState); |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 596 | this->init(interface); |
bsalomon | 3740972 | 2015-02-11 14:19:18 -0800 | [diff] [blame] | 597 | #if GR_GL_PER_GL_FUNC_CALLBACK |
| 598 | interface->fCallback = set_current_context_from_interface; |
| 599 | interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(fState); |
| 600 | #endif |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | SkNullGLContext::~SkNullGLContext() { |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 604 | this->teardown(); |
bsalomon | 3740972 | 2015-02-11 14:19:18 -0800 | [diff] [blame] | 605 | fState->unref(); |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 606 | } |
| 607 | |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 608 | void SkNullGLContext::onPlatformMakeCurrent() const { set_current_context(fState); } |