1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3  Copyright (c) 2008-2010 Ricardo Quesada
  4  Copyright (c) 2011      Zynga Inc.
  5 
  6  http://www.cocos2d-x.org
  7 
  8  Permission is hereby granted, free of charge, to any person obtaining a copy
  9  of this software and associated documentation files (the "Software"), to deal
 10  in the Software without restriction, including without limitation the rights
 11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  copies of the Software, and to permit persons to whom the Software is
 13  furnished to do so, subject to the following conditions:
 14 
 15  The above copyright notice and this permission notice shall be included in
 16  all copies or substantial portions of the Software.
 17 
 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  THE SOFTWARE.
 25  ****************************************************************************/
 26 
 27 //CONSTANTS:
 28 
 29 //----------------------Possible texture pixel formats----------------------------
 30 /**
 31  * 32-bit texture: RGBA8888
 32  * @constant
 33  * @type {Number}
 34  */
 35 cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888 = 0;
 36 
 37 /**
 38  * 24-bit texture: RGBA888
 39  * @constant
 40  * @type {Number}
 41  */
 42 cc.TEXTURE_2D_PIXEL_FORMAT_RGB888 = 1;
 43 
 44 /**
 45  * 16-bit texture without Alpha channel
 46  * @constant
 47  * @type {Number}
 48  */
 49 cc.TEXTURE_2D_PIXEL_FORMAT_RGB565 = 2;
 50 
 51 /**
 52  * 8-bit textures used as masks
 53  * @constant
 54  * @type {Number}
 55  */
 56 cc.TEXTURE_2D_PIXEL_FORMAT_A8 = 3;
 57 
 58 /**
 59  * 8-bit intensity texture
 60  * @constant
 61  * @type {Number}
 62  */
 63 cc.TEXTURE_2D_PIXEL_FORMAT_I8 = 4;
 64 
 65 /**
 66  * 16-bit textures used as masks
 67  * @constant
 68  * @type {Number}
 69  */
 70 cc.TEXTURE_2D_PIXEL_FORMAT_AI88 = 5;
 71 
 72 /**
 73  * 16-bit textures: RGBA4444
 74  * @constant
 75  * @type {Number}
 76  */
 77 cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444 = 6;
 78 
 79 /**
 80  * 16-bit textures: RGB5A1
 81  * @constant
 82  * @type {Number}
 83  */
 84 cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1 = 7;
 85 
 86 /**
 87  * 4-bit PVRTC-compressed texture: PVRTC4
 88  * @constant
 89  * @type {Number}
 90  */
 91 cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC4 = 8;
 92 
 93 /**
 94  * 2-bit PVRTC-compressed texture: PVRTC2
 95  * @constant
 96  * @type {Number}
 97  */
 98 cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC2 = 9;
 99 
