static bool Bitmap_compress(JNIEnv* env, jobject clazz, SkBitmap* bitmap, int format, int quality, jobject jstream, jbyteArray jstorage) { SkImageEncoder::Type fm; switch (format) { case kJPEG_JavaEncodeFormat: fm = SkImageEncoder::kJPEG_Type; break; case kPNG_JavaEncodeFormat: fm = SkImageEncoder::kPNG_Type; break; case kWEBP_JavaEncodeFormat: fm = SkImageEncoder::kWEBP_Type; break; default: return false; } bool success = false; SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage); if (NULL != strm) { SkImageEncoder* encoder = SkImageEncoder::Create(fm); if (NULL != encoder) { success = encoder->encodeStream(strm, *bitmap, quality); delete encoder; } delete strm; } return success; }
/****************************************************************************** * convert rgb565 to jpeg file *******************************************************************************/ bool rgb565toJpeg(uint8_t *pbufIn, uint8_t *pbufOut, uint32_t width, uint32_t height, uint32_t *psize) { uint32_t size = 0; // It's sw encode, the buffer address must be virtual address CAM_LOGD("[rgb565toJpeg] pbufIn: 0x%x, pbufOut: 0x%x, width: %d, height: %d \n", (uint32_t) pbufIn, (uint32_t) pbufOut, width, height); SkImageEncoder::Type fm = SkImageEncoder::kJPEG_Type; bool success = false; SkImageEncoder *pencoder = SkImageEncoder::Create(fm); if (NULL != pencoder) { SkMemoryWStream *pskMemStream = new SkMemoryWStream(pbufOut, width * height * 2); if (NULL != pskMemStream) { SkBitmap *pbitmap = new SkBitmap; if (NULL != pbitmap) { pbitmap->setConfig(SkBitmap::kRGB_565_Config, width, height); pbitmap->setPixels(pbufIn); success = pencoder->encodeStream(pskMemStream, *pbitmap, 85); if (success) { size = pskMemStream->getBytesWritten(); } } delete pbitmap; } delete pskMemStream; delete pencoder; } *psize = size; CAM_LOGD(" size: %d \n", size); return success; }
static jboolean Bitmap_compress(JNIEnv* env, jobject clazz, jlong bitmapHandle, jint format, jint quality, jobject jstream, jbyteArray jstorage) { SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle); SkImageEncoder::Type fm; switch (format) { case kJPEG_JavaEncodeFormat: fm = SkImageEncoder::kJPEG_Type; break; case kPNG_JavaEncodeFormat: fm = SkImageEncoder::kPNG_Type; break; case kWEBP_JavaEncodeFormat: fm = SkImageEncoder::kWEBP_Type; break; default: return JNI_FALSE; } bool success = false; if (NULL != bitmap) { SkAutoLockPixels alp(*bitmap); if (NULL == bitmap->getPixels()) { return JNI_FALSE; } SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage); if (NULL == strm) { return JNI_FALSE; } SkImageEncoder* encoder = SkImageEncoder::Create(fm); if (NULL != encoder) { success = encoder->encodeStream(strm, *bitmap, quality); delete encoder; } delete strm; } return success ? JNI_TRUE : JNI_FALSE; }
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; }