blob: c546060736599f9244158a6fd859274684d9ea99 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// 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"
Nicolas Capensd022e412016-09-26 13:30:14 -040020#include "Reactor/Reactor.hpp"
Nicolas Capens0bac2852016-05-07 06:09:58 -040021
22#include <string.h>
23
24namespace sw
25{
26 class Blitter
27 {
Nicolas Capens8f7739a2017-12-16 02:06:56 -050028 struct Options
Nicolas Capens0bac2852016-05-07 06:09:58 -040029 {
Nicolas Capensa6bc61d2017-12-20 11:07:45 -050030 Options() = default;
Nicolas Capens1ab837c2017-12-16 02:28:02 -050031 Options(bool filter, bool useStencil, bool convertSRGB)
Alexis Hetu73771b12017-12-19 15:37:46 -050032 : writeMask(0xF), clearOperation(false), filter(filter), useStencil(useStencil), convertSRGB(convertSRGB), clampToEdge(false) {}
Nicolas Capens8f7739a2017-12-16 02:06:56 -050033 Options(unsigned int writeMask)
Alexis Hetu73771b12017-12-19 15:37:46 -050034 : writeMask(writeMask), clearOperation(true), filter(false), useStencil(false), convertSRGB(true), clampToEdge(false) {}
Nicolas Capens8f7739a2017-12-16 02:06:56 -050035
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;
Nicolas Capens1ab837c2017-12-16 02:28:02 -050052 bool convertSRGB : 1;
Alexis Hetu73771b12017-12-19 15:37:46 -050053 bool clampToEdge : 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -040054 };
55
Nicolas Capens92eb0412019-08-20 14:13:17 -040056 struct State : Memset<State>, Options
Nicolas Capens0bac2852016-05-07 06:09:58 -040057 {
Nicolas Capens92eb0412019-08-20 14:13:17 -040058 State() : Memset(this, 0) {}
59 State(const Options &options) : Memset(this, 0), Options(options) {}
Nicolas Capens8f7739a2017-12-16 02:06:56 -050060
61 bool operator==(const State &state) const
Nicolas Capens0bac2852016-05-07 06:09:58 -040062 {
Nicolas Capens92eb0412019-08-20 14:13:17 -040063 static_assert(is_memcmparable<State>::value, "Cannot memcmp State");
Nicolas Capens8f7739a2017-12-16 02:06:56 -050064 return memcmp(this, &state, sizeof(State)) == 0;
Nicolas Capens0bac2852016-05-07 06:09:58 -040065 }
66
67 Format sourceFormat;
68 Format destFormat;
Nicolas Capensbfa23b32017-12-11 10:06:37 -050069 int destSamples;
Nicolas Capens0bac2852016-05-07 06:09:58 -040070 };
71
72 struct BlitData
73 {
74 void *source;
75 void *dest;
76 int sPitchB;
77 int dPitchB;
Nicolas Capensbfa23b32017-12-11 10:06:37 -050078 int dSliceB;
Nicolas Capens0bac2852016-05-07 06:09:58 -040079
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();
Nicolas Capens0bac2852016-05-07 06:09:58 -040096 virtual ~Blitter();
97
Nicolas Capensbfa23b32017-12-11 10:06:37 -050098 void clear(void *pixel, sw::Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask);
Nicolas Capens8f7739a2017-12-16 02:06:56 -050099 void blit(Surface *source, const SliceRectF &sRect, Surface *dest, const SliceRect &dRect, const Options &options);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400100 void blit3D(Surface *source, Surface *dest);
101
102 private:
Nicolas Capensbfa23b32017-12-11 10:06:37 -0500103 bool fastClear(void *pixel, sw::Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask);
Nicolas Capens802d1422017-02-14 17:23:54 -0500104
Nicolas Capens8f7739a2017-12-16 02:06:56 -0500105 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);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400109 static bool GetScale(float4& scale, Format format);
Alexis Hetud0a459f2018-01-08 17:22:15 -0500110 static bool ApplyScaleAndClamp(Float4 &value, const State &state, bool preScaled = false);
Nicolas Capens8f7739a2017-12-16 02:06:56 -0500111 static Int ComputeOffset(Int &x, Int &y, Int &pitchB, int bytes, bool quadLayout);
Nicolas Capens1ab837c2017-12-16 02:28:02 -0500112 static Float4 LinearToSRGB(Float4 &color);
113 static Float4 sRGBtoLinear(Float4 &color);
Nicolas Capens8f7739a2017-12-16 02:06:56 -0500114 bool blitReactor(Surface *source, const SliceRectF &sRect, Surface *dest, const SliceRect &dRect, const Options &options);
Ben Clayton6897e9b2019-07-16 17:27:27 +0100115 std::shared_ptr<Routine> generate(const State &state);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400116
Nicolas Capens8f7739a2017-12-16 02:06:56 -0500117 RoutineCache<State> *blitCache;
Jorge E. Moreiraf8faed62016-12-02 17:03:54 -0800118 MutexLock criticalSection;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400119 };
Nicolas Capens0bac2852016-05-07 06:09:58 -0400120}
121
122#endif // sw_Blitter_hpp