blob: 94230247cf941af5090cd8f6be7dcbd64939f782 [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:
86 PixelBuffer();
87 PixelFormat format;
88 int width_, height_;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000089 };
90
91 // FullFramePixelBuffer
92
93 class FullFramePixelBuffer : public PixelBuffer {
94 public:
95 FullFramePixelBuffer(const PixelFormat& pf, int width, int height,
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010096 rdr::U8* data_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000097 virtual ~FullFramePixelBuffer();
98
Pierre Ossman1ed4d502014-01-07 15:28:45 +000099 public:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000100 // - Get the number of pixels per row in the actual pixel buffer data area
101 // This may in some cases NOT be the same as width().
102 virtual int getStride() const;
103
104 // Get a pointer to specified pixel data
Pierre Ossman945cdda2014-01-28 14:13:12 +0100105 virtual const rdr::U8* getBuffer(const Rect& r, int* stride) {
106 return getBufferRW(r, stride);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000107 }
Pierre Ossman945cdda2014-01-28 14:13:12 +0100108 virtual rdr::U8* getBufferRW(const Rect& r, int* stride);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000109
110 ///////////////////////////////////////////////
111 // Basic rendering operations
112 // These operations DO NOT clip to the pixelbuffer area, or trap overruns.
113
114 // Fill a rectangle
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100115 void fillRect(const Rect &dest, Pixel pix);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000116
117 // Copy pixel data to the buffer
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100118 void imageRect(const Rect &dest, const void* pixels, int stride=0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000119
120 // Copy pixel data from one PixelBuffer location to another
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100121 void copyRect(const Rect &dest, const Point& move_by_delta);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000122
123 // Copy pixel data to the buffer through a mask
124 // pixels is a pointer to the pixel to be copied to r.tl.
125 // maskPos specifies the pixel offset in the mask to start from.
126 // mask_ is a pointer to the mask bits at (0,0).
127 // pStride and mStride are the strides of the pixel and mask buffers.
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100128 void maskRect(const Rect& r, const void* pixels, const void* mask_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000129
130 // pixel is the Pixel value to be used where mask_ is set
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100131 void maskRect(const Rect& r, Pixel pixel, const void* mask_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000132
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000133 protected:
134 FullFramePixelBuffer();
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +0100135
136 rdr::U8* data;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000137 };
138
139 // -=- Managed pixel buffer class
140 // Automatically allocates enough space for the specified format & area
141
142 class ManagedPixelBuffer : public FullFramePixelBuffer {
143 public:
144 ManagedPixelBuffer();
145 ManagedPixelBuffer(const PixelFormat& pf, int width, int height);
146 virtual ~ManagedPixelBuffer();
147
148 // Manage the pixel buffer layout
149 virtual void setPF(const PixelFormat &pf);
150 virtual void setSize(int w, int h);
151
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000152 // Return the total number of bytes of pixel data in the buffer
153 int dataLen() const { return width_ * height_ * (format.bpp/8); }
154
155 protected:
156 unsigned long datasize;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000157 void checkDataSize();
158 };
159
160};
161
162#endif // __RFB_PIXEL_BUFFER_H__