Debian Codegraph @

media_softlet/agnostic/common/vp/kdll/hal_kerneldll_next.c

raw
1 /*
2 * Copyright (c) 2021, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 //!
23 //! \file hal_kerneldll_next.c
24 //! \brief Kernel Dynamic Linking/Loading routines for FC
25 //!
26
27 #ifndef VPHAL_LIB
28
29 #if IMOLA
30 #include <stdlib.h>
31 #endif // IMOLA
32 #include <math.h> //for sin & cos
33 #endif // VPHAL_LIB
34
35 #if EMUL || VPHAL_LIB
36 #include <math.h>
37 #include "support.h"
38 #elif LINUX
39 #else // !(EMUL | VPHAL_LIB) && !LINUX
40
41 #endif // EMUL | VPHAL_LIB
42
43 #include "hal_kerneldll_next.h"
44 #include "vp_utils.h"
45
46 // Define _DEBUG symbol for KDLL Release build before loading the "vpkrnheader.h" file
47 // This is necessary for full kernels names in both Release/Debug versions of KDLL app
48 #if EMUL || VPHAL_LIB
49 #ifndef _DEBUG
50 #define _DEBUG 2
51 #endif // _DEBUG
52 #endif // EMUL || VPHAL_LIB
53
54 // Kernel IDs and Kernel Names
55 #include "vpkrnheader.h" // IDR_VP_TOTAL_NUM_KERNELS
56 #include "cm_fc_ld.h"
57
58 // Undefine _DEBUG symbol for the remaining of the KDLL Release build
59 #if _DEBUG == 2
60 #undef _DEBUG
61 #endif // _DEBUG
62
63
64 #ifndef PI
65 #define PI 3.1415926535897932f
66 #endif // PI
67
68 #ifdef __cplusplus
69 extern "C" {
70 #endif // __cplusplus
71
72 #define FOLD_HASH(folded_hash, hash) \
73 { \
74 folded_hash = (((hash) >> 8) ^ (hash)) & 0x00ff00ff; \
75 folded_hash = ((folded_hash >> 16) ^ folded_hash) & 0xff; \
76 } \
77
78 const bool g_cIsFormatYUV[Format_Count] =
79 {
80 false, // Format_Any
81 false, // Format_A8R8G8B8
82 false, // Format_X8R8G8B8
83 false, // Format_A8B8G8R8
84 false, // Format_X8B8G8R8
85 false, // Format_A16B16G16R16
86 false, // Format_A16R16G16B16
87 false, // Format_R5G6B5
88 false, // Format_R32U
89 false, // Format_R32F
90 false, // Format_R8G8B8
91 false, // Format_RGBP
92 false, // Format_BGRP
93 true, // Format_YUY2
94 true, // Format_YUYV
95 true, // Format_YVYU
96 true, // Format_UYVY
97 true, // Format_VYUY
98 true, // Format_Y216
99 true, // Format_Y210
100 true, // Format_Y416
101 true, // Format_AYUV
102 true, // Format_AUYV
103 true, // Format_Y410
104 true, // Format_400P
105 true, // Format_NV12
106 true, // Format_NV12_UnAligned
107 true, // Format_NV21
108 true, // Format_NV11
109 true, // Format_NV11_UnAligned
110 true, // Format_P208
111 true, // Format_P208_UnAligned
112 true, // Format_IMC1
113 true, // Format_IMC2
114 true, // Format_IMC3
115 true, // Format_IMC4
116 true, // Format_422H
117 true, // Format_422V
118 true, // Format_444P
119 true, // Format_411P
120 true, // Format_411R
121 true, // Format_I420
122 true, // Format_IYUV
123 true, // Format_YV12
124 true, // Format_YVU9
125 true, // Format_AI44 (YUV originally, palette may be converted to RGB)
126 true, // Format_IA44 (same as above)
127 false, // Format_P8 (using RGB since P8 is uncommon in FC)
128 false, // Format_A8P8 (same as above)
129 false, // Format_A8
130 false, // Format_L8
131 false, // Format_A4L4
132 false, // Format_A8L8
133 true, // Format_IRW0
134 true, // Format_IRW1
135 true, // Format_IRW2
136 true, // Format_IRW3
137 true, // Format_IRW4
138 true, // Format_IRW5
139 true, // Format_IRW6
140 true, // Format_IRW7
141 false, // Format_STMM
142 false, // Format_Buffer
143 false, // Format_Buffer_2D
144 false, // Format_V8U8
145 false, // Format_R32S
146 false, // Format_R8U
147 false, // Format_R8G8UN
148 false, // Format_R8G8SN
149 false, // Format_G8R8_G8B8
150 false, // Format_R16U
151 false, // Format_R16S
152 false, // Format_R16UN
153 false, // Format_RAW
154 false, // Format_Y8
155 false, // Format_Y1
156 false, // Format_Y16U
157 false, // Format_Y16S
158 false, // Format_L16
159 false, // Format_D16
160 false, // Format_R10G10B10A2
161 false, // Format_B10G10R10A2
162 true, // Format_P016
163 true, // Format_P010
164 true // Format_YV12_Planar
165 };
166
167 bool KernelDll_IsYUVFormat(MOS_FORMAT format)
168 {
169 if (format >= Format_Any && format < Format_Count)
170 {
171 return g_cIsFormatYUV[format];
172 }
173 else
174 {
175 return false;
176 }
177 }
178
179 /*----------------------------------------------------------------------------
180 | Purpose : Group common color spaces into one
181 | Returns : Return the representative color space of the group
182 \---------------------------------------------------------------------------*/
183 VPHAL_CSPACE KernelDll_TranslateCspace(VPHAL_CSPACE cspace)
184 {
185 switch (cspace)
186 {
187 case CSpace_BT709:
188 case CSpace_xvYCC709:
189 return CSpace_BT709;
190
191 case CSpace_BT601:
192 case CSpace_xvYCC601:
193 return CSpace_BT601;
194
195 case CSpace_BT601_FullRange:
196 return CSpace_BT601_FullRange;
197
198 case CSpace_BT709_FullRange:
199 return CSpace_BT709_FullRange;
200
201 case CSpace_RGB:
202 case CSpace_sRGB:
203 return CSpace_sRGB;
204
205 case CSpace_stRGB:
206 return CSpace_stRGB;
207
208 case CSpace_Gray:
209 case CSpace_BT601Gray:
210 return CSpace_BT601Gray;
211
212 case CSpace_BT601Gray_FullRange:
213 return CSpace_BT601Gray_FullRange;
214
215 case CSpace_BT2020:
216 return CSpace_BT2020;
217
218 case CSpace_BT2020_FullRange:
219 return CSpace_BT2020_FullRange;
220
221 case CSpace_BT2020_RGB:
222 return CSpace_BT2020_RGB;
223
224 case CSpace_BT2020_stRGB:
225 return CSpace_BT2020_stRGB;
226
227 default:
228 return CSpace_None;
229 }
230 }
231
232 void KernelDll_MatrixProduct(
233 float * dest,
234 const float *m1,
235 const float *m2)
236 {
237 bool save;
238 float temp[12];
239
240 // setup temp matrix to allow the following operations:
241 // dest = dest * m2
242 // dest = m1 * dest
243 // dest = dest * dest
244 save = (m1 == dest) || (m2 == dest);
245 m1 = (m1 == dest) ? temp : m1;
246 m2 = (m2 == dest) ? temp : m2;
247 if (save)
248 MOS_SecureMemcpy(temp, sizeof(temp), (void *)dest, sizeof(temp));
249
250 // Multiply the matrices
251 dest[0] = m1[0] * m2[0] + m1[1] * m2[4] + m1[2] * m2[8];
252 dest[1] = m1[0] * m2[1] + m1[1] * m2[5] + m1[2] * m2[9];
253 dest[2] = m1[0] * m2[2] + m1[1] * m2[6] + m1[2] * m2[10];
254 dest[3] = m1[0] * m2[3] + m1[1] * m2[7] + m1[2] * m2[11] + m1[3];
255 dest[4] = m1[4] * m2[0] + m1[5] * m2[4] + m1[6] * m2[8];
256 dest[5] = m1[4] * m2[1] + m1[5] * m2[5] + m1[6] * m2[9];
257 dest[6] = m1[4] * m2[2] + m1[5] * m2[6] + m1[6] * m2[10];
258 dest[7] = m1[4] * m2[3] + m1[5] * m2[7] + m1[6] * m2[11] + m1[7];
259 dest[8] = m1[8] * m2[0] + m1[9] * m2[4] + m1[10] * m2[8];
260 dest[9] = m1[8] * m2[1] + m1[9] * m2[5] + m1[10] * m2[9];
261 dest[10] = m1[8] * m2[2] + m1[9] * m2[6] + m1[10] * m2[10];
262 dest[11] = m1[8] * m2[3] + m1[9] * m2[7] + m1[10] * m2[11] + m1[11];
263 }
264
265 void KernelDll_UpdateCscCoefficients(Kdll_State *pState,
266 Kdll_CSC_Matrix * pMatrix)
267 {
268 float csc[12]; // CSC matrix (YUV->RGB)
269 float icsc[12]; // ICSC matrix (RGB->YUV), (YUV->YUV)
270 float m[12]; // auxiliary matrix
271 float matrix[12]; // final matrix
272 Kdll_CSCType csctype;
273 Kdll_Procamp *pProcamp = nullptr;
274 VPHAL_CSPACE src = pMatrix->SrcSpace;
275 VPHAL_CSPACE dst = pMatrix->DstSpace;
276 bool bCSC, bICSC;
277
278 bCSC = bICSC = false;
279 MOS_ZeroMemory(m, sizeof(m));
280 MOS_ZeroMemory(csc, sizeof(csc));
281 MOS_ZeroMemory(icsc, sizeof(icsc));
282
283 // Select procamp parameters
284 if (pMatrix->iProcampID > DL_PROCAMP_DISABLED &&
285 pMatrix->iProcampID < pState->iProcampSize &&
286 pState->pProcamp != nullptr)
287 {
288 pProcamp = pState->pProcamp + pMatrix->iProcampID;
289 }
290
291 // Setup CSC matrix
292 if (src != dst)
293 {
294 if ((dst == CSpace_sRGB) && (src != CSpace_stRGB))
295 {
296 KernelDll_GetCSCMatrix(src, dst, csc);
297 MOS_SecureMemcpy(m, sizeof(csc), (void *)csc, sizeof(csc));
298 bCSC = true;
299 csctype = CSC_YUV_RGB;
300 }
301 else if ((dst == CSpace_stRGB) && (src != CSpace_sRGB))
302 {
303 KernelDll_GetCSCMatrix(src, dst, csc);
304 MOS_SecureMemcpy(m, sizeof(csc), (void *)csc, sizeof(csc));
305 bCSC = true;
306 csctype = CSC_YUV_RGB;
307 }
308 else
309 {
310 KernelDll_GetCSCMatrix(src, dst, icsc);
311 MOS_SecureMemcpy(m, sizeof(icsc), (void *)icsc, sizeof(icsc));
312 bICSC = true;
313 if (KernelDll_IsCspace(src, CSpace_RGB) && !KernelDll_IsCspace(dst, CSpace_RGB))
314 {
315 csctype = CSC_RGB_YUV;
316 }
317 else if (KernelDll_IsCspace(src, CSpace_BT2020_RGB) && KernelDll_IsCspace(dst, CSpace_BT2020))
318 {
319 csctype = CSC_RGB_YUV;
320 }
321 else if (KernelDll_IsCspace(src, CSpace_BT2020) && KernelDll_IsCspace(dst, CSpace_BT2020_RGB))
322 {
323 csctype = CSC_YUV_RGB;
324 }
325 else if (KernelDll_IsCspace(src, CSpace_BT2020_RGB) && KernelDll_IsCspace(dst, CSpace_BT2020_RGB))
326 {
327 csctype = CSC_RGB_RGB;
328
329 // Kernel params didn't support 10bit, it need transformation from 10bit to 8bit.
330 m[3] = ROUND_FLOAT(m[3], 0.25f); // 10bit to 8bit (value/4)
331 m[7] = ROUND_FLOAT(m[7], 0.25f); // 10bit to 8bit (value/4)
332 m[11] = ROUND_FLOAT(m[11], 0.25f); // 10bit to 8bit (value/4)
333 }
334 else
335 {
336 csctype = CSC_YUV_YUV;
337 }
338 }
339 }
340 // Setup CSC matrix for procamp in sRGB space
341 else if ((dst == CSpace_sRGB) && (pProcamp))
342 {
343 KernelDll_GetCSCMatrix(CSpace_sRGB, CSpace_BT709, icsc);
344 KernelDll_GetCSCMatrix(CSpace_BT709, CSpace_sRGB, csc);
345 bICSC = bCSC = true;
346 csctype = CSC_RGB_RGB;
347 }
348 // Setup CSC matrix for procamp in stRGB space
349 else if ((dst == CSpace_stRGB) && (pProcamp))
350 {
351 KernelDll_GetCSCMatrix(CSpace_stRGB, CSpace_BT709, icsc);
352 KernelDll_GetCSCMatrix(CSpace_BT709, CSpace_stRGB, csc);
353 bICSC = bCSC = true;
354 csctype = CSC_RGB_RGB;
355 }
356 else
357 {
358 MOS_SecureMemcpy(m, sizeof(g_cCSC_Identity), (void *)g_cCSC_Identity, sizeof(g_cCSC_Identity));
359 csctype = CSC_YUV_YUV;
360 }
361
362 // Product only happens if Procamp is present
363 // Otherwise use the original matrix
364 if (pProcamp)
365 {
366 float b, c, h, s;
367
368 // Calculate procamp parameters
369 b = pProcamp->fBrightness;
370 c = pProcamp->fContrast;
371 h = pProcamp->fHue * (PI / 180.0f);
372 s = pProcamp->fSaturation;
373
374 // procamp matrix
375 //
376 // [Y'] [ c 0 0 ] [Y] [ 16 - 16 * c + b ]
377 // [U'] = [ 0 c*s*cos(h) c*s*sin(h) ] [U] + [ 128 - 128*c*s*(cos(h)+sin(h)) ]
378 // [V'] [ 0 -c*s*sin(h) c*s*cos(h) ] [V] [ 128 - 128*c*s*(cos(h)-sin(h)) ]
379
380 matrix[0] = c;
381 matrix[1] = 0.0f;
382 matrix[2] = 0.0f;
383 matrix[3] = 16.0f - 16.0f * c + b;
384 matrix[4] = 0.0f;
385 matrix[5] = (float)cos(h) * c * s;
386 matrix[6] = (float)sin(h) * c * s;
387 matrix[7] = 128.0f * (1.0f - matrix[5] - matrix[6]);
388 matrix[8] = 0.0f;
389 matrix[9] = -matrix[6];
390 matrix[10] = matrix[5];
391 matrix[11] = 128.0f * (1.0f - matrix[5] + matrix[6]);
392
393 // Calculate final CSC matrix (csc * pa * icsc)
394 if (bICSC)
395 { // Calculate [pa] * [icsc]
396 KernelDll_MatrixProduct(matrix, matrix, icsc);
397 }
398
399 if (bCSC)
400 { // Calculate [csc] * [pa] (if no icsc)
401 // or [csc] * [pa] * [icsc]
402 KernelDll_MatrixProduct(matrix, csc, matrix);
403 }
404
405 // Update procamp version
406 pMatrix->iProcampVersion = pProcamp->iProcampVersion;
407
408 // Use the output matrix to generate kernel CSC parameters
409 MOS_SecureMemcpy(m, sizeof(m), (void *)matrix, sizeof(m));
410 }
411
412 // normalize for kernel use
413 matrix[0] = ROUND_FLOAT(m[0], 128.0f); // 9.7
414 matrix[1] = ROUND_FLOAT(m[1], 128.0f); // 9.7
415 matrix[2] = ROUND_FLOAT(m[2], 128.0f); // 9.7
416 matrix[3] = ROUND_FLOAT(m[3], 0.5f); // 16.0 (value/2)
417 matrix[4] = ROUND_FLOAT(m[4], 128.0f); // 9.7
418 matrix[5] = ROUND_FLOAT(m[5], 128.0f); // 9.7
419 matrix[6] = ROUND_FLOAT(m[6], 128.0f); // 9.7
420 matrix[7] = ROUND_FLOAT(m[7], 0.5f); // 16.0 (value/2)
421 matrix[8] = ROUND_FLOAT(m[8], 128.0f); // 9.7
422 matrix[9] = ROUND_FLOAT(m[9], 128.0f); // 9.7
423 matrix[10] = ROUND_FLOAT(m[10], 128.0f); // 9.7
424 matrix[11] = ROUND_FLOAT(m[11], 0.5f); // 16.0 (value/2)
425
426 // Save matrix as kernel CSC coefficients
427 pState->pfnMapCSCMatrix(csctype, matrix, pMatrix->Coeff);
428 }
429
430 //---------------------------------------------------------------------------------------
431 // KernelDll_StartKernelSearch_Next - Starts kernel search
432 //
433 // Parameters:
434 // Kdll_State *pState - [in] Dynamic Linking State
435 // Kdll_FilterEntry *pFilter - [in] Search filter (array of search entries)
436 // int iFilterSize - [in] Search filter size
437 // Kdll_SearchState *pSearchState - [in/out] Kernel search state
438 //
439 // Output: none
440 //---------------------------------------------------------------------------------------
441 void KernelDll_StartKernelSearch_Next(
442 Kdll_State * pState,
443 Kdll_SearchState *pSearchState,
444 Kdll_FilterEntry *pFilter,
445 int32_t iFilterSize,
446 uint32_t uiIs64BInstrEnabled)
447 {
448 int32_t nLayer;
449
450 VP_RENDER_FUNCTION_ENTER;
451
452 // Reset all states
453 MOS_ZeroMemory(pSearchState, sizeof(Kdll_SearchState));
454
455 // Setup KDLL state
456 pSearchState->pKdllState = pState; // KDLL state
457
458 // Cleanup kernel table
459 pSearchState->KernelCount = 0; // # of kernels
460
461 // Cleanup patch data
462 memset(pSearchState->Patches, 0, sizeof(pSearchState->Patches));
463 memset(pSearchState->PatchID, -1, sizeof(pSearchState->PatchID));
464 pSearchState->PatchCount = 0;
465
466 // Copy original filter; filter will be modified as part of the search
467 if (pFilter && iFilterSize > 0)
468 {
469 MOS_SecureMemcpy(pSearchState->Filter, iFilterSize * sizeof(Kdll_FilterEntry), pFilter, iFilterSize * sizeof(Kdll_FilterEntry));
470 pSearchState->pFilter = pSearchState->Filter;
471 pSearchState->iFilterSize = iFilterSize;
472
473 // Copy the render target format
474 pSearchState->target_format = pSearchState->pFilter[iFilterSize - 1].format;
475
476 // Copy the render target tile type
477 pSearchState->target_tiletype = pSearchState->pFilter[iFilterSize - 1].tiletype;
478
479 // Indicate whether to use 64B save kernel for render target surface
480 if (uiIs64BInstrEnabled &&
481 ((pSearchState->target_tiletype == MOS_TILE_X) ||
482 (pSearchState->target_tiletype == MOS_TILE_LINEAR)))
483 {
484 pSearchState->b64BSaveEnabled = true;
485 }
486 }
487 }
488
489 void KernelDll_ModifyFunctionPointers_Next(Kdll_State *pState)
490 {
491 pState->pfnStartKernelSearch = KernelDll_StartKernelSearch_Next;
492 }
493
494 bool KernelDll_IsCspace(VPHAL_CSPACE cspace, VPHAL_CSPACE match)
495 {
496 switch (match)
497 {
498 case CSpace_RGB:
499 return (cspace == CSpace_sRGB ||
500 cspace == CSpace_stRGB);
501
502 case CSpace_YUV:
503 return (cspace == CSpace_BT709 ||
504 cspace == CSpace_BT601 ||
505 cspace == CSpace_BT601_FullRange ||
506 cspace == CSpace_BT709_FullRange ||
507 cspace == CSpace_xvYCC709 ||
508 cspace == CSpace_xvYCC601);
509
510 case CSpace_Gray:
511 return (cspace == CSpace_BT601Gray ||
512 cspace == CSpace_BT601Gray_FullRange);
513
514 case CSpace_Any:
515 return (cspace != CSpace_None);
516
517 case CSpace_BT2020:
518 return (cspace == CSpace_BT2020 ||
519 cspace == CSpace_BT2020_FullRange);
520
521 case CSpace_BT2020_RGB:
522 return (cspace == CSpace_BT2020_RGB ||
523 cspace == CSpace_BT2020_stRGB);
524
525 default:
526 return (cspace == match);
527 }
528
529 return false;
530 }
531
532 /*----------------------------------------------------------------------------
533 | Name : KernelDll_GetYuvRangeAndOffset
534 | Purpose : Get the YUV offset and excursion for the input color space
535 | Return : true if success else false
536 \---------------------------------------------------------------------------*/
537 bool KernelDll_GetYuvRangeAndOffset(
538 Kdll_CSpace cspace,
539 float * pLumaOffset,
540 float * pLumaExcursion,
541 float * pChromaZero,
542 float * pChromaExcursion)
543 {
544 bool res = true;
545
546 switch (cspace)
547 {
548 case CSpace_BT601_FullRange:
549 case CSpace_BT709_FullRange:
550 case CSpace_BT601Gray_FullRange:
551 case CSpace_BT2020_FullRange:
552 *pLumaOffset = 0.0f;
553 *pLumaExcursion = 255.0f;
554 *pChromaZero = 128.0f;
555 *pChromaExcursion = 255.0f;
556 break;
557
558 case CSpace_BT601:
559 case CSpace_BT709:
560 case CSpace_xvYCC601: // since matrix is the same as 601, use the same range
561 case CSpace_xvYCC709: // since matrix is the same as 709, use the same range
562 case CSpace_BT601Gray:
563 case CSpace_BT2020:
564 *pLumaOffset = 16.0f;
565 *pLumaExcursion = 219.0f;
566 *pChromaZero = 128.0f;
567 *pChromaExcursion = 224.0f;
568 break;
569
570 default:
571 res = false;
572 break;
573 }
574
575 return res;
576 }
577
578 /*----------------------------------------------------------------------------
579 | Name : KernelDll_GetRgbRangeAndOffset
580 | Purpose : Get the RGB offset and excursion for the input color space
581 | Return : true if success else false
582 \---------------------------------------------------------------------------*/
583 bool KernelDll_GetRgbRangeAndOffset(
584 Kdll_CSpace cspace,
585 float * pRgbOffset,
586 float * pRgbExcursion)
587 {
588 bool res = true;
589
590 switch (cspace)
591 {
592 case CSpace_sRGB:
593 case CSpace_BT2020_RGB:
594 *pRgbOffset = 0.0f;
595 *pRgbExcursion = 255.0f;
596 break;
597
598 case CSpace_stRGB:
599 case CSpace_BT2020_stRGB:
600 *pRgbOffset = 16.0f;
601 *pRgbExcursion = 219.0f;
602 break;
603
604 default:
605 res = false;
606 break;
607 }
608
609 return res;
610 }
611
612 /*----------------------------------------------------------------------------
613 | Name : KernelDll_CalcYuvToRgbMatrix
614 | Purpose : Given the YUV->RGB transfer matrix, get the final matrix after
615 | applying offsets and excursions.
616 |
617 | [R'] [R_o] [R_e/Y_e 0 0 ] [Y' - Y_o]
618 | [G'] = [R_o] + [YUVtoRGBCoeff (3x3 matrix)]. [ 0 R_e/C_e 0 ]. [Cb' - C_z]
619 | [B'] [R_o] [ 0 0 R_e/C_e]. [Cr' - C_z]
620 |
621 | [R'] = [C0 C1 C2] [Y' ] [C3] {Out pMatrix}
622 | [G'] = [C4 C5 C6].[Cb'] + [C7]
623 | [B'] = [C8 C9 C10] [Cr'] + [C11]
624 |
625 | Return : true if success else false
626 \---------------------------------------------------------------------------*/
627 bool KernelDll_CalcYuvToRgbMatrix(
628 Kdll_CSpace src, // [in] YUV Color space
629 Kdll_CSpace dst, // [in] RGB Color space
630 float * pTransferMatrix, // [in] Transfer matrix (3x3)
631 float * pOutMatrix) // [out] Conversion matrix (3x4)
632 {
633 bool res;
634 float Y_o, Y_e, C_z, C_e;
635 float R_o, R_e;
636
637 res = true;
638
639 res = KernelDll_GetRgbRangeAndOffset(dst, &R_o, &R_e);
640 if (res == false)
641 {
642 goto finish;
643 }
644
645 res = KernelDll_GetYuvRangeAndOffset(src, &Y_o, &Y_e, &C_z, &C_e);
646 if (res == false)
647 {
648 goto finish;
649 }
650
651 // after + (3x3)(3x3)
652 pOutMatrix[0] = pTransferMatrix[0] * R_e / Y_e;
653 pOutMatrix[4] = pTransferMatrix[3] * R_e / Y_e;
654 pOutMatrix[8] = pTransferMatrix[6] * R_e / Y_e;
655 pOutMatrix[1] = pTransferMatrix[1] * R_e / C_e;
656 pOutMatrix[5] = pTransferMatrix[4] * R_e / C_e;
657 pOutMatrix[9] = pTransferMatrix[7] * R_e / C_e;
658 pOutMatrix[2] = pTransferMatrix[2] * R_e / C_e;
659 pOutMatrix[6] = pTransferMatrix[5] * R_e / C_e;
660 pOutMatrix[10] = pTransferMatrix[8] * R_e / C_e;
661
662 // (3x1) - (3x3)(3x3)(3x1)
663 pOutMatrix[3] = R_o - (pOutMatrix[0] * Y_o + pOutMatrix[1] * C_z + pOutMatrix[2] * C_z);
664 pOutMatrix[7] = R_o - (pOutMatrix[4] * Y_o + pOutMatrix[5] * C_z + pOutMatrix[6] * C_z);
665 pOutMatrix[11] = R_o - (pOutMatrix[8] * Y_o + pOutMatrix[9] * C_z + pOutMatrix[10] * C_z);
666
667 finish:
668 return res;
669 }
670
671 /*----------------------------------------------------------------------------
672 | Name : KernelDll_CalcRgbToYuvMatrix
673 | Purpose : Given the RGB->YUV transfer matrix, get the final matrix after
674 | applying offsets and excursions.
675 |
676 | [Y' ] [Y_o - Y_e.R_o/R_e] [Y_e/R_e 0 0 ] [ RGB to YUV ] [R']
677 | [Cb'] = [C_z] + [ 0 C_e/R_e 0 ]. [Transfer matrix]. [G']
678 | [Cr'] [C_z] [ 0 0 C_e/R_e] [ 3x3 matrix ] [B']
679 |
680 | [Y' ] = [C0 C1 C2] [R'] [C3] {Out pMatrix}
681 | [Cb'] = [C4 C5 C6].[G'] + [C7]
682 | [Cr'] = [C8 C9 C10] [B'] + [C11]
683 |
684 | Return : true if success else false
685 \---------------------------------------------------------------------------*/
686 bool KernelDll_CalcRgbToYuvMatrix(
687 Kdll_CSpace src, // [in] RGB Color space
688 Kdll_CSpace dst, // [in] YUV Color space
689 float * pTransferMatrix, // [in] Transfer matrix (3x3)
690 float * pOutMatrix) // [out] Conversion matrix (3x4)
691 {
692 bool res;
693 float Y_o, Y_e, C_z, C_e;
694 float R_o, R_e;
695
696 res = true;
697
698 res = KernelDll_GetRgbRangeAndOffset(src, &R_o, &R_e);
699 if (res == false)
700 {
701 goto finish;
702 }
703
704 res = KernelDll_GetYuvRangeAndOffset(dst, &Y_o, &Y_e, &C_z, &C_e);
705 if (res == false)
706 {
707 goto finish;
708 }
709
710 // multiplication of + onwards
711 pOutMatrix[0] = pTransferMatrix[0] * Y_e / R_e;
712 pOutMatrix[1] = pTransferMatrix[1] * Y_e / R_e;
713 pOutMatrix[2] = pTransferMatrix[2] * Y_e / R_e;
714 pOutMatrix[4] = pTransferMatrix[3] * C_e / R_e;
715 pOutMatrix[5] = pTransferMatrix[4] * C_e / R_e;
716 pOutMatrix[6] = pTransferMatrix[5] * C_e / R_e;
717 pOutMatrix[8] = pTransferMatrix[6] * C_e / R_e;
718 pOutMatrix[9] = pTransferMatrix[7] * C_e / R_e;
719 pOutMatrix[10] = pTransferMatrix[8] * C_e / R_e;
720
721 // before +
722 pOutMatrix[3] = Y_o - Y_e * R_o / R_e;
723 pOutMatrix[7] = C_z;
724 pOutMatrix[11] = C_z;
725
726 finish:
727 return res;
728 }
729
730 /*----------------------------------------------------------------------------
731 | Name : KernelDll_CalcGrayCoeffs
732 | Purpose : Given CSC matrix, calculate the new matrix making Chroma zero.
733 | Chroma will be read from the surface, but we need to factor in C_z
734 | by adjusting this in the constant.
735 |
736 | [R'] = [C0 C1 C2] [Y' ] [C3] {Out pMatrix}
737 | [G'] = [C4 C5 C6].[C_z] + [C7]
738 | [B'] = [C8 C9 C10] [C_z] [C11]
739 |
740 | New C3 = C1 * C_z + C2 * C_z + C3
741 |
742 | Return : true if success else false
743 \---------------------------------------------------------------------------*/
744 bool KernelDll_CalcGrayCoeffs(
745 Kdll_CSpace src, // [in] YUV source Color space
746 float * pMatrix) // [in/out] Conversion matrix (3x4)
747 {
748 float Y_o, Y_e, C_z, C_e;
749 bool res;
750
751 res = true;
752
753 res = KernelDll_GetYuvRangeAndOffset(src, &Y_o, &Y_e, &C_z, &C_e);
754 if (res == false)
755 {
756 goto finish;
757 }
758
759 // Calculate the constant offset by factoring in C_z
760 pMatrix[3] = pMatrix[1] * C_z + pMatrix[2] * C_z + pMatrix[3];
761 pMatrix[7] = pMatrix[5] * C_z + pMatrix[6] * C_z + pMatrix[7];
762 pMatrix[11] = pMatrix[9] * C_z + pMatrix[10] * C_z + pMatrix[11];
763
764 // Nullify the effect of chroma read
765 pMatrix[1] = pMatrix[2] = 0;
766 pMatrix[5] = pMatrix[6] = 0;
767 pMatrix[9] = pMatrix[10] = 0;
768
769 finish:
770 return res;
771 }
772
773 /*----------------------------------------------------------------------------
774 | Name : KernelDll_3x3MatrixProduct
775 | Purpose : Given two [3x4] input matrices, calculate [3x3]x[3x3] ignoring
776 | the last column in both inputs
777 | Return : none
778 \---------------------------------------------------------------------------*/
779 void KernelDll_3x3MatrixProduct(
780 float * dest,
781 const float *m1,
782 const float *m2)
783 {
784 dest[0] = m1[0] * m2[0] + m1[1] * m2[4] + m1[2] * m2[8];
785 dest[1] = m1[0] * m2[1] + m1[1] * m2[5] + m1[2] * m2[9];
786 dest[2] = m1[0] * m2[2] + m1[1] * m2[6] + m1[2] * m2[10];
787
788 dest[4] = m1[4] * m2[0] + m1[5] * m2[4] + m1[6] * m2[8];
789 dest[5] = m1[4] * m2[1] + m1[5] * m2[5] + m1[6] * m2[9];
790 dest[6] = m1[4] * m2[2] + m1[5] * m2[6] + m1[6] * m2[10];
791
792 dest[8] = m1[8] * m2[0] + m1[9] * m2[4] + m1[10] * m2[8];
793 dest[9] = m1[8] * m2[1] + m1[9] * m2[5] + m1[10] * m2[9];
794 dest[10] = m1[8] * m2[2] + m1[9] * m2[6] + m1[10] * m2[10];
795 }
796
797 /*----------------------------------------------------------------------------
798 | Name : KernelDll_CalcYuvToYuvMatrix
799 | Purpose : Calculate the matrix equation for converting b/w YUV color spaces.
800 | 1. Get conversion matrix from Source YUV to sRGB
801 | 2. Get conversion matrix from sRGB to Destination YUV
802 | 3. Apply the transformation below to get the final matrix
803 |
804 | [Y'dst] = [C0 C1 C2] [C0 C1 C2][Y'src] [C0 C1 C2] [C3] [C3]
805 | [U'] = [C4 C5 C6].[C4 C5 C6][C_z] + [C4 C5 C6].[C7] + [C7]
806 | [V'] = [C8 C9 C10] [C8 C9 C10][C_z] [C8 C9 C10] [C11] [C11]
807 | dst matrix src matrix dst matrix src dst
808 |
809 | [Y'dst] = [C0 C1 C2] [Y'src] [C3] {Out pMatrix}
810 | [U'] = [C4 C5 C6].[C_z] + [C7]
811 | [V'] = [C8 C9 C10] [C_z] [C11]
812 |
813 | Return : true if success else false
814 \---------------------------------------------------------------------------*/
815 bool KernelDll_CalcYuvToYuvMatrix(
816 Kdll_CSpace src, // [in] YUV Color space
817 Kdll_CSpace dst, // [in] YUV Color space
818 float * pOutMatrix) // [out] Conversion matrix (3x4)
819 {
820 float fYuvToRgb[12] = {0};
821 float fRgbToYuv[12] = {0};
822 bool res;
823
824 res = true;
825
826 // 1. Get conversion matrix from Source YUV to sRGB
827 if (IS_BT601_CSPACE(src))
828 {
829 res = KernelDll_CalcYuvToRgbMatrix(src, CSpace_sRGB, (float *)g_cCSC_BT601_YUV_RGB, fYuvToRgb);
830 }
831 else if(IS_COLOR_SPACE_BT2020_YUV(src))
832 {
833 switch (src)
834 {
835 case CSpace_BT2020:
836 res = KernelDll_CalcYuvToRgbMatrix(CSpace_BT2020, CSpace_sRGB, (float *)g_cCSC_BT2020_LimitedYUV_RGB, fYuvToRgb);
837 break;
838 case CSpace_BT2020_FullRange:
839 res = KernelDll_CalcYuvToRgbMatrix(CSpace_BT2020_FullRange, CSpace_sRGB, (float *)g_cCSC_BT2020_YUV_RGB, fYuvToRgb);
840 break;
841 default:
842 res = false;
843 break;
844 }
845 }
846 else
847 {
848 res = KernelDll_CalcYuvToRgbMatrix(src, CSpace_sRGB, (float *)g_cCSC_BT709_YUV_RGB, fYuvToRgb);
849 }
850 if (res == false)
851 {
852 goto finish;
853 }
854
855 // 2. Get conversion matrix from sRGB to Destination YUV
856 if (IS_BT601_CSPACE(dst))
857 {
858 res = KernelDll_CalcRgbToYuvMatrix(CSpace_sRGB, dst, (float *)g_cCSC_BT601_RGB_YUV, fRgbToYuv);
859 }
860 else if (IS_COLOR_SPACE_BT2020_YUV(dst))
861 {
862 switch (dst)
863 {
864 case CSpace_BT2020_FullRange:
865 res = KernelDll_CalcRgbToYuvMatrix(CSpace_sRGB, dst, (float *)g_cCSC_BT2020_RGB_YUV, fRgbToYuv);
866 break;
867 case CSpace_BT2020:
868 res = KernelDll_CalcRgbToYuvMatrix(CSpace_sRGB, dst, (float *)g_cCSC_BT2020_RGB_LimitedYUV, fRgbToYuv);
869 break;
870 default:
871 res = false;
872 break;
873 }
874 }
875 else
876 {
877 res = KernelDll_CalcRgbToYuvMatrix(CSpace_sRGB, dst, (float *)g_cCSC_BT709_RGB_YUV, fRgbToYuv);
878 }
879 if (res == false)
880 {
881 goto finish;
882 }
883
884 // 3. Multiply the 2 matrices above
885 KernelDll_3x3MatrixProduct(pOutMatrix, fRgbToYuv, fYuvToRgb);
886
887 // Perform [3x3][3x1] matrix multiply + [3x1] matrix
888 pOutMatrix[3] = fRgbToYuv[0] * fYuvToRgb[3] + fRgbToYuv[1] * fYuvToRgb[7] +
889 fRgbToYuv[2] * fYuvToRgb[11] + fRgbToYuv[3];
890 pOutMatrix[7] = fRgbToYuv[4] * fYuvToRgb[3] + fRgbToYuv[5] * fYuvToRgb[7] +
891 fRgbToYuv[6] * fYuvToRgb[11] + fRgbToYuv[7];
892 pOutMatrix[11] = fRgbToYuv[8] * fYuvToRgb[3] + fRgbToYuv[9] * fYuvToRgb[7] +
893 fRgbToYuv[10] * fYuvToRgb[11] + fRgbToYuv[11];
894
895 finish:
896 return res;
897 }
898
899 /*----------------------------------------------------------------------------
900 | Name : KernelDll_GetCSCMatrix
901 | Purpose : Get the required matrix for the given CSC conversion
902 | Return :
903 \---------------------------------------------------------------------------*/
904 void KernelDll_GetCSCMatrix(
905 Kdll_CSpace src, // [in] Source Color space
906 Kdll_CSpace dst, // [in] Destination Color space
907 float * pCSC_Matrix) // [out] CSC matrix to use
908 {
909 bool bMatrix;
910 bool bSrcGray;
911 Kdll_CSpace temp;
912 int32_t i;
913
914 bMatrix = false;
915 bSrcGray = KernelDll_IsCspace(src, CSpace_Gray);
916
917 // convert gray color spaces to its equivalent non-gray cpsace
918 switch (src)
919 {
920 case CSpace_BT601Gray:
921 temp = CSpace_BT601;
922 break;
923 case CSpace_BT601Gray_FullRange:
924 temp = CSpace_BT601_FullRange;
925 break;
926 default:
927 temp = src;
928 break;
929 }
930
931 // BT601/709 YUV to sRGB/stRGB conversion
932 if (KernelDll_IsCspace(temp, CSpace_YUV) || KernelDll_IsCspace(temp, CSpace_Gray))
933 {
934 if (KernelDll_IsCspace(dst, CSpace_RGB))
935 {
936 if (IS_BT601_CSPACE(temp))
937 {
938 KernelDll_CalcYuvToRgbMatrix(temp, dst, (float *)g_cCSC_BT601_YUV_RGB, pCSC_Matrix);
939 bMatrix = true;
940 }
941 else // if (IS_BT709_CSPACE(temp))
942 {
943 KernelDll_CalcYuvToRgbMatrix(temp, dst, (float *)g_cCSC_BT709_YUV_RGB, pCSC_Matrix);
944 bMatrix = true;
945 }
946 }
947 }
948 // sRGB/stRGB to BT601/709 YUV conversion
949 else if (KernelDll_IsCspace(temp, CSpace_RGB))
950 {
951 if (KernelDll_IsCspace(dst, CSpace_YUV))
952 {
953 if (IS_BT601_CSPACE(dst))
954 {
955 KernelDll_CalcRgbToYuvMatrix(temp, dst, (float *)g_cCSC_BT601_RGB_YUV, pCSC_Matrix);
956 bMatrix = true;
957 }
958 else // if (IS_BT709_CSPACE(temp))
959 {
960 KernelDll_CalcRgbToYuvMatrix(temp, dst, (float *)g_cCSC_BT709_RGB_YUV, pCSC_Matrix);
961 bMatrix = true;
962 }
963 }
964 }
965 // BT2020 YUV to RGB conversion
966 else if (KernelDll_IsCspace(temp, CSpace_BT2020))
967 {
968 if (KernelDll_IsCspace(dst, CSpace_BT2020_RGB))
969 {
970 KernelDll_CalcYuvToRgbMatrix(temp, dst, (float *)g_cCSC_BT2020_YUV_RGB, pCSC_Matrix);
971 bMatrix = true;
972 }
973 }
974 // BT2020 RGB to YUV conversion
975 else if (KernelDll_IsCspace(temp, CSpace_BT2020_RGB))
976 {
977 if (KernelDll_IsCspace(dst, CSpace_BT2020))
978 {
979 KernelDll_CalcRgbToYuvMatrix(temp, dst, (float *)g_cCSC_BT2020_RGB_YUV, pCSC_Matrix);
980 bMatrix = true;
981 }
982 }
983
984 // If matrix has not been derived yet, its one of the below special cases
985 if (!bMatrix)
986 {
987 if (temp == dst) // Check if its identity matrix
988 {
989 MOS_SecureMemcpy(pCSC_Matrix, sizeof(g_cCSC_Identity), (void *)g_cCSC_Identity, sizeof(g_cCSC_Identity));
990 }
991 else if (KernelDll_IsCspace(temp, CSpace_RGB)) // sRGB to stRGB inter-conversions
992 {
993 if (temp == CSpace_sRGB)
994 {
995 MOS_SecureMemcpy(pCSC_Matrix, sizeof(g_cCSC_sRGB_stRGB), (void *)g_cCSC_sRGB_stRGB, sizeof(g_cCSC_sRGB_stRGB));
996 }
997 else //temp == CSpace_stRGB
998 {
999 MOS_SecureMemcpy(pCSC_Matrix, sizeof(g_cCSC_stRGB_sRGB), (void *)g_cCSC_stRGB_sRGB, sizeof(g_cCSC_stRGB_sRGB));
1000 }
1001 }
1002 else if (KernelDll_IsCspace(temp, CSpace_YUV)) // 601 to 709 inter-conversions
1003 {
1004 KernelDll_CalcYuvToYuvMatrix(temp, dst, pCSC_Matrix);
1005 }
1006 else if (KernelDll_IsCspace(temp, CSpace_BT2020_RGB))
1007 {
1008 if (temp == CSpace_BT2020_RGB) //BT2020_RGB to BT2020_limited_RGB conversions
1009 {
1010 MOS_SecureMemcpy(pCSC_Matrix, sizeof(g_cCSC_BT2020RGB_BT2020stRGB), (void *)g_cCSC_BT2020RGB_BT2020stRGB, sizeof(g_cCSC_BT2020RGB_BT2020stRGB));
1011 }
1012 else if (temp == CSpace_BT2020_stRGB) //BT2020_limited_RGB to BT2020_RGB conversions
1013 {
1014 MOS_SecureMemcpy(pCSC_Matrix, sizeof(g_cCSC_BT2020stRGB_BT2020RGB), (void *)g_cCSC_BT2020stRGB_BT2020RGB, sizeof(g_cCSC_BT2020stRGB_BT2020RGB));
1015 }
1016 }
1017 else if (KernelDll_IsCspace(temp, CSpace_BT2020)) // BT2020 limited_YUV to BT2020_FullRange_YUV conversions
1018 {
1019 KernelDll_CalcYuvToYuvMatrix(temp, dst, pCSC_Matrix);
1020 }
1021 else
1022 {
1023 VP_RENDER_ASSERTMESSAGE("Not supported color space conversion(from %d to %d)", src, dst);
1024 MT_ERR2(MT_VP_KERNEL_CSC, MT_VP_KERNEL_CSPACE, src, MT_VP_KERNEL_CSPACE, dst);
1025 }
1026 }
1027
1028 // Calculate the Gray transformation matrix now
1029 if (bSrcGray)
1030 {
1031 KernelDll_CalcGrayCoeffs(src, pCSC_Matrix);
1032 }
1033
1034 VP_RENDER_NORMALMESSAGE("");
1035 for (i = 0; i < 3; i++)
1036 {
1037 VP_RENDER_NORMALMESSAGE("%f\t%f\t%f\t%f",
1038 pCSC_Matrix[4 * i],
1039 pCSC_Matrix[4 * i + 1],
1040 pCSC_Matrix[4 * i + 2],
1041 pCSC_Matrix[4 * i + 3]);
1042 }
1043 }
1044
1045 bool KernelDll_MapCSCMatrix(
1046 Kdll_CSCType csctype,
1047 const float *matrix,
1048 short * coeff)
1049 {
1050 // Unified kernel architecture requires that the color space
1051 // conversion coefficients programmed in specific orders, depends on the
1052 // type of the color space conversion.
1053 //
1054 // M (matrix) ---> C (coeff)
1055
1056 switch (csctype)
1057 {
1058 case CSC_YUV_RGB:
1059 // direct mapping from matrix to coeff
1060 coeff[0] = FLOAT_TO_SHORT(matrix[0]); // M0 --> C0
1061 coeff[1] = FLOAT_TO_SHORT(matrix[1]); // M1 --> C1
1062 coeff[2] = FLOAT_TO_SHORT(matrix[2]); // M2 --> C2
1063 coeff[3] = FLOAT_TO_SHORT(matrix[3]); // M3 --> C3
1064 coeff[4] = FLOAT_TO_SHORT(matrix[4]); // M4 --> C4
1065 coeff[5] = FLOAT_TO_SHORT(matrix[5]); // M5 --> C5
1066 coeff[6] = FLOAT_TO_SHORT(matrix[6]); // M6 --> C6
1067 coeff[7] = FLOAT_TO_SHORT(matrix[7]); // M7 --> C7
1068 coeff[8] = FLOAT_TO_SHORT(matrix[8]); // M8 --> C8
1069 coeff[9] = FLOAT_TO_SHORT(matrix[9]); // M9 --> C9
1070 coeff[10] = FLOAT_TO_SHORT(matrix[10]); // M10 --> C10
1071 coeff[11] = FLOAT_TO_SHORT(matrix[11]); // M11 --> C11
1072 break;
1073
1074 case CSC_RGB_YUV:
1075 coeff[6] = FLOAT_TO_SHORT(matrix[0]); // M0 --> C6
1076 coeff[4] = FLOAT_TO_SHORT(matrix[1]); // M1 --> C4
1077 coeff[5] = FLOAT_TO_SHORT(matrix[2]); // M2 --> C5
1078 coeff[7] = FLOAT_TO_SHORT(matrix[3]); // M3 --> C7
1079 coeff[10] = FLOAT_TO_SHORT(matrix[4]); // M4 --> C10
1080 coeff[8] = FLOAT_TO_SHORT(matrix[5]); // M5 --> C8
1081 coeff[9] = FLOAT_TO_SHORT(matrix[6]); // M6 --> C9
1082 coeff[11] = FLOAT_TO_SHORT(matrix[7]); // M7 --> C11
1083 coeff[2] = FLOAT_TO_SHORT(matrix[8]); // M8 --> C2
1084 coeff[0] = FLOAT_TO_SHORT(matrix[9]); // M9 --> C0
1085 coeff[1] = FLOAT_TO_SHORT(matrix[10]); // M10 --> C1
1086 coeff[3] = FLOAT_TO_SHORT(matrix[11]); // M11 --> C3
1087 break;
1088
1089 case CSC_YUV_YUV:
1090 coeff[4] = FLOAT_TO_SHORT(matrix[0]); // M0 --> C4
1091 coeff[5] = FLOAT_TO_SHORT(matrix[1]); // M1 --> C5
1092 coeff[6] = FLOAT_TO_SHORT(matrix[2]); // M2 --> C6
1093 coeff[7] = FLOAT_TO_SHORT(matrix[3]); // M3 --> C7
1094 coeff[8] = FLOAT_TO_SHORT(matrix[4]); // M4 --> C8
1095 coeff[9] = FLOAT_TO_SHORT(matrix[5]); // M5 --> C9
1096 coeff[10] = FLOAT_TO_SHORT(matrix[6]); // M6 --> C10
1097 coeff[11] = FLOAT_TO_SHORT(matrix[7]); // M7 --> C11
1098 coeff[0] = FLOAT_TO_SHORT(matrix[8]); // M8 --> C0
1099 coeff[1] = FLOAT_TO_SHORT(matrix[9]); // M9 --> C1
1100 coeff[2] = FLOAT_TO_SHORT(matrix[10]); // M10 --> C2
1101 coeff[3] = FLOAT_TO_SHORT(matrix[11]); // M11 --> C3
1102 break;
1103
1104 default:
1105 //CSC_RGB_RGB
1106 coeff[0] = FLOAT_TO_SHORT(matrix[1]); // M1 --> C0
1107 coeff[1] = FLOAT_TO_SHORT(matrix[2]); // M2 --> C1
1108 coeff[2] = FLOAT_TO_SHORT(matrix[0]); // M0 --> C2
1109 coeff[3] = FLOAT_TO_SHORT(matrix[3]); // M3 --> C3
1110 coeff[4] = FLOAT_TO_SHORT(matrix[5]); // M5 --> C4
1111 coeff[5] = FLOAT_TO_SHORT(matrix[6]); // M6 --> C5
1112 coeff[6] = FLOAT_TO_SHORT(matrix[4]); // M4 --> C6
1113 coeff[7] = FLOAT_TO_SHORT(matrix[7]); // M7 --> C7
1114 coeff[8] = FLOAT_TO_SHORT(matrix[9]); // M9 --> C8
1115 coeff[9] = FLOAT_TO_SHORT(matrix[10]); // M10 --> C9
1116 coeff[10] = FLOAT_TO_SHORT(matrix[8]); // M8 --> C10
1117 coeff[11] = FLOAT_TO_SHORT(matrix[11]); // M11 --> C11
1118 break;
1119 }
1120
1121 return true;
1122 }
1123
1124 bool KernelDll_IsFormat(
1125 MOS_FORMAT format,
1126 VPHAL_CSPACE cspace,
1127 MOS_FORMAT match)
1128 {
1129 switch (match)
1130 {
1131 case Format_Any:
1132 return (format != Format_None);
1133 break;
1134
1135 case Format_RGB_Swap:
1136 return (IS_RGB_SWAP(format));
1137
1138 case Format_RGB_No_Swap:
1139 return (IS_RGB_NO_SWAP(format));
1140
1141 case Format_RGB:
1142 if (IS_PAL_FORMAT(format))
1143 {
1144 return (KernelDll_IsCspace(cspace, CSpace_RGB));
1145 }
1146 else
1147 {
1148 return (IS_RGB_FORMAT(format) && !IS_PL3_RGB_FORMAT(format));
1149 }
1150
1151 case Format_RGB32:
1152 return (IS_RGB32_FORMAT(format));
1153
1154 case Format_PA:
1155 if (IS_PAL_FORMAT(format))
1156 {
1157 return (KernelDll_IsCspace(cspace, CSpace_YUV));
1158 }
1159 else
1160 {
1161 return (IS_PA_FORMAT(format) ||
1162 format == Format_AUYV);
1163 }
1164
1165 case Format_PL2:
1166 return (IS_PL2_FORMAT(format));
1167
1168 case Format_PL2_UnAligned:
1169 return (IS_PL2_FORMAT_UnAligned(format));
1170
1171 case Format_PL3:
1172 return (IS_PL3_FORMAT(format));
1173
1174 case Format_PL3_RGB:
1175 return (IS_PL3_RGB_FORMAT(format));
1176
1177 case Format_AYUV:
1178 return (format == Format_AYUV);
1179
1180 case Format_PAL:
1181 return (IS_PAL_FORMAT(format));
1182
1183 default:
1184 return (format == match);
1185 }
1186
1187 return false;
1188 }
1189
1190 //---------------------------------------------------------------------------------------
1191 // KernelDll_SetupProcampParameters - Setup Kernel Procamp Parameters
1192 //
1193 // Parameters:
1194 // Kdll_State *pState - [in] Kernel dll State to release
1195 // Kdll_Procamp *pProcamp - [in] Pointer to array of Procamp Parameters
1196 // int32_t iProcampSize - [in] Size of the array
1197 //
1198 // Output: Pointer to allocated Kernel dll state
1199 // nullptr - Failed to allocate Kernel dll state
1200 //-----------------------------------------------------------------------------------------
1201 void KernelDll_SetupProcampParameters(Kdll_State *pState,
1202 Kdll_Procamp *pProcamp,
1203 int32_t iProcampSize)
1204 {
1205 VP_RENDER_FUNCTION_ENTER;
1206
1207 // Setup pointer to procamp parameters
1208 pState->pProcamp = pProcamp;
1209 pState->iProcampSize = iProcampSize;
1210 }
1211
1212 //--------------------------------------------------------------
1213 // Fowler/Noll/Vo FNV-1a hash algorithm - public domain
1214 //--------------------------------------------------------------
1215 uint32_t KernelDll_SimpleHash(void *pData, int32_t iSize)
1216 {
1217 static const uint32_t k = 0x1000193;
1218 uint32_t hash = 0x811c9dc5;
1219 char *p = (char *)pData;
1220
1221 for(; iSize > 0; iSize--)
1222 {
1223 hash ^= (*p++);
1224 hash *= k;
1225 }
1226
1227 return hash;
1228 }
1229
1230 //--------------------------------------------------------------
1231 // KernelDll_GetCombinedKernel - Search combined kernel
1232 //--------------------------------------------------------------
1233 Kdll_CacheEntry *KernelDll_GetCombinedKernel(
1234 Kdll_State *pState,
1235 Kdll_FilterEntry *pFilter,
1236 int32_t iFilterSize,
1237 uint32_t dwHash)
1238 {
1239 Kdll_KernelHashTable *pHashTable;
1240 Kdll_KernelHashEntry *entries, *curr, *next;
1241 uint32_t folded_hash;
1242 uint16_t entry;
1243
1244 VP_RENDER_FUNCTION_ENTER;
1245
1246 // Get hash table
1247 pHashTable = &pState->KernelHashTable;
1248
1249 // fold hash from 32 to 8 bit :-)
1250 FOLD_HASH(folded_hash, dwHash)
1251
1252 // No entries
1253 entry = pHashTable->wHashTable[folded_hash];
1254 if (entry == 0 || entry > DL_MAX_COMBINED_KERNELS ) return nullptr;
1255
1256 entries = (&pHashTable->HashEntry[0]) - 1; // all indices are 1 based (0 means null)
1257 curr = &entries[entry];
1258 for (; (curr != nullptr); curr = next)
1259 {
1260 // match 32-bit hash, then compare filter
1261 if (curr->dwHash == dwHash &&
1262 curr->iFilter == iFilterSize)
1263 {
1264 if (memcmp(curr->pFilter, pFilter, iFilterSize * sizeof(Kdll_FilterEntry)) == 0)
1265 {
1266 break;
1267 }
1268 }
1269
1270 // Next entry with the same 8-bit folded hash
1271 next = (curr->next) ? (&entries[curr->next]) : nullptr;
1272 }
1273
1274 if (curr)
1275 { // Kernel already cached
1276 curr->pCacheEntry->dwRefresh = pState->dwRefresh++;
1277 return (curr->pCacheEntry);
1278 }
1279 else
1280 { // Kernel must be built
1281 return nullptr;
1282 }
1283 }
1284
1285 /*----------------------------------------------------------------------------
1286 | Name : KernelDll_FindRule
1287 | Purpose : Find a rule that matches the current search/input state
1288 |
1289 | Input : pState - Kernel Dll state
1290 | pSearchState - current DL search state
1291 |
1292 | Return :
1293 \---------------------------------------------------------------------------*/
1294 bool KernelDll_FindRule(
1295 Kdll_State * pState,
1296 Kdll_SearchState *pSearchState)
1297 {
1298 uint32_t parser_state = (uint32_t)pSearchState->state;
1299 Kdll_RuleEntrySet * pRuleSet;
1300 const Kdll_RuleEntry *pRuleEntry;
1301 int32_t iRuleCount;
1302 int32_t iMatchCount;
1303 bool bLayerFormatMatched;
1304 bool bSrc0FormatMatched;
1305 bool bSrc1FormatMatched;
1306 bool bTargetFormatMatched;
1307 bool bSrc0SampingMatched;
1308
1309 VP_RENDER_FUNCTION_ENTER;
1310
1311 // All Custom states are handled as a single group
1312 if (parser_state >= Parser_Custom)
1313 {
1314 parser_state = Parser_Custom;
1315 }
1316
1317 pRuleSet = pState->pDllRuleTable[parser_state];
1318 iRuleCount = pState->iDllRuleCount[parser_state];
1319
1320 if (pRuleSet == nullptr || iRuleCount == 0)
1321 {
1322 VP_RENDER_NORMALMESSAGE("Search rules undefined.");
1323 pSearchState->pMatchingRuleSet = nullptr;
1324 return false;
1325 }
1326
1327 // Search matching entry
1328 for (; iRuleCount > 0; iRuleCount--, pRuleSet++)
1329 {
1330 // Points to the first rule, get number of matches
1331 pRuleEntry = pRuleSet->pRuleEntry;
1332 iMatchCount = pRuleSet->iMatchCount;
1333
1334 // Initialize for each Ruleset
1335 bLayerFormatMatched = false;
1336 bSrc0FormatMatched = false;
1337 bSrc1FormatMatched = false;
1338 bTargetFormatMatched = false;
1339 bSrc0SampingMatched = false;
1340
1341 // Match all rules within the same RuleSet
1342 for (; iMatchCount > 0; iMatchCount--, pRuleEntry++)
1343 {
1344 switch (pRuleEntry->id)
1345 {
1346 // Match current Parser State
1347 case RID_IsParserState:
1348 if (pSearchState->state == (Kdll_ParserState)pRuleEntry->value)
1349 {
1350 continue;
1351 }
1352 else
1353 {
1354 break;
1355 }
1356
1357 // Match render method
1358 case RID_IsRenderMethod:
1359 if (pSearchState->pFilter->RenderMethod == (Kdll_RenderMethod)pRuleEntry->value)
1360 {
1361 continue;
1362 }
1363 else
1364 {
1365 break;
1366 }
1367
1368 // Match target color space
1369 case RID_IsTargetCspace:
1370 if (KernelDll_IsCspace(pSearchState->cspace, (VPHAL_CSPACE)pRuleEntry->value))
1371 {
1372 continue;
1373 }
1374 else
1375 {
1376 break;
1377 }
1378
1379 // Match current layer ID
1380 case RID_IsLayerID:
1381 if (pSearchState->pFilter->layer == (Kdll_Layer)pRuleEntry->value)
1382 {
1383 continue;
1384 }
1385 else
1386 {
1387 break;
1388 }
1389
1390 // Match current layer format
1391 case RID_IsLayerFormat:
1392 if (pRuleEntry->logic == Kdll_Or && bLayerFormatMatched)
1393 {
1394 // Already found matching format in the ruleset
1395 continue;
1396 }
1397 else
1398 {
1399 // Check if the layer format matches the rule
1400 if (KernelDll_IsFormat(pSearchState->pFilter->format,
1401 pSearchState->pFilter->cspace,
1402 (MOS_FORMAT)pRuleEntry->value))
1403 {
1404 bLayerFormatMatched = true;
1405 }
1406
1407 if (pRuleEntry->logic == Kdll_None && !bLayerFormatMatched)
1408 {
1409 // Last entry and No matching format was found
1410 break;
1411 }
1412 else
1413 {
1414 continue;
1415 }
1416 }
1417
1418 // Match shuffling requirement
1419 case RID_IsShuffling:
1420 if (pSearchState->ShuffleSamplerData == (Kdll_Shuffling)pRuleEntry->value)
1421 {
1422 continue;
1423 }
1424 else
1425 {
1426 break;
1427 }
1428
1429 // Check if RT rotates
1430 case RID_IsRTRotate:
1431 if (pSearchState->bRTRotate == (pRuleEntry->value ? true : false))
1432 {
1433 continue;
1434 }
1435 else
1436 {
1437 break;
1438 }
1439
1440 // Match current layer rotation
1441 case RID_IsLayerRotation:
1442 if (pSearchState->pFilter->rotation == (MEDIA_ROTATION)pRuleEntry->value)
1443 {
1444 continue;
1445 }
1446 else
1447 {
1448 break;
1449 }
1450
1451 // Match Src0 source format (surface)
1452 case RID_IsSrc0Format:
1453 if (pRuleEntry->logic == Kdll_Or && bSrc0FormatMatched)
1454 {
1455 // Already found matching format in the ruleset
1456 continue;
1457 }
1458 else
1459 {
1460 // Check if the source 0 format matches the rule
1461 // The intermediate colorspace is used to determine
1462 // if palettized input is given in RGB or YUV format.
1463 if (KernelDll_IsFormat(pSearchState->src0_format,
1464 pSearchState->cspace,
1465 (MOS_FORMAT)pRuleEntry->value))
1466 {
1467 bSrc0FormatMatched = true;
1468 }
1469
1470 if (pRuleEntry->logic == Kdll_None && !bSrc0FormatMatched)
1471 {
1472 // Last entry and No matching format was found
1473 break;
1474 }
1475 else
1476 {
1477 continue;
1478 }
1479 }
1480
1481 // Match Src0 sampling mode
1482 case RID_IsSrc0Sampling:
1483 // Check if the layer format matches the rule
1484 if (pSearchState->src0_sampling == (Kdll_Sampling)pRuleEntry->value)
1485 {
1486 bSrc0SampingMatched = true;
1487 continue;
1488 }
1489 else if (bSrc0SampingMatched || pRuleEntry->logic == Kdll_Or)
1490 {
1491 continue;
1492 }
1493 else if ((Kdll_Sampling)pRuleEntry->value == Sample_Any &&
1494 pSearchState->src0_sampling != Sample_None)
1495 {
1496 continue;
1497 }
1498 else
1499 {
1500 break;
1501 }
1502
1503 // Match Src0 rotation
1504 case RID_IsSrc0Rotation:
1505 if (pSearchState->src0_rotation == (MEDIA_ROTATION)pRuleEntry->value)
1506 {
1507 continue;
1508 }
1509 else
1510 {
1511 break;
1512 }
1513
1514 // Match Src0 Colorfill
1515 case RID_IsSrc0ColorFill:
1516 if (pSearchState->src0_colorfill == (int32_t)pRuleEntry->value)
1517 {
1518 continue;
1519 }
1520 else
1521 {
1522 break;
1523 }
1524
1525 // Match Src0 Luma Key
1526 case RID_IsSrc0LumaKey:
1527 if (pSearchState->src0_lumakey == (int32_t)pRuleEntry->value)
1528 {
1529 continue;
1530 }
1531 else
1532 {
1533 break;
1534 }
1535
1536 // Match Src0 Procamp
1537 case RID_IsSrc0Procamp:
1538 if (pSearchState->pFilter->procamp == (int32_t)pRuleEntry->value)
1539 {
1540 continue;
1541 }
1542 else
1543 {
1544 break;
1545 }
1546
1547 // Match Src0 CSC coefficients
1548 case RID_IsSrc0Coeff:
1549 if (pSearchState->src0_coeff == (Kdll_CoeffID)pRuleEntry->value)
1550 {
1551 continue;
1552 }
1553 else if ((Kdll_CoeffID)pRuleEntry->value == CoeffID_Any &&
1554 pSearchState->src0_coeff != CoeffID_None)
1555 {
1556 continue;
1557 }
1558 else
1559 {
1560 break;
1561 }
1562
1563 // Match Src0 CSC coefficients setting mode
1564 case RID_IsSetCoeffMode:
1565 if (pSearchState->pFilter->SetCSCCoeffMode == (Kdll_SetCSCCoeffMethod)pRuleEntry->value)
1566 {
1567 continue;
1568 }
1569 else
1570 {
1571 break;
1572 }
1573
1574 // Match Src0 processing mode
1575 case RID_IsSrc0Processing:
1576 if (pSearchState->src0_process == (Kdll_Processing)pRuleEntry->value)
1577 {
1578 continue;
1579 }
1580 if ((Kdll_Processing)pRuleEntry->value == Process_Any &&
1581 pSearchState->src0_process != Process_None)
1582 {
1583 continue;
1584 }
1585 else
1586 {
1587 break;
1588 }
1589
1590 // Match Src0 chromasiting mode
1591 case RID_IsSrc0Chromasiting:
1592 if (pSearchState->Filter->chromasiting == (int32_t)pRuleEntry->value)
1593 {
1594 continue;
1595 }
1596 else
1597 {
1598 break;
1599 }
1600
1601 // Match Src1 source format (surface)
1602 case RID_IsSrc1Format:
1603 if (pRuleEntry->logic == Kdll_Or && bSrc1FormatMatched)
1604 {
1605 // Already found matching format in the ruleset
1606 continue;
1607 }
1608 else
1609 {
1610 // Check if the source 1 format matches the rule
1611 // The intermediate colorspace is used to determine
1612 // if palettized input is given in RGB or YUV format.
1613 if (KernelDll_IsFormat(pSearchState->src1_format,
1614 pSearchState->cspace,
1615 (MOS_FORMAT)pRuleEntry->value))
1616 {
1617 bSrc1FormatMatched = true;
1618 }
1619
1620 if (pRuleEntry->logic == Kdll_None && !bSrc1FormatMatched)
1621 {
1622 // Last entry and No matching format was found
1623 break;
1624 }
1625 else
1626 {
1627 continue;
1628 }
1629 }
1630 // Match Src1 sampling mode
1631 case RID_IsSrc1Sampling:
1632 if (pSearchState->src1_sampling == (Kdll_Sampling)pRuleEntry->value)
1633 {
1634 continue;
1635 }
1636 else if ((Kdll_Sampling)pRuleEntry->value == Sample_Any &&
1637 pSearchState->src1_sampling != Sample_None)
1638 {
1639 continue;
1640 }
1641 else
1642 {
1643 break;
1644 }
1645
1646 // Match Src1 Luma Key
1647 case RID_IsSrc1LumaKey:
1648 if (pSearchState->src1_lumakey == (int32_t)pRuleEntry->value)
1649 {
1650 continue;
1651 }
1652 else
1653 {
1654 break;
1655 }
1656
1657 // Match Src1 Sampler LumaKey
1658 case RID_IsSrc1SamplerLumaKey:
1659 if (pSearchState->src1_samplerlumakey == (int32_t)pRuleEntry->value)
1660 {
1661 continue;
1662 }
1663 else
1664 {
1665 break;
1666 }
1667
1668 // Match Src1 Procamp
1669 case RID_IsSrc1Procamp:
1670 if (pSearchState->pFilter->procamp == (int32_t)pRuleEntry->value)
1671 {
1672 continue;
1673 }
1674 else
1675 {
1676 break;
1677 }
1678
1679 // Match Src1 CSC coefficients
1680 case RID_IsSrc1Coeff:
1681 if (pSearchState->src1_coeff == (Kdll_CoeffID)pRuleEntry->value)
1682 {
1683 continue;
1684 }
1685 else if ((Kdll_CoeffID)pRuleEntry->value == CoeffID_Any &&
1686 pSearchState->src1_coeff != CoeffID_None)
1687 {
1688 continue;
1689 }
1690 else
1691 {
1692 break;
1693 }
1694
1695 // Match Src1 processing mode
1696 case RID_IsSrc1Processing:
1697 if (pSearchState->src1_process == (Kdll_Processing)pRuleEntry->value)
1698 {
1699 continue;
1700 }
1701 if ((Kdll_Processing)pRuleEntry->value == Process_Any &&
1702 pSearchState->src1_process != Process_None)
1703 {
1704 continue;
1705 }
1706 else
1707 {
1708 break;
1709 }
1710
1711 // Match Src1 chromasiting mode
1712 case RID_IsSrc1Chromasiting:
1713 //pSearchState->pFilter is pointed to the real sub layer
1714 if (pSearchState->pFilter->chromasiting == (int32_t)pRuleEntry->value)
1715 {
1716 continue;
1717 }
1718 else
1719 {
1720 break;
1721 }
1722
1723 // Match Layer number
1724 case RID_IsLayerNumber:
1725 if (pSearchState->layer_number == (int32_t)pRuleEntry->value)
1726 {
1727 continue;
1728 }
1729 else
1730 {
1731 break;
1732 }
1733
1734 // Match quadrant
1735 case RID_IsQuadrant:
1736 if (pSearchState->quadrant == (int32_t)pRuleEntry->value)
1737 {
1738 continue;
1739 }
1740 else
1741 {
1742 break;
1743 }
1744
1745 // Set CSC flag before Mix
1746 case RID_IsCSCBeforeMix:
1747 if (pSearchState->bCscBeforeMix == (pRuleEntry->value ? true : false))
1748 {
1749 continue;
1750 }
1751 else
1752 {
1753 break;
1754 }
1755
1756 case RID_IsDualOutput:
1757 if (pSearchState->pFilter->dualout == (pRuleEntry->value ? true : false))
1758 {
1759 continue;
1760 }
1761 else
1762 {
1763 break;
1764 }
1765
1766 case RID_IsTargetFormat:
1767 if (pRuleEntry->logic == Kdll_Or && bTargetFormatMatched)
1768 {
1769 // Already found matching format in the ruleset
1770 continue;
1771 }
1772 else
1773 {
1774 if (pSearchState->target_format == (MOS_FORMAT)pRuleEntry->value)
1775 {
1776 bTargetFormatMatched = true;
1777 }
1778
1779 if (pRuleEntry->logic == Kdll_None && !bTargetFormatMatched)
1780 {
1781 // Last entry and No matching format was found
1782 break;
1783 }
1784 else
1785 {
1786 continue;
1787 }
1788 }
1789
1790 case RID_Is64BSaveEnabled:
1791 if (pSearchState->b64BSaveEnabled == (pRuleEntry->value ? true : false))
1792 {
1793 continue;
1794 }
1795 else
1796 {
1797 break;
1798 }
1799
1800 case RID_IsTargetTileType:
1801 if (pRuleEntry->logic == Kdll_None &&
1802 pSearchState->target_tiletype == (MOS_TILE_TYPE)pRuleEntry->value)
1803 {
1804 continue;
1805 }
1806 else if (pRuleEntry->logic == Kdll_Not &&
1807 pSearchState->target_tiletype != (MOS_TILE_TYPE)pRuleEntry->value)
1808 {
1809 continue;
1810 }
1811 else
1812 {
1813 break;
1814 }
1815
1816 case RID_IsProcampEnabled:
1817 if (pSearchState->bProcamp == (pRuleEntry->value ? true : false))
1818 {
1819 continue;
1820 }
1821 else
1822 {
1823 break;
1824 }
1825
1826 case RID_IsConstOutAlpha:
1827 if (pSearchState->pFilter->bFillOutputAlphaWithConstant == (pRuleEntry->value ? true : false))
1828 {
1829 continue;
1830 }
1831 else
1832 {
1833 break;
1834 }
1835
1836 case RID_IsDitherNeeded:
1837 if (pSearchState->pFilter->bIsDitherNeeded == (pRuleEntry->value ? true : false))
1838 {
1839 continue;
1840 }
1841 else
1842 {
1843 break;
1844 }
1845 // Undefined search rule will fail
1846 default:
1847 VP_RENDER_ASSERTMESSAGE("Invalid rule %d @ layer %d, state %d.", pRuleEntry->id, pSearchState->layer_number, pSearchState->state);
1848 MT_ERR3(MT_VP_KERNEL_RULE, MT_VP_KERNEL_RULE_ID, pRuleEntry->id, MT_VP_KERNEL_RULE_LAYERNUM, pSearchState->layer_number, MT_VP_KERNEL_RULE_SEARCH_STATE, pSearchState->state);
1849 break;
1850 } // End of switch to deal with all matching rule IDs
1851
1852 // Rule didn't match - try another RuleSet
1853 break;
1854 } // End of file loop to test all rules for the current RuleSet
1855
1856 // Match
1857 if (iMatchCount == 0)
1858 {
1859 pSearchState->pMatchingRuleSet = pRuleSet;
1860 return true;
1861 }
1862 } // End of for loop to test all RuleSets for the current parser state
1863
1864 // Failed to find a matching rule -> kernel search will fail
1865 VP_RENDER_NORMALMESSAGE("Fail to find a matching rule @ layer %d, state %d.", pSearchState->layer_number, pSearchState->state);
1866 MT_ERR2(MT_VP_KERNEL_RULE, MT_VP_KERNEL_RULE_LAYERNUM, pSearchState->layer_number, MT_VP_KERNEL_RULE_SEARCH_STATE, pSearchState->state);
1867
1868 // No match -> return
1869 pSearchState->pMatchingRuleSet = nullptr;
1870 return false;
1871 }
1872
1873 //--------------------------------------------------------------
1874 // Append kernel, include symbols to resolve
1875 //--------------------------------------------------------------
1876 bool Kdll_AppendKernel(Kdll_KernelCache *pKernelCache,
1877 Kdll_SearchState * pSearchState,
1878 int32_t iKUID,
1879 Kdll_PatchData * pKernelPatch)
1880 {
1881 Kdll_State * pState;
1882 Kdll_Symbol * pSymbols;
1883 Kdll_CacheEntry *kernels;
1884 Kdll_LinkData * link;
1885 Kdll_LinkData * liSearch_reloc;
1886 uint8_t * kernel;
1887 int * size;
1888 int * left;
1889 int dwSize;
1890 int i;
1891 int base;
1892 bool bInline;
1893 bool res;
1894
1895 VP_RENDER_FUNCTION_ENTER;
1896
1897 res = false;
1898
1899 // Check if Kernel ID is valid
1900 if (iKUID >= pKernelCache->iCacheEntries)
1901 {
1902 VP_RENDER_NORMALMESSAGE("invalid Kernel ID %d.", iKUID);
1903 goto cleanup;
1904 }
1905
1906 // Get KDLL state
1907 pState = pSearchState->pKdllState;
1908
1909 // Get current combined kernel
1910 kernel = pSearchState->Kernel;
1911 size = &pSearchState->KernelSize;
1912 left = &pSearchState->KernelLeft;
1913 pSymbols = &pSearchState->KernelLink;
1914 base = (*size) >> 2;
1915
1916 // Find selected kernel and kernel size; check if there is enough space
1917 kernels = &pKernelCache->pCacheEntries[iKUID];
1918 dwSize = kernels->iSize;
1919 if (*left < dwSize)
1920 {
1921 VP_RENDER_NORMALMESSAGE("exceeded maximum kernel size.");
1922 goto cleanup;
1923 }
1924
1925 // Check if there is enough space for symbols
1926 if (pSymbols->dwCount + kernels->nLink >= pSymbols->dwSize)
1927 {
1928 VP_RENDER_NORMALMESSAGE("exceeded maximum numbers of symbols to resolve.");
1929 goto cleanup;
1930 }
1931
1932 #if EMUL || VPHAL_LIB
1933 VP_RENDER_NORMALMESSAGE("%s.", kernels->szName);
1934
1935 if (pState->pfnCbListKernel)
1936 {
1937 pState->pfnCbListKernel(pState->pToken, kernels->szName);
1938 }
1939 #elif _DEBUG // EMUL || VPHAL_LIB
1940 VP_RENDER_NORMALMESSAGE("%s.", kernels->szName);
1941 #endif // _DEBUG
1942
1943 // Append symbols to resolve, relocate symbols
1944 link = kernels->pLink;
1945 liSearch_reloc = pSymbols->pLink + pSymbols->dwCount;
1946
1947 bInline = false;
1948 if (link)
1949 {
1950 for (i = kernels->nLink; i > 0; i--, link++)
1951 {
1952 if (link->bInline)
1953 {
1954 // Inline code included
1955 if (!link->bExport)
1956 {
1957 bInline = true;
1958 }
1959 }
1960 else
1961 {
1962 *liSearch_reloc = *link;
1963 liSearch_reloc->dwOffset += base;
1964 liSearch_reloc++;
1965
1966 pSymbols->dwCount++;
1967 }
1968 }
1969 }
1970
1971 // Append kernel
1972 MOS_SecureMemcpy(&kernel[*size], dwSize, (void *)kernels->pBinary, dwSize);
1973
1974 // Patch kernel
1975 if (pKernelPatch)
1976 {
1977 uint8_t *pSource = pKernelPatch->Data;
1978 uint8_t *pDestination = kernel + (*size);
1979 int32_t i;
1980
1981 Kdll_PatchBlock *pBlock = pKernelPatch->Patch;
1982 for (i = pKernelPatch->nPatches; i > 0; i--, pBlock++)
1983 {
1984 MOS_SecureMemcpy(pDestination + pBlock->DstOffset, pBlock->BlockSize, (void *)(pSource + pBlock->SrcOffset), pBlock->BlockSize);
1985 }
1986 }
1987
1988 res = true;
1989 *size += dwSize;
1990 *left -= dwSize;
1991
1992 // Insert inline code
1993 if (bInline)
1994 {
1995 for (link = kernels->pLink, i = kernels->nLink; (i > 0) && (res); i--, link++)
1996 {
1997 if (link->bInline && (!link->bExport))
1998 {
1999 iKUID = pKernelCache->pExports[link->iLabelID].iKUID;
2000 res &= Kdll_AppendKernel(pKernelCache, pSearchState, iKUID, pKernelPatch);
2001 }
2002 }
2003 }
2004
2005 cleanup:
2006 return res;
2007 }
2008
2009 //--------------------------------------------------------------
2010 // Resolve kernel dependencies and perform patching
2011 //--------------------------------------------------------------
2012 bool Kdll_ResolveKernelDependencies(
2013 Kdll_State * pState,
2014 Kdll_SearchState *pSearchState)
2015 {
2016 Kdll_KernelCache *cache = &pState->ComponentKernelCache;
2017 uint8_t * kernel = pSearchState->Kernel;
2018 Kdll_Symbol * pSymbols = &pSearchState->KernelLink;
2019 uint32_t nExports = cache->nExports;
2020 Kdll_LinkData * pExports = cache->pExports;
2021 Kdll_LinkData * pLink;
2022 int32_t iKUID;
2023 int32_t iOffset;
2024 uint32_t dwResolveOffset[DL_MAX_EXPORT_COUNT];
2025 bool bResolveDone;
2026 int32_t i;
2027 uint32_t * d;
2028
2029 VP_RENDER_FUNCTION_ENTER;
2030
2031 MOS_ZeroMemory(dwResolveOffset, sizeof(dwResolveOffset));
2032
2033 do
2034 {
2035 // Update exports
2036 for (pLink = pSymbols->pLink, i = pSymbols->dwCount; i > 0; i--, pLink++)
2037 {
2038 if (pLink->bExport)
2039 {
2040 dwResolveOffset[pLink->iLabelID] = pLink->dwOffset;
2041 }
2042 }
2043
2044 bResolveDone = true;
2045 for (pLink = pSymbols->pLink, i = pSymbols->dwCount; i > 0; i--, pLink++)
2046 {
2047 // validate label
2048 if (pLink->iLabelID > nExports || // invalid label
2049 pExports[pLink->iLabelID].bExport == 0) // label not in the export table
2050 {
2051 VP_RENDER_ASSERTMESSAGE("Invalid/unresolved label %d.", pLink->iLabelID);
2052 return false;
2053 }
2054
2055 // load dependencies
2056 if (!pLink->bExport && !dwResolveOffset[pLink->iLabelID])
2057 {
2058 // set flag for another pass as newly loaded
2059 // kernels may contain dependencies of their own
2060 bResolveDone = false;
2061
2062 // Load dependencies
2063 iKUID = pExports[pLink->iLabelID].iKUID;
2064 Kdll_AppendKernel(cache, pSearchState, iKUID, nullptr);
2065
2066 // Restart
2067 break;
2068 }
2069 } // for
2070 } while (!bResolveDone);
2071
2072 // All modules must be loaded by now, start patching
2073 for (pLink = pSymbols->pLink, i = pSymbols->dwCount; i > 0; i--, pLink++)
2074 {
2075 iOffset = (int32_t)dwResolveOffset[pLink->iLabelID] - 4;
2076 iOffset -= pLink->dwOffset;
2077
2078 d = ((uint32_t *)kernel) + pLink->dwOffset;
2079
2080 // Patch offset
2081 if (!pLink->bExport && !pLink->bInline)
2082 {
2083 d[3] = iOffset << 2; // jmpi - index * 8 bits
2084 }
2085 }
2086
2087 return true;
2088 }
2089
2090 //---------------------------------------------------------------------------------------
2091 // Kdll_SearchKernel - Performs full kernel search, including selection of best match
2092 // Search state must be initialized by KernelDll_StartKernelSearch
2093 //
2094 // Parameters:
2095 // Kdll_State *pState - [in] Dynamic Linking state
2096 // Kdll_SearchState *pSearchState - [in/out] Kernel search state
2097 //
2098 // Output: true if suceeded, false otherwise
2099 //---------------------------------------------------------------------------------------
2100 bool KernelDll_SearchKernel(Kdll_State *pState,
2101 Kdll_SearchState * pSearchState)
2102 {
2103 VP_RENDER_FUNCTION_ENTER;
2104
2105 // Check parameters
2106 if ((!pSearchState) || pSearchState->iFilterSize < 1)
2107 {
2108 VP_RENDER_NORMALMESSAGE("Search is empty, must contain 2 or more layers.");
2109 return false;
2110 }
2111
2112 // Setup CSC; allocate and calculate CSC matrices
2113 if (!pState->pfnSetupCSC(pState, pSearchState))
2114 {
2115 VP_RENDER_NORMALMESSAGE("CSC setup failed.");
2116 return false;
2117 }
2118
2119 // Initial search states
2120 pSearchState->bCscBeforeMix = false;
2121 pSearchState->state = Parser_Begin;
2122 pSearchState->cspace = pSearchState->CscParams.ColorSpace;
2123 pSearchState->quadrant = 0;
2124 pSearchState->layer_number = 0;
2125
2126 pSearchState->pMatchingRuleSet = nullptr;
2127
2128 // Reset Src0 state
2129 pSearchState->src0_format = Format_None;
2130 pSearchState->src0_sampling = Sample_None;
2131 pSearchState->src0_colorfill = false;
2132 pSearchState->src0_lumakey = LumaKey_False;
2133 pSearchState->src0_coeff = CoeffID_None;
2134
2135 // Reset Src1 state
2136 pSearchState->src1_format = Format_None;
2137 pSearchState->src1_sampling = Sample_None;
2138 pSearchState->src1_lumakey = LumaKey_False;
2139 pSearchState->src1_samplerlumakey = LumaKey_False;
2140 pSearchState->src1_coeff = CoeffID_None;
2141 pSearchState->src1_process = Process_None;
2142
2143 // Search loop
2144 while (pSearchState->state != Parser_End)
2145 {
2146 #if EMUL || VPHAL_LIB
2147 if (pState->pfnCbSearchSate)
2148 {
2149 pState->pfnCbSearchSate(pState->pToken, CB_REASON_BEGIN_SEARCH, pSearchState);
2150 }
2151 #endif
2152 // Find rule that matches
2153 if (!pState->pfnFindRule(pState, pSearchState))
2154 {
2155 #if EMUL || VPHAL_LIB
2156 if (pState->pfnCbSearchSate)
2157 {
2158 pState->pfnCbSearchSate(pState->pToken, CB_REASON_SEARCH_FAILED, pSearchState);
2159 }
2160 #endif
2161 return false;
2162 }
2163
2164 #if EMUL || VPHAL_LIB
2165 if (pState->pfnCbSearchSate)
2166 {
2167 pState->pfnCbSearchSate(pState->pToken, CB_REASON_BEGIN_UPDATE, pSearchState);
2168 }
2169 #endif
2170 // Update state
2171 if (!pState->pfnUpdateState(pState, pSearchState))
2172 {
2173 #if EMUL || VPHAL_LIB
2174 if (pState->pfnCbSearchSate)
2175 {
2176 pState->pfnCbSearchSate(pState->pToken, CB_REASON_UPDATE_FAILED, pSearchState);
2177 }
2178 #endif
2179 return false;
2180 }
2181 }
2182
2183 #if EMUL || VPHAL_LIB
2184 if (pState->pfnCbSearchSate)
2185 {
2186 pState->pfnCbSearchSate(pState->pToken, CB_REASON_END_SEARCH, pSearchState);
2187 }
2188 #endif
2189
2190 VP_RENDER_VERBOSEMESSAGE("Search completed successfully.");
2191 return true;
2192 }
2193
2194 //--------------------------------------------------------------
2195 // KernelDll_BuildKernel - build kernel
2196 //--------------------------------------------------------------
2197 bool KernelDll_BuildKernel(Kdll_State *pState, Kdll_SearchState *pSearchState)
2198 {
2199 Kdll_KernelCache *pKernelCache = &pState->ComponentKernelCache;
2200 Kdll_KernelCache *pCustomCache = pState->pCustomKernelCache;
2201 Kdll_PatchData * pKernelPatch;
2202 bool res;
2203 int32_t offset = 0;
2204 int32_t * pKernelID, *pGroupID, *pPatchID;
2205
2206 VP_RENDER_FUNCTION_ENTER;
2207
2208 pSearchState->KernelLink.dwSize = DL_MAX_SYMBOLS;
2209 pSearchState->KernelLink.dwCount = 0;
2210 pSearchState->KernelLink.pLink = pSearchState->LinkArray;
2211 pSearchState->KernelSize = 0;
2212 pSearchState->KernelLeft = sizeof(pSearchState->Kernel);
2213 pSearchState->KernelLink.dwCount = 0;
2214
2215 #if EMUL || VPHAL_LIB || _DEBUG || _RELEASE_INTERNAL
2216 VP_RENDER_NORMALMESSAGE("Component Kernels:");
2217 #endif // EMUL || VPHAL_LIB || _DEBUG
2218
2219 pKernelID = pSearchState->KernelID;
2220 pGroupID = pSearchState->KernelGrp;
2221 pPatchID = pSearchState->PatchID;
2222
2223 for (offset = 0; offset < pSearchState->KernelCount; offset++, pKernelID++, pGroupID++, pPatchID++)
2224 {
2225 // Get patch information associated with the kernel
2226 pKernelPatch = (*pPatchID >= 0) ? &(pSearchState->Patches[*pPatchID]) : nullptr;
2227
2228 // Append/Patch kernel from custom cache
2229 if (*pGroupID == GROUP_CUSTOM)
2230 {
2231 res = Kdll_AppendKernel(pCustomCache, pSearchState, *pKernelID, pKernelPatch);
2232 }
2233 // Append/Patch kernel from internal cache
2234 else
2235 {
2236 res = Kdll_AppendKernel(pKernelCache, pSearchState, *pKernelID, pKernelPatch);
2237 }
2238
2239 if (!res)
2240 {
2241 VP_RENDER_ASSERTMESSAGE("Failed to build kernel ID %d.", pSearchState->KernelID[offset]);
2242 return false;
2243 }
2244 else
2245 {
2246 Kdll_CacheEntry *kernels = (*pGroupID == GROUP_CUSTOM) ? &pCustomCache->pCacheEntries[*pKernelID] : &pKernelCache->pCacheEntries[*pKernelID];
2247 VP_RENDER_NORMALMESSAGE("Component kernels [%d]: %s", *pKernelID, kernels->szName);
2248 }
2249 }
2250
2251 // Resolve kernel dependencies
2252 res = Kdll_ResolveKernelDependencies(pState, pSearchState);
2253 if (!res)
2254 {
2255 VP_RENDER_ASSERTMESSAGE("Failed to resolve symbols.");
2256 return false;
2257 }
2258
2259 return true;
2260 }
2261
2262 //---------------------------------------------------------------------------------------
2263 // KernelDll_StartKernelSearch - Starts kernel search
2264 //
2265 // Parameters:
2266 // Kdll_State *pState - [in] Dynamic Linking State
2267 // Kdll_FilterEntry *pFilter - [in] Search filter (array of search entries)
2268 // int iFilterSize - [in] Search filter size
2269 // Kdll_SearchState *pSearchState - [in/out] Kernel search state
2270 //
2271 // Output: none
2272 //---------------------------------------------------------------------------------------
2273 void KernelDll_StartKernelSearch(
2274 Kdll_State * pState,
2275 Kdll_SearchState *pSearchState,
2276 Kdll_FilterEntry *pFilter,
2277 int32_t iFilterSize,
2278 uint32_t uiIs64BInstrEnabled)
2279 {
2280 int32_t nLayer;
2281
2282 VP_RENDER_FUNCTION_ENTER;
2283
2284 // Reset all states
2285 MOS_ZeroMemory(pSearchState, sizeof(Kdll_SearchState));
2286
2287 // Setup KDLL state
2288 pSearchState->pKdllState = pState; // KDLL state
2289
2290 // Cleanup kernel table
2291 pSearchState->KernelCount = 0; // # of kernels
2292
2293 // Cleanup patch data
2294 memset(pSearchState->Patches, 0, sizeof(pSearchState->Patches));
2295 memset(pSearchState->PatchID, -1, sizeof(pSearchState->PatchID));
2296 pSearchState->PatchCount = 0;
2297
2298 // Copy original filter; filter will be modified as part of the search
2299 if (pFilter && iFilterSize > 0)
2300 {
2301 MOS_SecureMemcpy(pSearchState->Filter, iFilterSize * sizeof(Kdll_FilterEntry), pFilter, iFilterSize * sizeof(Kdll_FilterEntry));
2302 pSearchState->pFilter = pSearchState->Filter;
2303 pSearchState->iFilterSize = iFilterSize;
2304
2305 for (nLayer = 0; nLayer < iFilterSize; nLayer++)
2306 {
2307 // DScale Kernels are enabled for all gen9 stepping.
2308 //For Gen9+, kernel don't support sublayer DScale+rotation
2309 //Sampler_unorm does not support Y410/RGB10, we need to use sampler_16 to support Y410/RGB10
2310 if (!pFilter[nLayer].bEnableDscale &&
2311 (!pFilter[nLayer].bWaEnableDscale ||
2312 (pFilter[nLayer].layer == Layer_SubVideo &&
2313 pFilter[nLayer].rotation != MEDIA_ROTATION(VPHAL_ROTATION_IDENTITY))))
2314 {
2315 if (pFilter[nLayer].sampler == Sample_Scaling_034x)
2316 {
2317 pSearchState->pFilter[nLayer].sampler = Sample_Scaling;
2318 }
2319 else if (pFilter[nLayer].sampler == Sample_iScaling_034x)
2320 {
2321 pSearchState->pFilter[nLayer].sampler = Sample_iScaling;
2322 }
2323 else if (pFilter[nLayer].sampler == Sample_iScaling_AVS)
2324 {
2325 pSearchState->pFilter[nLayer].sampler = Sample_iScaling_AVS;
2326 }
2327 }
2328 }
2329
2330 // Copy the render target format
2331 pSearchState->target_format = pSearchState->pFilter[iFilterSize - 1].format;
2332
2333 // Copy the render target tile type
2334 pSearchState->target_tiletype = pSearchState->pFilter[iFilterSize - 1].tiletype;
2335
2336 // Indicate whether to use 64B save kernel for render target surface
2337 if (uiIs64BInstrEnabled &&
2338 ((pSearchState->target_tiletype == MOS_TILE_X) ||
2339 (pSearchState->target_tiletype == MOS_TILE_LINEAR)))
2340 {
2341 pSearchState->b64BSaveEnabled = true;
2342 }
2343 }
2344 }
2345
2346 /*----------------------------------------------------------------------------
2347 | Name : KernelDll_SetupCSC
2348 | Purpose : Defines CSC conversions necessary for a given filter
2349 |
2350 | Input : pState - Kernel Dll state
2351 | pSearchState - current DL search state
2352 |
2353 | Return :
2354 \---------------------------------------------------------------------------*/
2355 bool KernelDll_SetupCSC(
2356 Kdll_State * pState,
2357 Kdll_SearchState *pSearchState)
2358 {
2359 int i, m; // Integer iterators
2360
2361 bool bCoeffID_0_Used = false;
2362
2363 VPHAL_CSPACE cspace = CSpace_None; // Current ColorSpace
2364 VPHAL_CSPACE out_cspace = CSpace_None; // Render Target CS
2365 VPHAL_CSPACE main_cspace = CSpace_None; // Main video CS
2366 VPHAL_CSPACE sel_cspace = CSpace_Any; // Selected CS
2367
2368 Kdll_FilterEntry *pFilter; // Current Filter information
2369 int iFilterSize = pSearchState->iFilterSize;
2370 Kdll_CSC_Params * pCSC = &pSearchState->CscParams;
2371
2372 int csc_count; // Number of CSC operations
2373 int matrix_count; // Number of Matrices in use
2374 int procamp_count = 0; // Number of PA operations
2375 int sel_csc_count = -1; // Minimum number of CSC operations
2376 int iCoeffID; // coeffID for layers other than main video
2377 uint8_t cspace_in_use[CSpace_Count]; // Color Spaces in use
2378
2379 Kdll_CSC_Matrix curr_matrix;
2380 Kdll_CSC_Matrix *matrix = pCSC->Matrix; // Color Space conversion matrix
2381 uint8_t * matrixID = pCSC->MatrixID; // CSC coefficient allocation table
2382 bool forceToTargetColorSpace = false;
2383
2384 // Clear all CSC matrices
2385 MOS_ZeroMemory(matrix, sizeof(pCSC->Matrix));
2386 memset(matrixID, DL_CSC_DISABLED, sizeof(pCSC->MatrixID));
2387 memset(pCSC->PatchMatrixID, DL_CSC_DISABLED, sizeof(pCSC->PatchMatrixID));
2388 pCSC->PatchMatrixNum = 0;
2389
2390 // Clear array of color spaces in use
2391 MOS_ZeroMemory(cspace_in_use, sizeof(cspace_in_use));
2392
2393 //---------------------------------------------------------------//
2394 // Collect information about Color Spaces in use
2395 // Get Primary Video and Render Target Color Spaces
2396 // Force xvYCC passthrough if enabled
2397 //---------------------------------------------------------------//
2398 for (i = iFilterSize, pFilter = pSearchState->Filter; i > 0; i--, pFilter++)
2399 {
2400 if (pFilter->forceToTargetColorSpace)
2401 {
2402 forceToTargetColorSpace = true;
2403 }
2404 // Disable Procamp for all layers except Main Video
2405 // Disable Procamp if source is RGB
2406 if (pFilter->layer != Layer_MainVideo ||
2407 pFilter->cspace == CSpace_sRGB ||
2408 pFilter->cspace == CSpace_stRGB)
2409 {
2410 pFilter->procamp = DL_PROCAMP_DISABLED;
2411 }
2412
2413 // Count number of procamp operations (limited by number of independent coefficients)
2414 // Ignore layers with palletized/constant colors
2415 if (pFilter->procamp != DL_PROCAMP_DISABLED &&
2416 pFilter->cspace != CSpace_Any)
2417 {
2418 procamp_count++;
2419 }
2420
2421 // Set xvYCC passthrough mode
2422 if (pFilter->cspace == CSpace_xvYCC709 ||
2423 pFilter->cspace == CSpace_xvYCC601)
2424 {
2425 sel_cspace = pFilter->cspace;
2426 }
2427
2428 // Get Main Video color space
2429 if (pFilter->layer == Layer_MainVideo)
2430 {
2431 main_cspace = pFilter->cspace;
2432 }
2433
2434 // Get Render Target color space
2435 if (pFilter->layer == Layer_RenderTarget)
2436 {
2437 // Target is sRGB/stRGB
2438 if (!KernelDll_IsYUVFormat(pFilter->format))
2439 {
2440 // Disable xvYCC passthrough (sRGB cannot have extended gamut)
2441 sel_cspace = CSpace_Any;
2442 }
2443 out_cspace = pFilter->cspace;
2444 }
2445
2446 // Mark color spaces in use for search that follows
2447 if (pFilter->cspace > CSpace_Any && pFilter->cspace < CSpace_Count)
2448 {
2449 cspace_in_use[pFilter->cspace] = 1;
2450 }
2451 }
2452
2453 // Check max number of procamp operations
2454 if (procamp_count > DL_PROCAMP_MAX)
2455 {
2456 return false;
2457 }
2458
2459 //---------------------------------------------------------------//
2460 // Search Color Space that provides minimum number of CSC conversions
2461 // If there are multiple solutions, select main video cspace (quality)
2462 //---------------------------------------------------------------//
2463 if (sel_cspace == CSpace_Any)
2464 {
2465 if (forceToTargetColorSpace)
2466 {
2467 sel_cspace = out_cspace;
2468 }
2469 else
2470 {
2471 int cs;
2472 for (cs = (CSpace_Any + 1); cs < CSpace_Count; cs++)
2473 {
2474 // Skip color spaces not in use
2475 cspace = (VPHAL_CSPACE)cs;
2476 if (!cspace_in_use[cspace])
2477 {
2478 continue;
2479 }
2480
2481 // xvYCC and BT are treated as same for CSC considerations (BT.x to xvYCC.x matrix is I)
2482 cspace = KernelDll_TranslateCspace(cspace);
2483
2484 // Count # of CS conversions and matrices
2485 csc_count = 0;
2486 for (i = iFilterSize, pFilter = pSearchState->Filter; i > 0; i--, pFilter++)
2487 {
2488 // Ignore layers where the Color Space may be set in software (colorfill, palletized)
2489 if (pFilter->cspace == CSpace_Any)
2490 {
2491 continue;
2492 }
2493
2494 // Check if CSC/PA is required
2495 if (KernelDll_TranslateCspace(pFilter->cspace) != cspace ||
2496 pFilter->procamp != DL_PROCAMP_DISABLED)
2497 {
2498 csc_count++;
2499 }
2500 }
2501
2502 // Save best choice as requiring minimum number of CSC operations
2503 if ((sel_csc_count < 0) || // Initial value
2504 (csc_count < sel_csc_count) || // Minimum number of CSC operations
2505 (csc_count == sel_csc_count && cs == main_cspace)) // Use main cspace as default if same CSC count
2506 {
2507 sel_cspace = cspace;
2508 sel_csc_count = csc_count;
2509 }
2510 }
2511 }
2512 }
2513
2514 // Due to put the colorfill behind CSC, so Src0 cspace needs to change
2515 // to selspace in order to fill colorfill values correctly.
2516 pState->colorfill_cspace = sel_cspace;
2517
2518 // color space is selected by now... setup CSC matrices
2519 matrix_count = 0;
2520 iCoeffID = 1;
2521 for (i = iFilterSize, pFilter = pSearchState->Filter; i > 0; i--, pFilter++)
2522 {
2523 // Setup CSC for palettized/colorfill layers
2524 if (pFilter->cspace == CSpace_Any)
2525 {
2526 // Set Color Space and format (for software)
2527 if (pFilter->format == Format_Any)
2528 {
2529 pFilter->format = KernelDll_IsCspace(sel_cspace, CSpace_YUV) ? Format_AYUV : Format_A8R8G8B8;
2530 }
2531
2532 pFilter->cspace = sel_cspace;
2533 pFilter->matrix = DL_CSC_DISABLED;
2534 }
2535 else
2536 {
2537 // Setup CSC parameters: SrcSpace is the layer color space,
2538 // DstSpace is the internal color space selected
2539 curr_matrix.SrcSpace = KernelDll_TranslateCspace(pFilter->cspace);
2540 curr_matrix.DstSpace = KernelDll_TranslateCspace(sel_cspace);
2541 curr_matrix.iProcampID = pFilter->procamp;
2542
2543 // Check if CSC is necessary
2544 if (curr_matrix.SrcSpace == curr_matrix.DstSpace &&
2545 curr_matrix.iProcampID == DL_PROCAMP_DISABLED)
2546 {
2547 pFilter->matrix = DL_CSC_DISABLED;
2548 curr_matrix.iCoeffID = CoeffID_None;
2549 continue;
2550 }
2551
2552 // Reserve CoeffID_0 for main video - CoeffID_0 gets CSC coeff from static parameters
2553 // If main video doesn't use CoeffID_0, assign to RT
2554 if ((pFilter->layer == Layer_MainVideo) ||
2555 (pFilter->layer == Layer_RenderTarget))
2556 {
2557 if (bCoeffID_0_Used)
2558 {
2559 curr_matrix.iCoeffID = (Kdll_CoeffID)iCoeffID++;
2560 }
2561 else
2562 {
2563 curr_matrix.iCoeffID = CoeffID_0;
2564 bCoeffID_0_Used = true;
2565 }
2566 }
2567 else
2568 {
2569 curr_matrix.iCoeffID = (Kdll_CoeffID)iCoeffID++;
2570 }
2571
2572 // CSC at the target layer is from internal cspace (SrcSpace)
2573 // to external cspace (DstCspace)
2574 if (pFilter->layer == Layer_RenderTarget)
2575 {
2576 VPHAL_CSPACE aux = curr_matrix.SrcSpace;
2577 curr_matrix.SrcSpace = curr_matrix.DstSpace;
2578 curr_matrix.DstSpace = aux;
2579 }
2580
2581 // Search CSC matrix - avoid duplicated CSC matrices
2582 for (m = 0; m < matrix_count; m++)
2583 {
2584 if (curr_matrix.SrcSpace == matrix[m].SrcSpace &&
2585 curr_matrix.DstSpace == matrix[m].DstSpace &&
2586 curr_matrix.iProcampID == matrix[m].iProcampID)
2587 {
2588 break;
2589 }
2590 }
2591
2592 // Check limit
2593 if (m == matrix_count)
2594 {
2595 // Exceeded number of CSC matrices allowed
2596 if (matrix_count == DL_CSC_MAX)
2597 {
2598 VP_RENDER_ASSERTMESSAGE("CSC matrix count %d exceeded number of CSC matrices allowed!", matrix_count);
2599 return false;
2600 }
2601
2602 matrix[m].bInUse = true;
2603 matrix[m].SrcSpace = curr_matrix.SrcSpace;
2604 matrix[m].DstSpace = curr_matrix.DstSpace;
2605 matrix[m].iProcampID = curr_matrix.iProcampID;
2606 matrix[m].iCoeffID = curr_matrix.iCoeffID;
2607
2608 // Calculate coefficients for the first time
2609 KernelDll_UpdateCscCoefficients(pState, &matrix[m]);
2610
2611 // Next matrix
2612 matrix_count++;
2613 }
2614
2615 // point to the matrix
2616 pFilter->matrix = m;
2617 }
2618 }
2619
2620 // Link matrices to kernel coefficients (and vice-versa)
2621 matrix = pCSC->Matrix;
2622 for (m = 0; m < matrix_count; m++, matrix++)
2623 {
2624 // Coefficient table points to matrix index
2625 matrixID[matrix->iCoeffID] = (uint8_t)m;
2626 }
2627
2628 // Save selected color space
2629 pCSC->ColorSpace = sel_cspace;
2630
2631 return true;
2632 }
2633
2634 /*----------------------------------------------------------------------------
2635 | Name : KernelDll_GetPatchData
2636 | Purpose : Get binary data block to be used for kernel patching
2637 |
2638 | Input : pState - [in] Current DL state
2639 | pSearchState - [in] Current DL search state
2640 | iPatchKind - [in] Patch kind
2641 | pSize - [out] Data block Size
2642 |
2643 | Return : nullptr - Unsupported patch data kind
2644 | <>nullptr - Pointer to data block
2645 \---------------------------------------------------------------------------*/
2646 static uint8_t *KernelDll_GetPatchData(
2647 Kdll_State * pState,
2648 Kdll_SearchState *pSearchState,
2649 int32_t iPatchKind,
2650 int32_t * pSize)
2651 {
2652 MOS_UNUSED(pState);
2653
2654 VP_RENDER_FUNCTION_ENTER;
2655
2656 if (iPatchKind == PatchKind_CSC_Coeff_Src0 ||
2657 iPatchKind == PatchKind_CSC_Coeff_Src1)
2658 {
2659 Kdll_CoeffID coeffID = CoeffID_None;
2660 uint8_t matrixID = DL_CSC_DISABLED;
2661
2662 // Get matrix id
2663 if (iPatchKind == PatchKind_CSC_Coeff_Src0)
2664 {
2665 coeffID = pSearchState->src0_coeff;
2666 }
2667 else
2668 {
2669 coeffID = pSearchState->src1_coeff;
2670 }
2671
2672 // Get matrix associated with the coefficient ID
2673 if (coeffID > CoeffID_None)
2674 {
2675 matrixID = pSearchState->CscParams.MatrixID[coeffID];
2676 }
2677
2678 // Found matrix
2679 if (matrixID < DL_CSC_MAX)
2680 {
2681 Kdll_CSC_Matrix *pMatrix = &(pSearchState->CscParams.Matrix[matrixID]);
2682
2683 *pSize = 12 * sizeof(uint16_t);
2684
2685 if (pState->bEnableCMFC)
2686 {
2687 if (pSearchState->CscParams.PatchMatrixNum < DL_CSC_MAX)
2688 {
2689 pSearchState->CscParams.PatchMatrixID[pSearchState->CscParams.PatchMatrixNum] = matrixID;
2690 pSearchState->CscParams.PatchMatrixNum++;
2691 }
2692 else
2693 {
2694 VP_RENDER_ASSERTMESSAGE("Patch CSC coefficient number %d exceed limitation %d!", pSearchState->CscParams.PatchMatrixNum, DL_CSC_MAX);
2695 return nullptr;
2696 }
2697 }
2698
2699 return ((uint8_t *)pMatrix->Coeff);
2700 }
2701 }
2702 else
2703 {
2704 VP_RENDER_ASSERTMESSAGE("Invalid patch kind %d.", iPatchKind);
2705 }
2706
2707 return nullptr;
2708 }
2709
2710 /*----------------------------------------------------------------------------
2711 | Name : KernelDll_UpdateState
2712 | Purpose : Update search state using current matching rule
2713 |
2714 | Input : pState - Kernel Dll state
2715 | pSearchState - current DL search state
2716 |
2717 | Return :
2718 \---------------------------------------------------------------------------*/
2719 bool KernelDll_UpdateState(
2720 Kdll_State * pState,
2721 Kdll_SearchState *pSearchState)
2722 {
2723 Kdll_RuleEntrySet * pRuleSet = pSearchState->pMatchingRuleSet;
2724 const Kdll_RuleEntry *pRuleEntry;
2725 int32_t iSetCount;
2726
2727 VP_RENDER_FUNCTION_ENTER;
2728
2729 // Ensures that we have a matching rule
2730 if (pRuleSet == nullptr)
2731 {
2732 return false;
2733 }
2734
2735 // Get rule entry and number of state update ("Set") rules; validate
2736 pRuleEntry = pRuleSet->pRuleEntry;
2737 iSetCount = pRuleSet->iSetCount;
2738 if (pRuleEntry == nullptr || iSetCount < 1)
2739 {
2740 VP_RENDER_NORMALMESSAGE("Invalid rule set.");
2741 return false;
2742 }
2743
2744 // Jump to set rules (skip match rules)
2745 pRuleEntry += pRuleSet->iMatchCount;
2746
2747 // Apply state update rules
2748 for (; iSetCount > 0; iSetCount--, pRuleEntry++)
2749 {
2750 switch (pRuleEntry->id)
2751 {
2752 // Add kernel to the Dynamic Linking array
2753 case RID_SetKernel:
2754 if (pSearchState->KernelCount < DL_MAX_KERNELS)
2755 {
2756 int32_t i = pSearchState->KernelCount++;
2757 pSearchState->KernelID[i] = pRuleEntry->value;
2758 pSearchState->KernelGrp[i] = pRuleSet->iGroup; // Group associated with the kernel ID
2759 }
2760 else
2761 {
2762 VP_RENDER_ASSERTMESSAGE("reached maximum number of component kernels.");
2763 MT_ERR1(MT_VP_KERNEL_RULE, MT_CODE_LINE, __LINE__);
2764 return false;
2765 }
2766 break;
2767
2768 // Set Parser State
2769 case RID_SetParserState:
2770 pSearchState->state = (Kdll_ParserState)pRuleEntry->value;
2771 break;
2772
2773 // Move to Next/Prev Layer
2774 case RID_SetNextLayer:
2775 if (pRuleEntry->value == -1)
2776 {
2777 pSearchState->layer_number--;
2778 pSearchState->pFilter--;
2779 }
2780 else if (pRuleEntry->value == -2) // jump to layer main video
2781 {
2782 do
2783 {
2784 pSearchState->layer_number--;
2785 pSearchState->pFilter--;
2786 if (pSearchState->pFilter == nullptr || pSearchState->layer_number < 0)
2787 {
2788 return false;
2789 }
2790 } while (pSearchState->pFilter->layer != Layer_MainVideo);
2791 }
2792 else if (pRuleEntry->value == 2) // jump to target layer
2793 {
2794 while (pSearchState->pFilter->layer < Layer_RenderTarget)
2795 {
2796 pSearchState->layer_number++;
2797 pSearchState->pFilter++;
2798 }
2799 }
2800 else
2801 {
2802 pSearchState->layer_number++;
2803 pSearchState->pFilter++;
2804 }
2805 break;
2806
2807 // Set patch data
2808 case RID_SetPatchData: {
2809 uint8_t * pData = nullptr;
2810 int32_t iSize = 0;
2811 int32_t iKernelIndex = pSearchState->KernelCount - 1;
2812 int32_t iPatchIndex;
2813 Kdll_PatchData *pPatch;
2814
2815 // Get block of data for patching
2816 pData = KernelDll_GetPatchData(pState, pSearchState, (Kdll_PatchKind)pRuleEntry->value, &iSize);
2817 if (pData == nullptr || iSize == 0)
2818 {
2819 VP_RENDER_ASSERTMESSAGE("invalid patch.");
2820 MT_ERR1(MT_VP_KERNEL_RULE, MT_CODE_LINE, __LINE__);
2821 return false;
2822 }
2823
2824 // Append to the existing patch data block
2825 iPatchIndex = pSearchState->PatchID[iKernelIndex];
2826
2827 // Allocate new patch structure
2828 if (iPatchIndex < 0)
2829 {
2830 // Fail to allocate
2831 if (pSearchState->PatchCount >= DL_MAX_PATCHES)
2832 {
2833 VP_RENDER_ASSERTMESSAGE("reached maximum number of patches.");
2834 MT_ERR1(MT_VP_KERNEL_RULE, MT_CODE_LINE, __LINE__);
2835 return false;
2836 }
2837
2838 // Get new patch block
2839 iPatchIndex = pSearchState->PatchCount++;
2840 pSearchState->PatchID[iKernelIndex] = iPatchIndex;
2841
2842 // Reset new patch entry
2843 pPatch = &(pSearchState->Patches[iPatchIndex]);
2844 MOS_ZeroMemory(pPatch, sizeof(Kdll_PatchData));
2845 }
2846 else
2847 {
2848 // Get Patch entry already in use
2849 pPatch = &(pSearchState->Patches[iPatchIndex]);
2850 }
2851
2852 // Check if data can be appended
2853 if (pPatch->iPatchDataSize + iSize > DL_MAX_PATCH_DATA_SIZE)
2854 {
2855 VP_RENDER_ASSERTMESSAGE("exceeded maximum patch size.");
2856 MT_ERR1(MT_VP_KERNEL_RULE, MT_CODE_LINE, __LINE__);
2857 return false;
2858 }
2859
2860 // Append patch data
2861 MOS_SecureMemcpy(pPatch->Data + pPatch->iPatchDataSize, iSize, (void *)pData, iSize);
2862 pPatch->iPatchDataSize += iSize;
2863 }
2864 break;
2865
2866 // Set patch operation
2867 case RID_SetPatch: {
2868 int32_t iKernelIndex = pSearchState->KernelCount - 1;
2869 int32_t iPatchIndex = pSearchState->PatchID[iKernelIndex];
2870 Kdll_PatchData * pPatch;
2871 uint8_t * pPatchRule;
2872 Kdll_PatchBlock *pPatchBlock;
2873 int32_t nPatches;
2874
2875 // No patch associated with the current kernel
2876 if (iPatchIndex < 0)
2877 {
2878 return false;
2879 }
2880
2881 // Get Patch entry
2882 pPatch = &(pSearchState->Patches[iPatchIndex]);
2883
2884 // Get number of patches and pointer to first rule extension (patch rule)
2885 nPatches = pRuleEntry->value;
2886 pPatchRule = (uint8_t *)(pRuleEntry + 1);
2887
2888 // Check if rules can be applied
2889 if (nPatches + pPatch->nPatches > DL_MAX_PATCH_BLOCKS)
2890 {
2891 VP_RENDER_ASSERTMESSAGE("exceeded number of patch blocks.");
2892 MT_ERR1(MT_VP_KERNEL_RULE, MT_CODE_LINE, __LINE__);
2893 return false;
2894 }
2895
2896 // Set Patches
2897 pPatchBlock = &(pPatch->Patch[pPatch->nPatches]);
2898 for (; nPatches > 0; nPatches--, pPatchBlock++, pPatch->nPatches++)
2899 {
2900 pPatchBlock->BlockSize = ((Kdll_PatchRuleEntry *)pPatchRule)->Size;
2901 pPatchBlock->SrcOffset = ((Kdll_PatchRuleEntry *)pPatchRule)->Source;
2902 pPatchBlock->DstOffset = ((Kdll_PatchRuleEntry *)pPatchRule)->Dest;
2903 pPatchRule += sizeof(Kdll_RuleEntry);
2904 }
2905
2906 // Skip rule extensions
2907 iSetCount -= pRuleEntry->value;
2908 pRuleEntry += pRuleEntry->value;
2909 }
2910 break;
2911
2912 // Set destination colorspace
2913 case RID_SetTargetCspace:
2914 if ((VPHAL_CSPACE)pRuleEntry->value == CSpace_Source)
2915 {
2916 pSearchState->cspace = pSearchState->pFilter->cspace;
2917 }
2918 else
2919 {
2920 pSearchState->cspace = (VPHAL_CSPACE)pRuleEntry->value;
2921 }
2922 break;
2923
2924 // Set Src0 source format
2925 case RID_SetSrc0Format:
2926 if ((MOS_FORMAT)pRuleEntry->value == Format_Source)
2927 {
2928 pSearchState->src0_format = pSearchState->pFilter->format;
2929 }
2930 else
2931 {
2932 pSearchState->src0_format = (MOS_FORMAT)pRuleEntry->value;
2933 }
2934 break;
2935
2936 // Set Src0 sampling mode
2937 case RID_SetSrc0Sampling:
2938 if ((Kdll_Sampling)pRuleEntry->value == Sample_Source)
2939 {
2940 pSearchState->src0_sampling = pSearchState->pFilter->sampler;
2941 }
2942 else
2943 {
2944 pSearchState->src0_sampling = (Kdll_Sampling)pRuleEntry->value;
2945 }
2946 break;
2947
2948 // Set Src0 Rotation
2949 case RID_SetSrc0Rotation:
2950 pSearchState->src0_rotation = pSearchState->pFilter->rotation;
2951 break;
2952
2953 // Set Src0 Colorfill
2954 case RID_SetSrc0ColorFill:
2955 if ((int32_t)pRuleEntry->value == ColorFill_Source)
2956 {
2957 pSearchState->src0_colorfill = pSearchState->pFilter->colorfill;
2958 }
2959 else
2960 {
2961 pSearchState->src0_colorfill = (int32_t)pRuleEntry->value;
2962 }
2963 break;
2964
2965 // Set Src0 luma key
2966 case RID_SetSrc0LumaKey:
2967 if (pRuleEntry->value == LumaKey_Source)
2968 {
2969 pSearchState->src0_lumakey = pSearchState->pFilter->lumakey;
2970 }
2971 else
2972 {
2973 pSearchState->src0_lumakey = (int32_t)pRuleEntry->value;
2974 }
2975 break;
2976
2977 // Set Src0 Procamp
2978 case RID_SetSrc0Procamp:
2979 if (pRuleEntry->value == Procamp_Source)
2980 {
2981 pSearchState->src0_procamp = pSearchState->pFilter->procamp;
2982 }
2983 else
2984 {
2985 pSearchState->src0_procamp = (int32_t)pRuleEntry->value;
2986 }
2987 break;
2988
2989 // Set Src0 CSC coefficients
2990 case RID_SetSrc0Coeff:
2991 if ((Kdll_CoeffID)pRuleEntry->value == CoeffID_Source)
2992 {
2993 if (pSearchState->pFilter->matrix == DL_CSC_DISABLED)
2994 {
2995 pSearchState->src0_coeff = CoeffID_None;
2996 }
2997 else
2998 {
2999 Kdll_CSC_Matrix *matrix = pSearchState->CscParams.Matrix;
3000 matrix += pSearchState->pFilter->matrix;
3001
3002 pSearchState->src0_coeff = matrix->iCoeffID;
3003 }
3004 }
3005 else
3006 {
3007 pSearchState->src0_coeff = (Kdll_CoeffID)pRuleEntry->value;
3008 }
3009 break;
3010
3011 case RID_SetSrc0Processing:
3012 if ((Kdll_Processing)pRuleEntry->value == Process_Source)
3013 {
3014 pSearchState->src0_process = pSearchState->pFilter->process;
3015 }
3016 else
3017 {
3018 pSearchState->src0_process = (Kdll_Processing)pRuleEntry->value;
3019 }
3020 break;
3021
3022 // Set Src1 source format
3023 case RID_SetSrc1Format:
3024 if ((MOS_FORMAT)pRuleEntry->value == Format_Source)
3025 {
3026 pSearchState->src1_format = pSearchState->pFilter->format;
3027 }
3028 else
3029 {
3030 pSearchState->src1_format = (MOS_FORMAT)pRuleEntry->value;
3031 }
3032 break;
3033
3034 // Set Src1 sampling mode
3035 case RID_SetSrc1Sampling:
3036 if ((Kdll_Sampling)pRuleEntry->value == Sample_Source)
3037 {
3038 pSearchState->src1_sampling = pSearchState->pFilter->sampler;
3039 }
3040 else
3041 {
3042 pSearchState->src1_sampling = (Kdll_Sampling)pRuleEntry->value;
3043 }
3044 break;
3045
3046 // Set Src1 Rotation
3047 case RID_SetSrc1Rotation:
3048 pSearchState->src1_rotation = pSearchState->pFilter->rotation;
3049 break;
3050
3051 // Set Src1 luma key
3052 case RID_SetSrc1LumaKey:
3053 if (pRuleEntry->value == LumaKey_Source)
3054 {
3055 pSearchState->src1_lumakey = pSearchState->pFilter->lumakey;
3056 }
3057 else
3058 {
3059 pSearchState->src1_lumakey = (int32_t)pRuleEntry->value;
3060 }
3061 break;
3062
3063 // Set Src1 Sampler LumaKey
3064 case RID_SetSrc1SamplerLumaKey:
3065 if (pRuleEntry->value == LumaKey_Source)
3066 {
3067 pSearchState->src1_samplerlumakey = pSearchState->pFilter->samplerlumakey;
3068 }
3069 else
3070 {
3071 pSearchState->src1_samplerlumakey = (int32_t)pRuleEntry->value;
3072 }
3073 break;
3074
3075 // Set Src1 Procamp
3076 case RID_SetSrc1Procamp:
3077 if (pRuleEntry->value == Procamp_Source)
3078 {
3079 pSearchState->src1_procamp = pSearchState->pFilter->procamp;
3080 }
3081 else
3082 {
3083 pSearchState->src1_procamp = (int32_t)pRuleEntry->value;
3084 }
3085 break;
3086
3087 // Set Src1 CSC coefficients
3088 case RID_SetSrc1Coeff:
3089 if ((Kdll_CoeffID)pRuleEntry->value == CoeffID_Source)
3090 {
3091 if (pSearchState->pFilter->matrix == DL_CSC_DISABLED)
3092 {
3093 pSearchState->src1_coeff = CoeffID_None;
3094 }
3095 else
3096 {
3097 Kdll_CSC_Matrix *matrix = pSearchState->CscParams.Matrix;
3098 matrix += pSearchState->pFilter->matrix;
3099
3100 pSearchState->src1_coeff = matrix->iCoeffID;
3101 }
3102 }
3103 else
3104 {
3105 pSearchState->src1_coeff = (Kdll_CoeffID)pRuleEntry->value;
3106 }
3107 break;
3108
3109 // Set Src1 processing mode
3110 case RID_SetSrc1Processing:
3111 if ((Kdll_Processing)pRuleEntry->value == Process_Source)
3112 {
3113 pSearchState->src1_process = pSearchState->pFilter->process;
3114 }
3115 else
3116 {
3117 pSearchState->src1_process = (Kdll_Processing)pRuleEntry->value;
3118 }
3119 break;
3120
3121 // Set current quadrant
3122 case RID_SetQuadrant:
3123 pSearchState->quadrant = (int32_t)pRuleEntry->value;
3124 break;
3125
3126 // Set CSC flag before Mix
3127 case RID_SetCSCBeforeMix:
3128 pSearchState->bCscBeforeMix = pRuleEntry->value ? true : false;
3129 break;
3130
3131 // Unsupported "Set" rule
3132 default:
3133 // Failed to find a matching rule -> kernel search will fail
3134 VP_RENDER_ASSERTMESSAGE("Invalid rule %d @ layer %d, state %d.", pRuleEntry->id, pSearchState->layer_number, pSearchState->state);
3135 MT_ERR1(MT_VP_KERNEL_RULE, MT_CODE_LINE, __LINE__);
3136 return false;
3137 }
3138 }
3139
3140 // Reset matching rule
3141 pSearchState->pMatchingRuleSet = nullptr;
3142 return true;
3143 }
3144
3145 //-----------------------------------------------------------------------------------------
3146 // KernelDll_SortRuleTable - Sort master dynamic linking rule table
3147 //
3148 // Parameters:
3149 // char *pState - [in] Kernel Dll state
3150 //
3151 // Output: true - Master rule table (and acceleration table) successfully created
3152 // false - Failed to setup master rule table
3153 //-----------------------------------------------------------------------------------------
3154 bool KernelDll_SortRuleTable(Kdll_State *pState)
3155 {
3156 uint8_t group;
3157 int32_t state;
3158 const Kdll_RuleEntry *pRule = nullptr;
3159 Kdll_RuleEntrySet * pRuleSet;
3160 int32_t i, j;
3161
3162 int32_t iTotal = 0;
3163 int32_t iNoOverr[Parser_Count]; // Non-overridable (enforced) rules
3164 int32_t iDefault[Parser_Count]; // Default rules
3165 int32_t iCustom[Parser_Count]; // Custom rules
3166
3167 VP_RENDER_FUNCTION_ENTER;
3168
3169 // Release previous table (rule table update)
3170 if (pState->pSortedRules)
3171 {
3172 MOS_FreeMemory(pState->pSortedRules);
3173 pState->pSortedRules = nullptr;
3174
3175 MOS_ZeroMemory(pState->pDllRuleTable, sizeof(pState->pDllRuleTable));
3176 MOS_ZeroMemory(pState->iDllRuleCount, sizeof(pState->iDllRuleCount));
3177 }
3178
3179 // Zero counters
3180 MOS_ZeroMemory(iNoOverr, sizeof(iNoOverr));
3181 MOS_ZeroMemory(iDefault, sizeof(iDefault));
3182 MOS_ZeroMemory(iCustom, sizeof(iCustom));
3183
3184 // Count number of entries for each state
3185 for (i = 0; i < 2; i++)
3186 {
3187 if (i == 0)
3188 {
3189 pRule = pState->pRuleTableDefault;
3190 }
3191 else if (i == 1)
3192 {
3193 pRule = pState->pRuleTableCustom;
3194 }
3195
3196 // Table not set - continue
3197 if (!pRule)
3198 continue;
3199
3200 for (; pRule->id != RID_Op_EOF; pRule++)
3201 {
3202 // Skip extended rules (variable lenght)
3203 if (RID_IS_EXTENDED(pRule->id))
3204 { // value contains number of entries
3205 pRule += pRule->value;
3206 }
3207 else if (pRule->id == RID_Op_NewEntry)
3208 {
3209 // Save Rule Group
3210 if (i == 0)
3211 {
3212 group = pRule->value;
3213 }
3214 else
3215 {
3216 group = RULE_CUSTOM;
3217 }
3218
3219 // Second rule must always be RID_IsParserState
3220 pRule++;
3221 if (pRule->id != RID_IsParserState)
3222 {
3223 VP_RENDER_ASSERTMESSAGE("Rule does not start with State.");
3224 MT_ERR1(MT_VP_KERNEL_RULE, MT_CODE_LINE, __LINE__);
3225 return false;
3226 }
3227
3228 // Get Parser State -> validate value
3229 state = pRule->value;
3230 if (state < Parser_Begin)
3231 {
3232 VP_RENDER_ASSERTMESSAGE("Invalid State %d.", state);
3233 MT_ERR1(MT_VP_KERNEL_RULE, MT_CODE_LINE, __LINE__);
3234 return false;
3235 }
3236 else if (state >= Parser_Custom)
3237 { // Custom states are set together in the same entry
3238 state = Parser_Custom;
3239 }
3240
3241 if (group == RULE_NO_OVERRIDE)
3242 {
3243 iNoOverr[state]++;
3244 }
3245 else if (group == RULE_DEFAULT)
3246 {
3247 iDefault[state]++;
3248 }
3249 else
3250 {
3251 iCustom[state]++;
3252 }
3253
3254 iTotal++;
3255 }
3256 }
3257 }
3258
3259 // Allocate rules
3260 pState->pSortedRules = (Kdll_RuleEntrySet *)MOS_AllocAndZeroMemory(iTotal * sizeof(Kdll_RuleEntrySet));
3261 if (!pState->pSortedRules)
3262 {
3263 VP_RENDER_ASSERTMESSAGE("Failed to allocate rule table.");
3264 MT_ERR1(MT_VP_KERNEL_RULE, MT_CODE_LINE, __LINE__);
3265 return false;
3266 }
3267
3268 // Setup pointers to sorted rules
3269 pState->pDllRuleTable[0] = pState->pSortedRules;
3270 for (j = 0, i = 0; i < Parser_Count; i++)
3271 {
3272 // Setup start pointer and number of entries to search for each state
3273 pState->pDllRuleTable[i] = pState->pDllRuleTable[j] + pState->iDllRuleCount[j];
3274 pState->iDllRuleCount[i] = iNoOverr[i] + iCustom[i] + iDefault[i];
3275
3276 // Setup offsets to rules for sorting
3277 iDefault[i] = iNoOverr[i] + iCustom[i]; // Last set of rules
3278 iCustom[i] = iNoOverr[i]; // 2nd set of rules
3279 iNoOverr[i] = 0; // 1st set of rules
3280
3281 j = i;
3282 }
3283
3284 // Sort rules for fast access
3285 // Integrate enforced, custom, default rules into one single access table
3286 for (i = 0; i < 2; i++)
3287 {
3288 if (i == 0)
3289 {
3290 pRule = pState->pRuleTableDefault;
3291 }
3292 else if (i == 1)
3293 {
3294 pRule = pState->pRuleTableCustom;
3295 }
3296
3297 // Table not set - continue
3298 if (!pRule)
3299 continue;
3300
3301 while (pRule->id != RID_Op_EOF)
3302 {
3303 if (pRule->id != RID_Op_NewEntry)
3304 {
3305 VP_RENDER_ASSERTMESSAGE("New rule entry expected.");
3306 MT_ERR1(MT_VP_KERNEL_RULE, MT_CODE_LINE, __LINE__);
3307 return false;
3308 }
3309
3310 // Save Rule Group
3311 if (i == 0)
3312 {
3313 group = pRule->value;
3314 }
3315 else
3316 {
3317 group = RULE_CUSTOM;
3318 }
3319
3320 // Get Parser State -> validate value
3321 pRule++;
3322 state = pRule->value;
3323 if (state >= Parser_Custom)
3324 { // Custom states are set together in the same entry
3325 state = Parser_Custom;
3326 }
3327 else
3328 { // Skip state check - already handled by acceleration table
3329 pRule++;
3330 }
3331
3332 // Point to sorted rule set entry
3333 if (group == RULE_NO_OVERRIDE)
3334 {
3335 j = iNoOverr[state]++;
3336 }
3337 else if (group == RULE_DEFAULT)
3338 {
3339 j = iDefault[state]++;
3340 }
3341 else
3342 {
3343 j = iCustom[state]++;
3344 }
3345
3346 // Point to sorted ruleset for the current parser state
3347 pRuleSet = pState->pDllRuleTable[state] + j;
3348
3349 // Fill RuleSet
3350 pRuleSet->pRuleEntry = pRule;
3351 pRuleSet->iGroup = group;
3352
3353 // Count number of match rules, including extended rules
3354 while (RID_IS_MATCH(pRule->id))
3355 {
3356 if (RID_IS_EXTENDED(pRule->id))
3357 {
3358 pRuleSet->iMatchCount += pRule->value;
3359 pRule += pRule->value;
3360 }
3361
3362 pRuleSet->iMatchCount++;
3363 pRule++;
3364 }
3365
3366 // Count number of set rules, including extended rules
3367 while (RID_IS_SET(pRule->id))
3368 {
3369 if (RID_IS_EXTENDED(pRule->id))
3370 {
3371 pRuleSet->iSetCount += pRule->value;
3372 pRule += pRule->value;
3373 }
3374
3375 pRuleSet->iSetCount++;
3376 pRule++;
3377 }
3378
3379 // Rule must have at least one "Set" rule
3380 if (pRuleSet->iSetCount < 1)
3381 {
3382 VP_RENDER_ASSERTMESSAGE("Ruleset must have at least one set rule.");
3383 MT_ERR1(MT_VP_KERNEL_RULE, MT_CODE_LINE, __LINE__);
3384 return false;
3385 }
3386 }
3387 }
3388
3389 // Rule table is now sorted and integrated with custom rules
3390 return true;
3391 }
3392
3393 //---------------------------------------------------------------------------------------
3394 // KernelDll_AllocateStates - Allocate Kernel Dynamic Linking/Loading (Dll) States
3395 //
3396 // - Allocate DL states
3397 // - Setup export/import list for linking
3398 // - Prepare pool of search nodes
3399 // - Load component kernels from binary file
3400 // - Setup kernel cache
3401 // - Setup kernel dynamic linking rules
3402 //
3403 // Parameters: [in] pKernelBin - Pointer to Kernel binary file loaded in sys memory
3404 // [in] uKernelSize - Kernel file size
3405 // [in] pFcPatchBin - Pointer to FC patch binary file loaded in sys memory
3406 // [in] uFcPatchCacheSize - FC patch binary file size
3407 // [in] platform - Gfx platform
3408 // [in] pDefaultRules - Dynamic Linking Rules Table
3409 //
3410 // Output: Pointer to allocated Kernel dll state
3411 // nullptr - Failed to allocate Kernel dll state
3412 //-----------------------------------------------------------------------------------------
3413 Kdll_State *KernelDll_AllocateStates(
3414 void * pKernelBin,
3415 uint32_t uKernelSize,
3416 void * pFcPatchCache,
3417 uint32_t uFcPatchCacheSize,
3418 const Kdll_RuleEntry *pDefaultRules,
3419 void (*ModifyFunctionPointers)(PKdll_State))
3420 {
3421 Kdll_State * pState;
3422 Kdll_CacheEntry * pCacheEntry;
3423 Kdll_KernelCache * pKernelCache;
3424 Kdll_KernelHashTable *pHashTable;
3425 Kdll_KernelHashEntry *pHashEntries;
3426
3427 int32_t iSize;
3428 int32_t nExports = 0;
3429 int32_t nImports = 0;
3430 uint32_t * pLinkOffset = nullptr;
3431 Kdll_LinkData * pLinkSort = nullptr;
3432 Kdll_LinkData * pLinkData;
3433 Kdll_LinkData * pExports;
3434 Kdll_LinkFileHeader *pLinkHeader;
3435
3436 int32_t i, j;
3437 uint32_t *pOffsets;
3438 uint8_t * pBase;
3439
3440 VP_RENDER_FUNCTION_ENTER;
3441
3442 // Allocate dynamic linking states
3443 i = sizeof(Kdll_State); // Dynamic linking states
3444 i += sizeof(Kdll_CacheEntry) * IDR_VP_TOTAL_NUM_KERNELS; // Component kernel cache entries
3445 i += sizeof(Kdll_CacheEntry) * IDR_VP_TOTAL_NUM_KERNELS; // CMFC kernel patch cache entries
3446 i += sizeof(Kdll_CacheEntry) * DL_DEFAULT_COMBINED_KERNELS; // Combined kernel cache entries
3447 i += DL_COMBINED_KERNEL_CACHE_SIZE; // Combined kernel buffer
3448 i += sizeof(Kdll_LinkData) * DL_MAX_EXPORT_COUNT; // Kernel Export table
3449
3450 pState = (Kdll_State *)MOS_AllocAndZeroMemory(i);
3451 if (!pState)
3452 {
3453 VP_RENDER_ASSERTMESSAGE("Failed to allocate kernel dll states.");
3454 MT_ERR1(MT_VP_KERNEL_RULE, MT_CODE_LINE, __LINE__);
3455 goto cleanup;
3456 }
3457 pState->iSize = i;
3458 pState->dwRefresh = 0;
3459 pState->pProcamp = nullptr;
3460 pState->iProcampSize = 0;
3461 pState->pSortedRules = nullptr;
3462
3463 if ((pFcPatchCache != nullptr) && (uFcPatchCacheSize != 0))
3464 {
3465 pState->bEnableCMFC = true;
3466 }
3467
3468 // Initialize platform specific function pointers
3469 if (!KernelDll_SetupFunctionPointers(pState, ModifyFunctionPointers))
3470 {
3471 VP_RENDER_ASSERTMESSAGE("Failed to setup function pointers.");
3472 MT_ERR1(MT_VP_KERNEL_RULE, MT_CODE_LINE, __LINE__);
3473 goto cleanup;
3474 }
3475
3476 pKernelCache = &pState->ComponentKernelCache;
3477
3478 // No custom kernels/rules
3479 pState->pRuleTableCustom = nullptr;
3480 pState->pCustomKernelCache = nullptr;
3481
3482 // Set Kernel DLL Rules
3483 pState->pRuleTableDefault = pDefaultRules;
3484
3485 // Integrate and sort rule tables
3486 KernelDll_SortRuleTable(pState);
3487
3488 // Setup component kernel cache
3489 pKernelCache->pCache = (uint8_t *)pKernelBin;
3490 pKernelCache->iCacheSize = (int32_t)uKernelSize;
3491 pKernelCache->iCacheFree = 0;
3492 pKernelCache->iCacheMaxEntries = IDR_VP_TOTAL_NUM_KERNELS;
3493 pKernelCache->iCacheEntries = IDR_VP_TOTAL_NUM_KERNELS;
3494 pKernelCache->pCacheEntries = (Kdll_CacheEntry *)(pState + 1);
3495
3496 pOffsets = (uint32_t *)pKernelCache->pCache;
3497 pBase = (uint8_t *)(pOffsets + IDR_VP_TOTAL_NUM_KERNELS + 1);
3498 pCacheEntry = pKernelCache->pCacheEntries;
3499 for (i = 0; i < IDR_VP_TOTAL_NUM_KERNELS; i++, pCacheEntry++)
3500 {
3501 pCacheEntry->iKUID = i;
3502 pCacheEntry->iKCID = -1;
3503 pCacheEntry->dwLoaded = 0;
3504 pCacheEntry->dwRefresh = 0;
3505 pCacheEntry->wHashEntry = 0;
3506 pCacheEntry->szName = g_cInit_ComponentNames[i];
3507 pCacheEntry->iSize = pOffsets[i + 1] - pOffsets[i];
3508 pCacheEntry->pBinary = (pCacheEntry->iSize > 0) ? (pBase + pOffsets[i]) : nullptr;
3509 }
3510
3511 // Setup CMFC kernel patch cache
3512 pKernelCache = &pState->CmFcPatchCache;
3513
3514 if (pState->bEnableCMFC && pFcPatchCache)
3515 {
3516 pKernelCache->pCache = (uint8_t *)pFcPatchCache;
3517 pKernelCache->iCacheSize = (int32_t)uFcPatchCacheSize;
3518 pKernelCache->iCacheFree = 0;
3519 pKernelCache->iCacheMaxEntries = IDR_VP_TOTAL_NUM_KERNELS;
3520 pKernelCache->iCacheEntries = IDR_VP_TOTAL_NUM_KERNELS;
3521 pKernelCache->pCacheEntries = pCacheEntry;
3522
3523 pOffsets = (uint32_t *)pKernelCache->pCache;
3524 pBase = (uint8_t *)(pOffsets + IDR_VP_TOTAL_NUM_KERNELS + 1);
3525 for (i = 0; i < IDR_VP_TOTAL_NUM_KERNELS; i++, pCacheEntry++)
3526 {
3527 pCacheEntry->iKUID = i;
3528 pCacheEntry->iKCID = -1;
3529 pCacheEntry->dwLoaded = 0;
3530 pCacheEntry->dwRefresh = 0;
3531 pCacheEntry->wHashEntry = 0;
3532 pCacheEntry->szName = g_cInit_ComponentNames[i];
3533 pCacheEntry->iSize = pOffsets[i + 1] - pOffsets[i];
3534 pCacheEntry->pBinary = (pCacheEntry->iSize > 0) ? (pBase + pOffsets[i]) : nullptr;
3535 }
3536 }
3537 else
3538 {
3539 pCacheEntry += IDR_VP_TOTAL_NUM_KERNELS;
3540 }
3541
3542 // Setup combined kernel cache
3543 pKernelCache = &pState->KernelCache;
3544 pKernelCache->iCacheMaxEntries = DL_DEFAULT_COMBINED_KERNELS;
3545 pKernelCache->iCacheEntries = 0;
3546 pKernelCache->iCacheSize = DL_COMBINED_KERNEL_CACHE_SIZE; // Size of kernel cache
3547 pKernelCache->iCacheFree = DL_COMBINED_KERNEL_CACHE_SIZE; // Free cache size
3548 pKernelCache->iCacheID = 0x00010000; // Cache ID
3549 pKernelCache->pCacheEntries = pCacheEntry; // Cached kernel entries
3550 pKernelCache->pCache = (uint8_t *)(pCacheEntry + DL_DEFAULT_COMBINED_KERNELS); // kernels
3551
3552 // reset cache entries
3553 for (i = 0; i < DL_DEFAULT_COMBINED_KERNELS; i++, pCacheEntry++)
3554 {
3555 pCacheEntry->iKUID = -1;
3556 pCacheEntry->iKCID = -1;
3557 pCacheEntry->pBinary = pKernelCache->pCache + i * DL_CACHE_BLOCK_SIZE;
3558 if (i != DL_DEFAULT_COMBINED_KERNELS - 1)
3559 {
3560 pCacheEntry->pNextEntry = pCacheEntry + 1;
3561 }
3562 else
3563 {
3564 pCacheEntry->pNextEntry = nullptr;
3565 }
3566 }
3567
3568 //------------------------------------
3569 // Setup hash table
3570 //------------------------------------
3571 pHashTable = &pState->KernelHashTable;
3572 pHashEntries = pState->KernelHashTable.HashEntry - 1;
3573
3574 pHashTable->pool = 1; // first in pool (1 based index)
3575 pHashTable->last = DL_MAX_COMBINED_KERNELS; // last in pool (for releasing)
3576
3577 for (i = 1; i <= DL_MAX_COMBINED_KERNELS; i++)
3578 {
3579 pHashEntries[i].next = i + 1;
3580 }
3581 pHashEntries[i - 1].next = 0; // last entry
3582
3583 //------------------------------------
3584 // Setup dynamic linking import/export array
3585 //------------------------------------
3586 pCacheEntry = pState->ComponentKernelCache.pCacheEntries;
3587 iSize = pCacheEntry[IDR_VP_LinkFile].iSize;
3588 if (iSize == 0)
3589 {
3590 VP_RENDER_NORMALMESSAGE("Link file is missing.");
3591 goto cleanup;
3592 }
3593
3594 // Get link file binary data
3595 pLinkHeader = (Kdll_LinkFileHeader *)pCacheEntry[IDR_VP_LinkFile].pBinary;
3596 if (pLinkHeader == nullptr ||
3597 pLinkHeader->dwVersion != IDR_VP_LINKFILE_VERSION ||
3598 sizeof(Kdll_LinkFileHeader) != IDR_VP_LINKFILE_HEADER)
3599 {
3600 VP_RENDER_ASSERTMESSAGE("Invalid link file version.");
3601 goto cleanup;
3602 }
3603 iSize = (iSize - IDR_VP_LINKFILE_HEADER) / sizeof(Kdll_LinkData);
3604
3605 // Create temporary list of sorted link data and offsets
3606 pLinkSort = (Kdll_LinkData *)MOS_AllocAndZeroMemory(iSize * sizeof(Kdll_LinkData));
3607 pLinkOffset = (uint32_t *)MOS_AllocAndZeroMemory((IDR_VP_TOTAL_NUM_KERNELS + 1) * sizeof(uint32_t));
3608 if (!pLinkSort || !pLinkOffset)
3609 {
3610 VP_RENDER_ASSERTMESSAGE("Failed to allocate temporary buffers.");
3611 goto cleanup;
3612 }
3613
3614 // Count number of imports for each component kernel
3615 pCacheEntry[0].pLink = pLinkData = (Kdll_LinkData *)(pLinkHeader + 1);
3616 for (i = iSize; i > 0; i--, pLinkData++)
3617 {
3618 if (pLinkData->iKUID < IDR_VP_TOTAL_NUM_KERNELS)
3619 {
3620 pCacheEntry[pLinkData->iKUID].nLink++;
3621 }
3622
3623 nExports += pLinkData->bExport;
3624 nImports += !pLinkData->bExport;
3625 }
3626
3627 // Sanity check
3628 if (nExports != (int32_t)pLinkHeader->dwExports ||
3629 nImports != (int32_t)pLinkHeader->dwImports)
3630 {
3631 VP_RENDER_ASSERTMESSAGE("Inconsistent header data.");
3632 goto cleanup;
3633 }
3634
3635 if (nExports > DL_MAX_EXPORT_COUNT)
3636 {
3637 VP_RENDER_ASSERTMESSAGE("Unsupported number of exports %d > %d.", nExports, DL_MAX_EXPORT_COUNT);
3638 goto cleanup;
3639 }
3640
3641 pState->ComponentKernelCache.pExports = pExports = (Kdll_LinkData *)(pKernelCache->pCache + pKernelCache->iCacheSize);
3642 pState->ComponentKernelCache.nExports = nExports;
3643
3644 // Calculate offsets for sorting
3645 pLinkOffset[0] = 0;
3646 pLinkData = pCacheEntry[0].pLink;
3647 for (i = 1; i < IDR_VP_TOTAL_NUM_KERNELS; i++)
3648 {
3649 pLinkOffset[i] = pLinkOffset[i - 1] + pCacheEntry[i - 1].nLink;
3650 pCacheEntry[i].pLink = (pCacheEntry[i].nLink) ? (pLinkData + pLinkOffset[i]) : nullptr;
3651 }
3652 pLinkOffset[i] = pLinkOffset[i - 1] + pCacheEntry[i - 1].nLink;
3653
3654 // Sort link data
3655 for (i = iSize; i > 0; i--, pLinkData++)
3656 {
3657 j = pLinkOffset[MOS_MIN(pLinkData->iKUID, IDR_VP_TOTAL_NUM_KERNELS)]++;
3658 pLinkSort[j] = *pLinkData;
3659
3660 // Add to export table
3661 if (pLinkData->bExport &&
3662 pLinkData->iLabelID < DL_MAX_EXPORT_COUNT)
3663 {
3664 pExports[pLinkData->iLabelID] = *pLinkData;
3665 }
3666 }
3667
3668 // Copy sort data
3669 pLinkData = pCacheEntry[0].pLink;
3670 MOS_SecureMemcpy(pLinkData, iSize * sizeof(Kdll_LinkData), (void *)pLinkSort, iSize * sizeof(Kdll_LinkData));
3671
3672 // Release sort buffers
3673 MOS_FreeMemory(pLinkOffset);
3674 MOS_FreeMemory(pLinkSort);
3675
3676 // Return
3677 return pState;
3678
3679 cleanup:
3680 if (pState)
3681 {
3682 MOS_FreeMemory(pState->pSortedRules);
3683 pState->pSortedRules = nullptr;
3684 }
3685
3686 // Free DL States and temporary sort buffers
3687 MOS_FreeMemory(pState);
3688 MOS_FreeMemory(pLinkSort);
3689 MOS_FreeMemory(pLinkOffset);
3690
3691 return nullptr;
3692 }
3693
3694 //--------------------------------------------------------------
3695 // KernelDll_ReleaseAdditionalCacheEntries - Release the additional kernel cache entries
3696 //--------------------------------------------------------------
3697 void KernelDll_ReleaseAdditionalCacheEntries(Kdll_KernelCache *pCache)
3698 {
3699 VP_RENDER_FUNCTION_ENTER;
3700 if (pCache->iCacheMaxEntries > DL_DEFAULT_COMBINED_KERNELS)
3701 {
3702 Kdll_CacheEntry *pNewEntries, *pEntries;
3703 pNewEntries = (pCache->pCacheEntries + DL_DEFAULT_COMBINED_KERNELS - 1)->pNextEntry;
3704 for (int i = 0; i < (pCache->iCacheMaxEntries - DL_DEFAULT_COMBINED_KERNELS) / DL_NEW_COMBINED_KERNELS; i++)
3705 {
3706 pEntries = (pNewEntries + DL_NEW_COMBINED_KERNELS - 1)->pNextEntry;
3707 MOS_FreeMemory(pNewEntries);
3708 pNewEntries = pEntries;
3709 }
3710 }
3711 }
3712
3713 //---------------------------------------------------------------------------------------
3714 // KernelDll_ReleaseStates - Release Kernel Dynamic Linking/Loading (Dll) States
3715 //
3716 // Parameters:
3717 // Kdll_State *pState - [in] Kernel dll State to release
3718 //
3719 // Output: Pointer to allocated Kernel dll state
3720 // nullptr - Failed to allocate Kernel dll state
3721 //-----------------------------------------------------------------------------------------
3722 void KernelDll_ReleaseStates(Kdll_State *pState)
3723 {
3724 VP_RENDER_FUNCTION_ENTER;
3725
3726 if (!pState)
3727 return;
3728 KernelDll_ReleaseAdditionalCacheEntries(&pState->KernelCache);
3729 MOS_FreeMemory(pState->ComponentKernelCache.pCache);
3730 MOS_FreeMemory(pState->CmFcPatchCache.pCache);
3731 MOS_FreeMemory(pState->pSortedRules);
3732 MOS_FreeMemory(pState);
3733 }
3734
3735 //---------------------------------------------------------------------------------------
3736 // KernelDll_SetupFunctionPointers - Setup Function pointers based on platform
3737 //
3738 // Parameters:
3739 // char *pState - [in/out] Kernel Dll state
3740 // platform - [in] platform
3741 //
3742 // Output: true - Function pointers are set
3743 // false - Failed to setup function pointers (invalid platform)
3744 //-----------------------------------------------------------------------------------------
3745 static bool KernelDll_SetupFunctionPointers(
3746 Kdll_State *pState,
3747 void (*ModifyFunctionPointers)(PKdll_State))
3748 {
3749 VP_RENDER_FUNCTION_ENTER;
3750
3751 pState->pfnSetupCSC = KernelDll_SetupCSC;
3752 pState->pfnMapCSCMatrix = KernelDll_MapCSCMatrix;
3753 pState->pfnFindRule = KernelDll_FindRule;
3754 pState->pfnUpdateState = KernelDll_UpdateState;
3755 pState->pfnSearchKernel = KernelDll_SearchKernel;
3756 pState->pfnBuildKernel = KernelDll_BuildKernel;
3757 pState->pfnStartKernelSearch = KernelDll_StartKernelSearch;
3758
3759 if (ModifyFunctionPointers != nullptr)
3760 {
3761 (*ModifyFunctionPointers)(pState);
3762 }
3763
3764 #if EMUL || VPHAL_LIB
3765 // Disable callbacks
3766 pState->pToken = nullptr;
3767 pState->pfnCbListKernel = nullptr;
3768 pState->pfnCbSearchSate = nullptr;
3769 #endif // EMUL || VPHAL_LIB
3770
3771 return true;
3772 }
3773
3774 //---------------------------------------------------------------------------------------
3775 // Kdll_AddKernelList - Add kernel to CM FC kernel list
3776 //
3777 // Parameters:
3778 // Kdll_KernelCache *pKernelCache - [in] Component kernel cache
3779 // Kdll_KernelCache *pCmFcPatchCache - [in] Component kernel patch data cache
3780 // Kdll_SearchState *pSearchState - [in/out] Kernel search state
3781 // Kdll_PatchData *pKernelPatch - [in] Kernel Patch data
3782 // void *pPatchDst - [in] Patch data Dst address
3783 // int32_t iKUID - [in] Kernel Unique ID
3784 // cm_fc_kernel_t *Cm_Fc_Kernels - [in/out] CM FC Kernels
3785 //
3786 // Output: true if suceeded, false otherwise
3787 //---------------------------------------------------------------------------------------
3788 bool Kdll_AddKernelList(Kdll_KernelCache *pKernelCache,
3789 Kdll_KernelCache * pCmFcPatchCache,
3790 Kdll_SearchState * pSearchState,
3791 int32_t iKUID,
3792 Kdll_PatchData * pKernelPatch,
3793 void * pPatchDst,
3794 cm_fc_kernel_t * Cm_Fc_Kernels)
3795 {
3796 Kdll_State * pState;
3797 Kdll_Symbol * pSymbols;
3798 Kdll_CacheEntry *kernels;
3799 Kdll_CacheEntry *pPatch;
3800 Kdll_LinkData * link;
3801 Kdll_LinkData * liSearch_reloc;
3802 int * size;
3803 int * left;
3804 int dwSize;
3805 int i;
3806 int base;
3807 bool bInline;
3808 bool res;
3809
3810 VP_RENDER_FUNCTION_ENTER;
3811
3812 res = false;
3813
3814 // Check if Kernel ID is valid
3815 if (iKUID >= pKernelCache->iCacheEntries)
3816 {
3817 VP_RENDER_NORMALMESSAGE("invalid Kernel ID %d.", iKUID);
3818 goto finish;
3819 }
3820
3821 // Get KDLL state
3822 pState = pSearchState->pKdllState;
3823
3824 // Get current combined kernel
3825 size = &pSearchState->KernelSize;
3826 left = &pSearchState->KernelLeft;
3827 pSymbols = &pSearchState->KernelLink;
3828 base = (*size) >> 2;
3829
3830 // Find selected kernel/patch and kernel size; check if there is enough space
3831 kernels = &pKernelCache->pCacheEntries[iKUID];
3832 pPatch = &pCmFcPatchCache->pCacheEntries[iKUID];
3833 dwSize = kernels->iSize;
3834 if (*left < dwSize)
3835 {
3836 VP_RENDER_NORMALMESSAGE("exceeded maximum kernel size.");
3837 goto finish;
3838 }
3839
3840 // Check if there is enough space for symbols
3841 if (pSymbols->dwCount + kernels->nLink >= pSymbols->dwSize)
3842 {
3843 VP_RENDER_NORMALMESSAGE("exceeded maximum numbers of symbols to resolve.");
3844 goto finish;
3845 }
3846
3847 #if EMUL || VPHAL_LIB
3848 VP_RENDER_NORMALMESSAGE("%s.", kernels->szName);
3849
3850 if (pState->pfnCbListKernel)
3851 {
3852 pState->pfnCbListKernel(pState->pToken, kernels->szName);
3853 }
3854 #elif _DEBUG || _RELEASE_INTERNAL // EMUL || VPHAL_LIB
3855 VP_RENDER_NORMALMESSAGE("%s.", kernels->szName);
3856 #endif // _DEBUG
3857
3858 MT_LOG1(MT_VP_KERNEL_LIST_ADD, MT_NORMAL, MT_VP_KERNEL_ID, iKUID);
3859
3860 // Append symbols to resolve, relocate symbols
3861 link = kernels->pLink;
3862 liSearch_reloc = pSymbols->pLink + pSymbols->dwCount;
3863
3864 bInline = false;
3865 if (link)
3866 {
3867 for (i = kernels->nLink; i > 0; i--, link++)
3868 {
3869 if (link->bInline)
3870 {
3871 // Inline code included
3872 if (!link->bExport)
3873 {
3874 bInline = true;
3875 }
3876 }
3877 else
3878 {
3879 *liSearch_reloc = *link;
3880 liSearch_reloc->dwOffset += base;
3881 liSearch_reloc++;
3882
3883 pSymbols->dwCount++;
3884 }
3885 }
3886 }
3887
3888 *size += dwSize;
3889 *left -= dwSize;
3890 Cm_Fc_Kernels->binary_buf = (const char *)kernels->pBinary;
3891 Cm_Fc_Kernels->binary_size = kernels->iSize;
3892 Cm_Fc_Kernels->patch_buf = (const char *)pPatch->pBinary;
3893 Cm_Fc_Kernels->patch_size = pPatch->iSize;
3894 res = true;
3895
3896 finish:
3897 return res;
3898 }
3899
3900 //---------------------------------------------------------------------------------------
3901 // KernelDll_BuildKernel_CmFc - Build CM based FC combine Kernel
3902 //
3903 // Parameters: [in/out] pState - Pointer to Kernel binary file loaded in sys memory
3904 // [in/out] pSearchState - Kernel file size
3905 //
3906 // Output: bool
3907 // TRUE - Successful FALSE - Failed
3908 //-----------------------------------------------------------------------------------------
3909 bool KernelDll_BuildKernel_CmFc(Kdll_State *pState, Kdll_SearchState *pSearchState)
3910 {
3911 Kdll_KernelCache *pKernelCache = &pState->ComponentKernelCache;
3912 Kdll_KernelCache *pPatchCache = &pState->CmFcPatchCache;
3913 Kdll_KernelCache *pCustomCache = pState->pCustomKernelCache;
3914 bool res;
3915 int32_t offset = 0;
3916 int32_t * pKernelID, *pPatchID;
3917 uint8_t * pPatchData;
3918 Kdll_PatchData * pKernelPatch;
3919 uint8_t * kernel = pSearchState->Kernel;
3920 Kdll_Symbol * pSymbols = &pSearchState->KernelLink;
3921 uint32_t nExports = pKernelCache->nExports;
3922 Kdll_LinkData * pExports = pKernelCache->pExports;
3923 Kdll_LinkData * pLink;
3924 int32_t iOffset;
3925 uint32_t dwResolveOffset[DL_MAX_EXPORT_COUNT];
3926 uint32_t dwTotalKernelCount;
3927 size_t stEstimatedKernelSize;
3928 int32_t iKUID;
3929 bool bResolveDone;
3930 int32_t i;
3931 cm_fc_kernel_t Cm_Fc_kernels[DL_MAX_KERNELS];
3932
3933 VP_RENDER_FUNCTION_ENTER;
3934
3935 // Disable pop-up box window for STL assertion to avoid VM hang in auto test.
3936 #if (!LINUX && !ANDROID)
3937 uint32_t prevErrorMode = ::SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
3938 #if defined(_MSC_VER)
3939 ::_set_error_mode(_OUT_TO_STDERR);
3940 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
3941 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
3942 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
3943 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
3944 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
3945 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
3946 #endif
3947 #endif
3948
3949 pSearchState->KernelLink.dwSize = DL_MAX_SYMBOLS;
3950 pSearchState->KernelLink.dwCount = 0;
3951 pSearchState->KernelLink.pLink = pSearchState->LinkArray;
3952 pSearchState->KernelSize = 0;
3953 pSearchState->KernelLeft = sizeof(pSearchState->Kernel);
3954 pSearchState->KernelLink.dwCount = 0;
3955
3956 MOS_ZeroMemory(Cm_Fc_kernels, sizeof(Cm_Fc_kernels));
3957 dwTotalKernelCount = 0;
3958 stEstimatedKernelSize = 0;
3959
3960 #if EMUL || VPHAL_LIB || _DEBUG
3961 VP_RENDER_NORMALMESSAGE("Component Kernels:");
3962 #endif // EMUL || VPHAL_LIB || _DEBUG
3963
3964 pKernelID = pSearchState->KernelID;
3965 pPatchID = pSearchState->PatchID;
3966 pPatchData = nullptr;
3967
3968 for (offset = 0; offset < pSearchState->KernelCount; offset++, pKernelID++, pPatchID++, dwTotalKernelCount++)
3969 {
3970 // Get patch information associated with the kernel
3971 pKernelPatch = (*pPatchID >= 0) ? &(pSearchState->Patches[*pPatchID]) : nullptr;
3972
3973 // Append/Patch kernel from internal cache
3974 res = Kdll_AddKernelList(pKernelCache, pPatchCache, pSearchState, *pKernelID, pKernelPatch, pPatchData, &Cm_Fc_kernels[dwTotalKernelCount]);
3975
3976 stEstimatedKernelSize += Cm_Fc_kernels[dwTotalKernelCount].binary_size;
3977
3978 if (*pKernelID == IDR_VP_EOT)
3979 {
3980 dwTotalKernelCount--;
3981 }
3982
3983 if (!res)
3984 {
3985 VP_RENDER_NORMALMESSAGE("Failed to build kernel ID %d.", pSearchState->KernelID[offset]);
3986 res = false;
3987 goto finish;
3988 }
3989 }
3990
3991 // Resolve kernel dependencies
3992 MOS_ZeroMemory(dwResolveOffset, sizeof(dwResolveOffset));
3993
3994 do
3995 {
3996 // Update exports
3997 for (pLink = pSymbols->pLink, i = pSymbols->dwCount; i > 0; i--, pLink++)
3998 {
3999 if (pLink->bExport)
4000 {
4001 dwResolveOffset[pLink->iLabelID] = pLink->dwOffset;
4002 }
4003 }
4004
4005 bResolveDone = true;
4006 for (pLink = pSymbols->pLink, i = pSymbols->dwCount; i > 0; i--, pLink++)
4007 {
4008 // validate label
4009 if (pLink->iLabelID > nExports || // invalid label
4010 pExports[pLink->iLabelID].bExport == 0) // label not in the export table
4011 {
4012 VP_RENDER_NORMALMESSAGE("Invalid/unresolved label %d.", pLink->iLabelID);
4013 res = false;
4014 goto finish;
4015 }
4016
4017 // load dependencies
4018 if (!pLink->bExport && !dwResolveOffset[pLink->iLabelID])
4019 {
4020 // set flag for another pass as newly loaded
4021 // kernels may contain dependencies of their own
4022 bResolveDone = false;
4023
4024 // Add dependencies to kernel list
4025 iKUID = pExports[pLink->iLabelID].iKUID;
4026 res = Kdll_AddKernelList(pKernelCache, pPatchCache, pSearchState, iKUID, nullptr, nullptr, &Cm_Fc_kernels[dwTotalKernelCount]);
4027
4028 if (!res)
4029 {
4030 VP_RENDER_NORMALMESSAGE("Failed to build kernel ID %d.", pSearchState->KernelID[offset]);
4031 res = false;
4032 goto finish;
4033 }
4034
4035 dwTotalKernelCount++;
4036
4037 // Restart
4038 break;
4039 }
4040 } // for
4041 } while (!bResolveDone);
4042
4043 if (stEstimatedKernelSize > DL_MAX_KERNEL_SIZE)
4044 {
4045 res = false;
4046 VP_RENDER_NORMALMESSAGE("Kernel size exceeded kdll limitatin.");
4047 goto finish;
4048 }
4049
4050 stEstimatedKernelSize = DL_MAX_KERNEL_SIZE;
4051
4052 // Get combine kernel binary from CMFC lib
4053 if (CM_FC_OK != cm_fc_combine_kernels(dwTotalKernelCount, Cm_Fc_kernels, (char *)pSearchState->Kernel, &stEstimatedKernelSize, nullptr))
4054 {
4055 res = false;
4056 VP_RENDER_NORMALMESSAGE("cm_fc_combine_kernels() function call failed.");
4057 goto finish;
4058 }
4059
4060 // Get combine kernel binary size from CMFC lib
4061 pSearchState->KernelSize = (int)stEstimatedKernelSize;
4062
4063 res = true;
4064
4065 finish:
4066 #if (!LINUX && !ANDROID)
4067 ::SetErrorMode(prevErrorMode);
4068 #endif
4069 return res;
4070 }
4071
4072 //--------------------------------------------------------------
4073 // KernelDll_AllocateHashEntry - Allocate hash entry
4074 //--------------------------------------------------------------
4075 uint16_t KernelDll_AllocateHashEntry(Kdll_KernelHashTable *pHashTable,
4076 uint32_t hash)
4077 {
4078 Kdll_KernelHashEntry *pHashEntry = &pHashTable->HashEntry[0] - 1;
4079 Kdll_KernelHashEntry *pNewEntry;
4080 uint32_t folded_hash;
4081 uint16_t entry;
4082
4083 VP_RENDER_FUNCTION_ENTER;
4084
4085 entry = pHashTable->pool;
4086 if (!entry)
4087 {
4088 return 0;
4089 }
4090
4091 // Get entry from pool
4092 pNewEntry = &pHashEntry[entry];
4093 pHashTable->pool = pNewEntry->next;
4094 if (pHashTable->last == entry)
4095 {
4096 pHashTable->last = 0;
4097 }
4098
4099 // Initialize entry, attach to the hash table
4100 FOLD_HASH(folded_hash, hash);
4101 pNewEntry->dwHash = hash;
4102 pNewEntry->next = pHashTable->wHashTable[folded_hash];
4103 pNewEntry->iFilter = 0;
4104 pNewEntry->pFilter = nullptr;
4105 pNewEntry->pCacheEntry = nullptr;
4106 pHashTable->wHashTable[folded_hash] = entry;
4107 return entry;
4108 }
4109
4110 //--------------------------------------------------------------
4111 // KernelDll_CacheGarbageCollection - performs garbage collection
4112 //--------------------------------------------------------------
4113 bool KernelDll_GarbageCollection(Kdll_State *pState, int32_t size)
4114 {
4115 Kdll_KernelCache *pCache = &pState->KernelCache;
4116 Kdll_CacheEntry *pEntry = pCache->pCacheEntries;
4117 Kdll_CacheEntry *pOldest = nullptr;
4118 Kdll_KernelHashTable *pHashTable = &pState->KernelHashTable;
4119 Kdll_KernelHashEntry *pHashEntry = &pHashTable->HashEntry[0] - 1;
4120 uint32_t dwOldest = (uint32_t)-1;
4121 uint16_t wEntry = 0;
4122 int32_t i;
4123
4124 MOS_UNUSED(size);
4125
4126 VP_RENDER_FUNCTION_ENTER;
4127
4128 // Adjust refresh values to avoid overflow
4129 if (pState->dwRefresh > 0xffff0000)
4130 {
4131 pState->dwRefresh -= 0x80000000;
4132 for (i = pCache->iCacheMaxEntries; i > 0; i--)
4133 {
4134 if (pEntry->dwRefresh < 0x80000000)
4135 pEntry->dwRefresh = 0;
4136 else
4137 pEntry->dwRefresh -= 0x80000000;
4138 pEntry = pEntry->pNextEntry;
4139 }
4140 }
4141
4142 // No need to deallocate old entries
4143 if (pCache->iCacheEntries < DL_MAX_COMBINED_KERNELS)
4144 {
4145 return true;
4146 }
4147
4148 for (i = pCache->iCacheMaxEntries; i > 0; i--)
4149 {
4150 // deallocate old unreferenced entries
4151 if (pEntry->iKCID != -1 && pEntry->dwLoaded == 0)
4152 {
4153 if (pEntry->dwRefresh < dwOldest)
4154 {
4155 pOldest = pEntry;
4156 dwOldest = pEntry->dwRefresh;
4157 wEntry = pEntry->wHashEntry;
4158 }
4159 }
4160 pEntry = pEntry->pNextEntry;
4161 }
4162
4163 // No entry to release, sanity checks
4164 pHashEntry += wEntry;
4165 if (!pOldest ||
4166 wEntry == 0 ||
4167 pHashEntry->pCacheEntry != pOldest)
4168 {
4169 VP_RENDER_ASSERT(false);
4170 return false;
4171 }
4172
4173 // Release hash and cache entries
4174 KernelDll_ReleaseHashEntry(pHashTable, wEntry);
4175 KernelDll_ReleaseCacheEntry(pCache, pOldest);
4176
4177 return true;
4178 }
4179
4180 //--------------------------------------------------------------
4181 // KernelDll_AllocateCacheEntry - Allocate cache entry for a given size
4182 //--------------------------------------------------------------
4183 Kdll_CacheEntry *
4184 KernelDll_AllocateCacheEntry(Kdll_KernelCache *pCache, int32_t iSize)
4185 {
4186 Kdll_CacheEntry *pEntry = pCache->pCacheEntries;
4187 uint8_t *pCacheBinary = nullptr;
4188 Kdll_CacheEntry *pCacheNextEntry = nullptr;
4189 int32_t i, j;
4190
4191 VP_RENDER_FUNCTION_ENTER;
4192
4193 // Check size
4194 if (iSize > DL_CACHE_BLOCK_SIZE)
4195 {
4196 return nullptr;
4197 }
4198
4199 // Search empty entry
4200 j = pCache->iCacheMaxEntries;
4201 for (i = 0; i < j; i++)
4202 {
4203 if (pEntry->iKCID == -1)
4204 {
4205 break;
4206 }
4207 pEntry = pEntry->pNextEntry;
4208 }
4209 if (i == j)
4210 {
4211 // Try to allocate more cache entries
4212 pEntry = KernelDll_AllocateAdditionalCacheEntries(pCache);
4213 if(! pEntry)
4214 {
4215 return nullptr;
4216 }
4217 }
4218
4219 // Reset entry
4220 pCacheBinary = pEntry->pBinary;
4221 pCacheNextEntry = pEntry->pNextEntry;
4222 MOS_ZeroMemory(pEntry, sizeof(Kdll_CacheEntry));
4223 pEntry->iSize = iSize;
4224 pEntry->pBinary = pCacheBinary;
4225 pEntry->pNextEntry = pCacheNextEntry;
4226
4227 // Increment entries
4228 pCache->iCacheEntries++;
4229 return pEntry;
4230 }
4231
4232 //--------------------------------------------------------------
4233 // KernelDll_AllocateAdditionalCacheEntries - Allocate more kernel cache entries
4234 //--------------------------------------------------------------
4235 Kdll_CacheEntry *
4236 KernelDll_AllocateAdditionalCacheEntries(Kdll_KernelCache *pCache)
4237 {
4238 Kdll_CacheEntry *pNewEntry = nullptr;
4239 Kdll_CacheEntry *pChcheEntry;
4240 int i, j;
4241
4242 VP_RENDER_FUNCTION_ENTER;
4243
4244 // Check num
4245 if (pCache->iCacheEntries + DL_NEW_COMBINED_KERNELS > DL_MAX_COMBINED_KERNELS)
4246 {
4247 VP_RENDER_ASSERTMESSAGE("KernelDll_AllocateAdditionalCacheEntries: Can't allocate more kernel cache entries\n");
4248 return nullptr;
4249 }
4250
4251 // Allocate the new entires
4252 i = (sizeof(Kdll_CacheEntry) + DL_CACHE_BLOCK_SIZE) * DL_NEW_COMBINED_KERNELS;
4253 pNewEntry = (Kdll_CacheEntry *)MOS_AllocAndZeroMemory(i);
4254 if (!pNewEntry)
4255 {
4256 VP_RENDER_ASSERTMESSAGE("KernelDll_AllocateAdditionalCacheEntries: Failed to allocate kernel cache entries\n");
4257 return nullptr;
4258 }
4259
4260 // Update the cache entires
4261 pChcheEntry = pCache->pCacheEntries;
4262 for(j = 0; j < pCache->iCacheMaxEntries - 1; j++)
4263 {
4264 pChcheEntry = pChcheEntry->pNextEntry;
4265 }
4266 pChcheEntry->pNextEntry = pNewEntry;
4267 for(j = 0; j < DL_NEW_COMBINED_KERNELS; j++, pNewEntry++)
4268 {
4269 pNewEntry->iKUID = -1;
4270 pNewEntry->iKCID = -1;
4271 pNewEntry->pBinary = (uint8_t *)(pNewEntry + DL_NEW_COMBINED_KERNELS - j) + j * DL_CACHE_BLOCK_SIZE;
4272 if(j != DL_NEW_COMBINED_KERNELS - 1)
4273 {
4274 pNewEntry->pNextEntry = pNewEntry + 1;
4275 }
4276 else
4277 {
4278 pNewEntry->pNextEntry = nullptr;
4279 }
4280 }
4281
4282 pCache->iCacheMaxEntries += DL_NEW_COMBINED_KERNELS;
4283 pCache->iCacheSize += DL_NEW_COMBINED_KERNELS * DL_CACHE_BLOCK_SIZE;
4284 pCache->iCacheFree += DL_NEW_COMBINED_KERNELS * DL_CACHE_BLOCK_SIZE;
4285 return (Kdll_CacheEntry *)(pNewEntry - DL_NEW_COMBINED_KERNELS);
4286 }
4287
4288 //--------------------------------------------------------------
4289 // KernelDll_AddKernel - Add kernel into hash table and kernel cache
4290 //--------------------------------------------------------------
4291 Kdll_CacheEntry *
4292 KernelDll_AddKernel(Kdll_State *pState, // Kernel Dll state
4293 Kdll_SearchState *pSearchState, // Search state
4294 Kdll_FilterEntry *pFilter, // Original filter
4295 int32_t iFilterSize, // Original filter size
4296 uint32_t dwHash)
4297 {
4298 Kdll_CacheEntry *pCacheEntry;
4299 Kdll_KernelHashTable *pHashTable;
4300 Kdll_KernelHashEntry *pHashEntry;
4301 uint16_t entry;
4302 int32_t size;
4303 uint8_t *ptr;
4304
4305 VP_RENDER_FUNCTION_ENTER;
4306
4307 // Check kernel
4308 if (pSearchState->KernelSize <= 0)
4309 {
4310 return nullptr;
4311 }
4312
4313 // Get hash table
4314 pHashTable = &pState->KernelHashTable;
4315 pHashEntry = &pHashTable->HashEntry[0] - 1; // all indices are 1 based (0 = null)
4316
4317 // allocate space in kernel cache to store the kernel, filter, CSC parameters
4318 size = pSearchState->KernelSize + // Kernel
4319 pSearchState->iFilterSize * sizeof(Kdll_FilterEntry) * 2 + // Original + Modified Filter
4320 sizeof(Kdll_CSC_Params) + // CSC parameters
4321 sizeof(VPHAL_CSPACE); // Intermediate Color Space for colorfill
4322
4323 // Run garbage collection, create space for new kernel and metadata
4324 KernelDll_GarbageCollection(pState, size);
4325
4326 // Get new kernel cache entry
4327 pCacheEntry = KernelDll_AllocateCacheEntry(&pState->KernelCache, size);
4328 if (!pCacheEntry)
4329 {
4330 VP_RENDER_ASSERTMESSAGE("Failed to allocate cache space for new kernel.");
4331 return nullptr;
4332 }
4333
4334 // Get hash entry
4335 entry = KernelDll_AllocateHashEntry(pHashTable, dwHash);
4336 if (!entry)
4337 {
4338 VP_RENDER_ASSERTMESSAGE("Failed to allocate hash entry for new kernel.");
4339 KernelDll_ReleaseCacheEntry(&pState->KernelCache, pCacheEntry);
4340 return nullptr;
4341 }
4342
4343 // Setup cache entry, copy kernel
4344 pCacheEntry->iKUID = -1;
4345 pCacheEntry->iKCID = pState->KernelCache.iCacheID; // Create new kernel cache id (KCID)
4346 pCacheEntry->dwRefresh = pState->dwRefresh++;
4347 pCacheEntry->wHashEntry = entry;
4348
4349 // Save kernel
4350 pCacheEntry->iSize = pSearchState->KernelSize;
4351 MOS_SecureMemcpy(pCacheEntry->pBinary, pSearchState->KernelSize, (void *)pSearchState->Kernel, pSearchState->KernelSize);
4352 ptr = pCacheEntry->pBinary + pSearchState->KernelSize;
4353
4354 // Save modified filter
4355 pCacheEntry->iFilterSize = pSearchState->iFilterSize;
4356 pCacheEntry->pFilter = (Kdll_FilterEntry *) (ptr);
4357 MOS_SecureMemcpy(ptr, pSearchState->iFilterSize * sizeof(Kdll_FilterEntry), (void *)pSearchState->Filter, pSearchState->iFilterSize * sizeof(Kdll_FilterEntry));
4358 ptr += pSearchState->iFilterSize * sizeof(Kdll_FilterEntry);
4359
4360 // Save CSC parameters associated with the kernel
4361 pCacheEntry->pCscParams = (Kdll_CSC_Params *) (ptr);
4362 MOS_SecureMemcpy(ptr, sizeof(Kdll_CSC_Params), (void *)&pSearchState->CscParams, sizeof(Kdll_CSC_Params));
4363 ptr += sizeof(Kdll_CSC_Params);
4364 // Save intermediate color space for colorfill
4365 pCacheEntry->colorfill_cspace = pState->colorfill_cspace;
4366 ptr += sizeof(VPHAL_CSPACE);
4367
4368 // increment KCID (Range = 0x00010000 - 0x7fffffff)
4369 pState->KernelCache.iCacheID = 0x00010000 + (pState->KernelCache.iCacheID - 0x0000ffff) % 0x7fff0000;
4370
4371 // Setup hash entry, copy filter
4372 pHashEntry += entry;
4373 pHashEntry->pCacheEntry = pCacheEntry;
4374
4375 // Save original filter for search purposes - modified filter is used for rendering
4376 pHashEntry->iFilter = iFilterSize;
4377 pHashEntry->pFilter = (Kdll_FilterEntry *) (ptr);
4378 MOS_SecureMemcpy(ptr, iFilterSize * sizeof(Kdll_FilterEntry), (void *)pFilter, iFilterSize * sizeof(Kdll_FilterEntry));
4379
4380 return pCacheEntry;
4381 }
4382
4383 //--------------------------------------------------------------
4384 // KernelDll_ReleaseHashEntry - Release hash table entry
4385 //--------------------------------------------------------------
4386 void KernelDll_ReleaseHashEntry(Kdll_KernelHashTable *pHashTable, uint16_t entry)
4387 {
4388 Kdll_KernelHashEntry *pHashEntry = &pHashTable->HashEntry[0] - 1;
4389 uint32_t folded_hash;
4390 uint16_t next;
4391
4392 VP_RENDER_FUNCTION_ENTER;
4393
4394 if (entry == 0)
4395 {
4396 return;
4397 }
4398
4399 // unlink entry
4400 next = pHashEntry[entry].next;
4401 pHashEntry[entry].next = 0;
4402
4403 // remove references to entry from hash table
4404 FOLD_HASH(folded_hash, pHashEntry[entry].dwHash);
4405 if (pHashTable->wHashTable[folded_hash] == entry)
4406 {
4407 pHashTable->wHashTable[folded_hash] = next;
4408 }
4409 else
4410 {
4411 uint16_t prev = pHashTable->wHashTable[folded_hash];
4412
4413 while (prev != 0 &&
4414 pHashEntry[prev].next != entry)
4415 {
4416 prev = pHashEntry[prev].next;
4417 }
4418
4419 if (prev)
4420 {
4421 pHashEntry[prev].next = next;
4422 }
4423 }
4424
4425 // return entry to pool
4426 if (pHashTable->pool == 0)
4427 {
4428 pHashTable->pool = entry;
4429 }
4430 else
4431 {
4432 pHashEntry[pHashTable->last].next = entry;
4433 }
4434 pHashTable->last = entry;
4435 }
4436
4437 //--------------------------------------------------------------
4438 // KernelDll_ReleaseCacheEntry - Release cache entry
4439 //--------------------------------------------------------------
4440 void KernelDll_ReleaseCacheEntry(Kdll_KernelCache *pCache,
4441 Kdll_CacheEntry *pEntry)
4442 {
4443 pEntry->iKUID = -1;
4444 pEntry->iKCID = -1;
4445 pCache->iCacheEntries--;
4446 }
4447
4448 //---------------------------------------------------------------------------------------
4449 // KernelDll_SetupFunctionPointers - Setup Function pointers based on platform
4450 //
4451 // Parameters:
4452 // KdllState *pState - [in/out] Kernel Dll state
4453 //
4454 // Output: true - Function pointers are set
4455 // false - Failed to setup function pointers (invalid platform)
4456 //-----------------------------------------------------------------------------------------
4457 bool KernelDll_SetupFunctionPointers_Ext(
4458 Kdll_State *pState)
4459 {
4460 VP_RENDER_FUNCTION_ENTER;
4461
4462 if (pState && pState->bEnableCMFC)
4463 {
4464 pState->pfnBuildKernel = KernelDll_BuildKernel_CmFc;
4465 }
4466
4467 return true;
4468 }
4469
4470 #ifdef __cplusplus
4471 }
4472 #endif // __cplusplus