示例#1
0
文件: heap_page.cpp 项目: Babelz/SaNi
	void HeapPage::joinBlocks(std::list<HeapBlock> &newReleasedBlocks, std::list<HeapBlock> &newBlocks)
	{
		auto current = blocks.begin();

		while (current != blocks.end()) {
			if (current->isReleased()) {
				// Found released blocks, look forward for more
				// released blocks.
				HeapBlock& head = *current;
				uint32 bytes = 0;

				while (current != blocks.end() && current->isReleased()) {
					bytes += current->getSize();

					current++;
				}

				const uint32 diff = bytes - head.getSize();
				head.grow(diff);

				newReleasedBlocks.push_back(head);
				newBlocks.push_back(head);
			} else {
				// Just push the block to block list, it is occupied.
				newBlocks.push_back(*current);

				current++;
			}
		}
	}
PassRefPtr<WebCLBuffer> WebCLContext::createBufferBase(unsigned memFlags, unsigned sizeInBytes, void* hostPtr, ExceptionState& es)
{
    if (isReleased()) {
        es.throwWebCLException(WebCLException::InvalidContext, WebCLException::invalidContextMessage);
        return nullptr;
    }

    if (!WebCLInputChecker::isValidMemoryObjectFlag(memFlags)) {
        es.throwWebCLException(WebCLException::InvalidValue, WebCLException::invalidValueMessage);
        return nullptr;
    }

    unsigned num = 0;
    for (size_t i = 0; i < m_devices.size(); ++i) {
        unsigned maxMemAllocSize = m_devices[i]->getMaxMemAllocSize();
        if (maxMemAllocSize && maxMemAllocSize < sizeInBytes) {
            num++;
        }
    }

    if (num != 0 && num == m_devices.size()) {
        es.throwWebCLException(WebCLException::InvalidBufferSize, WebCLException::invalidBufferSizeMessage);
        return nullptr;
    }

    if (sizeInBytes == 0) {
        es.throwWebCLException(WebCLException::InvalidBufferSize, WebCLException::invalidBufferSizeMessage);
        return nullptr;
    }
    RefPtr<WebCLBuffer> buffer = WebCLBuffer::create(this, memFlags, sizeInBytes, hostPtr, es);
    if (!hostPtr && buffer)
        m_memoryUtil->bufferCreated(buffer.get(), es);

    return buffer.release();
}
ScriptValue WebCLContext::getInfo(ScriptState* scriptState, int paramName, ExceptionState& es)
{
    v8::Handle<v8::Object> creationContext = scriptState->context()->Global();
    v8::Isolate* isolate = scriptState->isolate();

    if (isReleased()) {
        es.throwWebCLException(WebCLException::InvalidContext, WebCLException::invalidContextMessage);
        return ScriptValue(scriptState, v8::Null(isolate));
    }

    cl_int err = CL_SUCCESS;
    cl_uint uintUnits = 0;
    Vector<RefPtr<WebCLDevice>> result;
    switch (paramName) {
    case CL_CONTEXT_NUM_DEVICES:
        err = clGetContextInfo(m_clContext, CL_CONTEXT_NUM_DEVICES, sizeof(cl_uint), &uintUnits, nullptr);
        if (err == CL_SUCCESS)
            return ScriptValue(scriptState, v8::Integer::NewFromUnsigned(isolate, static_cast<unsigned>(uintUnits)));
        WebCLException::throwException(err, es);
        return ScriptValue(scriptState, v8::Null(isolate));
    case CL_CONTEXT_DEVICES:
        return ScriptValue(scriptState, toV8(m_devices, creationContext, isolate));
    default:
        es.throwWebCLException(WebCLException::InvalidValue, WebCLException::invalidValueMessage);
        return ScriptValue(scriptState, v8::Null(isolate));
    }
}
示例#4
0
void WebCLKernel::setArg(unsigned index, WebCLSampler* sampler, ExceptionState& es)
{
    if (isReleased()) {
        es.throwWebCLException(WebCLException::INVALID_KERNEL, WebCLException::invalidKernelMessage);
        return;
    }

    cl_sampler clSamplerId = nullptr;
    if (sampler) {
        clSamplerId = sampler->getSampler();
        if (!clSamplerId) {
            es.throwWebCLException(WebCLException::INVALID_MEM_OBJECT, WebCLException::invalidMemObjectMessage);
            return;
        }
    }

    WebCLKernelArgInfo* argInfo = getArgInfo(index, es);
    if (!argInfo || argInfo->type() != WebCLKernelArgInfo::SAMPLER) {
        es.throwWebCLException(WebCLException::INVALID_ARG_VALUE, WebCLException::invalidArgIndexMessage);
        return;
    }
    cl_int err = clSetKernelArg(m_clKernel, index, sizeof(cl_sampler), &clSamplerId);
    if (err != CL_SUCCESS)
        WebCLException::throwException(err, es);
    else
        argInfo->setAssociated(true);
}
void WebCLUserEvent::setStatus(cl_int executionStatus, ExceptionState& es)
{
    ASSERT(isUserEvent());
    if (isReleased()) {
        es.throwWebCLException(WebCLException::INVALID_EVENT, WebCLException::invalidEventMessage);
        return;
    }

    if (!(executionStatus < 0 || executionStatus == CL_COMPLETE)) {
        es.throwWebCLException(WebCLException::INVALID_VALUE, WebCLException::invalidValueMessage);
        return;
    }

    if (m_eventStatusSituation == StatusSet) {
        es.throwWebCLException(WebCLException::INVALID_OPERATION, WebCLException::invalidOperationMessage);
        return;
    }

    m_eventStatusSituation = StatusSet;
    m_executionStatus = executionStatus;

    cl_int err = clSetUserEventStatus(m_clEvent, executionStatus);
    if (err != CL_SUCCESS)
        WebCLException::throwException(err, es);
}
示例#6
0
__USE_CORTEX_NAMESPACE


// ---------------------------------------------------------------- //
// *** deletion
// ---------------------------------------------------------------- //

