blob: ad30cf22cb6e48dc0c2b54b9745786d3a15df1a0 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- PixelBuffer.h
20//
21// The PixelBuffer class encapsulates the PixelFormat and dimensions
22// of a block of pixel data.
23
24#ifndef __RFB_PIXEL_BUFFER_H__
25#define __RFB_PIXEL_BUFFER_H__
26
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000027#include <rfb/PixelFormat.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000028#include <rfb/Rect.h>
29#include <rfb/Pixel.h>
30
31namespace rfb {
32
33 class Region;
34
Pierre Ossman8b56a872014-02-12 13:23:30 +010035 class PixelBuffer {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000036 public:
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010037 PixelBuffer(const PixelFormat& pf, int width, int height);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000038 virtual ~PixelBuffer();
39
40 ///////////////////////////////////////////////
41 // Format / Layout
42 //
43
Pierre Ossman1ed4d502014-01-07 15:28:45 +000044 public:
Pierre Ossman1b350ed2014-01-28 13:47:18 +010045 // Get pixel format
46 const PixelFormat &getPF() const { return format; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000047
48 // Get width, height and number of pixels
49 int width() const { return width_; }
50 int height() const { return height_; }
51 int area() const { return width_ * height_; }
52
53 // Get rectangle encompassing this buffer
54 // Top-left of rectangle is either at (0,0), or the specified point.
55 Rect getRect() const { return Rect(0, 0, width_, height_); }
56 Rect getRect(const Point& pos) const {
57 return Rect(pos, pos.translate(Point(width_, height_)));
58 }
59
60 ///////////////////////////////////////////////
61 // Access to pixel data
62 //
63
64 // Get a pointer into the buffer
65 // The pointer is to the top-left pixel of the specified Rect.
66 // The buffer stride (in pixels) is returned.
Pierre Ossman945cdda2014-01-28 14:13:12 +010067 virtual const rdr::U8* getBuffer(const Rect& r, int* stride) = 0;
68 virtual rdr::U8* getBufferRW(const Rect& r, int* stride) = 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000069
70 // Get pixel data for a given part of the buffer
71 // Data is copied into the supplied buffer, with the specified
Pierre Ossman8b56a872014-02-12 13:23:30 +010072 // 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);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000075
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000076 ///////////////////////////////////////////////
77 // Framebuffer update methods
78 //
79
80 // Ensure that the specified rectangle of buffer is up to date.
81 // Overridden by derived classes implementing framebuffer access
82 // to copy the required display data into place.
83 virtual void grabRegion(const Region& region) {}
84
85 protected:
Pierre Ossman1b350ed2014-01-28 13:47:18 +010086 // Only for subclasses that support changing parameters
87 void setPF(const PixelFormat &pf) { format = pf; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000088 PixelBuffer();
89 PixelFormat format;
90 int width_, height_;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000091 };
92
93 // FullFramePixelBuffer
94
95 class FullFramePixelBuffer : public PixelBuffer {
96 public:
97 FullFramePixelBuffer(const PixelFormat& pf, int width, int height,
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010098 rdr::U8* data_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000099 virtual ~FullFramePixelBuffer();
100
Pierre Ossman1ed4d502014-01-07 15:28:45 +0000101 public:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000102 // - Get the number of pixels per row in the actual pixel buffer data area
103 // This may in some cases NOT be the same as width().
104 virtual int getStride() const;
105
106 // Get a pointer to specified pixel data
Pierre Ossman945cdda2014-01-28 14:13:12 +0100107 virtual const rdr::U8* getBuffer(const Rect& r, int* stride) {
108 return getBufferRW(r, stride);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000109 }
Pierre Ossman945cdda2014-01-28 14:13:12 +0100110 virtual rdr::U8* getBufferRW(const Rect& r, int* stride);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000111
112 ///////////////////////////////////////////////
113 // Basic rendering operations
114 // These operations DO NOT clip to the pixelbuffer area, or trap overruns.
115
116 // Fill a rectangle
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100117 void fillRect(const Rect &dest, Pixel pix);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000118
119 // Copy pixel data to the buffer
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100120 void imageRect(const Rect &dest, const void* pixels, int stride=0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000121
122 // Copy pixel data from one PixelBuffer location to another
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100123 void copyRect(const Rect &dest, const Point& move_by_delta);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000124
125 // Copy pixel data to the buffer through a mask
126 // pixels is a pointer to the pixel to be copied to r.tl.
127 // maskPos specifies the pixel offset in the mask to start from.
128 // mask_ is a pointer to the mask bits at (0,0).
129 // pStride and mStride are the strides of the pixel and mask buffers.
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100130 void maskRect(const Rect& r, const void* pixels, const void* mask_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000131
132 // pixel is the Pixel value to be used where mask_ is set
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100133 void maskRect(const Rect& r, Pixel pixel, const void* mask_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000134
135 // *** Should this be visible?
136 rdr::U8* data;
137
138 protected:
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100139 void setPF(const PixelFormat &pf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000140 FullFramePixelBuffer();
DRC4f24c1a2011-11-03 23:56:10 +0000141 void (*fillRectFn)(rdr::U8 *, int, const Rect&, Pixel);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000142 };
143
144 // -=- Managed pixel buffer class
145 // Automatically allocates enough space for the specified format & area
146
147 class ManagedPixelBuffer : public FullFramePixelBuffer {
148 public:
149 ManagedPixelBuffer();
150 ManagedPixelBuffer(const PixelFormat& pf, int width, int height);
151 virtual ~ManagedPixelBuffer();
152
153 // Manage the pixel buffer layout
154 virtual void setPF(const PixelFormat &pf);
155 virtual void setSize(int w, int h);
156
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000157 // Return the total number of bytes of pixel data in the buffer
158 int dataLen() const { return width_ * height_ * (format.bpp/8); }
159
160 protected:
161 unsigned long datasize;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000162 void checkDataSize();
163 };
164
165};
166
167#endif // __RFB_PIXEL_BUFFER_H__