Merge mozilla-central to autoland. a=merge CLOSED TREE
[gecko.git] / dom / canvas / DrawTargetWebgl.h
blob955ccaabf0f708857d2931696d9635804b12750f
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef _MOZILLA_GFX_DRAWTARGETWEBGL_H
8 #define _MOZILLA_GFX_DRAWTARGETWEBGL_H
10 #include "GLTypes.h"
11 #include "mozilla/Array.h"
12 #include "mozilla/gfx/2D.h"
13 #include "mozilla/gfx/PathSkia.h"
14 #include "mozilla/LinkedList.h"
15 #include "mozilla/WeakPtr.h"
16 #include "mozilla/ThreadLocal.h"
17 #include "mozilla/ipc/SharedMemoryBasic.h"
18 #include "mozilla/layers/LayersTypes.h"
20 #include <vector>
22 namespace WGR {
23 struct OutputVertex;
24 struct PathBuilder;
25 } // namespace WGR
27 namespace mozilla {
29 class WebGLContext;
30 class WebGLBuffer;
31 class WebGLFramebuffer;
32 class WebGLProgram;
33 class WebGLRenderbuffer;
34 class WebGLTexture;
35 class WebGLUniformLocation;
36 class WebGLVertexArray;
38 namespace gl {
39 class GLContext;
40 } // namespace gl
42 namespace layers {
43 class RemoteTextureOwnerClient;
44 } // namespace layers
46 namespace gfx {
48 class DataSourceSurface;
49 class DrawTargetSkia;
50 class DrawTargetWebgl;
51 class PathSkia;
52 class SourceSurfaceSkia;
53 class SourceSurfaceWebgl;
55 class TextureHandle;
56 class SharedTexture;
57 class SharedTextureHandle;
58 class StandaloneTexture;
59 class GlyphCache;
60 class PathCache;
61 struct PathVertexRange;
63 // SharedContextWebgl stores most of the actual WebGL state that may be used by
64 // any number of DrawTargetWebgl's that use it. Foremost, it holds the actual
65 // WebGL client context, programs, and buffers for mapping to WebGL.
66 // Secondarily, it holds shared caches for surfaces, glyphs, paths, and
67 // shadows so that each DrawTargetWebgl does not require its own cache. It is
68 // important that SetTarget is called to install the current DrawTargetWebgl
69 // before actually using the SharedContext, as the individual framebuffers
70 // and viewport are still maintained in DrawTargetWebgl itself.
71 class SharedContextWebgl : public mozilla::RefCounted<SharedContextWebgl>,
72 public mozilla::SupportsWeakPtr {
73 friend class DrawTargetWebgl;
74 friend class SourceSurfaceWebgl;
75 friend class TextureHandle;
76 friend class SharedTextureHandle;
77 friend class StandaloneTexture;
79 public:
80 MOZ_DECLARE_REFCOUNTED_TYPENAME(SharedContextWebgl)
82 static already_AddRefed<SharedContextWebgl> Create();
84 ~SharedContextWebgl();
86 gl::GLContext* GetGLContext();
88 void EnterTlsScope();
89 void ExitTlsScope();
91 bool IsContextLost() const;
93 void OnMemoryPressure();
95 private:
96 SharedContextWebgl();
98 WeakPtr<DrawTargetWebgl> mCurrentTarget;
99 IntSize mViewportSize;
100 // The current integer-aligned scissor rect.
101 IntRect mClipRect;
102 // The current fractional AA'd clip rect bounds.
103 Rect mClipAARect;
105 RefPtr<WebGLContext> mWebgl;
107 // Avoid spurious state changes by caching last used state.
108 RefPtr<WebGLProgram> mLastProgram;
109 RefPtr<WebGLTexture> mLastTexture;
110 RefPtr<WebGLTexture> mLastClipMask;
112 // WebGL shader resources
113 RefPtr<WebGLBuffer> mPathVertexBuffer;
114 RefPtr<WebGLVertexArray> mPathVertexArray;
115 // The current insertion offset into the GPU path buffer.
116 uint32_t mPathVertexOffset = 0;
117 // The maximum size of the GPU path buffer.
118 uint32_t mPathVertexCapacity = 0;
119 // The maximum supported type complexity of a GPU path.
120 uint32_t mPathMaxComplexity = 0;
121 // Whether to accelerate stroked paths with AAStroke.
122 bool mPathAAStroke = true;
123 // Whether to accelerate stroked paths with WGR.
124 bool mPathWGRStroke = false;
126 WGR::PathBuilder* mWGRPathBuilder = nullptr;
127 // Temporary buffer for generating WGR output into.
128 UniquePtr<WGR::OutputVertex[]> mWGROutputBuffer;
130 RefPtr<WebGLProgram> mSolidProgram;
131 Maybe<uint32_t> mSolidProgramViewport;
132 Maybe<uint32_t> mSolidProgramAA;
133 Maybe<uint32_t> mSolidProgramTransform;
134 Maybe<uint32_t> mSolidProgramColor;
135 Maybe<uint32_t> mSolidProgramClipMask;
136 Maybe<uint32_t> mSolidProgramClipBounds;
137 RefPtr<WebGLProgram> mImageProgram;
138 Maybe<uint32_t> mImageProgramViewport;
139 Maybe<uint32_t> mImageProgramAA;
140 Maybe<uint32_t> mImageProgramTransform;
141 Maybe<uint32_t> mImageProgramTexMatrix;
142 Maybe<uint32_t> mImageProgramTexBounds;
143 Maybe<uint32_t> mImageProgramColor;
144 Maybe<uint32_t> mImageProgramSwizzle;
145 Maybe<uint32_t> mImageProgramSampler;
146 Maybe<uint32_t> mImageProgramClipMask;
147 Maybe<uint32_t> mImageProgramClipBounds;
149 struct SolidProgramUniformState {
150 Maybe<Array<float, 2>> mViewport;
151 Maybe<Array<float, 1>> mAA;
152 Maybe<Array<float, 6>> mTransform;
153 Maybe<Array<float, 4>> mColor;
154 Maybe<Array<float, 4>> mClipBounds;
155 } mSolidProgramUniformState;
157 struct ImageProgramUniformState {
158 Maybe<Array<float, 2>> mViewport;
159 Maybe<Array<float, 1>> mAA;
160 Maybe<Array<float, 6>> mTransform;
161 Maybe<Array<float, 6>> mTexMatrix;
162 Maybe<Array<float, 4>> mTexBounds;
163 Maybe<Array<float, 4>> mColor;
164 Maybe<Array<float, 1>> mSwizzle;
165 Maybe<Array<float, 4>> mClipBounds;
166 } mImageProgramUniformState;
168 // Scratch framebuffer used to wrap textures for miscellaneous utility ops.
169 RefPtr<WebGLFramebuffer> mScratchFramebuffer;
170 // Buffer filled with zero data for initializing textures.
171 RefPtr<WebGLBuffer> mZeroBuffer;
172 size_t mZeroSize = 0;
173 // 1x1 texture with solid white mask for disabling clipping
174 RefPtr<WebGLTexture> mNoClipMask;
176 uint32_t mMaxTextureSize = 0;
177 bool mRasterizationTruncates = false;
179 // The current blending operation.
180 CompositionOp mLastCompositionOp = CompositionOp::OP_SOURCE;
181 // The constant blend color used for the blending operation.
182 Maybe<DeviceColor> mLastBlendColor;
184 // The cached scissor state. Operations that rely on scissor state should
185 // take care to enable or disable the cached scissor state as necessary.
186 bool mScissorEnabled = false;
187 IntRect mLastScissor = {-1, -1, -1, -1};
189 // A most-recently-used list of allocated texture handles.
190 LinkedList<RefPtr<TextureHandle>> mTextureHandles;
191 size_t mNumTextureHandles = 0;
192 // User data key linking a SourceSurface with its TextureHandle.
193 UserDataKey mTextureHandleKey = {0};
194 // User data key linking a SourceSurface with its shadow blur TextureHandle.
195 UserDataKey mShadowTextureKey = {0};
196 // User data key linking a ScaledFont with its GlyphCache.
197 UserDataKey mGlyphCacheKey = {0};
198 // List of all GlyphCaches currently allocated to fonts.
199 LinkedList<GlyphCache> mGlyphCaches;
200 // Cache of rasterized paths.
201 UniquePtr<PathCache> mPathCache;
202 // Collection of allocated shared texture pages that may be shared amongst
203 // many handles.
204 std::vector<RefPtr<SharedTexture>> mSharedTextures;
205 // Collection of allocated standalone textures that have a single assigned
206 // handle.
207 std::vector<RefPtr<StandaloneTexture>> mStandaloneTextures;
208 size_t mUsedTextureMemory = 0;
209 size_t mTotalTextureMemory = 0;
210 // The total reserved memory for empty texture pages that are kept around
211 // for future allocations.
212 size_t mEmptyTextureMemory = 0;
213 // A memory pressure event may signal from another thread that caches should
214 // be cleared if possible.
215 Atomic<bool> mShouldClearCaches;
216 // The total number of DrawTargetWebgls using this shared context.
217 size_t mDrawTargetCount = 0;
218 // Whether we are inside a scoped usage of TLS MakeCurrent, and what previous
219 // value to restore it to when exiting the scope.
220 Maybe<bool> mTlsScope;
222 // Cached unit circle path
223 RefPtr<Path> mUnitCirclePath;
225 bool Initialize();
226 bool CreateShaders();
227 void ResetPathVertexBuffer(bool aChanged = true);
229 void BlendFunc(GLenum aSrcFactor, GLenum aDstFactor);
230 void SetBlendState(CompositionOp aOp,
231 const Maybe<DeviceColor>& aColor = Nothing());
233 void SetClipRect(const Rect& aClipRect);
234 void SetClipRect(const IntRect& aClipRect) { SetClipRect(Rect(aClipRect)); }
235 bool SetClipMask(const RefPtr<WebGLTexture>& aTex);
236 bool SetNoClipMask();
237 bool HasClipMask() const {
238 return mLastClipMask && mLastClipMask != mNoClipMask;
241 Maybe<uint32_t> GetUniformLocation(const RefPtr<WebGLProgram>& prog,
242 const std::string& aName) const;
244 template <class T, size_t N>
245 void UniformData(GLenum aFuncElemType, const Maybe<uint32_t>& aLoc,
246 const Array<T, N>& aData);
248 // Avoids redundant UniformData calls by caching the previously set value.
249 template <class T, size_t N>
250 void MaybeUniformData(GLenum aFuncElemType, const Maybe<uint32_t>& aLoc,
251 const Array<T, N>& aData, Maybe<Array<T, N>>& aCached);
253 bool IsCurrentTarget(DrawTargetWebgl* aDT) const {
254 return aDT == mCurrentTarget;
256 bool SetTarget(DrawTargetWebgl* aDT);
258 // Reset the current target.
259 void ClearTarget() { mCurrentTarget = nullptr; }
260 // Reset the last used texture to force binding next use.
261 void ClearLastTexture(bool aFullClear = false);
263 bool SupportsPattern(const Pattern& aPattern);
265 void EnableScissor(const IntRect& aRect);
266 void DisableScissor();
268 void SetTexFilter(WebGLTexture* aTex, bool aFilter);
269 void InitTexParameters(WebGLTexture* aTex, bool aFilter = true);
271 bool ReadInto(uint8_t* aDstData, int32_t aDstStride, SurfaceFormat aFormat,
272 const IntRect& aBounds, TextureHandle* aHandle = nullptr);
273 already_AddRefed<DataSourceSurface> ReadSnapshot(
274 TextureHandle* aHandle = nullptr);
275 already_AddRefed<TextureHandle> WrapSnapshot(const IntSize& aSize,
276 SurfaceFormat aFormat,
277 RefPtr<WebGLTexture> aTex);
278 already_AddRefed<TextureHandle> CopySnapshot(
279 const IntRect& aRect, TextureHandle* aHandle = nullptr);
281 already_AddRefed<WebGLTexture> GetCompatibleSnapshot(
282 SourceSurface* aSurface) const;
283 bool IsCompatibleSurface(SourceSurface* aSurface) const;
285 bool UploadSurface(DataSourceSurface* aData, SurfaceFormat aFormat,
286 const IntRect& aSrcRect, const IntPoint& aDstOffset,
287 bool aInit, bool aZero = false,
288 const RefPtr<WebGLTexture>& aTex = nullptr);
289 already_AddRefed<TextureHandle> AllocateTextureHandle(
290 SurfaceFormat aFormat, const IntSize& aSize, bool aAllowShared = true,
291 bool aRenderable = false);
292 void DrawQuad();
293 void DrawTriangles(const PathVertexRange& aRange);
294 bool DrawRectAccel(const Rect& aRect, const Pattern& aPattern,
295 const DrawOptions& aOptions,
296 Maybe<DeviceColor> aMaskColor = Nothing(),
297 RefPtr<TextureHandle>* aHandle = nullptr,
298 bool aTransformed = true, bool aClipped = true,
299 bool aAccelOnly = false, bool aForceUpdate = false,
300 const StrokeOptions* aStrokeOptions = nullptr,
301 const PathVertexRange* aVertexRange = nullptr,
302 const Matrix* aRectXform = nullptr);
304 already_AddRefed<TextureHandle> DrawStrokeMask(
305 const PathVertexRange& aVertexRange, const IntSize& aSize);
306 bool DrawPathAccel(const Path* aPath, const Pattern& aPattern,
307 const DrawOptions& aOptions,
308 const StrokeOptions* aStrokeOptions = nullptr,
309 bool aAllowStrokeAlpha = false,
310 const ShadowOptions* aShadow = nullptr,
311 bool aCacheable = true,
312 const Matrix* aPathXform = nullptr);
314 bool DrawCircleAccel(const Point& aCenter, float aRadius,
315 const Pattern& aPattern, const DrawOptions& aOptions,
316 const StrokeOptions* aStrokeOptions = nullptr);
318 bool DrawGlyphsAccel(ScaledFont* aFont, const GlyphBuffer& aBuffer,
319 const Pattern& aPattern, const DrawOptions& aOptions,
320 const StrokeOptions* aStrokeOptions,
321 bool aUseSubpixelAA);
323 void PruneTextureHandle(const RefPtr<TextureHandle>& aHandle);
324 bool PruneTextureMemory(size_t aMargin = 0, bool aPruneUnused = true);
326 bool RemoveSharedTexture(const RefPtr<SharedTexture>& aTexture);
327 bool RemoveStandaloneTexture(const RefPtr<StandaloneTexture>& aTexture);
329 void UnlinkSurfaceTextures();
330 void UnlinkSurfaceTexture(const RefPtr<TextureHandle>& aHandle);
331 void UnlinkGlyphCaches();
333 void ClearAllTextures();
334 void ClearEmptyTextureMemory();
335 void ClearCachesIfNecessary();
337 void CachePrefs();
340 // DrawTargetWebgl implements a subset of the DrawTarget API suitable for use
341 // by CanvasRenderingContext2D. It maps these to a client WebGL context so that
342 // they can be accelerated where possible by WebGL. It manages both routing to
343 // appropriate shaders and texture allocation/caching for surfaces. For commands
344 // that are not feasible to accelerate with WebGL, it mirrors state to a backup
345 // DrawTargetSkia that can be used as a fallback software renderer. Multiple
346 // instances of DrawTargetWebgl within a process will actually share a single
347 // WebGL context so that data can be more easily interchanged between them and
348 // also to enable more reasonable limiting of resource usage.
349 class DrawTargetWebgl : public DrawTarget, public SupportsWeakPtr {
350 friend class SourceSurfaceWebgl;
351 friend class SharedContextWebgl;
353 public:
354 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawTargetWebgl, override)
356 private:
357 IntSize mSize;
358 RefPtr<WebGLFramebuffer> mFramebuffer;
359 RefPtr<WebGLTexture> mTex;
360 RefPtr<WebGLTexture> mClipMask;
361 // The integer-aligned, scissor-compatible conservative bounds of the clip.
362 IntRect mClipBounds;
363 // The fractional, AA'd bounds of the clip rect, if applicable.
364 Rect mClipAARect;
365 RefPtr<DrawTargetSkia> mSkia;
366 // Skia DT pointing to the same pixel data, but without any applied clips.
367 RefPtr<DrawTargetSkia> mSkiaNoClip;
368 // The Shmem backing the Skia DT, if applicable.
369 RefPtr<mozilla::ipc::SharedMemoryBasic> mShmem;
370 // The currently cached snapshot of the WebGL context
371 RefPtr<SourceSurfaceWebgl> mSnapshot;
372 // The mappable size of mShmem.
373 uint32_t mShmemSize = 0;
374 // Whether the framebuffer is still in the initially clear state.
375 bool mIsClear = true;
376 // Whether or not the Skia target has valid contents and is being drawn to
377 bool mSkiaValid = false;
378 // Whether or not Skia layering over the WebGL context is enabled
379 bool mSkiaLayer = false;
380 // Whether the WebGL target was clear when the Skia layer was established.
381 bool mSkiaLayerClear = false;
382 // Whether or not the WebGL context has valid contents and is being drawn to
383 bool mWebglValid = true;
384 // Whether or not the clip state has changed since last used by
385 // SharedContextWebgl.
386 bool mClipChanged = true;
387 // Whether or not the clip state needs to be refreshed. Sometimes the clip
388 // state may be overwritten and require a refresh later, even though it has
389 // not changed.
390 bool mRefreshClipState = true;
391 // The number of layers currently pushed.
392 int32_t mLayerDepth = 0;
394 RefPtr<TextureHandle> mSnapshotTexture;
396 // Store a log of clips currently pushed so that they can be used to init
397 // the clip state of temporary DTs.
398 struct ClipStack {
399 Matrix mTransform;
400 Rect mRect;
401 RefPtr<const Path> mPath;
403 bool operator==(const ClipStack& aOther) const;
406 std::vector<ClipStack> mClipStack;
408 // The previous state of the clip stack when a mask was generated.
409 std::vector<ClipStack> mCachedClipStack;
411 // UsageProfile stores per-frame counters for significant profiling events
412 // that assist in determining whether acceleration should still be used for
413 // a Canvas2D user.
414 struct UsageProfile {
415 uint32_t mFailedFrames = 0;
416 uint32_t mFrameCount = 0;
417 uint32_t mCacheMisses = 0;
418 uint32_t mCacheHits = 0;
419 uint32_t mUncachedDraws = 0;
420 uint32_t mLayers = 0;
421 uint32_t mReadbacks = 0;
422 uint32_t mFallbacks = 0;
424 void BeginFrame();
425 void EndFrame();
426 bool RequiresRefresh() const;
428 void OnCacheMiss() { ++mCacheMisses; }
429 void OnCacheHit() { ++mCacheHits; }
430 void OnUncachedDraw() { ++mUncachedDraws; }
431 void OnLayer() { ++mLayers; }
432 void OnReadback() { ++mReadbacks; }
433 void OnFallback() { ++mFallbacks; }
436 UsageProfile mProfile;
438 RefPtr<SharedContextWebgl> mSharedContext;
440 public:
441 DrawTargetWebgl();
442 ~DrawTargetWebgl();
444 static bool CanCreate(const IntSize& aSize, SurfaceFormat aFormat);
445 static already_AddRefed<DrawTargetWebgl> Create(
446 const IntSize& aSize, SurfaceFormat aFormat,
447 const RefPtr<SharedContextWebgl>& aSharedContext);
449 bool Init(const IntSize& aSize, SurfaceFormat aFormat,
450 const RefPtr<SharedContextWebgl>& aSharedContext);
452 bool IsValid() const override;
454 DrawTargetType GetType() const override {
455 return DrawTargetType::HARDWARE_RASTER;
457 BackendType GetBackendType() const override { return BackendType::WEBGL; }
458 IntSize GetSize() const override { return mSize; }
459 const RefPtr<SharedContextWebgl>& GetSharedContext() const {
460 return mSharedContext;
463 bool HasDataSnapshot() const;
464 bool EnsureDataSnapshot();
465 void PrepareShmem();
466 already_AddRefed<SourceSurface> GetDataSnapshot();
467 already_AddRefed<SourceSurface> Snapshot() override;
468 already_AddRefed<SourceSurface> GetOptimizedSnapshot(DrawTarget* aTarget);
469 already_AddRefed<SourceSurface> GetBackingSurface() override;
470 void DetachAllSnapshots() override;
472 void BeginFrame(bool aInvalidContents = false);
473 void EndFrame();
474 bool RequiresRefresh() const { return mProfile.RequiresRefresh(); }
476 bool LockBits(uint8_t** aData, IntSize* aSize, int32_t* aStride,
477 SurfaceFormat* aFormat, IntPoint* aOrigin = nullptr) override;
478 void ReleaseBits(uint8_t* aData) override;
480 void Flush() override {}
481 void DrawSurface(
482 SourceSurface* aSurface, const Rect& aDest, const Rect& aSource,
483 const DrawSurfaceOptions& aSurfOptions = DrawSurfaceOptions(),
484 const DrawOptions& aOptions = DrawOptions()) override;
485 void DrawFilter(FilterNode* aNode, const Rect& aSourceRect,
486 const Point& aDestPoint,
487 const DrawOptions& aOptions = DrawOptions()) override;
488 void DrawSurfaceWithShadow(SourceSurface* aSurface, const Point& aDest,
489 const ShadowOptions& aShadow,
490 CompositionOp aOperator) override;
491 void DrawShadow(const Path* aPath, const Pattern& aPattern,
492 const ShadowOptions& aShadow, const DrawOptions& aOptions,
493 const StrokeOptions* aStrokeOptions = nullptr) override;
495 void ClearRect(const Rect& aRect) override;
496 void CopySurface(SourceSurface* aSurface, const IntRect& aSourceRect,
497 const IntPoint& aDestination) override;
498 void FillRect(const Rect& aRect, const Pattern& aPattern,
499 const DrawOptions& aOptions = DrawOptions()) override;
500 void StrokeRect(const Rect& aRect, const Pattern& aPattern,
501 const StrokeOptions& aStrokeOptions = StrokeOptions(),
502 const DrawOptions& aOptions = DrawOptions()) override;
503 bool StrokeLineAccel(const Point& aStart, const Point& aEnd,
504 const Pattern& aPattern,
505 const StrokeOptions& aStrokeOptions,
506 const DrawOptions& aOptions, bool aClosed = false);
507 void StrokeLine(const Point& aStart, const Point& aEnd,
508 const Pattern& aPattern,
509 const StrokeOptions& aStrokeOptions = StrokeOptions(),
510 const DrawOptions& aOptions = DrawOptions()) override;
511 void Stroke(const Path* aPath, const Pattern& aPattern,
512 const StrokeOptions& aStrokeOptions = StrokeOptions(),
513 const DrawOptions& aOptions = DrawOptions()) override;
514 void Fill(const Path* aPath, const Pattern& aPattern,
515 const DrawOptions& aOptions = DrawOptions()) override;
516 void FillCircle(const Point& aOrigin, float aRadius, const Pattern& aPattern,
517 const DrawOptions& aOptions = DrawOptions()) override;
518 void StrokeCircle(const Point& aOrigin, float aRadius,
519 const Pattern& aPattern,
520 const StrokeOptions& aStrokeOptions = StrokeOptions(),
521 const DrawOptions& aOptions = DrawOptions()) override;
523 void SetPermitSubpixelAA(bool aPermitSubpixelAA) override;
524 void FillGlyphs(ScaledFont* aFont, const GlyphBuffer& aBuffer,
525 const Pattern& aPattern,
526 const DrawOptions& aOptions = DrawOptions()) override;
527 void StrokeGlyphs(ScaledFont* aFont, const GlyphBuffer& aBuffer,
528 const Pattern& aPattern,
529 const StrokeOptions& aStrokeOptions = StrokeOptions(),
530 const DrawOptions& aOptions = DrawOptions()) override;
531 void Mask(const Pattern& aSource, const Pattern& aMask,
532 const DrawOptions& aOptions = DrawOptions()) override;
533 void MaskSurface(const Pattern& aSource, SourceSurface* aMask, Point aOffset,
534 const DrawOptions& aOptions = DrawOptions()) override;
535 bool Draw3DTransformedSurface(SourceSurface* aSurface,
536 const Matrix4x4& aMatrix) override;
537 void PushClip(const Path* aPath) override;
538 void PushClipRect(const Rect& aRect) override;
539 void PushDeviceSpaceClipRects(const IntRect* aRects,
540 uint32_t aCount) override;
541 void PopClip() override;
542 bool RemoveAllClips() override;
543 void CopyToFallback(DrawTarget* aDT);
544 void PushLayer(bool aOpaque, Float aOpacity, SourceSurface* aMask,
545 const Matrix& aMaskTransform,
546 const IntRect& aBounds = IntRect(),
547 bool aCopyBackground = false) override;
548 void PushLayerWithBlend(
549 bool aOpaque, Float aOpacity, SourceSurface* aMask,
550 const Matrix& aMaskTransform, const IntRect& aBounds = IntRect(),
551 bool aCopyBackground = false,
552 CompositionOp aCompositionOp = CompositionOp::OP_OVER) override;
553 void PopLayer() override;
554 already_AddRefed<SourceSurface> CreateSourceSurfaceFromData(
555 unsigned char* aData, const IntSize& aSize, int32_t aStride,
556 SurfaceFormat aFormat) const override;
557 already_AddRefed<SourceSurface> OptimizeSourceSurface(
558 SourceSurface* aSurface) const override;
559 already_AddRefed<SourceSurface> OptimizeSourceSurfaceForUnknownAlpha(
560 SourceSurface* aSurface) const override;
561 already_AddRefed<SourceSurface> CreateSourceSurfaceFromNativeSurface(
562 const NativeSurface& aSurface) const override;
563 already_AddRefed<DrawTarget> CreateSimilarDrawTarget(
564 const IntSize& aSize, SurfaceFormat aFormat) const override;
565 bool CanCreateSimilarDrawTarget(const IntSize& aSize,
566 SurfaceFormat aFormat) const override;
567 RefPtr<DrawTarget> CreateClippedDrawTarget(const Rect& aBounds,
568 SurfaceFormat aFormat) override;
570 already_AddRefed<PathBuilder> CreatePathBuilder(
571 FillRule aFillRule = FillRule::FILL_WINDING) const override;
572 already_AddRefed<GradientStops> CreateGradientStops(
573 GradientStop* aStops, uint32_t aNumStops,
574 ExtendMode aExtendMode = ExtendMode::CLAMP) const override;
575 already_AddRefed<FilterNode> CreateFilter(FilterType aType) override;
576 void SetTransform(const Matrix& aTransform) override;
577 void* GetNativeSurface(NativeSurfaceType aType) override;
579 bool CopyToSwapChain(
580 layers::TextureType aTextureType, layers::RemoteTextureId aId,
581 layers::RemoteTextureOwnerId aOwnerId,
582 layers::RemoteTextureOwnerClient* aOwnerClient = nullptr);
584 void OnMemoryPressure() { mSharedContext->OnMemoryPressure(); }
586 operator std::string() const {
587 std::stringstream stream;
588 stream << "DrawTargetWebgl(" << this << ")";
589 return stream.str();
592 mozilla::ipc::SharedMemoryBasic::Handle TakeShmemHandle() const {
593 return mShmem ? mShmem->TakeHandle()
594 : mozilla::ipc::SharedMemoryBasic::NULLHandle();
597 uint32_t GetShmemSize() const { return mShmemSize; }
599 private:
600 bool SupportsPattern(const Pattern& aPattern) {
601 return mSharedContext->SupportsPattern(aPattern);
604 bool SetSimpleClipRect();
605 bool GenerateComplexClipMask();
606 bool PrepareContext(bool aClipped = true);
608 void DrawRectFallback(const Rect& aRect, const Pattern& aPattern,
609 const DrawOptions& aOptions,
610 Maybe<DeviceColor> aMaskColor = Nothing(),
611 bool aTransform = true, bool aClipped = true,
612 const StrokeOptions* aStrokeOptions = nullptr);
613 bool DrawRect(const Rect& aRect, const Pattern& aPattern,
614 const DrawOptions& aOptions,
615 Maybe<DeviceColor> aMaskColor = Nothing(),
616 RefPtr<TextureHandle>* aHandle = nullptr,
617 bool aTransformed = true, bool aClipped = true,
618 bool aAccelOnly = false, bool aForceUpdate = false,
619 const StrokeOptions* aStrokeOptions = nullptr);
621 ColorPattern GetClearPattern() const;
623 bool RectContainsViewport(const Rect& aRect) const;
625 bool ShouldAccelPath(const DrawOptions& aOptions,
626 const StrokeOptions* aStrokeOptions);
627 void DrawPath(const Path* aPath, const Pattern& aPattern,
628 const DrawOptions& aOptions,
629 const StrokeOptions* aStrokeOptions = nullptr,
630 bool aAllowStrokeAlpha = false);
631 void DrawCircle(const Point& aOrigin, float aRadius, const Pattern& aPattern,
632 const DrawOptions& aOptions,
633 const StrokeOptions* aStrokeOptions = nullptr);
635 bool MarkChanged();
637 bool ReadIntoSkia();
638 void FlattenSkia();
639 bool PrepareSkia();
640 bool FlushFromSkia();
642 void MarkSkiaChanged(bool aOverwrite = false);
643 void MarkSkiaChanged(const DrawOptions& aOptions);
645 bool ShouldUseSubpixelAA(ScaledFont* aFont, const DrawOptions& aOptions);
647 bool ReadInto(uint8_t* aDstData, int32_t aDstStride);
648 already_AddRefed<DataSourceSurface> ReadSnapshot();
649 already_AddRefed<TextureHandle> CopySnapshot(const IntRect& aRect);
650 already_AddRefed<TextureHandle> CopySnapshot() {
651 return CopySnapshot(GetRect());
654 void ClearSnapshot(bool aCopyOnWrite = true, bool aNeedHandle = false);
656 bool CreateFramebuffer();
658 // PrepareContext may sometimes be used recursively. When this occurs, ensure
659 // that clip state is restored after the context is used.
660 struct AutoRestoreContext {
661 DrawTargetWebgl* mTarget;
662 Rect mClipAARect;
663 RefPtr<WebGLTexture> mLastClipMask;
665 explicit AutoRestoreContext(DrawTargetWebgl* aTarget);
667 ~AutoRestoreContext();
671 } // namespace gfx
672 } // namespace mozilla
674 #endif // _MOZILLA_GFX_DRAWTARGETWEBGL_H