// clients must call release() rather than deleting,
// to ensure that all observers are notified of the
// object's demise.  if the object has already been
// released, return an error.

status_t ObservableLooper::release() {

    // +++++ what if I'm not running?
    // +++++ is a lock necessary?

    if(isReleased())
        return B_NOT_ALLOWED;

    // send request through proper channels
    BMessenger(this).SendMessage(B_QUIT_REQUESTED);

    return B_OK;
}
示例#7
0
ScriptValue WebCLKernel::getInfo(ScriptState* scriptState, int kernelInfo, ExceptionState& es)
{
    v8::Handle<v8::Object> creationContext = scriptState->context()->Global();
    v8::Isolate* isolate = scriptState->isolate();

    if (isReleased()) {
        es.throwWebCLException(WebCLException::INVALID_KERNEL, WebCLException::invalidKernelMessage);
        return ScriptValue(scriptState, v8::Null(isolate));
    }

    cl_int err = CL_SUCCESS;
    cl_uint uintUnits = 0;
    switch (kernelInfo) {
    case CL_KERNEL_FUNCTION_NAME:
        return ScriptValue(scriptState, v8String(isolate, m_kernelName));
    case CL_KERNEL_NUM_ARGS:
        err = clGetKernelInfo(m_clKernel, CL_KERNEL_NUM_ARGS, sizeof(cl_uint), &uintUnits, nullptr);
        if (err == CL_SUCCESS)
            return ScriptValue(scriptState, v8::Integer::NewFromUnsigned(isolate, static_cast<unsigned>(uintUnits)));
        break;
    case CL_KERNEL_PROGRAM:
        return ScriptValue(scriptState, toV8(m_program, creationContext, isolate));
        break;
    case CL_KERNEL_CONTEXT:
        return ScriptValue(scriptState, toV8(context(), creationContext, isolate));
        break;
    default:
        es.throwWebCLException(WebCLException::INVALID_VALUE, WebCLException::invalidValueMessage);
        return ScriptValue(scriptState, v8::Null(isolate));
    }
    WebCLException::throwException(err, es);
    return ScriptValue(scriptState, v8::Null(isolate));
}
PassRefPtr<WebCLProgram> WebCLContext::createProgram(const String& kernelSource, ExceptionState& es)
{
    if (isReleased()) {
        es.throwWebCLException(WebCLException::InvalidContext, WebCLException::invalidContextMessage);
        return nullptr;
    }

    if (!kernelSource.length()) {
        es.throwWebCLException(WebCLException::InvalidValue, WebCLException::invalidValueMessage);
        return nullptr;
    }

    const char* source = strdup(kernelSource.utf8().data());
    cl_int err = CL_SUCCESS;
    cl_program clProgramId = clCreateProgramWithSource(m_clContext, 1, (const char**)&source, nullptr, &err);
    if (err != CL_SUCCESS) {
        WebCLException::throwException(err, es);
        return nullptr;
    }

    RefPtr<WebCLProgram> program = WebCLProgram::create(clProgramId, this, kernelSource);
    if (!program) {
        es.throwWebCLException(WebCLException::InvalidProgram, WebCLException::invalidProgramMessage);
        return nullptr;
    }

    return program.release();
}
ScriptValue WebCLProgram::getInfo(ScriptState* scriptState, int paramName, ExceptionState& es)
{
    v8::Handle<v8::Object> creationContext = scriptState->context()->Global();
    v8::Isolate* isolate = scriptState->isolate();

    if (isReleased()) {
        es.throwWebCLException(WebCLException::INVALID_PROGRAM, WebCLException::invalidProgramMessage);
        return ScriptValue(scriptState, v8::Null(isolate));
    }

    cl_int err = CL_SUCCESS;
    cl_uint uintUnits = 0;
    Vector<RefPtr<WebCLDevice>> result = context()->getDevices();

    switch(paramName) {
    case CL_PROGRAM_NUM_DEVICES:
        err = clGetProgramInfo(m_clProgram, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &uintUnits, nullptr);
        if (err == CL_SUCCESS)
            return ScriptValue(scriptState, v8::Integer::NewFromUnsigned(isolate, static_cast<unsigned>(uintUnits)));
        WebCLException::throwException(err, es);
        return ScriptValue(scriptState, v8::Null(isolate));
    case CL_PROGRAM_SOURCE:
        return ScriptValue(scriptState, v8String(isolate, m_programSource));
    case CL_PROGRAM_CONTEXT:
        return ScriptValue(scriptState, toV8(context(), creationContext, isolate));
    case CL_PROGRAM_DEVICES:
        return ScriptValue(scriptState, toV8(result, creationContext, isolate));
    default:
        es.throwWebCLException(WebCLException::INVALID_VALUE, WebCLException::invalidValueMessage);
        return ScriptValue(scriptState, v8::Null(isolate));
    }
}
示例#10
0
PassRefPtr<WebCLKernel> WebCLProgram::createKernel(const String& kernelName, ExceptionState& es)
{
    if (isReleased()) {
        es.throwWebCLException(WebCLException::INVALID_PROGRAM, WebCLException::invalidProgramMessage);
        return nullptr;
    }

    if (!m_isProgramBuilt) {
        es.throwWebCLException(WebCLException::INVALID_PROGRAM_EXECUTABLE, WebCLException::invalidProgramExecutableMessage);
        return nullptr;
    }

    if (!m_programSource.contains(kernelName)) {
        es.throwWebCLException(WebCLException::INVALID_KERNEL_NAME, WebCLException::invalidKernelNameMessage);
        return nullptr;
    }

    cl_int err = CL_SUCCESS;
    const char* kernelNameStr = strdup(kernelName.utf8().data());
    cl_kernel clKernelId = clCreateKernel(m_clProgram, kernelNameStr, &err);

    if (err != CL_SUCCESS) {
        WebCLException::throwException(err, es);
        return nullptr;
    }
    RefPtr<WebCLKernel> kernel = WebCLKernel::create(clKernelId, context(), this, kernelName);
    return kernel;
}
示例#11
0
void WebCLKernel::setArg(unsigned index, WebCLMemoryObject* object, ExceptionState& es)
{
    if (isReleased()) {
        es.throwWebCLException(WebCLException::INVALID_KERNEL, WebCLException::invalidKernelMessage);
        return;
    }

    cl_mem clObject = nullptr;
    if (object) {
        clObject = object->getMem();
        if (!clObject) {
            es.throwWebCLException(WebCLException::INVALID_MEM_OBJECT, WebCLException::invalidMemObjectMessage);
            return;
        }
    }

    WebCLKernelArgInfo* argInfo = getArgInfo(index, es);
    if (!argInfo || (object->type() == WebCLMemoryObject::IMAGE && argInfo->type() != WebCLKernelArgInfo::IMAGE) || (object->type() == WebCLMemoryObject::BUFFER && (argInfo->addressQualifier().isEmpty() || argInfo->type() != WebCLKernelArgInfo::BUFFER))) {
        es.throwWebCLException(WebCLException::INVALID_ARG_VALUE, WebCLException::invalidArgIndexMessage);
        return;
    }

    cl_int err = clSetKernelArg(m_clKernel, index, sizeof(cl_mem), &clObject);
    if (err != CL_SUCCESS)
        WebCLException::throwException(err, es);
    else
        argInfo->setAssociated(true);
}
示例#12
0
void WebCLEvent::setCallback(unsigned commandExecCallbackType, WebCLCallback* callback, ExceptionState& es)
{
    if (isReleased()) {
        es.throwWebCLException(WebCLException::INVALID_EVENT, WebCLException::invalidEventMessage);
        return;
    }

    if (commandExecCallbackType != CL_COMPLETE) {
        es.throwWebCLException(WebCLException::INVALID_VALUE, WebCLException::invalidValueMessage);
        return;
    }

    ASSERT(callback);
    if (m_callbacks.size()) {
        m_callbacks.append(adoptRef(callback));
        return;
    }

    m_callbacks.clear();
    m_callbacks.append(adoptRef(callback));
    WebCLEventHolder* holder = new WebCLEventHolder;
    holder->event = createWeakPtr();
    holder->type = commandExecCallbackType;
    cl_int err = clSetEventCallback(m_clEvent, commandExecCallbackType, &callbackProxy, holder);

    if (err != CL_SUCCESS)
        WebCLException::throwException(err, es);
}
示例#13
0
/**
 * Returns the state of the button
 *	BUTTON_MONITOR_NOT_PRESSED 0
 *  BUTTON_MONITOR_DEPRESSED 1
 *  BUTTON_MONITOR_PRESSED 2
 *  BUTTON_MONITOR_REPEATING 3
 *  BUTTON_MONITOR_FAST_REPEATING 4
 *  BUTTON_MONITOR_RELEASED 5
 */
