size_t BufferedFile::get_bytes(address buffer, jint num, bool is_buffered) { jint total_read = 0; Buffer::Raw fb = data_buffer(); if (!is_buffered) { OsFile_seek(file_pointer(), (long)(-(count() - index())), SEEK_CUR); set_count(0); // flush buffer set_index(0); return OsFile_read(file_pointer(), buffer, 1, num); } else { while (!at_eof() && num > 0) { int num_to_read = (num > (count() - index())) ? (count() - index()) :num; // num_to_read may be zero if (num_to_read) { jvm_memcpy(buffer, fb().base_address() + (index() * sizeof (jbyte)), num_to_read); buffer += num_to_read; num -= num_to_read; set_index(index() + num_to_read); total_read += num_to_read; } if (num > 0) { refill_buffer(); } } } return total_read; }
void BufferedFile::refill_buffer() { if (at_eof()) { return; } int bytes_to_read = buffer_size(); int max = file_size() - file_pos(); if (bytes_to_read > max) { bytes_to_read = max; } int count_read; { // The file_pointer() may be shared with a FileDecoder // object. This may cause BufferedFile::file_pos() to become out // of date. A call to OsFile_seek() makes everything consistent again. AllocationDisabler raw_pointers_used_in_this_block; Buffer::Raw fb = data_buffer(); OsFile_seek(file_pointer(), file_pos(), SEEK_SET); count_read = OsFile_read(file_pointer(), fb().base_address(), 1, bytes_to_read); } set_count(count_read); set_file_pos(long(file_pos() + count_read)); set_index(0); if (count_read <= 0) { set_at_eof(true); } return; }
GLFWwindow *setup_program(t_properties *properties) { static const float verts[] = {-1, 1, -1, -1, 1, -1, 1, 1}; static const GLuint inds[] = {0, 1, 3, 1, 2, 3}; GLFWwindow *window; properties->width = 1000; properties->height = 1000; window = make_glfw(properties->width, properties->height); glClearColor(0, 0, 0, 1); properties->model = vao(); properties->verts = data_buffer((GLvoid *)verts, sizeof(verts)); properties->indices = index_buffer((GLvoid *)inds, sizeof(inds)); vao_add_indices(properties->model, properties->indices); vao_add_vdata(properties->model, properties->verts, 2, GL_FALSE); ft_putnbr(glGetError()); ft_putendl(" -0"); properties->shaders[0] = load_vertex(); properties->shaders[1] = load_fragment(properties->map); properties->program = shader_program(properties->shaders, 2); ft_putnbr(glGetError()); ft_putendl(" -1"); glUseProgram(properties->program); glBindVertexArray(properties->model); glEnableVertexAttribArray(0); init_uniforms(properties->program); return (window); }
/*---------------------------------------------------------------------- | PLT_InputDatagramStream::Read +---------------------------------------------------------------------*/ NPT_Result PLT_InputDatagramStream::Read(void* buffer, NPT_Size bytes_to_read, NPT_Size* bytes_read /*= 0*/) { if (bytes_read) *bytes_read = 0; if (bytes_to_read == 0) { return NPT_SUCCESS; } NPT_DataBuffer data_buffer(buffer, bytes_to_read, false); // read data into it now NPT_SocketAddress addr; NPT_Result res = m_Socket->Receive(data_buffer, &addr); // update info m_Socket->GetInfo(m_Info); m_Info.remote_address = addr; if (bytes_read) *bytes_read = data_buffer.GetDataSize(); return res; }
data_buffer get_data_buffer(std::size_t size, std::string const& name) { data_buffer buffer; if (cache_.get(size, buffer)) return buffer; return data_buffer(name.c_str(), size); }
static ioremap::elliptics::data_pointer convert(const type &ob) { ioremap::elliptics::data_buffer data_buffer(sizeof(type)); type t; t.tv_sec = dnet_bswap64(ob.tv_sec); t.tv_nsec = dnet_bswap64(ob.tv_nsec); data_buffer.write(t.tv_sec); data_buffer.write(t.tv_nsec); return std::move(data_buffer); }
void SpotifyDownloadDialog::slotDownloadFinished() { if( m_downloadReply->error() != QNetworkReply::NoError ) { m_ui->messageWidget->setText(i18n("Downloading is interrupted due to %1") .arg(m_downloadReply->errorString())); m_ui->messageWidget->animatedShow(); return; } QByteArray data( m_downloadReply->readAll() ); QScopedPointer<QBuffer> data_buffer(new QBuffer(&data)); KZip archive( data_buffer.data() ); if( !archive.open( QIODevice::ReadOnly ) || !archive.directory() ) { m_ui->messageWidget->setText(i18n("Failed to read data from the downloaded file. " "Please try again later.")); m_ui->messageWidget->animatedShow(); return; } archive.directory()->copyTo( Collections::SpotifyCollection::resolverDownloadPath() ); QFile file( Collections::SpotifyCollection::resolverPath() ); if( !file.exists() ) { //TODO: display error to the user m_ui->messageWidget->setText(i18n( "Failed to extract the Spotify resolver to %1 " "Please check if the path is writeable." ) .arg( Collections::SpotifyCollection::resolverDownloadPath() )); m_ui->messageWidget->animatedShow(); return; } file.setPermissions( file.permissions() | QFile::ExeUser ); // Notify controller to load the resolver Spotify::Controller* controller = The::SpotifyController(); controller->setFilePath( Collections::SpotifyCollection::resolverPath() ); controller->reload(); accept(); }
/** * save jpeg images * @param name - file name to which data should be saved * @param data - smart array containing data to be stored (must be 32Bit per Pixel) * @param width,height - size of the image data * @param quality - quality of the jpeg image (default is 80) * @return 0 if no error occurs */ int nrCTextureLoader::save_jpeg(const string& name,const shared_array<byte>& data,int width,int height,int quality) { struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; char nname[1024]; sprintf(nname, "%s%s", nrVFS.getPathToFileSystem(), name.c_str()); FILE *file = fopen(nname,"wb"); if(!file) { nrLog.Log(NR_LOG_ENGINE, "nrCTextureLoader::load_jpeg(): error create '%s' file",name.c_str()); return 0; } shared_array<byte> data_buffer(new byte[width * height * 3]); for(int i = 0, j = 0; i < width * height * 4; i += 4, j += 3) { data_buffer[j + 0] = data[i + 0]; data_buffer[j + 1] = data[i + 1]; data_buffer[j + 2] = data[i + 2]; } cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); jpeg_stdio_dest(&cinfo,file); cinfo.image_width = width; cinfo.image_height = height; cinfo.input_components = 3; cinfo.in_color_space = JCS_RGB; jpeg_set_defaults(&cinfo); jpeg_set_quality(&cinfo,quality,TRUE); jpeg_start_compress(&cinfo,TRUE); int row_stride = width * 3; while(cinfo.next_scanline < cinfo.image_height) { JSAMPROW row_pointer; row_pointer = &data_buffer[cinfo.next_scanline * row_stride]; jpeg_write_scanlines(&cinfo,&row_pointer,1); } jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); //delete [] data_buffer; data_buffer.reset(); fclose(file); return 1; }
/* ************************************************************************* * * ****** st_SRotation: * ************************************************************************* */ TEST( C99_CommonBeamElementDriftTests, MinimalAddToBufferCopyRemapRead ) { using size_t = ::st_buffer_size_t; using object_t = ::st_Object; using raw_t = unsigned char; using belem_t = ::st_SRotation; using real_t = SIXTRL_REAL_T; static real_t const ZERO = real_t{ 0.0 }; static real_t const EPS = real_t{ 1e-13 }; /* --------------------------------------------------------------------- */ std::mt19937_64::result_type const seed = 20180830u; std::mt19937_64 prng; prng.seed( seed ); using angle_dist_t = std::uniform_real_distribution< real_t >; angle_dist_t angle_dist( real_t{ -1.57079632679 }, real_t{ +1.57079632679 } ); static SIXTRL_CONSTEXPR_OR_CONST size_t NUM_BEAM_ELEMENTS = size_t{ 1000 }; ::st_object_type_id_t const BEAM_ELEMENT_TYPE_ID = ::st_OBJECT_TYPE_SROTATION; std::vector< belem_t > orig_beam_elements( NUM_BEAM_ELEMENTS, belem_t{} ); size_t const slot_size = ::st_BUFFER_DEFAULT_SLOT_SIZE; size_t const num_objs = NUM_BEAM_ELEMENTS; size_t const num_garbage = size_t{ 0 }; size_t const num_dataptrs = size_t{ 0 }; size_t num_slots = size_t{ 0 }; for( size_t ii = size_t{ 0 } ; ii < NUM_BEAM_ELEMENTS ; ++ii ) { real_t const angle = angle_dist( prng ); belem_t* ptr_srot = ::st_SRotation_preset( &orig_beam_elements[ ii ] ); ASSERT_TRUE( ptr_srot != nullptr ); ::st_SRotation_set_angle( ptr_srot, angle ); ASSERT_TRUE( EPS > std::fabs( std::cos( angle ) - ::st_SRotation_get_cos_angle( ptr_srot ) ) ); ASSERT_TRUE( EPS > std::fabs( std::sin( angle ) - ::st_SRotation_get_sin_angle( ptr_srot ) ) ); real_t const cmp_angle = ::st_SRotation_get_angle( ptr_srot ); real_t const delta = std::fabs( angle - cmp_angle ); if( EPS <= std::fabs( delta ) ) { std::cout << "here" << std::endl; } ASSERT_TRUE( EPS > std::fabs( angle - ::st_SRotation_get_angle( ptr_srot ) ) ); num_slots += ::st_ManagedBuffer_predict_required_num_slots( nullptr, sizeof( ::st_SRotation ), ::st_SRotation_get_num_dataptrs( ptr_srot ), nullptr, nullptr, slot_size ); } /* --------------------------------------------------------------------- */ size_t const requ_buffer_size = ::st_ManagedBuffer_calculate_buffer_length( nullptr, num_objs, num_slots, num_dataptrs, num_garbage, slot_size ); ::st_Buffer* eb = ::st_Buffer_new( requ_buffer_size ); ASSERT_TRUE( eb != nullptr ); /* --------------------------------------------------------------------- */ size_t be_index = size_t{ 0 }; belem_t* ptr_orig = &orig_beam_elements[ be_index++ ]; ASSERT_TRUE( ptr_orig != nullptr ); object_t* ptr_object = ::st_Buffer_add_object( eb, ptr_orig, sizeof( belem_t ), BEAM_ELEMENT_TYPE_ID, ::st_SRotation_get_num_dataptrs( ptr_orig ), nullptr, nullptr, nullptr ); ASSERT_TRUE( ptr_object != nullptr ); ASSERT_TRUE( ::st_Buffer_get_num_of_objects( eb ) == be_index ); ASSERT_TRUE( ::st_Object_get_const_begin_ptr( ptr_object ) != nullptr ); ASSERT_TRUE( ::st_Object_get_size( ptr_object ) >= sizeof( belem_t ) ); ASSERT_TRUE( ::st_Object_get_type_id( ptr_object ) == BEAM_ELEMENT_TYPE_ID ); belem_t* ptr_srot = reinterpret_cast< belem_t* >( ::st_Object_get_begin_ptr( ptr_object ) ); ASSERT_TRUE( ptr_srot != nullptr ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_angle( ptr_srot ) - ::st_SRotation_get_angle( ptr_orig ) ) ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_cos_angle( ptr_srot ) - ::st_SRotation_get_cos_angle( ptr_orig ) ) ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_sin_angle( ptr_srot ) - ::st_SRotation_get_sin_angle( ptr_orig ) ) ); /* --------------------------------------------------------------------- */ ptr_orig = &orig_beam_elements[ be_index++ ]; ptr_srot = ::st_SRotation_new( eb ); ASSERT_TRUE( ptr_srot != nullptr ); ASSERT_TRUE( ::st_Buffer_get_num_of_objects( eb ) == be_index ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_angle( ptr_srot ) - ZERO ) ); ::st_SRotation_set_angle( ptr_srot, ::st_SRotation_get_angle( ptr_orig ) ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_angle( ptr_srot ) - ::st_SRotation_get_angle( ptr_orig ) ) ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_cos_angle( ptr_srot ) - ::st_SRotation_get_cos_angle( ptr_orig ) ) ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_sin_angle( ptr_srot ) - ::st_SRotation_get_sin_angle( ptr_orig ) ) ); /* --------------------------------------------------------------------- */ ptr_orig = &orig_beam_elements[ be_index++ ]; ptr_srot = ::st_SRotation_add( eb, ::st_SRotation_get_angle( ptr_orig ) ); ASSERT_TRUE( ptr_srot != nullptr ); ASSERT_TRUE( ::st_Buffer_get_num_of_objects( eb ) == be_index ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_angle( ptr_srot ) - ::st_SRotation_get_angle( ptr_orig ) ) ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_cos_angle( ptr_srot ) - ::st_SRotation_get_cos_angle( ptr_orig ) ) ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_sin_angle( ptr_srot ) - ::st_SRotation_get_sin_angle( ptr_orig ) ) ); /* --------------------------------------------------------------------- */ ptr_orig = &orig_beam_elements[ be_index++ ]; ptr_srot = ::st_SRotation_add_detailed( eb, std::cos( ::st_SRotation_get_angle( ptr_orig ) ), std::sin( ::st_SRotation_get_angle( ptr_orig ) ) ); ASSERT_TRUE( ptr_srot != nullptr ); ASSERT_TRUE( ::st_Buffer_get_num_of_objects( eb ) == be_index ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_angle( ptr_srot ) - ::st_SRotation_get_angle( ptr_orig ) ) ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_cos_angle( ptr_srot ) - ::st_SRotation_get_cos_angle( ptr_orig ) ) ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_sin_angle( ptr_srot ) - ::st_SRotation_get_sin_angle( ptr_orig ) ) ); for( ; be_index < NUM_BEAM_ELEMENTS ; ) { ptr_orig = &orig_beam_elements[ be_index++ ]; ptr_srot = ::st_SRotation_add( eb, ::st_SRotation_get_angle( ptr_orig ) ); ASSERT_TRUE( ptr_srot != nullptr ); ASSERT_TRUE( ::st_Buffer_get_num_of_objects( eb ) == be_index ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_angle( ptr_srot ) - ::st_SRotation_get_angle( ptr_orig ) ) ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_cos_angle( ptr_srot ) - ::st_SRotation_get_cos_angle( ptr_orig ) ) ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_sin_angle( ptr_srot ) - ::st_SRotation_get_sin_angle( ptr_orig ) ) ); } /* --------------------------------------------------------------------- */ ASSERT_TRUE( ::st_Buffer_get_size( eb ) > size_t{ 0 } ); std::vector< raw_t > data_buffer( ::st_Buffer_get_size( eb ), raw_t{ 0 } ); data_buffer.assign( ::st_Buffer_get_const_data_begin( eb ), ::st_Buffer_get_const_data_end( eb ) ); ::st_Buffer cmp_buffer; ::st_Buffer_preset( &cmp_buffer ); int success = ::st_Buffer_init( &cmp_buffer, data_buffer.data(), data_buffer.size() ); ASSERT_TRUE( success == 0 ); ASSERT_TRUE( ::st_Buffer_get_num_of_objects( eb ) == ::st_Buffer_get_num_of_objects( &cmp_buffer ) ); object_t const* obj_it = ::st_Buffer_get_const_objects_begin( eb ); object_t const* obj_end = ::st_Buffer_get_const_objects_end( eb ); object_t const* cmp_it = ::st_Buffer_get_const_objects_begin( &cmp_buffer ); be_index = size_t{ 0 }; for( ; obj_it != obj_end ; ++obj_it, ++cmp_it ) { ptr_orig = &orig_beam_elements[ be_index++ ]; ASSERT_TRUE( ::st_Object_get_type_id( obj_it ) == BEAM_ELEMENT_TYPE_ID ); ASSERT_TRUE( ::st_Object_get_type_id( obj_it ) == ::st_Object_get_type_id( cmp_it ) ); ASSERT_TRUE( ::st_Object_get_size( obj_it ) >= sizeof( belem_t ) ); ASSERT_TRUE( ::st_Object_get_size( obj_it ) == ::st_Object_get_size( cmp_it ) ); belem_t const* elem = reinterpret_cast< belem_t const* >( ::st_Object_get_const_begin_ptr( obj_it ) ); belem_t const* cmp_elem = reinterpret_cast< belem_t const* >( ::st_Object_get_const_begin_ptr( cmp_it ) ); ASSERT_TRUE( ptr_orig != elem ); ASSERT_TRUE( ptr_orig != cmp_elem ); ASSERT_TRUE( elem != nullptr ); ASSERT_TRUE( cmp_elem != nullptr ); ASSERT_TRUE( cmp_elem != elem ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_angle( elem ) - ::st_SRotation_get_angle( ptr_orig ) ) ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_cos_angle( elem ) - ::st_SRotation_get_cos_angle( ptr_orig ) ) ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_sin_angle( elem ) - ::st_SRotation_get_sin_angle( ptr_orig ) ) ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_angle( cmp_elem ) - ::st_SRotation_get_angle( ptr_orig ) ) ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_cos_angle( cmp_elem ) - ::st_SRotation_get_cos_angle( ptr_orig ) ) ); ASSERT_TRUE( EPS > std::fabs( ::st_SRotation_get_sin_angle( cmp_elem ) - ::st_SRotation_get_sin_angle( ptr_orig ) ) ); } /* --------------------------------------------------------------------- */ ::st_Buffer_delete( eb ); ::st_Buffer_free( &cmp_buffer ); }
/** * Performs a Y2R colorspace conversion. * * The Y2R hardware implements hardware-accelerated YUV to RGB colorspace conversions. It is most * commonly used for video playback or to display camera input to the screen. * * The conversion process is quite configurable, and can be divided in distinct steps. From * observation, it appears that the hardware buffers a single 8-pixel tall strip of image data * internally and converts it in one go before writing to the output and loading the next strip. * * The steps taken to convert one strip of image data are: * * - The hardware receives data via CDMA (http://3dbrew.org/wiki/Corelink_DMA_Engines), which is * presumably stored in one or more internal buffers. This process can be done in several separate * transfers, as long as they don't exceed the size of the internal image buffer. This allows * flexibility in input strides. * - The input data is decoded into a YUV tuple. Several formats are suported, see the `InputFormat` * enum. * - The YUV tuple is converted, using fixed point calculations, to RGB. This step can be configured * using a set of coefficients to support different colorspace standards. See `CoefficientSet`. * - The strip can be optionally rotated 90, 180 or 270 degrees. Since each strip is processed * independently, this notably rotates each *strip*, not the entire image. This means that for 90 * or 270 degree rotations, the output will be in terms of several 8 x height images, and for any * non-zero rotation the strips will have to be re-arranged so that the parts of the image will * not be shuffled together. This limitation makes this a feature of somewhat dubious utility. 90 * or 270 degree rotations in images with non-even height don't seem to work properly. * - The data is converted to the output RGB format. See the `OutputFormat` enum. * - The data can be output either linearly line-by-line or in the swizzled 8x8 tile format used by * the PICA. This is decided by the `BlockAlignment` enum. If 8x8 alignment is used, then the * image must have a height divisible by 8. The image width must always be divisible by 8. * - The final data is then CDMAed out to main memory and the next image strip is processed. This * offers the same flexibility as the input stage. * * In this implementation, to avoid the combinatorial explosion of parameter combinations, common * intermediate formats are used and where possible tables or parameters are used instead of * diverging code paths to keep the amount of branches in check. Some steps are also merged to * increase efficiency. * * Output for all valid settings combinations matches hardware, however output in some edge-cases * differs: * * - `Block8x8` alignment with non-mod8 height produces different garbage patterns on the last * strip, especially when combined with rotation. * - Hardware, when using `Linear` alignment with a non-even height and 90 or 270 degree rotation * produces misaligned output on the last strip. This implmentation produces output with the * correct "expected" alignment. * * Hardware behaves strangely (doesn't fire the completion interrupt, for example) in these cases, * so they are believed to be invalid configurations anyway. */ void PerformConversion(ConversionConfiguration& cvt) { ASSERT(cvt.input_line_width % 8 == 0); ASSERT(cvt.block_alignment != BlockAlignment::Block8x8 || cvt.input_lines % 8 == 0); // Tiles per row size_t num_tiles = cvt.input_line_width / 8; ASSERT(num_tiles <= MAX_TILES); // Buffer used as a CDMA source/target. std::unique_ptr<u8[]> data_buffer(new u8[cvt.input_line_width * 8 * 4]); // Intermediate storage for decoded 8x8 image tiles. Always stored as RGB32. std::unique_ptr<ImageTile[]> tiles(new ImageTile[num_tiles]); ImageTile tmp_tile; // LUT used to remap writes to a tile. Used to allow linear or swizzled output without // requiring two different code paths. const u8* tile_remap = nullptr; switch (cvt.block_alignment) { case BlockAlignment::Linear: tile_remap = linear_lut; break; case BlockAlignment::Block8x8: tile_remap = morton_lut; break; } for (unsigned int y = 0; y < cvt.input_lines; y += 8) { unsigned int row_height = std::min(cvt.input_lines - y, 8u); // Total size in pixels of incoming data required for this strip. const size_t row_data_size = row_height * cvt.input_line_width; u8* input_Y = data_buffer.get(); u8* input_U = input_Y + 8 * cvt.input_line_width; u8* input_V = input_U + 8 * cvt.input_line_width / 2; switch (cvt.input_format) { case InputFormat::YUV422_Indiv8: ReceiveData<1>(input_Y, cvt.src_Y, row_data_size); ReceiveData<1>(input_U, cvt.src_U, row_data_size / 2); ReceiveData<1>(input_V, cvt.src_V, row_data_size / 2); break; case InputFormat::YUV420_Indiv8: ReceiveData<1>(input_Y, cvt.src_Y, row_data_size); ReceiveData<1>(input_U, cvt.src_U, row_data_size / 4); ReceiveData<1>(input_V, cvt.src_V, row_data_size / 4); break; case InputFormat::YUV422_Indiv16: ReceiveData<2>(input_Y, cvt.src_Y, row_data_size); ReceiveData<2>(input_U, cvt.src_U, row_data_size / 2); ReceiveData<2>(input_V, cvt.src_V, row_data_size / 2); break; case InputFormat::YUV420_Indiv16: ReceiveData<2>(input_Y, cvt.src_Y, row_data_size); ReceiveData<2>(input_U, cvt.src_U, row_data_size / 4); ReceiveData<2>(input_V, cvt.src_V, row_data_size / 4); break; case InputFormat::YUYV422_Interleaved: input_U = nullptr; input_V = nullptr; ReceiveData<1>(input_Y, cvt.src_YUYV, row_data_size * 2); break; } // Note(yuriks): If additional optimization is required, input_format can be moved to a // template parameter, so that its dispatch can be moved to outside the inner loop. ConvertYUVToRGB(cvt.input_format, input_Y, input_U, input_V, tiles.get(), cvt.input_line_width, row_height, cvt.coefficients); u32* output_buffer = reinterpret_cast<u32*>(data_buffer.get()); for (size_t i = 0; i < num_tiles; ++i) { int image_strip_width = 0; int output_stride = 0; switch (cvt.rotation) { case Rotation::None: RotateTile0(tiles[i], tmp_tile, row_height, tile_remap); image_strip_width = cvt.input_line_width; output_stride = 8; break; case Rotation::Clockwise_90: RotateTile90(tiles[i], tmp_tile, row_height, tile_remap); image_strip_width = 8; output_stride = 8 * row_height; break; case Rotation::Clockwise_180: // For 180 and 270 degree rotations we also invert the order of tiles in the strip, // since the rotates are done individually on each tile. RotateTile180(tiles[num_tiles - i - 1], tmp_tile, row_height, tile_remap); image_strip_width = cvt.input_line_width; output_stride = 8; break; case Rotation::Clockwise_270: RotateTile270(tiles[num_tiles - i - 1], tmp_tile, row_height, tile_remap); image_strip_width = 8; output_stride = 8 * row_height; break; } switch (cvt.block_alignment) { case BlockAlignment::Linear: WriteTileToOutput(output_buffer, tmp_tile, row_height, image_strip_width); output_buffer += output_stride; break; case BlockAlignment::Block8x8: WriteTileToOutput(output_buffer, tmp_tile, 8, 8); output_buffer += TILE_SIZE; break; } } // Note(yuriks): If additional optimization is required, output_format can be moved to a // template parameter, so that its dispatch can be moved to outside the inner loop. SendData(reinterpret_cast<u32*>(data_buffer.get()), cvt.dst, (int)row_data_size, cvt.output_format, (u8)cvt.alpha); } }
void *thread_process() { int thisret,size,i,fields,j,bufout=0,counter=0; int fetch_resu; long int datalen,buflen=0; int ociposi=-1; char buf[_buflen],tmp[_buflen],*realdata ; mytext *sqldata; int realdatalen=0; ub1 *bufp; int bufpint=1024; dvoid *hdlptr = (dvoid *) 0,*tmphdlptr = (dvoid *) 0; ub1 in_out = 0; sb2 indptr = 0; ub1 piece = OCI_FIRST_PIECE; ub4 hdltype = OCI_HTYPE_DEFINE, iter = 0, idx = 0; int poid; pthread_detach(pthread_self()); sqldata=(mytext *)malloc(sizeof(mytext)); sqldata->next=0; sqldata->last=0; sqldata->size=0; bufp=(ub1 *)malloc(sizeof(ub1)*_clobmax);//**bufp must set to _clobmax realdata=(char *)malloc(_buflen); realdatalen=_buflen; if((ociposi=getsession())==-1){ printf("pool is busy!!\n"); buf[16]=0; exit(1); } poci[ociposi].threadid=pthread_self(); poci[ociposi].id=ociposi; printf("thread id=%d , ociposi=%d \n",pthread_self(),ociposi); memset(buf,0,_buflen); while(1){ pthread_mutex_lock(&mylock); thisret=accept(sock,NULL,NULL); pthread_mutex_unlock(&mylock); poci[ociposi].busy=1; bufout=0; conn_times++; if(conn_times>=1000000){conn_times1++;conn_times=0;} if(conn_times1>=1000000)conn_times1=0; printf("connections %d millions %d socket=%d ociposi=%d\n",conn_times1,conn_times,thisret,ociposi); poci[ociposi].datetime=datetime->tm_min*60+datetime->tm_sec; poci[ociposi].transfer=0; //data_buffer(sqldata,"",2,0); while(sqldata->next){ sqldata=sqldata->next; } sqldata->size=0; while(sqldata->last){ sqldata=sqldata->last; sqldata->size=0; } poci[ociposi].sock=thisret; while(size=recv(thisret,buf,_buflen,0)){ buflen=size; buf[buflen]=0; poci[ociposi].transfer=1; //printf("--buflen=%d \n",buflen); if(buflen>2){ } else{ bufout++; if(bufout>=5 || buflen<=0)break; } size=buflen; if(poci[ociposi].sock==0)continue; poci[ociposi].datetime=datetime->tm_min*60+datetime->tm_sec; data_buffer(sqldata,buf,1,buflen); //end j=0; if(size>9) for(i=size-9;i<size;i++){ tmp[j]=buf[i]; j++; } tmp[j]=0; if((strncmp(buf,_end,strlen(_end))==0)||(strncmp(tmp,_end,strlen(_end))==0)){ break; } //set commit off if(strncmp(buf,_commitoff,strlen(_commitoff))==0){ poci[ociposi].commitmode=1; memset(buf,0,sizeof(buf)); send(thisret,_autocommitoff,strlen(_autocommitoff),0); continue; } //_commit if(strncmp(buf,_commit,strlen(_commit))==0){ OCITransCommit(poci[ociposi].svchp,poci[ociposi].errhp,OCI_DEFAULT ); memset(buf,0,sizeof(buf)); continue; } //set commit on if(strncmp(buf,_commiton,strlen(_commiton))==0){ poci[ociposi].commitmode=0; memset(buf,0,sizeof(buf)); send(thisret,_autocommiton,strlen(_autocommiton),0); continue; } //rollback if(strncmp(buf,_rollback,strlen(_rollback))==0){ OCITransRollback(poci[ociposi].svchp,poci[ociposi].errhp,OCI_DEFAULT) ; poci[ociposi].commitmode=1; memset(buf,0,sizeof(buf)); continue; } //_serverdown if(strncmp(buf,_serverdown,strlen(_serverdown))==0){ OCITerminate(OCI_DEFAULT); exit(1); } if(sqldata->size+1>=_mytextlen) while(sqldata->next){ if(sqldata->size+1>=_mytextlen) sqldata=sqldata->next; else break; } //end input size=sqldata->size; datalen=size-2; j=0; if(size>15) for(i=size-15;i<size;i++){ tmp[j]=sqldata->mydata[i]; j++; } tmp[j]=0; if((strncmp(tmp,_inputend,strlen(_inputend))==0)){ datalen=data_buffer(sqldata,buf,3,0); if(datalen+1>realdatalen){ printf("free\n"); free(realdata); printf("malloc\n"); realdata=(char *)malloc(datalen+1); realdatalen=datalen+1; } data_buffer(sqldata,realdata,4,0); size=realdatalen; realdata[size-17]=0; OCIHandleAlloc(poci[ociposi].envhp,(dvoid*)&poci[ociposi].stmthp,OCI_HTYPE_STMT,0,0); printf("oracle_query \n"); fields=oracle_query(&poci[ociposi],realdata,datalen-17,tmp); printf("oracle_query ok fields=%d \n",fields); if(fields<=0)break; //printf("start fetch\n"); i=0; counter=0; if(fields>0){ piece = OCI_FIRST_PIECE ; bufp[0]=0; fetch_data(&poci[ociposi]); counter++;printf("counter=%d \n",counter); send_process(_output_begin,strlen(_output_begin),&poci[ociposi]); while (poci[ociposi].status != OCI_NO_DATA){ //if(OCI_NEED_DATA==poci[ociposi].status){ //bufpint=4096; //poci[ociposi].status = OCIStmtGetPieceInfo(poci[ociposi].stmthp, poci[ociposi].errhp, &hdlptr, &hdltype,&in_out, &iter, &idx, &piece); //poci[ociposi].status = OCIStmtSetPieceInfo(hdlptr, hdltype, poci[ociposi].errhp,(dvoid *) bufp, &bufpint, piece,(CONST dvoid *) &indptr, (ub2 *) 0); //} //printf("data=%s\n",poci[ociposi].data[0]); //printf("data=%s\n",poci[ociposi].data[1]); //printf("data=%s\n",poci[ociposi].data[2]); fetch_data(&poci[ociposi]); if(poci[ociposi].status<0)break; counter++;printf("OCI_NO_DATA=%d , poci[ociposi].status=%d , counter=%d fetch_resu=%d \n",OCI_NO_DATA,poci[ociposi].status,counter); bufp[bufpint]=0; for (i=0;i<fields;i++){ send_process(poci[ociposi].data[i],strlen(poci[ociposi].data[i]),&poci[ociposi]); if(i+1<fields){ send_process(_output_column,strlen(_output_column),&poci[ociposi]); } } send_process(_output_row,strlen(_output_row),&poci[ociposi]); /* if(poci[ociposi].status==OCI_NEED_DATA){ if(tmphdlptr!=hdlptr){ i++; if(i>1) if((i-1)%fields==0){ send_process(_output_row,strlen(_output_row),&poci[ociposi]); }else{ send_process(_output_column,strlen(_output_column),&poci[ociposi]); } tmphdlptr=hdlptr; } send_process(bufp,bufpint,&poci[ociposi]); }*/ } if(i>1){ send_process(_output_column,strlen(_output_column),&poci[ociposi]); send_process(bufp,bufpint,&poci[ociposi]); send_process(_output_row,strlen(_output_row),&poci[ociposi]); } send_process(_output_end,strlen(_output_end),&poci[ociposi]); }else{ if((fields<0) && (strlen(poci[ociposi].errbuf)>0)){ send_process(_output_beginerr,strlen(_output_beginerr),&poci[ociposi]); send_process(poci[ociposi].errbuf,512,&poci[ociposi]); send_process(_output_end,strlen(_output_end),&poci[ociposi]); }else{ send_process(_ok,strlen(_ok),&poci[ociposi]); } } OCIHandleFree(poci[ociposi].stmthp,OCI_HTYPE_STMT); //printf("fetch ok\n"); send_process("",-1,&poci[ociposi]); //printf("data send ok\n"); datalen=0; //data_buffer(sqldata,"",2,0); while(sqldata->next){ sqldata=sqldata->next; } sqldata->size=0; while(sqldata->last){ sqldata=sqldata->last; sqldata->size=0; } //printf("query complete\n"); } poci[ociposi].transfer=0; } close(thisret); poci[ociposi].busy=0; if(fields==-99){ poci[ociposi].id=-1; OCILogoff(poci[ociposi].svchp,poci[ociposi].errhp); OCIHandleFree(poci[ociposi].envhp,OCI_HTYPE_ENV); pthread_create(&ph[ociposi], NULL, &thread_process,NULL); printf("exit program \n"); break; } } }
virtual RingBufferView<T_DATA>& data_send_buffer() { return data_buffer(); }
std::unique_ptr<ProcessDataSnapshot> SystemInformationSampler::TakeSnapshot() { // Preallocate the buffer with the size determined on the previous call to // QuerySystemProcessInformation. This should be sufficient most of the time. // QuerySystemProcessInformation will grow the buffer if necessary. ByteBuffer data_buffer(previous_buffer_size_); if (!QuerySystemProcessInformation(&data_buffer)) return std::unique_ptr<ProcessDataSnapshot>(); previous_buffer_size_ = data_buffer.capacity(); std::unique_ptr<ProcessDataSnapshot> snapshot(new ProcessDataSnapshot); LARGE_INTEGER perf_counter_value; QueryPerformanceCounter(&perf_counter_value); snapshot->timestamp = static_cast<double>( (perf_counter_value.QuadPart - initial_counter_.QuadPart) / perf_frequency_.QuadPart); for (size_t offset = 0; offset < data_buffer.size();) { auto pi = reinterpret_cast<const SYSTEM_PROCESS_INFORMATION*>( data_buffer.data() + offset); // Validate that the offset is valid and all needed data is within // the buffer boundary. if (offset + sizeof(SYSTEM_PROCESS_INFORMATION) > data_buffer.size()) break; if (offset + sizeof(SYSTEM_PROCESS_INFORMATION) + (pi->NumberOfThreads - 1) * sizeof(SYSTEM_THREAD_INFORMATION) > data_buffer.size()) break; if (pi->ImageName.Buffer) { // Validate that the image name is within the buffer boundary. // ImageName.Length seems to be in bytes rather than characters. size_t image_name_offset = reinterpret_cast<BYTE*>(pi->ImageName.Buffer) - data_buffer.data(); if (image_name_offset + pi->ImageName.Length > data_buffer.size()) break; // Check if this is a chrome process. Ignore all other processes. if (wcsncmp(target_process_name_filter(), pi->ImageName.Buffer, lstrlen(target_process_name_filter())) == 0) { // Collect enough data to be able to do a diff between two snapshots. // Some threads might stop or new threads might be created between two // snapshots. If a thread with a large number of context switches gets // terminated the total number of context switches for the process might // go down and the delta would be negative. // To avoid that we need to compare thread IDs between two snapshots and // not count context switches for threads that are missing in the most // recent snapshot. ProcessData process_data; process_data.cpu_time = pi->KernelTime + pi->UserTime; process_data.working_set = pi->WorkingSetPrivateSize; // Iterate over threads and store each thread's ID and number of context // switches. for (ULONG thread_index = 0; thread_index < pi->NumberOfThreads; ++thread_index) { const SYSTEM_THREAD_INFORMATION* ti = &pi->Threads[thread_index]; if (ti->ClientId.UniqueProcess != pi->ProcessId) continue; ThreadData thread_data; thread_data.thread_id = ti->ClientId.UniqueThread; thread_data.context_switches = ti->ContextSwitchCount; process_data.threads.push_back(thread_data); } // Order thread data by thread ID to help diff two snapshots. std::sort(process_data.threads.begin(), process_data.threads.end(), [](const ThreadData& l, const ThreadData r) { return l.thread_id < r.thread_id; }); snapshot->processes.insert( std::make_pair(pi->ProcessId, std::move(process_data))); } } // Check for end of the list. if (!pi->NextEntryOffset) break; // Jump to the next entry. offset += pi->NextEntryOffset; } return snapshot; }