blob: b0db6eb5d84115dd58343afb6434ad20df5936ff [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman2e5a1062014-01-30 17:57:27 +01002 * Copyright 2014 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
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 Kaplinskya2adc8d2006-05-25 05:01:55 +000028#include <rfb/PixelFormat.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000029#include <rfb/Rect.h>
30#include <rfb/Pixel.h>
31
32namespace rfb {
33
34 class Region;
35
Pierre Ossman8b56a872014-02-12 13:23:30 +010036 class PixelBuffer {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000037 public:
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010038 PixelBuffer(const PixelFormat& pf, int width, int height);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000039 virtual ~PixelBuffer();
40
41 ///////////////////////////////////////////////
42 // Format / Layout
43 //
44
Pierre Ossman1ed4d502014-01-07 15:28:45 +000045 public:
Pierre Ossman1b350ed2014-01-28 13:47:18 +010046 // Get pixel format
47 const PixelFormat &getPF() const { return format; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000048
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 Ossmand4f718d2014-02-13 14:37:25 +010068 virtual const rdr::U8* getBuffer(const Rect& r, int* stride) const = 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.
Pierre Ossmand4f718d2014-02-13 14:37:25 +010074 void getImage(void* imageBuf, const Rect& r, int stride=0) const;
Pierre Ossman9cbdec62014-02-12 13:24:15 +010075 // 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,
Pierre Ossmand4f718d2014-02-13 14:37:25 +010079 const Rect& r, int stride=0) const;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000080
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000081 ///////////////////////////////////////////////
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 Kaplinskya2adc8d2006-05-25 05:01:55 +000094 };
95
Pierre Ossmana32040d2014-02-06 16:31:10 +010096 // ModifiablePixelBuffer
97 class ModifiablePixelBuffer : public PixelBuffer {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000098 public:
Pierre Ossmana32040d2014-02-06 16:31:10 +010099 ModifiablePixelBuffer(const PixelFormat& pf, int width, int height);
100 virtual ~ModifiablePixelBuffer();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000101
Pierre Ossmana32040d2014-02-06 16:31:10 +0100102 ///////////////////////////////////////////////
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
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000117 ///////////////////////////////////////////////
118 // Basic rendering operations
119 // These operations DO NOT clip to the pixelbuffer area, or trap overruns.
120
121 // Fill a rectangle
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100122 void fillRect(const Rect &dest, Pixel pix);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000123
124 // Copy pixel data to the buffer
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100125 void imageRect(const Rect &dest, const void* pixels, int stride=0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000126
127 // Copy pixel data from one PixelBuffer location to another
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100128 void copyRect(const Rect &dest, const Point& move_by_delta);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000129
130 // Copy pixel data to the buffer through a mask
131 // pixels is a pointer to the pixel to be copied to r.tl.
132 // maskPos specifies the pixel offset in the mask to start from.
133 // mask_ is a pointer to the mask bits at (0,0).
134 // pStride and mStride are the strides of the pixel and mask buffers.
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100135 void maskRect(const Rect& r, const void* pixels, const void* mask_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000136
137 // pixel is the Pixel value to be used where mask_ is set
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100138 void maskRect(const Rect& r, Pixel pixel, const void* mask_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000139
Pierre Ossman9da47f82014-02-13 10:38:48 +0100140 // Render in a specific format
141 // Does the exact same thing as the above methods, but the given
142 // pixel values are defined by the given PixelFormat.
143 void fillRect(const PixelFormat& pf, const Rect &dest, Pixel pix);
144 void imageRect(const PixelFormat& pf, const Rect &dest,
145 const void* pixels, int stride=0);
146
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000147 protected:
Pierre Ossmana32040d2014-02-06 16:31:10 +0100148 ModifiablePixelBuffer();
149 };
150
151 // FullFramePixelBuffer
152
153 class FullFramePixelBuffer : public ModifiablePixelBuffer {
154 public:
155 FullFramePixelBuffer(const PixelFormat& pf, int width, int height,
156 rdr::U8* data_, int stride);
157 virtual ~FullFramePixelBuffer();
158
159 public:
Pierre Ossmand4f718d2014-02-13 14:37:25 +0100160 virtual const rdr::U8* getBuffer(const Rect& r, int* stride) const;
Pierre Ossmana32040d2014-02-06 16:31:10 +0100161 virtual rdr::U8* getBufferRW(const Rect& r, int* stride);
162 virtual void commitBufferRW(const Rect& r);
163
164 protected:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000165 FullFramePixelBuffer();
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +0100166
167 rdr::U8* data;
Pierre Ossman2e5a1062014-01-30 17:57:27 +0100168 int stride;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169 };
170
171 // -=- Managed pixel buffer class
172 // Automatically allocates enough space for the specified format & area
173
174 class ManagedPixelBuffer : public FullFramePixelBuffer {
175 public:
176 ManagedPixelBuffer();
177 ManagedPixelBuffer(const PixelFormat& pf, int width, int height);
178 virtual ~ManagedPixelBuffer();
179
180 // Manage the pixel buffer layout
181 virtual void setPF(const PixelFormat &pf);
182 virtual void setSize(int w, int h);
183
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000184 // Return the total number of bytes of pixel data in the buffer
185 int dataLen() const { return width_ * height_ * (format.bpp/8); }
186
187 protected:
188 unsigned long datasize;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000189 void checkDataSize();
190 };
191
192};
193
194#endif // __RFB_PIXEL_BUFFER_H__