1 /* 2 * Copyright (C) 2012 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 package com.android.gallery3d.ui; 18 19 import com.android.gallery3d.ui.GLRoot.OnGLIdleListener; 20 21 import java.util.ArrayDeque; 22 23 public class TextureUploader implements OnGLIdleListener { 24 private static final int INIT_CAPACITY = 64; 25 private static final int QUOTA_PER_FRAME = 1; 26 27 private final ArrayDeque<UploadedTexture> mFgTextures = 28 new ArrayDeque<UploadedTexture>(INIT_CAPACITY); 29 private final ArrayDeque<UploadedTexture> mBgTextures = 30 new ArrayDeque<UploadedTexture>(INIT_CAPACITY); 31 private final GLRoot mGLRoot; 32 private transient boolean mIsQueued = false; 33 TextureUploader(GLRoot root)34 public TextureUploader(GLRoot root) { 35 mGLRoot = root; 36 } 37 clear()38 public synchronized void clear() { 39 while (!mFgTextures.isEmpty()) { 40 mFgTextures.pop().setIsUploading(false); 41 } 42 while (!mBgTextures.isEmpty()) { 43 mBgTextures.pop().setIsUploading(false); 44 } 45 } 46 47 // caller should hold synchronized on "this" queueSelfIfNeed()48 private void queueSelfIfNeed() { 49 if (mIsQueued) return; 50 mIsQueued = true; 51 mGLRoot.addOnGLIdleListener(this); 52 } 53 addBgTexture(UploadedTexture t)54 public synchronized void addBgTexture(UploadedTexture t) { 55 if (t.isContentValid()) return; 56 mBgTextures.addLast(t); 57 t.setIsUploading(true); 58 queueSelfIfNeed(); 59 } 60 addFgTexture(UploadedTexture t)61 public synchronized void addFgTexture(UploadedTexture t) { 62 if (t.isContentValid()) return; 63 mFgTextures.addLast(t); 64 t.setIsUploading(true); 65 queueSelfIfNeed(); 66 } 67 upload(GLCanvas canvas, ArrayDeque<UploadedTexture> deque, int uploadQuota, boolean isBackground)68 private int upload(GLCanvas canvas, ArrayDeque<UploadedTexture> deque, 69 int uploadQuota, boolean isBackground) { 70 while (uploadQuota > 0) { 71 UploadedTexture t; 72 synchronized (this) { 73 if (deque.isEmpty()) break; 74 t = deque.removeFirst(); 75 t.setIsUploading(false); 76 if (t.isContentValid()) continue; 77 78 // this has to be protected by the synchronized block 79 // to prevent the inner bitmap get recycled 80 t.updateContent(canvas); 81 } 82 83 // It will took some more time for a texture to be drawn for 84 // the first time. 85 // Thus, when scrolling, if a new column appears on screen, 86 // it may cause a UI jank even these textures are uploaded. 87 if (isBackground) t.draw(canvas, 0, 0); 88 --uploadQuota; 89 } 90 return uploadQuota; 91 } 92 93 @Override onGLIdle(GLCanvas canvas, boolean renderRequested)94 public boolean onGLIdle(GLCanvas canvas, boolean renderRequested) { 95 int uploadQuota = QUOTA_PER_FRAME; 96 uploadQuota = upload(canvas, mFgTextures, uploadQuota, false); 97 if (uploadQuota < QUOTA_PER_FRAME) mGLRoot.requestRender(); 98 upload(canvas, mBgTextures, uploadQuota, true); 99 synchronized (this) { 100 mIsQueued = !mFgTextures.isEmpty() || !mBgTextures.isEmpty(); 101 return mIsQueued; 102 } 103 } 104 } 105