Question

I am using Skia (prebuilt) to draw in an android native window, that i get from a java surface. The code performs well on every device I could test it on, except for a Samsung Galaxy S3 (with android 4.0.4 on it). The first image is what I should see, and the second what appears on the Galaxy S3:

Screenshot of the test on an emulator: shows a red rectangle Screenshot of the test on a Samsung Galaxy S3: shows a series of red-on-black diagonal stripes

The jni function that draws the rectangle is:

JNIEXPORT void JNICALL Java_com_test_TestSkia_InitializeWindow(JNIEnv* env, jobject thiz, jobject surface)
{
     ANativeWindow* window = ANativeWindow_fromSurface(env, surface);
     ANativeWindow_Buffer buffer;
     ANativeWindow_lock(window, &buffer, NULL);
     SkBitmap  bitmap;
     bitmap.setConfig(convertPixelFormat(buffer.format), buffer.width, buffer.height);
     bitmap.setPixels(buffer.bits);
     SkCanvas canvas;
     canvas.setBitmapDevice(bitmap);
     SkRect rectRed;
     rectRed.setXYWH(30,30,400,700);
     SkPaint paint;
     paint.setColor(SK_ColorRED);
     canvas.drawRect(rectRed,paint);
     ANativeWindow_unlockAndPost(window);
}

I tested it on the following devices, with no issue:

  • Sony X8 (android 2.3.7 customized rom)
  • Samsung Nexus S (android 4.0.4)
  • Samsung Galaxy Tab 10.1 (android 3.2)
  • Samsung Galaxy S2 (android 2.3.5)
  • Emulator (android 4.0.3, same screen size and dpi than the galaxy S3)

I tried calling ANativeWindow_setBuffersGeometry(window,0,0,0) with no luck (anyway, the buffer's dimensions and format after the window lock appear to be ok).

So far, only the Galaxy S3 is giving me troubles, at this point I don't know what to do, I can't even tell if the problem is with the ndk native window api or with skia, or maybe it is related to the galaxy S3 using TouchWiz... any idea would be welcome!

Was it helpful?

Solution

You need to take buffer.stride into account. Your picture clearly shows that you are having buffer.stride != buffer.width

I'm not able to test, but probably the following change is enough:

 SkBitmap bitmap;
 SkBitmap::Config cfg = convertPixelFormat(buffer.format);
 bitmap.setConfig(cfg, buffer.width, buffer.height,
       SkBitmap::ComputeBytesPerPixel(cfg) * buffer.stride);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top