Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / gpu / command_buffer / service / texture_manager.h
bloba323076a97d821ab89b112bdbd01f584c2788bbd
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 private:
205 friend class MailboxManagerImpl;
206 friend class MailboxManagerSync;
207 friend class MailboxManagerTest;
208 friend class TextureDefinition;
209 friend class TextureManager;
210 friend class TextureRef;
211 friend class TextureTestHelper;
213 ~Texture();
214 void AddTextureRef(TextureRef* ref);
215 void RemoveTextureRef(TextureRef* ref, bool have_context);
216 MemoryTypeTracker* GetMemTracker();
218 // Condition on which this texture is renderable. Can be ONLY_IF_NPOT if it
219 // depends on context support for non-power-of-two textures (i.e. will be
220 // renderable if NPOT support is in the context, otherwise not, e.g. texture
221 // with a NPOT level). ALWAYS means it doesn't depend on context features
222 // (e.g. complete POT), NEVER means it's not renderable regardless (e.g.
223 // incomplete).
224 enum CanRenderCondition {
225 CAN_RENDER_ALWAYS,
226 CAN_RENDER_NEVER,
227 CAN_RENDER_ONLY_IF_NPOT
230 struct LevelInfo {
231 LevelInfo();
232 LevelInfo(const LevelInfo& rhs);
233 ~LevelInfo();
235 gfx::Rect cleared_rect;
236 GLenum target;
237 GLint level;
238 GLenum internal_format;
239 GLsizei width;
240 GLsizei height;
241 GLsizei depth;
242 GLint border;
243 GLenum format;
244 GLenum type;
245 scoped_refptr<gfx::GLImage> image;
246 uint32 estimated_size;
249 struct FaceInfo {
250 FaceInfo();
251 ~FaceInfo();
253 GLsizei num_mip_levels;
254 std::vector<LevelInfo> level_infos;
257 // Set the info for a particular level.
258 void SetLevelInfo(const FeatureInfo* feature_info,
259 GLenum target,
260 GLint level,
261 GLenum internal_format,
262 GLsizei width,
263 GLsizei height,
264 GLsizei depth,
265 GLint border,
266 GLenum format,
267 GLenum type,
268 const gfx::Rect& cleared_rect);
270 // In GLES2 "texture complete" means it has all required mips for filtering
271 // down to a 1x1 pixel texture, they are in the correct order, they are all
272 // the same format.
273 bool texture_complete() const {
274 return texture_complete_;
277 // In GLES2 "cube complete" means all 6 faces level 0 are defined, all the
278 // same format, all the same dimensions and all width = height.
279 bool cube_complete() const {
280 return cube_complete_;
283 // Whether or not this texture is a non-power-of-two texture.
284 bool npot() const {
285 return npot_;
288 // Marks a |rect| of a particular level as cleared.
289 void SetLevelClearedRect(GLenum target,
290 GLint level,
291 const gfx::Rect& cleared_rect);
293 // Marks a particular level as cleared or uncleared.
294 void SetLevelCleared(GLenum target, GLint level, bool cleared);
296 // Updates the cleared flag for this texture by inspecting all the mips.
297 void UpdateCleared();
299 // Clears any renderable uncleared levels.
300 // Returns false if a GL error was generated.
301 bool ClearRenderableLevels(GLES2Decoder* decoder);
303 // Clears the level.
304 // Returns false if a GL error was generated.
305 bool ClearLevel(GLES2Decoder* decoder, GLenum target, GLint level);
307 // Sets a texture parameter.
308 // TODO(gman): Expand to SetParameteriv,fv
309 // Returns GL_NO_ERROR on success. Otherwise the error to generate.
310 GLenum SetParameteri(
311 const FeatureInfo* feature_info, GLenum pname, GLint param);
312 GLenum SetParameterf(
313 const FeatureInfo* feature_info, GLenum pname, GLfloat param);
315 // Makes each of the mip levels as though they were generated.
316 bool MarkMipmapsGenerated(const FeatureInfo* feature_info);
318 bool NeedsMips() const {
319 return min_filter_ != GL_NEAREST && min_filter_ != GL_LINEAR;
322 // True if this texture meets all the GLES2 criteria for rendering.
323 // See section 3.8.2 of the GLES2 spec.
324 bool CanRender(const FeatureInfo* feature_info) const;
326 // Returns true if mipmaps can be generated by GL.
327 bool CanGenerateMipmaps(const FeatureInfo* feature_info) const;
329 // Returns true if any of the texture dimensions are not a power of two.
330 static bool TextureIsNPOT(GLsizei width, GLsizei height, GLsizei depth);
332 // Returns true if texture face is complete relative to the first face.
333 static bool TextureFaceComplete(const Texture::LevelInfo& first_face,
334 size_t face_index,
335 GLenum target,
336 GLenum internal_format,
337 GLsizei width,
338 GLsizei height,
339 GLsizei depth,
340 GLenum format,
341 GLenum type);
343 // Returns true if texture mip level is complete relative to first level.
344 static bool TextureMipComplete(const Texture::LevelInfo& level0_face,
345 GLenum target,
346 GLint level,
347 GLenum internal_format,
348 GLsizei width,
349 GLsizei height,
350 GLsizei depth,
351 GLenum format,
352 GLenum type);
354 // Sets the Texture's target
355 // Parameters:
356 // target: GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP or
357 // GL_TEXTURE_EXTERNAL_OES or GL_TEXTURE_RECTANGLE_ARB
358 // GL_TEXTURE_2D_ARRAY or GL_TEXTURE_3D (for GLES3)
359 // max_levels: The maximum levels this type of target can have.
360 void SetTarget(
361 const FeatureInfo* feature_info, GLenum target, GLint max_levels);
363 // Update info about this texture.
364 void Update(const FeatureInfo* feature_info);
366 // Set the image for a particular level.
367 void SetLevelImage(
368 const FeatureInfo* feature_info,
369 GLenum target,
370 GLint level,
371 gfx::GLImage* image);
373 // Appends a signature for the given level.
374 void AddToSignature(
375 const FeatureInfo* feature_info,
376 GLenum target, GLint level, std::string* signature) const;
378 void SetMailboxManager(MailboxManager* mailbox_manager);
380 // Updates the unsafe textures count in all the managers referencing this
381 // texture.
382 void UpdateSafeToRenderFrom(bool cleared);
384 // Updates the uncleared mip count in all the managers referencing this
385 // texture.
386 void UpdateMipCleared(LevelInfo* info,
387 GLsizei width,
388 GLsizei height,
389 const gfx::Rect& cleared_rect);
391 // Computes the CanRenderCondition flag.
392 CanRenderCondition GetCanRenderCondition() const;
394 // Updates the unrenderable texture count in all the managers referencing this
395 // texture.
396 void UpdateCanRenderCondition();
398 // Updates the images count in all the managers referencing this
399 // texture.
400 void UpdateHasImages();
402 // Increment the framebuffer state change count in all the managers
403 // referencing this texture.
404 void IncAllFramebufferStateChangeCount();
406 MailboxManager* mailbox_manager_;
408 // Info about each face and level of texture.
409 std::vector<FaceInfo> face_infos_;
411 // The texture refs that point to this Texture.
412 typedef std::set<TextureRef*> RefSet;
413 RefSet refs_;
415 // The single TextureRef that accounts for memory for this texture. Must be
416 // one of refs_.
417 TextureRef* memory_tracking_ref_;
419 // The id of the texure
420 GLuint service_id_;
422 // Whether all renderable mips of this texture have been cleared.
423 bool cleared_;
425 int num_uncleared_mips_;
426 int num_npot_faces_;
428 // The target. 0 if unset, otherwise GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP.
429 // Or GL_TEXTURE_2D_ARRAY or GL_TEXTURE_3D (for GLES3).
430 GLenum target_;
432 // Texture parameters.
433 GLenum min_filter_;
434 GLenum mag_filter_;
435 GLenum wrap_r_;
436 GLenum wrap_s_;
437 GLenum wrap_t_;
438 GLenum usage_;
439 GLenum pool_;
440 GLenum compare_func_;
441 GLenum compare_mode_;
442 GLfloat max_lod_;
443 GLfloat min_lod_;
444 GLint base_level_;
445 GLint max_level_;
447 // The maximum level that has been set.
448 GLint max_level_set_;
450 // Whether or not this texture is "texture complete"
451 bool texture_complete_;
453 // Whether mip levels have changed and should be reverified.
454 bool texture_mips_dirty_;
455 bool texture_mips_complete_;
457 // Whether or not this texture is "cube complete"
458 bool cube_complete_;
460 // Whether any level 0 faces have changed and should be reverified.
461 bool texture_level0_dirty_;
462 bool texture_level0_complete_;
464 // Whether or not this texture is non-power-of-two
465 bool npot_;
467 // Whether this texture has ever been bound.
468 bool has_been_bound_;
470 // The number of framebuffers this texture is attached to.
471 int framebuffer_attachment_count_;
473 // Whether the texture is immutable and no further changes to the format
474 // or dimensions of the texture object can be made.
475 bool immutable_;
477 // Whether or not this texture has images.
478 bool has_images_;
480 // Size in bytes this texture is assumed to take in memory.
481 uint32 estimated_size_;
483 // Cache of the computed CanRenderCondition flag.
484 CanRenderCondition can_render_condition_;
486 // Whether we have initialized TEXTURE_MAX_ANISOTROPY to 1.
487 bool texture_max_anisotropy_initialized_;
489 DISALLOW_COPY_AND_ASSIGN(Texture);
492 // This class represents a texture in a client context group. It's mostly 1:1
493 // with a client id, though it can outlive the client id if it's still bound to
494 // a FBO or another context when destroyed.
495 // Multiple TextureRef can point to the same texture with cross-context sharing.
496 class GPU_EXPORT TextureRef : public base::RefCounted<TextureRef> {
497 public:
498 TextureRef(TextureManager* manager, GLuint client_id, Texture* texture);
499 static scoped_refptr<TextureRef> Create(TextureManager* manager,
500 GLuint client_id,
501 GLuint service_id);
503 void AddObserver() { num_observers_++; }
504 void RemoveObserver() { num_observers_--; }
506 const Texture* texture() const { return texture_; }
507 Texture* texture() { return texture_; }
508 GLuint client_id() const { return client_id_; }
509 GLuint service_id() const { return texture_->service_id(); }
510 GLint num_observers() const { return num_observers_; }
512 private:
513 friend class base::RefCounted<TextureRef>;
514 friend class Texture;
515 friend class TextureManager;
517 ~TextureRef();
518 const TextureManager* manager() const { return manager_; }
519 TextureManager* manager() { return manager_; }
520 void reset_client_id() { client_id_ = 0; }
522 TextureManager* manager_;
523 Texture* texture_;
524 GLuint client_id_;
525 GLint num_observers_;
527 DISALLOW_COPY_AND_ASSIGN(TextureRef);
530 // Holds data that is per gles2_cmd_decoder, but is related to to the
531 // TextureManager.
532 struct DecoderTextureState {
533 // total_texture_upload_time automatically initialized to 0 in default
534 // constructor.
535 explicit DecoderTextureState(bool texsubimage_faster_than_teximage)
536 : tex_image_failed(false),
537 texture_upload_count(0),
538 texsubimage_faster_than_teximage(texsubimage_faster_than_teximage) {}
540 // This indicates all the following texSubImage*D calls that are part of the
541 // failed texImage*D call should be ignored. The client calls have a lock
542 // around them, so it will affect only a single texImage*D + texSubImage*D
543 // group.
544 bool tex_image_failed;
546 // Command buffer stats.
547 int texture_upload_count;
548 base::TimeDelta total_texture_upload_time;
550 bool texsubimage_faster_than_teximage;
553 // This class keeps track of the textures and their sizes so we can do NPOT and
554 // texture complete checking.
556 // NOTE: To support shared resources an instance of this class will need to be
557 // shared by multiple GLES2Decoders.
558 class GPU_EXPORT TextureManager {
559 public:
560 class GPU_EXPORT DestructionObserver {
561 public:
562 DestructionObserver();
563 virtual ~DestructionObserver();
565 // Called in ~TextureManager.
566 virtual void OnTextureManagerDestroying(TextureManager* manager) = 0;
568 // Called via ~TextureRef.
569 virtual void OnTextureRefDestroying(TextureRef* texture) = 0;
571 private:
572 DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
575 enum DefaultAndBlackTextures {
576 kTexture2D,
577 kCubeMap,
578 kExternalOES,
579 kRectangleARB,
580 kNumDefaultTextures
583 TextureManager(MemoryTracker* memory_tracker,
584 FeatureInfo* feature_info,
585 GLsizei max_texture_size,
586 GLsizei max_cube_map_texture_size,
587 GLsizei max_rectangle_texture_size,
588 GLsizei max_3d_texture_size,
589 bool use_default_textures);
590 ~TextureManager();
592 void set_framebuffer_manager(FramebufferManager* manager) {
593 framebuffer_manager_ = manager;
596 // Init the texture manager.
597 bool Initialize();
599 // Must call before destruction.
600 void Destroy(bool have_context);
602 // Returns the maximum number of levels.
603 GLint MaxLevelsForTarget(GLenum target) const {
604 switch (target) {
605 case GL_TEXTURE_2D:
606 return max_levels_;
607 case GL_TEXTURE_RECTANGLE_ARB:
608 case GL_TEXTURE_EXTERNAL_OES:
609 return 1;
610 case GL_TEXTURE_3D:
611 case GL_TEXTURE_2D_ARRAY:
612 return max_3d_levels_;
613 default:
614 return max_cube_map_levels_;
618 // Returns the maximum size.
619 GLsizei MaxSizeForTarget(GLenum target) const {
620 switch (target) {
621 case GL_TEXTURE_2D:
622 case GL_TEXTURE_EXTERNAL_OES:
623 return max_texture_size_;
624 case GL_TEXTURE_RECTANGLE:
625 return max_rectangle_texture_size_;
626 case GL_TEXTURE_3D:
627 case GL_TEXTURE_2D_ARRAY:
628 return max_3d_texture_size_;
629 default:
630 return max_cube_map_texture_size_;
634 // Returns the maxium number of levels a texture of the given size can have.
635 static GLsizei ComputeMipMapCount(GLenum target,
636 GLsizei width,
637 GLsizei height,
638 GLsizei depth);
640 // Checks if a dimensions are valid for a given target.
641 bool ValidForTarget(
642 GLenum target, GLint level,
643 GLsizei width, GLsizei height, GLsizei depth);
645 // True if this texture meets all the GLES2 criteria for rendering.
646 // See section 3.8.2 of the GLES2 spec.
647 bool CanRender(const TextureRef* ref) const {
648 return ref->texture()->CanRender(feature_info_.get());
651 // Returns true if mipmaps can be generated by GL.
652 bool CanGenerateMipmaps(const TextureRef* ref) const {
653 return ref->texture()->CanGenerateMipmaps(feature_info_.get());
656 // Sets the Texture's target
657 // Parameters:
658 // target: GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP
659 // GL_TEXTURE_2D_ARRAY or GL_TEXTURE_3D (for GLES3)
660 // max_levels: The maximum levels this type of target can have.
661 void SetTarget(
662 TextureRef* ref,
663 GLenum target);
665 // Set the info for a particular level in a TexureInfo.
666 void SetLevelInfo(TextureRef* ref,
667 GLenum target,
668 GLint level,
669 GLenum internal_format,
670 GLsizei width,
671 GLsizei height,
672 GLsizei depth,
673 GLint border,
674 GLenum format,
675 GLenum type,
676 const gfx::Rect& cleared_rect);
678 // Adapter to call above function.
679 void SetLevelInfoFromParams(TextureRef* ref,
680 const gpu::AsyncTexImage2DParams& params) {
681 SetLevelInfo(ref, params.target, params.level, params.internal_format,
682 params.width, params.height, 1 /* depth */, params.border,
683 params.format, params.type,
684 gfx::Rect(params.width, params.height) /* cleared_rect */);
687 Texture* Produce(TextureRef* ref);
689 // Maps an existing texture into the texture manager, at a given client ID.
690 TextureRef* Consume(GLuint client_id, Texture* texture);
692 // Sets |rect| of mip as cleared.
693 void SetLevelClearedRect(TextureRef* ref,
694 GLenum target,
695 GLint level,
696 const gfx::Rect& cleared_rect);
698 // Sets a mip as cleared.
699 void SetLevelCleared(TextureRef* ref, GLenum target,
700 GLint level, bool cleared);
702 // Sets a texture parameter of a Texture
703 // Returns GL_NO_ERROR on success. Otherwise the error to generate.
704 // TODO(gman): Expand to SetParameteriv,fv
705 void SetParameteri(
706 const char* function_name, ErrorState* error_state,
707 TextureRef* ref, GLenum pname, GLint param);
708 void SetParameterf(
709 const char* function_name, ErrorState* error_state,
710 TextureRef* ref, GLenum pname, GLfloat param);
712 // Makes each of the mip levels as though they were generated.
713 // Returns false if that's not allowed for the given texture.
714 bool MarkMipmapsGenerated(TextureRef* ref);
716 // Clears any uncleared renderable levels.
717 bool ClearRenderableLevels(GLES2Decoder* decoder, TextureRef* ref);
719 // Clear a specific level.
720 bool ClearTextureLevel(
721 GLES2Decoder* decoder, TextureRef* ref, GLenum target, GLint level);
723 // Creates a new texture info.
724 TextureRef* CreateTexture(GLuint client_id, GLuint service_id);
726 // Gets the texture info for the given texture.
727 TextureRef* GetTexture(GLuint client_id) const;
729 // Removes a texture info.
730 void RemoveTexture(GLuint client_id);
732 // Gets a Texture for a given service id (note: it assumes the texture object
733 // is still mapped in this TextureManager).
734 Texture* GetTextureForServiceId(GLuint service_id) const;
736 TextureRef* GetDefaultTextureInfo(GLenum target) {
737 switch (target) {
738 case GL_TEXTURE_2D:
739 return default_textures_[kTexture2D].get();
740 case GL_TEXTURE_CUBE_MAP:
741 return default_textures_[kCubeMap].get();
742 case GL_TEXTURE_EXTERNAL_OES:
743 return default_textures_[kExternalOES].get();
744 case GL_TEXTURE_RECTANGLE_ARB:
745 return default_textures_[kRectangleARB].get();
746 default:
747 NOTREACHED();
748 return NULL;
752 bool HaveUnrenderableTextures() const {
753 return num_unrenderable_textures_ > 0;
756 bool HaveUnsafeTextures() const {
757 return num_unsafe_textures_ > 0;
760 bool HaveUnclearedMips() const {
761 return num_uncleared_mips_ > 0;
764 bool HaveImages() const {
765 return num_images_ > 0;
768 GLuint black_texture_id(GLenum target) const {
769 switch (target) {
770 case GL_SAMPLER_2D:
771 return black_texture_ids_[kTexture2D];
772 case GL_SAMPLER_CUBE:
773 return black_texture_ids_[kCubeMap];
774 case GL_SAMPLER_EXTERNAL_OES:
775 return black_texture_ids_[kExternalOES];
776 case GL_SAMPLER_2D_RECT_ARB:
777 return black_texture_ids_[kRectangleARB];
778 default:
779 NOTREACHED();
780 return 0;
784 size_t mem_represented() const {
785 return
786 memory_tracker_managed_->GetMemRepresented() +
787 memory_tracker_unmanaged_->GetMemRepresented();
790 void SetLevelImage(
791 TextureRef* ref,
792 GLenum target,
793 GLint level,
794 gfx::GLImage* image);
796 size_t GetSignatureSize() const;
798 void AddToSignature(
799 TextureRef* ref,
800 GLenum target,
801 GLint level,
802 std::string* signature) const;
804 void AddObserver(DestructionObserver* observer) {
805 destruction_observers_.push_back(observer);
808 void RemoveObserver(DestructionObserver* observer) {
809 for (unsigned int i = 0; i < destruction_observers_.size(); i++) {
810 if (destruction_observers_[i] == observer) {
811 std::swap(destruction_observers_[i], destruction_observers_.back());
812 destruction_observers_.pop_back();
813 return;
816 NOTREACHED();
819 struct DoTexImageArguments {
820 enum TexImageCommandType {
821 kTexImage2D,
822 kTexImage3D,
825 GLenum target;
826 GLint level;
827 GLenum internal_format;
828 GLsizei width;
829 GLsizei height;
830 GLsizei depth;
831 GLint border;
832 GLenum format;
833 GLenum type;
834 const void* pixels;
835 uint32 pixels_size;
836 TexImageCommandType command_type;
839 bool ValidateTexImage(
840 ContextState* state,
841 const char* function_name,
842 const DoTexImageArguments& args,
843 // Pointer to TextureRef filled in if validation successful.
844 // Presumes the pointer is valid.
845 TextureRef** texture_ref);
847 void ValidateAndDoTexImage(
848 DecoderTextureState* texture_state,
849 ContextState* state,
850 DecoderFramebufferState* framebuffer_state,
851 const char* function_name,
852 const DoTexImageArguments& args);
854 // TODO(kloveless): Make GetTexture* private once this is no longer called
855 // from gles2_cmd_decoder.
856 TextureRef* GetTextureInfoForTarget(ContextState* state, GLenum target);
857 TextureRef* GetTextureInfoForTargetUnlessDefault(
858 ContextState* state, GLenum target);
860 bool ValidateFormatAndTypeCombination(
861 ErrorState* error_state, const char* function_name,
862 GLenum format, GLenum type);
864 // Note that internal_format is only checked in relation to the format
865 // parameter, so that this function may be used to validate texSubImage2D.
866 bool ValidateTextureParameters(
867 ErrorState* error_state, const char* function_name,
868 GLenum format, GLenum type, GLenum internal_format, GLint level);
870 private:
871 friend class Texture;
872 friend class TextureRef;
874 // Helper for Initialize().
875 scoped_refptr<TextureRef> CreateDefaultAndBlackTextures(
876 GLenum target,
877 GLuint* black_texture);
879 void DoTexImage(
880 DecoderTextureState* texture_state,
881 ErrorState* error_state,
882 DecoderFramebufferState* framebuffer_state,
883 const char* function_name,
884 TextureRef* texture_ref,
885 const DoTexImageArguments& args);
887 void StartTracking(TextureRef* texture);
888 void StopTracking(TextureRef* texture);
890 void UpdateSafeToRenderFrom(int delta);
891 void UpdateUnclearedMips(int delta);
892 void UpdateCanRenderCondition(Texture::CanRenderCondition old_condition,
893 Texture::CanRenderCondition new_condition);
894 void UpdateNumImages(int delta);
895 void IncFramebufferStateChangeCount();
897 GLenum AdjustTexFormat(GLenum format) const;
899 MemoryTypeTracker* GetMemTracker(GLenum texture_pool);
900 scoped_ptr<MemoryTypeTracker> memory_tracker_managed_;
901 scoped_ptr<MemoryTypeTracker> memory_tracker_unmanaged_;
903 scoped_refptr<FeatureInfo> feature_info_;
905 FramebufferManager* framebuffer_manager_;
907 // Info for each texture in the system.
908 typedef base::hash_map<GLuint, scoped_refptr<TextureRef> > TextureMap;
909 TextureMap textures_;
911 GLsizei max_texture_size_;
912 GLsizei max_cube_map_texture_size_;
913 GLsizei max_rectangle_texture_size_;
914 GLsizei max_3d_texture_size_;
915 GLint max_levels_;
916 GLint max_cube_map_levels_;
917 GLint max_3d_levels_;
919 const bool use_default_textures_;
921 int num_unrenderable_textures_;
922 int num_unsafe_textures_;
923 int num_uncleared_mips_;
924 int num_images_;
926 // Counts the number of Textures allocated with 'this' as its manager.
927 // Allows to check no Texture will outlive this.
928 unsigned int texture_count_;
930 bool have_context_;
932 // Black (0,0,0,1) textures for when non-renderable textures are used.
933 // NOTE: There is no corresponding Texture for these textures.
934 // TextureInfos are only for textures the client side can access.
935 GLuint black_texture_ids_[kNumDefaultTextures];
937 // The default textures for each target (texture name = 0)
938 scoped_refptr<TextureRef> default_textures_[kNumDefaultTextures];
940 std::vector<DestructionObserver*> destruction_observers_;
942 DISALLOW_COPY_AND_ASSIGN(TextureManager);
945 // This class records texture upload time when in scope.
946 class ScopedTextureUploadTimer {
947 public:
948 explicit ScopedTextureUploadTimer(DecoderTextureState* texture_state);
949 ~ScopedTextureUploadTimer();
951 private:
952 DecoderTextureState* texture_state_;
953 base::TimeTicks begin_time_;
954 DISALLOW_COPY_AND_ASSIGN(ScopedTextureUploadTimer);
957 } // namespace gles2
958 } // namespace gpu
960 #endif // GPU_COMMAND_BUFFER_SERVICE_TEXTURE_MANAGER_H_