Infobar material design refresh: bg color
[chromium-blink-merge.git] / gpu / command_buffer / service / texture_manager.h
blob479678cddb0ed39b0145df1bfba26b965e8ab57e
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/feature_info.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(const FeatureInfo::Workarounds& workarounds)
540 : tex_image_failed(false),
541 texture_upload_count(0),
542 texsubimage_faster_than_teximage(
543 workarounds.texsubimage_faster_than_teximage),
544 force_cube_map_positive_x_allocation(
545 workarounds.force_cube_map_positive_x_allocation),
546 force_cube_complete(workarounds.force_cube_complete) {}
548 // This indicates all the following texSubImage*D calls that are part of the
549 // failed texImage*D call should be ignored. The client calls have a lock
550 // around them, so it will affect only a single texImage*D + texSubImage*D
551 // group.
552 bool tex_image_failed;
554 // Command buffer stats.
555 int texture_upload_count;
556 base::TimeDelta total_texture_upload_time;
558 bool texsubimage_faster_than_teximage;
559 bool force_cube_map_positive_x_allocation;
560 bool force_cube_complete;
563 // This class keeps track of the textures and their sizes so we can do NPOT and
564 // texture complete checking.
566 // NOTE: To support shared resources an instance of this class will need to be
567 // shared by multiple GLES2Decoders.
568 class GPU_EXPORT TextureManager : public base::trace_event::MemoryDumpProvider {
569 public:
570 class GPU_EXPORT DestructionObserver {
571 public:
572 DestructionObserver();
573 virtual ~DestructionObserver();
575 // Called in ~TextureManager.
576 virtual void OnTextureManagerDestroying(TextureManager* manager) = 0;
578 // Called via ~TextureRef.
579 virtual void OnTextureRefDestroying(TextureRef* texture) = 0;
581 private:
582 DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
585 enum DefaultAndBlackTextures {
586 kTexture2D,
587 kCubeMap,
588 kExternalOES,
589 kRectangleARB,
590 kNumDefaultTextures
593 TextureManager(MemoryTracker* memory_tracker,
594 FeatureInfo* feature_info,
595 GLsizei max_texture_size,
596 GLsizei max_cube_map_texture_size,
597 GLsizei max_rectangle_texture_size,
598 GLsizei max_3d_texture_size,
599 bool use_default_textures);
600 ~TextureManager() override;
602 void set_framebuffer_manager(FramebufferManager* manager) {
603 framebuffer_manager_ = manager;
606 // Init the texture manager.
607 bool Initialize();
609 // Must call before destruction.
610 void Destroy(bool have_context);
612 // Returns the maximum number of levels.
613 GLint MaxLevelsForTarget(GLenum target) const {
614 switch (target) {
615 case GL_TEXTURE_2D:
616 return max_levels_;
617 case GL_TEXTURE_RECTANGLE_ARB:
618 case GL_TEXTURE_EXTERNAL_OES:
619 return 1;
620 case GL_TEXTURE_3D:
621 case GL_TEXTURE_2D_ARRAY:
622 return max_3d_levels_;
623 default:
624 return max_cube_map_levels_;
628 // Returns the maximum size.
629 GLsizei MaxSizeForTarget(GLenum target) const {
630 switch (target) {
631 case GL_TEXTURE_2D:
632 case GL_TEXTURE_EXTERNAL_OES:
633 return max_texture_size_;
634 case GL_TEXTURE_RECTANGLE:
635 return max_rectangle_texture_size_;
636 case GL_TEXTURE_3D:
637 case GL_TEXTURE_2D_ARRAY:
638 return max_3d_texture_size_;
639 default:
640 return max_cube_map_texture_size_;
644 // Returns the maxium number of levels a texture of the given size can have.
645 static GLsizei ComputeMipMapCount(GLenum target,
646 GLsizei width,
647 GLsizei height,
648 GLsizei depth);
650 // Checks if a dimensions are valid for a given target.
651 bool ValidForTarget(
652 GLenum target, GLint level,
653 GLsizei width, GLsizei height, GLsizei depth);
655 // True if this texture meets all the GLES2 criteria for rendering.
656 // See section 3.8.2 of the GLES2 spec.
657 bool CanRender(const TextureRef* ref) const {
658 return ref->texture()->CanRender(feature_info_.get());
661 // Returns true if mipmaps can be generated by GL.
662 bool CanGenerateMipmaps(const TextureRef* ref) const {
663 return ref->texture()->CanGenerateMipmaps(feature_info_.get());
666 // Sets the Texture's target
667 // Parameters:
668 // target: GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP
669 // GL_TEXTURE_2D_ARRAY or GL_TEXTURE_3D (for GLES3)
670 // max_levels: The maximum levels this type of target can have.
671 void SetTarget(
672 TextureRef* ref,
673 GLenum target);
675 // Set the info for a particular level in a TexureInfo.
676 void SetLevelInfo(TextureRef* ref,
677 GLenum target,
678 GLint level,
679 GLenum internal_format,
680 GLsizei width,
681 GLsizei height,
682 GLsizei depth,
683 GLint border,
684 GLenum format,
685 GLenum type,
686 const gfx::Rect& cleared_rect);
688 Texture* Produce(TextureRef* ref);
690 // Maps an existing texture into the texture manager, at a given client ID.
691 TextureRef* Consume(GLuint client_id, Texture* texture);
693 // Sets |rect| of mip as cleared.
694 void SetLevelClearedRect(TextureRef* ref,
695 GLenum target,
696 GLint level,
697 const gfx::Rect& cleared_rect);
699 // Sets a mip as cleared.
700 void SetLevelCleared(TextureRef* ref, GLenum target,
701 GLint level, bool cleared);
703 // Sets a texture parameter of a Texture
704 // Returns GL_NO_ERROR on success. Otherwise the error to generate.
705 // TODO(gman): Expand to SetParameteriv,fv
706 void SetParameteri(
707 const char* function_name, ErrorState* error_state,
708 TextureRef* ref, GLenum pname, GLint param);
709 void SetParameterf(
710 const char* function_name, ErrorState* error_state,
711 TextureRef* ref, GLenum pname, GLfloat param);
713 // Makes each of the mip levels as though they were generated.
714 // Returns false if that's not allowed for the given texture.
715 bool MarkMipmapsGenerated(TextureRef* ref);
717 // Clears any uncleared renderable levels.
718 bool ClearRenderableLevels(GLES2Decoder* decoder, TextureRef* ref);
720 // Clear a specific level.
721 bool ClearTextureLevel(
722 GLES2Decoder* decoder, TextureRef* ref, GLenum target, GLint level);
724 // Creates a new texture info.
725 TextureRef* CreateTexture(GLuint client_id, GLuint service_id);
727 // Gets the texture info for the given texture.
728 TextureRef* GetTexture(GLuint client_id) const;
730 // Removes a texture info.
731 void RemoveTexture(GLuint client_id);
733 // Gets a Texture for a given service id (note: it assumes the texture object
734 // is still mapped in this TextureManager).
735 Texture* GetTextureForServiceId(GLuint service_id) const;
737 TextureRef* GetDefaultTextureInfo(GLenum target) {
738 switch (target) {
739 case GL_TEXTURE_2D:
740 return default_textures_[kTexture2D].get();
741 case GL_TEXTURE_CUBE_MAP:
742 return default_textures_[kCubeMap].get();
743 case GL_TEXTURE_EXTERNAL_OES:
744 return default_textures_[kExternalOES].get();
745 case GL_TEXTURE_RECTANGLE_ARB:
746 return default_textures_[kRectangleARB].get();
747 default:
748 NOTREACHED();
749 return NULL;
753 bool HaveUnrenderableTextures() const {
754 return num_unrenderable_textures_ > 0;
757 bool HaveUnsafeTextures() const {
758 return num_unsafe_textures_ > 0;
761 bool HaveUnclearedMips() const {
762 return num_uncleared_mips_ > 0;
765 bool HaveImages() const {
766 return num_images_ > 0;
769 GLuint black_texture_id(GLenum target) const {
770 switch (target) {
771 case GL_SAMPLER_2D:
772 return black_texture_ids_[kTexture2D];
773 case GL_SAMPLER_CUBE:
774 return black_texture_ids_[kCubeMap];
775 case GL_SAMPLER_EXTERNAL_OES:
776 return black_texture_ids_[kExternalOES];
777 case GL_SAMPLER_2D_RECT_ARB:
778 return black_texture_ids_[kRectangleARB];
779 default:
780 NOTREACHED();
781 return 0;
785 size_t mem_represented() const {
786 return
787 memory_tracker_managed_->GetMemRepresented() +
788 memory_tracker_unmanaged_->GetMemRepresented();
791 void SetLevelImage(
792 TextureRef* ref,
793 GLenum target,
794 GLint level,
795 gfx::GLImage* image);
797 size_t GetSignatureSize() const;
799 void AddToSignature(
800 TextureRef* ref,
801 GLenum target,
802 GLint level,
803 std::string* signature) const;
805 void AddObserver(DestructionObserver* observer) {
806 destruction_observers_.push_back(observer);
809 void RemoveObserver(DestructionObserver* observer) {
810 for (unsigned int i = 0; i < destruction_observers_.size(); i++) {
811 if (destruction_observers_[i] == observer) {
812 std::swap(destruction_observers_[i], destruction_observers_.back());
813 destruction_observers_.pop_back();
814 return;
817 NOTREACHED();
820 struct DoTexImageArguments {
821 enum TexImageCommandType {
822 kTexImage2D,
823 kTexImage3D,
826 GLenum target;
827 GLint level;
828 GLenum internal_format;
829 GLsizei width;
830 GLsizei height;
831 GLsizei depth;
832 GLint border;
833 GLenum format;
834 GLenum type;
835 const void* pixels;
836 uint32 pixels_size;
837 TexImageCommandType command_type;
840 bool ValidateTexImage(
841 ContextState* state,
842 const char* function_name,
843 const DoTexImageArguments& args,
844 // Pointer to TextureRef filled in if validation successful.
845 // Presumes the pointer is valid.
846 TextureRef** texture_ref);
848 void ValidateAndDoTexImage(
849 DecoderTextureState* texture_state,
850 ContextState* state,
851 DecoderFramebufferState* framebuffer_state,
852 const char* function_name,
853 const DoTexImageArguments& args);
855 // TODO(kloveless): Make GetTexture* private once this is no longer called
856 // from gles2_cmd_decoder.
857 TextureRef* GetTextureInfoForTarget(ContextState* state, GLenum target);
858 TextureRef* GetTextureInfoForTargetUnlessDefault(
859 ContextState* state, GLenum target);
861 bool ValidateFormatAndTypeCombination(
862 ErrorState* error_state, const char* function_name,
863 GLenum format, GLenum type);
865 // Note that internal_format is only checked in relation to the format
866 // parameter, so that this function may be used to validate texSubImage2D.
867 bool ValidateTextureParameters(
868 ErrorState* error_state, const char* function_name,
869 GLenum format, GLenum type, GLenum internal_format, GLint level);
871 // base::trace_event::MemoryDumpProvider implementation.
872 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
873 base::trace_event::ProcessMemoryDump* pmd) override;
875 private:
876 friend class Texture;
877 friend class TextureRef;
879 // Helper for Initialize().
880 scoped_refptr<TextureRef> CreateDefaultAndBlackTextures(
881 GLenum target,
882 GLuint* black_texture);
884 void DoTexImage(
885 DecoderTextureState* texture_state,
886 ErrorState* error_state,
887 DecoderFramebufferState* framebuffer_state,
888 const char* function_name,
889 TextureRef* texture_ref,
890 const DoTexImageArguments& args);
892 void StartTracking(TextureRef* texture);
893 void StopTracking(TextureRef* texture);
895 void UpdateSafeToRenderFrom(int delta);
896 void UpdateUnclearedMips(int delta);
897 void UpdateCanRenderCondition(Texture::CanRenderCondition old_condition,
898 Texture::CanRenderCondition new_condition);
899 void UpdateNumImages(int delta);
900 void IncFramebufferStateChangeCount();
902 GLenum AdjustTexFormat(GLenum format) const;
904 // Helper function called by OnMemoryDump.
905 void DumpTextureRef(base::trace_event::ProcessMemoryDump* pmd,
906 TextureRef* ref);
908 MemoryTypeTracker* GetMemTracker(GLenum texture_pool);
909 scoped_ptr<MemoryTypeTracker> memory_tracker_managed_;
910 scoped_ptr<MemoryTypeTracker> memory_tracker_unmanaged_;
911 MemoryTracker* memory_tracker_;
913 scoped_refptr<FeatureInfo> feature_info_;
915 FramebufferManager* framebuffer_manager_;
917 // Info for each texture in the system.
918 typedef base::hash_map<GLuint, scoped_refptr<TextureRef> > TextureMap;
919 TextureMap textures_;
921 GLsizei max_texture_size_;
922 GLsizei max_cube_map_texture_size_;
923 GLsizei max_rectangle_texture_size_;
924 GLsizei max_3d_texture_size_;
925 GLint max_levels_;
926 GLint max_cube_map_levels_;
927 GLint max_3d_levels_;
929 const bool use_default_textures_;
931 int num_unrenderable_textures_;
932 int num_unsafe_textures_;
933 int num_uncleared_mips_;
934 int num_images_;
936 // Counts the number of Textures allocated with 'this' as its manager.
937 // Allows to check no Texture will outlive this.
938 unsigned int texture_count_;
940 bool have_context_;
942 // Black (0,0,0,1) textures for when non-renderable textures are used.
943 // NOTE: There is no corresponding Texture for these textures.
944 // TextureInfos are only for textures the client side can access.
945 GLuint black_texture_ids_[kNumDefaultTextures];
947 // The default textures for each target (texture name = 0)
948 scoped_refptr<TextureRef> default_textures_[kNumDefaultTextures];
950 std::vector<DestructionObserver*> destruction_observers_;
952 DISALLOW_COPY_AND_ASSIGN(TextureManager);
955 // This class records texture upload time when in scope.
956 class ScopedTextureUploadTimer {
957 public:
958 explicit ScopedTextureUploadTimer(DecoderTextureState* texture_state);
959 ~ScopedTextureUploadTimer();
961 private:
962 DecoderTextureState* texture_state_;
963 base::TimeTicks begin_time_;
964 DISALLOW_COPY_AND_ASSIGN(ScopedTextureUploadTimer);
967 } // namespace gles2
968 } // namespace gpu
970 #endif // GPU_COMMAND_BUFFER_SERVICE_TEXTURE_MANAGER_H_