Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / gpu / command_buffer / service / texture_manager.h
blob56d96ba5622b4a0d4fe892c0e7370ca999d630e4
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.
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) const;
160 bool IsValid() const {
161 return !!target();
164 bool IsAttachedToFramebuffer() const {
165 return framebuffer_attachment_count_ != 0;
168 void AttachToFramebuffer() {
169 ++framebuffer_attachment_count_;
172 void DetachFromFramebuffer() {
173 DCHECK_GT(framebuffer_attachment_count_, 0);
174 --framebuffer_attachment_count_;
177 void SetImmutable(bool immutable) {
178 immutable_ = immutable;
181 bool IsImmutable() const {
182 return immutable_;
185 // Get the cleared rectangle for a particular level. Returns an empty
186 // rectangle if level does not exist.
187 gfx::Rect GetLevelClearedRect(GLenum target, GLint level) const;
189 // Whether a particular level/face is cleared.
190 bool IsLevelCleared(GLenum target, GLint level) const;
192 // Whether the texture has been defined
193 bool IsDefined() const {
194 return estimated_size() > 0;
197 // Initialize TEXTURE_MAX_ANISOTROPY to 1 if we haven't done so yet.
198 void InitTextureMaxAnisotropyIfNeeded(GLenum target);
200 void OnWillModifyPixels();
201 void OnDidModifyPixels();
203 void DumpLevelMemory(base::trace_event::ProcessMemoryDump* pmd,
204 uint64_t client_tracing_id,
205 const std::string& dump_name) const;
207 private:
208 friend class MailboxManagerImpl;
209 friend class MailboxManagerSync;
210 friend class MailboxManagerTest;
211 friend class TextureDefinition;
212 friend class TextureManager;
213 friend class TextureRef;
214 friend class TextureTestHelper;
216 ~Texture();
217 void AddTextureRef(TextureRef* ref);
218 void RemoveTextureRef(TextureRef* ref, bool have_context);
219 MemoryTypeTracker* GetMemTracker();
221 // Condition on which this texture is renderable. Can be ONLY_IF_NPOT if it
222 // depends on context support for non-power-of-two textures (i.e. will be
223 // renderable if NPOT support is in the context, otherwise not, e.g. texture
224 // with a NPOT level). ALWAYS means it doesn't depend on context features
225 // (e.g. complete POT), NEVER means it's not renderable regardless (e.g.
226 // incomplete).
227 enum CanRenderCondition {
228 CAN_RENDER_ALWAYS,
229 CAN_RENDER_NEVER,
230 CAN_RENDER_ONLY_IF_NPOT
233 struct LevelInfo {
234 LevelInfo();
235 LevelInfo(const LevelInfo& rhs);
236 ~LevelInfo();
238 gfx::Rect cleared_rect;
239 GLenum target;
240 GLint level;
241 GLenum internal_format;
242 GLsizei width;
243 GLsizei height;
244 GLsizei depth;
245 GLint border;
246 GLenum format;
247 GLenum type;
248 scoped_refptr<gfx::GLImage> image;
249 uint32 estimated_size;
252 struct FaceInfo {
253 FaceInfo();
254 ~FaceInfo();
256 GLsizei num_mip_levels;
257 std::vector<LevelInfo> level_infos;
260 // Set the info for a particular level.
261 void SetLevelInfo(const FeatureInfo* feature_info,
262 GLenum target,
263 GLint level,
264 GLenum internal_format,
265 GLsizei width,
266 GLsizei height,
267 GLsizei depth,
268 GLint border,
269 GLenum format,
270 GLenum type,
271 const gfx::Rect& cleared_rect);
273 // In GLES2 "texture complete" means it has all required mips for filtering
274 // down to a 1x1 pixel texture, they are in the correct order, they are all
275 // the same format.
276 bool texture_complete() const {
277 return texture_complete_;
280 // In GLES2 "cube complete" means all 6 faces level 0 are defined, all the
281 // same format, all the same dimensions and all width = height.
282 bool cube_complete() const {
283 return cube_complete_;
286 // Whether or not this texture is a non-power-of-two texture.
287 bool npot() const {
288 return npot_;
291 // Marks a |rect| of a particular level as cleared.
292 void SetLevelClearedRect(GLenum target,
293 GLint level,
294 const gfx::Rect& cleared_rect);
296 // Marks a particular level as cleared or uncleared.
297 void SetLevelCleared(GLenum target, GLint level, bool cleared);
299 // Updates the cleared flag for this texture by inspecting all the mips.
300 void UpdateCleared();
302 // Clears any renderable uncleared levels.
303 // Returns false if a GL error was generated.
304 bool ClearRenderableLevels(GLES2Decoder* decoder);
306 // Clears the level.
307 // Returns false if a GL error was generated.
308 bool ClearLevel(GLES2Decoder* decoder, GLenum target, GLint level);
310 // Sets a texture parameter.
311 // TODO(gman): Expand to SetParameteriv,fv
312 // Returns GL_NO_ERROR on success. Otherwise the error to generate.
313 GLenum SetParameteri(
314 const FeatureInfo* feature_info, GLenum pname, GLint param);
315 GLenum SetParameterf(
316 const FeatureInfo* feature_info, GLenum pname, GLfloat param);
318 // Makes each of the mip levels as though they were generated.
319 bool MarkMipmapsGenerated(const FeatureInfo* feature_info);
321 bool NeedsMips() const {
322 return min_filter_ != GL_NEAREST && min_filter_ != GL_LINEAR;
325 // True if this texture meets all the GLES2 criteria for rendering.
326 // See section 3.8.2 of the GLES2 spec.
327 bool CanRender(const FeatureInfo* feature_info) const;
329 // Returns true if mipmaps can be generated by GL.
330 bool CanGenerateMipmaps(const FeatureInfo* feature_info) const;
332 // Returns true if any of the texture dimensions are not a power of two.
333 static bool TextureIsNPOT(GLsizei width, GLsizei height, GLsizei depth);
335 // Returns true if texture face is complete relative to the first face.
336 static bool TextureFaceComplete(const Texture::LevelInfo& first_face,
337 size_t face_index,
338 GLenum target,
339 GLenum internal_format,
340 GLsizei width,
341 GLsizei height,
342 GLsizei depth,
343 GLenum format,
344 GLenum type);
346 // Returns true if texture mip level is complete relative to first level.
347 static bool TextureMipComplete(const Texture::LevelInfo& level0_face,
348 GLenum target,
349 GLint level,
350 GLenum internal_format,
351 GLsizei width,
352 GLsizei height,
353 GLsizei depth,
354 GLenum format,
355 GLenum type);
357 // Sets the Texture's target
358 // Parameters:
359 // target: GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP or
360 // GL_TEXTURE_EXTERNAL_OES or GL_TEXTURE_RECTANGLE_ARB
361 // GL_TEXTURE_2D_ARRAY or GL_TEXTURE_3D (for GLES3)
362 // max_levels: The maximum levels this type of target can have.
363 void SetTarget(
364 const FeatureInfo* feature_info, GLenum target, GLint max_levels);
366 // Update info about this texture.
367 void Update(const FeatureInfo* feature_info);
369 // Set the image for a particular level.
370 void SetLevelImage(
371 const FeatureInfo* feature_info,
372 GLenum target,
373 GLint level,
374 gfx::GLImage* image);
376 // Appends a signature for the given level.
377 void AddToSignature(
378 const FeatureInfo* feature_info,
379 GLenum target, GLint level, std::string* signature) const;
381 void SetMailboxManager(MailboxManager* mailbox_manager);
383 // Updates the unsafe textures count in all the managers referencing this
384 // texture.
385 void UpdateSafeToRenderFrom(bool cleared);
387 // Updates the uncleared mip count in all the managers referencing this
388 // texture.
389 void UpdateMipCleared(LevelInfo* info,
390 GLsizei width,
391 GLsizei height,
392 const gfx::Rect& cleared_rect);
394 // Computes the CanRenderCondition flag.
395 CanRenderCondition GetCanRenderCondition() const;
397 // Updates the unrenderable texture count in all the managers referencing this
398 // texture.
399 void UpdateCanRenderCondition();
401 // Updates the images count in all the managers referencing this
402 // texture.
403 void UpdateHasImages();
405 // Increment the framebuffer state change count in all the managers
406 // referencing this texture.
407 void IncAllFramebufferStateChangeCount();
409 MailboxManager* mailbox_manager_;
411 // Info about each face and level of texture.
412 std::vector<FaceInfo> face_infos_;
414 // The texture refs that point to this Texture.
415 typedef std::set<TextureRef*> RefSet;
416 RefSet refs_;
418 // The single TextureRef that accounts for memory for this texture. Must be
419 // one of refs_.
420 TextureRef* memory_tracking_ref_;
422 // The id of the texure
423 GLuint service_id_;
425 // Whether all renderable mips of this texture have been cleared.
426 bool cleared_;
428 int num_uncleared_mips_;
429 int num_npot_faces_;
431 // The target. 0 if unset, otherwise GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP.
432 // Or GL_TEXTURE_2D_ARRAY or GL_TEXTURE_3D (for GLES3).
433 GLenum target_;
435 // Texture parameters.
436 GLenum min_filter_;
437 GLenum mag_filter_;
438 GLenum wrap_r_;
439 GLenum wrap_s_;
440 GLenum wrap_t_;
441 GLenum usage_;
442 GLenum pool_;
443 GLenum compare_func_;
444 GLenum compare_mode_;
445 GLfloat max_lod_;
446 GLfloat min_lod_;
447 GLint base_level_;
448 GLint max_level_;
450 // The maximum level that has been set.
451 GLint max_level_set_;
453 // Whether or not this texture is "texture complete"
454 bool texture_complete_;
456 // Whether mip levels have changed and should be reverified.
457 bool texture_mips_dirty_;
458 bool texture_mips_complete_;
460 // Whether or not this texture is "cube complete"
461 bool cube_complete_;
463 // Whether any level 0 faces have changed and should be reverified.
464 bool texture_level0_dirty_;
465 bool texture_level0_complete_;
467 // Whether or not this texture is non-power-of-two
468 bool npot_;
470 // Whether this texture has ever been bound.
471 bool has_been_bound_;
473 // The number of framebuffers this texture is attached to.
474 int framebuffer_attachment_count_;
476 // Whether the texture is immutable and no further changes to the format
477 // or dimensions of the texture object can be made.
478 bool immutable_;
480 // Whether or not this texture has images.
481 bool has_images_;
483 // Size in bytes this texture is assumed to take in memory.
484 uint32 estimated_size_;
486 // Cache of the computed CanRenderCondition flag.
487 CanRenderCondition can_render_condition_;
489 // Whether we have initialized TEXTURE_MAX_ANISOTROPY to 1.
490 bool texture_max_anisotropy_initialized_;
492 DISALLOW_COPY_AND_ASSIGN(Texture);
495 // This class represents a texture in a client context group. It's mostly 1:1
496 // with a client id, though it can outlive the client id if it's still bound to
497 // a FBO or another context when destroyed.
498 // Multiple TextureRef can point to the same texture with cross-context sharing.
499 class GPU_EXPORT TextureRef : public base::RefCounted<TextureRef> {
500 public:
501 TextureRef(TextureManager* manager, GLuint client_id, Texture* texture);
502 static scoped_refptr<TextureRef> Create(TextureManager* manager,
503 GLuint client_id,
504 GLuint service_id);
506 void AddObserver() { num_observers_++; }
507 void RemoveObserver() { num_observers_--; }
509 const Texture* texture() const { return texture_; }
510 Texture* texture() { return texture_; }
511 GLuint client_id() const { return client_id_; }
512 GLuint service_id() const { return texture_->service_id(); }
513 GLint num_observers() const { return num_observers_; }
515 private:
516 friend class base::RefCounted<TextureRef>;
517 friend class Texture;
518 friend class TextureManager;
520 ~TextureRef();
521 const TextureManager* manager() const { return manager_; }
522 TextureManager* manager() { return manager_; }
523 void reset_client_id() { client_id_ = 0; }
525 TextureManager* manager_;
526 Texture* texture_;
527 GLuint client_id_;
528 GLint num_observers_;
530 DISALLOW_COPY_AND_ASSIGN(TextureRef);
533 // Holds data that is per gles2_cmd_decoder, but is related to to the
534 // TextureManager.
535 struct DecoderTextureState {
536 // total_texture_upload_time automatically initialized to 0 in default
537 // constructor.
538 explicit DecoderTextureState(const FeatureInfo::Workarounds& workarounds)
539 : tex_image_failed(false),
540 texture_upload_count(0),
541 texsubimage_faster_than_teximage(
542 workarounds.texsubimage_faster_than_teximage),
543 force_cube_map_positive_x_allocation(
544 workarounds.force_cube_map_positive_x_allocation),
545 force_cube_complete(workarounds.force_cube_complete) {}
547 // This indicates all the following texSubImage*D calls that are part of the
548 // failed texImage*D call should be ignored. The client calls have a lock
549 // around them, so it will affect only a single texImage*D + texSubImage*D
550 // group.
551 bool tex_image_failed;
553 // Command buffer stats.
554 int texture_upload_count;
555 base::TimeDelta total_texture_upload_time;
557 bool texsubimage_faster_than_teximage;
558 bool force_cube_map_positive_x_allocation;
559 bool force_cube_complete;
562 // This class keeps track of the textures and their sizes so we can do NPOT and
563 // texture complete checking.
565 // NOTE: To support shared resources an instance of this class will need to be
566 // shared by multiple GLES2Decoders.
567 class GPU_EXPORT TextureManager : public base::trace_event::MemoryDumpProvider {
568 public:
569 class GPU_EXPORT DestructionObserver {
570 public:
571 DestructionObserver();
572 virtual ~DestructionObserver();
574 // Called in ~TextureManager.
575 virtual void OnTextureManagerDestroying(TextureManager* manager) = 0;
577 // Called via ~TextureRef.
578 virtual void OnTextureRefDestroying(TextureRef* texture) = 0;
580 private:
581 DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
584 enum DefaultAndBlackTextures {
585 kTexture2D,
586 kTexture3D,
587 kTexture2DArray,
588 kCubeMap,
589 kExternalOES,
590 kRectangleARB,
591 kNumDefaultTextures
594 TextureManager(MemoryTracker* memory_tracker,
595 FeatureInfo* feature_info,
596 GLsizei max_texture_size,
597 GLsizei max_cube_map_texture_size,
598 GLsizei max_rectangle_texture_size,
599 GLsizei max_3d_texture_size,
600 bool use_default_textures);
601 ~TextureManager() override;
603 void set_framebuffer_manager(FramebufferManager* manager) {
604 framebuffer_manager_ = manager;
607 // Init the texture manager.
608 bool Initialize();
610 // Must call before destruction.
611 void Destroy(bool have_context);
613 // Returns the maximum number of levels.
614 GLint MaxLevelsForTarget(GLenum target) const {
615 switch (target) {
616 case GL_TEXTURE_2D:
617 return max_levels_;
618 case GL_TEXTURE_RECTANGLE_ARB:
619 case GL_TEXTURE_EXTERNAL_OES:
620 return 1;
621 case GL_TEXTURE_3D:
622 case GL_TEXTURE_2D_ARRAY:
623 return max_3d_levels_;
624 default:
625 return max_cube_map_levels_;
629 // Returns the maximum size.
630 GLsizei MaxSizeForTarget(GLenum target) const {
631 switch (target) {
632 case GL_TEXTURE_2D:
633 case GL_TEXTURE_EXTERNAL_OES:
634 return max_texture_size_;
635 case GL_TEXTURE_RECTANGLE:
636 return max_rectangle_texture_size_;
637 case GL_TEXTURE_3D:
638 case GL_TEXTURE_2D_ARRAY:
639 return max_3d_texture_size_;
640 default:
641 return max_cube_map_texture_size_;
645 // Returns the maxium number of levels a texture of the given size can have.
646 static GLsizei ComputeMipMapCount(GLenum target,
647 GLsizei width,
648 GLsizei height,
649 GLsizei depth);
651 // Checks if a dimensions are valid for a given target.
652 bool ValidForTarget(
653 GLenum target, GLint level,
654 GLsizei width, GLsizei height, GLsizei depth);
656 // True if this texture meets all the GLES2 criteria for rendering.
657 // See section 3.8.2 of the GLES2 spec.
658 bool CanRender(const TextureRef* ref) const {
659 return ref->texture()->CanRender(feature_info_.get());
662 // Returns true if mipmaps can be generated by GL.
663 bool CanGenerateMipmaps(const TextureRef* ref) const {
664 return ref->texture()->CanGenerateMipmaps(feature_info_.get());
667 // Sets the Texture's target
668 // Parameters:
669 // target: GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP
670 // GL_TEXTURE_2D_ARRAY or GL_TEXTURE_3D (for GLES3)
671 // max_levels: The maximum levels this type of target can have.
672 void SetTarget(
673 TextureRef* ref,
674 GLenum target);
676 // Set the info for a particular level in a TexureInfo.
677 void SetLevelInfo(TextureRef* ref,
678 GLenum target,
679 GLint level,
680 GLenum internal_format,
681 GLsizei width,
682 GLsizei height,
683 GLsizei depth,
684 GLint border,
685 GLenum format,
686 GLenum type,
687 const gfx::Rect& cleared_rect);
689 Texture* Produce(TextureRef* ref);
691 // Maps an existing texture into the texture manager, at a given client ID.
692 TextureRef* Consume(GLuint client_id, Texture* texture);
694 // Sets |rect| of mip as cleared.
695 void SetLevelClearedRect(TextureRef* ref,
696 GLenum target,
697 GLint level,
698 const gfx::Rect& cleared_rect);
700 // Sets a mip as cleared.
701 void SetLevelCleared(TextureRef* ref, GLenum target,
702 GLint level, bool cleared);
704 // Sets a texture parameter of a Texture
705 // Returns GL_NO_ERROR on success. Otherwise the error to generate.
706 // TODO(gman): Expand to SetParameteriv,fv
707 void SetParameteri(
708 const char* function_name, ErrorState* error_state,
709 TextureRef* ref, GLenum pname, GLint param);
710 void SetParameterf(
711 const char* function_name, ErrorState* error_state,
712 TextureRef* ref, GLenum pname, GLfloat param);
714 // Makes each of the mip levels as though they were generated.
715 // Returns false if that's not allowed for the given texture.
716 bool MarkMipmapsGenerated(TextureRef* ref);
718 // Clears any uncleared renderable levels.
719 bool ClearRenderableLevels(GLES2Decoder* decoder, TextureRef* ref);
721 // Clear a specific level.
722 bool ClearTextureLevel(
723 GLES2Decoder* decoder, TextureRef* ref, GLenum target, GLint level);
725 // Creates a new texture info.
726 TextureRef* CreateTexture(GLuint client_id, GLuint service_id);
728 // Gets the texture info for the given texture.
729 TextureRef* GetTexture(GLuint client_id) const;
731 // Removes a texture info.
732 void RemoveTexture(GLuint client_id);
734 // Gets a Texture for a given service id (note: it assumes the texture object
735 // is still mapped in this TextureManager).
736 Texture* GetTextureForServiceId(GLuint service_id) const;
738 TextureRef* GetDefaultTextureInfo(GLenum target) {
739 switch (target) {
740 case GL_TEXTURE_2D:
741 return default_textures_[kTexture2D].get();
742 case GL_TEXTURE_3D:
743 return default_textures_[kTexture3D].get();
744 case GL_TEXTURE_2D_ARRAY:
745 return default_textures_[kTexture2DArray].get();
746 case GL_TEXTURE_CUBE_MAP:
747 return default_textures_[kCubeMap].get();
748 case GL_TEXTURE_EXTERNAL_OES:
749 return default_textures_[kExternalOES].get();
750 case GL_TEXTURE_RECTANGLE_ARB:
751 return default_textures_[kRectangleARB].get();
752 default:
753 NOTREACHED();
754 return NULL;
758 bool HaveUnrenderableTextures() const {
759 return num_unrenderable_textures_ > 0;
762 bool HaveUnsafeTextures() const {
763 return num_unsafe_textures_ > 0;
766 bool HaveUnclearedMips() const {
767 return num_uncleared_mips_ > 0;
770 bool HaveImages() const {
771 return num_images_ > 0;
774 GLuint black_texture_id(GLenum target) const {
775 switch (target) {
776 case GL_SAMPLER_2D:
777 return black_texture_ids_[kTexture2D];
778 case GL_SAMPLER_3D:
779 return black_texture_ids_[kTexture3D];
780 case GL_SAMPLER_2D_ARRAY:
781 return black_texture_ids_[kTexture2DArray];
782 case GL_SAMPLER_CUBE:
783 return black_texture_ids_[kCubeMap];
784 case GL_SAMPLER_EXTERNAL_OES:
785 return black_texture_ids_[kExternalOES];
786 case GL_SAMPLER_2D_RECT_ARB:
787 return black_texture_ids_[kRectangleARB];
788 default:
789 NOTREACHED();
790 return 0;
794 size_t mem_represented() const {
795 return
796 memory_tracker_managed_->GetMemRepresented() +
797 memory_tracker_unmanaged_->GetMemRepresented();
800 void SetLevelImage(
801 TextureRef* ref,
802 GLenum target,
803 GLint level,
804 gfx::GLImage* image);
806 size_t GetSignatureSize() const;
808 void AddToSignature(
809 TextureRef* ref,
810 GLenum target,
811 GLint level,
812 std::string* signature) const;
814 void AddObserver(DestructionObserver* observer) {
815 destruction_observers_.push_back(observer);
818 void RemoveObserver(DestructionObserver* observer) {
819 for (unsigned int i = 0; i < destruction_observers_.size(); i++) {
820 if (destruction_observers_[i] == observer) {
821 std::swap(destruction_observers_[i], destruction_observers_.back());
822 destruction_observers_.pop_back();
823 return;
826 NOTREACHED();
829 struct DoTexImageArguments {
830 enum TexImageCommandType {
831 kTexImage2D,
832 kTexImage3D,
835 GLenum target;
836 GLint level;
837 GLenum internal_format;
838 GLsizei width;
839 GLsizei height;
840 GLsizei depth;
841 GLint border;
842 GLenum format;
843 GLenum type;
844 const void* pixels;
845 uint32 pixels_size;
846 TexImageCommandType command_type;
849 bool ValidateTexImage(
850 ContextState* state,
851 const char* function_name,
852 const DoTexImageArguments& args,
853 // Pointer to TextureRef filled in if validation successful.
854 // Presumes the pointer is valid.
855 TextureRef** texture_ref);
857 void ValidateAndDoTexImage(
858 DecoderTextureState* texture_state,
859 ContextState* state,
860 DecoderFramebufferState* framebuffer_state,
861 const char* function_name,
862 const DoTexImageArguments& args);
864 // TODO(kloveless): Make GetTexture* private once this is no longer called
865 // from gles2_cmd_decoder.
866 TextureRef* GetTextureInfoForTarget(ContextState* state, GLenum target);
867 TextureRef* GetTextureInfoForTargetUnlessDefault(
868 ContextState* state, GLenum target);
870 bool ValidateFormatAndTypeCombination(
871 ErrorState* error_state, const char* function_name,
872 GLenum format, GLenum type);
874 // Note that internal_format is only checked in relation to the format
875 // parameter, so that this function may be used to validate texSubImage2D.
876 bool ValidateTextureParameters(
877 ErrorState* error_state, const char* function_name,
878 GLenum format, GLenum type, GLenum internal_format, GLint level);
880 // base::trace_event::MemoryDumpProvider implementation.
881 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
882 base::trace_event::ProcessMemoryDump* pmd) override;
884 private:
885 friend class Texture;
886 friend class TextureRef;
888 // Helper for Initialize().
889 scoped_refptr<TextureRef> CreateDefaultAndBlackTextures(
890 GLenum target,
891 GLuint* black_texture);
893 void DoTexImage(
894 DecoderTextureState* texture_state,
895 ErrorState* error_state,
896 DecoderFramebufferState* framebuffer_state,
897 const char* function_name,
898 TextureRef* texture_ref,
899 const DoTexImageArguments& args);
901 void StartTracking(TextureRef* texture);
902 void StopTracking(TextureRef* texture);
904 void UpdateSafeToRenderFrom(int delta);
905 void UpdateUnclearedMips(int delta);
906 void UpdateCanRenderCondition(Texture::CanRenderCondition old_condition,
907 Texture::CanRenderCondition new_condition);
908 void UpdateNumImages(int delta);
909 void IncFramebufferStateChangeCount();
911 GLenum AdjustTexFormat(GLenum format) const;
913 // Helper function called by OnMemoryDump.
914 void DumpTextureRef(base::trace_event::ProcessMemoryDump* pmd,
915 TextureRef* ref);
917 MemoryTypeTracker* GetMemTracker(GLenum texture_pool);
918 scoped_ptr<MemoryTypeTracker> memory_tracker_managed_;
919 scoped_ptr<MemoryTypeTracker> memory_tracker_unmanaged_;
920 MemoryTracker* memory_tracker_;
922 scoped_refptr<FeatureInfo> feature_info_;
924 FramebufferManager* framebuffer_manager_;
926 // Info for each texture in the system.
927 typedef base::hash_map<GLuint, scoped_refptr<TextureRef> > TextureMap;
928 TextureMap textures_;
930 GLsizei max_texture_size_;
931 GLsizei max_cube_map_texture_size_;
932 GLsizei max_rectangle_texture_size_;
933 GLsizei max_3d_texture_size_;
934 GLint max_levels_;
935 GLint max_cube_map_levels_;
936 GLint max_3d_levels_;
938 const bool use_default_textures_;
940 int num_unrenderable_textures_;
941 int num_unsafe_textures_;
942 int num_uncleared_mips_;
943 int num_images_;
945 // Counts the number of Textures allocated with 'this' as its manager.
946 // Allows to check no Texture will outlive this.
947 unsigned int texture_count_;
949 bool have_context_;
951 // Black (0,0,0,1) textures for when non-renderable textures are used.
952 // NOTE: There is no corresponding Texture for these textures.
953 // TextureInfos are only for textures the client side can access.
954 GLuint black_texture_ids_[kNumDefaultTextures];
956 // The default textures for each target (texture name = 0)
957 scoped_refptr<TextureRef> default_textures_[kNumDefaultTextures];
959 std::vector<DestructionObserver*> destruction_observers_;
961 DISALLOW_COPY_AND_ASSIGN(TextureManager);
964 // This class records texture upload time when in scope.
965 class ScopedTextureUploadTimer {
966 public:
967 explicit ScopedTextureUploadTimer(DecoderTextureState* texture_state);
968 ~ScopedTextureUploadTimer();
970 private:
971 DecoderTextureState* texture_state_;
972 base::TimeTicks begin_time_;
973 DISALLOW_COPY_AND_ASSIGN(ScopedTextureUploadTimer);
976 } // namespace gles2
977 } // namespace gpu
979 #endif // GPU_COMMAND_BUFFER_SERVICE_TEXTURE_MANAGER_H_