1 /* 2 * Copyright 2012, 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 platform_graphics_context_recording_h 27 #define platform_graphics_context_recording_h 28 29 #include "PlatformGraphicsContext.h" 30 31 #include "RecordingContextCanvasProxy.h" 32 #include "SkRefCnt.h" 33 34 namespace android { 35 class LinearAllocator; 36 } 37 38 namespace WebCore { 39 namespace GraphicsOperation { 40 class Operation; 41 } 42 43 class CanvasState; 44 class RecordingImpl; 45 class PlatformGraphicsContextSkia; 46 class RecordingData; 47 48 class Recording : public SkRefCnt { 49 public: 50 Recording() 51 : m_recording(0) 52 {} 53 ~Recording(); 54 55 void draw(SkCanvas* canvas); 56 void setRecording(RecordingImpl* impl); 57 RecordingImpl* recording() { return m_recording; } 58 59 private: 60 RecordingImpl* m_recording; 61 }; 62 63 class PlatformGraphicsContextRecording : public PlatformGraphicsContext { 64 public: 65 PlatformGraphicsContextRecording(Recording* picture); 66 virtual ~PlatformGraphicsContextRecording(); 67 virtual bool isPaintingDisabled(); 68 69 virtual SkCanvas* recordingCanvas(); 70 virtual void setTextOffset(FloatSize offset) { m_textOffset = offset; } 71 72 virtual ContextType type() { return RecordingContext; } 73 74 // State management 75 virtual void beginTransparencyLayer(float opacity); 76 virtual void endTransparencyLayer(); 77 virtual void save(); 78 virtual void restore(); 79 80 // State values 81 virtual void setAlpha(float alpha); 82 virtual void setCompositeOperation(CompositeOperator op); 83 virtual bool setFillColor(const Color& c); 84 virtual bool setFillShader(SkShader* fillShader); 85 virtual void setLineCap(LineCap cap); 86 virtual void setLineDash(const DashArray& dashes, float dashOffset); 87 virtual void setLineJoin(LineJoin join); 88 virtual void setMiterLimit(float limit); 89 virtual void setShadow(int radius, int dx, int dy, SkColor c); 90 virtual void setShouldAntialias(bool useAA); 91 virtual bool setStrokeColor(const Color& c); 92 virtual bool setStrokeShader(SkShader* strokeShader); 93 virtual void setStrokeStyle(StrokeStyle style); 94 virtual void setStrokeThickness(float f); 95 96 // Matrix operations 97 virtual void concatCTM(const AffineTransform& affine); 98 virtual void rotate(float angleInRadians); 99 virtual void scale(const FloatSize& size); 100 virtual void translate(float x, float y); 101 virtual const SkMatrix& getTotalMatrix(); 102 103 // Clipping 104 virtual void addInnerRoundedRectClip(const IntRect& rect, int thickness); 105 virtual void canvasClip(const Path& path); 106 virtual bool clip(const FloatRect& rect); 107 virtual bool clip(const Path& path); 108 virtual bool clipConvexPolygon(size_t numPoints, const FloatPoint*, bool antialias); 109 virtual bool clipOut(const IntRect& r); 110 virtual bool clipOut(const Path& p); 111 virtual bool clipPath(const Path& pathToClip, WindRule clipRule); 112 virtual SkIRect getTotalClipBounds() { return enclosingIntRect(mRecordingStateStack.last().mBounds); } 113 114 // Drawing 115 virtual void clearRect(const FloatRect& rect); 116 virtual void drawBitmapPattern(const SkBitmap& bitmap, const SkMatrix& matrix, 117 CompositeOperator compositeOp, const FloatRect& destRect); 118 virtual void drawBitmapRect(const SkBitmap& bitmap, const SkIRect* srcPtr, 119 const SkRect& dst, CompositeOperator op = CompositeSourceOver); 120 virtual void drawConvexPolygon(size_t numPoints, const FloatPoint* points, 121 bool shouldAntialias); 122 virtual void drawEllipse(const IntRect& rect); 123 virtual void drawFocusRing(const Vector<IntRect>& rects, int /* width */, 124 int /* offset */, const Color& color); 125 virtual void drawHighlightForText(const Font& font, const TextRun& run, 126 const FloatPoint& point, int h, 127 const Color& backgroundColor, ColorSpace colorSpace, 128 int from, int to, bool isActive); 129 virtual void drawLine(const IntPoint& point1, const IntPoint& point2); 130 virtual void drawLineForText(const FloatPoint& pt, float width); 131 virtual void drawLineForTextChecking(const FloatPoint& pt, float width, 132 GraphicsContext::TextCheckingLineStyle); 133 virtual void drawRect(const IntRect& rect); 134 virtual void fillPath(const Path& pathToFill, WindRule fillRule); 135 virtual void fillRect(const FloatRect& rect); 136 virtual void fillRect(const FloatRect& rect, const Color& color); 137 virtual void fillRoundedRect(const IntRect& rect, const IntSize& topLeft, 138 const IntSize& topRight, const IntSize& bottomLeft, 139 const IntSize& bottomRight, const Color& color); 140 virtual void strokeArc(const IntRect& r, int startAngle, int angleSpan); 141 virtual void strokePath(const Path& pathToStroke); 142 virtual void strokeRect(const FloatRect& rect, float lineWidth); 143 144 virtual void drawPosText(const void* text, size_t byteLength, 145 const SkPoint pos[], const SkPaint& paint); 146 virtual void drawMediaButton(const IntRect& rect, RenderSkinMediaButton::MediaButton buttonType, 147 bool translucent = false, bool drawBackground = true, 148 const IntRect& thumb = IntRect()); 149 150 float maxZoomScale() { return m_maxZoomScale; } 151 bool isEmpty() { return m_isEmpty; } 152 private: 153 154 virtual bool shadowsIgnoreTransforms() const { 155 return false; 156 } 157 158 void clipState(const FloatRect& clip); 159 void appendDrawingOperation(GraphicsOperation::Operation* operation, const FloatRect& bounds); 160 void appendStateOperation(GraphicsOperation::Operation* operation); 161 void pushStateOperation(CanvasState* canvasState); 162 void popStateOperation(); 163 void pushMatrix(); 164 void popMatrix(); 165 IntRect calculateFinalBounds(FloatRect bounds); 166 IntRect calculateCoveredBounds(FloatRect bounds); 167 android::LinearAllocator* heap(); 168 169 SkPicture* mPicture; 170 SkMatrix* mCurrentMatrix; 171 172 Recording* mRecording; 173 class RecordingState { 174 public: 175 RecordingState(CanvasState* state, const RecordingState* parent) 176 : mCanvasState(state) 177 , mHasDrawing(false) 178 , mHasClip(parent ? parent->mHasClip : false) 179 , mOpaqueTrackingDisabled(parent ? parent->mOpaqueTrackingDisabled : false) 180 , mBounds(parent ? parent->mBounds : FloatRect()) 181 {} 182 183 RecordingState(const RecordingState& other) 184 : mCanvasState(other.mCanvasState) 185 , mHasDrawing(other.mHasDrawing) 186 , mHasClip(other.mHasClip) 187 , mOpaqueTrackingDisabled(other.mOpaqueTrackingDisabled) 188 , mBounds(other.mBounds) 189 {} 190 191 void disableOpaqueTracking() { mOpaqueTrackingDisabled = true; } 192 193 void clip(const FloatRect& rect) 194 { 195 if (mHasClip) 196 mBounds.intersect(rect); 197 else { 198 mBounds = rect; 199 mHasClip = true; 200 } 201 } 202 203 CanvasState* mCanvasState; 204 bool mHasDrawing; 205 bool mHasClip; 206 bool mOpaqueTrackingDisabled; 207 FloatRect mBounds; 208 }; 209 Vector<RecordingState> mRecordingStateStack; 210 Vector<SkMatrix> mMatrixStack; 211 State* mOperationState; 212 213 float m_maxZoomScale; 214 bool m_isEmpty; 215 RecordingContextCanvasProxy m_canvasProxy; 216 FloatSize m_textOffset; 217 }; 218 219 } 220 #endif 221