1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 18 package com.replica.replicaisland; 19 20 /** 21 * This class manages drawable objects that have short lifetimes (one or two frames). It provides 22 * type-specific allocator functions and a type-insensitive release function. This class manages 23 * pools of objects so no actual allocations occur after bootstrap. 24 */ 25 public class DrawableFactory extends BaseObject { 26 private final static int BITMAP_POOL_SIZE = 768; 27 28 private DrawableBitmapPool mBitmapPool; 29 private ScrollableBitmapPool mScrollableBitmapPool; 30 private TiledBackgroundVertexGridPool mTiledBackgroundVertexGridPool; 31 32 // This class wraps several object pools and provides a type-sensitive release function. DrawableFactory()33 public DrawableFactory() { 34 super(); 35 mBitmapPool = new DrawableBitmapPool(BITMAP_POOL_SIZE); 36 mTiledBackgroundVertexGridPool = new TiledBackgroundVertexGridPool(); 37 mScrollableBitmapPool = new ScrollableBitmapPool(); 38 } 39 40 @Override reset()41 public void reset() { 42 } 43 allocateDrawableBitmap()44 public DrawableBitmap allocateDrawableBitmap() { 45 return mBitmapPool.allocate(); 46 } 47 allocateTiledBackgroundVertexGrid()48 public TiledBackgroundVertexGrid allocateTiledBackgroundVertexGrid() { 49 return mTiledBackgroundVertexGridPool.allocate(); 50 } 51 allocateScrollableBitmap()52 public ScrollableBitmap allocateScrollableBitmap() { 53 return mScrollableBitmapPool.allocate(); 54 } 55 release(DrawableObject object)56 public void release(DrawableObject object) { 57 ObjectPool pool = object.getParentPool(); 58 if (pool != null) { 59 pool.release(object); 60 } 61 // Objects with no pool weren't created by this factory. Ignore them. 62 } 63 64 private class DrawableBitmapPool extends TObjectPool<DrawableBitmap> { 65 DrawableBitmapPool(int size)66 public DrawableBitmapPool(int size) { 67 super(size); 68 } 69 70 @Override reset()71 public void reset() { 72 } 73 74 @Override fill()75 protected void fill() { 76 int size = getSize(); 77 for (int x = 0; x < size; x++) { 78 DrawableBitmap entry = new DrawableBitmap(null, 0, 0); 79 entry.setParentPool(this); 80 getAvailable().add(entry); 81 } 82 } 83 84 @Override release(Object entry)85 public void release(Object entry) { 86 ((DrawableBitmap)entry).reset(); 87 super.release(entry); 88 } 89 90 @Override allocate()91 public DrawableBitmap allocate() { 92 DrawableBitmap result = super.allocate(); 93 ContextParameters params = sSystemRegistry.contextParameters; 94 if (result != null && params != null) { 95 result.setViewSize(params.gameWidth, params.gameHeight); 96 } 97 return result; 98 } 99 } 100 101 private class ScrollableBitmapPool extends TObjectPool<ScrollableBitmap> { 102 ScrollableBitmapPool()103 public ScrollableBitmapPool() { 104 super(); 105 } 106 107 @Override reset()108 public void reset() { 109 } 110 111 @Override fill()112 protected void fill() { 113 int size = getSize(); 114 for (int x = 0; x < size; x++) { 115 ScrollableBitmap entry = new ScrollableBitmap(null, 0, 0); 116 entry.setParentPool(this); 117 getAvailable().add(entry); 118 } 119 } 120 121 @Override release(Object entry)122 public void release(Object entry) { 123 ((ScrollableBitmap)entry).reset(); 124 super.release(entry); 125 } 126 127 128 } 129 130 private class TiledBackgroundVertexGridPool extends TObjectPool<TiledBackgroundVertexGrid> { 131 TiledBackgroundVertexGridPool()132 public TiledBackgroundVertexGridPool() { 133 super(); 134 } 135 136 @Override reset()137 public void reset() { 138 } 139 140 @Override fill()141 protected void fill() { 142 int size = getSize(); 143 for (int x = 0; x < size; x++) { 144 TiledBackgroundVertexGrid entry = new TiledBackgroundVertexGrid(); 145 entry.setParentPool(this); 146 getAvailable().add(entry); 147 } 148 } 149 150 @Override release(Object entry)151 public void release(Object entry) { 152 TiledBackgroundVertexGrid bg = (TiledBackgroundVertexGrid)entry; 153 bg.reset(); 154 super.release(entry); 155 } 156 157 } 158 } 159