Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / gpu / command_buffer / service / texture_manager.h
blob976213b72cc993bd934506cbaec7e8e42bae5f27
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_SERVICE_TEXTURE_MANAGER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_TEXTURE_MANAGER_H_
8 #include <algorithm>
9 #include <list>
10 #include <set>
11 #include <string>
12 #include <vector>
13 #include "base/basictypes.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/memory/ref_counted.h"
16 #include "gpu/command_buffer/service/async_pixel_transfer_delegate.h"
17 #include "gpu/command_buffer/service/gl_utils.h"
18 #include "gpu/command_buffer/service/memory_tracking.h"
19 #include "gpu/gpu_export.h"
20 #include "ui/gfx/geometry/rect.h"
21 #include "ui/gl/gl_image.h"
23 namespace gpu {
24 namespace gles2 {
26 class GLES2Decoder;
27 struct ContextState;
28 struct DecoderFramebufferState;
29 class Display;
30 class ErrorState;
31 class FeatureInfo;
32 class FramebufferManager;
33 class MailboxManager;
34 class TextureManager;
35 class TextureRef;
37 // Info about Textures currently in the system.
38 // This class wraps a real GL texture, keeping track of its meta-data. It is
39 // jointly owned by possibly multiple TextureRef.
40 class GPU_EXPORT Texture {
41 public:
42 explicit Texture(GLuint service_id);
44 GLenum min_filter() const {
45 return min_filter_;
48 GLenum mag_filter() const {
49 return mag_filter_;
52 GLenum wrap_r() const {
53 return wrap_r_;
56 GLenum wrap_s() const {
57 return wrap_s_;
60 GLenum wrap_t() const {
61 return wrap_t_;
64 GLenum usage() const {
65 return usage_;
68 GLenum pool() const {
69 return pool_;
72 GLenum compare_func() const {
73 return compare_func_;
76 GLenum compare_mode() const {
77 return compare_mode_;
80 GLfloat max_lod() const {
81 return max_lod_;
84 GLfloat min_lod() const {
85 return min_lod_;
88 GLint base_level() const {
89 return base_level_;
92 GLint max_level() const {
93 return max_level_;
96 int num_uncleared_mips() const {
97 return num_uncleared_mips_;
100 uint32 estimated_size() const {
101 return estimated_size_;
104 bool CanRenderTo() const {
105 return target_ != GL_TEXTURE_EXTERNAL_OES;
108 // The service side OpenGL id of the texture.
109 GLuint service_id() const {
110 return service_id_;
113 void SetServiceId(GLuint service_id) {
114 DCHECK(service_id);
115 service_id_ = service_id;
118 // Returns the target this texure was first bound to or 0 if it has not
119 // been bound. Once a texture is bound to a specific target it can never be
120 // bound to a different target.
121 GLenum target() const {
122 return target_;
125 bool SafeToRenderFrom() const {
126 return cleared_;
129 // Get the width/height/depth for a particular level. Returns false if level
130 // does not exist.
131 // |depth| is optional and can be nullptr.
132 bool GetLevelSize(
133 GLint target, GLint level,
134 GLsizei* width, GLsizei* height, GLsizei* depth) const;
136 // Get the type of a level. Returns false if level does not exist.
137 bool GetLevelType(
138 GLint target, GLint level, GLenum* type, GLenum* internal_format) const;
140 // Get the image bound to a particular level. Returns NULL if level
141 // does not exist.
142 gfx::GLImage* GetLevelImage(GLint target, GLint level) const;
144 bool HasImages() const {
145 return has_images_;
148 // Returns true of the given dimensions are inside the dimensions of the
149 // level and if the type matches the level.
150 bool ValidForTexture(
151 GLint target,
152 GLint level,
153 GLint xoffset,
154 GLint yoffset,
155 GLint zoffset,
156 GLsizei width,
157 GLsizei height,
158 GLsizei depth,
159 GLenum type) const;
161 bool IsValid() const {
162 return !!target();
165 bool IsAttachedToFramebuffer() const {
166 return framebuffer_attachment_count_ != 0;
169 void AttachToFramebuffer() {
170 ++framebuffer_attachment_count_;
173 void DetachFromFramebuffer() {
174 DCHECK_GT(framebuffer_attachment_count_, 0);
175 --framebuffer_attachment_count_;
178 void SetImmutable(bool immutable) {
179 immutable_ = immutable;
182 bool IsImmutable() const {
183 return immutable_;
186 // Get the cleared rectangle for a particular level. Returns an empty
187 // rectangle if level does not exist.
188 gfx::Rect GetLevelClearedRect(GLenum target, GLint level) const;
190 // Whether a particular level/face is cleared.
191 bool IsLevelCleared(GLenum target, GLint level) const;
193 // Whether the texture has been defined
194 bool IsDefined() const {
195 return estimated_size() > 0;
198 // Initialize TEXTURE_MAX_ANISOTROPY to 1 if we haven't done so yet.
199 void InitTextureMaxAnisotropyIfNeeded(GLenum target);
201 void OnWillModifyPixels();
202 void OnDidModifyPixels();
204 void DumpLevelMemory(base::trace_event::ProcessMemoryDump* pmd,
205 uint64_t client_tracing_id,
206 const std::string& dump_name) const;
208 private:
209 friend class MailboxManagerImpl;
210 friend class MailboxManagerSync;
211 friend class MailboxManagerTest;
212 friend class TextureDefinition;
213 friend class TextureManager;
214 friend class TextureRef;
215 friend class TextureTestHelper;
217 ~Texture();
218 void AddTextureRef(TextureRef* ref);
219 void RemoveTextureRef(TextureRef* ref, bool have_context);
220 MemoryTypeTracker* GetMemTracker();
222 // Condition on which this texture is renderable. Can be ONLY_IF_NPOT if it
223 // depends on context support for non-power-of-two textures (i.e. will be
224 // renderable if NPOT support is in the context, otherwise not, e.g. texture
225 // with a NPOT level). ALWAYS means it doesn't depend on context features
226 // (e.g. complete POT), NEVER means it's not renderable regardless (e.g.
227 // incomplete).
228 enum CanRenderCondition {
229 CAN_RENDER_ALWAYS,
230 CAN_RENDER_NEVER,
231 CAN_RENDER_ONLY_IF_NPOT
234 struct LevelInfo {
235 LevelInfo();
236 LevelInfo(const LevelInfo& rhs);
237 ~LevelInfo();
239 gfx::Rect cleared_rect;
240 GLenum target;
241 GLint level;
242 GLenum internal_format;
243 GLsizei width;
244 GLsizei height;
245 GLsizei depth;
246 GLint border;
247 GLenum format;
248 GLenum type;
249 scoped_refptr<gfx::GLImage> image;
250 uint32 estimated_size;
253 struct FaceInfo {
254 FaceInfo();
255 ~FaceInfo();
257 GLsizei num_mip_levels;
258 std::vector<LevelInfo> level_infos;
261 // Set the info for a particular level.
262 void SetLevelInfo(const FeatureInfo* feature_info,
263 GLenum target,
264 GLint level,
265 GLenum internal_format,
266 GLsizei width,
267 GLsizei height,
268 GLsizei depth,
269 GLint border,
270 GLenum format,
271 GLenum type,
272 const gfx::Rect& cleared_rect);
274 // In GLES2 "texture complete" means it has all required mips for filtering
275 // down to a 1x1 pixel texture, they are in the correct order, they are all
276 // the same format.
277 bool texture_complete() const {
278 return texture_complete_;
281 // In GLES2 "cube complete" means all 6 faces level 0 are defined, all the
282 // same format, all the same dimensions and all width = height.
283 bool cube_complete() const {
284 return cube_complete_;
287 // Whether or not this texture is a non-power-of-two texture.
288 bool npot() const {
289 return npot_;
292 // Marks a |rect| of a particular level as cleared.
293 void SetLevelClearedRect(GLenum target,
294 GLint level,
295 const gfx::Rect& cleared_rect);
297 // Marks a particular level as cleared or uncleared.
298 void SetLevelCleared(GLenum target, GLint level, bool cleared);
300 // Updates the cleared flag for this texture by inspecting all the mips.
301 void UpdateCleared();
303 // Clears any renderable uncleared levels.
304 // Returns false if a GL error was generated.
305 bool ClearRenderableLevels(GLES2Decoder* decoder);
307 // Clears the level.
308 // Returns false if a GL error was generated.
309 bool ClearLevel(GLES2Decoder* decoder, GLenum target, GLint level);
311 // Sets a texture parameter.
312 // TODO(gman): Expand to SetParameteriv,fv
313 // Returns GL_NO_ERROR on success. Otherwise the error to generate.
314 GLenum SetParameteri(
315 const FeatureInfo* feature_info, GLenum pname, GLint param);
316 GLenum SetParameterf(
317 const FeatureInfo* feature_info, GLenum pname, GLfloat param);
319 // Makes each of the mip levels as though they were generated.
320 bool MarkMipmapsGenerated(const FeatureInfo* feature_info);
322 bool NeedsMips() const {
323 return min_filter_ != GL_NEAREST && min_filter_ != GL_LINEAR;
326 // True if this texture meets all the GLES2 criteria for rendering.
327 // See section 3.8.2 of the GLES2 spec.
328 bool CanRender(const FeatureInfo* feature_info) const;
330 // Returns true if mipmaps can be generated by GL.
331 bool CanGenerateMipmaps(const FeatureInfo* feature_info) const;
333 // Returns true if any of the texture dimensions are not a power of two.
334 static bool TextureIsNPOT(GLsizei width, GLsizei height, GLsizei depth);
336 // Returns true if texture face is complete relative to the first face.
337 static bool TextureFaceComplete(const Texture::LevelInfo& first_face,
338 size_t face_index,
339 GLenum target,
340 GLenum internal_format,
341 GLsizei width,
342 GLsizei height,
343 GLsizei depth,
344 GLenum format,
345 GLenum type);
347 // Returns true if texture mip level is complete relative to first level.
348 static bool TextureMipComplete(const Texture::LevelInfo& level0_face,
349 GLenum target,
350 GLint level,
351 GLenum internal_format,
352 GLsizei width,
353 GLsizei height,
354 GLsizei depth,
355 GLenum format,
356 GLenum type);
358 // Sets the Texture's target
359 // Parameters:
360 // target: GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP or
361 // GL_TEXTURE_EXTERNAL_OES or GL_TEXTURE_RECTANGLE_ARB
362 // GL_TEXTURE_2D_ARRAY or GL_TEXTURE_3D (for GLES3)
363 // max_levels: The maximum levels this type of target can have.
364 void SetTarget(
365 const FeatureInfo* feature_info, GLenum target, GLint max_levels);
367 // Update info about this texture.
368 void Update(const FeatureInfo* feature_info);
370 // Set the image for a particular level.
371 void SetLevelImage(
372 const FeatureInfo* feature_info,
373 GLenum target,
374 GLint level,
375 gfx::GLImage* image);
377 // Appends a signature for the given level.
378 void AddToSignature(
379 const FeatureInfo* feature_info,
380 GLenum target, GLint level, std::string* signature) const;
382 void SetMailboxManager(MailboxManager* mailbox_manager);
384 // Updates the unsafe textures count in all the managers referencing this
385 // texture.
386 void UpdateSafeToRenderFrom(bool cleared);
388 // Updates the uncleared mip count in all the managers referencing this
389 // texture.
390 void UpdateMipCleared(LevelInfo* info,
391 GLsizei width,
392 GLsizei height,
393 const gfx::Rect& cleared_rect);
395 // Computes the CanRenderCondition flag.
396 CanRenderCondition GetCanRenderCondition() const;
398 // Updates the unrenderable texture count in all the managers referencing this
399 // texture.
400 void UpdateCanRenderCondition();
402 // Updates the images count in all the managers referencing this
403 // texture.
404 void UpdateHasImages();
406 // Increment the framebuffer state change count in all the managers
407 // referencing this texture.
408 void IncAllFramebufferStateChangeCount();
410 MailboxManager* mailbox_manager_;
412 // Info about each face and level of texture.
413 std::vector<FaceInfo> face_infos_;
415 // The texture refs that point to this Texture.
416 typedef std::set<TextureRef*> RefSet;
417 RefSet refs_;
419 // The single TextureRef that accounts for memory for this texture. Must be
420 // one of refs_.
421 TextureRef* memory_tracking_ref_;
423 // The id of the texure
424 GLuint service_id_;
426 // Whether all renderable mips of this texture have been cleared.
427 bool cleared_;
429 int num_uncleared_mips_;
430 int num_npot_faces_;
432 // The target. 0 if unset, otherwise GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP.
433 // Or GL_TEXTURE_2D_ARRAY or GL_TEXTURE_3D (for GLES3).
434 GLenum target_;
436 // Texture parameters.
437 GLenum min_filter_;
438 GLenum mag_filter_;
439 GLenum wrap_r_;
440 GLenum wrap_s_;
441 GLenum wrap_t_;
442 GLenum usage_;
443 GLenum pool_;
444 GLenum compare_func_;
445 GLenum compare_mode_;
446 GLfloat max_lod_;
447 GLfloat min_lod_;
448 GLint base_level_;
449 GLint max_level_;
451 // The maximum level that has been set.
452 GLint max_level_set_;
454 // Whether or not this texture is "texture complete"
455 bool texture_complete_;
457 // Whether mip levels have changed and should be reverified.
458 bool texture_mips_dirty_;
459 bool texture_mips_complete_;
461 // Whether or not this texture is "cube complete"
462 bool cube_complete_;
464 // Whether any level 0 faces have changed and should be reverified.
465 bool texture_level0_dirty_;
466 bool texture_level0_complete_;
468 // Whether or not this texture is non-power-of-two
469 bool npot_;
471 // Whether this texture has ever been bound.
472 bool has_been_bound_;
474 // The number of framebuffers this texture is attached to.
475 int framebuffer_attachment_count_;
477 // Whether the texture is immutable and no further changes to the format
478 // or dimensions of the texture object can be made.
479 bool immutable_;
481 // Whether or not this texture has images.
482 bool has_images_;
484 // Size in bytes this texture is assumed to take in memory.
485 uint32 estimated_size_;
487 // Cache of the computed CanRenderCondition flag.
488 CanRenderCondition can_render_condition_;
490 // Whether we have initialized TEXTURE_MAX_ANISOTROPY to 1.
491 bool texture_max_anisotropy_initialized_;
493 DISALLOW_COPY_AND_ASSIGN(Texture);
496 // This class represents a texture in a client context group. It's mostly 1:1
497 // with a client id, though it can outlive the client id if it's still bound to
498 // a FBO or another context when destroyed.
499 // Multiple TextureRef can point to the same texture with cross-context sharing.
500 class GPU_EXPORT TextureRef : public base::RefCounted<TextureRef> {
501 public:
502 TextureRef(TextureManager* manager, GLuint client_id, Texture* texture);
503 static scoped_refptr<TextureRef> Create(TextureManager* manager,
504 GLuint client_id,
505 GLuint service_id);
507 void AddObserver() { num_observers_++; }
508 void RemoveObserver() { num_observers_--; }
510 const Texture* texture() const { return texture_; }
511 Texture* texture() { return texture_; }
512 GLuint client_id() const { return client_id_; }
513 GLuint service_id() const { return texture_->service_id(); }
514 GLint num_observers() const { return num_observers_; }
516 private:
517 friend class base::RefCounted<TextureRef>;
518 friend class Texture;
519 friend class TextureManager;
521 ~TextureRef();
522 const TextureManager* manager() const { return manager_; }
523 TextureManager* manager() { return manager_; }
524 void reset_client_id() { client_id_ = 0; }
526 TextureManager* manager_;
527 Texture* texture_;
528 GLuint client_id_;
529 GLint num_observers_;
531 DISALLOW_COPY_AND_ASSIGN(TextureRef);
534 // Holds data that is per gles2_cmd_decoder, but is related to to the
535 // TextureManager.
536 struct DecoderTextureState {
537 // total_texture_upload_time automatically initialized to 0 in default
538 // constructor.
539 explicit DecoderTextureState(bool texsubimage_faster_than_teximage)
540 : tex_image_failed(false),
541 texture_upload_count(0),
542 texsubimage_faster_than_teximage(texsubimage_faster_than_teximage) {}
544 // This indicates all the following texSubImage*D calls that are part of the
545 // failed texImage*D call should be ignored. The client calls have a lock
546 // around them, so it will affect only a single texImage*D + texSubImage*D
547 // group.
548 bool tex_image_failed;
550 // Command buffer stats.
551 int texture_upload_count;
552 base::TimeDelta total_texture_upload_time;
554 bool texsubimage_faster_than_teximage;
557 // This class keeps track of the textures and their sizes so we can do NPOT and
558 // texture complete checking.
560 // NOTE: To support shared resources an instance of this class will need to be
561 // shared by multiple GLES2Decoders.
562 class GPU_EXPORT TextureManager : public base::trace_event::MemoryDumpProvider {
563 public:
564 class GPU_EXPORT DestructionObserver {
565 public:
566 DestructionObserver();
567 virtual ~DestructionObserver();
569 // Called in ~TextureManager.
570 virtual void OnTextureManagerDestroying(TextureManager* manager) = 0;
572 // Called via ~TextureRef.
573 virtual void OnTextureRefDestroying(TextureRef* texture) = 0;
575 private:
576 DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
579 enum DefaultAndBlackTextures {
580 kTexture2D,
581 kCubeMap,
582 kExternalOES,
583 kRectangleARB,
584 kNumDefaultTextures
587 TextureManager(MemoryTracker* memory_tracker,
588 FeatureInfo* feature_info,
589 GLsizei max_texture_size,
590 GLsizei max_cube_map_texture_size,
591 GLsizei max_rectangle_texture_size,
592 GLsizei max_3d_texture_size,
593 bool use_default_textures);
594 ~TextureManager() override;
596 void set_framebuffer_manager(FramebufferManager* manager) {
597 framebuffer_manager_ = manager;
600 // Init the texture manager.
601 bool Initialize();
603 // Must call before destruction.
604 void Destroy(bool have_context);
606 // Returns the maximum number of levels.
607 GLint MaxLevelsForTarget(GLenum target) const {
608 switch (target) {
609 case GL_TEXTURE_2D:
610 return max_levels_;
611 case GL_TEXTURE_RECTANGLE_ARB:
612 case GL_TEXTURE_EXTERNAL_OES:
613 return 1;
614 case GL_TEXTURE_3D:
615 case GL_TEXTURE_2D_ARRAY:
616 return max_3d_levels_;
617 default:
618 return max_cube_map_levels_;
622 // Returns the maximum size.
623 GLsizei MaxSizeForTarget(GLenum target) const {
624 switch (target) {
625 case GL_TEXTURE_2D:
626 case GL_TEXTURE_EXTERNAL_OES:
627 return max_texture_size_;
628 case GL_TEXTURE_RECTANGLE:
629 return max_rectangle_texture_size_;
630 case GL_TEXTURE_3D:
631 case GL_TEXTURE_2D_ARRAY:
632 return max_3d_texture_size_;
633 default:
634 return max_cube_map_texture_size_;
638 // Returns the maxium number of levels a texture of the given size can have.
639 static GLsizei ComputeMipMapCount(GLenum target,
640 GLsizei width,
641 GLsizei height,
642 GLsizei depth);
644 // Checks if a dimensions are valid for a given target.
645 bool ValidForTarget(
646 GLenum target, GLint level,
647 GLsizei width, GLsizei height, GLsizei depth);
649 // True if this texture meets all the GLES2 criteria for rendering.
650 // See section 3.8.2 of the GLES2 spec.
651 bool CanRender(const TextureRef* ref) const {
652 return ref->texture()->CanRender(feature_info_.get());
655 // Returns true if mipmaps can be generated by GL.
656 bool CanGenerateMipmaps(const TextureRef* ref) const {
657 return ref->texture()->CanGenerateMipmaps(feature_info_.get());
660 // Sets the Texture's target
661 // Parameters:
662 // target: GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP
663 // GL_TEXTURE_2D_ARRAY or GL_TEXTURE_3D (for GLES3)
664 // max_levels: The maximum levels this type of target can have.
665 void SetTarget(
666 TextureRef* ref,
667 GLenum target);
669 // Set the info for a particular level in a TexureInfo.
670 void SetLevelInfo(TextureRef* ref,
671 GLenum target,
672 GLint level,
673 GLenum internal_format,
674 GLsizei width,
675 GLsizei height,
676 GLsizei depth,
677 GLint border,
678 GLenum format,
679 GLenum type,
680 const gfx::Rect& cleared_rect);
682 // Adapter to call above function.
683 void SetLevelInfoFromParams(TextureRef* ref,
684 const gpu::AsyncTexImage2DParams& params) {
685 SetLevelInfo(ref, params.target, params.level, params.internal_format,
686 params.width, params.height, 1 /* depth */, params.border,
687 params.format, params.type,
688 gfx::Rect(params.width, params.height) /* cleared_rect */);
691 Texture* Produce(TextureRef* ref);
693 // Maps an existing texture into the texture manager, at a given client ID.
694 TextureRef* Consume(GLuint client_id, Texture* texture);
696 // Sets |rect| of mip as cleared.
697 void SetLevelClearedRect(TextureRef* ref,
698 GLenum target,
699 GLint level,
700 const gfx::Rect& cleared_rect);
702 // Sets a mip as cleared.
703 void SetLevelCleared(TextureRef* ref, GLenum target,
704 GLint level, bool cleared);
706 // Sets a texture parameter of a Texture
707 // Returns GL_NO_ERROR on success. Otherwise the error to generate.
708 // TODO(gman): Expand to SetParameteriv,fv
709 void SetParameteri(
710 const char* function_name, ErrorState* error_state,
711 TextureRef* ref, GLenum pname, GLint param);
712 void SetParameterf(
713 const char* function_name, ErrorState* error_state,
714 TextureRef* ref, GLenum pname, GLfloat param);
716 // Makes each of the mip levels as though they were generated.
717 // Returns false if that's not allowed for the given texture.
718 bool MarkMipmapsGenerated(TextureRef* ref);
720 // Clears any uncleared renderable levels.
721 bool ClearRenderableLevels(GLES2Decoder* decoder, TextureRef* ref);
723 // Clear a specific level.
724 bool ClearTextureLevel(
725 GLES2Decoder* decoder, TextureRef* ref, GLenum target, GLint level);
727 // Creates a new texture info.
728 TextureRef* CreateTexture(GLuint client_id, GLuint service_id);
730 // Gets the texture info for the given texture.
731 TextureRef* GetTexture(GLuint client_id) const;
733 // Removes a texture info.
734 void RemoveTexture(GLuint client_id);
736 // Gets a Texture for a given service id (note: it assumes the texture object
737 // is still mapped in this TextureManager).
738 Texture* GetTextureForServiceId(GLuint service_id) const;
740 TextureRef* GetDefaultTextureInfo(GLenum target) {
741 switch (target) {
742 case GL_TEXTURE_2D:
743 return default_textures_[kTexture2D].get();
744 case GL_TEXTURE_CUBE_MAP:
745 return default_textures_[kCubeMap].get();
746 case GL_TEXTURE_EXTERNAL_OES:
747 return default_textures_[kExternalOES].get();
748 case GL_TEXTURE_RECTANGLE_ARB:
749 return default_textures_[kRectangleARB].get();
750 default:
751 NOTREACHED();
752 return NULL;
756 bool HaveUnrenderableTextures() const {
757 return num_unrenderable_textures_ > 0;
760 bool HaveUnsafeTextures() const {
761 return num_unsafe_textures_ > 0;
764 bool HaveUnclearedMips() const {
765 return num_uncleared_mips_ > 0;
768 bool HaveImages() const {
769 return num_images_ > 0;
772 GLuint black_texture_id(GLenum target) const {
773 switch (target) {
774 case GL_SAMPLER_2D:
775 return black_texture_ids_[kTexture2D];
776 case GL_SAMPLER_CUBE:
777 return black_texture_ids_[kCubeMap];
778 case GL_SAMPLER_EXTERNAL_OES:
779 return black_texture_ids_[kExternalOES];
780 case GL_SAMPLER_2D_RECT_ARB:
781 return black_texture_ids_[kRectangleARB];
782 default:
783 NOTREACHED();
784 return 0;
788 size_t mem_represented() const {
789 return
790 memory_tracker_managed_->GetMemRepresented() +
791 memory_tracker_unmanaged_->GetMemRepresented();
794 void SetLevelImage(
795 TextureRef* ref,
796 GLenum target,
797 GLint level,
798 gfx::GLImage* image);
800 size_t GetSignatureSize() const;
802 void AddToSignature(
803 TextureRef* ref,
804 GLenum target,
805 GLint level,
806 std::string* signature) const;
808 void AddObserver(DestructionObserver* observer) {
809 destruction_observers_.push_back(observer);
812 void RemoveObserver(DestructionObserver* observer) {
813 for (unsigned int i = 0; i < destruction_observers_.size(); i++) {
814 if (destruction_observers_[i] == observer) {
815 std::swap(destruction_observers_[i], destruction_observers_.back());
816 destruction_observers_.pop_back();
817 return;
820 NOTREACHED();
823 struct DoTexImageArguments {
824 enum TexImageCommandType {
825 kTexImage2D,
826 kTexImage3D,
829 GLenum target;
830 GLint level;
831 GLenum internal_format;
832 GLsizei width;
833 GLsizei height;
834 GLsizei depth;
835 GLint border;
836 GLenum format;
837 GLenum type;
838 const void* pixels;
839 uint32 pixels_size;
840 TexImageCommandType command_type;
843 bool ValidateTexImage(
844 ContextState* state,
845 const char* function_name,
846 const DoTexImageArguments& args,
847 // Pointer to TextureRef filled in if validation successful.
848 // Presumes the pointer is valid.
849 TextureRef** texture_ref);
851 void ValidateAndDoTexImage(
852 DecoderTextureState* texture_state,
853 ContextState* state,
854 DecoderFramebufferState* framebuffer_state,
855 const char* function_name,
856 const DoTexImageArguments& args);
858 // TODO(kloveless): Make GetTexture* private once this is no longer called
859 // from gles2_cmd_decoder.
860 TextureRef* GetTextureInfoForTarget(ContextState* state, GLenum target);
861 TextureRef* GetTextureInfoForTargetUnlessDefault(
862 ContextState* state, GLenum target);
864 bool ValidateFormatAndTypeCombination(
865 ErrorState* error_state, const char* function_name,
866 GLenum format, GLenum type);
868 // Note that internal_format is only checked in relation to the format
869 // parameter, so that this function may be used to validate texSubImage2D.
870 bool ValidateTextureParameters(
871 ErrorState* error_state, const char* function_name,
872 GLenum format, GLenum type, GLenum internal_format, GLint level);
874 // base::trace_event::MemoryDumpProvider implementation.
875 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
876 base::trace_event::ProcessMemoryDump* pmd) override;
878 private:
879 friend class Texture;
880 friend class TextureRef;
882 // Helper for Initialize().
883 scoped_refptr<TextureRef> CreateDefaultAndBlackTextures(
884 GLenum target,
885 GLuint* black_texture);
887 void DoTexImage(
888 DecoderTextureState* texture_state,
889 ErrorState* error_state,
890 DecoderFramebufferState* framebuffer_state,
891 const char* function_name,
892 TextureRef* texture_ref,
893 const DoTexImageArguments& args);
895 void StartTracking(TextureRef* texture);
896 void StopTracking(TextureRef* texture);
898 void UpdateSafeToRenderFrom(int delta);
899 void UpdateUnclearedMips(int delta);
900 void UpdateCanRenderCondition(Texture::CanRenderCondition old_condition,
901 Texture::CanRenderCondition new_condition);
902 void UpdateNumImages(int delta);
903 void IncFramebufferStateChangeCount();
905 GLenum AdjustTexFormat(GLenum format) const;
907 // Helper function called by OnMemoryDump.
908 void DumpTextureRef(base::trace_event::ProcessMemoryDump* pmd,
909 TextureRef* ref);
911 MemoryTypeTracker* GetMemTracker(GLenum texture_pool);
912 scoped_ptr<MemoryTypeTracker> memory_tracker_managed_;
913 scoped_ptr<MemoryTypeTracker> memory_tracker_unmanaged_;
914 MemoryTracker* memory_tracker_;
916 scoped_refptr<FeatureInfo> feature_info_;
918 FramebufferManager* framebuffer_manager_;
920 // Info for each texture in the system.
921 typedef base::hash_map<GLuint, scoped_refptr<TextureRef> > TextureMap;
922 TextureMap textures_;
924 GLsizei max_texture_size_;
925 GLsizei max_cube_map_texture_size_;
926 GLsizei max_rectangle_texture_size_;
927 GLsizei max_3d_texture_size_;
928 GLint max_levels_;
929 GLint max_cube_map_levels_;
930 GLint max_3d_levels_;
932 const bool use_default_textures_;
934 int num_unrenderable_textures_;
935 int num_unsafe_textures_;
936 int num_uncleared_mips_;
937 int num_images_;
939 // Counts the number of Textures allocated with 'this' as its manager.
940 // Allows to check no Texture will outlive this.
941 unsigned int texture_count_;
943 bool have_context_;
945 // Black (0,0,0,1) textures for when non-renderable textures are used.
946 // NOTE: There is no corresponding Texture for these textures.
947 // TextureInfos are only for textures the client side can access.
948 GLuint black_texture_ids_[kNumDefaultTextures];
950 // The default textures for each target (texture name = 0)
951 scoped_refptr<TextureRef> default_textures_[kNumDefaultTextures];
953 std::vector<DestructionObserver*> destruction_observers_;
955 DISALLOW_COPY_AND_ASSIGN(TextureManager);
958 // This class records texture upload time when in scope.
959 class ScopedTextureUploadTimer {
960 public:
961 explicit ScopedTextureUploadTimer(DecoderTextureState* texture_state);
962 ~ScopedTextureUploadTimer();
964 private:
965 DecoderTextureState* texture_state_;
966 base::TimeTicks begin_time_;
967 DISALLOW_COPY_AND_ASSIGN(ScopedTextureUploadTimer);
970 } // namespace gles2
971 } // namespace gpu
973 #endif // GPU_COMMAND_BUFFER_SERVICE_TEXTURE_MANAGER_H_