blob: e7270c32c47c6283f5d45e4f2be40bb520a0abda [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:
mtklein2766c002015-06-26 11:45:03 -070022
bsalomonbb0502e2015-02-11 11:11:11 -080023
halcanary96fcdcc2015-08-27 07:41:13 -070024 BufferObj(GrGLuint id) : fID(id), fDataPtr(nullptr), fSize(0), fMapped(false) {}
halcanary385fe4d2015-08-26 13:07:48 -070025 ~BufferObj() { delete[] fDataPtr; }
bsalomonbb0502e2015-02-11 11:11:11 -080026
27 void allocate(GrGLsizeiptr size, const GrGLchar* dataPtr) {
28 if (fDataPtr) {
29 SkASSERT(0 != fSize);
halcanary385fe4d2015-08-26 13:07:48 -070030 delete[] fDataPtr;
bsalomonbb0502e2015-02-11 11:11:11 -080031 }
32
33 fSize = size;
halcanary385fe4d2015-08-26 13:07:48 -070034 fDataPtr = new char[size];
bsalomonbb0502e2015-02-11 11:11:11 -080035 }
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:
mtklein2766c002015-06-26 11:45:03 -070054
bsalomonbb0502e2015-02-11 11:11:11 -080055
56 BufferManager() : fFreeListHead(kFreeListEnd) {}
57
58 ~BufferManager() {
halcanary96fcdcc2015-08-27 07:41:13 -070059 // nullptr out the entries that are really free list links rather than ptrs before deleting.
bsalomonbb0502e2015-02-11 11:11:11 -080060 intptr_t curr = fFreeListHead;
61 while (kFreeListEnd != curr) {
62 intptr_t next = reinterpret_cast<intptr_t>(fBuffers[SkToS32(curr)]);
halcanary96fcdcc2015-08-27 07:41:13 -070063 fBuffers[SkToS32(curr)] = nullptr;
bsalomonbb0502e2015-02-11 11:11:11 -080064 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();
halcanary385fe4d2015-08-26 13:07:48 -070083 buffer = new BufferObj(id);
bsalomonbb0502e2015-02-11 11:11:11 -080084 *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
halcanary385fe4d2015-08-26 13:07:48 -070090 buffer = new BufferObj(id);
bsalomonbb0502e2015-02-11 11:11:11 -080091 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();
halcanary385fe4d2015-08-26 13:07:48 -0700101 delete buffer;
bsalomonbb0502e2015-02-11 11:11:11 -0800102
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:
mtklein2766c002015-06-26 11:45:03 -0700120
bsalomonbb0502e2015-02-11 11:11:11 -0800121
122 BufferManager fBufferManager;
123 GrGLuint fCurrArrayBuffer;
124 GrGLuint fCurrElementArrayBuffer;
jvanverth73063dc2015-12-03 09:15:47 -0800125 GrGLuint fCurrPixelPackBuffer;
126 GrGLuint fCurrPixelUnpackBuffer;
bsalomonbb0502e2015-02-11 11:11:11 -0800127 GrGLuint fCurrProgramID;
128 GrGLuint fCurrShaderID;
129
130
131 ContextState()
132 : fCurrArrayBuffer(0)
133 , fCurrElementArrayBuffer(0)
jvanverth73063dc2015-12-03 09:15:47 -0800134 , fCurrPixelPackBuffer(0)
135 , fCurrPixelUnpackBuffer(0)
bsalomonbb0502e2015-02-11 11:11:11 -0800136 , fCurrProgramID(0)
137 , fCurrShaderID(0) {}
138
bsalomon37409722015-02-11 14:19:18 -0800139 static ContextState* Get() { return current_context(); }
bsalomonbb0502e2015-02-11 11:11:11 -0800140};
141
142typedef SkNullGLContext::ContextState State;
143
144// Functions not declared in GrGLBogusInterface.h (not common with the Debug GL interface).
145
146namespace { // added to suppress 'no previous prototype' warning
147
148GrGLvoid GR_GL_FUNCTION_TYPE nullGLActiveTexture(GrGLenum texture) {}
149GrGLvoid GR_GL_FUNCTION_TYPE nullGLAttachShader(GrGLuint program, GrGLuint shader) {}
150GrGLvoid GR_GL_FUNCTION_TYPE nullGLBeginQuery(GrGLenum target, GrGLuint id) {}
151GrGLvoid GR_GL_FUNCTION_TYPE nullGLBindAttribLocation(GrGLuint program, GrGLuint index, const char* name) {}
152GrGLvoid GR_GL_FUNCTION_TYPE nullGLBindTexture(GrGLenum target, GrGLuint texture) {}
153GrGLvoid GR_GL_FUNCTION_TYPE nullGLBindVertexArray(GrGLuint id) {}
154
155GrGLvoid 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
163GrGLvoid GR_GL_FUNCTION_TYPE nullGLGenerateMipmap(GrGLenum target) {}
164
165GrGLvoid 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;
jvanverth73063dc2015-12-03 09:15:47 -0800179 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;
bsalomonbb0502e2015-02-11 11:11:11 -0800185 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
196GrGLvoid GR_GL_FUNCTION_TYPE nullGLPixelStorei(GrGLenum pname, GrGLint param) {}
197GrGLvoid GR_GL_FUNCTION_TYPE nullGLReadPixels(GrGLint x, GrGLint y, GrGLsizei width, GrGLsizei height, GrGLenum format, GrGLenum type, GrGLvoid* pixels) {}
198GrGLvoid GR_GL_FUNCTION_TYPE nullGLUseProgram(GrGLuint program) {}
199GrGLvoid GR_GL_FUNCTION_TYPE nullGLViewport(GrGLint x, GrGLint y, GrGLsizei width, GrGLsizei height) {}
200GrGLvoid GR_GL_FUNCTION_TYPE nullGLBindFramebuffer(GrGLenum target, GrGLuint framebuffer) {}
201GrGLvoid GR_GL_FUNCTION_TYPE nullGLBindRenderbuffer(GrGLenum target, GrGLuint renderbuffer) {}
202GrGLvoid GR_GL_FUNCTION_TYPE nullGLDeleteFramebuffers(GrGLsizei n, const GrGLuint *framebuffers) {}
203GrGLvoid GR_GL_FUNCTION_TYPE nullGLDeleteRenderbuffers(GrGLsizei n, const GrGLuint *renderbuffers) {}
204GrGLvoid GR_GL_FUNCTION_TYPE nullGLFramebufferRenderbuffer(GrGLenum target, GrGLenum attachment, GrGLenum renderbuffertarget, GrGLuint renderbuffer) {}
205GrGLvoid GR_GL_FUNCTION_TYPE nullGLFramebufferTexture2D(GrGLenum target, GrGLenum attachment, GrGLenum textarget, GrGLuint texture, GrGLint level) {}
206
207GrGLuint GR_GL_FUNCTION_TYPE nullGLCreateProgram() {
208 return ++State::Get()->fCurrProgramID;
209}
210
211GrGLuint GR_GL_FUNCTION_TYPE nullGLCreateShader(GrGLenum type) {
212 return ++State::Get()->fCurrShaderID;
213}
214
215// same delete used for shaders and programs
216GrGLvoid GR_GL_FUNCTION_TYPE nullGLDelete(GrGLuint program) {
217}
218
219GrGLvoid 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;
jvanverth73063dc2015-12-03 09:15:47 -0800228 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;
bsalomonbb0502e2015-02-11 11:11:11 -0800234 }
235}
236
237// deleting a bound buffer has the side effect of binding 0
238GrGLvoid 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 }
jvanverth73063dc2015-12-03 09:15:47 -0800247 if (ids[i] == state->fCurrPixelPackBuffer) {
248 state->fCurrPixelPackBuffer = 0;
249 }
250 if (ids[i] == state->fCurrPixelUnpackBuffer) {
251 state->fCurrPixelUnpackBuffer = 0;
252 }
bsalomonbb0502e2015-02-11 11:11:11 -0800253
254 BufferObj* buffer = state->fBufferManager.lookUp(ids[i]);
255 state->fBufferManager.free(buffer);
256 }
257}
258
259GrGLvoid* 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;
jvanverth73063dc2015-12-03 09:15:47 -0800270 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;
bsalomonbb0502e2015-02-11 11:11:11 -0800276 }
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 }
halcanary96fcdcc2015-08-27 07:41:13 -0700285 return nullptr;
bsalomonbb0502e2015-02-11 11:11:11 -0800286}
287
288GrGLvoid* 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;
jvanverth73063dc2015-12-03 09:15:47 -0800298 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;
bsalomonbb0502e2015-02-11 11:11:11 -0800304 }
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);
halcanary96fcdcc2015-08-27 07:41:13 -0700314 return nullptr; // no buffer bound to target
bsalomonbb0502e2015-02-11 11:11:11 -0800315}
316
317GrGLvoid GR_GL_FUNCTION_TYPE nullGLFlushMappedBufferRange(GrGLenum target,
318 GrGLintptr offset,
319 GrGLsizeiptr length) {}
320
321
322GrGLboolean 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;
jvanverth73063dc2015-12-03 09:15:47 -0800332 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;
bsalomonbb0502e2015-02-11 11:11:11 -0800338 }
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
350GrGLvoid 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;
jvanverth73063dc2015-12-03 09:15:47 -0800363 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;
bsalomonbb0502e2015-02-11 11:11:11 -0800369 }
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
bsalomon37409722015-02-11 14:19:18 -0800383class NullInterface : public GrGLInterface {
384public:
385 NullInterface(State* state) : fState(SkRef(state)) {}
mtklein36352bf2015-03-25 18:17:31 -0700386 ~NullInterface() override {
bsalomon37409722015-02-11 14:19:18 -0800387 fState->unref();
388 }
389 State* fState;
390};
391
bsalomonbb0502e2015-02-11 11:11:11 -0800392} // end anonymous namespace
393
bsalomon37409722015-02-11 14:19:18 -0800394static GrGLInterface* create_null_interface(State* state) {
halcanary385fe4d2015-08-26 13:07:48 -0700395 GrGLInterface* interface = new NullInterface(state);
bsalomonbb0502e2015-02-11 11:11:11 -0800396
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;
cdaltonbae6f6c2015-04-22 10:39:03 -0700409 functions->fBlendEquation = noOpGLBlendEquation;
bsalomonbb0502e2015-02-11 11:11:11 -0800410 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;
cdalton626e1ff2015-06-12 13:56:46 -0700434 functions->fDrawArraysInstanced = noOpGLDrawArraysInstanced;
bsalomonbb0502e2015-02-11 11:11:11 -0800435 functions->fDrawBuffer = noOpGLDrawBuffer;
436 functions->fDrawBuffers = noOpGLDrawBuffers;
437 functions->fDrawElements = noOpGLDrawElements;
cdalton626e1ff2015-06-12 13:56:46 -0700438 functions->fDrawElementsInstanced = noOpGLDrawElementsInstanced;
bsalomonbb0502e2015-02-11 11:11:11 -0800439 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;
cdalton626e1ff2015-06-12 13:56:46 -0700518 functions->fVertexAttribDivisor = noOpGLVertexAttribDivisor;
bsalomonbb0502e2015-02-11 11:11:11 -0800519 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,
bsalomonb1a32ad2015-11-16 06:48:44 -0800540 functions->fGetIntegerv, nullptr, GR_EGL_NO_DISPLAY);
bsalomonbb0502e2015-02-11 11:11:11 -0800541 return interface;
542}
543
544//////////////////////////////////////////////////////////////////////////////
545
546static void* create_tls() {
halcanary385fe4d2015-08-26 13:07:48 -0700547 State** current = new State*;
halcanary96fcdcc2015-08-27 07:41:13 -0700548 *current = nullptr;
bsalomonbb0502e2015-02-11 11:11:11 -0800549 return current;
550}
551
552static void delete_tls(void* ctx) {
bsalomon37409722015-02-11 14:19:18 -0800553 State** current = static_cast<State**>(ctx);
bsalomonbb0502e2015-02-11 11:11:11 -0800554 if (*current) {
555 (*current)->unref();
556 }
halcanary385fe4d2015-08-26 13:07:48 -0700557 delete current;
bsalomonbb0502e2015-02-11 11:11:11 -0800558}
559
bsalomon37409722015-02-11 14:19:18 -0800560static State* current_context() {
561 return *static_cast<State**>(SkTLS::Get(create_tls, delete_tls));
bsalomonbb0502e2015-02-11 11:11:11 -0800562}
563
bsalomon37409722015-02-11 14:19:18 -0800564static void set_current_context(State* state) {
565 State** current = static_cast<State**>(SkTLS::Get(create_tls, delete_tls));
bsalomonbb0502e2015-02-11 11:11:11 -0800566 if (*current) {
567 (*current)->unref();
568 }
bsalomon37409722015-02-11 14:19:18 -0800569 *current = state;
570 if (state) {
571 state->ref();
bsalomonbb0502e2015-02-11 11:11:11 -0800572 }
573}
574
bsalomon37409722015-02-11 14:19:18 -0800575#if GR_GL_PER_GL_FUNC_CALLBACK
576static void set_current_context_from_interface(const GrGLInterface* interface) {
577 set_current_context(reinterpret_cast<State*>(interface->fCallbackData));
578}
579#endif
580
rmistry0f515bd2015-12-22 10:22:26 -0800581SkNullGLContext* SkNullGLContext::Create(GrGLStandard forcedGpuAPI) {
582 if (kGLES_GrGLStandard == forcedGpuAPI) {
583 return nullptr;
584 }
halcanary385fe4d2015-08-26 13:07:48 -0700585 SkNullGLContext* ctx = new SkNullGLContext;
bsalomonbb0502e2015-02-11 11:11:11 -0800586 if (!ctx->isValid()) {
halcanary385fe4d2015-08-26 13:07:48 -0700587 delete ctx;
halcanary96fcdcc2015-08-27 07:41:13 -0700588 return nullptr;
bsalomonbb0502e2015-02-11 11:11:11 -0800589 }
590 return ctx;
591}
bsalomon@google.com74913722011-10-27 20:44:19 +0000592
kkinnunen30bc88c2014-10-15 23:03:54 -0700593SkNullGLContext::SkNullGLContext() {
halcanary385fe4d2015-08-26 13:07:48 -0700594 fState = new ContextState;
bsalomon37409722015-02-11 14:19:18 -0800595 GrGLInterface* interface = create_null_interface(fState);
cdaltond416a5b2015-06-23 13:23:44 -0700596 this->init(interface);
bsalomon37409722015-02-11 14:19:18 -0800597#if GR_GL_PER_GL_FUNC_CALLBACK
598 interface->fCallback = set_current_context_from_interface;
599 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(fState);
600#endif
kkinnunen30bc88c2014-10-15 23:03:54 -0700601}
602
603SkNullGLContext::~SkNullGLContext() {
cdaltond416a5b2015-06-23 13:23:44 -0700604 this->teardown();
bsalomon37409722015-02-11 14:19:18 -0800605 fState->unref();
kkinnunen30bc88c2014-10-15 23:03:54 -0700606}
607
cdaltond416a5b2015-06-23 13:23:44 -0700608void SkNullGLContext::onPlatformMakeCurrent() const { set_current_context(fState); }