summaryrefslogtreecommitdiffstats
path: root/gfx/layers/CanvasRenderer.h
blob: 7d60c7beae101f0fac7810783ea37b76601e4a17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef GFX_CANVASRENDERER_H
#define GFX_CANVASRENDERER_H

#include <memory>            // for weak_ptr
#include <stdint.h>          // for uint32_t
#include "GLContextTypes.h"  // for GLContext
#include "gfxContext.h"      // for gfxContext, etc
#include "gfxTypes.h"
#include "gfxPlatform.h"          // for gfxImageFormat
#include "mozilla/Assertions.h"   // for MOZ_ASSERT, etc
#include "mozilla/Preferences.h"  // for Preferences
#include "mozilla/RefPtr.h"       // for RefPtr
#include "mozilla/gfx/2D.h"       // for DrawTarget
#include "mozilla/Maybe.h"
#include "mozilla/mozalloc.h"  // for operator delete, etc
#include "mozilla/WeakPtr.h"   // for WeakPtr
#include "nsISupportsImpl.h"   // for MOZ_COUNT_CTOR, etc
#include "nsICanvasRenderingContextInternal.h"

namespace mozilla {
namespace layers {

class KnowsCompositor;
class PersistentBufferProvider;
class WebRenderCanvasRendererAsync;

TextureType TexTypeForWebgl(KnowsCompositor*);

struct CanvasRendererData final {
  CanvasRendererData();
  ~CanvasRendererData();

  WeakPtr<nsICanvasRenderingContextInternal> mContext;

  // The size of the canvas content
  gfx::IntSize mSize = {0, 0};

  bool mDoPaintCallbacks = false;
  bool mIsOpaque = true;
  bool mIsAlphaPremult = true;

  gl::OriginPos mOriginPos = gl::OriginPos::TopLeft;

  // Used in remote texture push callback
  Maybe<RemoteTextureOwnerId> mRemoteTextureOwnerId = Nothing();

  nsICanvasRenderingContextInternal* GetContext() const {
    return mContext.get();
  }
};

// Based class which used for canvas rendering. There are many derived classes
// for different purposes. such as:
//
// ShareableCanvasRenderer provides IPC capabilities that allow sending canvas
// content over IPC. This is pure virtual class because the IPC handling is
// different in different LayerManager. So that we have following classes
// inherit ShareableCanvasRenderer.
//
// WebRenderCanvasRenderer inherits ShareableCanvasRenderer and provides all
// functionality that WebRender uses.
// WebRenderCanvasRendererAsync inherits WebRenderCanvasRenderer and be used in
// layers-free mode of WebRender.
//
// class diagram:
//
//                       +--------------+
//                       |CanvasRenderer|
//                       +-------+------+
//                               ^
//                               |
//                   +-----------+-----------+
//                   |ShareableCanvasRenderer|
//                   +-----+-----------------+
//                               ^
//                               |
//                   +-----------+-----------+
//                   |WebRenderCanvasRenderer|
//                   +-----------+-----------+
//                               ^
//                               |
//                 +-------------+--------------+
//                 |WebRenderCanvasRendererAsync|
//                 +----------------------------+

class BorrowedSourceSurface final {
 public:
  const WeakPtr<PersistentBufferProvider> mReturnTo;
  const RefPtr<gfx::SourceSurface> mSurf;  /// non-null

  BorrowedSourceSurface(PersistentBufferProvider*, RefPtr<gfx::SourceSurface>);
  ~BorrowedSourceSurface();
};

// -

class CanvasRenderer : public RefCounted<CanvasRenderer> {
  friend class CanvasRendererSourceSurface;

 public:
  MOZ_DECLARE_REFCOUNTED_TYPENAME(CanvasRenderer)

 private:
  bool mDirty = false;

 protected:
  CanvasRendererData mData;

 public:
  explicit CanvasRenderer();
  virtual ~CanvasRenderer();

 public:
  virtual void Initialize(const CanvasRendererData&);
  virtual bool IsDataValid(const CanvasRendererData&) const;

  virtual void ClearCachedResources() {}
  virtual void DisconnectClient() {}

  const gfx::IntSize& GetSize() const { return mData.mSize; }
  bool IsOpaque() const { return mData.mIsOpaque; }
  bool YIsDown() const { return mData.mOriginPos == gl::OriginPos::TopLeft; }
  Maybe<RemoteTextureOwnerId> GetRemoteTextureOwnerId() {
    return mData.mRemoteTextureOwnerId;
  }

  void SetDirty() { mDirty = true; }
  void ResetDirty() { mDirty = false; }
  bool IsDirty() const { return mDirty; }

  virtual WebRenderCanvasRendererAsync* AsWebRenderCanvasRendererAsync() {
    return nullptr;
  }

  std::shared_ptr<BorrowedSourceSurface> BorrowSnapshot(
      bool requireAlphaPremult = true) const;

  void FirePreTransactionCallback() const;
  void FireDidTransactionCallback() const;
};

}  // namespace layers
}  // namespace mozilla

#endif