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_SURFACE_H_INCLUDED
9#define OS_SURFACE_H_INCLUDED
10#pragma once
11
12#include "base/string.h"
13#include "gfx/clip.h"
14#include "gfx/color.h"
15#include "gfx/point.h"
16#include "gfx/rect.h"
17#include "os/color_space.h"
18#include "os/paint.h"
19#include "os/ref.h"
20#include "os/sampling.h"
21#include "os/surface_format.h"
22
23#include <string>
24
25namespace gfx {
26 class Matrix;
27 class Path;
28}
29
30namespace os {
31
32 class ColorSpace;
33 struct Sampling;
34 class Surface;
35 class SurfaceLock;
36 using SurfaceRef = Ref<Surface>;
37
38 class Surface : public RefCount {
39 public:
40 virtual ~Surface() { }
41 virtual int width() const = 0;
42 virtual int height() const = 0;
43 gfx::Rect bounds() const { return gfx::Rect(0, 0, width(), height()); }
44 virtual const ColorSpaceRef& colorSpace() const = 0;
45 virtual bool isDirectToScreen() const = 0;
46
47 // Call if you are not going to modify the pixels of this surface
48 // in the future. E.g. useful for sprite sheets/texture atlases.
49 virtual void setImmutable() = 0;
50
51 virtual int getSaveCount() const = 0;
52 virtual gfx::Rect getClipBounds() const = 0;
53 virtual void saveClip() = 0;
54 virtual void restoreClip() = 0;
55 virtual bool clipRect(const gfx::Rect& rc) = 0;
56
57 virtual void save() = 0;
58 virtual void concat(const gfx::Matrix& matrix) = 0;
59 virtual void setMatrix(const gfx::Matrix& matrix) = 0;
60 virtual void resetMatrix() = 0;
61 virtual void restore() = 0;
62 virtual gfx::Matrix matrix() const = 0;
63
64 virtual void lock() = 0;
65 virtual void unlock() = 0;
66
67 virtual void clear() = 0;
68
69 virtual uint8_t* getData(int x, int y) const = 0;
70 virtual void getFormat(SurfaceFormatData* formatData) const = 0;
71
72 virtual gfx::Color getPixel(int x, int y) const = 0;
73 virtual void putPixel(gfx::Color color, int x, int y) = 0;
74
75 virtual void drawLine(const float x0, const float y0,
76 const float x1, const float y1,
77 const os::Paint& paint) = 0;
78
79 void drawLine(const int x0, const int y0,
80 const int x1, const int y1,
81 const os::Paint& paint) {
82 drawLine(float(x0), float(y0),
83 float(x1), float(y1), paint);
84 }
85
86 void drawLine(const gfx::PointF& a, const gfx::PointF& b,
87 const os::Paint& paint) {
88 drawLine(float(a.x), float(a.y),
89 float(b.x), float(b.y), paint);
90 }
91
92 void drawLine(const gfx::Point& a, const gfx::Point& b,
93 const os::Paint& paint) {
94 drawLine(float(a.x), float(a.y),
95 float(b.x), float(b.y), paint);
96 }
97
98 virtual void drawRect(const gfx::RectF& rc,
99 const os::Paint& paint) = 0;
100
101 void drawRect(const gfx::Rect& rc,
102 const os::Paint& paint) {
103 drawRect(gfx::RectF(float(rc.x), float(rc.y),
104 float(rc.w), float(rc.h)),
105 paint);
106 }
107
108 virtual void drawCircle(const float cx, const float cy,
109 const float radius,
110 const os::Paint& paint) = 0;
111
112 void drawCircle(const gfx::PointF& center,
113 float radius,
114 const os::Paint& paint) {
115 drawCircle(float(center.x), float(center.y), radius, paint);
116 }
117
118 void drawCircle(const gfx::Point& center,
119 int radius,
120 const os::Paint& paint) {
121 drawCircle(float(center.x), float(center.y), float(radius), paint);
122 }
123
124 virtual void drawPath(const gfx::Path& path,
125 const os::Paint& paint) = 0;
126
127 virtual void blitTo(Surface* dest, int srcx, int srcy, int dstx, int dsty, int width, int height) const = 0;
128 virtual void scrollTo(const gfx::Rect& rc, int dx, int dy) = 0;
129 // TODO merge all these functions expoing a SkPaint-like structure
130 virtual void drawSurface(const Surface* src, int dstx, int dsty) = 0;
131 virtual void drawSurface(const Surface* src,
132 const gfx::Rect& srcRect,
133 const gfx::Rect& dstRect,
134 const os::Sampling& sampling = os::Sampling(),
135 const os::Paint* paint = nullptr) = 0;
136 virtual void drawRgbaSurface(const Surface* src, int dstx, int dsty) = 0;
137 virtual void drawRgbaSurface(const Surface* src, int srcx, int srcy, int dstx, int dsty, int width, int height) = 0;
138 virtual void drawColoredRgbaSurface(const Surface* src, gfx::Color fg, gfx::Color bg, const gfx::Clip& clip) = 0;
139 virtual void drawSurfaceNine(os::Surface* surface,
140 const gfx::Rect& src,
141 const gfx::Rect& center,
142 const gfx::Rect& dst,
143 const bool drawCenter,
144 const os::Paint* paint) = 0;
145
146 virtual void applyScale(int scaleFactor) = 0;
147 virtual void* nativeHandle() = 0;
148 };
149
150 class SurfaceLock {
151 public:
152 SurfaceLock(Surface* surface) : m_surface(surface) { m_surface->lock(); }
153 ~SurfaceLock() { m_surface->unlock(); }
154 private:
155 Surface* m_surface;
156 };
157
158} // namespace os
159
160#endif
161