]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android/external-libkmsxx.git/blobdiff - kms++util/src/drawing.cpp
libkmsxx: Add Android.bp file to build with AOSP
[android/external-libkmsxx.git] / kms++util / src / drawing.cpp
index f8cc03fb56413a17469f25134f2503cb10e77a3c..4e5c6c1fe8ac83dbaca9dc3e48f4f96764315ef9 100644 (file)
@@ -6,8 +6,11 @@ using namespace std;
 
 namespace kms
 {
-void draw_rgb_pixel(IMappedFramebuffer& buf, unsigned x, unsigned y, RGB color)
+void draw_rgb_pixel(IFramebuffer& buf, unsigned x, unsigned y, RGB color)
 {
+       if (x >= buf.width() || y >= buf.height())
+               throw runtime_error("attempt to draw outside the buffer");
+
        switch (buf.format()) {
        case PixelFormat::XRGB8888:
        case PixelFormat::ARGB8888:
@@ -56,8 +59,11 @@ void draw_rgb_pixel(IMappedFramebuffer& buf, unsigned x, unsigned y, RGB color)
        }
 }
 
-void draw_yuv422_macropixel(IMappedFramebuffer& buf, unsigned x, unsigned y, YUV yuv1, YUV yuv2)
+void draw_yuv422_macropixel(IFramebuffer& buf, unsigned x, unsigned y, YUV yuv1, YUV yuv2)
 {
+       if ((x + 1) >= buf.width() || y >= buf.height())
+               throw runtime_error("attempt to draw outside the buffer");
+
        ASSERT((x & 1) == 0);
 
        uint8_t *p = (uint8_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
@@ -101,9 +107,12 @@ void draw_yuv422_macropixel(IMappedFramebuffer& buf, unsigned x, unsigned y, YUV
        }
 }
 
-void draw_yuv420_macropixel(IMappedFramebuffer& buf, unsigned x, unsigned y,
+void draw_yuv420_macropixel(IFramebuffer& buf, unsigned x, unsigned y,
                            YUV yuv1, YUV yuv2, YUV yuv3, YUV yuv4)
 {
+       if ((x + 1) >= buf.width() || (y + 1) >= buf.height())
+               throw runtime_error("attempt to draw outside the buffer");
+
        ASSERT((x & 1) == 0);
        ASSERT((y & 1) == 0);
 
@@ -143,12 +152,49 @@ void draw_yuv420_macropixel(IMappedFramebuffer& buf, unsigned x, unsigned y,
        }
 }
 
-void draw_rect(IMappedFramebuffer &fb, uint32_t x, uint32_t y, uint32_t w, uint32_t h, RGB color)
+void draw_rect(IFramebuffer &fb, uint32_t x, uint32_t y, uint32_t w, uint32_t h, RGB color)
 {
-       for (unsigned i = x; i < x + w; ++i) {
-               for (unsigned j = y; j < y + h; ++j) {
-                       draw_rgb_pixel(fb, i, j, color);
+       unsigned i, j;
+       YUV yuvcolor = color.yuv();
+
+       switch (fb.format()) {
+       case PixelFormat::XRGB8888:
+       case PixelFormat::XBGR8888:
+       case PixelFormat::ARGB8888:
+       case PixelFormat::ABGR8888:
+       case PixelFormat::RGB888:
+       case PixelFormat::BGR888:
+       case PixelFormat::RGB565:
+       case PixelFormat::BGR565:
+               for (j = 0; j < h; j++) {
+                       for (i = 0; i < w; i++) {
+                               draw_rgb_pixel(fb, x + i, y + j, color);
+                       }
                }
+               break;
+
+       case PixelFormat::UYVY:
+       case PixelFormat::YUYV:
+       case PixelFormat::YVYU:
+       case PixelFormat::VYUY:
+               for (j = 0; j < h; j++) {
+                       for (i = 0; i < w; i += 2) {
+                               draw_yuv422_macropixel(fb, x + i, y + j, yuvcolor, yuvcolor);
+                       }
+               }
+               break;
+
+       case PixelFormat::NV12:
+       case PixelFormat::NV21:
+               for (j = 0; j < h; j += 2) {
+                       for (i = 0; i < w; i += 2) {
+                               draw_yuv420_macropixel(fb, x + i, y + j,
+                                                      yuvcolor, yuvcolor, yuvcolor, yuvcolor);
+                       }
+               }
+               break;
+       default:
+               throw std::invalid_argument("unknown pixelformat");
        }
 }
 
@@ -162,7 +208,7 @@ static bool get_char_pixel(char c, uint32_t x, uint32_t y)
        return bit;
 }
 
-static void draw_char(IMappedFramebuffer& buf, uint32_t xpos, uint32_t ypos, char c, RGB color)
+static void draw_char(IFramebuffer& buf, uint32_t xpos, uint32_t ypos, char c, RGB color)
 {
        unsigned x, y;
        YUV yuvcolor = color.yuv();
@@ -195,7 +241,7 @@ static void draw_char(IMappedFramebuffer& buf, uint32_t xpos, uint32_t ypos, cha
                                bool b1 = get_char_pixel(c, x + 1, y);
 
                                draw_yuv422_macropixel(buf, xpos + x, ypos + y,
-                                                      b0 ? yuvcolor : YUV(), b1 ? yuvcolor : YUV());
+                                                      b0 ? yuvcolor : YUV(RGB()), b1 ? yuvcolor : YUV(RGB()));
                        }
                }
                break;
@@ -210,8 +256,8 @@ static void draw_char(IMappedFramebuffer& buf, uint32_t xpos, uint32_t ypos, cha
                                bool b11 = get_char_pixel(c, x + 1, y + 1);
 
                                draw_yuv420_macropixel(buf, xpos + x, ypos + y,
-                                                      b00 ? yuvcolor : YUV(), b10 ? yuvcolor : YUV(),
-                                                      b01 ? yuvcolor : YUV(), b11 ? yuvcolor : YUV());
+                                                      b00 ? yuvcolor : YUV(RGB()), b10 ? yuvcolor : YUV(RGB()),
+                                                      b01 ? yuvcolor : YUV(RGB()), b11 ? yuvcolor : YUV(RGB()));
                        }
                }
                break;
@@ -220,7 +266,7 @@ static void draw_char(IMappedFramebuffer& buf, uint32_t xpos, uint32_t ypos, cha
        }
 }
 
-void draw_text(IMappedFramebuffer& buf, uint32_t x, uint32_t y, const string& str, RGB color)
+void draw_text(IFramebuffer& buf, uint32_t x, uint32_t y, const string& str, RGB color)
 {
        for(unsigned i = 0; i < str.size(); i++)
                draw_char(buf, (x + 8 * i), y, str[i], color);