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; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 69 | |
| 70 | // Get pixel data for a given part of the buffer |
| 71 | // Data is copied into the supplied buffer, with the specified |
Pierre Ossman | 8b56a87 | 2014-02-12 13:23:30 +0100 | [diff] [blame] | 72 | // stride. Try to avoid using this though as getBuffer() will in |
| 73 | // most cases avoid the extra memory copy. |
| 74 | void getImage(void* imageBuf, const Rect& r, int stride=0); |
Pierre Ossman | 9cbdec6 | 2014-02-12 13:24:15 +0100 | [diff] [blame] | 75 | // Get pixel data in a given format |
| 76 | // Works just the same as getImage(), but guaranteed to be in a |
| 77 | // specific format. |
| 78 | void getImage(const PixelFormat& pf, void* imageBuf, |
| 79 | const Rect& r, int stride=0); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 80 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 81 | /////////////////////////////////////////////// |
| 82 | // Framebuffer update methods |
| 83 | // |
| 84 | |
| 85 | // Ensure that the specified rectangle of buffer is up to date. |
| 86 | // Overridden by derived classes implementing framebuffer access |
| 87 | // to copy the required display data into place. |
| 88 | virtual void grabRegion(const Region& region) {} |
| 89 | |
| 90 | protected: |
| 91 | PixelBuffer(); |
| 92 | PixelFormat format; |
| 93 | int width_, height_; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Pierre Ossman | a32040d | 2014-02-06 16:31:10 +0100 | [diff] [blame] | 96 | // ModifiablePixelBuffer |
| 97 | class ModifiablePixelBuffer : public PixelBuffer { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 98 | public: |
Pierre Ossman | a32040d | 2014-02-06 16:31:10 +0100 | [diff] [blame] | 99 | ModifiablePixelBuffer(const PixelFormat& pf, int width, int height); |
| 100 | virtual ~ModifiablePixelBuffer(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 101 | |
Pierre Ossman | a32040d | 2014-02-06 16:31:10 +0100 | [diff] [blame] | 102 | /////////////////////////////////////////////// |
| 103 | // Access to pixel data |
| 104 | // |
| 105 | |
| 106 | // Get a writeable pointer into the buffer |
| 107 | // Like getBuffer(), the pointer is to the top-left pixel of the |
| 108 | // specified Rect and the stride in pixels is returned. |
| 109 | virtual rdr::U8* getBufferRW(const Rect& r, int* stride) = 0; |
| 110 | // Commit the modified contents |
| 111 | // Ensures that the changes to the specified Rect is properly |
| 112 | // stored away and any temporary buffers are freed. The Rect given |
| 113 | // here needs to match the Rect given to the earlier call to |
| 114 | // getBufferRW(). |
| 115 | virtual void commitBufferRW(const Rect& r) = 0; |
| 116 | |
| 117 | // Default trivial handling of read-only access |
Pierre Ossman | 945cdda | 2014-01-28 14:13:12 +0100 | [diff] [blame] | 118 | virtual const rdr::U8* getBuffer(const Rect& r, int* stride) { |
| 119 | return getBufferRW(r, stride); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /////////////////////////////////////////////// |
| 123 | // Basic rendering operations |
| 124 | // These operations DO NOT clip to the pixelbuffer area, or trap overruns. |
| 125 | |
| 126 | // Fill a rectangle |
Pierre Ossman | 1b350ed | 2014-01-28 13:47:18 +0100 | [diff] [blame] | 127 | void fillRect(const Rect &dest, Pixel pix); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 128 | |
| 129 | // Copy pixel data to the buffer |
Pierre Ossman | 1b350ed | 2014-01-28 13:47:18 +0100 | [diff] [blame] | 130 | void imageRect(const Rect &dest, const void* pixels, int stride=0); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 131 | |
| 132 | // Copy pixel data from one PixelBuffer location to another |
Pierre Ossman | 1b350ed | 2014-01-28 13:47:18 +0100 | [diff] [blame] | 133 | void copyRect(const Rect &dest, const Point& move_by_delta); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 134 | |
| 135 | // Copy pixel data to the buffer through a mask |
| 136 | // pixels is a pointer to the pixel to be copied to r.tl. |
| 137 | // maskPos specifies the pixel offset in the mask to start from. |
| 138 | // mask_ is a pointer to the mask bits at (0,0). |
| 139 | // pStride and mStride are the strides of the pixel and mask buffers. |
Pierre Ossman | 1b350ed | 2014-01-28 13:47:18 +0100 | [diff] [blame] | 140 | void maskRect(const Rect& r, const void* pixels, const void* mask_); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 141 | |
| 142 | // pixel is the Pixel value to be used where mask_ is set |
Pierre Ossman | 1b350ed | 2014-01-28 13:47:18 +0100 | [diff] [blame] | 143 | void maskRect(const Rect& r, Pixel pixel, const void* mask_); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 144 | |
Pierre Ossman | 9da47f8 | 2014-02-13 10:38:48 +0100 | [diff] [blame] | 145 | // Render in a specific format |
| 146 | // Does the exact same thing as the above methods, but the given |
| 147 | // pixel values are defined by the given PixelFormat. |
| 148 | void fillRect(const PixelFormat& pf, const Rect &dest, Pixel pix); |
| 149 | void imageRect(const PixelFormat& pf, const Rect &dest, |
| 150 | const void* pixels, int stride=0); |
| 151 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 152 | protected: |
Pierre Ossman | a32040d | 2014-02-06 16:31:10 +0100 | [diff] [blame] | 153 | ModifiablePixelBuffer(); |
| 154 | }; |
| 155 | |
| 156 | // FullFramePixelBuffer |
| 157 | |
| 158 | class FullFramePixelBuffer : public ModifiablePixelBuffer { |
| 159 | public: |
| 160 | FullFramePixelBuffer(const PixelFormat& pf, int width, int height, |
| 161 | rdr::U8* data_, int stride); |
| 162 | virtual ~FullFramePixelBuffer(); |
| 163 | |
| 164 | public: |
| 165 | virtual rdr::U8* getBufferRW(const Rect& r, int* stride); |
| 166 | virtual void commitBufferRW(const Rect& r); |
| 167 | |
| 168 | protected: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 169 | FullFramePixelBuffer(); |
Pierre Ossman | ff9eb5a | 2014-01-30 17:47:31 +0100 | [diff] [blame] | 170 | |
| 171 | rdr::U8* data; |
Pierre Ossman | 2e5a106 | 2014-01-30 17:57:27 +0100 | [diff] [blame] | 172 | int stride; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | // -=- Managed pixel buffer class |
| 176 | // Automatically allocates enough space for the specified format & area |
| 177 | |
| 178 | class ManagedPixelBuffer : public FullFramePixelBuffer { |
| 179 | public: |
| 180 | ManagedPixelBuffer(); |
| 181 | ManagedPixelBuffer(const PixelFormat& pf, int width, int height); |
| 182 | virtual ~ManagedPixelBuffer(); |
| 183 | |
| 184 | // Manage the pixel buffer layout |
| 185 | virtual void setPF(const PixelFormat &pf); |
| 186 | virtual void setSize(int w, int h); |
| 187 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 188 | // Return the total number of bytes of pixel data in the buffer |
| 189 | int dataLen() const { return width_ * height_ * (format.bpp/8); } |
| 190 | |
| 191 | protected: |
| 192 | unsigned long datasize; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 193 | void checkDataSize(); |
| 194 | }; |
| 195 | |
| 196 | }; |
| 197 | |
| 198 | #endif // __RFB_PIXEL_BUFFER_H__ |