Source code

Revision control

Other Tools

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/. */
6
7
#ifndef GFX_CANVASRENDERER_H
8
#define GFX_CANVASRENDERER_H
9
10
#include <stdint.h> // for uint32_t
11
#include "GLContextTypes.h" // for GLContext
12
#include "gfxContext.h" // for gfxContext, etc
13
#include "gfxTypes.h"
14
#include "gfxPlatform.h" // for gfxImageFormat
15
#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
16
#include "mozilla/Preferences.h" // for Preferences
17
#include "mozilla/RefPtr.h" // for RefPtr
18
#include "mozilla/gfx/2D.h" // for DrawTarget
19
#include "mozilla/mozalloc.h" // for operator delete, etc
20
#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc
21
22
namespace mozilla {
23
namespace layers {
24
25
class AsyncCanvasRenderer;
26
class ClientCanvasRenderer;
27
class CopyableCanvasRenderer;
28
class OOPCanvasRenderer;
29
class PersistentBufferProvider;
30
class WebRenderCanvasRendererAsync;
31
32
struct CanvasInitializeData final {
33
CanvasInitializeData();
34
~CanvasInitializeData();
35
36
// One of these four must be specified for Canvas2D, but never more than one
37
RefPtr<PersistentBufferProvider>
38
mBufferProvider; // A BufferProvider for the Canvas contents
39
RefPtr<mozilla::gl::GLContext> mGLContext; // or this, for GL.
40
RefPtr<AsyncCanvasRenderer> mRenderer; // or this, for OffscreenCanvas
41
RefPtr<OOPCanvasRenderer> mOOPRenderer; // or this, for WebGL in host process
42
43
typedef void (*TransactionCallback)(void* closureData);
44
TransactionCallback mPreTransCallback = nullptr;
45
void* mPreTransCallbackData = nullptr;
46
TransactionCallback mDidTransCallback = nullptr;
47
void* mDidTransCallbackData = nullptr;
48
49
// The size of the canvas content
50
gfx::IntSize mSize = {0, 0};
51
52
// Whether the canvas drawingbuffer has an alpha channel.
53
bool mHasAlpha = false;
54
55
// Whether mGLContext contains data that is alpha-premultiplied.
56
bool mIsGLAlphaPremult = true;
57
};
58
59
// Based class which used for canvas rendering. There are many derived classes
60
// for different purposes. such as:
61
//
62
// CopyableCanvasRenderer provides a utility which readback canvas content to a
63
// SourceSurface. BasicCanvasLayer uses CopyableCanvasRenderer.
64
//
65
// ShareableCanvasRenderer provides IPC capabilities that allow sending canvas
66
// content over IPC. This is pure virtual class because the IPC handling is
67
// different in different LayerManager. So that we have following classes
68
// inherite ShareableCanvasRenderer.
69
//
70
// ClientCanvasRenderer inherites ShareableCanvasRenderer and be used in
71
// ClientCanvasLayer.
72
// WebRenderCanvasRenderer inherites ShareableCanvasRenderer and provides all
73
// functionality that WebRender uses.
74
// WebRenderCanvasRendererAsync inherites WebRenderCanvasRenderer and be used in
75
// layers-free mode of WebRender.
76
//
77
// class diagram shows below:
78
//
79
// +--------------+
80
// |CanvasRenderer|
81
// +-------+------+
82
// ^
83
// |
84
// +----------------------+
85
// |CopyableCanvasRenderer|
86
// +----------------------+
87
// ^
88
// |
89
// +-----------+-----------+
90
// |ShareableCanvasRenderer|
91
// +-----+-----------------+
92
// ^ ^
93
// +-------------+ +-------+
94
// | |
95
// +--------------------+ +---------+-------------+
96
// |ClientCanvasRenderer| |WebRenderCanvasRenderer|
97
// +--------------------+ +-----------+-----------+
98
// ^
99
// |
100
// +-------------+--------------+
101
// |WebRenderCanvasRendererAsync|
102
// +----------------------------+
103
class CanvasRenderer {
104
public:
105
CanvasRenderer();
106
virtual ~CanvasRenderer();
107
108
public:
109
virtual void Initialize(const CanvasInitializeData& aData);
110
virtual bool IsDataValid(const CanvasInitializeData& aData) { return true; }
111
112
virtual void ClearCachedResources() {}
113
virtual void Destroy() {}
114
115
const gfx::IntSize& GetSize() const { return mSize; }
116
117
void SetDirty() { mDirty = true; }
118
void ResetDirty() { mDirty = false; }
119
bool IsDirty() const { return mDirty; }
120
121
virtual CopyableCanvasRenderer* AsCopyableCanvasRenderer() { return nullptr; }
122
virtual ClientCanvasRenderer* AsClientCanvasRenderer() { return nullptr; }
123
virtual WebRenderCanvasRendererAsync* AsWebRenderCanvasRendererAsync() {
124
return nullptr;
125
}
126
127
protected:
128
void FirePreTransactionCallback() {
129
if (mPreTransCallback) {
130
mPreTransCallback(mPreTransCallbackData);
131
}
132
}
133
134
void FireDidTransactionCallback() {
135
if (mDidTransCallback) {
136
mDidTransCallback(mDidTransCallbackData);
137
}
138
}
139
140
typedef void (*TransactionCallback)(void* closureData);
141
TransactionCallback mPreTransCallback;
142
void* mPreTransCallbackData;
143
TransactionCallback mDidTransCallback;
144
void* mDidTransCallbackData;
145
gfx::IntSize mSize;
146
147
private:
148
/**
149
* Set to true in Updated(), cleared during a transaction.
150
*/
151
bool mDirty;
152
};
153
154
} // namespace layers
155
} // namespace mozilla
156
157
#endif