Bug 1892041 - Part 1: Update test262 features. r=spidermonkey-reviewers,dminor
[gecko.git] / dom / canvas / DrawTargetWebgl.h
blob8dc9fff04889b2a2b99503f30710111977e792c5
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 void ClearCaches();
97 private:
98 SharedContextWebgl();
100 WeakPtr<DrawTargetWebgl> mCurrentTarget;
101 IntSize mViewportSize;
102 // The current integer-aligned scissor rect.
103 IntRect mClipRect;
104 // The current fractional AA'd clip rect bounds.
105 Rect mClipAARect;
107 RefPtr<WebGLContext> mWebgl;
109 // Avoid spurious state changes by caching last used state.
110 RefPtr<WebGLProgram> mLastProgram;
111 RefPtr<WebGLTexture> mLastTexture;
112 RefPtr<WebGLTexture> mLastClipMask;
114 // WebGL shader resources
115 RefPtr<WebGLBuffer> mPathVertexBuffer;
116 RefPtr<WebGLVertexArray> mPathVertexArray;
117 // The current insertion offset into the GPU path buffer.
118 uint32_t mPathVertexOffset = 0;
119 // The maximum size of the GPU path buffer.
120 uint32_t mPathVertexCapacity = 0;
121 // The maximum supported type complexity of a GPU path.
122 uint32_t mPathMaxComplexity = 0;
123 // Whether to accelerate stroked paths with AAStroke.
124 bool mPathAAStroke = true;
125 // Whether to accelerate stroked paths with WGR.
126 bool mPathWGRStroke = false;
128 WGR::PathBuilder* mWGRPathBuilder = nullptr;
129 // Temporary buffer for generating WGR output into.
130 UniquePtr<WGR::OutputVertex[]> mWGROutputBuffer;
132 RefPtr<WebGLProgram> mSolidProgram;
133 Maybe<uint32_t> mSolidProgramViewport;
134 Maybe<uint32_t> mSolidProgramAA;
135 Maybe<uint32_t> mSolidProgramTransform;
136 Maybe<uint32_t> mSolidProgramColor;
137 Maybe<uint32_t> mSolidProgramClipMask;
138 Maybe<uint32_t> mSolidProgramClipBounds;
139 RefPtr<WebGLProgram> mImageProgram;
140 Maybe<uint32_t> mImageProgramViewport;
141 Maybe<uint32_t> mImageProgramAA;
142 Maybe<uint32_t> mImageProgramTransform;
143 Maybe<uint32_t> mImageProgramTexMatrix;
144 Maybe<uint32_t> mImageProgramTexBounds;
145 Maybe<uint32_t> mImageProgramColor;
146 Maybe<uint32_t> mImageProgramSwizzle;
147 Maybe<uint32_t> mImageProgramSampler;
148 Maybe<uint32_t> mImageProgramClipMask;
149 Maybe<uint32_t> mImageProgramClipBounds;
151 struct SolidProgramUniformState {
152 Maybe<Array<float, 2>> mViewport;
153 Maybe<Array<float, 1>> mAA;
154 Maybe<Array<float, 6>> mTransform;
155 Maybe<Array<float, 4>> mColor;
156 Maybe<Array<float, 4>> mClipBounds;
157 } mSolidProgramUniformState;
159 struct ImageProgramUniformState {
160 Maybe<Array<float, 2>> mViewport;
161 Maybe<Array<float, 1>> mAA;
162 Maybe<Array<float, 6>> mTransform;
163 Maybe<Array<float, 6>> mTexMatrix;
164 Maybe<Array<float, 4>> mTexBounds;
165 Maybe<Array<float, 4>> mColor;
166 Maybe<Array<float, 1>> mSwizzle;
167 Maybe<Array<float, 4>> mClipBounds;
168 } mImageProgramUniformState;
170 // Scratch framebuffer used to wrap textures for miscellaneous utility ops.
171 RefPtr<WebGLFramebuffer> mScratchFramebuffer;
172 // Buffer filled with zero data for initializing textures.
173 RefPtr<WebGLBuffer> mZeroBuffer;
174 size_t mZeroSize = 0;
175 // 1x1 texture with solid white mask for disabling clipping
176 RefPtr<WebGLTexture> mNoClipMask;
178 uint32_t mMaxTextureSize = 0;
179 bool mRasterizationTruncates = false;
181 // The current blending operation.
182 CompositionOp mLastCompositionOp = CompositionOp::OP_SOURCE;
183 // The constant blend color used for the blending operation.
184 Maybe<DeviceColor> mLastBlendColor;
186 // The cached scissor state. Operations that rely on scissor state should
187 // take care to enable or disable the cached scissor state as necessary.
188 bool mScissorEnabled = false;
189 IntRect mLastScissor = {-1, -1, -1, -1};
191 // A most-recently-used list of allocated texture handles.
192 LinkedList<RefPtr<TextureHandle>> mTextureHandles;
193 size_t mNumTextureHandles = 0;
194 // User data key linking a SourceSurface with its TextureHandle.
195 UserDataKey mTextureHandleKey = {0};
196 // User data key linking a SourceSurface with its shadow blur TextureHandle.
197 UserDataKey mShadowTextureKey = {0};
198 // User data key linking a ScaledFont with its GlyphCache.
199 UserDataKey mGlyphCacheKey = {0};
200 // List of all GlyphCaches currently allocated to fonts.
201 LinkedList<GlyphCache> mGlyphCaches;
202 // Cache of rasterized paths.
203 UniquePtr<PathCache> mPathCache;
204 // Collection of allocated shared texture pages that may be shared amongst
205 // many handles.
206 std::vector<RefPtr<SharedTexture>> mSharedTextures;
207 // Collection of allocated standalone textures that have a single assigned
208 // handle.
209 std::vector<RefPtr<StandaloneTexture>> mStandaloneTextures;
210 size_t mUsedTextureMemory = 0;
211 size_t mTotalTextureMemory = 0;
212 // The total reserved memory for empty texture pages that are kept around
213 // for future allocations.
214 size_t mEmptyTextureMemory = 0;
215 // A memory pressure event may signal from another thread that caches should
216 // be cleared if possible.
217 Atomic<bool> mShouldClearCaches;
218 // The total number of DrawTargetWebgls using this shared context.
219 size_t mDrawTargetCount = 0;
220 // Whether we are inside a scoped usage of TLS MakeCurrent, and what previous
221 // value to restore it to when exiting the scope.
222 Maybe<bool> mTlsScope;
224 // Cached unit circle path
225 RefPtr<Path> mUnitCirclePath;
227 bool Initialize();
228 bool CreateShaders();
229 void ResetPathVertexBuffer(bool aChanged = true);
231 void BlendFunc(GLenum aSrcFactor, GLenum aDstFactor);
232 void SetBlendState(CompositionOp aOp,
233 const Maybe<DeviceColor>& aColor = Nothing());
235 void SetClipRect(const Rect& aClipRect);
236 void SetClipRect(const IntRect& aClipRect) { SetClipRect(Rect(aClipRect)); }
237 bool SetClipMask(const RefPtr<WebGLTexture>& aTex);
238 bool SetNoClipMask();
239 bool HasClipMask() const {
240 return mLastClipMask && mLastClipMask != mNoClipMask;
243 Maybe<uint32_t> GetUniformLocation(const RefPtr<WebGLProgram>& prog,
244 const std::string& aName) const;
246 template <class T, size_t N>
247 void UniformData(GLenum aFuncElemType, const Maybe<uint32_t>& aLoc,
248 const Array<T, N>& aData);
250 // Avoids redundant UniformData calls by caching the previously set value.
251 template <class T, size_t N>
252 void MaybeUniformData(GLenum aFuncElemType, const Maybe<uint32_t>& aLoc,
253 const Array<T, N>& aData, Maybe<Array<T, N>>& aCached);
255 bool IsCurrentTarget(DrawTargetWebgl* aDT) const {
256 return aDT == mCurrentTarget;
258 bool SetTarget(DrawTargetWebgl* aDT);
260 // Reset the current target.
261 void ClearTarget() { mCurrentTarget = nullptr; }
262 // Reset the last used texture to force binding next use.
263 void ClearLastTexture(bool aFullClear = false);
265 bool SupportsPattern(const Pattern& aPattern);
267 void EnableScissor(const IntRect& aRect);
268 void DisableScissor();
270 void SetTexFilter(WebGLTexture* aTex, bool aFilter);
271 void InitTexParameters(WebGLTexture* aTex, bool aFilter = true);
273 bool ReadInto(uint8_t* aDstData, int32_t aDstStride, SurfaceFormat aFormat,
274 const IntRect& aBounds, TextureHandle* aHandle = nullptr);
275 already_AddRefed<DataSourceSurface> ReadSnapshot(
276 TextureHandle* aHandle = nullptr);
277 already_AddRefed<TextureHandle> WrapSnapshot(const IntSize& aSize,
278 SurfaceFormat aFormat,
279 RefPtr<WebGLTexture> aTex);
280 already_AddRefed<TextureHandle> CopySnapshot(
281 const IntRect& aRect, TextureHandle* aHandle = nullptr);
283 already_AddRefed<WebGLTexture> GetCompatibleSnapshot(
284 SourceSurface* aSurface) const;
285 bool IsCompatibleSurface(SourceSurface* aSurface) const;
287 bool UploadSurface(DataSourceSurface* aData, SurfaceFormat aFormat,
288 const IntRect& aSrcRect, const IntPoint& aDstOffset,
289 bool aInit, bool aZero = false,
290 const RefPtr<WebGLTexture>& aTex = nullptr);
291 already_AddRefed<TextureHandle> AllocateTextureHandle(
292 SurfaceFormat aFormat, const IntSize& aSize, bool aAllowShared = true,
293 bool aRenderable = false);
294 void DrawQuad();
295 void DrawTriangles(const PathVertexRange& aRange);
296 bool DrawRectAccel(const Rect& aRect, const Pattern& aPattern,
297 const DrawOptions& aOptions,
298 Maybe<DeviceColor> aMaskColor = Nothing(),
299 RefPtr<TextureHandle>* aHandle = nullptr,
300 bool aTransformed = true, bool aClipped = true,
301 bool aAccelOnly = false, bool aForceUpdate = false,
302 const StrokeOptions* aStrokeOptions = nullptr,
303 const PathVertexRange* aVertexRange = nullptr,
304 const Matrix* aRectXform = nullptr);
306 already_AddRefed<TextureHandle> DrawStrokeMask(
307 const PathVertexRange& aVertexRange, const IntSize& aSize);
308 bool DrawPathAccel(const Path* aPath, const Pattern& aPattern,
309 const DrawOptions& aOptions,
310 const StrokeOptions* aStrokeOptions = nullptr,
311 bool aAllowStrokeAlpha = false,
312 const ShadowOptions* aShadow = nullptr,
313 bool aCacheable = true,
314 const Matrix* aPathXform = nullptr);
316 bool DrawCircleAccel(const Point& aCenter, float aRadius,
317 const Pattern& aPattern, const DrawOptions& aOptions,
318 const StrokeOptions* aStrokeOptions = nullptr);
320 bool DrawGlyphsAccel(ScaledFont* aFont, const GlyphBuffer& aBuffer,
321 const Pattern& aPattern, const DrawOptions& aOptions,
322 const StrokeOptions* aStrokeOptions,
323 bool aUseSubpixelAA);
325 void PruneTextureHandle(const RefPtr<TextureHandle>& aHandle);
326 bool PruneTextureMemory(size_t aMargin = 0, bool aPruneUnused = true);
328 bool RemoveSharedTexture(const RefPtr<SharedTexture>& aTexture);
329 bool RemoveStandaloneTexture(const RefPtr<StandaloneTexture>& aTexture);
331 void UnlinkSurfaceTextures();
332 void UnlinkSurfaceTexture(const RefPtr<TextureHandle>& aHandle);
333 void UnlinkGlyphCaches();
335 void ClearAllTextures();
336 void ClearEmptyTextureMemory();
337 void ClearCachesIfNecessary();
339 void CachePrefs();
342 // DrawTargetWebgl implements a subset of the DrawTarget API suitable for use
343 // by CanvasRenderingContext2D. It maps these to a client WebGL context so that
344 // they can be accelerated where possible by WebGL. It manages both routing to
345 // appropriate shaders and texture allocation/caching for surfaces. For commands
346 // that are not feasible to accelerate with WebGL, it mirrors state to a backup
347 // DrawTargetSkia that can be used as a fallback software renderer. Multiple
348 // instances of DrawTargetWebgl within a process will actually share a single
349 // WebGL context so that data can be more easily interchanged between them and
350 // also to enable more reasonable limiting of resource usage.
351 class DrawTargetWebgl : public DrawTarget, public SupportsWeakPtr {
352 friend class SourceSurfaceWebgl;
353 friend class SharedContextWebgl;
355 public:
356 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawTargetWebgl, override)
358 private:
359 IntSize mSize;
360 RefPtr<WebGLFramebuffer> mFramebuffer;
361 RefPtr<WebGLTexture> mTex;
362 RefPtr<WebGLTexture> mClipMask;
363 // The integer-aligned, scissor-compatible conservative bounds of the clip.
364 IntRect mClipBounds;
365 // The fractional, AA'd bounds of the clip rect, if applicable.
366 Rect mClipAARect;
367 RefPtr<DrawTargetSkia> mSkia;
368 // Skia DT pointing to the same pixel data, but without any applied clips.
369 RefPtr<DrawTargetSkia> mSkiaNoClip;
370 // The Shmem backing the Skia DT, if applicable.
371 RefPtr<mozilla::ipc::SharedMemoryBasic> mShmem;
372 // The currently cached snapshot of the WebGL context
373 RefPtr<SourceSurfaceWebgl> mSnapshot;
374 // The mappable size of mShmem.
375 uint32_t mShmemSize = 0;
376 // Whether the framebuffer is still in the initially clear state.
377 bool mIsClear = true;
378 // Whether or not the Skia target has valid contents and is being drawn to
379 bool mSkiaValid = false;
380 // Whether or not Skia layering over the WebGL context is enabled
381 bool mSkiaLayer = false;
382 // Whether the WebGL target was clear when the Skia layer was established.
383 bool mSkiaLayerClear = false;
384 // Whether or not the WebGL context has valid contents and is being drawn to
385 bool mWebglValid = true;
386 // Whether or not the clip state has changed since last used by
387 // SharedContextWebgl.
388 bool mClipChanged = true;
389 // Whether or not the clip state needs to be refreshed. Sometimes the clip
390 // state may be overwritten and require a refresh later, even though it has
391 // not changed.
392 bool mRefreshClipState = true;
393 // The number of layers currently pushed.
394 int32_t mLayerDepth = 0;
396 RefPtr<TextureHandle> mSnapshotTexture;
398 // Store a log of clips currently pushed so that they can be used to init
399 // the clip state of temporary DTs.
400 struct ClipStack {
401 Matrix mTransform;
402 Rect mRect;
403 RefPtr<const Path> mPath;
405 bool operator==(const ClipStack& aOther) const;
408 std::vector<ClipStack> mClipStack;
410 // The previous state of the clip stack when a mask was generated.
411 std::vector<ClipStack> mCachedClipStack;
413 // UsageProfile stores per-frame counters for significant profiling events
414 // that assist in determining whether acceleration should still be used for
415 // a Canvas2D user.
416 struct UsageProfile {
417 uint32_t mFailedFrames = 0;
418 uint32_t mFrameCount = 0;
419 uint32_t mCacheMisses = 0;
420 uint32_t mCacheHits = 0;
421 uint32_t mUncachedDraws = 0;
422 uint32_t mLayers = 0;
423 uint32_t mReadbacks = 0;
424 uint32_t mFallbacks = 0;
426 void BeginFrame();
427 void EndFrame();
428 bool RequiresRefresh() const;
430 void OnCacheMiss() { ++mCacheMisses; }
431 void OnCacheHit() { ++mCacheHits; }
432 void OnUncachedDraw() { ++mUncachedDraws; }
433 void OnLayer() { ++mLayers; }
434 void OnReadback() { ++mReadbacks; }
435 void OnFallback() { ++mFallbacks; }
438 UsageProfile mProfile;
440 RefPtr<SharedContextWebgl> mSharedContext;
442 public:
443 DrawTargetWebgl();
444 ~DrawTargetWebgl();
446 static bool CanCreate(const IntSize& aSize, SurfaceFormat aFormat);
447 static already_AddRefed<DrawTargetWebgl> Create(
448 const IntSize& aSize, SurfaceFormat aFormat,
449 const RefPtr<SharedContextWebgl>& aSharedContext);
451 bool Init(const IntSize& aSize, SurfaceFormat aFormat,
452 const RefPtr<SharedContextWebgl>& aSharedContext);
454 bool IsValid() const override;
456 DrawTargetType GetType() const override {
457 return DrawTargetType::HARDWARE_RASTER;
459 BackendType GetBackendType() const override { return BackendType::WEBGL; }
460 IntSize GetSize() const override { return mSize; }
461 const RefPtr<SharedContextWebgl>& GetSharedContext() const {
462 return mSharedContext;
465 bool HasDataSnapshot() const;
466 bool EnsureDataSnapshot();
467 void PrepareShmem();
468 already_AddRefed<SourceSurface> GetDataSnapshot();
469 already_AddRefed<SourceSurface> Snapshot() override;
470 already_AddRefed<SourceSurface> GetOptimizedSnapshot(DrawTarget* aTarget);
471 already_AddRefed<SourceSurface> GetBackingSurface() override;
472 void DetachAllSnapshots() override;
474 void BeginFrame(bool aInvalidContents = false);
475 void EndFrame();
476 bool RequiresRefresh() const { return mProfile.RequiresRefresh(); }
478 bool LockBits(uint8_t** aData, IntSize* aSize, int32_t* aStride,
479 SurfaceFormat* aFormat, IntPoint* aOrigin = nullptr) override;
480 void ReleaseBits(uint8_t* aData) override;
482 void Flush() override {}
483 void DrawSurface(
484 SourceSurface* aSurface, const Rect& aDest, const Rect& aSource,
485 const DrawSurfaceOptions& aSurfOptions = DrawSurfaceOptions(),
486 const DrawOptions& aOptions = DrawOptions()) override;
487 void DrawFilter(FilterNode* aNode, const Rect& aSourceRect,
488 const Point& aDestPoint,
489 const DrawOptions& aOptions = DrawOptions()) override;
490 void DrawSurfaceWithShadow(SourceSurface* aSurface, const Point& aDest,
491 const ShadowOptions& aShadow,
492 CompositionOp aOperator) override;
493 void DrawShadow(const Path* aPath, const Pattern& aPattern,
494 const ShadowOptions& aShadow, const DrawOptions& aOptions,
495 const StrokeOptions* aStrokeOptions = nullptr) override;
497 void ClearRect(const Rect& aRect) override;
498 void CopySurface(SourceSurface* aSurface, const IntRect& aSourceRect,
499 const IntPoint& aDestination) override;
500 void FillRect(const Rect& aRect, const Pattern& aPattern,
501 const DrawOptions& aOptions = DrawOptions()) override;
502 void StrokeRect(const Rect& aRect, const Pattern& aPattern,
503 const StrokeOptions& aStrokeOptions = StrokeOptions(),
504 const DrawOptions& aOptions = DrawOptions()) override;
505 bool StrokeLineAccel(const Point& aStart, const Point& aEnd,
506 const Pattern& aPattern,
507 const StrokeOptions& aStrokeOptions,
508 const DrawOptions& aOptions, bool aClosed = false);
509 void StrokeLine(const Point& aStart, const Point& aEnd,
510 const Pattern& aPattern,
511 const StrokeOptions& aStrokeOptions = StrokeOptions(),
512 const DrawOptions& aOptions = DrawOptions()) override;
513 void Stroke(const Path* aPath, const Pattern& aPattern,
514 const StrokeOptions& aStrokeOptions = StrokeOptions(),
515 const DrawOptions& aOptions = DrawOptions()) override;
516 void Fill(const Path* aPath, const Pattern& aPattern,
517 const DrawOptions& aOptions = DrawOptions()) override;
518 void FillCircle(const Point& aOrigin, float aRadius, const Pattern& aPattern,
519 const DrawOptions& aOptions = DrawOptions()) override;
520 void StrokeCircle(const Point& aOrigin, float aRadius,
521 const Pattern& aPattern,
522 const StrokeOptions& aStrokeOptions = StrokeOptions(),
523 const DrawOptions& aOptions = DrawOptions()) override;
525 void SetPermitSubpixelAA(bool aPermitSubpixelAA) override;
526 void FillGlyphs(ScaledFont* aFont, const GlyphBuffer& aBuffer,
527 const Pattern& aPattern,
528 const DrawOptions& aOptions = DrawOptions()) override;
529 void StrokeGlyphs(ScaledFont* aFont, const GlyphBuffer& aBuffer,
530 const Pattern& aPattern,
531 const StrokeOptions& aStrokeOptions = StrokeOptions(),
532 const DrawOptions& aOptions = DrawOptions()) override;
533 void Mask(const Pattern& aSource, const Pattern& aMask,
534 const DrawOptions& aOptions = DrawOptions()) override;
535 void MaskSurface(const Pattern& aSource, SourceSurface* aMask, Point aOffset,
536 const DrawOptions& aOptions = DrawOptions()) override;
537 bool Draw3DTransformedSurface(SourceSurface* aSurface,
538 const Matrix4x4& aMatrix) override;
539 void PushClip(const Path* aPath) override;
540 void PushClipRect(const Rect& aRect) override;
541 void PushDeviceSpaceClipRects(const IntRect* aRects,
542 uint32_t aCount) override;
543 void PopClip() override;
544 bool RemoveAllClips() override;
545 void CopyToFallback(DrawTarget* aDT);
546 void PushLayer(bool aOpaque, Float aOpacity, SourceSurface* aMask,
547 const Matrix& aMaskTransform,
548 const IntRect& aBounds = IntRect(),
549 bool aCopyBackground = false) override;
550 void PushLayerWithBlend(
551 bool aOpaque, Float aOpacity, SourceSurface* aMask,
552 const Matrix& aMaskTransform, const IntRect& aBounds = IntRect(),
553 bool aCopyBackground = false,
554 CompositionOp aCompositionOp = CompositionOp::OP_OVER) override;
555 void PopLayer() override;
556 already_AddRefed<SourceSurface> CreateSourceSurfaceFromData(
557 unsigned char* aData, const IntSize& aSize, int32_t aStride,
558 SurfaceFormat aFormat) const override;
559 already_AddRefed<SourceSurface> OptimizeSourceSurface(
560 SourceSurface* aSurface) const override;
561 already_AddRefed<SourceSurface> OptimizeSourceSurfaceForUnknownAlpha(
562 SourceSurface* aSurface) const override;
563 already_AddRefed<SourceSurface> CreateSourceSurfaceFromNativeSurface(
564 const NativeSurface& aSurface) const override;
565 already_AddRefed<DrawTarget> CreateSimilarDrawTarget(
566 const IntSize& aSize, SurfaceFormat aFormat) const override;
567 bool CanCreateSimilarDrawTarget(const IntSize& aSize,
568 SurfaceFormat aFormat) const override;
569 RefPtr<DrawTarget> CreateClippedDrawTarget(const Rect& aBounds,
570 SurfaceFormat aFormat) override;
572 already_AddRefed<PathBuilder> CreatePathBuilder(
573 FillRule aFillRule = FillRule::FILL_WINDING) const override;
574 already_AddRefed<GradientStops> CreateGradientStops(
575 GradientStop* aStops, uint32_t aNumStops,
576 ExtendMode aExtendMode = ExtendMode::CLAMP) const override;
577 already_AddRefed<FilterNode> CreateFilter(FilterType aType) override;
578 void SetTransform(const Matrix& aTransform) override;
579 void* GetNativeSurface(NativeSurfaceType aType) override;
581 bool CopyToSwapChain(
582 layers::TextureType aTextureType, layers::RemoteTextureId aId,
583 layers::RemoteTextureOwnerId aOwnerId,
584 layers::RemoteTextureOwnerClient* aOwnerClient = nullptr);
586 void OnMemoryPressure() { mSharedContext->OnMemoryPressure(); }
588 operator std::string() const {
589 std::stringstream stream;
590 stream << "DrawTargetWebgl(" << this << ")";
591 return stream.str();
594 mozilla::ipc::SharedMemoryBasic::Handle TakeShmemHandle() const {
595 return mShmem ? mShmem->TakeHandle()
596 : mozilla::ipc::SharedMemoryBasic::NULLHandle();
599 uint32_t GetShmemSize() const { return mShmemSize; }
601 private:
602 bool SupportsPattern(const Pattern& aPattern) {
603 return mSharedContext->SupportsPattern(aPattern);
606 bool SetSimpleClipRect();
607 bool GenerateComplexClipMask();
608 bool PrepareContext(bool aClipped = true);
610 void DrawRectFallback(const Rect& aRect, const Pattern& aPattern,
611 const DrawOptions& aOptions,
612 Maybe<DeviceColor> aMaskColor = Nothing(),
613 bool aTransform = true, bool aClipped = true,
614 const StrokeOptions* aStrokeOptions = nullptr);
615 bool DrawRect(const Rect& aRect, const Pattern& aPattern,
616 const DrawOptions& aOptions,
617 Maybe<DeviceColor> aMaskColor = Nothing(),
618 RefPtr<TextureHandle>* aHandle = nullptr,
619 bool aTransformed = true, bool aClipped = true,
620 bool aAccelOnly = false, bool aForceUpdate = false,
621 const StrokeOptions* aStrokeOptions = nullptr);
623 ColorPattern GetClearPattern() const;
625 bool RectContainsViewport(const Rect& aRect) const;
627 bool ShouldAccelPath(const DrawOptions& aOptions,
628 const StrokeOptions* aStrokeOptions);
629 void DrawPath(const Path* aPath, const Pattern& aPattern,
630 const DrawOptions& aOptions,
631 const StrokeOptions* aStrokeOptions = nullptr,
632 bool aAllowStrokeAlpha = false);
633 void DrawCircle(const Point& aOrigin, float aRadius, const Pattern& aPattern,
634 const DrawOptions& aOptions,
635 const StrokeOptions* aStrokeOptions = nullptr);
637 bool MarkChanged();
639 bool ReadIntoSkia();
640 void FlattenSkia();
641 bool PrepareSkia();
642 bool FlushFromSkia();
644 void MarkSkiaChanged(bool aOverwrite = false);
645 void MarkSkiaChanged(const DrawOptions& aOptions);
647 bool ShouldUseSubpixelAA(ScaledFont* aFont, const DrawOptions& aOptions);
649 bool ReadInto(uint8_t* aDstData, int32_t aDstStride);
650 already_AddRefed<DataSourceSurface> ReadSnapshot();
651 already_AddRefed<TextureHandle> CopySnapshot(const IntRect& aRect);
652 already_AddRefed<TextureHandle> CopySnapshot() {
653 return CopySnapshot(GetRect());
656 void ClearSnapshot(bool aCopyOnWrite = true, bool aNeedHandle = false);
658 bool CreateFramebuffer();
660 // PrepareContext may sometimes be used recursively. When this occurs, ensure
661 // that clip state is restored after the context is used.
662 struct AutoRestoreContext {
663 DrawTargetWebgl* mTarget;
664 Rect mClipAARect;
665 RefPtr<WebGLTexture> mLastClipMask;
667 explicit AutoRestoreContext(DrawTargetWebgl* aTarget);
669 ~AutoRestoreContext();
673 } // namespace gfx
674 } // namespace mozilla
676 #endif // _MOZILLA_GFX_DRAWTARGETWEBGL_H