1 /* 2 * Copyright 2010, The Android Open Source Project 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #ifndef BaseTileTexture_h 27 #define BaseTileTexture_h 28 29 #include "DoubleBufferedTexture.h" 30 #include "GLWebViewState.h" 31 #include "TextureOwner.h" 32 #include "TilePainter.h" 33 #include <SkBitmap.h> 34 35 class SkCanvas; 36 37 namespace WebCore { 38 39 class BaseTile; 40 41 class TextureTileInfo { 42 public: 43 TextureTileInfo() 44 : m_x(-1) 45 , m_y(-1) 46 , m_layerId(-1) 47 , m_scale(0) 48 , m_texture(0) 49 , m_painter(0) 50 , m_picture(0) 51 , m_inverted(false) 52 { 53 } 54 int m_x; 55 int m_y; 56 int m_layerId; 57 float m_scale; 58 TextureInfo* m_texture; 59 TilePainter* m_painter; 60 unsigned int m_picture; 61 bool m_inverted; 62 }; 63 64 // DoubleBufferedTexture using a SkBitmap as backing mechanism 65 class BaseTileTexture : public DoubleBufferedTexture { 66 public: 67 // This object is to be constructed on the consumer's thread and must have 68 // a width and height greater than 0. 69 BaseTileTexture(uint32_t w, uint32_t h); 70 virtual ~BaseTileTexture(); 71 72 // these functions override their parent 73 virtual TextureInfo* producerLock(); 74 virtual void producerRelease(); 75 virtual void producerReleaseAndSwap(); 76 77 // updates the texture with current bitmap and releases (and if needed also 78 // swaps) the texture. 79 virtual void producerUpdate(TextureInfo* textureInfo, const SkBitmap& bitmap); 80 81 // allows consumer thread to assign ownership of the texture to the tile. It 82 // returns false if ownership cannot be transferred because the tile is busy 83 bool acquire(TextureOwner* owner, bool force = false); 84 bool release(TextureOwner* owner); 85 86 // removes Tile->Texture, and Texture->Tile links to fully discard the texture 87 void releaseAndRemoveFromTile(); 88 89 // set the texture owner if not busy. Return false if busy, true otherwise. 90 bool setOwner(TextureOwner* owner, bool force = false); 91 92 // private member accessor functions 93 TextureOwner* owner() { return m_owner; } // only used by the consumer thread 94 95 bool busy(); 96 void setNotBusy(); 97 98 const SkSize& getSize() const { return m_size; } 99 100 void setTile(TextureInfo* info, int x, int y, float scale, 101 TilePainter* painter, unsigned int pictureCount); 102 bool readyFor(BaseTile* baseTile); 103 float scale(); 104 105 // OpenGL ID of backing texture, 0 when not allocated 106 GLuint m_ownTextureId; 107 // these are used for dynamically (de)allocating backing graphics memory 108 void requireGLTexture(); 109 void discardGLTexture(); 110 111 void setOwnTextureTileInfoFromQueue(const TextureTileInfo* info); 112 113 protected: 114 HashMap<SharedTexture*, TextureTileInfo*> m_texturesInfo; 115 116 private: 117 void destroyTextures(SharedTexture** textures); 118 TextureTileInfo m_ownTextureTileInfo; 119 120 SkSize m_size; 121 SkBitmap::Config m_config; 122 123 // BaseTile owning the texture, only modified by UI thread 124 TextureOwner* m_owner; 125 126 // This values signals that the texture is currently in use by the consumer. 127 // This allows us to prevent the owner of the texture from changing while the 128 // consumer is holding a lock on the texture. 129 bool m_busy; 130 // We mutex protect the reads/writes of m_busy to ensure that we are reading 131 // the most up-to-date value even across processors in an SMP system. 132 android::Mutex m_busyLock; 133 // We use this condition variable to signal that the texture 134 // is not busy anymore 135 android::Condition m_busyCond; 136 }; 137 138 } // namespace WebCore 139 140 #endif // BaseTileTexture_h 141