1ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org#include <math.h> 2ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org#include <jni.h> 3ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org#include <android/bitmap.h> 4ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org 5ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org#include "SkCanvas.h" 6ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org#include "SkGraphics.h" 7ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org#include "SkSurface.h" 8ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org#include "SkString.h" 9ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org#include "SkTime.h" 10ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org 11ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org 12ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org/** 13ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org * Draws something into the given bitmap 14ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org * @param env 15ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org * @param thiz 16ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org * @param dstBitmap The bitmap to place the results of skia into 17ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org * @param elapsedTime The number of milliseconds since the app was started 18ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org */ 19ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.orgextern "C" 20ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.orgJNIEXPORT void JNICALL Java_com_example_HelloSkiaActivity_drawIntoBitmap(JNIEnv* env, 21ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org jobject thiz, jobject dstBitmap, jlong elapsedTime) 22ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org{ 23ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org // Grab the dst bitmap info and pixels 24ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org AndroidBitmapInfo dstInfo; 25ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org void* dstPixels; 26ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org AndroidBitmap_getInfo(env, dstBitmap, &dstInfo); 27ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org AndroidBitmap_lockPixels(env, dstBitmap, &dstPixels); 28ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org 2932678d9a453e2c9fd26e92be429cdd84250b4d85commit-bot@chromium.org SkImageInfo info = SkImageInfo::MakeN32Premul(dstInfo.width, dstInfo.height); 30ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org 31ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org // Create a surface from the given bitmap 32ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterDirect(info, dstPixels, dstInfo.stride)); 33ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org SkCanvas* canvas = surface->getCanvas(); 34ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org 35ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org // Draw something "interesting" 36ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org 37ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org // Clear the canvas with a white color 38ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org canvas->drawColor(SK_ColorWHITE); 39ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org 40ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org // Setup a SkPaint for drawing our text 41ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org SkPaint paint; 42ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org paint.setColor(SK_ColorBLACK); // This is a solid black color for our text 43ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org paint.setTextSize(SkIntToScalar(30)); // Sets the text size to 30 pixels 44ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org paint.setAntiAlias(true); // We turn on anti-aliasing so that the text to looks good. 45ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org 46ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org // Draw some text 47ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org SkString text("Skia is Best!"); 48ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org SkScalar fontHeight = paint.getFontSpacing(); 49ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org canvas->drawText(text.c_str(), text.size(), // text's data and length 50ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org 10, fontHeight, // X and Y coordinates to place the text 51ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org paint); // SkPaint to tell how to draw the text 52ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org 53ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org // Adapt the SkPaint for drawing blue lines 54ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org paint.setAntiAlias(false); // Turning off anti-aliasing speeds up the line drawing 55ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org paint.setColor(0xFF0000FF); // This is a solid blue color for our lines 56ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org paint.setStrokeWidth(SkIntToScalar(2)); // This makes the lines have a thickness of 2 pixels 57ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org 58ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org // Draw some interesting lines using trig functions 59ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org for (int i = 0; i < 100; i++) 60ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org { 61ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org float x = (float)i / 99.0f; 62ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org float offset = elapsedTime / 1000.0f; 63ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org canvas->drawLine(sin(x * M_PI + offset) * 800.0f, 0, // first endpoint 64ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org cos(x * M_PI + offset) * 800.0f, 800, // second endpoint 65ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org paint); // SkPapint to tell how to draw the line 66ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org } 67ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org 68ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org // Unlock the dst's pixels 69ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org AndroidBitmap_unlockPixels(env, dstBitmap); 70ecaa59d878942fc87b0daa5171dfcdcb133c84a8commit-bot@chromium.org} 71