blob: 1e4119196cab1959dbfd589a0edc9691878bb703 [file] [log] [blame]
Jamie Madilld2b50a02016-06-09 00:13:35 -07001//
2// Copyright 2016 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// renderer_utils:
7// Helper methods pertaining to most or all back-ends.
8//
9
10#ifndef LIBANGLE_RENDERER_RENDERER_UTILS_H_
11#define LIBANGLE_RENDERER_RENDERER_UTILS_H_
12
13#include <cstdint>
14
Jamie Madilldcc9b512017-06-05 15:28:45 -040015#include <limits>
Jamie Madilld2b50a02016-06-09 00:13:35 -070016#include <map>
17
Jamie Madillfb05bcb2017-06-07 15:43:18 -040018#include "common/angleutils.h"
Jamie Madilld2b50a02016-06-09 00:13:35 -070019#include "libANGLE/angletypes.h"
20
Jamie Madill86e0b7f2016-08-09 11:10:29 -040021namespace angle
22{
23struct Format;
Jamie Madill222c5172017-07-19 16:15:42 -040024} // namespace angle
Jamie Madill86e0b7f2016-08-09 11:10:29 -040025
Jamie Madilld2b50a02016-06-09 00:13:35 -070026namespace gl
27{
28struct FormatType;
29struct InternalFormat;
Jamie Madill222c5172017-07-19 16:15:42 -040030} // namespace gl
31
32namespace egl
33{
34class AttributeMap;
35} // namespace egl
Jamie Madilld2b50a02016-06-09 00:13:35 -070036
37namespace rx
38{
39
Jamie Madilldcc9b512017-06-05 15:28:45 -040040class ResourceSerial
41{
42 public:
43 constexpr ResourceSerial() : mValue(kDirty) {}
44 constexpr ResourceSerial(uintptr_t value) : mValue(value) {}
45 constexpr bool operator==(ResourceSerial other) const { return mValue == other.mValue; }
46 constexpr bool operator!=(ResourceSerial other) const { return mValue != other.mValue; }
47
48 void dirty() { mValue = kDirty; }
49
50 private:
51 constexpr static uintptr_t kDirty = std::numeric_limits<uintptr_t>::max();
52
53 uintptr_t mValue;
54};
55
Jamie Madillfb05bcb2017-06-07 15:43:18 -040056class SerialFactory;
57
58class Serial final
59{
60 public:
Jamie Madill0e7f1732017-09-09 23:32:50 -040061 constexpr Serial() : mValue(kInvalid) {}
Jamie Madillfb05bcb2017-06-07 15:43:18 -040062 constexpr Serial(const Serial &other) = default;
63 Serial &operator=(const Serial &other) = default;
64
Jamie Madill0e7f1732017-09-09 23:32:50 -040065 constexpr bool operator==(const Serial &other) const
66 {
67 return mValue != kInvalid && mValue == other.mValue;
68 }
69 constexpr bool operator!=(const Serial &other) const
70 {
71 return mValue == kInvalid || mValue != other.mValue;
72 }
Jamie Madillfb05bcb2017-06-07 15:43:18 -040073 constexpr bool operator>(const Serial &other) const { return mValue > other.mValue; }
74 constexpr bool operator>=(const Serial &other) const { return mValue >= other.mValue; }
75 constexpr bool operator<(const Serial &other) const { return mValue < other.mValue; }
76 constexpr bool operator<=(const Serial &other) const { return mValue <= other.mValue; }
77
78 private:
79 friend class SerialFactory;
80 constexpr explicit Serial(uint64_t value) : mValue(value) {}
81 uint64_t mValue;
Jamie Madill0e7f1732017-09-09 23:32:50 -040082 static constexpr uint64_t kInvalid = 0;
Jamie Madillfb05bcb2017-06-07 15:43:18 -040083};
84
85class SerialFactory final : angle::NonCopyable
86{
87 public:
88 SerialFactory() : mSerial(1) {}
89
90 Serial generate()
91 {
92 ASSERT(mSerial != std::numeric_limits<uint64_t>::max());
93 return Serial(mSerial++);
94 }
95
96 private:
97 uint64_t mSerial;
98};
99
Jamie Madilla5b15612016-08-09 11:10:27 -0400100using MipGenerationFunction = void (*)(size_t sourceWidth,
101 size_t sourceHeight,
102 size_t sourceDepth,
103 const uint8_t *sourceData,
104 size_t sourceRowPitch,
105 size_t sourceDepthPitch,
106 uint8_t *destData,
107 size_t destRowPitch,
108 size_t destDepthPitch);
109
Jamie Madilld2b50a02016-06-09 00:13:35 -0700110typedef void (*ColorReadFunction)(const uint8_t *source, uint8_t *dest);
111typedef void (*ColorWriteFunction)(const uint8_t *source, uint8_t *dest);
112typedef void (*ColorCopyFunction)(const uint8_t *source, uint8_t *dest);
113
Jamie Madill4f57e5f2016-10-27 17:36:53 -0400114class FastCopyFunctionMap
115{
116 public:
117 struct Entry
118 {
119 GLenum format;
120 GLenum type;
121 ColorCopyFunction func;
122 };
123
124 constexpr FastCopyFunctionMap() : FastCopyFunctionMap(nullptr, 0) {}
125
126 constexpr FastCopyFunctionMap(const Entry *data, size_t size) : mSize(size), mData(data) {}
127
128 bool has(const gl::FormatType &formatType) const;
129 ColorCopyFunction get(const gl::FormatType &formatType) const;
130
131 private:
132 size_t mSize;
133 const Entry *mData;
134};
Jamie Madilld2b50a02016-06-09 00:13:35 -0700135
Jamie Madill4928b7c2017-06-20 12:57:39 -0400136struct PackPixelsParams : private angle::NonCopyable
Jamie Madilld2b50a02016-06-09 00:13:35 -0700137{
138 PackPixelsParams();
139 PackPixelsParams(const gl::Rectangle &area,
140 GLenum format,
141 GLenum type,
142 GLuint outputPitch,
143 const gl::PixelPackState &pack,
144 ptrdiff_t offset);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400145 PackPixelsParams(const gl::Context *context, const PackPixelsParams &other);
Jamie Madilld2b50a02016-06-09 00:13:35 -0700146
147 gl::Rectangle area;
148 GLenum format;
149 GLenum type;
150 GLuint outputPitch;
151 gl::Buffer *packBuffer;
152 gl::PixelPackState pack;
153 ptrdiff_t offset;
154};
155
156void PackPixels(const PackPixelsParams &params,
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400157 const angle::Format &sourceFormat,
Jamie Madilld2b50a02016-06-09 00:13:35 -0700158 int inputPitch,
159 const uint8_t *source,
160 uint8_t *destination);
161
162ColorWriteFunction GetColorWriteFunction(const gl::FormatType &formatType);
163ColorCopyFunction GetFastCopyFunction(const FastCopyFunctionMap &fastCopyFunctions,
164 const gl::FormatType &formatType);
165
Jamie Madillabaab232017-01-10 12:37:37 -0500166using InitializeTextureDataFunction = void (*)(size_t width,
167 size_t height,
168 size_t depth,
169 uint8_t *output,
170 size_t outputRowPitch,
171 size_t outputDepthPitch);
172
Jamie Madillb2e48632016-08-09 18:08:03 -0400173using LoadImageFunction = void (*)(size_t width,
174 size_t height,
175 size_t depth,
176 const uint8_t *input,
177 size_t inputRowPitch,
178 size_t inputDepthPitch,
179 uint8_t *output,
180 size_t outputRowPitch,
181 size_t outputDepthPitch);
182
183struct LoadImageFunctionInfo
184{
185 LoadImageFunctionInfo() : loadFunction(nullptr), requiresConversion(false) {}
186 LoadImageFunctionInfo(LoadImageFunction loadFunction, bool requiresConversion)
187 : loadFunction(loadFunction), requiresConversion(requiresConversion)
188 {
189 }
190
191 LoadImageFunction loadFunction;
192 bool requiresConversion;
193};
194
195using LoadFunctionMap = LoadImageFunctionInfo (*)(GLenum);
196
Jamie Madill222c5172017-07-19 16:15:42 -0400197bool ShouldUseDebugLayers(const egl::AttributeMap &attribs);
198
Geoff Langd93cd6c2017-08-11 16:32:41 -0400199void CopyImageCHROMIUM(const uint8_t *sourceData,
200 size_t sourceRowPitch,
201 size_t sourcePixelBytes,
202 ColorReadFunction readFunction,
203 uint8_t *destData,
204 size_t destRowPitch,
205 size_t destPixelBytes,
206 ColorWriteFunction colorWriteFunction,
207 GLenum destUnsizedFormat,
208 GLenum destComponentType,
209 size_t width,
210 size_t height,
211 bool unpackFlipY,
212 bool unpackPremultiplyAlpha,
213 bool unpackUnmultiplyAlpha);
214
Jamie Madilld2b50a02016-06-09 00:13:35 -0700215} // namespace rx
216
217#endif // LIBANGLE_RENDERER_RENDERER_UTILS_H_