Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | 2e5a106 | 2014-01-30 17:57:27 +0100 | [diff] [blame^] | 2 | * Copyright 2014 Pierre Ossman for Cendio AB |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 3 | * |
| 4 | * This is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This software is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this software; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | * USA. |
| 18 | */ |
| 19 | |
| 20 | // -=- PixelBuffer.h |
| 21 | // |
| 22 | // The PixelBuffer class encapsulates the PixelFormat and dimensions |
| 23 | // of a block of pixel data. |
| 24 | |
| 25 | #ifndef __RFB_PIXEL_BUFFER_H__ |
| 26 | #define __RFB_PIXEL_BUFFER_H__ |
| 27 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 28 | #include <rfb/PixelFormat.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 29 | #include <rfb/Rect.h> |
| 30 | #include <rfb/Pixel.h> |
| 31 | |
| 32 | namespace rfb { |
| 33 | |
| 34 | class Region; |
| 35 | |
Pierre Ossman | 8b56a87 | 2014-02-12 13:23:30 +0100 | [diff] [blame] | 36 | class PixelBuffer { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 37 | public: |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 38 | PixelBuffer(const PixelFormat& pf, int width, int height); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 39 | virtual ~PixelBuffer(); |
| 40 | |
| 41 | /////////////////////////////////////////////// |
| 42 | // Format / Layout |
| 43 | // |
| 44 | |
Pierre Ossman | 1ed4d50 | 2014-01-07 15:28:45 +0000 | [diff] [blame] | 45 | public: |
Pierre Ossman | 1b350ed | 2014-01-28 13:47:18 +0100 | [diff] [blame] | 46 | // Get pixel format |
| 47 | const PixelFormat &getPF() const { return format; } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 48 | |
| 49 | // Get width, height and number of pixels |
| 50 | int width() const { return width_; } |
| 51 | int height() const { return height_; } |
| 52 | int area() const { return width_ * height_; } |
| 53 | |
| 54 | // Get rectangle encompassing this buffer |
| 55 | // Top-left of rectangle is either at (0,0), or the specified point. |
| 56 | Rect getRect() const { return Rect(0, 0, width_, height_); } |
| 57 | Rect getRect(const Point& pos) const { |
| 58 | return Rect(pos, pos.translate(Point(width_, height_))); |
| 59 | } |
| 60 | |
| 61 | /////////////////////////////////////////////// |
| 62 | // Access to pixel data |
| 63 | // |
| 64 | |
| 65 | // Get a pointer into the buffer |
| 66 | // The pointer is to the top-left pixel of the specified Rect. |
| 67 | // The buffer stride (in pixels) is returned. |
Pierre Ossman | 945cdda | 2014-01-28 14:13:12 +0100 | [diff] [blame] | 68 | virtual const rdr::U8* getBuffer(const Rect& r, int* stride) = 0; |
| 69 | virtual rdr::U8* getBufferRW(const Rect& r, int* stride) = 0; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 70 | |
| 71 | // Get pixel data for a given part of the buffer |
| 72 | // Data is copied into the supplied buffer, with the specified |
Pierre Ossman | 8b56a87 | 2014-02-12 13:23:30 +0100 | [diff] [blame] | 73 | // stride. Try to avoid using this though as getBuffer() will in |
| 74 | // most cases avoid the extra memory copy. |
| 75 | void getImage(void* imageBuf, const Rect& r, int stride=0); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 76 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 77 | /////////////////////////////////////////////// |
| 78 | // Framebuffer update methods |
| 79 | // |
| 80 | |
| 81 | // Ensure that the specified rectangle of buffer is up to date. |
| 82 | // Overridden by derived classes implementing framebuffer access |
| 83 | // to copy the required display data into place. |
| 84 | virtual void grabRegion(const Region& region) {} |
| 85 | |
| 86 | protected: |
| 87 | PixelBuffer(); |
| 88 | PixelFormat format; |
| 89 | int width_, height_; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | // FullFramePixelBuffer |
| 93 | |
| 94 | class FullFramePixelBuffer : public PixelBuffer { |
| 95 | public: |
| 96 | FullFramePixelBuffer(const PixelFormat& pf, int width, int height, |
Pierre Ossman | 2e5a106 | 2014-01-30 17:57:27 +0100 | [diff] [blame^] | 97 | rdr::U8* data, int stride); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 98 | virtual ~FullFramePixelBuffer(); |
| 99 | |
Pierre Ossman | 1ed4d50 | 2014-01-07 15:28:45 +0000 | [diff] [blame] | 100 | public: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 101 | // Get a pointer to specified pixel data |
Pierre Ossman | 945cdda | 2014-01-28 14:13:12 +0100 | [diff] [blame] | 102 | virtual const rdr::U8* getBuffer(const Rect& r, int* stride) { |
| 103 | return getBufferRW(r, stride); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 104 | } |
Pierre Ossman | 945cdda | 2014-01-28 14:13:12 +0100 | [diff] [blame] | 105 | virtual rdr::U8* getBufferRW(const Rect& r, int* stride); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 106 | |
| 107 | /////////////////////////////////////////////// |
| 108 | // Basic rendering operations |
| 109 | // These operations DO NOT clip to the pixelbuffer area, or trap overruns. |
| 110 | |
| 111 | // Fill a rectangle |
Pierre Ossman | 1b350ed | 2014-01-28 13:47:18 +0100 | [diff] [blame] | 112 | void fillRect(const Rect &dest, Pixel pix); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 113 | |
| 114 | // Copy pixel data to the buffer |
Pierre Ossman | 1b350ed | 2014-01-28 13:47:18 +0100 | [diff] [blame] | 115 | void imageRect(const Rect &dest, const void* pixels, int stride=0); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 116 | |
| 117 | // Copy pixel data from one PixelBuffer location to another |
Pierre Ossman | 1b350ed | 2014-01-28 13:47:18 +0100 | [diff] [blame] | 118 | void copyRect(const Rect &dest, const Point& move_by_delta); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 119 | |
| 120 | // Copy pixel data to the buffer through a mask |
| 121 | // pixels is a pointer to the pixel to be copied to r.tl. |
| 122 | // maskPos specifies the pixel offset in the mask to start from. |
| 123 | // mask_ is a pointer to the mask bits at (0,0). |
| 124 | // pStride and mStride are the strides of the pixel and mask buffers. |
Pierre Ossman | 1b350ed | 2014-01-28 13:47:18 +0100 | [diff] [blame] | 125 | void maskRect(const Rect& r, const void* pixels, const void* mask_); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 126 | |
| 127 | // pixel is the Pixel value to be used where mask_ is set |
Pierre Ossman | 1b350ed | 2014-01-28 13:47:18 +0100 | [diff] [blame] | 128 | void maskRect(const Rect& r, Pixel pixel, const void* mask_); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 129 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 130 | protected: |
| 131 | FullFramePixelBuffer(); |
Pierre Ossman | ff9eb5a | 2014-01-30 17:47:31 +0100 | [diff] [blame] | 132 | |
| 133 | rdr::U8* data; |
Pierre Ossman | 2e5a106 | 2014-01-30 17:57:27 +0100 | [diff] [blame^] | 134 | int stride; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | // -=- Managed pixel buffer class |
| 138 | // Automatically allocates enough space for the specified format & area |
| 139 | |
| 140 | class ManagedPixelBuffer : public FullFramePixelBuffer { |
| 141 | public: |
| 142 | ManagedPixelBuffer(); |
| 143 | ManagedPixelBuffer(const PixelFormat& pf, int width, int height); |
| 144 | virtual ~ManagedPixelBuffer(); |
| 145 | |
| 146 | // Manage the pixel buffer layout |
| 147 | virtual void setPF(const PixelFormat &pf); |
| 148 | virtual void setSize(int w, int h); |
| 149 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 150 | // Return the total number of bytes of pixel data in the buffer |
| 151 | int dataLen() const { return width_ * height_ * (format.bpp/8); } |
| 152 | |
| 153 | protected: |
| 154 | unsigned long datasize; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 155 | void checkDataSize(); |
| 156 | }; |
| 157 | |
| 158 | }; |
| 159 | |
| 160 | #endif // __RFB_PIXEL_BUFFER_H__ |