• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef GPU_COMMAND_BUFFER_SERVICE_TEXTURE_MANAGER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_TEXTURE_MANAGER_H_
7 
8 #include <list>
9 #include <set>
10 #include <string>
11 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/logging.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/observer_list.h"
17 #include "gpu/command_buffer/service/async_pixel_transfer_delegate.h"
18 #include "gpu/command_buffer/service/gl_utils.h"
19 #include "gpu/command_buffer/service/memory_tracking.h"
20 #include "gpu/gpu_export.h"
21 #include "ui/gl/gl_image.h"
22 
23 namespace gpu {
24 
25 class StreamTextureManager;
26 
27 namespace gles2 {
28 
29 class GLES2Decoder;
30 struct ContextState;
31 struct DecoderFramebufferState;
32 class Display;
33 class ErrorState;
34 class FeatureInfo;
35 class FramebufferManager;
36 class MailboxManager;
37 class TextureManager;
38 class TextureRef;
39 
40 // Info about Textures currently in the system.
41 // This class wraps a real GL texture, keeping track of its meta-data. It is
42 // jointly owned by possibly multiple TextureRef.
43 class GPU_EXPORT Texture {
44  public:
45   explicit Texture(GLuint service_id);
46 
min_filter()47   GLenum min_filter() const {
48     return min_filter_;
49   }
50 
mag_filter()51   GLenum mag_filter() const {
52     return mag_filter_;
53   }
54 
wrap_s()55   GLenum wrap_s() const {
56     return wrap_s_;
57   }
58 
wrap_t()59   GLenum wrap_t() const {
60     return wrap_t_;
61   }
62 
usage()63   GLenum usage() const {
64     return usage_;
65   }
66 
pool()67   GLenum pool() const {
68     return pool_;
69   }
70 
num_uncleared_mips()71   int num_uncleared_mips() const {
72     return num_uncleared_mips_;
73   }
74 
estimated_size()75   uint32 estimated_size() const {
76     return estimated_size_;
77   }
78 
CanRenderTo()79   bool CanRenderTo() const {
80     return !stream_texture_ && target_ != GL_TEXTURE_EXTERNAL_OES;
81   }
82 
83   // The service side OpenGL id of the texture.
service_id()84   GLuint service_id() const {
85     return service_id_;
86   }
87 
SetServiceId(GLuint service_id)88   void SetServiceId(GLuint service_id) {
89     DCHECK(service_id);
90     service_id_ = service_id;
91   }
92 
93   // Returns the target this texure was first bound to or 0 if it has not
94   // been bound. Once a texture is bound to a specific target it can never be
95   // bound to a different target.
target()96   GLenum target() const {
97     return target_;
98   }
99 
SafeToRenderFrom()100   bool SafeToRenderFrom() const {
101     return cleared_;
102   }
103 
104   // Get the width and height for a particular level. Returns false if level
105   // does not exist.
106   bool GetLevelSize(
107       GLint target, GLint level, GLsizei* width, GLsizei* height) const;
108 
109   // Get the type of a level. Returns false if level does not exist.
110   bool GetLevelType(
111       GLint target, GLint level, GLenum* type, GLenum* internal_format) const;
112 
113   // Get the image bound to a particular level. Returns NULL if level
114   // does not exist.
115   gfx::GLImage* GetLevelImage(GLint target, GLint level) const;
116 
HasImages()117   bool HasImages() const {
118     return has_images_;
119   }
120 
121   // Returns true of the given dimensions are inside the dimensions of the
122   // level and if the format and type match the level.
123   bool ValidForTexture(
124       GLint target,
125       GLint level,
126       GLint xoffset,
127       GLint yoffset,
128       GLsizei width,
129       GLsizei height,
130       GLenum format,
131       GLenum type) const;
132 
IsValid()133   bool IsValid() const {
134     return !!target();
135   }
136 
IsAttachedToFramebuffer()137   bool IsAttachedToFramebuffer() const {
138     return framebuffer_attachment_count_ != 0;
139   }
140 
AttachToFramebuffer()141   void AttachToFramebuffer() {
142     ++framebuffer_attachment_count_;
143   }
144 
DetachFromFramebuffer()145   void DetachFromFramebuffer() {
146     DCHECK_GT(framebuffer_attachment_count_, 0);
147     --framebuffer_attachment_count_;
148   }
149 
IsStreamTexture()150   bool IsStreamTexture() const {
151     return stream_texture_;
152   }
153 
SetImmutable(bool immutable)154   void SetImmutable(bool immutable) {
155     immutable_ = immutable;
156   }
157 
IsImmutable()158   bool IsImmutable() const {
159     return immutable_;
160   }
161 
162   // Whether a particular level/face is cleared.
163   bool IsLevelCleared(GLenum target, GLint level) const;
164 
165   // Whether the texture has been defined
IsDefined()166   bool IsDefined() const {
167     return estimated_size() > 0;
168   }
169 
170   void OnWillModifyPixels();
171   void OnDidModifyPixels();
172 
173  private:
174   friend class MailboxManager;
175   friend class MailboxManagerTest;
176   friend class TextureDefinition;
177   friend class TextureManager;
178   friend class TextureRef;
179   friend class TextureTestHelper;
180 
181   ~Texture();
182   void AddTextureRef(TextureRef* ref);
183   void RemoveTextureRef(TextureRef* ref, bool have_context);
184   MemoryTypeTracker* GetMemTracker();
185 
186   // Condition on which this texture is renderable. Can be ONLY_IF_NPOT if it
187   // depends on context support for non-power-of-two textures (i.e. will be
188   // renderable if NPOT support is in the context, otherwise not, e.g. texture
189   // with a NPOT level). ALWAYS means it doesn't depend on context features
190   // (e.g. complete POT), NEVER means it's not renderable regardless (e.g.
191   // incomplete).
192   enum CanRenderCondition {
193     CAN_RENDER_ALWAYS,
194     CAN_RENDER_NEVER,
195     CAN_RENDER_ONLY_IF_NPOT
196   };
197 
198   struct LevelInfo {
199     LevelInfo();
200     LevelInfo(const LevelInfo& rhs);
201     ~LevelInfo();
202 
203     bool cleared;
204     GLenum target;
205     GLint level;
206     GLenum internal_format;
207     GLsizei width;
208     GLsizei height;
209     GLsizei depth;
210     GLint border;
211     GLenum format;
212     GLenum type;
213     scoped_refptr<gfx::GLImage> image;
214     uint32 estimated_size;
215   };
216 
217   // Set the info for a particular level.
218   void SetLevelInfo(
219       const FeatureInfo* feature_info,
220       GLenum target,
221       GLint level,
222       GLenum internal_format,
223       GLsizei width,
224       GLsizei height,
225       GLsizei depth,
226       GLint border,
227       GLenum format,
228       GLenum type,
229       bool cleared);
230 
231   // In GLES2 "texture complete" means it has all required mips for filtering
232   // down to a 1x1 pixel texture, they are in the correct order, they are all
233   // the same format.
texture_complete()234   bool texture_complete() const {
235     return texture_complete_;
236   }
237 
238   // In GLES2 "cube complete" means all 6 faces level 0 are defined, all the
239   // same format, all the same dimensions and all width = height.
cube_complete()240   bool cube_complete() const {
241     return cube_complete_;
242   }
243 
244   // Whether or not this texture is a non-power-of-two texture.
npot()245   bool npot() const {
246     return npot_;
247   }
248 
SetStreamTexture(bool stream_texture)249   void SetStreamTexture(bool stream_texture) {
250     stream_texture_ = stream_texture;
251     UpdateCanRenderCondition();
252   }
253 
254   // Marks a particular level as cleared or uncleared.
255   void SetLevelCleared(GLenum target, GLint level, bool cleared);
256 
257   // Updates the cleared flag for this texture by inspecting all the mips.
258   void UpdateCleared();
259 
260   // Clears any renderable uncleared levels.
261   // Returns false if a GL error was generated.
262   bool ClearRenderableLevels(GLES2Decoder* decoder);
263 
264   // Clears the level.
265   // Returns false if a GL error was generated.
266   bool ClearLevel(GLES2Decoder* decoder, GLenum target, GLint level);
267 
268   // Sets a texture parameter.
269   // TODO(gman): Expand to SetParameteri,f,iv,fv
270   // Returns GL_NO_ERROR on success. Otherwise the error to generate.
271   GLenum SetParameter(
272       const FeatureInfo* feature_info, GLenum pname, GLint param);
273 
274   // Makes each of the mip levels as though they were generated.
275   bool MarkMipmapsGenerated(const FeatureInfo* feature_info);
276 
NeedsMips()277   bool NeedsMips() const {
278     return min_filter_ != GL_NEAREST && min_filter_ != GL_LINEAR;
279   }
280 
281   // True if this texture meets all the GLES2 criteria for rendering.
282   // See section 3.8.2 of the GLES2 spec.
283   bool CanRender(const FeatureInfo* feature_info) const;
284 
285   // Returns true if mipmaps can be generated by GL.
286   bool CanGenerateMipmaps(const FeatureInfo* feature_info) const;
287 
288   // Sets the Texture's target
289   // Parameters:
290   //   target: GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP or
291   //           GL_TEXTURE_EXTERNAL_OES or GL_TEXTURE_RECTANGLE_ARB
292   //   max_levels: The maximum levels this type of target can have.
293   void SetTarget(
294       const FeatureInfo* feature_info, GLenum target, GLint max_levels);
295 
296   // Update info about this texture.
297   void Update(const FeatureInfo* feature_info);
298 
299   // Set the image for a particular level.
300   void SetLevelImage(
301       const FeatureInfo* feature_info,
302       GLenum target,
303       GLint level,
304       gfx::GLImage* image);
305 
306   // Appends a signature for the given level.
307   void AddToSignature(
308       const FeatureInfo* feature_info,
309       GLenum target, GLint level, std::string* signature) const;
310 
311   void SetMailboxManager(MailboxManager* mailbox_manager);
312 
313   // Updates the unsafe textures count in all the managers referencing this
314   // texture.
315   void UpdateSafeToRenderFrom(bool cleared);
316 
317   // Updates the uncleared mip count in all the managers referencing this
318   // texture.
319   void UpdateMipCleared(LevelInfo* info, bool cleared);
320 
321   // Computes the CanRenderCondition flag.
322   CanRenderCondition GetCanRenderCondition() const;
323 
324   // Updates the unrenderable texture count in all the managers referencing this
325   // texture.
326   void UpdateCanRenderCondition();
327 
328   // Updates the images count in all the managers referencing this
329   // texture.
330   void UpdateHasImages();
331 
332   // Increment the framebuffer state change count in all the managers
333   // referencing this texture.
334   void IncAllFramebufferStateChangeCount();
335 
336   MailboxManager* mailbox_manager_;
337 
338   // Info about each face and level of texture.
339   std::vector<std::vector<LevelInfo> > level_infos_;
340 
341   // The texture refs that point to this Texture.
342   typedef std::set<TextureRef*> RefSet;
343   RefSet refs_;
344 
345   // The single TextureRef that accounts for memory for this texture. Must be
346   // one of refs_.
347   TextureRef* memory_tracking_ref_;
348 
349   // The id of the texure
350   GLuint service_id_;
351 
352   // Whether all renderable mips of this texture have been cleared.
353   bool cleared_;
354 
355   int num_uncleared_mips_;
356 
357   // The target. 0 if unset, otherwise GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP.
358   GLenum target_;
359 
360   // Texture parameters.
361   GLenum min_filter_;
362   GLenum mag_filter_;
363   GLenum wrap_s_;
364   GLenum wrap_t_;
365   GLenum usage_;
366   GLenum pool_;
367 
368   // The maximum level that has been set.
369   GLint max_level_set_;
370 
371   // Whether or not this texture is "texture complete"
372   bool texture_complete_;
373 
374   // Whether or not this texture is "cube complete"
375   bool cube_complete_;
376 
377   // Whether or not this texture is non-power-of-two
378   bool npot_;
379 
380   // Whether this texture has ever been bound.
381   bool has_been_bound_;
382 
383   // The number of framebuffers this texture is attached to.
384   int framebuffer_attachment_count_;
385 
386   // Whether this is a special streaming texture.
387   bool stream_texture_;
388 
389   // Whether the texture is immutable and no further changes to the format
390   // or dimensions of the texture object can be made.
391   bool immutable_;
392 
393   // Whether or not this texture has images.
394   bool has_images_;
395 
396   // Size in bytes this texture is assumed to take in memory.
397   uint32 estimated_size_;
398 
399   // Cache of the computed CanRenderCondition flag.
400   CanRenderCondition can_render_condition_;
401 
402   DISALLOW_COPY_AND_ASSIGN(Texture);
403 };
404 
405 // This class represents a texture in a client context group. It's mostly 1:1
406 // with a client id, though it can outlive the client id if it's still bound to
407 // a FBO or another context when destroyed.
408 // Multiple TextureRef can point to the same texture with cross-context sharing.
409 //
410 // Note: for stream textures, the TextureRef that created the stream texture is
411 // set as the "owner" of the stream texture, i.e. it will call
412 // DestroyStreamTexture on destruction. This is because the StreamTextureManager
413 // isn't generally shared between ContextGroups, so ownership can't be at the
414 // Texture level. We also can't have multiple StreamTexture on the same service
415 // id, so there can be only one owner.
416 class GPU_EXPORT TextureRef : public base::RefCounted<TextureRef> {
417  public:
418   TextureRef(TextureManager* manager, GLuint client_id, Texture* texture);
419   static scoped_refptr<TextureRef> Create(TextureManager* manager,
420                                           GLuint client_id,
421                                           GLuint service_id);
texture()422   const Texture* texture() const { return texture_; }
texture()423   Texture* texture() { return texture_; }
client_id()424   GLuint client_id() const { return client_id_; }
service_id()425   GLuint service_id() const { return texture_->service_id(); }
426 
427  private:
428   friend class base::RefCounted<TextureRef>;
429   friend class Texture;
430   friend class TextureManager;
431 
432   ~TextureRef();
manager()433   const TextureManager* manager() const { return manager_; }
manager()434   TextureManager* manager() { return manager_; }
reset_client_id()435   void reset_client_id() { client_id_ = 0; }
set_is_stream_texture_owner(bool owner)436   void set_is_stream_texture_owner(bool owner) {
437     is_stream_texture_owner_ = owner;
438   }
is_stream_texture_owner()439   bool is_stream_texture_owner() const { return is_stream_texture_owner_; }
440 
441   TextureManager* manager_;
442   Texture* texture_;
443   GLuint client_id_;
444   bool is_stream_texture_owner_;
445 
446   DISALLOW_COPY_AND_ASSIGN(TextureRef);
447 };
448 
449 // Holds data that is per gles2_cmd_decoder, but is related to to the
450 // TextureManager.
451 struct DecoderTextureState {
452   // total_texture_upload_time automatically initialized to 0 in default
453   // constructor.
DecoderTextureStateDecoderTextureState454   DecoderTextureState(bool texsubimage2d_faster_than_teximage2d)
455       : tex_image_2d_failed(false),
456         texture_upload_count(0),
457         texsubimage2d_faster_than_teximage2d(
458             texsubimage2d_faster_than_teximage2d) {}
459 
460   // This indicates all the following texSubImage2D calls that are part of the
461   // failed texImage2D call should be ignored.
462   bool tex_image_2d_failed;
463 
464   // Command buffer stats.
465   int texture_upload_count;
466   base::TimeDelta total_texture_upload_time;
467 
468   bool texsubimage2d_faster_than_teximage2d;
469 };
470 
471 // This class keeps track of the textures and their sizes so we can do NPOT and
472 // texture complete checking.
473 //
474 // NOTE: To support shared resources an instance of this class will need to be
475 // shared by multiple GLES2Decoders.
476 class GPU_EXPORT TextureManager {
477  public:
478   class GPU_EXPORT DestructionObserver {
479    public:
480     DestructionObserver();
481     virtual ~DestructionObserver();
482 
483     // Called in ~TextureManager.
484     virtual void OnTextureManagerDestroying(TextureManager* manager) = 0;
485 
486     // Called via ~TextureRef.
487     virtual void OnTextureRefDestroying(TextureRef* texture) = 0;
488 
489    private:
490     DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
491   };
492 
493   enum DefaultAndBlackTextures {
494     kTexture2D,
495     kCubeMap,
496     kExternalOES,
497     kRectangleARB,
498     kNumDefaultTextures
499   };
500 
501   TextureManager(MemoryTracker* memory_tracker,
502                  FeatureInfo* feature_info,
503                  GLsizei max_texture_size,
504                  GLsizei max_cube_map_texture_size);
505   ~TextureManager();
506 
set_framebuffer_manager(FramebufferManager * manager)507   void set_framebuffer_manager(FramebufferManager* manager) {
508     framebuffer_manager_ = manager;
509   }
510 
set_stream_texture_manager(StreamTextureManager * manager)511   void set_stream_texture_manager(StreamTextureManager* manager) {
512     stream_texture_manager_ = manager;
513   }
514 
515   // Init the texture manager.
516   bool Initialize();
517 
518   // Must call before destruction.
519   void Destroy(bool have_context);
520 
521   // Returns the maximum number of levels.
MaxLevelsForTarget(GLenum target)522   GLint MaxLevelsForTarget(GLenum target) const {
523     switch (target) {
524       case GL_TEXTURE_2D:
525         return  max_levels_;
526       case GL_TEXTURE_EXTERNAL_OES:
527         return 1;
528       default:
529         return max_cube_map_levels_;
530     }
531   }
532 
533   // Returns the maximum size.
MaxSizeForTarget(GLenum target)534   GLsizei MaxSizeForTarget(GLenum target) const {
535     switch (target) {
536       case GL_TEXTURE_2D:
537       case GL_TEXTURE_EXTERNAL_OES:
538         return max_texture_size_;
539       default:
540         return max_cube_map_texture_size_;
541     }
542   }
543 
544   // Returns the maxium number of levels a texture of the given size can have.
545   static GLsizei ComputeMipMapCount(GLenum target,
546                                     GLsizei width,
547                                     GLsizei height,
548                                     GLsizei depth);
549 
550   // Checks if a dimensions are valid for a given target.
551   bool ValidForTarget(
552       GLenum target, GLint level,
553       GLsizei width, GLsizei height, GLsizei depth);
554 
555   // True if this texture meets all the GLES2 criteria for rendering.
556   // See section 3.8.2 of the GLES2 spec.
CanRender(const TextureRef * ref)557   bool CanRender(const TextureRef* ref) const {
558     return ref->texture()->CanRender(feature_info_.get());
559   }
560 
561   // Returns true if mipmaps can be generated by GL.
CanGenerateMipmaps(const TextureRef * ref)562   bool CanGenerateMipmaps(const TextureRef* ref) const {
563     return ref->texture()->CanGenerateMipmaps(feature_info_.get());
564   }
565 
566   // Sets the Texture's target
567   // Parameters:
568   //   target: GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP
569   //   max_levels: The maximum levels this type of target can have.
570   void SetTarget(
571       TextureRef* ref,
572       GLenum target);
573 
574   // Marks a texture as a stream texture, and the ref as the stream texture
575   // owner.
576   void SetStreamTexture(TextureRef* ref, bool stream_texture);
577 
578   // Whether the TextureRef is the stream texture owner.
579   bool IsStreamTextureOwner(TextureRef* ref);
580 
581   // Set the info for a particular level in a TexureInfo.
582   void SetLevelInfo(
583       TextureRef* ref,
584       GLenum target,
585       GLint level,
586       GLenum internal_format,
587       GLsizei width,
588       GLsizei height,
589       GLsizei depth,
590       GLint border,
591       GLenum format,
592       GLenum type,
593       bool cleared);
594 
595   // Adapter to call above function.
SetLevelInfoFromParams(TextureRef * ref,const gpu::AsyncTexImage2DParams & params)596   void SetLevelInfoFromParams(TextureRef* ref,
597                               const gpu::AsyncTexImage2DParams& params) {
598     SetLevelInfo(
599         ref, params.target, params.level, params.internal_format,
600         params.width, params.height, 1 /* depth */,
601         params.border, params.format,
602         params.type, true /* cleared */ );
603   }
604 
605   Texture* Produce(TextureRef* ref);
606 
607   // Maps an existing texture into the texture manager, at a given client ID.
608   TextureRef* Consume(GLuint client_id, Texture* texture);
609 
610   // Sets a mip as cleared.
611   void SetLevelCleared(TextureRef* ref, GLenum target,
612                        GLint level, bool cleared);
613 
614   // Sets a texture parameter of a Texture
615   // Returns GL_NO_ERROR on success. Otherwise the error to generate.
616   // TODO(gman): Expand to SetParameteri,f,iv,fv
617   void SetParameter(
618       const char* function_name, ErrorState* error_state,
619       TextureRef* ref, GLenum pname, GLint param);
620 
621   // Makes each of the mip levels as though they were generated.
622   // Returns false if that's not allowed for the given texture.
623   bool MarkMipmapsGenerated(TextureRef* ref);
624 
625   // Clears any uncleared renderable levels.
626   bool ClearRenderableLevels(GLES2Decoder* decoder, TextureRef* ref);
627 
628   // Clear a specific level.
629   bool ClearTextureLevel(
630       GLES2Decoder* decoder, TextureRef* ref, GLenum target, GLint level);
631 
632   // Creates a new texture info.
633   TextureRef* CreateTexture(GLuint client_id, GLuint service_id);
634 
635   // Gets the texture info for the given texture.
636   TextureRef* GetTexture(GLuint client_id) const;
637 
638   // Removes a texture info.
639   void RemoveTexture(GLuint client_id);
640 
641   // Gets a Texture for a given service id (note: it assumes the texture object
642   // is still mapped in this TextureManager).
643   Texture* GetTextureForServiceId(GLuint service_id) const;
644 
GetDefaultTextureInfo(GLenum target)645   TextureRef* GetDefaultTextureInfo(GLenum target) {
646     switch (target) {
647       case GL_TEXTURE_2D:
648         return default_textures_[kTexture2D].get();
649       case GL_TEXTURE_CUBE_MAP:
650         return default_textures_[kCubeMap].get();
651       case GL_TEXTURE_EXTERNAL_OES:
652         return default_textures_[kExternalOES].get();
653       case GL_TEXTURE_RECTANGLE_ARB:
654         return default_textures_[kRectangleARB].get();
655       default:
656         NOTREACHED();
657         return NULL;
658     }
659   }
660 
HaveUnrenderableTextures()661   bool HaveUnrenderableTextures() const {
662     return num_unrenderable_textures_ > 0;
663   }
664 
HaveUnsafeTextures()665   bool HaveUnsafeTextures() const {
666     return num_unsafe_textures_ > 0;
667   }
668 
HaveUnclearedMips()669   bool HaveUnclearedMips() const {
670     return num_uncleared_mips_ > 0;
671   }
672 
HaveImages()673   bool HaveImages() const {
674     return num_images_ > 0;
675   }
676 
black_texture_id(GLenum target)677   GLuint black_texture_id(GLenum target) const {
678     switch (target) {
679       case GL_SAMPLER_2D:
680         return black_texture_ids_[kTexture2D];
681       case GL_SAMPLER_CUBE:
682         return black_texture_ids_[kCubeMap];
683       case GL_SAMPLER_EXTERNAL_OES:
684         return black_texture_ids_[kExternalOES];
685       case GL_SAMPLER_2D_RECT_ARB:
686         return black_texture_ids_[kRectangleARB];
687       default:
688         NOTREACHED();
689         return 0;
690     }
691   }
692 
mem_represented()693   size_t mem_represented() const {
694     return
695         memory_tracker_managed_->GetMemRepresented() +
696         memory_tracker_unmanaged_->GetMemRepresented();
697   }
698 
699   void SetLevelImage(
700       TextureRef* ref,
701       GLenum target,
702       GLint level,
703       gfx::GLImage* image);
704 
705   void AddToSignature(
706       TextureRef* ref,
707       GLenum target,
708       GLint level,
709       std::string* signature) const;
710 
AddObserver(DestructionObserver * observer)711   void AddObserver(DestructionObserver* observer) {
712     destruction_observers_.AddObserver(observer);
713   }
714 
RemoveObserver(DestructionObserver * observer)715   void RemoveObserver(DestructionObserver* observer) {
716     destruction_observers_.RemoveObserver(observer);
717   }
718 
719   struct DoTextImage2DArguments {
720     GLenum target;
721     GLint level;
722     GLenum internal_format;
723     GLsizei width;
724     GLsizei height;
725     GLint border;
726     GLenum format;
727     GLenum type;
728     const void* pixels;
729     uint32 pixels_size;
730   };
731 
732   bool ValidateTexImage2D(
733     ContextState* state,
734     const char* function_name,
735     const DoTextImage2DArguments& args,
736     // Pointer to TextureRef filled in if validation successful.
737     // Presumes the pointer is valid.
738     TextureRef** texture_ref);
739 
740   void ValidateAndDoTexImage2D(
741     DecoderTextureState* texture_state,
742     ContextState* state,
743     DecoderFramebufferState* framebuffer_state,
744     const DoTextImage2DArguments& args);
745 
746   // TODO(kloveless): Make GetTexture* private once this is no longer called
747   // from gles2_cmd_decoder.
748   TextureRef* GetTextureInfoForTarget(ContextState* state, GLenum target);
749   TextureRef* GetTextureInfoForTargetUnlessDefault(
750       ContextState* state, GLenum target);
751 
752   bool ValidateTextureParameters(
753     ErrorState* error_state, const char* function_name,
754     GLenum target, GLenum format, GLenum type, GLint level);
755 
756  private:
757   friend class Texture;
758   friend class TextureRef;
759 
760   // Helper for Initialize().
761   scoped_refptr<TextureRef> CreateDefaultAndBlackTextures(
762       GLenum target,
763       GLuint* black_texture);
764 
765   void DoTexImage2D(
766     DecoderTextureState* texture_state,
767     ErrorState* error_state,
768     DecoderFramebufferState* framebuffer_state,
769     TextureRef* texture_ref,
770     const DoTextImage2DArguments& args);
771 
772   void StartTracking(TextureRef* texture);
773   void StopTracking(TextureRef* texture);
774 
775   void UpdateSafeToRenderFrom(int delta);
776   void UpdateUnclearedMips(int delta);
777   void UpdateCanRenderCondition(Texture::CanRenderCondition old_condition,
778                                 Texture::CanRenderCondition new_condition);
779   void UpdateNumImages(int delta);
780   void IncFramebufferStateChangeCount();
781 
782   MemoryTypeTracker* GetMemTracker(GLenum texture_pool);
783   scoped_ptr<MemoryTypeTracker> memory_tracker_managed_;
784   scoped_ptr<MemoryTypeTracker> memory_tracker_unmanaged_;
785 
786   scoped_refptr<FeatureInfo> feature_info_;
787 
788   FramebufferManager* framebuffer_manager_;
789   StreamTextureManager* stream_texture_manager_;
790 
791   // Info for each texture in the system.
792   typedef base::hash_map<GLuint, scoped_refptr<TextureRef> > TextureMap;
793   TextureMap textures_;
794 
795   GLsizei max_texture_size_;
796   GLsizei max_cube_map_texture_size_;
797   GLint max_levels_;
798   GLint max_cube_map_levels_;
799 
800   int num_unrenderable_textures_;
801   int num_unsafe_textures_;
802   int num_uncleared_mips_;
803   int num_images_;
804 
805   // Counts the number of Textures allocated with 'this' as its manager.
806   // Allows to check no Texture will outlive this.
807   unsigned int texture_count_;
808 
809   bool have_context_;
810 
811   // Black (0,0,0,1) textures for when non-renderable textures are used.
812   // NOTE: There is no corresponding Texture for these textures.
813   // TextureInfos are only for textures the client side can access.
814   GLuint black_texture_ids_[kNumDefaultTextures];
815 
816   // The default textures for each target (texture name = 0)
817   scoped_refptr<TextureRef> default_textures_[kNumDefaultTextures];
818 
819   ObserverList<DestructionObserver> destruction_observers_;
820 
821   DISALLOW_COPY_AND_ASSIGN(TextureManager);
822 };
823 
824 // This class records texture upload time when in scope.
825 class ScopedTextureUploadTimer {
826  public:
827   explicit ScopedTextureUploadTimer(DecoderTextureState* texture_state);
828   ~ScopedTextureUploadTimer();
829 
830  private:
831   DecoderTextureState* texture_state_;
832   base::TimeTicks begin_time_;
833   DISALLOW_COPY_AND_ASSIGN(ScopedTextureUploadTimer);
834 };
835 
836 }  // namespace gles2
837 }  // namespace gpu
838 
839 #endif  // GPU_COMMAND_BUFFER_SERVICE_TEXTURE_MANAGER_H_
840