blob: 7ced85e89e2d12ccbb0091eb9c4321c437f00f16 [file] [log] [blame]
bsalomon@google.com74913722011-10-27 20:44:19 +00001
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.com6bf38b52012-02-14 15:11:59 +00009#include "gl/SkNullGLContext.h"
bsalomonbb0502e2015-02-11 11:11:11 -080010#include "gl/GrGLInterface.h"
11#include "GrGLDefines.h"
12#include "GrGLNoOpInterface.h"
13#include "SkTDArray.h"
14#include "SkTLS.h"
15
bsalomon37409722015-02-11 14:19:18 -080016static SkNullGLContext::ContextState* current_context();
bsalomonbb0502e2015-02-11 11:11:11 -080017
18/////////////////////////////////////////////////////////////////////////////////////////////////
19
20class BufferObj {
21public:
22 SK_DECLARE_INST_COUNT(BufferObj);
23
24 BufferObj(GrGLuint id) : fID(id), fDataPtr(NULL), fSize(0), fMapped(false) {}
25 ~BufferObj() { SkDELETE_ARRAY(fDataPtr); }
26
27 void allocate(GrGLsizeiptr size, const GrGLchar* dataPtr) {
28 if (fDataPtr) {
29 SkASSERT(0 != fSize);
30 SkDELETE_ARRAY(fDataPtr);
31 }
32
33 fSize = size;
34 fDataPtr = SkNEW_ARRAY(char, size);
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
44private:
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.
52class BufferManager {
53public:
54 SK_DECLARE_INST_COUNT(BufferManager);
55
56 BufferManager() : fFreeListHead(kFreeListEnd) {}
57
58 ~BufferManager() {
59 // NULL out the entries that are really free list links rather than ptrs before deleting.
60 intptr_t curr = fFreeListHead;
61 while (kFreeListEnd != curr) {
62 intptr_t next = reinterpret_cast<intptr_t>(fBuffers[SkToS32(curr)]);
63 fBuffers[SkToS32(curr)] = NULL;
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();
83 buffer = SkNEW_ARGS(BufferObj, (id));
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
90 buffer = SkNEW_ARGS(BufferObj, (id));
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();
101 SkDELETE(buffer);
102
103 fBuffers[id] = reinterpret_cast<BufferObj*>(fFreeListHead);
104 fFreeListHead = id;
105 }
106
107private:
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 */
bsalomon37409722015-02-11 14:19:18 -0800118class SkNullGLContext::ContextState : public SkRefCnt {
bsalomonbb0502e2015-02-11 11:11:11 -0800119public:
120 SK_DECLARE_INST_COUNT(ContextState);
121
122 BufferManager fBufferManager;
123 GrGLuint fCurrArrayBuffer;
124 GrGLuint fCurrElementArrayBuffer;
125 GrGLuint fCurrProgramID;
126 GrGLuint fCurrShaderID;
127
128
129 ContextState()
130 : fCurrArrayBuffer(0)
131 , fCurrElementArrayBuffer(0)
132 , fCurrProgramID(0)
133 , fCurrShaderID(0) {}
134
bsalomon37409722015-02-11 14:19:18 -0800135 static ContextState* Get() { return current_context(); }
bsalomonbb0502e2015-02-11 11:11:11 -0800136};
137
138typedef SkNullGLContext::ContextState State;
139
140// Functions not declared in GrGLBogusInterface.h (not common with the Debug GL interface).
141
142namespace { // added to suppress 'no previous prototype' warning
143
144GrGLvoid GR_GL_FUNCTION_TYPE nullGLActiveTexture(GrGLenum texture) {}
145GrGLvoid GR_GL_FUNCTION_TYPE nullGLAttachShader(GrGLuint program, GrGLuint shader) {}
146GrGLvoid GR_GL_FUNCTION_TYPE nullGLBeginQuery(GrGLenum target, GrGLuint id) {}
147GrGLvoid GR_GL_FUNCTION_TYPE nullGLBindAttribLocation(GrGLuint program, GrGLuint index, const char* name) {}
148GrGLvoid GR_GL_FUNCTION_TYPE nullGLBindTexture(GrGLenum target, GrGLuint texture) {}
149GrGLvoid GR_GL_FUNCTION_TYPE nullGLBindVertexArray(GrGLuint id) {}
150
151GrGLvoid GR_GL_FUNCTION_TYPE nullGLGenBuffers(GrGLsizei n, GrGLuint* ids) {
152 State* state = State::Get();
153 for (int i = 0; i < n; ++i) {
154 BufferObj* buffer = state->fBufferManager.create();
155 ids[i] = buffer->id();
156 }
157}
158
159GrGLvoid GR_GL_FUNCTION_TYPE nullGLGenerateMipmap(GrGLenum target) {}
160
161GrGLvoid GR_GL_FUNCTION_TYPE nullGLBufferData(GrGLenum target,
162 GrGLsizeiptr size,
163 const GrGLvoid* data,
164 GrGLenum usage) {
165 State* state = State::Get();
166 GrGLuint id = 0;
167
168 switch (target) {
169 case GR_GL_ARRAY_BUFFER:
170 id = state->fCurrArrayBuffer;
171 break;
172 case GR_GL_ELEMENT_ARRAY_BUFFER:
173 id = state->fCurrElementArrayBuffer;
174 break;
175 default:
176 SkFAIL("Unexpected target to nullGLBufferData");
177 break;
178 }
179
180 if (id > 0) {
181 BufferObj* buffer = state->fBufferManager.lookUp(id);
182 buffer->allocate(size, (const GrGLchar*) data);
183 }
184}
185
186GrGLvoid GR_GL_FUNCTION_TYPE nullGLPixelStorei(GrGLenum pname, GrGLint param) {}
187GrGLvoid GR_GL_FUNCTION_TYPE nullGLReadPixels(GrGLint x, GrGLint y, GrGLsizei width, GrGLsizei height, GrGLenum format, GrGLenum type, GrGLvoid* pixels) {}
188GrGLvoid GR_GL_FUNCTION_TYPE nullGLUseProgram(GrGLuint program) {}
189GrGLvoid GR_GL_FUNCTION_TYPE nullGLViewport(GrGLint x, GrGLint y, GrGLsizei width, GrGLsizei height) {}
190GrGLvoid GR_GL_FUNCTION_TYPE nullGLBindFramebuffer(GrGLenum target, GrGLuint framebuffer) {}
191GrGLvoid GR_GL_FUNCTION_TYPE nullGLBindRenderbuffer(GrGLenum target, GrGLuint renderbuffer) {}
192GrGLvoid GR_GL_FUNCTION_TYPE nullGLDeleteFramebuffers(GrGLsizei n, const GrGLuint *framebuffers) {}
193GrGLvoid GR_GL_FUNCTION_TYPE nullGLDeleteRenderbuffers(GrGLsizei n, const GrGLuint *renderbuffers) {}
194GrGLvoid GR_GL_FUNCTION_TYPE nullGLFramebufferRenderbuffer(GrGLenum target, GrGLenum attachment, GrGLenum renderbuffertarget, GrGLuint renderbuffer) {}
195GrGLvoid GR_GL_FUNCTION_TYPE nullGLFramebufferTexture2D(GrGLenum target, GrGLenum attachment, GrGLenum textarget, GrGLuint texture, GrGLint level) {}
196
197GrGLuint GR_GL_FUNCTION_TYPE nullGLCreateProgram() {
198 return ++State::Get()->fCurrProgramID;
199}
200
201GrGLuint GR_GL_FUNCTION_TYPE nullGLCreateShader(GrGLenum type) {
202 return ++State::Get()->fCurrShaderID;
203}
204
205// same delete used for shaders and programs
206GrGLvoid GR_GL_FUNCTION_TYPE nullGLDelete(GrGLuint program) {
207}
208
209GrGLvoid GR_GL_FUNCTION_TYPE nullGLBindBuffer(GrGLenum target, GrGLuint buffer) {
210 State* state = State::Get();
211 switch (target) {
212 case GR_GL_ARRAY_BUFFER:
213 state->fCurrArrayBuffer = buffer;
214 break;
215 case GR_GL_ELEMENT_ARRAY_BUFFER:
216 state->fCurrElementArrayBuffer = buffer;
217 break;
218 }
219}
220
221// deleting a bound buffer has the side effect of binding 0
222GrGLvoid GR_GL_FUNCTION_TYPE nullGLDeleteBuffers(GrGLsizei n, const GrGLuint* ids) {
223 State* state = State::Get();
224 for (int i = 0; i < n; ++i) {
225 if (ids[i] == state->fCurrArrayBuffer) {
226 state->fCurrArrayBuffer = 0;
227 }
228 if (ids[i] == state->fCurrElementArrayBuffer) {
229 state->fCurrElementArrayBuffer = 0;
230 }
231
232 BufferObj* buffer = state->fBufferManager.lookUp(ids[i]);
233 state->fBufferManager.free(buffer);
234 }
235}
236
237GrGLvoid* GR_GL_FUNCTION_TYPE nullGLMapBufferRange(GrGLenum target, GrGLintptr offset,
238 GrGLsizeiptr length, GrGLbitfield access) {
239 State* state = State::Get();
240 GrGLuint id = 0;
241 switch (target) {
242 case GR_GL_ARRAY_BUFFER:
243 id = state->fCurrArrayBuffer;
244 break;
245 case GR_GL_ELEMENT_ARRAY_BUFFER:
246 id = state->fCurrElementArrayBuffer;
247 break;
248 }
249
250 if (id > 0) {
251 // We just ignore the offset and length here.
252 BufferObj* buffer = state->fBufferManager.lookUp(id);
253 SkASSERT(!buffer->mapped());
254 buffer->setMapped(true);
255 return buffer->dataPtr();
256 }
257 return NULL;
258}
259
260GrGLvoid* GR_GL_FUNCTION_TYPE nullGLMapBuffer(GrGLenum target, GrGLenum 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;
270 }
271
272 if (id > 0) {
273 BufferObj* buffer = state->fBufferManager.lookUp(id);
274 SkASSERT(!buffer->mapped());
275 buffer->setMapped(true);
276 return buffer->dataPtr();
277 }
278
279 SkASSERT(false);
280 return NULL; // no buffer bound to target
281}
282
283GrGLvoid GR_GL_FUNCTION_TYPE nullGLFlushMappedBufferRange(GrGLenum target,
284 GrGLintptr offset,
285 GrGLsizeiptr length) {}
286
287
288GrGLboolean GR_GL_FUNCTION_TYPE nullGLUnmapBuffer(GrGLenum target) {
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;
298 }
299 if (id > 0) {
300 BufferObj* buffer = state->fBufferManager.lookUp(id);
301 SkASSERT(buffer->mapped());
302 buffer->setMapped(false);
303 return GR_GL_TRUE;
304 }
305
306 GrAlwaysAssert(false);
307 return GR_GL_FALSE; // GR_GL_INVALID_OPERATION;
308}
309
310GrGLvoid GR_GL_FUNCTION_TYPE nullGLGetBufferParameteriv(GrGLenum target, GrGLenum pname, GrGLint* params) {
311 State* state = State::Get();
312 switch (pname) {
313 case GR_GL_BUFFER_MAPPED: {
314 *params = GR_GL_FALSE;
315 GrGLuint id = 0;
316 switch (target) {
317 case GR_GL_ARRAY_BUFFER:
318 id = state->fCurrArrayBuffer;
319 break;
320 case GR_GL_ELEMENT_ARRAY_BUFFER:
321 id = state->fCurrElementArrayBuffer;
322 break;
323 }
324 if (id > 0) {
325 BufferObj* buffer = state->fBufferManager.lookUp(id);
326 if (buffer->mapped()) {
327 *params = GR_GL_TRUE;
328 }
329 }
330 break; }
331 default:
332 SkFAIL("Unexpected pname to GetBufferParamateriv");
333 break;
334 }
335};
336
bsalomon37409722015-02-11 14:19:18 -0800337class NullInterface : public GrGLInterface {
338public:
339 NullInterface(State* state) : fState(SkRef(state)) {}
mtklein36352bf2015-03-25 18:17:31 -0700340 ~NullInterface() override {
bsalomon37409722015-02-11 14:19:18 -0800341 fState->unref();
342 }
343 State* fState;
344};
345
bsalomonbb0502e2015-02-11 11:11:11 -0800346} // end anonymous namespace
347
bsalomon37409722015-02-11 14:19:18 -0800348static GrGLInterface* create_null_interface(State* state) {
349 GrGLInterface* interface = SkNEW_ARGS(NullInterface, (state));
bsalomonbb0502e2015-02-11 11:11:11 -0800350
351 interface->fStandard = kGL_GrGLStandard;
352
353 GrGLInterface::Functions* functions = &interface->fFunctions;
354 functions->fActiveTexture = nullGLActiveTexture;
355 functions->fAttachShader = nullGLAttachShader;
356 functions->fBeginQuery = nullGLBeginQuery;
357 functions->fBindAttribLocation = nullGLBindAttribLocation;
358 functions->fBindBuffer = nullGLBindBuffer;
359 functions->fBindFragDataLocation = noOpGLBindFragDataLocation;
360 functions->fBindTexture = nullGLBindTexture;
361 functions->fBindVertexArray = nullGLBindVertexArray;
362 functions->fBlendColor = noOpGLBlendColor;
cdaltonbae6f6c2015-04-22 10:39:03 -0700363 functions->fBlendEquation = noOpGLBlendEquation;
bsalomonbb0502e2015-02-11 11:11:11 -0800364 functions->fBlendFunc = noOpGLBlendFunc;
365 functions->fBufferData = nullGLBufferData;
366 functions->fBufferSubData = noOpGLBufferSubData;
367 functions->fClear = noOpGLClear;
368 functions->fClearColor = noOpGLClearColor;
369 functions->fClearStencil = noOpGLClearStencil;
370 functions->fColorMask = noOpGLColorMask;
371 functions->fCompileShader = noOpGLCompileShader;
372 functions->fCompressedTexImage2D = noOpGLCompressedTexImage2D;
373 functions->fCompressedTexSubImage2D = noOpGLCompressedTexSubImage2D;
374 functions->fCopyTexSubImage2D = noOpGLCopyTexSubImage2D;
375 functions->fCreateProgram = nullGLCreateProgram;
376 functions->fCreateShader = nullGLCreateShader;
377 functions->fCullFace = noOpGLCullFace;
378 functions->fDeleteBuffers = nullGLDeleteBuffers;
379 functions->fDeleteProgram = nullGLDelete;
380 functions->fDeleteQueries = noOpGLDeleteIds;
381 functions->fDeleteShader = nullGLDelete;
382 functions->fDeleteTextures = noOpGLDeleteIds;
383 functions->fDeleteVertexArrays = noOpGLDeleteIds;
384 functions->fDepthMask = noOpGLDepthMask;
385 functions->fDisable = noOpGLDisable;
386 functions->fDisableVertexAttribArray = noOpGLDisableVertexAttribArray;
387 functions->fDrawArrays = noOpGLDrawArrays;
cdalton626e1ff2015-06-12 13:56:46 -0700388 functions->fDrawArraysInstanced = noOpGLDrawArraysInstanced;
bsalomonbb0502e2015-02-11 11:11:11 -0800389 functions->fDrawBuffer = noOpGLDrawBuffer;
390 functions->fDrawBuffers = noOpGLDrawBuffers;
391 functions->fDrawElements = noOpGLDrawElements;
cdalton626e1ff2015-06-12 13:56:46 -0700392 functions->fDrawElementsInstanced = noOpGLDrawElementsInstanced;
bsalomonbb0502e2015-02-11 11:11:11 -0800393 functions->fEnable = noOpGLEnable;
394 functions->fEnableVertexAttribArray = noOpGLEnableVertexAttribArray;
395 functions->fEndQuery = noOpGLEndQuery;
396 functions->fFinish = noOpGLFinish;
397 functions->fFlush = noOpGLFlush;
398 functions->fFlushMappedBufferRange = nullGLFlushMappedBufferRange;
399 functions->fFrontFace = noOpGLFrontFace;
400 functions->fGenBuffers = nullGLGenBuffers;
401 functions->fGenerateMipmap = nullGLGenerateMipmap;
402 functions->fGenQueries = noOpGLGenIds;
403 functions->fGenTextures = noOpGLGenIds;
404 functions->fGenVertexArrays = noOpGLGenIds;
405 functions->fGetBufferParameteriv = nullGLGetBufferParameteriv;
406 functions->fGetError = noOpGLGetError;
407 functions->fGetIntegerv = noOpGLGetIntegerv;
408 functions->fGetQueryObjecti64v = noOpGLGetQueryObjecti64v;
409 functions->fGetQueryObjectiv = noOpGLGetQueryObjectiv;
410 functions->fGetQueryObjectui64v = noOpGLGetQueryObjectui64v;
411 functions->fGetQueryObjectuiv = noOpGLGetQueryObjectuiv;
412 functions->fGetQueryiv = noOpGLGetQueryiv;
413 functions->fGetProgramInfoLog = noOpGLGetInfoLog;
414 functions->fGetProgramiv = noOpGLGetShaderOrProgramiv;
415 functions->fGetShaderInfoLog = noOpGLGetInfoLog;
416 functions->fGetShaderiv = noOpGLGetShaderOrProgramiv;
417 functions->fGetString = noOpGLGetString;
418 functions->fGetStringi = noOpGLGetStringi;
419 functions->fGetTexLevelParameteriv = noOpGLGetTexLevelParameteriv;
420 functions->fGetUniformLocation = noOpGLGetUniformLocation;
421 functions->fInsertEventMarker = noOpGLInsertEventMarker;
422 functions->fLineWidth = noOpGLLineWidth;
423 functions->fLinkProgram = noOpGLLinkProgram;
424 functions->fMapBuffer = nullGLMapBuffer;
425 functions->fMapBufferRange = nullGLMapBufferRange;
426 functions->fPixelStorei = nullGLPixelStorei;
427 functions->fPopGroupMarker = noOpGLPopGroupMarker;
428 functions->fPushGroupMarker = noOpGLPushGroupMarker;
429 functions->fQueryCounter = noOpGLQueryCounter;
430 functions->fReadBuffer = noOpGLReadBuffer;
431 functions->fReadPixels = nullGLReadPixels;
432 functions->fScissor = noOpGLScissor;
433 functions->fShaderSource = noOpGLShaderSource;
434 functions->fStencilFunc = noOpGLStencilFunc;
435 functions->fStencilFuncSeparate = noOpGLStencilFuncSeparate;
436 functions->fStencilMask = noOpGLStencilMask;
437 functions->fStencilMaskSeparate = noOpGLStencilMaskSeparate;
438 functions->fStencilOp = noOpGLStencilOp;
439 functions->fStencilOpSeparate = noOpGLStencilOpSeparate;
440 functions->fTexImage2D = noOpGLTexImage2D;
441 functions->fTexParameteri = noOpGLTexParameteri;
442 functions->fTexParameteriv = noOpGLTexParameteriv;
443 functions->fTexSubImage2D = noOpGLTexSubImage2D;
444 functions->fTexStorage2D = noOpGLTexStorage2D;
445 functions->fDiscardFramebuffer = noOpGLDiscardFramebuffer;
446 functions->fUniform1f = noOpGLUniform1f;
447 functions->fUniform1i = noOpGLUniform1i;
448 functions->fUniform1fv = noOpGLUniform1fv;
449 functions->fUniform1iv = noOpGLUniform1iv;
450 functions->fUniform2f = noOpGLUniform2f;
451 functions->fUniform2i = noOpGLUniform2i;
452 functions->fUniform2fv = noOpGLUniform2fv;
453 functions->fUniform2iv = noOpGLUniform2iv;
454 functions->fUniform3f = noOpGLUniform3f;
455 functions->fUniform3i = noOpGLUniform3i;
456 functions->fUniform3fv = noOpGLUniform3fv;
457 functions->fUniform3iv = noOpGLUniform3iv;
458 functions->fUniform4f = noOpGLUniform4f;
459 functions->fUniform4i = noOpGLUniform4i;
460 functions->fUniform4fv = noOpGLUniform4fv;
461 functions->fUniform4iv = noOpGLUniform4iv;
462 functions->fUniformMatrix2fv = noOpGLUniformMatrix2fv;
463 functions->fUniformMatrix3fv = noOpGLUniformMatrix3fv;
464 functions->fUniformMatrix4fv = noOpGLUniformMatrix4fv;
465 functions->fUnmapBuffer = nullGLUnmapBuffer;
466 functions->fUseProgram = nullGLUseProgram;
467 functions->fVertexAttrib1f = noOpGLVertexAttrib1f;
468 functions->fVertexAttrib2fv = noOpGLVertexAttrib2fv;
469 functions->fVertexAttrib3fv = noOpGLVertexAttrib3fv;
470 functions->fVertexAttrib4fv = noOpGLVertexAttrib4fv;
471 functions->fVertexAttribPointer = noOpGLVertexAttribPointer;
cdalton626e1ff2015-06-12 13:56:46 -0700472 functions->fVertexAttribDivisor = noOpGLVertexAttribDivisor;
bsalomonbb0502e2015-02-11 11:11:11 -0800473 functions->fViewport = nullGLViewport;
474 functions->fBindFramebuffer = nullGLBindFramebuffer;
475 functions->fBindRenderbuffer = nullGLBindRenderbuffer;
476 functions->fCheckFramebufferStatus = noOpGLCheckFramebufferStatus;
477 functions->fDeleteFramebuffers = nullGLDeleteFramebuffers;
478 functions->fDeleteRenderbuffers = nullGLDeleteRenderbuffers;
479 functions->fFramebufferRenderbuffer = nullGLFramebufferRenderbuffer;
480 functions->fFramebufferTexture2D = nullGLFramebufferTexture2D;
481 functions->fGenFramebuffers = noOpGLGenIds;
482 functions->fGenRenderbuffers = noOpGLGenIds;
483 functions->fGetFramebufferAttachmentParameteriv = noOpGLGetFramebufferAttachmentParameteriv;
484 functions->fGetRenderbufferParameteriv = noOpGLGetRenderbufferParameteriv;
485 functions->fRenderbufferStorage = noOpGLRenderbufferStorage;
486 functions->fRenderbufferStorageMultisample = noOpGLRenderbufferStorageMultisample;
487 functions->fBlitFramebuffer = noOpGLBlitFramebuffer;
488 functions->fResolveMultisampleFramebuffer = noOpGLResolveMultisampleFramebuffer;
489 functions->fMatrixLoadf = noOpGLMatrixLoadf;
490 functions->fMatrixLoadIdentity = noOpGLMatrixLoadIdentity;
491 functions->fBindFragDataLocationIndexed = noOpGLBindFragDataLocationIndexed;
492
493 interface->fExtensions.init(kGL_GrGLStandard, functions->fGetString, functions->fGetStringi,
494 functions->fGetIntegerv);
495 return interface;
496}
497
498//////////////////////////////////////////////////////////////////////////////
499
500static void* create_tls() {
bsalomon37409722015-02-11 14:19:18 -0800501 State** current = SkNEW(State*);
bsalomonbb0502e2015-02-11 11:11:11 -0800502 *current = NULL;
503 return current;
504}
505
506static void delete_tls(void* ctx) {
bsalomon37409722015-02-11 14:19:18 -0800507 State** current = static_cast<State**>(ctx);
bsalomonbb0502e2015-02-11 11:11:11 -0800508 if (*current) {
509 (*current)->unref();
510 }
511 SkDELETE(current);
512}
513
bsalomon37409722015-02-11 14:19:18 -0800514static State* current_context() {
515 return *static_cast<State**>(SkTLS::Get(create_tls, delete_tls));
bsalomonbb0502e2015-02-11 11:11:11 -0800516}
517
bsalomon37409722015-02-11 14:19:18 -0800518static void set_current_context(State* state) {
519 State** current = static_cast<State**>(SkTLS::Get(create_tls, delete_tls));
bsalomonbb0502e2015-02-11 11:11:11 -0800520 if (*current) {
521 (*current)->unref();
522 }
bsalomon37409722015-02-11 14:19:18 -0800523 *current = state;
524 if (state) {
525 state->ref();
bsalomonbb0502e2015-02-11 11:11:11 -0800526 }
527}
528
bsalomon37409722015-02-11 14:19:18 -0800529#if GR_GL_PER_GL_FUNC_CALLBACK
530static void set_current_context_from_interface(const GrGLInterface* interface) {
531 set_current_context(reinterpret_cast<State*>(interface->fCallbackData));
532}
533#endif
534
bsalomonbb0502e2015-02-11 11:11:11 -0800535SkNullGLContext* SkNullGLContext::Create(GrGLStandard forcedGpuAPI) {
536 if (kGLES_GrGLStandard == forcedGpuAPI) {
537 return NULL;
538 }
539 SkNullGLContext* ctx = SkNEW(SkNullGLContext);
540 if (!ctx->isValid()) {
541 SkDELETE(ctx);
542 return NULL;
543 }
544 return ctx;
545}
bsalomon@google.com74913722011-10-27 20:44:19 +0000546
kkinnunen30bc88c2014-10-15 23:03:54 -0700547SkNullGLContext::SkNullGLContext() {
bsalomonbb0502e2015-02-11 11:11:11 -0800548 fState = SkNEW(ContextState);
bsalomon37409722015-02-11 14:19:18 -0800549 GrGLInterface* interface = create_null_interface(fState);
550 fGL.reset(interface);
551#if GR_GL_PER_GL_FUNC_CALLBACK
552 interface->fCallback = set_current_context_from_interface;
553 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(fState);
554#endif
kkinnunen30bc88c2014-10-15 23:03:54 -0700555}
556
557SkNullGLContext::~SkNullGLContext() {
558 fGL.reset(NULL);
bsalomon37409722015-02-11 14:19:18 -0800559 fState->unref();
kkinnunen30bc88c2014-10-15 23:03:54 -0700560}
561
bsalomon37409722015-02-11 14:19:18 -0800562void SkNullGLContext::makeCurrent() const { set_current_context(fState); }