Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fix bad performance of cc.generateTextureCacheForColor, the function …

…never cached any of the generated channel canvases
  • Loading branch information...
commit a8f617df13a54d308ccaacabda6292ca4d3fbb80 1 parent 3ac3adf
Ivo Wetzel authored

Showing 1 changed file with 51 additions and 16 deletions. Show diff stats Hide diff stats

  1. +51 16 cocos2d/sprite_nodes/CCSprite.js
67 cocos2d/sprite_nodes/CCSprite.js
@@ -37,33 +37,60 @@ cc.SPRITE_INDEX_NOT_INITIALIZED = -1;
37 37 * @param {HTMLImageElement} texture
38 38 * @return {Array}
39 39 */
  40 +
40 41 cc.generateTextureCacheForColor = function (texture) {
  42 +
  43 + if (texture.hasOwnProperty('channelCache')) {
  44 + return texture.channelCache;
  45 + }
  46 +
  47 + var ref = cc.generateTextureCacheForColor;
  48 +
41 49 var w = texture.width;
42 50 var h = texture.height;
43   - var textureCache = [];
  51 + var textureCache = [
  52 + document.createElement("canvas"),
  53 + document.createElement("canvas"),
  54 + document.createElement("canvas"),
  55 + ];
  56 +
  57 + textureCache[0].width = w;
  58 + textureCache[0].height = h;
  59 + textureCache[1].width = w;
  60 + textureCache[1].height = h;
  61 + textureCache[2].width = w;
  62 + textureCache[2].height = h;
  63 +
  64 + if (w > ref.canvas.width || h > ref.canvas.height) {
  65 + ref.canvas.width = w;
  66 + ref.canvas.height = h;
44 67
45   - var canvas = document.createElement("canvas");
46   - canvas.width = w;
47   - canvas.height = h;
  68 + } else {
  69 + ref.canvas.width = ref.canvas.width;
  70 + ref.canvas.height = ref.canvas.height;
  71 + }
48 72
49   - var ctx = canvas.getContext("2d");
  73 + var ctx = ref.canvas.getContext("2d");
50 74 ctx.drawImage(texture, 0, 0);
51 75
52   - var tempCanvas = document.createElement("canvas");
53   - tempCanvas.width = w;
54   - tempCanvas.height = h;
55   - var tempCtx = tempCanvas.getContext('2d');
  76 + if (w > ref.tempCanvas.width || h > ref.tempCanvas.height) {
  77 + ref.tempCanvas.width = w;
  78 + ref.tempCanvas.height = h;
  79 +
  80 + } else {
  81 + ref.tempCanvas.width = ref.tempCanvas.width;
  82 + ref.tempCanvas.height = ref.tempCanvas.height;
  83 + }
56 84
57 85 var pixels = ctx.getImageData(0, 0, w, h).data;
58 86
59 87 for (var rgbI = 0; rgbI < 3; rgbI++) {
60   - var cacheCanvas = document.createElement("canvas");
61   - cacheCanvas.width = w;
62   - cacheCanvas.height = h;
63   - var cacheCtx = cacheCanvas.getContext('2d');
64 88
65   - tempCtx.drawImage(texture, 0, 0);
66   - var to = tempCtx.getImageData(0, 0, w, h);
  89 + var cacheCtx = textureCache[rgbI].getContext('2d');
  90 + cacheCtx.getImageData(0, 0, w, h).data;
  91 + ref.tempCtx.drawImage(texture, 0, 0);
  92 +
  93 + var to = ref.tempCtx.getImageData(0, 0, w, h);
67 94 var toData = to.data;
68 95
69 96 for (var i = 0; i < pixels.length; i += 4) {
@@ -72,12 +99,20 @@ cc.generateTextureCacheForColor = function (texture) {
72 99 toData[i + 2] = (rgbI === 2) ? pixels[i + 2] : 0;
73 100 toData[i + 3] = pixels[i + 3];
74 101 }
  102 +
75 103 cacheCtx.putImageData(to, 0, 0);
76   - textureCache.push(cacheCanvas);
  104 +
77 105 }
  106 +
  107 + texture.channelCache = textureCache;
78 108 return textureCache;
79 109 };
80 110
  111 +cc.generateTextureCacheForColor.canvas = document.createElement('canvas');
  112 +cc.generateTextureCacheForColor.tempCanvas = document.createElement('canvas');
  113 +cc.generateTextureCacheForColor.tempCtx = cc.generateTextureCacheForColor.tempCanvas.getContext('2d');
  114 +
  115 +
81 116 cc.generateTintImage2 = function (texture, color, rect) {
82 117 if (!rect) {
83 118 rect = cc.rect(0, 0, texture.width, texture.height);

0 comments on commit a8f617d

Please sign in to comment.
Something went wrong with that request. Please try again.