blob: ed3f952d669dfe37af5237d187ca49de046bfafb [file] [log] [blame]
Geoff Lang051dbc72015-01-05 15:48:58 -05001//
Jamie Madilld2b50a02016-06-09 00:13:35 -07002// Copyright 2016 The ANGLE Project Authors. All rights reserved.
Geoff Lang051dbc72015-01-05 15:48:58 -05003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
Jamie Madilld2b50a02016-06-09 00:13:35 -07006// renderer_utils:
7// Helper methods pertaining to most or all back-ends.
8//
Geoff Lang051dbc72015-01-05 15:48:58 -05009
Jamie Madilld2b50a02016-06-09 00:13:35 -070010#include "libANGLE/renderer/renderer_utils.h"
Geoff Lang051dbc72015-01-05 15:48:58 -050011
Geoff Lang6e4cfce2016-06-13 15:06:31 -040012#include "image_util/copyimage.h"
13#include "image_util/imageformats.h"
14
Jamie Madill222c5172017-07-19 16:15:42 -040015#include "libANGLE/AttributeMap.h"
Jamie Madilld2b50a02016-06-09 00:13:35 -070016#include "libANGLE/formatutils.h"
Jamie Madill86e0b7f2016-08-09 11:10:29 -040017#include "libANGLE/renderer/Format.h"
Geoff Lang6e4cfce2016-06-13 15:06:31 -040018
19#include <string.h>
Geoff Lang051dbc72015-01-05 15:48:58 -050020
21namespace rx
22{
23
Jamie Madilld2b50a02016-06-09 00:13:35 -070024namespace
25{
26typedef std::pair<gl::FormatType, ColorWriteFunction> FormatWriteFunctionPair;
27typedef std::map<gl::FormatType, ColorWriteFunction> FormatWriteFunctionMap;
Geoff Lang051dbc72015-01-05 15:48:58 -050028
Jamie Madilld2b50a02016-06-09 00:13:35 -070029static inline void InsertFormatWriteFunctionMapping(FormatWriteFunctionMap *map,
30 GLenum format,
31 GLenum type,
Geoff Lang051dbc72015-01-05 15:48:58 -050032 ColorWriteFunction writeFunc)
33{
Jamie Madilld2b50a02016-06-09 00:13:35 -070034 map->insert(FormatWriteFunctionPair(gl::FormatType(format, type), writeFunc));
Geoff Lang051dbc72015-01-05 15:48:58 -050035}
36
37static FormatWriteFunctionMap BuildFormatWriteFunctionMap()
38{
Geoff Lang6e4cfce2016-06-13 15:06:31 -040039 using namespace angle; // For image writing functions
40
Geoff Lang051dbc72015-01-05 15:48:58 -050041 FormatWriteFunctionMap map;
42
Jamie Madilld2b50a02016-06-09 00:13:35 -070043 // clang-format off
Geoff Lang051dbc72015-01-05 15:48:58 -050044 // | Format | Type | Color write function |
45 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_BYTE, WriteColor<R8G8B8A8, GLfloat> );
46 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_BYTE, WriteColor<R8G8B8A8S, GLfloat> );
47 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, WriteColor<R4G4B4A4, GLfloat> );
48 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, WriteColor<R5G5B5A1, GLfloat> );
49 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, WriteColor<R10G10B10A2, GLfloat> );
50 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_FLOAT, WriteColor<R32G32B32A32F, GLfloat>);
51 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_HALF_FLOAT, WriteColor<R16G16B16A16F, GLfloat>);
52 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_HALF_FLOAT_OES, WriteColor<R16G16B16A16F, GLfloat>);
Vincent Lang25ab4512016-05-13 18:13:59 +020053 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_UNSIGNED_SHORT,
54 WriteColor<R16G16B16A16, GLfloat>);
55 InsertFormatWriteFunctionMapping(&map, GL_RGBA, GL_SHORT, WriteColor<R16G16B16A16S, GLfloat>);
Geoff Lang051dbc72015-01-05 15:48:58 -050056
57 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8G8B8A8, GLuint> );
58 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_BYTE, WriteColor<R8G8B8A8S, GLint> );
59 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16G16B16A16, GLuint> );
60 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_SHORT, WriteColor<R16G16B16A16S, GLint> );
61 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_INT, WriteColor<R32G32B32A32, GLuint> );
62 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_INT, WriteColor<R32G32B32A32S, GLint> );
63 InsertFormatWriteFunctionMapping(&map, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV, WriteColor<R10G10B10A2, GLuint> );
64
65 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_BYTE, WriteColor<R8G8B8, GLfloat> );
66 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_BYTE, WriteColor<R8G8B8S, GLfloat> );
67 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, WriteColor<R5G6B5, GLfloat> );
68 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV, WriteColor<R11G11B10F, GLfloat> );
69 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV, WriteColor<R9G9B9E5, GLfloat> );
70 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_FLOAT, WriteColor<R32G32B32F, GLfloat> );
71 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_HALF_FLOAT, WriteColor<R16G16B16F, GLfloat> );
72 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_HALF_FLOAT_OES, WriteColor<R16G16B16F, GLfloat> );
Vincent Lang25ab4512016-05-13 18:13:59 +020073 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_UNSIGNED_SHORT,
74 WriteColor<R16G16B16, GLfloat>);
75 InsertFormatWriteFunctionMapping(&map, GL_RGB, GL_SHORT, WriteColor<R16G16B16S, GLfloat>);
Geoff Lang051dbc72015-01-05 15:48:58 -050076
77 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8G8B8, GLuint> );
78 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_BYTE, WriteColor<R8G8B8S, GLint> );
79 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16G16B16, GLuint> );
80 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_SHORT, WriteColor<R16G16B16S, GLint> );
81 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_UNSIGNED_INT, WriteColor<R32G32B32, GLuint> );
82 InsertFormatWriteFunctionMapping(&map, GL_RGB_INTEGER, GL_INT, WriteColor<R32G32B32S, GLint> );
83
84 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_UNSIGNED_BYTE, WriteColor<R8G8, GLfloat> );
85 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_BYTE, WriteColor<R8G8S, GLfloat> );
86 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_FLOAT, WriteColor<R32G32F, GLfloat> );
87 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_HALF_FLOAT, WriteColor<R16G16F, GLfloat> );
88 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_HALF_FLOAT_OES, WriteColor<R16G16F, GLfloat> );
Vincent Lang25ab4512016-05-13 18:13:59 +020089 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_UNSIGNED_SHORT, WriteColor<R16G16, GLfloat>);
90 InsertFormatWriteFunctionMapping(&map, GL_RG, GL_SHORT, WriteColor<R16G16S, GLfloat>);
Geoff Lang051dbc72015-01-05 15:48:58 -050091
92 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8G8, GLuint> );
93 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_BYTE, WriteColor<R8G8S, GLint> );
94 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16G16, GLuint> );
95 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_SHORT, WriteColor<R16G16S, GLint> );
96 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_UNSIGNED_INT, WriteColor<R32G32, GLuint> );
97 InsertFormatWriteFunctionMapping(&map, GL_RG_INTEGER, GL_INT, WriteColor<R32G32S, GLint> );
98
99 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_UNSIGNED_BYTE, WriteColor<R8, GLfloat> );
100 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_BYTE, WriteColor<R8S, GLfloat> );
101 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_FLOAT, WriteColor<R32F, GLfloat> );
102 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_HALF_FLOAT, WriteColor<R16F, GLfloat> );
103 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_HALF_FLOAT_OES, WriteColor<R16F, GLfloat> );
Vincent Lang25ab4512016-05-13 18:13:59 +0200104 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_UNSIGNED_SHORT, WriteColor<R16, GLfloat>);
105 InsertFormatWriteFunctionMapping(&map, GL_RED, GL_SHORT, WriteColor<R16S, GLfloat>);
Geoff Lang051dbc72015-01-05 15:48:58 -0500106
107 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_UNSIGNED_BYTE, WriteColor<R8, GLuint> );
108 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_BYTE, WriteColor<R8S, GLint> );
109 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_UNSIGNED_SHORT, WriteColor<R16, GLuint> );
110 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_SHORT, WriteColor<R16S, GLint> );
111 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_UNSIGNED_INT, WriteColor<R32, GLuint> );
112 InsertFormatWriteFunctionMapping(&map, GL_RED_INTEGER, GL_INT, WriteColor<R32S, GLint> );
113
114 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, WriteColor<L8A8, GLfloat> );
115 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_UNSIGNED_BYTE, WriteColor<L8, GLfloat> );
116 InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_UNSIGNED_BYTE, WriteColor<A8, GLfloat> );
117 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_FLOAT, WriteColor<L32A32F, GLfloat> );
118 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_FLOAT, WriteColor<L32F, GLfloat> );
119 InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_FLOAT, WriteColor<A32F, GLfloat> );
120 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT, WriteColor<L16A16F, GLfloat> );
121 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES, WriteColor<L16A16F, GLfloat> );
122 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_HALF_FLOAT, WriteColor<L16F, GLfloat> );
123 InsertFormatWriteFunctionMapping(&map, GL_LUMINANCE, GL_HALF_FLOAT_OES, WriteColor<L16F, GLfloat> );
124 InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_HALF_FLOAT, WriteColor<A16F, GLfloat> );
125 InsertFormatWriteFunctionMapping(&map, GL_ALPHA, GL_HALF_FLOAT_OES, WriteColor<A16F, GLfloat> );
126
127 InsertFormatWriteFunctionMapping(&map, GL_BGRA_EXT, GL_UNSIGNED_BYTE, WriteColor<B8G8R8A8, GLfloat> );
Austin Kinross5cf0f982015-08-12 09:35:10 -0700128 InsertFormatWriteFunctionMapping(&map, GL_BGRA_EXT, GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT, WriteColor<A4R4G4B4, GLfloat> );
129 InsertFormatWriteFunctionMapping(&map, GL_BGRA_EXT, GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT, WriteColor<A1R5G5B5, GLfloat> );
Geoff Lang051dbc72015-01-05 15:48:58 -0500130
131 InsertFormatWriteFunctionMapping(&map, GL_SRGB_EXT, GL_UNSIGNED_BYTE, WriteColor<R8G8B8, GLfloat> );
132 InsertFormatWriteFunctionMapping(&map, GL_SRGB_ALPHA_EXT, GL_UNSIGNED_BYTE, WriteColor<R8G8B8A8, GLfloat> );
133
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800134 InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE, nullptr );
135 InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_UNSIGNED_BYTE, nullptr );
136 InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, GL_UNSIGNED_BYTE, nullptr );
137 InsertFormatWriteFunctionMapping(&map, GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, GL_UNSIGNED_BYTE, nullptr );
Geoff Lang051dbc72015-01-05 15:48:58 -0500138
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800139 InsertFormatWriteFunctionMapping(&map, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, nullptr );
140 InsertFormatWriteFunctionMapping(&map, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr );
141 InsertFormatWriteFunctionMapping(&map, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr );
Geoff Lang051dbc72015-01-05 15:48:58 -0500142
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800143 InsertFormatWriteFunctionMapping(&map, GL_STENCIL, GL_UNSIGNED_BYTE, nullptr );
Geoff Lang051dbc72015-01-05 15:48:58 -0500144
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800145 InsertFormatWriteFunctionMapping(&map, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr );
146 InsertFormatWriteFunctionMapping(&map, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, nullptr );
Jamie Madilld2b50a02016-06-09 00:13:35 -0700147 // clang-format on
Geoff Lang051dbc72015-01-05 15:48:58 -0500148
149 return map;
150}
Geoff Langd93cd6c2017-08-11 16:32:41 -0400151
152void CopyColor(gl::ColorF *color)
153{
154 // No-op
155}
156
157void PremultiplyAlpha(gl::ColorF *color)
158{
159 color->red *= color->alpha;
160 color->green *= color->alpha;
161 color->blue *= color->alpha;
162}
163
164void UnmultiplyAlpha(gl::ColorF *color)
165{
166 if (color->alpha != 0.0f)
167 {
168 float invAlpha = 1.0f / color->alpha;
169 color->red *= invAlpha;
170 color->green *= invAlpha;
171 color->blue *= invAlpha;
172 }
173}
174
175void ClipChannelsR(gl::ColorF *color)
176{
177 color->green = 0.0f;
178 color->blue = 0.0f;
179 color->alpha = 1.0f;
180}
181
182void ClipChannelsRG(gl::ColorF *color)
183{
184 color->blue = 0.0f;
185 color->alpha = 1.0f;
186}
187
188void ClipChannelsRGB(gl::ColorF *color)
189{
190 color->alpha = 1.0f;
191}
192
193void ClipChannelsLuminance(gl::ColorF *color)
194{
195 color->alpha = 1.0f;
196}
197
198void ClipChannelsAlpha(gl::ColorF *color)
199{
200 color->red = 0.0f;
201 color->green = 0.0f;
202 color->blue = 0.0f;
203}
204
205void ClipChannelsNoOp(gl::ColorF *color)
206{
207}
208
209void WriteUintColor(const gl::ColorF &color,
210 ColorWriteFunction colorWriteFunction,
211 uint8_t *destPixelData)
212{
213 gl::ColorUI destColor(
214 static_cast<unsigned int>(color.red * 255), static_cast<unsigned int>(color.green * 255),
215 static_cast<unsigned int>(color.blue * 255), static_cast<unsigned int>(color.alpha * 255));
216 colorWriteFunction(reinterpret_cast<const uint8_t *>(&destColor), destPixelData);
217}
218
219void WriteFloatColor(const gl::ColorF &color,
220 ColorWriteFunction colorWriteFunction,
221 uint8_t *destPixelData)
222{
223 colorWriteFunction(reinterpret_cast<const uint8_t *>(&color), destPixelData);
224}
225
Jamie Madilld2b50a02016-06-09 00:13:35 -0700226} // anonymous namespace
Geoff Lang051dbc72015-01-05 15:48:58 -0500227
Jamie Madilld2b50a02016-06-09 00:13:35 -0700228PackPixelsParams::PackPixelsParams()
229 : format(GL_NONE), type(GL_NONE), outputPitch(0), packBuffer(nullptr), offset(0)
230{
231}
232
233PackPixelsParams::PackPixelsParams(const gl::Rectangle &areaIn,
234 GLenum formatIn,
235 GLenum typeIn,
236 GLuint outputPitchIn,
237 const gl::PixelPackState &packIn,
238 ptrdiff_t offsetIn)
239 : area(areaIn),
240 format(formatIn),
241 type(typeIn),
242 outputPitch(outputPitchIn),
243 packBuffer(packIn.pixelBuffer.get()),
244 pack(packIn.alignment, packIn.reverseRowOrder),
245 offset(offsetIn)
246{
247}
248
Jamie Madill4928b7c2017-06-20 12:57:39 -0400249PackPixelsParams::PackPixelsParams(const gl::Context *context, const PackPixelsParams &other)
250 : area(other.area),
251 format(other.format),
252 type(other.type),
253 outputPitch(other.outputPitch),
254 packBuffer(other.packBuffer),
255 pack(),
256 offset(other.offset)
257{
258 pack.copyFrom(context, other.pack);
259}
260
Jamie Madilld2b50a02016-06-09 00:13:35 -0700261void PackPixels(const PackPixelsParams &params,
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400262 const angle::Format &sourceFormat,
Jamie Madilld2b50a02016-06-09 00:13:35 -0700263 int inputPitchIn,
264 const uint8_t *sourceIn,
265 uint8_t *destWithoutOffset)
266{
267 uint8_t *destWithOffset = destWithoutOffset + params.offset;
268
269 const uint8_t *source = sourceIn;
270 int inputPitch = inputPitchIn;
271
272 if (params.pack.reverseRowOrder)
273 {
274 source += inputPitch * (params.area.height - 1);
275 inputPitch = -inputPitch;
276 }
277
Geoff Langca271392017-04-05 12:30:00 -0400278 const auto &sourceGLInfo = gl::GetSizedInternalFormatInfo(sourceFormat.glInternalFormat);
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400279
280 if (sourceGLInfo.format == params.format && sourceGLInfo.type == params.type)
Jamie Madilld2b50a02016-06-09 00:13:35 -0700281 {
282 // Direct copy possible
283 for (int y = 0; y < params.area.height; ++y)
284 {
285 memcpy(destWithOffset + y * params.outputPitch, source + y * inputPitch,
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400286 params.area.width * sourceGLInfo.pixelBytes);
Jamie Madilld2b50a02016-06-09 00:13:35 -0700287 }
288 return;
289 }
290
Geoff Langca271392017-04-05 12:30:00 -0400291 ASSERT(sourceGLInfo.sized);
Jamie Madillec0b5802016-07-04 13:11:59 -0400292
Jamie Madilld2b50a02016-06-09 00:13:35 -0700293 gl::FormatType formatType(params.format, params.type);
Jamie Madill30712062016-08-09 11:10:36 -0400294 ColorCopyFunction fastCopyFunc =
295 GetFastCopyFunction(sourceFormat.fastCopyFunctions, formatType);
Geoff Langca271392017-04-05 12:30:00 -0400296 const auto &destFormatInfo = gl::GetInternalFormatInfo(formatType.format, formatType.type);
Jamie Madilld2b50a02016-06-09 00:13:35 -0700297
298 if (fastCopyFunc)
299 {
300 // Fast copy is possible through some special function
301 for (int y = 0; y < params.area.height; ++y)
302 {
303 for (int x = 0; x < params.area.width; ++x)
304 {
305 uint8_t *dest =
306 destWithOffset + y * params.outputPitch + x * destFormatInfo.pixelBytes;
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400307 const uint8_t *src = source + y * inputPitch + x * sourceGLInfo.pixelBytes;
Jamie Madilld2b50a02016-06-09 00:13:35 -0700308
309 fastCopyFunc(src, dest);
310 }
311 }
312 return;
313 }
314
315 ColorWriteFunction colorWriteFunction = GetColorWriteFunction(formatType);
316
317 // Maximum size of any Color<T> type used.
318 uint8_t temp[16];
319 static_assert(sizeof(temp) >= sizeof(gl::ColorF) && sizeof(temp) >= sizeof(gl::ColorUI) &&
320 sizeof(temp) >= sizeof(gl::ColorI),
321 "Unexpected size of gl::Color struct.");
322
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400323 const auto &colorReadFunction = sourceFormat.colorReadFunction;
324
Jamie Madilld2b50a02016-06-09 00:13:35 -0700325 for (int y = 0; y < params.area.height; ++y)
326 {
327 for (int x = 0; x < params.area.width; ++x)
328 {
329 uint8_t *dest = destWithOffset + y * params.outputPitch + x * destFormatInfo.pixelBytes;
Jamie Madill86e0b7f2016-08-09 11:10:29 -0400330 const uint8_t *src = source + y * inputPitch + x * sourceGLInfo.pixelBytes;
Jamie Madilld2b50a02016-06-09 00:13:35 -0700331
332 // readFunc and writeFunc will be using the same type of color, CopyTexImage
333 // will not allow the copy otherwise.
334 colorReadFunction(src, temp);
335 colorWriteFunction(temp, dest);
336 }
337 }
338}
339
340ColorWriteFunction GetColorWriteFunction(const gl::FormatType &formatType)
Geoff Lang051dbc72015-01-05 15:48:58 -0500341{
342 static const FormatWriteFunctionMap formatTypeMap = BuildFormatWriteFunctionMap();
Jamie Madilld2b50a02016-06-09 00:13:35 -0700343 auto iter = formatTypeMap.find(formatType);
Geoff Lang051dbc72015-01-05 15:48:58 -0500344 ASSERT(iter != formatTypeMap.end());
345 if (iter != formatTypeMap.end())
346 {
347 return iter->second;
348 }
349 else
350 {
Jamie Madilld2b50a02016-06-09 00:13:35 -0700351 return nullptr;
Geoff Lang051dbc72015-01-05 15:48:58 -0500352 }
353}
354
Jamie Madilld2b50a02016-06-09 00:13:35 -0700355ColorCopyFunction GetFastCopyFunction(const FastCopyFunctionMap &fastCopyFunctions,
356 const gl::FormatType &formatType)
357{
Jamie Madill4f57e5f2016-10-27 17:36:53 -0400358 return fastCopyFunctions.get(formatType);
359}
360
361bool FastCopyFunctionMap::has(const gl::FormatType &formatType) const
362{
363 return (get(formatType) != nullptr);
364}
365
366ColorCopyFunction FastCopyFunctionMap::get(const gl::FormatType &formatType) const
367{
368 for (size_t index = 0; index < mSize; ++index)
369 {
370 if (mData[index].format == formatType.format && mData[index].type == formatType.type)
371 {
372 return mData[index].func;
373 }
374 }
375
376 return nullptr;
Geoff Lang051dbc72015-01-05 15:48:58 -0500377}
Jamie Madilld2b50a02016-06-09 00:13:35 -0700378
Jamie Madill222c5172017-07-19 16:15:42 -0400379bool ShouldUseDebugLayers(const egl::AttributeMap &attribs)
380{
381 EGLAttrib debugSetting =
382 attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE);
383
384// Prefer to enable debug layers if compiling in Debug, and disabled in Release.
385#if !defined(NDEBUG)
386 return (debugSetting != EGL_FALSE);
387#else
388 return (debugSetting == EGL_TRUE);
389#endif // !defined(NDEBUG)
390}
391
Geoff Langd93cd6c2017-08-11 16:32:41 -0400392void CopyImageCHROMIUM(const uint8_t *sourceData,
393 size_t sourceRowPitch,
394 size_t sourcePixelBytes,
395 ColorReadFunction colorReadFunction,
396 uint8_t *destData,
397 size_t destRowPitch,
398 size_t destPixelBytes,
399 ColorWriteFunction colorWriteFunction,
400 GLenum destUnsizedFormat,
401 GLenum destComponentType,
402 size_t width,
403 size_t height,
404 bool unpackFlipY,
405 bool unpackPremultiplyAlpha,
406 bool unpackUnmultiplyAlpha)
407{
408 using ConversionFunction = void (*)(gl::ColorF *);
409 ConversionFunction conversionFunction = CopyColor;
410 if (unpackPremultiplyAlpha != unpackUnmultiplyAlpha)
411 {
412 if (unpackPremultiplyAlpha)
413 {
414 conversionFunction = PremultiplyAlpha;
415 }
416 else
417 {
418 conversionFunction = UnmultiplyAlpha;
419 }
420 }
421
422 auto clipChannelsFunction = ClipChannelsNoOp;
423 switch (destUnsizedFormat)
424 {
425 case GL_RED:
426 clipChannelsFunction = ClipChannelsR;
427 break;
428 case GL_RG:
429 clipChannelsFunction = ClipChannelsRG;
430 break;
431 case GL_RGB:
432 clipChannelsFunction = ClipChannelsRGB;
433 break;
434 case GL_LUMINANCE:
435 clipChannelsFunction = ClipChannelsLuminance;
436 break;
437 case GL_ALPHA:
438 clipChannelsFunction = ClipChannelsAlpha;
439 break;
440 }
441
442 auto writeFunction = (destComponentType == GL_UNSIGNED_INT) ? WriteUintColor : WriteFloatColor;
443
444 for (size_t y = 0; y < height; y++)
445 {
446 for (size_t x = 0; x < width; x++)
447 {
448 const uint8_t *sourcePixelData = sourceData + y * sourceRowPitch + x * sourcePixelBytes;
449
450 gl::ColorF sourceColor;
451 colorReadFunction(sourcePixelData, reinterpret_cast<uint8_t *>(&sourceColor));
452
453 conversionFunction(&sourceColor);
454 clipChannelsFunction(&sourceColor);
455
456 size_t destY = 0;
457 if (unpackFlipY)
458 {
459 destY += (height - 1);
460 destY -= y;
461 }
462 else
463 {
464 destY += y;
465 }
466
467 uint8_t *destPixelData = destData + destY * destRowPitch + x * destPixelBytes;
468 writeFunction(sourceColor, colorWriteFunction, destPixelData);
469 }
470 }
471}
472
Jamie Madilld2b50a02016-06-09 00:13:35 -0700473} // namespace rx