1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
4 * Authors: Steve Borho <steve@borho.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com
22 *****************************************************************************/
24 #include "threadpool.h"
25 #include "threading.h"
26 #include "wavefront.h"
30 // x265 private namespace
32 bool WaveFront::init(int numRows
)
36 m_numWords
= (numRows
+ 31) >> 5;
37 m_internalDependencyBitmap
= X265_MALLOC(uint32_t, m_numWords
);
38 if (m_internalDependencyBitmap
)
39 memset((void*)m_internalDependencyBitmap
, 0, sizeof(uint32_t) * m_numWords
);
41 m_externalDependencyBitmap
= X265_MALLOC(uint32_t, m_numWords
);
42 if (m_externalDependencyBitmap
)
43 memset((void*)m_externalDependencyBitmap
, 0, sizeof(uint32_t) * m_numWords
);
45 return m_internalDependencyBitmap
&& m_externalDependencyBitmap
;
48 WaveFront::~WaveFront()
50 x265_free((void*)m_internalDependencyBitmap
);
51 x265_free((void*)m_externalDependencyBitmap
);
54 void WaveFront::clearEnabledRowMask()
56 memset((void*)m_externalDependencyBitmap
, 0, sizeof(uint32_t) * m_numWords
);
59 void WaveFront::enqueueRow(int row
)
61 uint32_t bit
= 1 << (row
& 31);
62 ATOMIC_OR(&m_internalDependencyBitmap
[row
>> 5], bit
);
63 if (m_pool
) m_pool
->pokeIdleThread();
66 void WaveFront::enableRow(int row
)
68 uint32_t bit
= 1 << (row
& 31);
69 ATOMIC_OR(&m_externalDependencyBitmap
[row
>> 5], bit
);
72 void WaveFront::enableAllRows()
74 memset((void*)m_externalDependencyBitmap
, ~0, sizeof(uint32_t) * m_numWords
);
77 bool WaveFront::dequeueRow(int row
)
79 uint32_t bit
= 1 << (row
& 31);
80 return !!(ATOMIC_AND(&m_internalDependencyBitmap
[row
>> 5], ~bit
) & bit
);
83 bool WaveFront::findJob(int threadId
)
88 for (int w
= 0; w
< m_numWords
; w
++)
90 uint32_t oldval
= m_internalDependencyBitmap
[w
] & m_externalDependencyBitmap
[w
];
95 uint32_t bit
= 1 << id
;
96 if (ATOMIC_AND(&m_internalDependencyBitmap
[w
], ~bit
) & bit
)
98 /* we cleared the bit, we get to process the row */
99 processRow(w
* 32 + id
, threadId
);
103 // some other thread cleared the bit, try another bit
104 oldval
= m_internalDependencyBitmap
[w
] & m_externalDependencyBitmap
[w
];
108 // made it through the bitmap without finding any enqueued rows