1// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef sw_Blitter_hpp
16#define sw_Blitter_hpp
17
18#include "Surface.hpp"
19#include "RoutineCache.hpp"
20#include "Reactor/Reactor.hpp"
21
22#include <string.h>
23
24namespace sw
25{
26 class Blitter
27 {
28 struct Options
29 {
30 Options() = default;
31 Options(bool filter, bool useStencil, bool convertSRGB)
32 : writeMask(0xF), clearOperation(false), filter(filter), useStencil(useStencil), convertSRGB(convertSRGB), clampToEdge(false) {}
33 Options(unsigned int writeMask)
34 : writeMask(writeMask), clearOperation(true), filter(false), useStencil(false), convertSRGB(true), clampToEdge(false) {}
35
36 union
37 {
38 struct
39 {
40 bool writeRed : 1;
41 bool writeGreen : 1;
42 bool writeBlue : 1;
43 bool writeAlpha : 1;
44 };
45
46 unsigned char writeMask;
47 };
48
49 bool clearOperation : 1;
50 bool filter : 1;
51 bool useStencil : 1;
52 bool convertSRGB : 1;
53 bool clampToEdge : 1;
54 };
55
56 struct State : Memset<State>, Options
57 {
58 State() : Memset(this, 0) {}
59 State(const Options &options) : Memset(this, 0), Options(options) {}
60
61 bool operator==(const State &state) const
62 {
63 static_assert(is_memcmparable<State>::value, "Cannot memcmp State");
64 return memcmp(this, &state, sizeof(State)) == 0;
65 }
66
67 Format sourceFormat;
68 Format destFormat;
69 int destSamples;
70 };
71
72 struct BlitData
73 {
74 void *source;
75 void *dest;
76 int sPitchB;
77 int dPitchB;
78 int dSliceB;
79
80 float x0;
81 float y0;
82 float w;
83 float h;
84
85 int y0d;
86 int y1d;
87 int x0d;
88 int x1d;
89
90 int sWidth;
91 int sHeight;
92 };
93
94 public:
95 Blitter();
96 virtual ~Blitter();
97
98 void clear(void *pixel, sw::Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask);
99 void blit(Surface *source, const SliceRectF &sRect, Surface *dest, const SliceRect &dRect, const Options &options);
100 void blit3D(Surface *source, Surface *dest);
101
102 private:
103 bool fastClear(void *pixel, sw::Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask);
104
105 bool read(Float4 &color, Pointer<Byte> element, const State &state);
106 bool write(Float4 &color, Pointer<Byte> element, const State &state);
107 bool read(Int4 &color, Pointer<Byte> element, const State &state);
108 bool write(Int4 &color, Pointer<Byte> element, const State &state);
109 static bool GetScale(float4& scale, Format format);
110 static bool ApplyScaleAndClamp(Float4 &value, const State &state, bool preScaled = false);
111 static Int ComputeOffset(Int &x, Int &y, Int &pitchB, int bytes, bool quadLayout);
112 static Float4 LinearToSRGB(Float4 &color);
113 static Float4 sRGBtoLinear(Float4 &color);
114 bool blitReactor(Surface *source, const SliceRectF &sRect, Surface *dest, const SliceRect &dRect, const Options &options);
115 std::shared_ptr<Routine> generate(const State &state);
116
117 RoutineCache<State> *blitCache;
118 MutexLock criticalSection;
119 };
120}
121
122#endif // sw_Blitter_hpp
123