100 /**
101  * Default texture format: RGBA8888
102  * @constant
103  * @type {Number}
104  */
105 cc.TEXTURE_2D_PIXEL_FORMAT_DEFAULT = cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888;
106 
107 // If the image has alpha, you can create RGBA8 (32-bit) or RGBA4 (16-bit) or RGB5A1 (16-bit)
108 // Default is: RGBA8888 (32-bit textures)
109 cc._defaultAlphaPixelFormat = cc.TEXTURE_2D_PIXEL_FORMAT_DEFAULT;
110 
111 // By default PVR images are treated as if they don't have the alpha channel premultiplied
112 cc.PVRHaveAlphaPremultiplied_ = false;
113 
114 /**
115  Extension to set the Min / Mag filter
116  */
117 cc._texParams = function (minFilter, magFilter, wrapS, wrapT) {
118     this.minFilter = minFilter || 0;
119     this.magFilter = magFilter || 0;
120     this.wrapS = wrapS || 0;
121     this.wrapT = wrapT || 0;
122 };
123 
124 /**
125  * <p>
126  * This class allows to easily create OpenGL 2D textures from images, text or raw data.                                    <br/>
127  * The created cc.Texture2D object will always have power-of-two dimensions.                                                <br/>
128  * Depending on how you create the cc.Texture2D object, the actual image area of the texture might be smaller than the texture dimensions <br/>
129  *  i.e. "contentSize" != (pixelsWide, pixelsHigh) and (maxS, maxT) != (1.0, 1.0).                                           <br/>
130  * Be aware that the content of the generated textures will be upside-down! </p>
131  * @class
132  * @extends cc.Class
133  */
134 cc.Texture2DWebGL = cc.Class.extend(/** @lends cc.Texture2D# */{
135     // By default PVR images are treated as if they don't have the alpha channel premultiplied
136     _pVRHaveAlphaPremultiplied:null,
137     _pixelFormat:null,
138     _pixelsWide:null,
139     _pixelsHigh:null,
140     _name:null,
141     _contentSize:null,
142     _maxS:null,
143     _maxT:null,
144     _hasPremultipliedAlpha:null,
145     _hasMipmaps:false,
146 
147     _shaderProgram:null,
148 
149     _isLoaded:false,
150     _htmlElementObj:null,
151     _webTextureObj:null,
152 
153     _loadedEventListeners:null,
154 
155     /*public:*/
156     ctor:function () {
157         this._pixelsWide = 0;
158         this._pixelsHigh = 0;
159         this._name = "";
160         this._maxS = 0;
161         this._maxT = 0;
162         this._hasPremultipliedAlpha = false;
163         this._contentSize = cc._sizeConst(0, 0);
164 
165         this._hasMipmaps = false;
166         this._pVRHaveAlphaPremultiplied = true;
167         this._pixelFormat = cc.Texture2D.defaultAlphaPixelFormat();
168 
169         this._shaderProgram = null;
170         this._isLoaded = false;
171         this._htmlElementObj = null;
172         this._webTextureObj = null;
173     },
174 
175     releaseTexture:function () {
176         if (this._webTextureObj)
177             cc.renderContext.deleteTexture(this._webTextureObj);
178     },
179 
180     /**
181      * pixel format of the texture
182      * @return {Number}
183      */
184     getPixelFormat:function () {
185         return this._pixelFormat;
186     },
187 
188     /**
189      * width in pixels
190      * @return {Number}
191      */
192     getPixelsWide:function () {
193         return this._pixelsWide;
194     },
195 
196     /**
197      * hight in pixels
198      * @return {Number}
199      */
200     getPixelsHigh:function () {
201         return this._pixelsHigh;
202     },
203 
204     /**
205      * get WebGLTexture Object
206      * @return {WebGLTexture}
207      */
208     getName:function () {
209         return this._webTextureObj;
210     },
211 
212     /**
213      * content size
214      * @return {cc.Size}
215      */
216     getContentSize:function () {
217         return cc.size(this._contentSize.width / cc.CONTENT_SCALE_FACTOR(), this._contentSize.height / cc.CONTENT_SCALE_FACTOR());
218     },
219 
220     getContentSizeInPixels:function () {
221         return this._contentSize;
222     },
223 
224     /** texture max S */
225     getMaxS:function () {
226         return this._maxS;
227     },
228 
229     setMaxS:function (maxS) {
230         this._maxS = maxS;
231     },
232 
233     /** texture max T */
234     getMaxT:function () {
235         return this._maxT;
236     },
237 
238     setMaxT:function (maxT) {
239         this._maxT = maxT;
240     },
241 
242     /**
243      * return shader program used by drawAtPoint and drawInRect
244      * @return {cc.GLProgram}
245      */
246     getShaderProgram:function () {
247         return this._shaderProgram;
248     },
249 
250     /**
251      * set shader program used by drawAtPoint and drawInRect
252      * @param {cc.GLProgram} shaderProgram
253      */
254     setShaderProgram:function (shaderProgram) {
255         this._shaderProgram = shaderProgram;
256     },
257 
258     /**
259      * whether or not the texture has their Alpha premultiplied
260      * @return {Boolean}
261      */
262     hasPremultipliedAlpha:function () {
263         return this._hasPremultipliedAlpha;
264     },
265 
266     hasMipmaps:function () {
267         return this._hasMipmaps;
268     },
269 
270     description:function () {
271         return "<cc.Texture2D | Name = " + this._name + " | Dimensions = " + this._pixelsWide + " x " + this._pixelsHigh
272             + " | Coordinates = (" + this._maxS + ", " + this._maxT + ")>";
273     },
274 
275     /**
276      * These functions are needed to create mutable textures
277      * @param {Array} data
278      */
279     releaseData:function (data) {
280         data = null;
281     },
282 
283     keepData:function (data, length) {
284         //The texture data mustn't be saved becuase it isn't a mutable texture.
285         return data;
286     },
287 
288     /**
289      * Intializes with a texture2d with data
290      * @param {Array} data
291      * @param {Number} pixelFormat
292      * @param {Number} pixelsWide
293      * @param {Number} pixelsHigh
294      * @param {cc.Size} contentSize
295      * @return {Boolean}
296      */
297     initWithData:function (data, pixelFormat, pixelsWide, pixelsHigh, contentSize) {
298         var gl = cc.renderContext;
299 
300         var bitsPerPixel = 0;
301         //Hack: bitsPerPixelForFormat returns wrong number for RGB_888 textures. See function.
302         if (pixelFormat === cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888) {
303             bitsPerPixel = 24;
304         } else {
305             bitsPerPixel = this.bitsPerPixelForFormat(pixelFormat);
306         }
307 
308         var bytesPerRow = pixelsWide * bitsPerPixel / 8;
309         if (bytesPerRow % 8 === 0) {
310             gl.pixelStorei(gl.UNPACK_ALIGNMENT, 8);
311         } else if (bytesPerRow % 4 === 0) {
312             gl.pixelStorei(gl.UNPACK_ALIGNMENT, 4);
313         } else if (bytesPerRow % 2 === 0) {
314             gl.pixelStorei(gl.UNPACK_ALIGNMENT, 2);
315         } else {
316             gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
317         }
318 
319         this._webTextureObj = gl.createTexture();
320         cc.glBindTexture2D(this);
321 
322         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
323         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
324         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
325         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
326 
327         // Specify OpenGL texture image
328         switch (pixelFormat) {
329             case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888:
330                 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, pixelsWide, pixelsHigh, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
331                 break;
332             case cc.TEXTURE_2D_PIXEL_FORMAT_RGB888:
333                 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, pixelsWide, pixelsHigh, 0, gl.RGB, gl.UNSIGNED_BYTE, data);
334                 break;
335             case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444:
336                 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, pixelsWide, pixelsHigh, 0, gl.RGBA, gl.UNSIGNED_SHORT_4_4_4_4, data);
337                 break;
338             case cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1:
339                 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, pixelsWide, pixelsHigh, 0, gl.RGBA, gl.UNSIGNED_SHORT_5_5_5_1, data);
340                 break;
341             case cc.TEXTURE_2D_PIXEL_FORMAT_RGB565:
342                 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, pixelsWide, pixelsHigh, 0, gl.RGB, gl.UNSIGNED_SHORT_5_6_5, data);
343                 break;
344             case cc.TEXTURE_2D_PIXEL_FORMAT_AI88:
345                 gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE_ALPHA, pixelsWide, pixelsHigh, 0, gl.LUMINANCE_ALPHA, gl.UNSIGNED_BYTE, data);
346                 break;
347             case cc.TEXTURE_2D_PIXEL_FORMAT_A8:
348                 gl.texImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, pixelsWide, pixelsHigh, 0, gl.ALPHA, gl.UNSIGNED_BYTE, data);
349                 break;
350             case cc.TEXTURE_2D_PIXEL_FORMAT_I8:
351                 gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, pixelsWide, pixelsHigh, 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, data);
352                 break;
353             default:
354                 throw "NSInternalInconsistencyException";
355                 break;
356         }
357 
358 
359         this._contentSize._width = contentSize.width;
360         this._contentSize._height = contentSize.height;
361         this._pixelsWide = pixelsWide;
362         this._pixelsHigh = pixelsHigh;
363         this._pixelFormat = pixelFormat;
364         this._maxS = contentSize.width / pixelsWide;
365         this._maxT = contentSize.height / pixelsHigh;
366 
367         this._hasPremultipliedAlpha = false;
368         this._hasMipmaps = false;
369         this.setShaderProgram(cc.ShaderCache.getInstance().programForKey(cc.SHADER_POSITION_TEXTURE));
370 
371         this._isLoaded = true;
372 
373         return true;
374     },
375 
376     /**
377      Drawing extensions to make it easy to draw basic quads using a CCTexture2D object.
378      These functions require gl.TEXTURE_2D and both gl.VERTEX_ARRAY and gl.TEXTURE_COORD_ARRAY client states to be enabled.
379      */
380 
381     /**
382      * draws a texture at a given point
383      * @param {cc.Point} point
384      */
385     drawAtPoint:function (point) {
386         var coordinates = [
387             0.0, this._maxT,
388             this._maxS, this._maxT,
389             0.0, 0.0,
390             this._maxS, 0.0 ];
391 
392         var width = this._pixelsWide * this._maxS,
393             height = this._pixelsHigh * this._maxT;
394 
395         var vertices = [
396             point.x, point.y, 0.0,
397             width + point.x, point.y, 0.0,
398             point.x, height + point.y, 0.0,
399             width + point.x, height + point.y, 0.0 ];
400 
401         cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_TEXCOORDS);
402         this._shaderProgram.use();
403         this._shaderProgram.setUniformsForBuiltins();
404 
405         cc.glBindTexture2D(this);
406 
407         var gl = cc.renderContext;
408         gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, vertices);
409         gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 0, coordinates);
410 
411         gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
412     },
413 
414     /**
415      * draws a texture inside a rect
416      * @param {cc.Rect} rect
417      */
418     drawInRect:function (rect) {
419         var coordinates = [
420             0.0, this._maxT,
421             this._maxS, this._maxT,
422             0.0, 0.0,
423             this._maxS, 0.0];
424 
425         var vertices = [    rect.x, rect.y, /*0.0,*/
426             rect.x + rect.width, rect.y, /*0.0,*/
427             rect.x, rect.y + rect.height, /*0.0,*/
428             rect.x + rect.width, rect.y + rect.height        /*0.0*/ ];
429 
430         cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_TEXCOORDS);
431         this._shaderProgram.use();
432         this._shaderProgram.setUniformsForBuiltins();
433 
434         cc.glBindTexture2D(this);
435 
436         var gl = cc.renderContext;
437         gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, vertices);
438         gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 0, coordinates);
439 
440         gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
441     },
442 
443     /**
444      Extensions to make it easy to create a CCTexture2D object from an image file.
445      Note that RGBA type textures will have their alpha premultiplied - use the blending mode (gl.ONE, gl.ONE_MINUS_SRC_ALPHA).
446      */
447 
448     /**
449      * Initializes a texture from a UIImage object
450      * @param uiImage
451      * @return {Boolean}
452      */
453     initWithImage:function (uiImage) {
454         if (uiImage == null) {
455             cc.log("cocos2d: cc.Texture2D. Can't create Texture. UIImage is nil");
456             return false;
457         }
458 
459         var imageWidth = uiImage.getWidth();
460         var imageHeight = uiImage.getHeight();
461 
462         var conf = cc.Configuration.getInstance();
463 
464         var maxTextureSize = conf.getMaxTextureSize();
465         if (imageWidth > maxTextureSize || imageHeight > maxTextureSize) {
466             cc.log("cocos2d: WARNING: Image (" + imageWidth + " x " + imageHeight + ") is bigger than the supported " + maxTextureSize + " x " + maxTextureSize);
467             return false;
468         }
469         this._isLoaded = true;
470 
471         // always load premultiplied images
472         return this._initPremultipliedATextureWithImage(uiImage, imageWidth, imageHeight);
473     },
474 
475     initWithElement:function (element) {
476         if (!element)
477             return;
478         this._webTextureObj = cc.renderContext.createTexture();
479         this._htmlElementObj = element;
480     },
481 
482     /**
483      * HTMLElement Object getter
484      * @return {HTMLElement}
485      */
486     getHtmlElementObj:function(){
487         return this._htmlElementObj;
488     },
489 
490     isLoaded:function () {
491         return this._isLoaded;
492     },
493 
494     handleLoadedTexture:function () {
495         this._isLoaded = true;
496         //upload image to buffer
497         var gl = cc.renderContext;
498 
499         cc.glBindTexture2D(this);
500 
501         gl.pixelStorei(gl.UNPACK_ALIGNMENT, 4);
502 
503         // Specify OpenGL texture image
504         gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this._htmlElementObj);
505 
506         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
507         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
508         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
509         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
510 
511         this.setShaderProgram(cc.ShaderCache.getInstance().programForKey(cc.SHADER_POSITION_TEXTURE));
512         cc.glBindTexture2D(null);
513 
514         var pixelsWide = this._htmlElementObj.width;
515         var pixelsHigh = this._htmlElementObj.height;
516 
517         this._pixelsWide = this._contentSize._width = pixelsWide;
518         this._pixelsHigh = this._contentSize._height = pixelsHigh;
519         this._pixelFormat = cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888;
520         this._maxS = 1;
521         this._maxT = 1;
522 
523         this._hasPremultipliedAlpha = false;
524         this._hasMipmaps = false;
525 
526         this._callLoadedEventCallbacks();
527     },
528 
529     /**
530      Extensions to make it easy to create a cc.Texture2D object from a string of text.
531      Note that the generated textures are of type A8 - use the blending mode (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA).
532      */
533     /**
534      * Initializes a texture from a string with dimensions, alignment, font name and font size (note: initWithString does not support on HTML5)
535      * @param {String} text
536      * @param {String | cc.FontDefinition} fontName or fontDefinition
537      * @param {Number} fontSize
538      * @param {cc.Size} dimensions
539      * @param {Number} hAlignment
540      * @param {Number} vAlignment
541      * @return {Boolean}
542      */
543     initWithString:function (text, fontName, fontSize, dimensions, hAlignment, vAlignment) {
544         if (arguments.length == 3) {
545             fontName = arguments[1];
546             fontSize = arguments[2];
547             dimensions = cc.size(0, 0);
548             hAlignment = cc.TEXT_ALIGNMENT_CENTER;
549             vAlignment = cc.VERTICAL_TEXT_ALIGNMENT_TOP;
550         }
551 
552         /*if (cc.ENABLE_CACHE_TEXTURE_DATA) {
553          // cache the texture data
554          cc.VolatileTexture.addStringTexture(this, text, dimensions, alignment, fontName, fontSize);
555          }*/
556 
557         var image = new cc.Image();
558         var eAlign;
559 
560         if (cc.VERTICAL_TEXT_ALIGNMENT_TOP === vAlignment) {
561             eAlign = (cc.TEXT_ALIGNMENT_CENTER === hAlignment) ? cc.ALIGN_TOP
562                 : (cc.TEXT_ALIGNMENT_LEFT === hAlignment) ? cc.ALIGN_TOP_LEFT : cc.ALIGN_TOP_RIGHT;
563         } else if (cc.VERTICAL_TEXT_ALIGNMENT_CENTER === vAlignment) {
564             eAlign = (cc.TEXT_ALIGNMENT_CENTER === hAlignment) ? cc.ALIGN_CENTER
565                 : (cc.TEXT_ALIGNMENT_LEFT === hAlignment) ? cc.ALIGN_LEFT : cc.ALIGN_RIGHT;
566         } else if (cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM === vAlignment) {
567             eAlign = (cc.TEXT_ALIGNMENT_CENTER === hAlignment) ? cc.ALIGN_BOTTOM
568                 : (cc.TEXT_ALIGNMENT_LEFT === hAlignment) ? cc.ALIGN_BOTTOM_LEFT : cc.ALIGN_BOTTOM_RIGHT;
569         } else {
570             cc.log("Not supported alignment format!");
571             return false;
572         }
573 
574         if (!image.initWithString(text, dimensions.width, dimensions.height, eAlign, fontName, fontSize))
575             return false;
576 
577         return this.initWithImage(image);
578     },
579 
580     /**
581      * Initializes a texture from a ETC file  (note: initWithETCFile does not support on HTML5)
582      * @note Compatible to Cocos2d-x
583      * @param {String} file
584      * @return {Boolean}
585      */
586     initWithETCFile:function (file) {
587         return false;
588     },
589 
590     /**
591      * Initializes a texture from a PVR file
592      * @param {String} file
593      * @return {Boolean}
594      */
595     initWithPVRFile:function (file) {
596         var ret = false;
597         // nothing to do with cc.Object.init
598 
599         var pvr = new cc.TexturePVR;
600         ret = pvr.initWithContentsOfFile(file);
601 
602         if (ret) {
603             pvr.setRetainName(true); // don't dealloc texture on release
604 
605             this._name = pvr.getName();
606             this._maxS = 1.0;
607             this._maxT = 1.0;
608             this._pixelsWide = pvr.getWidth();
609             this._pixelsHigh = pvr.getHeight();
610             this._contentSize._width = this._pixelsWide;
611             this._contentSize._height = this._pixelsHigh;
612             this._hasPremultipliedAlpha = cc.PVRHaveAlphaPremultiplied_;
613             this._pixelFormat = pvr.getFormat();
614 
615             this.setAntiAliasTexParameters();
616         } else {
617             cc.log("cocos2d: Couldn't load PVR image " + file);
618         }
619         return ret;
620     },
621 
622     /**
623      Extensions to make it easy to create a cc.Texture2D object from a PVRTC file
624      Note that the generated textures don't have their alpha premultiplied - use the blending mode (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA).
625      */
626     /**
627      * Initializes a texture from a PVRTC buffer
628      * @note compatible to cocos2d-iphone interface.
629      * @param {Array} data
630      * @param {Number} level
631      * @param {Number} bpp
632      * @param {Boolean} hasAlpha
633      * @param {Number} length
634      * @param {Number} pixelFormat
635      * @return {Boolean}
636      */
637     initWithPVRTCData:function (data, level, bpp, hasAlpha, length, pixelFormat) {
638         if (!(cc.Configuration.getInstance().supportsPVRTC())) {
639             cc.log("cocos2d: WARNING: PVRTC images is not supported.");
640             return false;
641         }
642         return true;
643     },
644 
645     /**
646      * sets the min filter, mag filter, wrap s and wrap t texture parameters. <br/>
647      * If the texture size is NPOT (non power of 2), then in can only use gl.CLAMP_TO_EDGE in gl.TEXTURE_WRAP_{S,T}.
648      * @param texParams
649      */
650     setTexParameters:function (texParams) {
651         var gl = cc.renderContext;
652 
653         cc.Assert((this._pixelsWide == cc.NextPOT(this._pixelsWide) && this._pixelsHigh == cc.NextPOT(this._pixelsHigh)) ||
654             (texParams.wrapS == gl.CLAMP_TO_EDGE && texParams.wrapT == gl.CLAMP_TO_EDGE),
655             "WebGLRenderingContext.CLAMP_TO_EDGE should be used in NPOT textures");
656 
657         cc.glBindTexture2D(this);
658         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, texParams.minFilter);
659         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, texParams.magFilter);
660         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, texParams.wrapS);
661         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, texParams.wrapT);
662 
663         //TODO
664         //VolatileTexture::setTexParameters(this, texParams);
665     },
666 
667     /**
668      * sets antialias texture parameters:              <br/>
669      *  - GL_TEXTURE_MIN_FILTER = GL_NEAREST           <br/>
670      *  - GL_TEXTURE_MAG_FILTER = GL_NEAREST
671      */
672     setAntiAliasTexParameters:function () {
673         var gl = cc.renderContext;
674 
675         cc.glBindTexture2D(this);
676         if (!this._hasMipmaps)
677             gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
678         else
679             gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
680         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
681         //TODO
682         /*#if CC_ENABLE_CACHE_TEXTURE_DATA
683          ccTexParams texParams = {m_bHasMipmaps?GL_LINEAR_MIPMAP_NEAREST:GL_LINEAR,GL_LINEAR,GL_NONE,GL_NONE};
684          VolatileTexture::setTexParameters(this, &texParams);
685          #endif*/
686     },
687 
688     /**
689      *  sets alias texture parameters:
690      *   GL_TEXTURE_MIN_FILTER = GL_NEAREST
691      *   GL_TEXTURE_MAG_FILTER = GL_NEAREST
692      */
693     setAliasTexParameters:function () {
694         var gl = cc.renderContext;
695 
696         cc.glBindTexture2D(this);
697         if (!this._hasMipmaps)
698             gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
699         else
700             gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_NEAREST);
701         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
702 
703         //TODO
704         /*#if CC_ENABLE_CACHE_TEXTURE_DATA
705          ccTexParams texParams = {m_bHasMipmaps?GL_NEAREST_MIPMAP_NEAREST:GL_NEAREST,GL_NEAREST,GL_NONE,GL_NONE};
706          VolatileTexture::setTexParameters(this, &texParams);
707          #endif*/
708     },
709 
710     /**
711      *  Generates mipmap images for the texture.<br/>
712      *  It only works if the texture size is POT (power of 2).
713      */
714     generateMipmap:function () {
715         cc.Assert(this._pixelsWide == cc.NextPOT(this._pixelsWide) && this._pixelsHigh == cc.NextPOT(this._pixelsHigh), "Mimpap texture only works in POT textures");
716 
717         cc.glBindTexture2D(this);
718         cc.renderContext.generateMipmap(cc.renderContext.TEXTURE_2D);
719         this._hasMipmaps = true;
720     },
721 
722     /**
723      * returns the pixel format.
724      * @return {String}
725      */
726     stringForFormat:function () {
727         switch (this._pixelFormat) {
728             case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888:
729                 return  "RGBA8888";
730 
731             case cc.TEXTURE_2D_PIXEL_FORMAT_RGB888:
732                 return  "RGB888";
733 
734             case cc.TEXTURE_2D_PIXEL_FORMAT_RGB565:
735                 return  "RGB565";
736 
737             case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444:
738                 return  "RGBA4444";
739 
740             case cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1:
741                 return  "RGB5A1";
742 
743             case cc.TEXTURE_2D_PIXEL_FORMAT_AI88:
744                 return  "AI88";
745 
746             case cc.TEXTURE_2D_PIXEL_FORMAT_A8:
747                 return  "A8";
748 
749             case cc.TEXTURE_2D_PIXEL_FORMAT_I8:
750                 return  "I8";
751 
752             case cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC4:
753                 return  "PVRTC4";
754 
755             case cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC2:
756                 return  "PVRTC2";
757 
758             default:
759                 cc.log("stringForFormat: " + this._pixelFormat + ", cannot give useful result, it's a unrecognized pixel format");
760                 break;
761         }
762         return "";
763     },
764 
765     /**
766      * returns the bits-per-pixel of the in-memory OpenGL texture
767      * @return {Number}
768      */
769     bitsPerPixelForFormat:function (format) {
770         format = format || this._pixelFormat;
771         switch (format) {
772             case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888:
773                 return 32;
774 
775             case cc.TEXTURE_2D_PIXEL_FORMAT_RGB888:
776                 return 32;
777 
778             case cc.TEXTURE_2D_PIXEL_FORMAT_RGB565:
779                 return 16;
780 
781             case cc.TEXTURE_2D_PIXEL_FORMAT_A8:
782                 return 8;
783 
784             case cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444:
785                 return 16;
786 
787             case cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1:
788                 return 16;
789 
790             case cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC4:
791                 return 4;
792 
793             case cc.TEXTURE_2D_PIXEL_FORMAT_PVRTC2:
794                 return 2;
795 
796             case cc.TEXTURE_2D_PIXEL_FORMAT_I8:
797                 return 8;
798 
799             case cc.TEXTURE_2D_PIXEL_FORMAT_AI88:
800                 return 16;
801 
802             default:
803                 cc.log("bitsPerPixelForFormat: " + this._pixelFormat + ", cannot give useful result, it's a illegal pixel format");
804                 return -1;
805         }
806     },
807 
808     _initPremultipliedATextureWithImage:function (uiImage, width, height) {
809         var tempData = uiImage.getData();
810         var inPixel32 = null;
811         var inPixel8 = null;
812         var outPixel16 = null;
813         var hasAlpha = uiImage.hasAlpha();
814         var imageSize = cc.size(uiImage.getWidth(), uiImage.getHeight());
815         var pixelFormat = cc.TEXTURE_2D_PIXEL_FORMAT_DEFAULT;
816         var bpp = uiImage.getBitsPerComponent();
817         var i;
818 
819         // compute pixel format
820         if (!hasAlpha) {
821             if (bpp >= 8) {
822                 pixelFormat = cc.TEXTURE_2D_PIXEL_FORMAT_RGB888;
823             } else {
824                 cc.log("cocos2d: cc.Texture2D: Using RGB565 texture since image has no alpha");
825                 pixelFormat = cc.TEXTURE_2D_PIXEL_FORMAT_RGB565;
826             }
827         }
828 
829         // Repack the pixel data into the right format
830         var length = width * height;
831 
832         if (pixelFormat == cc.TEXTURE_2D_PIXEL_FORMAT_RGB565) {
833             if (hasAlpha) {
834                 // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB"
835                 tempData = new Uint16Array(width * height);
836                 inPixel32 = uiImage.getData();
837 
838                 for (i = 0; i < length; ++i) {
839                     tempData[i] =
840                         ((((inPixel32[i] >> 0) & 0xFF) >> 3) << 11) | // R
841                             ((((inPixel32[i] >> 8) & 0xFF) >> 2) << 5) | // G
842                             ((((inPixel32[i] >> 16) & 0xFF) >> 3) << 0);    // B
843                 }
844             } else {
845                 // Convert "RRRRRRRRRGGGGGGGGBBBBBBBB" to "RRRRRGGGGGGBBBBB"
846                 tempData = new Uint16Array(width * height);
847                 inPixel8 = uiImage.getData();
848 
849                 for (i = 0; i < length; ++i) {
850                     tempData[i] =
851                         (((inPixel8[i] & 0xFF) >> 3) << 11) | // R
852                             (((inPixel8[i] & 0xFF) >> 2) << 5) | // G
853                             (((inPixel8[i] & 0xFF) >> 3) << 0);    // B
854                 }
855             }
856         } else if (pixelFormat == cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444) {
857             // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRGGGGBBBBAAAA"
858             tempData = new Uint16Array(width * height);
859             inPixel32 = uiImage.getData();
860 
861             for (i = 0; i < length; ++i) {
862                 tempData[i] =
863                     ((((inPixel32[i] >> 0) & 0xFF) >> 4) << 12) | // R
864                         ((((inPixel32[i] >> 8) & 0xFF) >> 4) << 8) | // G
865                         ((((inPixel32[i] >> 16) & 0xFF) >> 4) << 4) | // B
866                         ((((inPixel32[i] >> 24) & 0xFF) >> 4) << 0);  // A
867             }
868         } else if (pixelFormat == cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1) {
869             // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGBBBBBA"
870             tempData = new Uint16Array(width * height);
871             inPixel32 = uiImage.getData();
872 
873             for (i = 0; i < length; ++i) {
874                 tempData[i] =
875                     ((((inPixel32[i] >> 0) & 0xFF) >> 3) << 11) | // R
876                         ((((inPixel32[i] >> 8) & 0xFF) >> 3) << 6) | // G
877                         ((((inPixel32[i] >> 16) & 0xFF) >> 3) << 1) | // B
878                         ((((inPixel32[i] >> 24) & 0xFF) >> 7) << 0);  // A
879             }
880         } else if (pixelFormat == cc.TEXTURE_2D_PIXEL_FORMAT_A8) {
881             // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "AAAAAAAA"
882             tempData = new Uint8Array(width * height);
883             inPixel32 = uiImage.getData();
884 
885             for (i = 0; i < length; ++i) {
886                 tempData[i] = (inPixel32 >> 24) & 0xFF;  // A
887             }
888         }
889 
890         if (hasAlpha && pixelFormat == cc.TEXTURE_2D_PIXEL_FORMAT_RGB888) {
891             // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRRRRGGGGGGGGBBBBBBBB"
892             inPixel32 = uiImage.getData();
893             tempData = new Uint8Array(width * height * 3);
894 
895             for (i = 0; i < length; ++i) {
896                 tempData[i * 3] = (inPixel32 >> 0) & 0xFF; // R
897                 tempData[i * 3 + 1] = (inPixel32 >> 8) & 0xFF; // G
898                 tempData[i * 3 + 2] = (inPixel32 >> 16) & 0xFF; // B
899             }
900         }
901 
902         this.initWithData(tempData, pixelFormat, width, height, imageSize);
903 
904         if (tempData != uiImage.getData())
905             tempData = null;
906 
907         this._hasPremultipliedAlpha = uiImage.isPremultipliedAlpha();
908         return true;
909     },
910 
911     addLoadedEventListener: function (callback, target) {
912         if(!this._loadedEventListeners)
913             this._loadedEventListeners = [];
914         this._loadedEventListeners.push({eventCallback: callback, eventTarget: target});
915     },
916 
917     removeLoadedEventListener:function(target){
918         if(!this._loadedEventListeners)
919             return;
920         var locListeners = this._loadedEventListeners;
921         for(var i = 0;  i < locListeners.length; i++){
922             var selCallback = locListeners[i];
923             if(selCallback.eventTarget == target){
924                 locListeners.splice(i, 1);
925             }
926         }
927     },
928 
929     _callLoadedEventCallbacks: function () {
930         if(!this._loadedEventListeners)
931             return;
932         var locListeners = this._loadedEventListeners;
933         for (var i = 0, len = locListeners.length; i < len; i++) {
934             var selCallback = locListeners[i];
935             selCallback.eventCallback.call(selCallback.eventTarget, this);
936         }
937         locListeners.length = 0;
938     }
939 });
940 
941 /**
942  * <p>
943  * This class allows to easily create Canvas 2D textures from images, text or raw data.                                    <br/>
944  * The created cc.Texture2D object will always have power-of-two dimensions.                                                <br/>
945  * Depending on how you create the cc.Texture2D object, the actual image area of the texture might be smaller than the texture dimensions <br/>
946  *  i.e. "contentSize" != (pixelsWide, pixelsHigh) and (maxS, maxT) != (1.0, 1.0).                                           <br/>
947  * Be aware that the content of the generated textures will be upside-down! </p>
948  * @class
949  * @extends cc.Class
950  */
951 cc.Texture2DCanvas = cc.Class.extend(/** @lends cc.Texture2D# */{
952     _contentSize:null,
953     _isLoaded:false,
954     _htmlElementObj:null,
955 
956     _loadedEventListeners:null,
957     /*public:*/
958     ctor:function () {
959         this._contentSize = cc._sizeConst(0,0);
960         this._isLoaded = false;
961         this._htmlElementObj = null;
962     },
963 
964     /**
965      * width in pixels
966      * @return {Number}
967      */
968     getPixelsWide:function () {
969         return this._contentSize._width;
970     },
971 
972     /**
973      * hight in pixels
974      * @return {Number}
975      */
976     getPixelsHigh:function () {
977         return this._contentSize._height;
978     },
979 
980     /**
981      * content size
982      * @return {cc.Size}
983      */
984     getContentSize:function () {
985         var locScaleFactor = cc.CONTENT_SCALE_FACTOR();
986         return cc.size(this._contentSize._width / locScaleFactor, this._contentSize._height / locScaleFactor);
987     },
988 
989     getContentSizeInPixels:function () {
990         return this._contentSize;
991     },
992 
993     initWithElement:function (element) {
994         if (!element)
995             return;
996         this._htmlElementObj = element;
997     },
998 
999     /**
1000      * HTMLElement Object getter
1001      * @return {HTMLElement}
1002      */
1003     getHtmlElementObj:function(){
1004         return this._htmlElementObj;
1005     },
1006 
1007     isLoaded:function () {
1008         return this._isLoaded;
1009     },
1010 
1011     handleLoadedTexture:function () {
1012         this._isLoaded = true;
1013         var locElement =  this._htmlElementObj;
1014         this._contentSize._width = locElement.width;
1015         this._contentSize._height = locElement.height;
1016 
1017         this._callLoadedEventCallbacks();
1018     },
1019 
1020     description:function () {
1021         return "<cc.Texture2D | width = " + this._contentSize._width + " height " + this._contentSize._height+">";
1022     },
1023 
1024     /**
1025      * Initializes with a texture2d with data
1026      * @param {Array} data
1027      * @param {Number} pixelFormat
1028      * @param {Number} pixelsWide
1029      * @param {Number} pixelsHigh
1030      * @param {cc.Size} contentSize
1031      * @return {Boolean}
1032      */
1033     initWithData:function (data, pixelFormat, pixelsWide, pixelsHigh, contentSize) {
1034         //support only in WebGl rendering mode
1035         return false;
1036     },
1037 
1038     /**
1039      Extensions to make it easy to create a CCTexture2D object from an image file.
1040      Note that RGBA type textures will have their alpha premultiplied - use the blending mode (gl.ONE, gl.ONE_MINUS_SRC_ALPHA).
1041      */
1042     /**
1043      * Initializes a texture from a UIImage object
1044      * @param uiImage
1045      * @return {Boolean}
1046      */
1047     initWithImage:function (uiImage) {
1048         //support only in WebGl rendering mode
1049         return false;
1050     },
1051 
1052     /**
1053      Extensions to make it easy to create a cc.Texture2D object from a string of text.
1054      Note that the generated textures are of type A8 - use the blending mode (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA).
1055      */
1056     /**
1057      * Initializes a texture from a string with dimensions, alignment, font name and font size (note: initWithString does not support on HTML5)
1058      * @param {String} text
1059      * @param {String | cc.FontDefinition} fontName or fontDefinition
1060      * @param {Number} fontSize
1061      * @param {cc.Size} dimensions
1062      * @param {Number} hAlignment
1063      * @param {Number} vAlignment
1064      * @return {Boolean}
1065      */
1066     initWithString:function (text, fontName, fontSize, dimensions, hAlignment, vAlignment) {
1067         //support only in WebGl rendering mode
1068         return false;
1069     },
1070 
1071     releaseTexture:function () {
1072         //support only in WebGl rendering mode
1073     },
1074 
1075     /**
1076      * get WebGLTexture Object
1077      * @return {WebGLTexture}
1078      */
1079     getName:function () {
1080         //support only in WebGl rendering mode
1081         return null;
1082     },
1083 
1084     /** texture max S */
1085     getMaxS:function () {
1086         //support only in WebGl rendering mode
1087         return 1;
1088     },
1089 
1090     setMaxS:function (maxS) {
1091         //support only in WebGl rendering mode
1092     },
1093 
1094     /** texture max T */
1095     getMaxT:function () {
1096         return 1;
1097     },
1098 
1099     setMaxT:function (maxT) {
1100         //support only in WebGl rendering mode
1101     },
1102 
1103     /**
1104      * return shader program used by drawAtPoint and drawInRect
1105      * @return {cc.GLProgram}
1106      */
1107     getShaderProgram:function () {
1108         //support only in WebGl rendering mode
1109         return null;
1110     },
1111 
1112     /**
1113      * set shader program used by drawAtPoint and drawInRect
1114      * @param {cc.GLProgram} shaderProgram
1115      */
1116     setShaderProgram:function (shaderProgram) {
1117         //support only in WebGl rendering mode
1118     },
1119 
1120     /**
1121      * whether or not the texture has their Alpha premultiplied
1122      * @return {Boolean}
1123      */
1124     hasPremultipliedAlpha:function () {
1125         //support only in WebGl rendering mode
1126         return false;
1127     },
1128 
1129     hasMipmaps:function () {
1130         //support only in WebGl rendering mode
1131         return false;
1132     },
1133 
1134     /**
1135      * These functions are needed to create mutable textures
1136      * @param {Array} data
1137      */
1138     releaseData:function (data) {
1139         //support only in WebGl rendering mode
1140         data = null;
1141     },
1142 
1143     keepData:function (data, length) {
1144         //support only in WebGl rendering mode
1145         return data;
1146     },
1147 
1148     /**
1149      Drawing extensions to make it easy to draw basic quads using a CCTexture2D object.
1150      These functions require gl.TEXTURE_2D and both gl.VERTEX_ARRAY and gl.TEXTURE_COORD_ARRAY client states to be enabled.
1151      */
1152 
1153     /**
1154      * draws a texture at a given point
1155      * @param {cc.Point} point
1156      */
1157     drawAtPoint:function (point) {
1158         //support only in WebGl rendering mode
1159     },
1160 
1161     /**
1162      * draws a texture inside a rect
1163      * @param {cc.Rect} rect
1164      */
1165     drawInRect:function (rect) {
1166         //support only in WebGl rendering mode
1167     },
1168 
1169     /**
1170      * Initializes a texture from a ETC file  (note: initWithETCFile does not support on HTML5)
1171      * @note Compatible to Cocos2d-x
1172      * @param {String} file
1173      * @return {Boolean}
1174      */
1175     initWithETCFile:function (file) {
1176         //support only in WebGl rendering mode
1177         return false;
1178     },
1179 
1180     /**
1181      * Initializes a texture from a PVR file
1182      * @param {String} file
1183      * @return {Boolean}
1184      */
1185     initWithPVRFile:function (file) {
1186         //support only in WebGl rendering mode
1187         return false;
1188     },
1189 
1190     /**
1191      Extensions to make it easy to create a cc.Texture2D object from a PVRTC file
1192      Note that the generated textures don't have their alpha premultiplied - use the blending mode (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA).
1193      */
1194     /**
1195      * Initializes a texture from a PVRTC buffer
1196      * @note compatible to cocos2d-iphone interface.
1197      * @param {Array} data
1198      * @param {Number} level
1199      * @param {Number} bpp
1200      * @param {Boolean} hasAlpha
1201      * @param {Number} length
1202      * @param {Number} pixelFormat
1203      * @return {Boolean}
1204      */
1205     initWithPVRTCData:function (data, level, bpp, hasAlpha, length, pixelFormat) {
1206         //support only in WebGl rendering mode
1207         return true;
1208     },
1209 
1210     /**
1211      * sets the min filter, mag filter, wrap s and wrap t texture parameters. <br/>
1212      * If the texture size is NPOT (non power of 2), then in can only use gl.CLAMP_TO_EDGE in gl.TEXTURE_WRAP_{S,T}.
1213      * @param texParams
1214      */
1215     setTexParameters:function (texParams) {
1216         //support only in WebGl rendering mode
1217     },
1218 
1219     /**
1220      * sets antialias texture parameters:              <br/>
1221      *  - GL_TEXTURE_MIN_FILTER = GL_NEAREST           <br/>
1222      *  - GL_TEXTURE_MAG_FILTER = GL_NEAREST
1223      */
1224     setAntiAliasTexParameters:function () {
1225         //support only in WebGl rendering mode
1226     },
1227 
1228     /**
1229      *  sets alias texture parameters:
1230      *   GL_TEXTURE_MIN_FILTER = GL_NEAREST
1231      *   GL_TEXTURE_MAG_FILTER = GL_NEAREST
1232      */
1233     setAliasTexParameters:function () {
1234         //support only in WebGl rendering mode
1235     },
1236 
1237     /**
1238      *  Generates mipmap images for the texture.<br/>
1239      *  It only works if the texture size is POT (power of 2).
1240      */
1241     generateMipmap:function () {
1242         //support only in WebGl rendering mode
1243     },
1244 
1245     /**
1246      * returns the pixel format.
1247      * @return {String}
1248      */
1249     stringForFormat:function () {
1250         //support only in WebGl rendering mode
1251         return "";
1252     },
1253 
1254     /**
1255      * returns the bits-per-pixel of the in-memory OpenGL texture
1256      * @return {Number}
1257      */
1258     bitsPerPixelForFormat:function (format) {
1259         //support only in WebGl rendering mode
1260           return -1;
1261     },
1262 
1263     addLoadedEventListener:function(callback, target){
1264         if(!this._loadedEventListeners)
1265             this._loadedEventListeners = [];
1266         this._loadedEventListeners.push({eventCallback:callback, eventTarget:target});
1267     },
1268 
1269     removeLoadedEventListener:function(target){
1270         if(!this._loadedEventListeners)
1271             return;
1272         var locListeners = this._loadedEventListeners;
1273         for(var i = 0;  i < locListeners.length; i++){
1274             var selCallback = locListeners[i];
1275             if(selCallback.eventTarget == target){
1276                 locListeners.splice(i, 1);
1277             }
1278         }
1279     },
1280 
1281     _callLoadedEventCallbacks:function(){
1282         if(!this._loadedEventListeners)
1283             return;
1284         var locListeners = this._loadedEventListeners;
1285         for(var i = 0, len = locListeners.length;  i < len; i++){
1286             var selCallback = locListeners[i];
1287             selCallback.eventCallback.call(selCallback.eventTarget, this);
1288         }
1289         locListeners.length = 0;
1290     }
1291 });
1292 
1293 cc.Texture2D = cc.Browser.supportWebGL ? cc.Texture2DWebGL : cc.Texture2DCanvas;
1294 
1295 /**
1296  * <p>
1297  *     sets the default pixel format for UIImagescontains alpha channel.                                             <br/>
1298  *     If the UIImage contains alpha channel, then the options are:                                                  <br/>
1299  *      - generate 32-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888 (default one)                                <br/>
1300  *      - generate 24-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_RGB888                                                <br/>
1301  *      - generate 16-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_RGBA4444                                              <br/>
1302  *      - generate 16-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_RGB5A1                                                <br/>
1303  *      - generate 16-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_RGB565                                                <br/>
1304  *      - generate 8-bit textures: cc.TEXTURE_2D_PIXEL_FORMAT_A8 (only use it if you use just 1 color)               <br/>
1305  *                                                                                                                   <br/>
1306  *      How does it work ?                                                                                           <br/>
1307  *      - If the image is an RGBA (with Alpha) then the default pixel format will be used (it can be a 8-bit, 16-bit or 32-bit texture)      <br/>
1308  *      - If the image is an RGB (without Alpha) then an RGB565 or RGB888 texture will be used (16-bit texture)      <br/>
1309  * </p>
1310  * @param {Number} format
1311  */
1312 cc.Texture2D.setDefaultAlphaPixelFormat = function (format) {
1313     cc._defaultAlphaPixelFormat = format;
1314 };
1315 
1316 /**
1317  * returns the alpha pixel format
1318  * @return {Number}
1319  */
1320 cc.Texture2D.defaultAlphaPixelFormat = function () {
1321     return cc._defaultAlphaPixelFormat;
1322 };
1323 
1324 cc.Texture2D.getDefaultAlphaPixelFormat = function () {
1325     return cc._defaultAlphaPixelFormat;
1326 };
1327 
1328 /**
1329  * <p>
1330  *    treats (or not) PVR files as if they have alpha premultiplied.                                                <br/>
1331  *    Since it is impossible to know at runtime if the PVR images have the alpha channel premultiplied, it is       <br/>
1332  *    possible load them as if they have (or not) the alpha channel premultiplied.                                  <br/>
1333  *                                                                                                                  <br/>
1334  *    By default it is disabled.                                                                                    <br/>
1335  * </p>
1336  * @param haveAlphaPremultiplied
1337  * @constructor
1338  */
1339 cc.Texture2D.PVRImagesHavePremultipliedAlpha = function (haveAlphaPremultiplied) {
1340     cc.PVRHaveAlphaPremultiplied_ = haveAlphaPremultiplied;
1341 };
1342