File: RecordingImageBufferSurface.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (147 lines) | stat: -rw-r--r-- 5,634 bytes parent folder | download
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef RecordingImageBufferSurface_h
#define RecordingImageBufferSurface_h

#include "platform/graphics/GraphicsContext.h"
#include "platform/graphics/ImageBufferSurface.h"
#include "public/platform/WebThread.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "wtf/Allocator.h"
#include "wtf/Noncopyable.h"
#include <memory>

class SkCanvas;
class SkPicture;
class SkPictureRecorder;

namespace blink {

class ImageBuffer;
class RecordingImageBufferSurfaceTest;

class RecordingImageBufferFallbackSurfaceFactory {
  USING_FAST_MALLOC(RecordingImageBufferFallbackSurfaceFactory);
  WTF_MAKE_NONCOPYABLE(RecordingImageBufferFallbackSurfaceFactory);

 public:
  virtual std::unique_ptr<ImageBufferSurface> createSurface(const IntSize&,
                                                            OpacityMode,
                                                            sk_sp<SkColorSpace>,
                                                            SkColorType) = 0;
  virtual ~RecordingImageBufferFallbackSurfaceFactory() {}

 protected:
  RecordingImageBufferFallbackSurfaceFactory() {}
};

class PLATFORM_EXPORT RecordingImageBufferSurface : public ImageBufferSurface {
  WTF_MAKE_NONCOPYABLE(RecordingImageBufferSurface);
  USING_FAST_MALLOC(RecordingImageBufferSurface);

 public:
  // If the fallbackFactory is null the buffer surface should only be used
  // for one frame and should not be used for any operations which need a
  // raster surface, (i.e. writePixels).
  // Only #getPicture should be used to access the resulting frame.
  RecordingImageBufferSurface(
      const IntSize&,
      std::unique_ptr<RecordingImageBufferFallbackSurfaceFactory>
          fallbackFactory = nullptr,
      OpacityMode = NonOpaque,
      sk_sp<SkColorSpace> = nullptr,
      SkColorType = kN32_SkColorType);
  ~RecordingImageBufferSurface() override;

  // Implementation of ImageBufferSurface interfaces
  SkCanvas* canvas() override;
  void disableDeferral(DisableDeferralReason) override;
  sk_sp<SkPicture> getPicture() override;
  void flush(FlushReason) override;
  void didDraw(const FloatRect&) override;
  bool isValid() const override { return true; }
  bool isRecording() const override { return !m_fallbackSurface; }
  bool writePixels(const SkImageInfo& origInfo,
                   const void* pixels,
                   size_t rowBytes,
                   int x,
                   int y) override;
  void willOverwriteCanvas() override;
  virtual void finalizeFrame(const FloatRect&);
  void setImageBuffer(ImageBuffer*) override;
  sk_sp<SkImage> newImageSnapshot(AccelerationHint, SnapshotReason) override;
  void draw(GraphicsContext&,
            const FloatRect& destRect,
            const FloatRect& srcRect,
            SkBlendMode) override;
  bool isExpensiveToPaint() override;
  void setHasExpensiveOp() override { m_currentFrameHasExpensiveOp = true; }

  // Passthroughs to fallback surface
  bool restore() override;
  WebLayer* layer() const override;
  bool isAccelerated() const override;
  void setIsHidden(bool) override;

  // This enum is used in a UMA histogram.
  enum FallbackReason {
    FallbackReasonUnknown =
        0,  // This value should never appear in production histograms
    FallbackReasonCanvasNotClearedBetweenFrames = 1,
    FallbackReasonRunawayStateStack = 2,
    FallbackReasonWritePixels = 3,
    FallbackReasonFlushInitialClear = 4,
    FallbackReasonFlushForDrawImageOfWebGL = 5,
    FallbackReasonSnapshotForGetImageData = 6,
    FallbackReasonSnapshotForPaint = 8,
    FallbackReasonSnapshotForToDataURL = 9,
    FallbackReasonSnapshotForToBlob = 10,
    FallbackReasonSnapshotForCanvasListenerCapture = 11,
    FallbackReasonSnapshotForDrawImage = 12,
    FallbackReasonSnapshotForCreatePattern = 13,
    FallbackReasonExpensiveOverdrawHeuristic = 14,
    FallbackReasonTextureBackedPattern = 15,
    FallbackReasonDrawImageOfVideo = 16,
    FallbackReasonDrawImageOfAnimated2dCanvas = 17,
    FallbackReasonSubPixelTextAntiAliasingSupport = 18,
    FallbackReasonDrawImageWithTextureBackedSourceImage = 19,
    FallbackReasonSnapshotForTransferToImageBitmap = 20,
    FallbackReasonSnapshotForUnitTests =
        21,  // This value should never appear in production histograms
    FallbackReasonSnapshotGetCopiedImage = 22,
    FallbackReasonSnapshotWebGLDrawImageIntoBuffer = 23,
    FallbackReasonSnapshotForWebGLTexImage2D = 24,
    FallbackReasonSnapshotForWebGLTexSubImage2D = 25,
    FallbackReasonSnapshotForWebGLTexImage3D = 26,
    FallbackReasonSnapshotForWebGLTexSubImage3D = 27,
    FallbackReasonSnapshotForCopyToClipboard = 28,
    FallbackReasonSnapshotForCreateImageBitmap = 29,
    FallbackReasonCount,
  };

 private:
  friend class RecordingImageBufferSurfaceTest;  // for unit testing
  void fallBackToRasterCanvas(FallbackReason);
  void initializeCurrentFrame();
  bool finalizeFrameInternal(FallbackReason*);
  int approximateOpCount();

  std::unique_ptr<SkPictureRecorder> m_currentFrame;
  sk_sp<SkPicture> m_previousFrame;
  std::unique_ptr<ImageBufferSurface> m_fallbackSurface;
  ImageBuffer* m_imageBuffer;
  int m_initialSaveCount;
  int m_currentFramePixelCount;
  int m_previousFramePixelCount;
  bool m_frameWasCleared;
  bool m_didRecordDrawCommandsInCurrentFrame;
  bool m_currentFrameHasExpensiveOp;
  bool m_previousFrameHasExpensiveOp;
  std::unique_ptr<RecordingImageBufferFallbackSurfaceFactory> m_fallbackFactory;
};

}  // namespace blink

#endif