blob: d89793f5fd97b46918cd01297e399bf8010c472b [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>
Steve Kondik0c81bbb2017-07-10 08:56:00 -070031#include <rfb/util.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000032
33namespace rfb {
34
35 class Region;
36
Pierre Ossman8b56a872014-02-12 13:23:30 +010037 class PixelBuffer {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000038 public:
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010039 PixelBuffer(const PixelFormat& pf, int width, int height);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000040 virtual ~PixelBuffer();
41
42 ///////////////////////////////////////////////
43 // Format / Layout
44 //
45
Pierre Ossman1ed4d502014-01-07 15:28:45 +000046 public:
Pierre Ossman1b350ed2014-01-28 13:47:18 +010047 // Get pixel format
48 const PixelFormat &getPF() const { return format; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000049
50 // Get width, height and number of pixels
51 int width() const { return width_; }
52 int height() const { return height_; }
53 int area() const { return width_ * height_; }
54
55 // Get rectangle encompassing this buffer
56 // Top-left of rectangle is either at (0,0), or the specified point.
57 Rect getRect() const { return Rect(0, 0, width_, height_); }
58 Rect getRect(const Point& pos) const {
59 return Rect(pos, pos.translate(Point(width_, height_)));
60 }
61
62 ///////////////////////////////////////////////
63 // Access to pixel data
64 //
65
66 // Get a pointer into the buffer
67 // The pointer is to the top-left pixel of the specified Rect.
68 // The buffer stride (in pixels) is returned.
Pierre Ossmand4f718d2014-02-13 14:37:25 +010069 virtual const rdr::U8* getBuffer(const Rect& r, int* stride) const = 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000070
71 // Get pixel data for a given part of the buffer
72 // Data is copied into the supplied buffer, with the specified
Pierre Ossman8b56a872014-02-12 13:23:30 +010073 // stride. Try to avoid using this though as getBuffer() will in
74 // most cases avoid the extra memory copy.
Pierre Ossmand4f718d2014-02-13 14:37:25 +010075 void getImage(void* imageBuf, const Rect& r, int stride=0) const;
Pierre Ossman9cbdec62014-02-12 13:24:15 +010076 // Get pixel data in a given format
77 // Works just the same as getImage(), but guaranteed to be in a
78 // specific format.
79 void getImage(const PixelFormat& pf, void* imageBuf,
Pierre Ossmand4f718d2014-02-13 14:37:25 +010080 const Rect& r, int stride=0) const;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000081
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000082 ///////////////////////////////////////////////
83 // Framebuffer update methods
84 //
85
86 // Ensure that the specified rectangle of buffer is up to date.
87 // Overridden by derived classes implementing framebuffer access
88 // to copy the required display data into place.
Steve Kondik0c81bbb2017-07-10 08:56:00 -070089 virtual void grabRegion(const Region& __unused_attr region) {}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000090
91 protected:
92 PixelBuffer();
93 PixelFormat format;
94 int width_, height_;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000095 };
96
Pierre Ossmana32040d2014-02-06 16:31:10 +010097 // ModifiablePixelBuffer
98 class ModifiablePixelBuffer : public PixelBuffer {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000099 public:
Pierre Ossmana32040d2014-02-06 16:31:10 +0100100 ModifiablePixelBuffer(const PixelFormat& pf, int width, int height);
101 virtual ~ModifiablePixelBuffer();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000102
Pierre Ossmana32040d2014-02-06 16:31:10 +0100103 ///////////////////////////////////////////////
104 // Access to pixel data
105 //
106
107 // Get a writeable pointer into the buffer
108 // Like getBuffer(), the pointer is to the top-left pixel of the
109 // specified Rect and the stride in pixels is returned.
110 virtual rdr::U8* getBufferRW(const Rect& r, int* stride) = 0;
111 // Commit the modified contents
112 // Ensures that the changes to the specified Rect is properly
113 // stored away and any temporary buffers are freed. The Rect given
114 // here needs to match the Rect given to the earlier call to
115 // getBufferRW().
116 virtual void commitBufferRW(const Rect& r) = 0;
117
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000118 ///////////////////////////////////////////////
119 // Basic rendering operations
120 // These operations DO NOT clip to the pixelbuffer area, or trap overruns.
121
122 // Fill a rectangle
Pierre Ossman56f99d62015-06-05 12:57:02 +0200123 void fillRect(const Rect &dest, const void* pix);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000124
125 // Copy pixel data to the buffer
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100126 void imageRect(const Rect &dest, const void* pixels, int stride=0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000127
128 // Copy pixel data from one PixelBuffer location to another
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100129 void copyRect(const Rect &dest, const Point& move_by_delta);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000130
Pierre Ossman9da47f82014-02-13 10:38:48 +0100131 // Render in a specific format
132 // Does the exact same thing as the above methods, but the given
133 // pixel values are defined by the given PixelFormat.
Pierre Ossman56f99d62015-06-05 12:57:02 +0200134 void fillRect(const PixelFormat& pf, const Rect &dest, const void* pix);
Pierre Ossman9da47f82014-02-13 10:38:48 +0100135 void imageRect(const PixelFormat& pf, const Rect &dest,
136 const void* pixels, int stride=0);
137
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000138 protected:
Pierre Ossmana32040d2014-02-06 16:31:10 +0100139 ModifiablePixelBuffer();
140 };
141
142 // FullFramePixelBuffer
143
144 class FullFramePixelBuffer : public ModifiablePixelBuffer {
145 public:
146 FullFramePixelBuffer(const PixelFormat& pf, int width, int height,
147 rdr::U8* data_, int stride);
148 virtual ~FullFramePixelBuffer();
149
150 public:
Pierre Ossmand4f718d2014-02-13 14:37:25 +0100151 virtual const rdr::U8* getBuffer(const Rect& r, int* stride) const;
Pierre Ossmana32040d2014-02-06 16:31:10 +0100152 virtual rdr::U8* getBufferRW(const Rect& r, int* stride);
153 virtual void commitBufferRW(const Rect& r);
154
155 protected:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000156 FullFramePixelBuffer();
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +0100157
158 rdr::U8* data;
Pierre Ossman2e5a1062014-01-30 17:57:27 +0100159 int stride;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000160 };
161
162 // -=- Managed pixel buffer class
163 // Automatically allocates enough space for the specified format & area
164
165 class ManagedPixelBuffer : public FullFramePixelBuffer {
166 public:
167 ManagedPixelBuffer();
168 ManagedPixelBuffer(const PixelFormat& pf, int width, int height);
169 virtual ~ManagedPixelBuffer();
170
171 // Manage the pixel buffer layout
172 virtual void setPF(const PixelFormat &pf);
173 virtual void setSize(int w, int h);
174
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000175 // Return the total number of bytes of pixel data in the buffer
176 int dataLen() const { return width_ * height_ * (format.bpp/8); }
177
178 protected:
179 unsigned long datasize;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000180 void checkDataSize();
181 };
182
183};
184
185#endif // __RFB_PIXEL_BUFFER_H__