uint8_t NIButtonMonitor::getState(uint8_t buttonId) {
	if (isDepressed(buttonId)) return BUTTON_MONITOR_DEPRESSED;
	if (isReleased(buttonId)) return BUTTON_MONITOR_RELEASED;
	if (isFastRepeating(buttonId)) return BUTTON_MONITOR_FAST_REPEATING;
	if (isRepeating(buttonId)) return BUTTON_MONITOR_REPEATING;
	if (isPressed(buttonId)) return BUTTON_MONITOR_PRESSED;
	return BUTTON_MONITOR_NOT_PRESSED;
}
void Buttons::update(uint32_t cur_millis) {
	if (NULL == _getButtonsBitmaskFunc) return;
	uint32_t delta_millis = cur_millis - _old_millis;
	bitmask_t buttons_bitmask = _getButtonsBitmaskFunc();

	if (_buttons_bitmask)	_down_millis += delta_millis;
	else					_up_millis += delta_millis;
	
	if		(isReleased()) {
		_buttonsUp();
	}
	else if	(_buttons_state == BUTTONS_STATE_LONG_PULSE) {
		_buttons_state = BUTTONS_STATE_LONG_PRESS;
	}
	
	if		(_buttons_state == BUTTONS_STATE_UP) {
		if (buttons_bitmask) {
			_down_millis = 0;
			_pulse_count = 0;
			_buttons_bitmask = buttons_bitmask;
			_buttons_state = BUTTONS_STATE_BOUNCE;
		}
	}
	else if	(_buttons_state == BUTTONS_STATE_BOUNCE) {
		if (_buttons_bitmask != buttons_bitmask) {
			_buttonsUp();
		}
		else if (_down_millis >= _short_press_millis) {
			_buttons_state = BUTTONS_STATE_SHORT_PRESS;
			_onShortPressFunc();
		}
	}
	else if	(_buttons_state == BUTTONS_STATE_SHORT_PRESS) {
		if (0 == buttons_bitmask) {
			_buttons_state = BUTTONS_STATE_SHORT_RELEASE;
			_onShortReleaseFunc();
		}
		else if (_down_millis >= _long_press_millis) {
			setNextPulseMillis(_pulse_press_millis);
			_buttons_state = BUTTONS_STATE_LONG_PRESS;
			_onLongPressFunc();
		}
	}
	else if	(_buttons_state == BUTTONS_STATE_LONG_PRESS) {
		if (0 == buttons_bitmask) {
			_buttons_state = BUTTONS_STATE_LONG_RELEASE;
			_onLongReleaseFunc();
		}
		else if (_down_millis >= _next_pulse_millis) {
			setNextPulseMillis(_pulse_press_millis);
			_pulse_count++;
			_buttons_state = BUTTONS_STATE_LONG_PULSE;
			_onLongPulseFunc();
		}
	}
	
	_old_millis = cur_millis;
}
PassRefPtr<WebCLUserEvent> WebCLContext::createUserEvent(ExceptionState& es)
{
    if (isReleased()) {
        es.throwWebCLException(WebCLException::InvalidContext, WebCLException::invalidContextMessage);
        return nullptr;
    }

    RefPtr<WebCLUserEvent> event = WebCLUserEvent::create(this, es);
    return event.release();
}
示例#16
0
void WebCLKernel::release()
{
    if (isReleased())
        return;

    cl_int err = clReleaseKernel(m_clKernel);
    if (err != CL_SUCCESS)
        ASSERT_NOT_REACHED();

    m_clKernel = 0;
}
void WebCLContext::release()
{
    if (isReleased())
        return;

    cl_int err = clReleaseContext(m_clContext);
    if (err != CL_SUCCESS)
        ASSERT_NOT_REACHED();

    m_clContext = 0;
}
示例#18
0
void WebCLProgram::release()
{
    if (isReleased())
        return;

    cl_int err = clReleaseProgram(m_clProgram);
    if (err != CL_SUCCESS)
        ASSERT_NOT_REACHED();

    m_clProgram = 0;

    // Release the un-triggered callback.
    m_buildCallback.clear();
}
示例#19
0
void WebCLEvent::release()
{
    if (isReleased())
        return;

    cl_int err = clReleaseEvent(m_clEvent);
    if (err != CL_SUCCESS)
        ASSERT_NOT_REACHED();

    m_clEvent = 0;

    // Release un-triggered callbacks.
    m_callbacks.clear();
}
示例#20
0
ScriptValue WebCLEvent::getProfilingInfo(ScriptState* scriptState, unsigned paramName, ExceptionState& es)
{
    v8::Isolate* isolate = scriptState->isolate();

    if (isReleased()) {
        es.throwWebCLException(WebCLException::InvalidEvent, WebCLException::invalidEventMessage);
        return ScriptValue(scriptState, v8::Null(isolate));
    }

    int status = getStatus();
    unsigned properties = m_commandQueue ? m_commandQueue->getProperties() : 0;
    if (isUserEvent() || status != CL_COMPLETE || !(properties & CL_QUEUE_PROFILING_ENABLE)) {
        es.throwWebCLException(WebCLException::ProfilingInfoNotAvailable, WebCLException::profilingInfoNotAvailableMessage);
        return ScriptValue(scriptState, v8::Null(isolate));
    }

    cl_int err = CL_SUCCESS;
    cl_ulong ulongUnits = 0;
    switch (paramName) {
    case CL_PROFILING_COMMAND_QUEUED:
        err = clGetEventProfilingInfo(m_clEvent, CL_PROFILING_COMMAND_QUEUED, sizeof(cl_ulong), &ulongUnits, nullptr);
        if (err == CL_SUCCESS)
            return ScriptValue(scriptState, v8::Number::New(isolate, static_cast<double>(ulongUnits)));
        break;
    case CL_PROFILING_COMMAND_SUBMIT:
        err = clGetEventProfilingInfo(m_clEvent, CL_PROFILING_COMMAND_SUBMIT, sizeof(cl_ulong), &ulongUnits, nullptr);
        if (err == CL_SUCCESS)
            return ScriptValue(scriptState, v8::Number::New(isolate, static_cast<double>(ulongUnits)));
        break;
    case CL_PROFILING_COMMAND_START:
        err = clGetEventProfilingInfo(m_clEvent, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &ulongUnits, nullptr);
        if (err == CL_SUCCESS)
            return ScriptValue(scriptState, v8::Number::New(isolate, static_cast<double>(ulongUnits)));
        break;
    case CL_PROFILING_COMMAND_END:
        err = clGetEventProfilingInfo(m_clEvent, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &ulongUnits, nullptr);
        if (err == CL_SUCCESS)
            return ScriptValue(scriptState, v8::Number::New(isolate, static_cast<double>(ulongUnits)));
        break;
    default:
        es.throwWebCLException(WebCLException::InvalidValue, WebCLException::invalidValueMessage);
        return ScriptValue(scriptState, v8::Null(isolate));
    }

    WebCLException::throwException(err, es);
    return ScriptValue(scriptState, v8::Null(isolate));
}
void WebCLContext::releaseAll()
{
    if (isReleased())
        return;

    if (m_webCLObjects.size()) {
        for (int i = m_webCLObjects.size() - 1; i >= 0; i--) {
            WebCLObject* object = m_webCLObjects[i].get();
            if (!object)
                continue;
            object->release();
        }
        m_webCLObjects.clear();
    }

    release();
}
示例#22
0
unsigned WebCLEvent::getProfilingInfo(int paramName, ExceptionState& es)
{
    if (isReleased()) {
        es.throwWebCLException(WebCLException::INVALID_EVENT, WebCLException::invalidEventMessage);
        return 0;
    }

    int status = getStatus();
    unsigned properties = m_commandQueue ? m_commandQueue->getProperties() : 0;
    if (isUserEvent() || status != CL_COMPLETE || !(properties & CL_QUEUE_PROFILING_ENABLE)) {
        es.throwWebCLException(WebCLException::PROFILING_INFO_NOT_AVAILABLE, WebCLException::profilingInfoNotAvailableMessage);
        return 0;
    }

    cl_int err = CL_SUCCESS;
    cl_ulong ulongUnits = 0;
    switch(paramName) {
    case CL_PROFILING_COMMAND_QUEUED:
        err = clGetEventProfilingInfo(m_clEvent, CL_PROFILING_COMMAND_QUEUED, sizeof(cl_ulong), &ulongUnits, nullptr);
        if (err == CL_SUCCESS)
            return static_cast<unsigned long long>(ulongUnits);
        break;
    case CL_PROFILING_COMMAND_SUBMIT:
        err = clGetEventProfilingInfo(m_clEvent, CL_PROFILING_COMMAND_SUBMIT, sizeof(cl_ulong), &ulongUnits, nullptr);
        if (err == CL_SUCCESS)
            return static_cast<unsigned long long>(ulongUnits);
        break;
    case CL_PROFILING_COMMAND_START:
        err = clGetEventProfilingInfo(m_clEvent, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &ulongUnits, nullptr);
        if (err == CL_SUCCESS)
            return static_cast<unsigned long long>(ulongUnits);
        break;
    case CL_PROFILING_COMMAND_END:
        err = clGetEventProfilingInfo(m_clEvent, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &ulongUnits, nullptr);
        if (err == CL_SUCCESS)
            return static_cast<unsigned long long>(ulongUnits);
        break;
    default:
        es.throwWebCLException(WebCLException::INVALID_VALUE, WebCLException::invalidValueMessage);
        return 0;
    }

    WebCLException::throwException(err, es);
    return 0;
}
ScriptValue WebCLUserEvent::getInfo(ScriptState* scriptState, unsigned name, ExceptionState& es)
{
    v8::Handle<v8::Object> creationContext = scriptState->context()->Global();
    v8::Isolate* isolate = scriptState->isolate();

    if (isReleased()) {
        es.throwWebCLException(WebCLException::INVALID_EVENT, WebCLException::invalidEventMessage);
        return ScriptValue(scriptState, v8::Null(isolate));
    }

    switch (name) {
    case CL_EVENT_CONTEXT:
        return ScriptValue(scriptState, toV8(context(), creationContext, isolate));
    case CL_EVENT_COMMAND_QUEUE:
        return ScriptValue(scriptState, v8::Null(isolate));
    }

    return WebCLEvent::getInfo(scriptState, name, es);
}
Nullable<HeapVector<WebCLImageDescriptor>> WebCLContext::getSupportedImageFormats(unsigned memFlags, ExceptionState& es)
{
    HeapVector<WebCLImageDescriptor> supportedImageDescriptor;
    if (isReleased()) {
        es.throwWebCLException(WebCLException::InvalidContext, WebCLException::invalidContextMessage);
        return supportedImageDescriptor;
    }

    if (!WebCLInputChecker::isValidMemoryObjectFlag(memFlags)) {
        es.throwWebCLException(WebCLException::InvalidValue, WebCLException::invalidValueMessage);
        return supportedImageDescriptor;
    }

    cl_uint numberOfSupportedImageFormats = 0;
    cl_int err = clGetSupportedImageFormats(m_clContext, memFlags, CL_MEM_OBJECT_IMAGE2D, 0, 0, &numberOfSupportedImageFormats);

    if (err != CL_SUCCESS) {
        es.throwWebCLException(WebCLException::InvalidImageSize, WebCLException::invalidImageSizeMessage);
        return supportedImageDescriptor;
    }

    Vector<cl_image_format> supportedImages;
    supportedImages.reserveCapacity(numberOfSupportedImageFormats);
    supportedImages.resize(numberOfSupportedImageFormats);

    err = clGetSupportedImageFormats(m_clContext, memFlags, CL_MEM_OBJECT_IMAGE2D, numberOfSupportedImageFormats, supportedImages.data(), 0);
    if (err != CL_SUCCESS) {
        WebCLException::throwException(err, es);
    } else {
        for (size_t i = 0; i < static_cast<unsigned>(numberOfSupportedImageFormats); ++i) {
            if (WebCLInputChecker::isValidChannelOrder(supportedImages[i].image_channel_order) && WebCLInputChecker::isValidChannelType(supportedImages[i].image_channel_data_type)) {
                WebCLImageDescriptor des;
                des.setChannelOrder(supportedImages[i].image_channel_order);
                des.setChannelType(supportedImages[i].image_channel_data_type);
                supportedImageDescriptor.append(des);
            }
        }
    }

    return supportedImageDescriptor;
}
示例#25
0
void WebCLKernel::setArg(unsigned index, size_t argSize, const void* argValue, ExceptionState& es)
{
    if (isReleased()) {
        es.throwWebCLException(WebCLException::INVALID_KERNEL, WebCLException::invalidKernelMessage);
        return;
    }

    if (!WebCLInputChecker::isValidKernelArgIndex(this, index)) {
        es.throwWebCLException(WebCLException::INVALID_ARG_INDEX, WebCLException::invalidArgIndexMessage);
        return;
    }

    cl_int err = CL_SUCCESS;
    WebCLKernelArgInfo* argInfo = getArgInfo(index, es);
    err = clSetKernelArg(m_clKernel, index, argSize, argValue);

    if (err != CL_SUCCESS)
        WebCLException::throwException(err, es);
    else
        argInfo->setAssociated(true);
}
示例#26
0
ScriptValue WebCLEvent::getInfo(ScriptState* scriptState, unsigned paramName, ExceptionState& es)
{
    v8::Handle<v8::Object> creationContext = scriptState->context()->Global();
    v8::Isolate* isolate = scriptState->isolate();

    if (isReleased()) {
        es.throwWebCLException(WebCLException::INVALID_EVENT, WebCLException::invalidEventMessage);
        return ScriptValue(scriptState, v8::Null(isolate));
    }

    cl_int err = CL_SUCCESS;
    cl_int intUnits = 0;
    cl_command_type commandType = 0;
    switch(paramName) {
    case CL_EVENT_COMMAND_EXECUTION_STATUS:
        err = clGetEventInfo(m_clEvent, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(cl_int), &intUnits, nullptr);
        if (err == CL_SUCCESS)
            return ScriptValue(scriptState, v8::Integer::New(isolate, static_cast<int>(intUnits)));
        break;
    case CL_EVENT_COMMAND_TYPE:
        err = clGetEventInfo(m_clEvent, CL_EVENT_COMMAND_TYPE, sizeof(cl_command_type), &commandType, nullptr);
        if (err == CL_SUCCESS)
            return ScriptValue(scriptState, v8::Integer::NewFromUnsigned(isolate, static_cast<unsigned>(commandType)));
        break;
    case CL_EVENT_CONTEXT:
        ASSERT(!isUserEvent());
        return ScriptValue(scriptState, toV8(context(), creationContext, isolate));
    case CL_EVENT_COMMAND_QUEUE:
        ASSERT(m_commandQueue);
        ASSERT(!isUserEvent());
        return ScriptValue(scriptState, toV8(m_commandQueue, creationContext, isolate));
    default:
        es.throwWebCLException(WebCLException::INVALID_VALUE, WebCLException::invalidValueMessage);
        return ScriptValue(scriptState, v8::Null(isolate));
    }
    WebCLException::throwException(err, es);
    return ScriptValue(scriptState, v8::Null(isolate));
}
示例#27
0
ScriptValue WebCLKernel::getWorkGroupInfo(ScriptState* scriptState, WebCLDevice* device, int paramName, ExceptionState& es)
{
    v8::Handle<v8::Object> creationContext = scriptState->context()->Global();
    v8::Isolate* isolate = scriptState->isolate();

    if (isReleased()) {
        es.throwWebCLException(WebCLException::INVALID_KERNEL, WebCLException::invalidKernelMessage);
        return ScriptValue(scriptState, v8::Null(isolate));
    }

    cl_device_id clDevice = nullptr;
    Vector<RefPtr<WebCLDevice>> deviceList = context()->getDevices();
    if (device) {
        clDevice = device->getDeviceId();
        if (!clDevice) {
            es.throwWebCLException(WebCLException::INVALID_DEVICE, WebCLException::invalidDeviceMessage);
            return ScriptValue(scriptState, v8::Null(isolate));
        }

        size_t i;
        for (i = 0; i < deviceList.size(); i ++) {
            if (clDevice == deviceList[i]->getDeviceId())
                break;
        }

        if (i == deviceList.size()) {
            es.throwWebCLException(WebCLException::INVALID_DEVICE, WebCLException::invalidDeviceMessage);
            return ScriptValue(scriptState, v8::Null(isolate));
        }
    }

    if (!device && deviceList.size() != 1) {
        es.throwWebCLException(WebCLException::INVALID_DEVICE, WebCLException::invalidDeviceMessage);
        return ScriptValue(scriptState, v8::Null(isolate));
    }

    cl_int err = CL_SUCCESS;
    size_t sizetUnits = 0;
    size_t workGroupSize[3] = {0};
    cl_ulong ulongUnits = 0;
    switch (paramName) {
    case CL_KERNEL_WORK_GROUP_SIZE:
        err = clGetKernelWorkGroupInfo(m_clKernel, clDevice, CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &sizetUnits, nullptr);
        if (err == CL_SUCCESS)
            return ScriptValue(scriptState, v8::Integer::NewFromUnsigned(isolate, static_cast<unsigned>(sizetUnits)));
        break;
    case CL_KERNEL_PRIVATE_MEM_SIZE:
        err = clGetKernelWorkGroupInfo(m_clKernel, clDevice, CL_KERNEL_PRIVATE_MEM_SIZE, sizeof(cl_ulong), &ulongUnits, nullptr);
        if (err == CL_SUCCESS)
            return ScriptValue(scriptState, v8::Integer::NewFromUnsigned(isolate, static_cast<unsigned long long>(ulongUnits)));
        break;
    case CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE:
        err = clGetKernelWorkGroupInfo(m_clKernel, clDevice, CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE, sizeof(size_t), &sizetUnits, nullptr);
        if (err == CL_SUCCESS)
            return ScriptValue(scriptState, v8::Integer::NewFromUnsigned(isolate, static_cast<unsigned>(sizetUnits)));
        break;
    case CL_KERNEL_COMPILE_WORK_GROUP_SIZE:
        err = clGetKernelWorkGroupInfo(m_clKernel, clDevice, CL_KERNEL_COMPILE_WORK_GROUP_SIZE, sizeof(workGroupSize), &workGroupSize, nullptr);
        if (err == CL_SUCCESS) {
            Vector<unsigned> values;
            for (unsigned i = 0; i < 3; ++i)
                values.append((unsigned)workGroupSize[i]);
            return ScriptValue(scriptState, toV8(values, creationContext, isolate));
        }
        break;
    case CL_KERNEL_LOCAL_MEM_SIZE:
        err = clGetKernelWorkGroupInfo(m_clKernel, clDevice, CL_KERNEL_LOCAL_MEM_SIZE, sizeof(cl_ulong), &ulongUnits, nullptr);
        if (err == CL_SUCCESS)
            return ScriptValue(scriptState, v8::Integer::NewFromUnsigned(isolate, static_cast<unsigned long long>(ulongUnits)));
        break;
    default:
        es.throwWebCLException(WebCLException::INVALID_VALUE, WebCLException::invalidValueMessage);
        return ScriptValue(scriptState, v8::Null(isolate));
    }

    WebCLException::throwException(err, es);
    return ScriptValue(scriptState, v8::Null(isolate));
}
示例#28
0
void WebCLProgram::build(const Vector<RefPtr<WebCLDevice>>& devices, const String& buildOptions, WebCLCallback* callback, ExceptionState& es)
{
    if (isReleased()) {
        es.throwWebCLException(WebCLException::INVALID_PROGRAM, WebCLException::invalidProgramMessage);
        return;
    }

    size_t kernel_label = m_programSource.find("__kernel ", 0);
    while (kernel_label != WTF::kNotFound) {
        size_t openBrace = m_programSource.find("{", kernel_label);
        size_t openBraket = m_programSource.reverseFind("(", openBrace);
        size_t space = m_programSource.reverseFind(" ", openBraket);
        String kernelName = m_programSource.substring(space + 1, openBraket - space - 1);
        if (kernelName.length() > 254) {
            // Kernel Name length isn't allowed larger than 255.
            es.throwWebCLException(WebCLException::BUILD_PROGRAM_FAILURE, WebCLException::buildProgramFailureMessage);
            return;
        }

        size_t closeBraket = m_programSource.find(")", openBraket);
        String arguments = m_programSource.substring(openBraket + 1, closeBraket - openBraket - 1);
        if (arguments.contains("struct ") || arguments.contains("image1d_array_t ") || arguments.contains("image1d_buffer_t ") || arguments.contains("image1d_t ") || arguments.contains("image2d_array_t ")) {
            // 1. Kernel structure parameters aren't allowed;
            // 2. Kernel argument "image1d_t", "image1d_array_t", "image2d_array_t" and "image1d_buffer_t" aren't allowed;
            es.throwWebCLException(WebCLException::BUILD_PROGRAM_FAILURE, WebCLException::buildProgramFailureMessage);
            return;
        }

        size_t closeBrace = m_programSource.find("}", openBrace);
        String codeString =  m_programSource.substring(openBrace + 1, closeBrace - openBrace - 1).removeCharacters(isASCIILineBreakOrWhiteSpaceCharacter);
        if (codeString.isEmpty()) {
            // Kernel code isn't empty;
            es.throwWebCLException(WebCLException::BUILD_PROGRAM_FAILURE, WebCLException::buildProgramFailureMessage);
            return;
        }

        kernel_label = m_programSource.find("__kernel ", closeBrace);
    }

    if (buildOptions.length() > 0) {
        static AtomicString& buildOptionDashD = *new AtomicString("-D", AtomicString::ConstructFromLiteral);
        static HashSet<AtomicString>& webCLSupportedBuildOptions = *new HashSet<AtomicString>();
        if (webCLSupportedBuildOptions.isEmpty()) {
            webCLSupportedBuildOptions.add(AtomicString("-cl-opt-disable", AtomicString::ConstructFromLiteral));
            webCLSupportedBuildOptions.add(AtomicString("-cl-single-precision-constant", AtomicString::ConstructFromLiteral));
            webCLSupportedBuildOptions.add(AtomicString("-cl-denorms-are-zero", AtomicString::ConstructFromLiteral));
            webCLSupportedBuildOptions.add(AtomicString("-cl-mad-enable", AtomicString::ConstructFromLiteral));
            webCLSupportedBuildOptions.add(AtomicString("-cl-no-signed-zeros", AtomicString::ConstructFromLiteral));
            webCLSupportedBuildOptions.add(AtomicString("-cl-unsafe-math-optimizations", AtomicString::ConstructFromLiteral));
            webCLSupportedBuildOptions.add(AtomicString("-cl-finite-math-only", AtomicString::ConstructFromLiteral));
            webCLSupportedBuildOptions.add(AtomicString("-cl-fast-relaxed-math", AtomicString::ConstructFromLiteral));
            webCLSupportedBuildOptions.add(AtomicString("-w", AtomicString::ConstructFromLiteral));
            webCLSupportedBuildOptions.add(AtomicString("-Werror", AtomicString::ConstructFromLiteral));
        }

        Vector<String> webCLBuildOptionsVector;
        buildOptions.split(" ", false, webCLBuildOptionsVector);

        for (size_t i = 0; i < webCLBuildOptionsVector.size(); i++) {
            // Every build option must start with a hyphen.
            if (!webCLBuildOptionsVector[i].startsWith("-")) {
                es.throwWebCLException(WebCLException::INVALID_BUILD_OPTIONS, WebCLException::invalidBuildOptionsMessage);
                return;
            }

            if (webCLSupportedBuildOptions.contains(AtomicString(webCLBuildOptionsVector[i])))
                continue;

            if (webCLBuildOptionsVector[i].startsWith(buildOptionDashD)) {
                size_t j;
                for (j = i + 1; j < webCLBuildOptionsVector.size() && !webCLBuildOptionsVector[j].startsWith("-"); ++j) {}
                if (webCLBuildOptionsVector[i].stripWhiteSpace() == buildOptionDashD && j == i + 1) {
                    es.throwWebCLException(WebCLException::INVALID_BUILD_OPTIONS, WebCLException::invalidBuildOptionsMessage);
                    return;
                }

                i = --j;
                continue;
            }

            es.throwWebCLException(WebCLException::INVALID_BUILD_OPTIONS, WebCLException::invalidBuildOptionsMessage);
            return;
        }
    }

    pfnNotify callbackProxyPtr = nullptr;
    WebCLProgramHolder* holder = nullptr;
    if (callback) {
        if (m_buildCallback) {
            es.throwWebCLException(WebCLException::INVALID_OPERATION, WebCLException::invalidOperationMessage);
            return;
        }

        // Store the callback, eventList to HashTable and call callbackProxy.
        m_buildCallback = adoptRef(callback);
        callbackProxyPtr = &callbackProxy;
        holder = new WebCLProgramHolder;
        holder->program = createWeakPtr();
    }

    cl_int err = CL_SUCCESS;
    Vector<cl_device_id> clDevices;
    Vector<RefPtr<WebCLDevice>> contextDevices = context()->getDevices();
    if (devices.size()) {
        Vector<cl_device_id> inputDevices;
        for (auto device : devices)
            inputDevices.append(device->getDeviceId());

        size_t contextDevicesLength = contextDevices.size();
        for (size_t z, i = 0; i < inputDevices.size(); i++) {
            // Check if the inputDevices[i] is part of programs WebCLContext.
            for (z = 0; z < contextDevicesLength; z++) {
                if (contextDevices[z]->getDeviceId() == inputDevices[i]) {
                    break;
                }
            }

            if (z == contextDevicesLength) {
                es.throwWebCLException(WebCLException::INVALID_DEVICE, WebCLException::invalidDeviceMessage);
                return;
            }

            clDevices.append(inputDevices[i]);
        }
    } else {
        for (auto contextDevice : contextDevices)
            clDevices.append(contextDevice->getDeviceId());
    }

    if (!clDevices.size()) {
        es.throwWebCLException(WebCLException::INVALID_VALUE, WebCLException::invalidValueMessage);
        return;
    }

    m_isProgramBuilt = true;
    err = clBuildProgram(m_clProgram, clDevices.size(), clDevices.data(), buildOptions.utf8().data(), callbackProxyPtr, holder);

    if (err != CL_SUCCESS)
        es.throwWebCLException(WebCLException::BUILD_PROGRAM_FAILURE, WebCLException::buildProgramFailureMessage);
}
示例#29
0
Vector<RefPtr<WebCLKernel>> WebCLProgram::createKernelsInProgram(ExceptionState& es)
{
    if (isReleased()) {
        es.throwWebCLException(WebCLException::INVALID_PROGRAM, WebCLException::invalidProgramMessage);
        return Vector<RefPtr<WebCLKernel>>();
    }

    if (!m_isProgramBuilt) {
        es.throwWebCLException(WebCLException::INVALID_PROGRAM_EXECUTABLE, WebCLException::invalidProgramExecutableMessage);
        return Vector<RefPtr<WebCLKernel>>();
    }

    cl_uint num = 0;
    cl_int err = clCreateKernelsInProgram(m_clProgram, 0, nullptr, &num);
    if (err != CL_SUCCESS) {
        WebCLException::throwException(err, es);
        return Vector<RefPtr<WebCLKernel>>();
    }

    if (num == 0) {
        es.throwWebCLException(WebCLException::FAILURE, WebCLException::failureMessage);
        return Vector<RefPtr<WebCLKernel>>();
    }

    cl_kernel* kernelBuf = (cl_kernel*)malloc (sizeof(cl_kernel) * num);
    if (!kernelBuf) {
        return Vector<RefPtr<WebCLKernel>>();
    }

    err = clCreateKernelsInProgram(m_clProgram, num, kernelBuf, nullptr);

    if (err != CL_SUCCESS) {
        WebCLException::throwException(err, es);
        return Vector<RefPtr<WebCLKernel>>();
    }

    Vector<char> kernelName;
    size_t bytesOfKernelName = 0;
    Vector<RefPtr<WebCLKernel>> m_kernelList;
    for (size_t i = 0 ; i < num; i++) {
        err = clGetKernelInfo(kernelBuf[i], CL_KERNEL_FUNCTION_NAME, 0, nullptr, &bytesOfKernelName);
        if (err != CL_SUCCESS) {
            continue;
        }

        kernelName.reserveCapacity(bytesOfKernelName);
        kernelName.resize(bytesOfKernelName);

        err = clGetKernelInfo(kernelBuf[i], CL_KERNEL_FUNCTION_NAME, bytesOfKernelName, kernelName.data(), 0);

        if (err != CL_SUCCESS) {
            continue;
        }

        RefPtr<WebCLKernel> kernel = WebCLKernel::create(kernelBuf[i], context(), this, static_cast<const char*>(kernelName.data()));

        if (kernel)
            m_kernelList.append(kernel);
        kernelName.clear();
        bytesOfKernelName = 0;
    }

    return m_kernelList;
}
示例#30
0
ScriptValue WebCLProgram::getBuildInfo(ScriptState* scriptState, WebCLDevice* device, int paramName, ExceptionState& es)
{
    v8::Isolate* isolate = scriptState->isolate();

    if (isReleased()) {
        es.throwWebCLException(WebCLException::INVALID_PROGRAM, WebCLException::invalidProgramMessage);
        return ScriptValue(scriptState, v8::Null(isolate));
    }

    cl_device_id clDevice = nullptr;
    if (device) {
        clDevice = device->getDeviceId();
        if (!clDevice) {
            es.throwWebCLException(WebCLException::INVALID_DEVICE, WebCLException::invalidDeviceMessage);
            return ScriptValue(scriptState, v8::Null(isolate));
        }
        size_t i = 0;
        Vector<RefPtr<WebCLDevice>> deviceList = context()->getDevices();
        for (; i < deviceList.size(); i ++) {
            if (clDevice == deviceList[i]->getDeviceId())
                break;
        }
        if (i == deviceList.size()) {
            es.throwWebCLException(WebCLException::INVALID_DEVICE, WebCLException::invalidDeviceMessage);
            return ScriptValue(scriptState, v8::Null(isolate));
        }
    }

    cl_int err = CL_SUCCESS;
    char *buffer;
    size_t len = 0;
    switch (paramName) {
    case CL_PROGRAM_BUILD_LOG: {
        err = clGetProgramBuildInfo(m_clProgram, clDevice, CL_PROGRAM_BUILD_LOG, 0, nullptr, &len);
        if (err != CL_SUCCESS)
            break;
        buffer = new char[len + 1];
        err = clGetProgramBuildInfo(m_clProgram, clDevice, CL_PROGRAM_BUILD_LOG, len, buffer, nullptr);
        if (err != CL_SUCCESS)
            break;
        String result(buffer);
        delete [] buffer;
        return ScriptValue(scriptState, v8String(isolate, result));
    }
    case CL_PROGRAM_BUILD_OPTIONS: {
        err = clGetProgramBuildInfo(m_clProgram, clDevice, CL_PROGRAM_BUILD_OPTIONS, 0, nullptr, &len);
        if (err != CL_SUCCESS)
            break;
        buffer = new char[len + 1];
        err = clGetProgramBuildInfo(m_clProgram, clDevice, CL_PROGRAM_BUILD_OPTIONS, len, buffer, nullptr);
        if (err != CL_SUCCESS)
            break;
        String result(buffer);
        delete [] buffer;
        return ScriptValue(scriptState, v8String(isolate, result));
    }
    case CL_PROGRAM_BUILD_STATUS:
        cl_build_status buildStatus;
        err = clGetProgramBuildInfo(m_clProgram, clDevice, CL_PROGRAM_BUILD_STATUS, sizeof(cl_build_status), &buildStatus, nullptr);
        if (err == CL_SUCCESS)
            return ScriptValue(scriptState, v8::Integer::NewFromUnsigned(isolate, static_cast<unsigned>(buildStatus)));
        break;
    default:
        es.throwWebCLException(WebCLException::INVALID_VALUE, WebCLException::invalidValueMessage);
        return ScriptValue(scriptState, v8::Null(isolate));
    }

    WebCLException::throwException(err, es);
    return ScriptValue(scriptState, v8::Null(isolate));
}