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_
18 #include "base/compiler_specific.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_cmd_helper.h"
25 #include "gpu/command_buffer/client/gles2_impl_export.h"
26 #include "gpu/command_buffer/client/gles2_interface.h"
27 #include "gpu/command_buffer/client/mapped_memory.h"
28 #include "gpu/command_buffer/client/query_tracker.h"
29 #include "gpu/command_buffer/client/ref_counted.h"
30 #include "gpu/command_buffer/client/ring_buffer.h"
31 #include "gpu/command_buffer/client/share_group.h"
32 #include "gpu/command_buffer/common/capabilities.h"
33 #include "gpu/command_buffer/common/debug_marker_manager.h"
34 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
35 #include "gpu/command_buffer/common/id_allocator.h"
37 #if !defined(NDEBUG) && !defined(__native_client__) && !defined(GLES2_CONFORMANCE_TESTS) // NOLINT
38 #if defined(GLES2_INLINE_OPTIMIZATION)
39 // TODO(gman): Replace with macros that work with inline optmization.
40 #define GPU_CLIENT_SINGLE_THREAD_CHECK()
41 #define GPU_CLIENT_LOG(args)
42 #define GPU_CLIENT_LOG_CODE_BLOCK(code)
43 #define GPU_CLIENT_DCHECK_CODE_BLOCK(code)
45 #include "base/logging.h"
46 #define GPU_CLIENT_SINGLE_THREAD_CHECK() SingleThreadChecker checker(this);
47 #define GPU_CLIENT_LOG(args) DLOG_IF(INFO, debug_) << args;
48 #define GPU_CLIENT_LOG_CODE_BLOCK(code) code
49 #define GPU_CLIENT_DCHECK_CODE_BLOCK(code) code
50 #define GPU_CLIENT_DEBUG
53 #define GPU_CLIENT_SINGLE_THREAD_CHECK()
54 #define GPU_CLIENT_LOG(args)
55 #define GPU_CLIENT_LOG_CODE_BLOCK(code)
56 #define GPU_CLIENT_DCHECK_CODE_BLOCK(code)
59 #if defined(GPU_CLIENT_DEBUG)
60 // Set to 1 to have the client fail when a GL error is generated.
61 // This helps find bugs in the renderer since the debugger stops on the error.
63 # define GL_CLIENT_FAIL_GL_ERRORS
67 // Check that destination pointers point to initialized memory.
68 // When the context is lost, calling GL function has no effect so if destination
69 // pointers point to initialized memory it can often lead to crash bugs. eg.
72 // glGetShaderSource(shader, max_size, &len, buffer);
73 // std::string src(buffer, buffer + len); // len can be uninitialized here!!!
75 // Because this check is not official GL this check happens only on Chrome code,
78 // If it was up to us we'd just always write to the destination but the OpenGL
79 // spec defines the behavior of OpenGL functions, not us. :-(
80 #if defined(__native_client__) || defined(GLES2_CONFORMANCE_TESTS)
81 #define GPU_CLIENT_VALIDATE_DESTINATION_INITALIZATION_ASSERT(v)
82 #define GPU_CLIENT_DCHECK(v)
83 #elif defined(GPU_DCHECK)
84 #define GPU_CLIENT_VALIDATE_DESTINATION_INITALIZATION_ASSERT(v) GPU_DCHECK(v)
85 #define GPU_CLIENT_DCHECK(v) GPU_DCHECK(v)
87 #define GPU_CLIENT_VALIDATE_DESTINATION_INITALIZATION_ASSERT(v) DCHECK(v)
88 #define GPU_CLIENT_DCHECK(v) DCHECK(v)
90 #define GPU_CLIENT_VALIDATE_DESTINATION_INITALIZATION_ASSERT(v) ASSERT(v)
91 #define GPU_CLIENT_DCHECK(v) ASSERT(v)
94 #define GPU_CLIENT_VALIDATE_DESTINATION_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 #define GPU_CLIENT_VALIDATE_DESTINATION_OPTIONAL_INITALIZATION(type, ptr) \
99 GPU_CLIENT_VALIDATE_DESTINATION_INITALIZATION_ASSERT(!ptr || \
100 (ptr[0] == static_cast<type>(0) || ptr[0] == static_cast<type>(-1)));
102 struct GLUniformDefinitionCHROMIUM
;
107 class ScopedTransferBufferPtr
;
108 class TransferBufferInterface
;
113 class VertexArrayObjectManager
;
115 class GLES2ImplementationErrorMessageCallback
{
117 virtual ~GLES2ImplementationErrorMessageCallback() { }
118 virtual void OnErrorMessage(const char* msg
, int id
) = 0;
121 // This class emulates GLES2 over command buffers. It can be used by a client
122 // program so that the program does not need deal with shared memory and command
123 // buffer management. See gl2_lib.h. Note that there is a performance gain to
124 // be had by changing your code to use command buffers directly by using the
125 // GLES2CmdHelper but that entails changing your code to use and deal with
126 // shared memory and synchronization issues.
127 class GLES2_IMPL_EXPORT GLES2Implementation
128 : NON_EXPORTED_BASE(public GLES2Interface
),
129 NON_EXPORTED_BASE(public ContextSupport
) {
131 enum MappedMemoryLimit
{
132 kNoLimit
= MappedMemoryManager::kNoLimit
,
135 // Stores GL state that never changes.
136 struct GLES2_IMPL_EXPORT GLStaticState
{
140 typedef std::pair
<GLenum
, GLenum
> ShaderPrecisionKey
;
141 typedef std::map
<ShaderPrecisionKey
,
142 cmds::GetShaderPrecisionFormat::Result
>
144 ShaderPrecisionMap shader_precisions
;
147 // The maxiumum result size from simple GL get commands.
148 static const size_t kMaxSizeOfSimpleResult
= 16 * sizeof(uint32
); // NOLINT.
150 // used for testing only. If more things are reseved add them here.
151 static const unsigned int kStartingOffset
= kMaxSizeOfSimpleResult
;
153 // Size in bytes to issue async flush for transfer buffer.
154 static const unsigned int kSizeToFlush
= 256 * 1024;
156 // The bucket used for results. Public for testing only.
157 static const uint32 kResultBucketId
= 1;
159 // Alignment of allocations.
160 static const unsigned int kAlignment
= 4;
162 // GL names for the buffers used to emulate client side buffers.
163 static const GLuint kClientSideArrayId
= 0xFEDCBA98u
;
164 static const GLuint kClientSideElementArrayId
= 0xFEDCBA99u
;
166 // Number of swap buffers allowed before waiting.
167 static const size_t kMaxSwapBuffers
= 2;
169 GLES2Implementation(GLES2CmdHelper
* helper
,
170 ShareGroup
* share_group
,
171 TransferBufferInterface
* transfer_buffer
,
172 bool bind_generates_resource
,
173 bool lose_context_when_out_of_memory
,
174 bool support_client_side_arrays
,
175 GpuControl
* gpu_control
);
177 ~GLES2Implementation() override
;
180 unsigned int starting_transfer_buffer_size
,
181 unsigned int min_transfer_buffer_size
,
182 unsigned int max_transfer_buffer_size
,
183 unsigned int mapped_memory_limit
);
185 // The GLES2CmdHelper being used by this GLES2Implementation. You can use
186 // this to issue cmds at a lower level for certain kinds of optimization.
187 GLES2CmdHelper
* helper() const;
189 // Gets client side generated errors.
190 GLenum
GetClientSideGLError();
192 // Include the auto-generated part of this class. We split this because
193 // it means we can easily edit the non-auto generated parts right here in
194 // this file instead of having to edit some template or the code generator.
195 #include "gpu/command_buffer/client/gles2_implementation_autogen.h"
197 void DisableVertexAttribArray(GLuint index
) override
;
198 void EnableVertexAttribArray(GLuint index
) override
;
199 void GetVertexAttribfv(GLuint index
, GLenum pname
, GLfloat
* params
) override
;
200 void GetVertexAttribiv(GLuint index
, GLenum pname
, GLint
* params
) override
;
201 void GetVertexAttribIiv(GLuint index
, GLenum pname
, GLint
* params
) override
;
202 void GetVertexAttribIuiv(GLuint index
, GLenum pname
, GLuint
* params
) override
;
204 // ContextSupport implementation.
205 void Swap() override
;
206 void PartialSwapBuffers(const gfx::Rect
& sub_buffer
) override
;
207 void ScheduleOverlayPlane(int plane_z_order
,
208 gfx::OverlayTransform plane_transform
,
209 unsigned overlay_texture_id
,
210 const gfx::Rect
& display_bounds
,
211 const gfx::RectF
& uv_rect
) override
;
212 GLuint
InsertFutureSyncPointCHROMIUM() override
;
213 void RetireSyncPointCHROMIUM(GLuint sync_point
) override
;
215 void GetProgramInfoCHROMIUMHelper(GLuint program
, std::vector
<int8
>* result
);
216 GLint
GetAttribLocationHelper(GLuint program
, const char* name
);
217 GLint
GetUniformLocationHelper(GLuint program
, const char* name
);
218 GLint
GetFragDataLocationHelper(GLuint program
, const char* name
);
219 bool GetActiveAttribHelper(
220 GLuint program
, GLuint index
, GLsizei bufsize
, GLsizei
* length
,
221 GLint
* size
, GLenum
* type
, char* name
);
222 bool GetActiveUniformHelper(
223 GLuint program
, GLuint index
, GLsizei bufsize
, GLsizei
* length
,
224 GLint
* size
, GLenum
* type
, char* name
);
225 void GetUniformBlocksCHROMIUMHelper(
226 GLuint program
, std::vector
<int8
>* result
);
227 void GetUniformsES3CHROMIUMHelper(
228 GLuint program
, std::vector
<int8
>* result
);
229 GLuint
GetUniformBlockIndexHelper(GLuint program
, const char* name
);
230 bool GetActiveUniformBlockNameHelper(
231 GLuint program
, GLuint index
, GLsizei bufsize
,
232 GLsizei
* length
, char* name
);
233 bool GetActiveUniformBlockivHelper(
234 GLuint program
, GLuint index
, GLenum pname
, GLint
* params
);
235 void GetTransformFeedbackVaryingsCHROMIUMHelper(
236 GLuint program
, std::vector
<int8
>* result
);
237 bool GetTransformFeedbackVaryingHelper(
238 GLuint program
, GLuint index
, GLsizei bufsize
, GLsizei
* length
,
239 GLint
* size
, GLenum
* type
, char* name
);
240 bool GetUniformIndicesHelper(
241 GLuint program
, GLsizei count
, const char* const* names
, GLuint
* indices
);
242 bool GetActiveUniformsivHelper(
243 GLuint program
, GLsizei count
, const GLuint
* indices
,
244 GLenum pname
, GLint
* params
);
245 bool GetSyncivHelper(
246 GLsync sync
, GLenum pname
, GLsizei bufsize
, GLsizei
* length
,
249 void FreeUnusedSharedMemory();
250 void FreeEverything();
252 // ContextSupport implementation.
253 void SignalSyncPoint(uint32 sync_point
,
254 const base::Closure
& callback
) override
;
255 void SignalQuery(uint32 query
, const base::Closure
& callback
) override
;
256 void SetSurfaceVisible(bool visible
) override
;
257 void SetAggressivelyFreeResources(bool aggressively_free_resources
) override
;
259 void SetErrorMessageCallback(
260 GLES2ImplementationErrorMessageCallback
* callback
) {
261 error_message_callback_
= callback
;
264 ShareGroup
* share_group() const {
265 return share_group_
.get();
268 const Capabilities
& capabilities() const {
269 return capabilities_
;
272 GpuControl
* gpu_control() {
276 ShareGroupContextData
* share_group_context_data() {
277 return &share_group_context_data_
;
281 friend class GLES2ImplementationTest
;
282 friend class VertexArrayObjectManager
;
284 // Used to track whether an extension is available
285 enum ExtensionStatus
{
286 kAvailableExtensionStatus
,
287 kUnavailableExtensionStatus
,
288 kUnknownExtensionStatus
291 // Base class for mapped resources.
292 struct MappedResource
{
293 MappedResource(GLenum _access
, int _shm_id
, void* mem
, unsigned int offset
)
300 // access mode. Currently only GL_WRITE_ONLY is valid
303 // Shared memory ID for buffer.
306 // Address of shared memory
309 // Offset of shared memory
310 unsigned int shm_offset
;
313 // Used to track mapped textures.
314 struct MappedTexture
: public MappedResource
{
319 unsigned int shm_offset
,
328 : MappedResource(access
, shm_id
, shm_mem
, shm_offset
),
339 // These match the arguments to TexSubImage2D.
350 // Used to track mapped buffers.
351 struct MappedBuffer
: public MappedResource
{
356 unsigned int shm_offset
,
360 : MappedResource(access
, shm_id
, shm_mem
, shm_offset
),
366 // These match the arguments to BufferSubData.
374 : bound_texture_2d(0),
375 bound_texture_cube_map(0),
376 bound_texture_external_oes(0) {}
378 // texture currently bound to this unit's GL_TEXTURE_2D with glBindTexture
379 GLuint bound_texture_2d
;
381 // texture currently bound to this unit's GL_TEXTURE_CUBE_MAP with
383 GLuint bound_texture_cube_map
;
385 // texture currently bound to this unit's GL_TEXTURE_EXTERNAL_OES with
387 GLuint bound_texture_external_oes
;
390 // Checks for single threaded access.
391 class SingleThreadChecker
{
393 explicit SingleThreadChecker(GLES2Implementation
* gles2_implementation
);
394 ~SingleThreadChecker();
397 GLES2Implementation
* gles2_implementation_
;
400 // Gets the value of the result.
401 template <typename T
>
403 return static_cast<T
>(GetResultBuffer());
406 void* GetResultBuffer();
407 int32
GetResultShmId();
408 uint32
GetResultShmOffset();
410 // Lazily determines if GL_ANGLE_pack_reverse_row_order is available
411 bool IsAnglePackReverseRowOrderAvailable();
412 bool IsChromiumFramebufferMultisampleAvailable();
414 bool IsExtensionAvailableHelper(
415 const char* extension
, ExtensionStatus
* status
);
417 // Gets the GLError through our wrapper.
420 // Sets our wrapper for the GLError.
421 void SetGLError(GLenum error
, const char* function_name
, const char* msg
);
422 void SetGLErrorInvalidEnum(
423 const char* function_name
, GLenum value
, const char* label
);
425 // Returns the last error and clears it. Useful for debugging.
426 const std::string
& GetLastError() {
430 // Waits for all commands to execute.
433 // TODO(gman): These bucket functions really seem like they belong in
434 // CommandBufferHelper (or maybe BucketHelper?). Unfortunately they need
435 // a transfer buffer to function which is currently managed by this class.
437 // Gets the contents of a bucket.
438 bool GetBucketContents(uint32 bucket_id
, std::vector
<int8
>* data
);
440 // Sets the contents of a bucket.
441 void SetBucketContents(uint32 bucket_id
, const void* data
, size_t size
);
443 // Sets the contents of a bucket as a string.
444 void SetBucketAsCString(uint32 bucket_id
, const char* str
);
446 // Gets the contents of a bucket as a string. Returns false if there is no
447 // string available which is a separate case from the empty string.
448 bool GetBucketAsString(uint32 bucket_id
, std::string
* str
);
450 // Sets the contents of a bucket as a string.
451 void SetBucketAsString(uint32 bucket_id
, const std::string
& str
);
453 // Returns true if id is reserved.
454 bool IsBufferReservedId(GLuint id
);
455 bool IsFramebufferReservedId(GLuint id
) { return false; }
456 bool IsRenderbufferReservedId(GLuint id
) { return false; }
457 bool IsTextureReservedId(GLuint id
) { return false; }
458 bool IsVertexArrayReservedId(GLuint id
) { return false; }
459 bool IsProgramReservedId(GLuint id
) { return false; }
460 bool IsValuebufferReservedId(GLuint id
) { return false; }
461 bool IsSamplerReservedId(GLuint id
) { return false; }
462 bool IsTransformFeedbackReservedId(GLuint id
) { return false; }
464 void BindBufferHelper(GLenum target
, GLuint buffer
);
465 void BindBufferBaseHelper(GLenum target
, GLuint index
, GLuint buffer
);
466 void BindBufferRangeHelper(GLenum target
, GLuint index
, GLuint buffer
,
467 GLintptr offset
, GLsizeiptr size
);
468 void BindFramebufferHelper(GLenum target
, GLuint framebuffer
);
469 void BindRenderbufferHelper(GLenum target
, GLuint renderbuffer
);
470 void BindSamplerHelper(GLuint unit
, GLuint sampler
);
471 void BindTextureHelper(GLenum target
, GLuint texture
);
472 void BindTransformFeedbackHelper(GLenum target
, GLuint transformfeedback
);
473 void BindVertexArrayOESHelper(GLuint array
);
474 void BindValuebufferCHROMIUMHelper(GLenum target
, GLuint valuebuffer
);
475 void UseProgramHelper(GLuint program
);
477 void BindBufferStub(GLenum target
, GLuint buffer
);
478 void BindBufferBaseStub(GLenum target
, GLuint index
, GLuint buffer
);
479 void BindBufferRangeStub(GLenum target
, GLuint index
, GLuint buffer
,
480 GLintptr offset
, GLsizeiptr size
);
481 void BindFramebufferStub(GLenum target
, GLuint framebuffer
);
482 void BindRenderbufferStub(GLenum target
, GLuint renderbuffer
);
483 void BindTextureStub(GLenum target
, GLuint texture
);
484 void BindValuebufferCHROMIUMStub(GLenum target
, GLuint valuebuffer
);
486 void GenBuffersHelper(GLsizei n
, const GLuint
* buffers
);
487 void GenFramebuffersHelper(GLsizei n
, const GLuint
* framebuffers
);
488 void GenRenderbuffersHelper(GLsizei n
, const GLuint
* renderbuffers
);
489 void GenTexturesHelper(GLsizei n
, const GLuint
* textures
);
490 void GenVertexArraysOESHelper(GLsizei n
, const GLuint
* arrays
);
491 void GenQueriesEXTHelper(GLsizei n
, const GLuint
* queries
);
492 void GenValuebuffersCHROMIUMHelper(GLsizei n
, const GLuint
* valuebuffers
);
493 void GenSamplersHelper(GLsizei n
, const GLuint
* samplers
);
494 void GenTransformFeedbacksHelper(GLsizei n
, const GLuint
* transformfeedbacks
);
496 void DeleteBuffersHelper(GLsizei n
, const GLuint
* buffers
);
497 void DeleteFramebuffersHelper(GLsizei n
, const GLuint
* framebuffers
);
498 void DeleteRenderbuffersHelper(GLsizei n
, const GLuint
* renderbuffers
);
499 void DeleteTexturesHelper(GLsizei n
, const GLuint
* textures
);
500 bool DeleteProgramHelper(GLuint program
);
501 bool DeleteShaderHelper(GLuint shader
);
502 void DeleteQueriesEXTHelper(GLsizei n
, const GLuint
* queries
);
503 void DeleteVertexArraysOESHelper(GLsizei n
, const GLuint
* arrays
);
504 void DeleteValuebuffersCHROMIUMHelper(GLsizei n
, const GLuint
* valuebuffers
);
505 void DeleteSamplersHelper(GLsizei n
, const GLuint
* samplers
);
506 void DeleteTransformFeedbacksHelper(
507 GLsizei n
, const GLuint
* transformfeedbacks
);
508 void DeleteSyncHelper(GLsync sync
);
510 void DeleteBuffersStub(GLsizei n
, const GLuint
* buffers
);
511 void DeleteFramebuffersStub(GLsizei n
, const GLuint
* framebuffers
);
512 void DeleteRenderbuffersStub(GLsizei n
, const GLuint
* renderbuffers
);
513 void DeleteTexturesStub(GLsizei n
, const GLuint
* textures
);
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
,
534 GLenum internalformat
);
535 void DestroyImageCHROMIUMHelper(GLuint image_id
);
536 GLuint
CreateGpuMemoryBufferImageCHROMIUMHelper(GLsizei width
,
538 GLenum internalformat
,
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 RestoreElementAndArrayBuffers(bool restore
);
548 void RestoreArrayBuffer(bool restrore
);
550 // The pixels pointer should already account for unpack skip
551 // images/rows/pixels.
552 void TexSubImage2DImpl(
553 GLenum target
, GLint level
, GLint xoffset
, GLint yoffset
, GLsizei width
,
554 GLsizei height
, GLenum format
, GLenum type
, uint32 unpadded_row_size
,
555 const void* pixels
, uint32 pixels_padded_row_size
, GLboolean internal
,
556 ScopedTransferBufferPtr
* buffer
, uint32 buffer_padded_row_size
);
557 void TexSubImage3DImpl(
558 GLenum target
, GLint level
, GLint xoffset
, GLint yoffset
, GLint zoffset
,
559 GLsizei width
, GLsizei height
, GLsizei depth
, GLenum format
, GLenum type
,
560 uint32 unpadded_row_size
, const void* pixels
,
561 uint32 pixels_padded_row_size
, GLboolean internal
,
562 ScopedTransferBufferPtr
* buffer
, uint32 buffer_padded_row_size
);
564 // Helpers for query functions.
565 bool GetHelper(GLenum pname
, GLint
* params
);
566 GLuint
GetBoundBufferHelper(GLenum target
);
567 bool GetBooleanvHelper(GLenum pname
, GLboolean
* params
);
568 bool GetBufferParameterivHelper(GLenum target
, GLenum pname
, GLint
* params
);
569 bool GetFloatvHelper(GLenum pname
, GLfloat
* params
);
570 bool GetFramebufferAttachmentParameterivHelper(
571 GLenum target
, GLenum attachment
, GLenum pname
, GLint
* params
);
572 bool GetInteger64vHelper(GLenum pname
, GLint64
* params
);
573 bool GetIntegervHelper(GLenum pname
, GLint
* params
);
574 bool GetIntegeri_vHelper(GLenum pname
, GLuint index
, GLint
* data
);
575 bool GetInteger64i_vHelper(GLenum pname
, GLuint index
, GLint64
* data
);
576 bool GetInternalformativHelper(
577 GLenum target
, GLenum format
, GLenum pname
, GLsizei bufSize
,
579 bool GetProgramivHelper(GLuint program
, GLenum pname
, GLint
* params
);
580 bool GetSamplerParameterfvHelper(
581 GLuint sampler
, GLenum pname
, GLfloat
* params
);
582 bool GetSamplerParameterivHelper(
583 GLuint sampler
, GLenum pname
, GLint
* params
);
584 bool GetRenderbufferParameterivHelper(
585 GLenum target
, GLenum pname
, GLint
* params
);
586 bool GetShaderivHelper(GLuint shader
, GLenum pname
, GLint
* params
);
587 bool GetTexParameterfvHelper(GLenum target
, GLenum pname
, GLfloat
* params
);
588 bool GetTexParameterivHelper(GLenum target
, GLenum pname
, GLint
* params
);
589 const GLubyte
* GetStringHelper(GLenum name
);
591 bool IsExtensionAvailable(const char* ext
);
593 // Caches certain capabilties state. Return true if cached.
594 bool SetCapabilityState(GLenum cap
, bool enabled
);
596 IdHandlerInterface
* GetIdHandler(int id_namespace
) const;
597 // IdAllocators for objects that can't be shared among contexts.
598 // For now, used only for Queries. TODO(hj.r.chung) Should be added for
599 // Framebuffer and Vertex array objects.
600 IdAllocator
* GetIdAllocator(int id_namespace
) const;
605 void RunIfContextNotLost(const base::Closure
& callback
);
607 // Validate if an offset is valid, i.e., non-negative and fit into 32-bit.
608 // If not, generate an approriate error, and return false.
609 bool ValidateOffset(const char* func
, GLintptr offset
);
611 // Validate if a size is valid, i.e., non-negative and fit into 32-bit.
612 // If not, generate an approriate error, and return false.
613 bool ValidateSize(const char* func
, GLsizeiptr offset
);
615 // Remove the transfer buffer from the buffer tracker. For buffers used
616 // asynchronously the memory is free:ed if the upload has completed. For
617 // other buffers, the memory is either free:ed immediately or free:ed pending
619 void RemoveTransferBuffer(BufferTracker::Buffer
* buffer
);
621 // Returns true if the async upload token has passed.
623 // NOTE: This will detect wrapped async tokens by checking if the most
624 // significant bit of async token to check is 1 but the last read is 0, i.e.
625 // the uint32 wrapped.
626 bool HasAsyncUploadTokenPassed(uint32 token
) const {
627 return async_upload_sync_
->HasAsyncUploadTokenPassed(token
);
630 // Get the next async upload token.
631 uint32
NextAsyncUploadToken();
633 // Ensure that the shared memory used for synchronizing async upload tokens
636 // Returns false on error, true on success.
637 bool EnsureAsyncUploadSync();
639 // Checks the last read asynchronously upload token and frees any unmanaged
640 // transfer buffer that has its async token passed.
641 void PollAsyncUploads();
643 // Free every async upload buffer. If some async upload buffer is still in use
644 // wait for them to finish before freeing.
645 void FreeAllAsyncUploadBuffers();
647 bool GetBoundPixelTransferBuffer(
648 GLenum target
, const char* function_name
, GLuint
* buffer_id
);
649 BufferTracker::Buffer
* GetBoundPixelUnpackTransferBufferIfValid(
651 const char* function_name
, GLuint offset
, GLsizei size
);
653 // Pack 2D arrays of char into a bucket.
654 // Helper function for ShaderSource(), TransformFeedbackVaryings(), etc.
655 bool PackStringsToBucket(GLsizei count
,
656 const char* const* str
,
658 const char* func_name
);
660 const std::string
& GetLogPrefix() const;
662 #if defined(GL_CLIENT_FAIL_GL_ERRORS)
664 void FailGLError(GLenum error
);
666 void CheckGLError() { }
667 void FailGLError(GLenum
/* error */) { }
670 void RemoveMappedBufferRangeByTarget(GLenum target
);
671 void RemoveMappedBufferRangeById(GLuint buffer
);
672 void ClearMappedBufferRangeMap();
674 void DrawElementsImpl(GLenum mode
, GLsizei count
, GLenum type
,
675 const void* indices
, const char* func_name
);
678 GLES2CmdHelper
* helper_
;
679 TransferBufferInterface
* transfer_buffer_
;
680 std::string last_error_
;
681 DebugMarkerManager debug_marker_manager_
;
682 std::string this_in_hex_
;
684 std::queue
<int32
> swap_buffers_tokens_
;
685 std::queue
<int32
> rate_limit_tokens_
;
687 ExtensionStatus angle_pack_reverse_row_order_status_
;
688 ExtensionStatus chromium_framebuffer_multisample_
;
690 GLStaticState static_state_
;
691 ClientContextState state_
;
693 // pack alignment as last set by glPixelStorei
694 GLint pack_alignment_
;
696 // unpack alignment as last set by glPixelStorei
697 GLint unpack_alignment_
;
699 // unpack yflip as last set by glPixelstorei
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.
766 // Whether or not to print debugging info.
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.
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 typedef std::map
<GLuint
, QueryTracker::Query
*> QueryMap
;
804 QueryMap current_queries_
;
805 scoped_ptr
<IdAllocator
> query_id_allocator_
;
807 scoped_ptr
<BufferTracker
> buffer_tracker_
;
809 GLES2ImplementationErrorMessageCallback
* error_message_callback_
;
811 int current_trace_stack_
;
813 GpuControl
* gpu_control_
;
815 Capabilities capabilities_
;
817 // Flag to indicate whether the implementation can retain resources, or
818 // whether it should aggressively free them.
819 bool aggressively_free_resources_
;
821 base::WeakPtrFactory
<GLES2Implementation
> weak_ptr_factory_
;
823 DISALLOW_COPY_AND_ASSIGN(GLES2Implementation
);
826 inline bool GLES2Implementation::GetBufferParameterivHelper(
827 GLenum
/* target */, GLenum
/* pname */, GLint
* /* params */) {
831 inline bool GLES2Implementation::GetFramebufferAttachmentParameterivHelper(
833 GLenum
/* attachment */,
835 GLint
* /* params */) {
839 inline bool GLES2Implementation::GetRenderbufferParameterivHelper(
840 GLenum
/* target */, GLenum
/* pname */, GLint
* /* params */) {
844 inline bool GLES2Implementation::GetShaderivHelper(
845 GLuint
/* shader */, GLenum
/* pname */, GLint
* /* params */) {
849 inline bool GLES2Implementation::GetTexParameterfvHelper(
850 GLenum
/* target */, GLenum
/* pname */, GLfloat
* /* params */) {
854 inline bool GLES2Implementation::GetTexParameterivHelper(
855 GLenum
/* target */, GLenum
/* pname */, GLint
* /* params */) {
862 #endif // GPU_COMMAND_BUFFER_CLIENT_GLES2_IMPLEMENTATION_H_