EXPORT void benchmark(const char* url, int reloadCount, int width, int height) { ScriptController::initializeThreading(); // Setting this allows data: urls to load from a local file. SecurityOrigin::setLocalLoadPolicy(SecurityOrigin::AllowLocalLoadsForAll); // Create the fake JNIEnv and JavaVM InitializeJavaVM(); // The real function is private to libwebcore but we know what it does. notifyHistoryItemChanged = historyItemChanged; // Implement the shared timer callback MyJavaSharedClient client; JavaSharedClient::SetTimerClient(&client); JavaSharedClient::SetCookieClient(&client); // Create the page with all the various clients ChromeClientAndroid* chrome = new ChromeClientAndroid; EditorClientAndroid* editor = new EditorClientAndroid; Page* page = new Page(chrome, new ContextMenuClientAndroid, editor, new DragClientAndroid, new InspectorClientAndroid, 0, // PluginHalterClient 0); // GeolocationControllerClient editor->setPage(page); // Create MyWebFrame that intercepts network requests MyWebFrame* webFrame = new MyWebFrame(page); webFrame->setUserAgent("Performance testing"); // needs to be non-empty chrome->setWebFrame(webFrame); // ChromeClientAndroid maintains the reference. Release(webFrame); // Create the Frame and the FrameLoaderClient FrameLoaderClientAndroid* loader = new FrameLoaderClientAndroid(webFrame); RefPtr<Frame> frame = Frame::create(page, NULL, loader); loader->setFrame(frame.get()); // Build our View system, resize it to the given dimensions and release our // references. Note: We keep a referenec to frameView so we can layout and // draw later without risk of it being deleted. WebViewCore* webViewCore = new WebViewCore(JSC::Bindings::getJNIEnv(), MY_JOBJECT, frame.get()); RefPtr<FrameView> frameView = FrameView::create(frame.get()); WebFrameView* webFrameView = new WebFrameView(frameView.get(), webViewCore); frame->setView(frameView); frameView->resize(width, height); Release(webViewCore); Release(webFrameView); // Initialize the frame and turn of low-bandwidth display (it fails an // assertion in the Cache code) frame->init(); frame->selection()->setFocused(true); // Set all the default settings the Browser normally uses. Settings* s = frame->settings(); s->setLayoutAlgorithm(Settings::kLayoutNormal); // Normal layout for now s->setStandardFontFamily("sans-serif"); s->setFixedFontFamily("monospace"); s->setSansSerifFontFamily("sans-serif"); s->setSerifFontFamily("serif"); s->setCursiveFontFamily("cursive"); s->setFantasyFontFamily("fantasy"); s->setMinimumFontSize(8); s->setMinimumLogicalFontSize(8); s->setDefaultFontSize(16); s->setDefaultFixedFontSize(13); s->setLoadsImagesAutomatically(true); s->setJavaScriptEnabled(true); s->setDefaultTextEncodingName("latin1"); s->setPluginsEnabled(false); s->setShrinksStandaloneImagesToFit(false); s->setUseWideViewport(false); // Finally, load the actual data ResourceRequest req(url); frame->loader()->load(req, false); do { // Layout the page and service the timer frame->view()->layout(); while (client.m_hasTimer) { client.m_func(); JavaSharedClient::ServiceFunctionPtrQueue(); } JavaSharedClient::ServiceFunctionPtrQueue(); // Layout more if needed. while (frame->view()->needsLayout()) frame->view()->layout(); JavaSharedClient::ServiceFunctionPtrQueue(); if (reloadCount) frame->loader()->reload(true); } while (reloadCount--); // Draw into an offscreen bitmap SkBitmap bmp; bmp.setConfig(SkBitmap::kARGB_8888_Config, width, height); bmp.allocPixels(); SkCanvas canvas(bmp); PlatformGraphicsContext ctx(&canvas, NULL); GraphicsContext gc(&ctx); frame->view()->paintContents(&gc, IntRect(0, 0, width, height)); // Write the bitmap to the sdcard SkImageEncoder* enc = SkImageEncoder::Create(SkImageEncoder::kPNG_Type); enc->encodeFile("/sdcard/webcore_test.png", bmp, 100); delete enc; // Tear down the world. frame->loader()->detachFromParent(); delete page; }
void runSimpleRendering( string renderingFileName, string resourceDir, RenderingInfo* info) { SkColor defaultMapColor = SK_ColorLTGRAY; if (info->width > 10000 || info->height > 10000) { osmand_log_print(LOG_ERROR, "We don't rendering images more than 10000x10000 "); return; } osmand_log_print(LOG_INFO, "Rendering info bounds(%d, %d, %d, %d) zoom(%d), width/height(%d/%d) tilewidth/tileheight(%d/%d) fileName(%s)", info->left, info->top, info->right, info->bottom, info->zoom, info->width, info->height, info->tileWX, info->tileHY, info->tileFileName.c_str()); RenderingRulesStorage* st = new RenderingRulesStorage(renderingFileName.c_str()); st->parseRulesFromXmlInputStream(renderingFileName.c_str(), NULL); RenderingRuleSearchRequest* searchRequest = new RenderingRuleSearchRequest(st); ResultPublisher* publisher = new ResultPublisher(); SearchQuery q(floor(info->left), floor(info->right), ceil(info->top), ceil(info->bottom), searchRequest, publisher); q.zoom = info->zoom; ResultPublisher* res = searchObjectsForRendering(&q, true, "Nothing found"); osmand_log_print(LOG_INFO, "Found %d objects", res->result.size()); SkBitmap* bitmap = new SkBitmap(); bitmap->setConfig(SkBitmap::kRGB_565_Config, info->width, info->height); size_t bitmapDataSize = bitmap->getSize(); void* bitmapData = malloc(bitmapDataSize); bitmap->setPixels(bitmapData); osmand_log_print(LOG_INFO, "Initializing rendering style and rendering context"); ElapsedTimer initObjects; initObjects.start(); RenderingContext rc; rc.setDefaultIconsDir(resourceDir); searchRequest->clearState(); searchRequest->setIntFilter(st->PROPS.R_MINZOOM, info->zoom); if (searchRequest->searchRenderingAttribute(A_DEFAULT_COLOR)) { defaultMapColor = searchRequest->getIntPropertyValue(searchRequest->props()->R_ATTR_COLOR_VALUE); } searchRequest->clearState(); searchRequest->setIntFilter(st->PROPS.R_MINZOOM, info->zoom); if (searchRequest->searchRenderingAttribute(A_SHADOW_RENDERING)) { rc.setShadowRenderingMode(searchRequest->getIntPropertyValue(searchRequest->props()->R_ATTR_INT_VALUE)); //rc.setShadowRenderingColor(searchRequest->getIntPropertyValue(searchRequest->props()->R_SHADOW_COLOR)); } rc.setLocation( ((double)info->left)/getPowZoom(31-info->zoom), ((double)info->top)/getPowZoom(31-info->zoom) ); rc.setDimension(info->width, info->height); rc.setZoom(info->zoom); rc.setRotate(0); rc.setDensityScale(1); osmand_log_print(LOG_INFO, "Rendering image"); initObjects.pause(); SkCanvas* canvas = new SkCanvas(*bitmap); canvas->drawColor(defaultMapColor); doRendering(res->result, canvas, searchRequest, &rc); osmand_log_print(LOG_INFO, "End Rendering image"); osmand_log_print(LOG_INFO, "Native ok (init %d, rendering %d) ", initObjects.getElapsedTime(), rc.nativeOperations.getElapsedTime()); SkImageEncoder* enc = SkImageEncoder::Create(SkImageEncoder::kPNG_Type); if (enc != NULL && !enc->encodeFile(info->tileFileName.c_str(), *bitmap, 100)) { osmand_log_print(LOG_ERROR, "FAIL to save tile to %s", info->tileFileName.c_str()); } else { osmand_log_print(LOG_INFO, "Tile successfully saved to %s", info->tileFileName.c_str()); } delete enc; delete publisher; delete searchRequest; delete st; delete canvas; delete bitmap; free(bitmapData); return; }