1 /* 2 * Copyright (c) 2010, Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #ifndef SharedGraphicsContext3D_h 32 #define SharedGraphicsContext3D_h 33 34 #include "GraphicsContext3D.h" 35 #include "GraphicsTypes.h" 36 #include "ImageSource.h" 37 #include "Texture.h" 38 39 #include <wtf/HashMap.h> 40 #include <wtf/HashSet.h> 41 #include <wtf/OwnArrayPtr.h> 42 #include <wtf/OwnPtr.h> 43 #include <wtf/RefCounted.h> 44 #include <wtf/RefPtr.h> 45 46 #if ENABLE(SKIA_GPU) 47 class GrContext; 48 #endif 49 50 namespace WebCore { 51 52 class AffineTransform; 53 class BicubicShader; 54 class Color; 55 class ConvolutionShader; 56 class DrawingBuffer; 57 class FloatRect; 58 class HostWindow; 59 class IntSize; 60 class LoopBlinnSolidFillShader; 61 class SolidFillShader; 62 class TexShader; 63 64 typedef HashMap<NativeImagePtr, RefPtr<Texture> > TextureHashMap; 65 66 class SharedGraphicsContext3D : public RefCounted<SharedGraphicsContext3D> { 67 public: 68 static PassRefPtr<SharedGraphicsContext3D> create(HostWindow*); 69 ~SharedGraphicsContext3D(); 70 71 // Functions that delegate directly to GraphicsContext3D, with caching 72 void makeContextCurrent(); 73 void bindBuffer(GC3Denum target, Platform3DObject); 74 void bindFramebuffer(Platform3DObject framebuffer); 75 void bufferData(GC3Denum target, GC3Dsizeiptr, GC3Denum usage); 76 void bufferData(GC3Denum target, GC3Dsizeiptr, const void* data, GC3Denum usage); 77 void bufferSubData(GC3Denum target, GC3Dintptr offset, GC3Dsizeiptr, const void* data); 78 void setViewport(const IntSize&); 79 void scissor(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height); 80 void enable(GC3Denum capacity); 81 void disable(GC3Denum capacity); 82 void clearColor(const Color&); 83 void clear(GC3Dbitfield mask); 84 void drawArrays(GC3Denum mode, GC3Dint first, GC3Dsizei count); 85 GC3Denum getError(); 86 void getIntegerv(GC3Denum pname, GC3Dint* value); 87 void flush(); 88 89 Platform3DObject createBuffer(); 90 Platform3DObject createFramebuffer(); 91 Platform3DObject createTexture(); 92 93 void deleteFramebuffer(Platform3DObject framebuffer); 94 void deleteTexture(Platform3DObject texture); 95 96 void framebufferTexture2D(GC3Denum target, GC3Denum attachment, GC3Denum textarget, Platform3DObject, GC3Dint level); 97 void texParameteri(GC3Denum target, GC3Denum pname, GC3Dint param); 98 bool texImage2D(GC3Denum target, GC3Dint level, GC3Denum internalformat, GC3Dsizei width, GC3Dsizei height, GC3Dint border, GC3Denum format, GC3Denum type, const void* pixels); 99 void texSubImage2D(GC3Denum target, GC3Dint level, GC3Dint xoffset, GC3Dint yoffset, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, const void* pixels); 100 101 void readPixels(GC3Dint x, GC3Dint y, GC3Dsizei width, GC3Dsizei height, GC3Denum format, GC3Denum type, void* data); 102 103 bool paintsIntoCanvasBuffer() const; 104 105 // Shared logic for canvas 2d 106 void applyCompositeOperator(CompositeOperator); 107 void enableStencil(bool enable); 108 void useQuadVertices(); 109 110 void useFillSolidProgram(const AffineTransform&, const Color&); 111 void useTextureProgram(const AffineTransform&, const AffineTransform&, float alpha); 112 void useBicubicProgram(const AffineTransform&, const AffineTransform&, const float coefficients[16], const float imageIncrement[2], float alpha); 113 void useConvolutionProgram(const AffineTransform&, const AffineTransform& texTransform, const float* kernel, int kernelWidth, float imageIncrement[2]); 114 115 void setActiveTexture(GC3Denum textureUnit); 116 void bindTexture(GC3Denum target, Platform3DObject texture); 117 118 bool supportsBGRA(); 119 120 // Creates a texture associated with the given image. Is owned by this context's 121 // TextureHashMap. 122 Texture* createTexture(NativeImagePtr, Texture::Format, int width, int height); 123 Texture* getTexture(NativeImagePtr); 124 125 // Multiple SharedGraphicsContext3D can exist in a single process (one per compositing context) and the same 126 // NativeImagePtr may be uploaded as a texture into all of them. This function removes uploaded textures 127 // for a given NativeImagePtr in all contexts. 128 static void removeTexturesFor(NativeImagePtr); 129 130 // Creates a texture that is not associated with any image. The caller takes ownership of 131 // the texture. 132 PassRefPtr<Texture> createTexture(Texture::Format, int width, int height); 133 graphicsContext3D()134 GraphicsContext3D* graphicsContext3D() const { return m_context.get(); } 135 136 // Members for GPU-accelerated path rendering. 137 static bool useLoopBlinnForPathRendering(); 138 void useLoopBlinnInteriorProgram(unsigned vertexOffset, const AffineTransform&, const Color&); 139 void useLoopBlinnExteriorProgram(unsigned vertexOffset, unsigned klmOffset, const AffineTransform&, const Color&); 140 DrawingBuffer* getOffscreenBuffer(unsigned index, const IntSize&); 141 142 #if ENABLE(SKIA_GPU) 143 GrContext* grContext(); 144 #endif 145 146 private: 147 SharedGraphicsContext3D(PassRefPtr<GraphicsContext3D>, PassOwnPtr<SolidFillShader>, PassOwnPtr<TexShader>, PassOwnPtr<BicubicShader>, PassOwnArrayPtr<OwnPtr<ConvolutionShader> >); 148 149 // Used to implement removeTexturesFor(), see the comment above. 150 static HashSet<SharedGraphicsContext3D*>* allContexts(); 151 void removeTextureFor(NativeImagePtr); 152 153 RefPtr<GraphicsContext3D> m_context; 154 bool m_bgraSupported; 155 156 unsigned m_quadVertices; 157 158 OwnPtr<SolidFillShader> m_solidFillShader; 159 OwnPtr<TexShader> m_texShader; 160 OwnPtr<BicubicShader> m_bicubicShader; 161 OwnArrayPtr<OwnPtr<ConvolutionShader> > m_convolutionShaders; 162 163 TextureHashMap m_textures; 164 165 // Members for GPU-accelerated path rendering. 166 // FIXME: support more kinds of fill types for paths. 167 OwnPtr<LoopBlinnSolidFillShader> m_loopBlinnInteriorShader; 168 OwnPtr<LoopBlinnSolidFillShader> m_loopBlinnExteriorShader; 169 bool m_oesStandardDerivativesSupported; 170 171 WTF::Vector<RefPtr<DrawingBuffer> > m_offscreenBuffers; 172 173 #if ENABLE(SKIA_GPU) 174 GrContext* m_grContext; 175 #endif 176 }; 177 178 const float cMaxSigma = 4.0f; 179 const int cMaxKernelWidth = 25; 180 181 } // namespace WebCore 182 183 #endif // SharedGraphicsContext3D_h 184