Chromium Code Reviews| Index: experimental/HelloSkia/jni/helloskia.cpp |
| diff --git a/experimental/HelloSkia/jni/helloskia.cpp b/experimental/HelloSkia/jni/helloskia.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..480efaef576ceea36d2a4d3ccc8e5aec2d0e708a |
| --- /dev/null |
| +++ b/experimental/HelloSkia/jni/helloskia.cpp |
| @@ -0,0 +1,66 @@ |
| +#include <math.h> |
| +#include <string.h> |
| +#include <jni.h> |
| +#include <android/bitmap.h> |
| + |
| +#include "SkCanvas.h" |
| +#include "SkData.h" |
| +#include "SkGraphics.h" |
| +#include "SkSurface.h" |
| +#include "SkImage.h" |
| +#include "SkStream.h" |
| +#include "SkString.h" |
| +#include "SkTime.h" |
| + |
| +extern "C" |
| +JNIEXPORT void JNICALL Java_com_example_HelloSkiaActivity_drawIntoBitmap(JNIEnv* env, |
|
djsollen
2013/06/03 18:24:45
add a brief comment explaining what this method do
Zach Reizner
2013/06/03 22:54:25
Done.
|
| + jobject thiz, jobject dstBitmap) |
| +{ |
|
djsollen
2013/06/03 18:24:45
move to previous line
Zach Reizner
2013/06/03 22:54:25
Done.
|
| + |
| + // Grab the dst bitmap info and pixels |
| + AndroidBitmapInfo dstInfo; |
| + void* dstPixels; |
| + AndroidBitmap_getInfo(env, dstBitmap, &dstInfo); |
| + AndroidBitmap_lockPixels(env, dstBitmap, &dstPixels); |
| + |
| + SkAutoGraphics ag; |
|
Zach Reizner
2013/06/03 22:54:25
This was removed because repeated constructions/de
|
| + SkString path("skhello.png"); |
|
djsollen
2013/06/03 18:24:45
remove this unless you plan on drawing it.
Zach Reizner
2013/06/03 22:54:25
Done.
|
| + SkString text("Skia is the Best!"); |
|
djsollen
2013/06/03 18:24:45
define text where you use it not at the beginning
Zach Reizner
2013/06/03 22:54:25
Done.
|
| + |
| + // Setup |
| + SkPaint paint; |
|
djsollen
2013/06/03 18:24:45
same as above (define it closer to where it is use
Zach Reizner
2013/06/03 22:54:25
Done.
|
| + paint.setAntiAlias(true); |
| + paint.setTextSize(SkIntToScalar(30)); |
| + paint.setTextAlign(SkPaint::kLeft_Align); |
| + |
| + SkImage::Info info = { |
| + dstInfo.width, dstInfo.height, SkImage::kPMColor_ColorType, SkImage::kPremul_AlphaType |
| + }; |
| + |
| + // Create a surface and get its canvas |
| + SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); |
| + SkCanvas* canvas = surface->getCanvas(); |
| + |
| + // Draw something "interesting" |
|
djsollen
2013/06/03 18:24:45
let's draw some more interesting stuff. You can pr
Zach Reizner
2013/06/03 22:54:25
Done.
|
| + SkScalar fontHeight = paint.getFontSpacing(); |
| + canvas->drawColor(SK_ColorWHITE); |
| + canvas->drawText(text.c_str(), text.size(), |
| + 10, fontHeight, |
| + paint); |
| + |
| + // Grab the pixels from the canvas |
| + SkBitmap canvasBitmap; |
|
djsollen
2013/06/03 18:24:45
you shouldn't need this at all.
Zach Reizner
2013/06/03 22:54:25
Done.
|
| + canvasBitmap.setConfig(SkBitmap::kARGB_8888_Config, dstInfo.width, dstInfo.height); |
| + canvas->readPixels(&canvasBitmap, 0, 0); // Read from the top corner |
| + |
| + // Copy the pixels from Skia into the Java Bitmap |
| + for (uint32_t row = 0; row < dstInfo.height; row++) |
|
djsollen
2013/06/03 18:24:45
this isn't very efficient. We should just have our
Zach Reizner
2013/06/03 22:54:25
Done.
|
| + { |
| + void* dstRow = (char*)dstPixels + dstInfo.stride * row; |
| + void* srcRow = canvasBitmap.getAddr(0, row); |
| + memcpy(dstRow, srcRow, dstInfo.stride); |
| + } |
| + |
| + // Unlock the dst |
| + AndroidBitmap_unlockPixels(env, dstBitmap); |
| +} |