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 GLES2Canvas_h
32#define GLES2Canvas_h
33
34#include "AffineTransform.h"
35#include "Color.h"
36#include "ColorSpace.h"
37#include "GraphicsTypes.h"
38#include "ImageSource.h"
39#include "LoopBlinnPathCache.h"
40#include "Texture.h"
41
42#include <wtf/HashMap.h>
43#include <wtf/Noncopyable.h>
44#include <wtf/Vector.h>
45
46namespace WebCore {
47
48class Color;
49class DrawingBuffer;
50class FloatRect;
51class GraphicsContext3D;
52class Path;
53class SharedGraphicsContext3D;
54
55class GLES2Canvas {
56    WTF_MAKE_NONCOPYABLE(GLES2Canvas);
57public:
58    GLES2Canvas(SharedGraphicsContext3D*, DrawingBuffer*, const IntSize&);
59    ~GLES2Canvas();
60
61    void fillPath(const Path&);
62    void fillRect(const FloatRect&, const Color&, ColorSpace);
63    void fillRect(const FloatRect&);
64    void clearRect(const FloatRect&);
65    void setFillColor(const Color&, ColorSpace);
66    void setAlpha(float alpha);
67    void setShadowColor(const Color&, ColorSpace);
68    void setShadowOffset(const FloatSize&);
69    void setShadowBlur(float);
70    void setShadowsIgnoreTransforms(bool);
71    void setCompositeOperation(CompositeOperator);
72    void translate(float x, float y);
73    void rotate(float angleInRadians);
74    void scale(const FloatSize&);
75    void concatCTM(const AffineTransform&);
76    void setCTM(const AffineTransform&);
77    void clipPath(const Path&);
78    void clipOut(const Path&);
79
80    void save();
81    void restore();
82
83    // non-standard functions
84    // These are not standard GraphicsContext functions, and should be pushed
85    // down into a PlatformContextGLES2 at some point.
86
87    // This version is called by the canvas->canvas draws.
88    void drawTexturedRect(unsigned texture, const IntSize& textureSize, const FloatRect& srcRect, const FloatRect& dstRect, ColorSpace, CompositeOperator);
89    // This version is called by BitmapImage::draw().
90    void drawTexturedRect(Texture*, const FloatRect& srcRect, const FloatRect& dstRect, ColorSpace, CompositeOperator);
91    // This version is called by the above, and by the software->hardware uploads.
92    void drawTexturedRect(Texture*, const FloatRect& srcRect, const FloatRect& dstRect, const AffineTransform&, float alpha, ColorSpace, CompositeOperator, bool clip);
93    Texture* createTexture(NativeImagePtr, Texture::Format, int width, int height);
94    Texture* getTexture(NativeImagePtr);
95
96    SharedGraphicsContext3D* context() const { return m_context; }
97
98    void bindFramebuffer();
99
100    DrawingBuffer* drawingBuffer() const { return m_drawingBuffer; }
101
102private:
103    void applyState();
104    void scissorClear(float x, float y, float width, float height);
105    void drawTexturedRectTile(Texture* texture, int tile, const FloatRect& srcRect, const FloatRect& dstRect, const AffineTransform&, float alpha);
106    void drawTexturedQuad(const IntSize& textureSize, const FloatRect& srcRect, const FloatRect& dstRect, const AffineTransform&, float alpha);
107    void drawTexturedQuadMitchell(const IntSize& textureSize, const FloatRect& srcRect, const FloatRect& dstRect, const AffineTransform&, float alpha);
108    void convolveRect(unsigned texture, const IntSize& textureSize, const FloatRect& srcRect, const FloatRect& dstRect, float imageIncrement[2], const float* kernel, int kernelWidth);
109
110    void tesselateAndFillPath(const Path&, const Color&);
111    void fillPathInternal(const Path&, const Color&);
112    void fillRectInternal(const FloatRect&, const Color&);
113    FloatRect flipRect(const FloatRect&);
114    void clearBorders(const FloatRect&, int width);
115    void beginShadowDraw();
116    void endShadowDraw(const FloatRect& boundingBox);
117    void beginStencilDraw(unsigned op);
118    void applyClipping(bool enable);
119    void checkGLError(const char* header);
120
121    IntSize m_size;
122
123    SharedGraphicsContext3D* m_context;
124    DrawingBuffer* m_drawingBuffer;
125
126    struct State;
127    typedef WTF::Vector<State> StateVector;
128    StateVector m_stateStack;
129    State* m_state;
130    AffineTransform m_flipMatrix;
131
132    // Members for GPU-accelerated path rendering.
133    LoopBlinnPathCache m_pathCache;
134    unsigned m_pathIndexBuffer;
135    unsigned m_pathVertexBuffer;
136};
137
138}
139
140#endif // GLES2Canvas_h
141