Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / gpu / command_buffer / client / gles2_implementation.h
blobbceb91d49726b893a8a8b2c5ff97b478bc293640
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef GPU_COMMAND_BUFFER_CLIENT_GLES2_IMPLEMENTATION_H_
6 #define GPU_COMMAND_BUFFER_CLIENT_GLES2_IMPLEMENTATION_H_
8 #include <list>
9 #include <map>
10 #include <queue>
11 #include <set>
12 #include <string>
13 #include <utility>
14 #include <vector>
16 #include "base/basictypes.h"
17 #include "base/compiler_specific.h"
18 #include "base/macros.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "base/memory/weak_ptr.h"
21 #include "gpu/command_buffer/client/buffer_tracker.h"
22 #include "gpu/command_buffer/client/client_context_state.h"
23 #include "gpu/command_buffer/client/context_support.h"
24 #include "gpu/command_buffer/client/gles2_impl_export.h"
25 #include "gpu/command_buffer/client/gles2_interface.h"
26 #include "gpu/command_buffer/client/mapped_memory.h"
27 #include "gpu/command_buffer/client/ref_counted.h"
28 #include "gpu/command_buffer/client/share_group.h"
29 #include "gpu/command_buffer/common/capabilities.h"
30 #include "gpu/command_buffer/common/debug_marker_manager.h"
31 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
33 #if !defined(NDEBUG) && !defined(__native_client__) && !defined(GLES2_CONFORMANCE_TESTS) // NOLINT
34 #if defined(GLES2_INLINE_OPTIMIZATION)
35 // TODO(gman): Replace with macros that work with inline optmization.
36 #define GPU_CLIENT_SINGLE_THREAD_CHECK()
37 #define GPU_CLIENT_LOG(args)
38 #define GPU_CLIENT_LOG_CODE_BLOCK(code)
39 #define GPU_CLIENT_DCHECK_CODE_BLOCK(code)
40 #else
41 #include "base/logging.h"
42 #define GPU_CLIENT_SINGLE_THREAD_CHECK() SingleThreadChecker checker(this);
43 #define GPU_CLIENT_LOG(args) DLOG_IF(INFO, debug_) << args;
44 #define GPU_CLIENT_LOG_CODE_BLOCK(code) code
45 #define GPU_CLIENT_DCHECK_CODE_BLOCK(code) code
46 #define GPU_CLIENT_DEBUG
47 #endif
48 #else
49 #define GPU_CLIENT_SINGLE_THREAD_CHECK()
50 #define GPU_CLIENT_LOG(args)
51 #define GPU_CLIENT_LOG_CODE_BLOCK(code)
52 #define GPU_CLIENT_DCHECK_CODE_BLOCK(code)
53 #endif
55 #if defined(GPU_CLIENT_DEBUG)
56 // Set to 1 to have the client fail when a GL error is generated.
57 // This helps find bugs in the renderer since the debugger stops on the error.
58 # if 0
59 # define GL_CLIENT_FAIL_GL_ERRORS
60 # endif
61 #endif
63 // Check that destination pointers point to initialized memory.
64 // When the context is lost, calling GL function has no effect so if destination
65 // pointers point to initialized memory it can often lead to crash bugs. eg.
67 // GLsizei len;
68 // glGetShaderSource(shader, max_size, &len, buffer);
69 // std::string src(buffer, buffer + len); // len can be uninitialized here!!!
71 // Because this check is not official GL this check happens only on Chrome code,
72 // not Pepper.
74 // If it was up to us we'd just always write to the destination but the OpenGL
75 // spec defines the behavior of OpenGL functions, not us. :-(
76 #if defined(__native_client__) || defined(GLES2_CONFORMANCE_TESTS)
77 #define GPU_CLIENT_VALIDATE_DESTINATION_INITALIZATION_ASSERT(v)
78 #define GPU_CLIENT_DCHECK(v)
79 #elif defined(GPU_DCHECK)
80 #define GPU_CLIENT_VALIDATE_DESTINATION_INITALIZATION_ASSERT(v) GPU_DCHECK(v)
81 #define GPU_CLIENT_DCHECK(v) GPU_DCHECK(v)
82 #elif defined(DCHECK)
83 #define GPU_CLIENT_VALIDATE_DESTINATION_INITALIZATION_ASSERT(v) DCHECK(v)
84 #define GPU_CLIENT_DCHECK(v) DCHECK(v)
85 #else
86 #define GPU_CLIENT_VALIDATE_DESTINATION_INITALIZATION_ASSERT(v) ASSERT(v)
87 #define GPU_CLIENT_DCHECK(v) ASSERT(v)
88 #endif
90 #define GPU_CLIENT_VALIDATE_DESTINATION_INITALIZATION(type, ptr) \
91 GPU_CLIENT_VALIDATE_DESTINATION_INITALIZATION_ASSERT(ptr && \
92 (ptr[0] == static_cast<type>(0) || ptr[0] == static_cast<type>(-1)));
94 #define GPU_CLIENT_VALIDATE_DESTINATION_OPTIONAL_INITALIZATION(type, ptr) \
95 GPU_CLIENT_VALIDATE_DESTINATION_INITALIZATION_ASSERT(!ptr || \
96 (ptr[0] == static_cast<type>(0) || ptr[0] == static_cast<type>(-1)));
98 namespace gpu {
100 class GpuControl;
101 class IdAllocator;
102 class ScopedTransferBufferPtr;
103 class TransferBufferInterface;
105 namespace gles2 {
107 class GLES2CmdHelper;
108 class VertexArrayObjectManager;
109 class QueryTracker;
111 class GLES2ImplementationErrorMessageCallback {
112 public:
113 virtual ~GLES2ImplementationErrorMessageCallback() { }
114 virtual void OnErrorMessage(const char* msg, int id) = 0;
117 // This class emulates GLES2 over command buffers. It can be used by a client
118 // program so that the program does not need deal with shared memory and command
119 // buffer management. See gl2_lib.h. Note that there is a performance gain to
120 // be had by changing your code to use command buffers directly by using the
121 // GLES2CmdHelper but that entails changing your code to use and deal with
122 // shared memory and synchronization issues.
123 class GLES2_IMPL_EXPORT GLES2Implementation
124 : NON_EXPORTED_BASE(public GLES2Interface),
125 NON_EXPORTED_BASE(public ContextSupport) {
126 public:
127 enum MappedMemoryLimit {
128 kNoLimit = MappedMemoryManager::kNoLimit,
131 // Stores GL state that never changes.
132 struct GLES2_IMPL_EXPORT GLStaticState {
133 GLStaticState();
134 ~GLStaticState();
136 typedef std::pair<GLenum, GLenum> ShaderPrecisionKey;
137 typedef std::map<ShaderPrecisionKey,
138 cmds::GetShaderPrecisionFormat::Result>
139 ShaderPrecisionMap;
140 ShaderPrecisionMap shader_precisions;
143 // The maxiumum result size from simple GL get commands.
144 static const size_t kMaxSizeOfSimpleResult = 16 * sizeof(uint32); // NOLINT.
146 // used for testing only. If more things are reseved add them here.
147 static const unsigned int kStartingOffset = kMaxSizeOfSimpleResult;
149 // Size in bytes to issue async flush for transfer buffer.
150 static const unsigned int kSizeToFlush = 256 * 1024;
152 // The bucket used for results. Public for testing only.
153 static const uint32 kResultBucketId = 1;
155 // Alignment of allocations.
156 static const unsigned int kAlignment = 4;
158 // GL names for the buffers used to emulate client side buffers.
159 static const GLuint kClientSideArrayId = 0xFEDCBA98u;
160 static const GLuint kClientSideElementArrayId = 0xFEDCBA99u;
162 // Number of swap buffers allowed before waiting.
163 static const size_t kMaxSwapBuffers = 2;
165 GLES2Implementation(GLES2CmdHelper* helper,
166 ShareGroup* share_group,
167 TransferBufferInterface* transfer_buffer,
168 bool bind_generates_resource,
169 bool lose_context_when_out_of_memory,
170 bool support_client_side_arrays,
171 GpuControl* gpu_control);
173 ~GLES2Implementation() override;
175 bool Initialize(
176 unsigned int starting_transfer_buffer_size,
177 unsigned int min_transfer_buffer_size,
178 unsigned int max_transfer_buffer_size,
179 unsigned int mapped_memory_limit);
181 // The GLES2CmdHelper being used by this GLES2Implementation. You can use
182 // this to issue cmds at a lower level for certain kinds of optimization.
183 GLES2CmdHelper* helper() const;
185 // Gets client side generated errors.
186 GLenum GetClientSideGLError();
188 // Include the auto-generated part of this class. We split this because
189 // it means we can easily edit the non-auto generated parts right here in
190 // this file instead of having to edit some template or the code generator.
191 #include "gpu/command_buffer/client/gles2_implementation_autogen.h"
193 void DisableVertexAttribArray(GLuint index) override;
194 void EnableVertexAttribArray(GLuint index) override;
195 void GetVertexAttribfv(GLuint index, GLenum pname, GLfloat* params) override;
196 void GetVertexAttribiv(GLuint index, GLenum pname, GLint* params) override;
197 void GetVertexAttribIiv(GLuint index, GLenum pname, GLint* params) override;
198 void GetVertexAttribIuiv(GLuint index, GLenum pname, GLuint* params) override;
200 // ContextSupport implementation.
201 void Swap() override;
202 void PartialSwapBuffers(const gfx::Rect& sub_buffer) override;
203 void ScheduleOverlayPlane(int plane_z_order,
204 gfx::OverlayTransform plane_transform,
205 unsigned overlay_texture_id,
206 const gfx::Rect& display_bounds,
207 const gfx::RectF& uv_rect) override;
208 GLuint InsertFutureSyncPointCHROMIUM() override;
209 void RetireSyncPointCHROMIUM(GLuint sync_point) override;
211 void GetProgramInfoCHROMIUMHelper(GLuint program, std::vector<int8>* result);
212 GLint GetAttribLocationHelper(GLuint program, const char* name);
213 GLint GetUniformLocationHelper(GLuint program, const char* name);
214 GLint GetFragDataLocationHelper(GLuint program, const char* name);
215 bool GetActiveAttribHelper(
216 GLuint program, GLuint index, GLsizei bufsize, GLsizei* length,
217 GLint* size, GLenum* type, char* name);
218 bool GetActiveUniformHelper(
219 GLuint program, GLuint index, GLsizei bufsize, GLsizei* length,
220 GLint* size, GLenum* type, char* name);
221 void GetUniformBlocksCHROMIUMHelper(
222 GLuint program, std::vector<int8>* result);
223 void GetUniformsES3CHROMIUMHelper(
224 GLuint program, std::vector<int8>* result);
225 GLuint GetUniformBlockIndexHelper(GLuint program, const char* name);
226 bool GetActiveUniformBlockNameHelper(
227 GLuint program, GLuint index, GLsizei bufsize,
228 GLsizei* length, char* name);
229 bool GetActiveUniformBlockivHelper(
230 GLuint program, GLuint index, GLenum pname, GLint* params);
231 void GetTransformFeedbackVaryingsCHROMIUMHelper(
232 GLuint program, std::vector<int8>* result);
233 bool GetTransformFeedbackVaryingHelper(
234 GLuint program, GLuint index, GLsizei bufsize, GLsizei* length,
235 GLint* size, GLenum* type, char* name);
236 bool GetUniformIndicesHelper(
237 GLuint program, GLsizei count, const char* const* names, GLuint* indices);
238 bool GetActiveUniformsivHelper(
239 GLuint program, GLsizei count, const GLuint* indices,
240 GLenum pname, GLint* params);
241 bool GetSyncivHelper(
242 GLsync sync, GLenum pname, GLsizei bufsize, GLsizei* length,
243 GLint* values);
244 bool GetQueryObjectValueHelper(
245 const char* function_name, GLuint id, GLenum pname, GLuint64* params);
247 void FreeUnusedSharedMemory();
248 void FreeEverything();
250 // ContextSupport implementation.
251 void SignalSyncPoint(uint32 sync_point,
252 const base::Closure& callback) override;
253 void SignalQuery(uint32 query, const base::Closure& callback) override;
254 void SetSurfaceVisible(bool visible) override;
255 void SetAggressivelyFreeResources(bool aggressively_free_resources) override;
257 void SetErrorMessageCallback(
258 GLES2ImplementationErrorMessageCallback* callback) {
259 error_message_callback_ = callback;
262 ShareGroup* share_group() const {
263 return share_group_.get();
266 const Capabilities& capabilities() const {
267 return capabilities_;
270 GpuControl* gpu_control() {
271 return gpu_control_;
274 ShareGroupContextData* share_group_context_data() {
275 return &share_group_context_data_;
278 private:
279 friend class GLES2ImplementationTest;
280 friend class VertexArrayObjectManager;
281 friend class QueryTracker;
283 // Used to track whether an extension is available
284 enum ExtensionStatus {
285 kAvailableExtensionStatus,
286 kUnavailableExtensionStatus,
287 kUnknownExtensionStatus
290 // Base class for mapped resources.
291 struct MappedResource {
292 MappedResource(GLenum _access, int _shm_id, void* mem, unsigned int offset)
293 : access(_access),
294 shm_id(_shm_id),
295 shm_memory(mem),
296 shm_offset(offset) {
299 // access mode. Currently only GL_WRITE_ONLY is valid
300 GLenum access;
302 // Shared memory ID for buffer.
303 int shm_id;
305 // Address of shared memory
306 void* shm_memory;
308 // Offset of shared memory
309 unsigned int shm_offset;
312 // Used to track mapped textures.
313 struct MappedTexture : public MappedResource {
314 MappedTexture(
315 GLenum access,
316 int shm_id,
317 void* shm_mem,
318 unsigned int shm_offset,
319 GLenum _target,
320 GLint _level,
321 GLint _xoffset,
322 GLint _yoffset,
323 GLsizei _width,
324 GLsizei _height,
325 GLenum _format,
326 GLenum _type)
327 : MappedResource(access, shm_id, shm_mem, shm_offset),
328 target(_target),
329 level(_level),
330 xoffset(_xoffset),
331 yoffset(_yoffset),
332 width(_width),
333 height(_height),
334 format(_format),
335 type(_type) {
338 // These match the arguments to TexSubImage2D.
339 GLenum target;
340 GLint level;
341 GLint xoffset;
342 GLint yoffset;
343 GLsizei width;
344 GLsizei height;
345 GLenum format;
346 GLenum type;
349 // Used to track mapped buffers.
350 struct MappedBuffer : public MappedResource {
351 MappedBuffer(
352 GLenum access,
353 int shm_id,
354 void* shm_mem,
355 unsigned int shm_offset,
356 GLenum _target,
357 GLintptr _offset,
358 GLsizeiptr _size)
359 : MappedResource(access, shm_id, shm_mem, shm_offset),
360 target(_target),
361 offset(_offset),
362 size(_size) {
365 // These match the arguments to BufferSubData.
366 GLenum target;
367 GLintptr offset;
368 GLsizeiptr size;
371 struct TextureUnit {
372 TextureUnit()
373 : bound_texture_2d(0),
374 bound_texture_cube_map(0),
375 bound_texture_external_oes(0) {}
377 // texture currently bound to this unit's GL_TEXTURE_2D with glBindTexture
378 GLuint bound_texture_2d;
380 // texture currently bound to this unit's GL_TEXTURE_CUBE_MAP with
381 // glBindTexture
382 GLuint bound_texture_cube_map;
384 // texture currently bound to this unit's GL_TEXTURE_EXTERNAL_OES with
385 // glBindTexture
386 GLuint bound_texture_external_oes;
389 // Checks for single threaded access.
390 class SingleThreadChecker {
391 public:
392 explicit SingleThreadChecker(GLES2Implementation* gles2_implementation);
393 ~SingleThreadChecker();
395 private:
396 GLES2Implementation* gles2_implementation_;
399 // Gets the value of the result.
400 template <typename T>
401 T GetResultAs() {
402 return static_cast<T>(GetResultBuffer());
405 void* GetResultBuffer();
406 int32 GetResultShmId();
407 uint32 GetResultShmOffset();
409 // Lazily determines if GL_ANGLE_pack_reverse_row_order is available
410 bool IsAnglePackReverseRowOrderAvailable();
411 bool IsChromiumFramebufferMultisampleAvailable();
413 bool IsExtensionAvailableHelper(
414 const char* extension, ExtensionStatus* status);
416 // Gets the GLError through our wrapper.
417 GLenum GetGLError();
419 // Sets our wrapper for the GLError.
420 void SetGLError(GLenum error, const char* function_name, const char* msg);
421 void SetGLErrorInvalidEnum(
422 const char* function_name, GLenum value, const char* label);
424 // Returns the last error and clears it. Useful for debugging.
425 const std::string& GetLastError() {
426 return last_error_;
429 // Waits for all commands to execute.
430 void WaitForCmd();
432 // TODO(gman): These bucket functions really seem like they belong in
433 // CommandBufferHelper (or maybe BucketHelper?). Unfortunately they need
434 // a transfer buffer to function which is currently managed by this class.
436 // Gets the contents of a bucket.
437 bool GetBucketContents(uint32 bucket_id, std::vector<int8>* data);
439 // Sets the contents of a bucket.
440 void SetBucketContents(uint32 bucket_id, const void* data, size_t size);
442 // Sets the contents of a bucket as a string.
443 void SetBucketAsCString(uint32 bucket_id, const char* str);
445 // Gets the contents of a bucket as a string. Returns false if there is no
446 // string available which is a separate case from the empty string.
447 bool GetBucketAsString(uint32 bucket_id, std::string* str);
449 // Sets the contents of a bucket as a string.
450 void SetBucketAsString(uint32 bucket_id, const std::string& str);
452 // Returns true if id is reserved.
453 bool IsBufferReservedId(GLuint id);
454 bool IsFramebufferReservedId(GLuint id) { return false; }
455 bool IsRenderbufferReservedId(GLuint id) { return false; }
456 bool IsTextureReservedId(GLuint id) { return false; }
457 bool IsVertexArrayReservedId(GLuint id) { return false; }
458 bool IsProgramReservedId(GLuint id) { return false; }
459 bool IsValuebufferReservedId(GLuint id) { return false; }
460 bool IsSamplerReservedId(GLuint id) { return false; }
461 bool IsTransformFeedbackReservedId(GLuint id) { return false; }
463 void BindBufferHelper(GLenum target, GLuint buffer);
464 void BindBufferBaseHelper(GLenum target, GLuint index, GLuint buffer);
465 void BindBufferRangeHelper(GLenum target, GLuint index, GLuint buffer,
466 GLintptr offset, GLsizeiptr size);
467 void BindFramebufferHelper(GLenum target, GLuint framebuffer);
468 void BindRenderbufferHelper(GLenum target, GLuint renderbuffer);
469 void BindSamplerHelper(GLuint unit, GLuint sampler);
470 void BindTextureHelper(GLenum target, GLuint texture);
471 void BindTransformFeedbackHelper(GLenum target, GLuint transformfeedback);
472 void BindVertexArrayOESHelper(GLuint array);
473 void BindValuebufferCHROMIUMHelper(GLenum target, GLuint valuebuffer);
474 void UseProgramHelper(GLuint program);
476 void BindBufferStub(GLenum target, GLuint buffer);
477 void BindBufferBaseStub(GLenum target, GLuint index, GLuint buffer);
478 void BindBufferRangeStub(GLenum target, GLuint index, GLuint buffer,
479 GLintptr offset, GLsizeiptr size);
480 void BindFramebufferStub(GLenum target, GLuint framebuffer);
481 void BindRenderbufferStub(GLenum target, GLuint renderbuffer);
482 void BindTextureStub(GLenum target, GLuint texture);
483 void BindValuebufferCHROMIUMStub(GLenum target, GLuint valuebuffer);
485 void GenBuffersHelper(GLsizei n, const GLuint* buffers);
486 void GenFramebuffersHelper(GLsizei n, const GLuint* framebuffers);
487 void GenRenderbuffersHelper(GLsizei n, const GLuint* renderbuffers);
488 void GenTexturesHelper(GLsizei n, const GLuint* textures);
489 void GenVertexArraysOESHelper(GLsizei n, const GLuint* arrays);
490 void GenQueriesEXTHelper(GLsizei n, const GLuint* queries);
491 void GenValuebuffersCHROMIUMHelper(GLsizei n, const GLuint* valuebuffers);
492 void GenSamplersHelper(GLsizei n, const GLuint* samplers);
493 void GenTransformFeedbacksHelper(GLsizei n, const GLuint* transformfeedbacks);
495 void DeleteBuffersHelper(GLsizei n, const GLuint* buffers);
496 void DeleteFramebuffersHelper(GLsizei n, const GLuint* framebuffers);
497 void DeleteRenderbuffersHelper(GLsizei n, const GLuint* renderbuffers);
498 void DeleteTexturesHelper(GLsizei n, const GLuint* textures);
499 bool DeleteProgramHelper(GLuint program);
500 bool DeleteShaderHelper(GLuint shader);
501 void DeleteQueriesEXTHelper(GLsizei n, const GLuint* queries);
502 void DeleteVertexArraysOESHelper(GLsizei n, const GLuint* arrays);
503 void DeleteValuebuffersCHROMIUMHelper(GLsizei n, const GLuint* valuebuffers);
504 void DeleteSamplersHelper(GLsizei n, const GLuint* samplers);
505 void DeleteTransformFeedbacksHelper(
506 GLsizei n, const GLuint* transformfeedbacks);
507 void DeleteSyncHelper(GLsync sync);
509 void DeleteBuffersStub(GLsizei n, const GLuint* buffers);
510 void DeleteFramebuffersStub(GLsizei n, const GLuint* framebuffers);
511 void DeleteRenderbuffersStub(GLsizei n, const GLuint* renderbuffers);
512 void DeleteTexturesStub(GLsizei n, const GLuint* textures);
513 void DeletePathsCHROMIUMStub(GLuint first_client_id, GLsizei range);
514 void DeleteProgramStub(GLsizei n, const GLuint* programs);
515 void DeleteShaderStub(GLsizei n, const GLuint* shaders);
516 void DeleteVertexArraysOESStub(GLsizei n, const GLuint* arrays);
517 void DeleteValuebuffersCHROMIUMStub(GLsizei n, const GLuint* valuebuffers);
518 void DeleteSamplersStub(GLsizei n, const GLuint* samplers);
519 void DeleteTransformFeedbacksStub(
520 GLsizei n, const GLuint* transformfeedbacks);
521 void DeleteSyncStub(GLsizei n, const GLuint* syncs);
523 void BufferDataHelper(
524 GLenum target, GLsizeiptr size, const void* data, GLenum usage);
525 void BufferSubDataHelper(
526 GLenum target, GLintptr offset, GLsizeiptr size, const void* data);
527 void BufferSubDataHelperImpl(
528 GLenum target, GLintptr offset, GLsizeiptr size, const void* data,
529 ScopedTransferBufferPtr* buffer);
531 GLuint CreateImageCHROMIUMHelper(ClientBuffer buffer,
532 GLsizei width,
533 GLsizei height,
534 GLenum internalformat);
535 void DestroyImageCHROMIUMHelper(GLuint image_id);
536 GLuint CreateGpuMemoryBufferImageCHROMIUMHelper(GLsizei width,
537 GLsizei height,
538 GLenum internalformat,
539 GLenum usage);
541 // Helper for GetVertexAttrib
542 bool GetVertexAttribHelper(GLuint index, GLenum pname, uint32* param);
544 GLuint GetMaxValueInBufferCHROMIUMHelper(
545 GLuint buffer_id, GLsizei count, GLenum type, GLuint offset);
547 void WaitAllAsyncTexImage2DCHROMIUMHelper();
549 void RestoreElementAndArrayBuffers(bool restore);
550 void RestoreArrayBuffer(bool restrore);
552 // The pixels pointer should already account for unpack skip
553 // images/rows/pixels.
554 void TexSubImage2DImpl(
555 GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width,
556 GLsizei height, GLenum format, GLenum type, uint32 unpadded_row_size,
557 const void* pixels, uint32 pixels_padded_row_size, GLboolean internal,
558 ScopedTransferBufferPtr* buffer, uint32 buffer_padded_row_size);
559 void TexSubImage3DImpl(
560 GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset,
561 GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type,
562 uint32 unpadded_row_size, const void* pixels,
563 uint32 pixels_padded_row_size, GLboolean internal,
564 ScopedTransferBufferPtr* buffer, uint32 buffer_padded_row_size);
566 // Helpers for query functions.
567 bool GetHelper(GLenum pname, GLint* params);
568 GLuint GetBoundBufferHelper(GLenum target);
569 bool GetBooleanvHelper(GLenum pname, GLboolean* params);
570 bool GetBufferParameterivHelper(GLenum target, GLenum pname, GLint* params);
571 bool GetFloatvHelper(GLenum pname, GLfloat* params);
572 bool GetFramebufferAttachmentParameterivHelper(
573 GLenum target, GLenum attachment, GLenum pname, GLint* params);
574 bool GetInteger64vHelper(GLenum pname, GLint64* params);
575 bool GetIntegervHelper(GLenum pname, GLint* params);
576 bool GetIntegeri_vHelper(GLenum pname, GLuint index, GLint* data);
577 bool GetInteger64i_vHelper(GLenum pname, GLuint index, GLint64* data);
578 bool GetInternalformativHelper(
579 GLenum target, GLenum format, GLenum pname, GLsizei bufSize,
580 GLint* params);
581 bool GetProgramivHelper(GLuint program, GLenum pname, GLint* params);
582 bool GetSamplerParameterfvHelper(
583 GLuint sampler, GLenum pname, GLfloat* params);
584 bool GetSamplerParameterivHelper(
585 GLuint sampler, GLenum pname, GLint* params);
586 bool GetRenderbufferParameterivHelper(
587 GLenum target, GLenum pname, GLint* params);
588 bool GetShaderivHelper(GLuint shader, GLenum pname, GLint* params);
589 bool GetTexParameterfvHelper(GLenum target, GLenum pname, GLfloat* params);
590 bool GetTexParameterivHelper(GLenum target, GLenum pname, GLint* params);
591 const GLubyte* GetStringHelper(GLenum name);
593 bool IsExtensionAvailable(const char* ext);
595 // Caches certain capabilties state. Return true if cached.
596 bool SetCapabilityState(GLenum cap, bool enabled);
598 IdHandlerInterface* GetIdHandler(int id_namespace) const;
599 RangeIdHandlerInterface* GetRangeIdHandler(int id_namespace) const;
600 // IdAllocators for objects that can't be shared among contexts.
601 // For now, used only for Queries. TODO(hj.r.chung) Should be added for
602 // Framebuffer and Vertex array objects.
603 IdAllocator* GetIdAllocator(int id_namespace) const;
605 void FinishHelper();
606 void FlushHelper();
608 void RunIfContextNotLost(const base::Closure& callback);
610 // Validate if an offset is valid, i.e., non-negative and fit into 32-bit.
611 // If not, generate an approriate error, and return false.
612 bool ValidateOffset(const char* func, GLintptr offset);
614 // Validate if a size is valid, i.e., non-negative and fit into 32-bit.
615 // If not, generate an approriate error, and return false.
616 bool ValidateSize(const char* func, GLsizeiptr offset);
618 // Remove the transfer buffer from the buffer tracker. For buffers used
619 // asynchronously the memory is free:ed if the upload has completed. For
620 // other buffers, the memory is either free:ed immediately or free:ed pending
621 // a token.
622 void RemoveTransferBuffer(BufferTracker::Buffer* buffer);
624 // Returns true if the async upload token has passed.
626 // NOTE: This will detect wrapped async tokens by checking if the most
627 // significant bit of async token to check is 1 but the last read is 0, i.e.
628 // the uint32 wrapped.
629 bool HasAsyncUploadTokenPassed(uint32 token) const {
630 return async_upload_sync_->HasAsyncUploadTokenPassed(token);
633 // Get the next async upload token.
634 uint32 NextAsyncUploadToken();
636 // Ensure that the shared memory used for synchronizing async upload tokens
637 // has been mapped.
639 // Returns false on error, true on success.
640 bool EnsureAsyncUploadSync();
642 // Checks the last read asynchronously upload token and frees any unmanaged
643 // transfer buffer that has its async token passed.
644 void PollAsyncUploads();
646 // Free every async upload buffer. If some async upload buffer is still in use
647 // wait for them to finish before freeing.
648 void FreeAllAsyncUploadBuffers();
650 bool GetBoundPixelTransferBuffer(
651 GLenum target, const char* function_name, GLuint* buffer_id);
652 BufferTracker::Buffer* GetBoundPixelUnpackTransferBufferIfValid(
653 GLuint buffer_id,
654 const char* function_name, GLuint offset, GLsizei size);
656 // Pack 2D arrays of char into a bucket.
657 // Helper function for ShaderSource(), TransformFeedbackVaryings(), etc.
658 bool PackStringsToBucket(GLsizei count,
659 const char* const* str,
660 const GLint* length,
661 const char* func_name);
663 const std::string& GetLogPrefix() const;
665 #if defined(GL_CLIENT_FAIL_GL_ERRORS)
666 void CheckGLError();
667 void FailGLError(GLenum error);
668 #else
669 void CheckGLError() { }
670 void FailGLError(GLenum /* error */) { }
671 #endif
673 void RemoveMappedBufferRangeByTarget(GLenum target);
674 void RemoveMappedBufferRangeById(GLuint buffer);
675 void ClearMappedBufferRangeMap();
677 void DrawElementsImpl(GLenum mode, GLsizei count, GLenum type,
678 const void* indices, const char* func_name);
680 GLES2Util util_;
681 GLES2CmdHelper* helper_;
682 TransferBufferInterface* transfer_buffer_;
683 std::string last_error_;
684 DebugMarkerManager debug_marker_manager_;
685 std::string this_in_hex_;
687 std::queue<int32> swap_buffers_tokens_;
688 std::queue<int32> rate_limit_tokens_;
690 ExtensionStatus angle_pack_reverse_row_order_status_;
691 ExtensionStatus chromium_framebuffer_multisample_;
693 GLStaticState static_state_;
694 ClientContextState state_;
696 // pack alignment as last set by glPixelStorei
697 GLint pack_alignment_;
699 // unpack alignment as last set by glPixelStorei
700 GLint unpack_alignment_;
702 // unpack row length as last set by glPixelStorei
703 GLint unpack_row_length_;
705 // unpack image height as last set by glPixelStorei
706 GLint unpack_image_height_;
708 // unpack skip rows as last set by glPixelStorei
709 GLint unpack_skip_rows_;
711 // unpack skip pixels as last set by glPixelStorei
712 GLint unpack_skip_pixels_;
714 // unpack skip images as last set by glPixelStorei
715 GLint unpack_skip_images_;
717 // pack reverse row order as last set by glPixelstorei
718 bool pack_reverse_row_order_;
720 scoped_ptr<TextureUnit[]> texture_units_;
722 // 0 to gl_state_.max_combined_texture_image_units.
723 GLuint active_texture_unit_;
725 GLuint bound_framebuffer_;
726 GLuint bound_read_framebuffer_;
727 GLuint bound_renderbuffer_;
728 GLuint bound_valuebuffer_;
730 // The program in use by glUseProgram
731 GLuint current_program_;
733 GLuint bound_array_buffer_;
734 GLuint bound_copy_read_buffer_;
735 GLuint bound_copy_write_buffer_;
736 GLuint bound_pixel_pack_buffer_;
737 GLuint bound_pixel_unpack_buffer_;
738 GLuint bound_transform_feedback_buffer_;
739 GLuint bound_uniform_buffer_;
741 // The currently bound pixel transfer buffers.
742 GLuint bound_pixel_pack_transfer_buffer_id_;
743 GLuint bound_pixel_unpack_transfer_buffer_id_;
745 // The current asynchronous pixel buffer upload token.
746 uint32 async_upload_token_;
748 // The shared memory used for synchronizing asynchronous upload tokens.
749 AsyncUploadSync* async_upload_sync_;
750 int32 async_upload_sync_shm_id_;
751 unsigned int async_upload_sync_shm_offset_;
753 // Unmanaged pixel transfer buffer memory pending asynchronous upload token.
754 typedef std::list<std::pair<void*, uint32> > DetachedAsyncUploadMemoryList;
755 DetachedAsyncUploadMemoryList detached_async_upload_memory_;
757 // Client side management for vertex array objects. Needed to correctly
758 // track client side arrays.
759 scoped_ptr<VertexArrayObjectManager> vertex_array_object_manager_;
761 GLuint reserved_ids_[2];
763 // Current GL error bits.
764 uint32 error_bits_;
766 // Whether or not to print debugging info.
767 bool debug_;
769 // When true, the context is lost when a GL_OUT_OF_MEMORY error occurs.
770 const bool lose_context_when_out_of_memory_;
772 // Whether or not to support client side arrays.
773 const bool support_client_side_arrays_;
775 // Used to check for single threaded access.
776 int use_count_;
778 // Map of GLenum to Strings for glGetString. We need to cache these because
779 // the pointer passed back to the client has to remain valid for eternity.
780 typedef std::map<uint32, std::set<std::string> > GLStringMap;
781 GLStringMap gl_strings_;
783 // Similar cache for glGetRequestableExtensionsCHROMIUM. We don't
784 // have an enum for this so handle it separately.
785 std::set<std::string> requestable_extensions_set_;
787 typedef std::map<const void*, MappedBuffer> MappedBufferMap;
788 MappedBufferMap mapped_buffers_;
790 // TODO(zmo): Consolidate |mapped_buffers_| and |mapped_buffer_range_map_|.
791 typedef base::hash_map<GLuint, MappedBuffer> MappedBufferRangeMap;
792 MappedBufferRangeMap mapped_buffer_range_map_;
794 typedef std::map<const void*, MappedTexture> MappedTextureMap;
795 MappedTextureMap mapped_textures_;
797 scoped_ptr<MappedMemoryManager> mapped_memory_;
799 scoped_refptr<ShareGroup> share_group_;
800 ShareGroupContextData share_group_context_data_;
802 scoped_ptr<QueryTracker> query_tracker_;
803 scoped_ptr<IdAllocator> query_id_allocator_;
805 scoped_ptr<BufferTracker> buffer_tracker_;
807 GLES2ImplementationErrorMessageCallback* error_message_callback_;
809 int current_trace_stack_;
811 GpuControl* gpu_control_;
813 Capabilities capabilities_;
815 // Flag to indicate whether the implementation can retain resources, or
816 // whether it should aggressively free them.
817 bool aggressively_free_resources_;
819 base::WeakPtrFactory<GLES2Implementation> weak_ptr_factory_;
821 DISALLOW_COPY_AND_ASSIGN(GLES2Implementation);
824 inline bool GLES2Implementation::GetBufferParameterivHelper(
825 GLenum /* target */, GLenum /* pname */, GLint* /* params */) {
826 return false;
829 inline bool GLES2Implementation::GetFramebufferAttachmentParameterivHelper(
830 GLenum /* target */,
831 GLenum /* attachment */,
832 GLenum /* pname */,
833 GLint* /* params */) {
834 return false;
837 inline bool GLES2Implementation::GetRenderbufferParameterivHelper(
838 GLenum /* target */, GLenum /* pname */, GLint* /* params */) {
839 return false;
842 inline bool GLES2Implementation::GetShaderivHelper(
843 GLuint /* shader */, GLenum /* pname */, GLint* /* params */) {
844 return false;
847 inline bool GLES2Implementation::GetTexParameterfvHelper(
848 GLenum /* target */, GLenum /* pname */, GLfloat* /* params */) {
849 return false;
852 inline bool GLES2Implementation::GetTexParameterivHelper(
853 GLenum /* target */, GLenum /* pname */, GLint* /* params */) {
854 return false;
857 } // namespace gles2
858 } // namespace gpu
860 #endif // GPU_COMMAND_BUFFER_CLIENT_GLES2_IMPLEMENTATION_H_