blob: 7060a014cf677f7f5b243ecbe4a00425e337a400 [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 Ossman945cdda2014-01-28 14:13:12 +010068 virtual const rdr::U8* getBuffer(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
Pierre Ossmana32040d2014-02-06 16:31:10 +010091 // ModifiablePixelBuffer
92 class ModifiablePixelBuffer : public PixelBuffer {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000093 public:
Pierre Ossmana32040d2014-02-06 16:31:10 +010094 ModifiablePixelBuffer(const PixelFormat& pf, int width, int height);
95 virtual ~ModifiablePixelBuffer();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000096
Pierre Ossmana32040d2014-02-06 16:31:10 +010097 ///////////////////////////////////////////////
98 // Access to pixel data
99 //
100
101 // Get a writeable pointer into the buffer
102 // Like getBuffer(), the pointer is to the top-left pixel of the
103 // specified Rect and the stride in pixels is returned.
104 virtual rdr::U8* getBufferRW(const Rect& r, int* stride) = 0;
105 // Commit the modified contents
106 // Ensures that the changes to the specified Rect is properly
107 // stored away and any temporary buffers are freed. The Rect given
108 // here needs to match the Rect given to the earlier call to
109 // getBufferRW().
110 virtual void commitBufferRW(const Rect& r) = 0;
111
112 // Default trivial handling of read-only access
Pierre Ossman945cdda2014-01-28 14:13:12 +0100113 virtual const rdr::U8* getBuffer(const Rect& r, int* stride) {
114 return getBufferRW(r, stride);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115 }
116
117 ///////////////////////////////////////////////
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:
160 virtual rdr::U8* getBufferRW(const Rect& r, int* stride);
161 virtual void commitBufferRW(const Rect& r);
162
163 protected:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000164 FullFramePixelBuffer();
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +0100165
166 rdr::U8* data;
Pierre Ossman2e5a1062014-01-30 17:57:27 +0100167 int stride;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000168 };
169
170 // -=- Managed pixel buffer class
171 // Automatically allocates enough space for the specified format & area
172
173 class ManagedPixelBuffer : public FullFramePixelBuffer {
174 public:
175 ManagedPixelBuffer();
176 ManagedPixelBuffer(const PixelFormat& pf, int width, int height);
177 virtual ~ManagedPixelBuffer();
178
179 // Manage the pixel buffer layout
180 virtual void setPF(const PixelFormat &pf);
181 virtual void setSize(int w, int h);
182
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000183 // Return the total number of bytes of pixel data in the buffer
184 int dataLen() const { return width_ * height_ * (format.bpp/8); }
185
186 protected:
187 unsigned long datasize;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000188 void checkDataSize();
189 };
190
191};
192
193#endif // __RFB_PIXEL_BUFFER_H__