Path: blob/21.2-virgl/src/gallium/frontends/nine/nine_helpers.h
4561 views
/*1* Copyright 2011 Joakim Sindholt <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* on the rights to use, copy, modify, merge, publish, distribute, sub7* license, and/or sell copies of the Software, and to permit persons to whom8* the Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL17* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,18* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR19* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE20* USE OR OTHER DEALINGS IN THE SOFTWARE. */2122#ifndef _NINE_HELPERS_H_23#define _NINE_HELPERS_H_2425#include "iunknown.h"26#include "nine_lock.h"2728/*29* Note: we use these function rather than the MIN2, MAX2, CLAMP macros to30* avoid evaluating arguments (which are often function calls) more than once.31*/3233static inline unsigned _min(unsigned a, unsigned b)34{35return (a < b) ? a : b;36}373839/* Sshhh ... */40#define nine_reference(a, b) _nine_reference((void **)(a), (b))4142static inline void _nine_reference(void **ref, void *ptr)43{44if (*ref != ptr) {45if (*ref)46NineUnknown_Release(*ref);47if (ptr)48NineUnknown_AddRef(ptr);49*ref = ptr;50}51}5253#define nine_reference_set(a, b) _nine_reference_set((void **)(a), (b))5455static inline void _nine_reference_set(void **ref, void *ptr)56{57*ref = ptr;58if (ptr)59NineUnknown_AddRef(ptr);60}6162#define nine_bind(a, b) _nine_bind((void **)(a), (b))6364static inline void _nine_bind(void **dst, void *obj)65{66if (*dst != obj) {67if (*dst)68NineUnknown_Unbind(*dst);69if (obj)70NineUnknown_Bind(obj);71*dst = obj;72}73}7475#define NINE_DEVICE_CHILD_NEW(nine, out, dev, ...) \76{ \77struct NineUnknownParams __params; \78struct Nine##nine *__data; \79\80__data = CALLOC_STRUCT(Nine##nine); \81if (!__data) { return E_OUTOFMEMORY; } \82\83__params.vtable = ((dev)->params.BehaviorFlags & D3DCREATE_MULTITHREADED) ? &Lock##nine##_vtable : &Nine##nine##_vtable; \84__params.guids = Nine##nine##_IIDs; \85__params.dtor = (void *)Nine##nine##_dtor; \86__params.container = NULL; \87__params.device = dev; \88__params.start_with_bind_not_ref = false; \89{ \90HRESULT __hr = Nine##nine##_ctor(__data, &__params, ## __VA_ARGS__); \91if (FAILED(__hr)) { \92Nine##nine##_dtor(__data); \93return __hr; \94} \95} \96\97*(out) = __data; \98} \99return D3D_OK100101#define NINE_DEVICE_CHILD_BIND_NEW(nine, out, dev, ...) \102{ \103struct NineUnknownParams __params; \104struct Nine##nine *__data; \105\106__data = CALLOC_STRUCT(Nine##nine); \107if (!__data) { return E_OUTOFMEMORY; } \108\109__params.vtable = ((dev)->params.BehaviorFlags & D3DCREATE_MULTITHREADED) ? &Lock##nine##_vtable : &Nine##nine##_vtable; \110__params.guids = Nine##nine##_IIDs; \111__params.dtor = (void *)Nine##nine##_dtor; \112__params.container = NULL; \113__params.device = dev; \114__params.start_with_bind_not_ref = true; \115{ \116HRESULT __hr = Nine##nine##_ctor(__data, &__params, ## __VA_ARGS__); \117if (FAILED(__hr)) { \118Nine##nine##_dtor(__data); \119return __hr; \120} \121} \122\123*(out) = __data; \124} \125return D3D_OK126127#define NINE_NEW(nine, out, lock, ...) \128{ \129struct NineUnknownParams __params; \130struct Nine##nine *__data; \131\132__data = CALLOC_STRUCT(Nine##nine); \133if (!__data) { return E_OUTOFMEMORY; } \134\135__params.vtable = (lock) ? &Lock##nine##_vtable : &Nine##nine##_vtable; \136__params.guids = Nine##nine##_IIDs; \137__params.dtor = (void *)Nine##nine##_dtor; \138__params.container = NULL; \139__params.device = NULL; \140__params.start_with_bind_not_ref = false; \141{ \142HRESULT __hr = Nine##nine##_ctor(__data, &__params, ## __VA_ARGS__); \143if (FAILED(__hr)) { \144Nine##nine##_dtor(__data); \145return __hr; \146} \147} \148\149*(out) = __data; \150} \151return D3D_OK152153static inline float asfloat(DWORD value)154{155union {156float f;157DWORD w;158} u;159u.w = value;160return u.f;161}162163struct nine_range164{165struct nine_range *next;166int16_t bgn; /* inclusive */167int16_t end; /* exclusive */168};169170/* We won't ever need more than 256 ranges, so just allocate once. */171struct nine_range_pool172{173struct nine_range *free;174struct nine_range **slabs;175unsigned num_slabs;176unsigned num_slabs_max;177};178179static inline void180nine_range_pool_put(struct nine_range_pool *pool, struct nine_range *r)181{182r->next = pool->free;183pool->free = r;184}185186static inline void187nine_range_pool_put_chain(struct nine_range_pool *pool,188struct nine_range *head,189struct nine_range *tail)190{191tail->next = pool->free;192pool->free = head;193}194195void196nine_ranges_insert(struct nine_range **head, int16_t bgn, int16_t end,197struct nine_range_pool *pool);198199#endif /* _NINE_HELPERS_H_ */200201202