1 | // LAF OS Library |
2 | // Copyright (c) 2018-2022 Igara Studio S.A. |
3 | // Copyright (c) 2012-2018 David Capello |
4 | // |
5 | // This file is released under the terms of the MIT license. |
6 | // Read LICENSE.txt for more information. |
7 | |
8 | #ifndef OS_SKIA_SKIA_SURFACE_INCLUDED |
9 | #define OS_SKIA_SKIA_SURFACE_INCLUDED |
10 | #pragma once |
11 | |
12 | #include "base/exception.h" |
13 | #include "gfx/clip.h" |
14 | #include "gfx/matrix.h" |
15 | #include "os/common/generic_surface.h" |
16 | #include "os/common/sprite_sheet_font.h" |
17 | #include "os/skia/skia_color_space.h" |
18 | |
19 | #include "include/core/SkBitmap.h" |
20 | #include "include/core/SkPaint.h" |
21 | #include "include/core/SkSurface.h" |
22 | |
23 | #include <atomic> |
24 | |
25 | namespace os { |
26 | |
27 | class SkiaSurface final : public Surface { |
28 | public: |
29 | SkiaSurface(); |
30 | SkiaSurface(const sk_sp<SkSurface>& surface); |
31 | ~SkiaSurface(); |
32 | |
33 | void create(int width, int height, const os::ColorSpaceRef& cs); |
34 | void createRgba(int width, int height, const os::ColorSpaceRef& cs); |
35 | void destroy(); |
36 | |
37 | void flush() const; |
38 | void flushAndSubmit() const; |
39 | |
40 | // Surface impl |
41 | int width() const override; |
42 | int height() const override; |
43 | const ColorSpaceRef& colorSpace() const override; |
44 | bool isDirectToScreen() const override; |
45 | void setImmutable() override; |
46 | int getSaveCount() const override; |
47 | gfx::Rect getClipBounds() const override; |
48 | void saveClip() override; |
49 | void restoreClip() override; |
50 | bool clipRect(const gfx::Rect& rc) override; |
51 | void save() override; |
52 | void concat(const gfx::Matrix& matrix) override; |
53 | void setMatrix(const gfx::Matrix& matrix) override; |
54 | void resetMatrix() override; |
55 | void restore() override; |
56 | gfx::Matrix matrix() const override; |
57 | void lock() override; |
58 | void unlock() override; |
59 | void applyScale(int scaleFactor) override; |
60 | |
61 | void* nativeHandle() override; |
62 | |
63 | void clear() override; |
64 | uint8_t* getData(int x, int y) const override; |
65 | void getFormat(SurfaceFormatData* formatData) const override; |
66 | |
67 | gfx::Color getPixel(int x, int y) const override; |
68 | void putPixel(gfx::Color color, int x, int y) override; |
69 | |
70 | void drawLine(const float x0, const float y0, |
71 | const float x1, const float y1, |
72 | const Paint& paint) override; |
73 | |
74 | void drawRect(const gfx::RectF& rc, |
75 | const Paint& paint) override; |
76 | |
77 | void drawCircle(const float cx, const float cy, |
78 | const float radius, |
79 | const Paint& paint) override; |
80 | |
81 | void drawPath(const gfx::Path& path, |
82 | const Paint& paint) override; |
83 | |
84 | void blitTo(Surface* _dst, int srcx, int srcy, int dstx, int dsty, int width, int height) const override; |
85 | void scrollTo(const gfx::Rect& rc, int dx, int dy) override; |
86 | void drawSurface(const Surface* src, int dstx, int dsty) override; |
87 | void drawSurface(const Surface* src, |
88 | const gfx::Rect& srcRect, |
89 | const gfx::Rect& dstRect, |
90 | const Sampling& sampling, |
91 | const os::Paint* paint) override; |
92 | void drawRgbaSurface(const Surface* src, int dstx, int dsty) override; |
93 | void drawRgbaSurface(const Surface* src, int srcx, int srcy, int dstx, int dsty, int w, int h) override; |
94 | void drawColoredRgbaSurface(const Surface* src, gfx::Color fg, gfx::Color bg, const gfx::Clip& clipbase) override; |
95 | void drawSurfaceNine(os::Surface* surface, |
96 | const gfx::Rect& src, |
97 | const gfx::Rect& _center, |
98 | const gfx::Rect& dst, |
99 | const bool drawCenter, |
100 | const os::Paint* paint) override; |
101 | |
102 | bool isValid() const { |
103 | return !m_bitmap.isNull(); |
104 | } |
105 | |
106 | SkBitmap& bitmap() { |
107 | ASSERT(!m_bitmap.isNull()); |
108 | return m_bitmap; |
109 | } |
110 | SkCanvas& canvas() { return *m_canvas; } |
111 | |
112 | void swapBitmap(SkBitmap& other); |
113 | |
114 | static SurfaceRef loadSurface(const char* filename); |
115 | |
116 | private: |
117 | void skDrawSurface( |
118 | const Surface* src, |
119 | const gfx::Clip& clip, |
120 | const SkSamplingOptions& sampling, |
121 | const SkPaint& paint); |
122 | void skDrawSurface( |
123 | const Surface* src, |
124 | const gfx::Rect& srcRect, |
125 | const gfx::Rect& dstRect, |
126 | const SkSamplingOptions& sampling, |
127 | const SkPaint& paint); |
128 | void skDrawSurface( |
129 | const SkiaSurface* src, |
130 | const SkRect& srcRect, |
131 | const SkRect& dstRect, |
132 | const SkSamplingOptions& sampling, |
133 | const SkPaint& paint); |
134 | |
135 | #if SK_SUPPORT_GPU |
136 | const SkImage* getOrCreateTextureImage() const; |
137 | bool uploadBitmapAsTexture() const; |
138 | #endif |
139 | |
140 | sk_sp<SkColorSpace> skColorSpace() const { |
141 | if (m_colorSpace) |
142 | return static_cast<SkiaColorSpace*>(m_colorSpace.get())->skColorSpace(); |
143 | else |
144 | return nullptr; |
145 | } |
146 | |
147 | SkBitmap m_bitmap; |
148 | #if SK_SUPPORT_GPU |
149 | mutable sk_sp<SkImage> m_image; |
150 | #endif |
151 | sk_sp<SkSurface> m_surface; |
152 | ColorSpaceRef m_colorSpace; |
153 | SkCanvas* m_canvas; |
154 | SkPaint m_paint; |
155 | std::atomic<int> m_lock; |
156 | |
157 | }; |
158 | |
159 | } // namespace os |
160 | |
161 | #endif |
162 | |