blob: 59d71c796065db41133350b7709ca6985f6ba9d0 [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;
69 virtual rdr::U8* getBufferRW(const Rect& r, int* stride) = 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.
75 void getImage(void* imageBuf, const Rect& r, int stride=0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000076
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000077 ///////////////////////////////////////////////
78 // Framebuffer update methods
79 //
80
81 // Ensure that the specified rectangle of buffer is up to date.
82 // Overridden by derived classes implementing framebuffer access
83 // to copy the required display data into place.
84 virtual void grabRegion(const Region& region) {}
85
86 protected:
87 PixelBuffer();
88 PixelFormat format;
89 int width_, height_;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000090 };
91
92 // FullFramePixelBuffer
93
94 class FullFramePixelBuffer : public PixelBuffer {
95 public:
96 FullFramePixelBuffer(const PixelFormat& pf, int width, int height,
Pierre Ossman2e5a1062014-01-30 17:57:27 +010097 rdr::U8* data, int stride);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000098 virtual ~FullFramePixelBuffer();
99
Pierre Ossman1ed4d502014-01-07 15:28:45 +0000100 public:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000101 // Get a pointer to specified pixel data
Pierre Ossman945cdda2014-01-28 14:13:12 +0100102 virtual const rdr::U8* getBuffer(const Rect& r, int* stride) {
103 return getBufferRW(r, stride);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000104 }
Pierre Ossman945cdda2014-01-28 14:13:12 +0100105 virtual rdr::U8* getBufferRW(const Rect& r, int* stride);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000106
107 ///////////////////////////////////////////////
108 // Basic rendering operations
109 // These operations DO NOT clip to the pixelbuffer area, or trap overruns.
110
111 // Fill a rectangle
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100112 void fillRect(const Rect &dest, Pixel pix);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000113
114 // Copy pixel data to the buffer
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100115 void imageRect(const Rect &dest, const void* pixels, int stride=0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000116
117 // Copy pixel data from one PixelBuffer location to another
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100118 void copyRect(const Rect &dest, const Point& move_by_delta);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000119
120 // Copy pixel data to the buffer through a mask
121 // pixels is a pointer to the pixel to be copied to r.tl.
122 // maskPos specifies the pixel offset in the mask to start from.
123 // mask_ is a pointer to the mask bits at (0,0).
124 // pStride and mStride are the strides of the pixel and mask buffers.
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100125 void maskRect(const Rect& r, const void* pixels, const void* mask_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000126
127 // pixel is the Pixel value to be used where mask_ is set
Pierre Ossman1b350ed2014-01-28 13:47:18 +0100128 void maskRect(const Rect& r, Pixel pixel, const void* mask_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000129
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000130 protected:
131 FullFramePixelBuffer();
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +0100132
133 rdr::U8* data;
Pierre Ossman2e5a1062014-01-30 17:57:27 +0100134 int stride;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000135 };
136
137 // -=- Managed pixel buffer class
138 // Automatically allocates enough space for the specified format & area
139
140 class ManagedPixelBuffer : public FullFramePixelBuffer {
141 public:
142 ManagedPixelBuffer();
143 ManagedPixelBuffer(const PixelFormat& pf, int width, int height);
144 virtual ~ManagedPixelBuffer();
145
146 // Manage the pixel buffer layout
147 virtual void setPF(const PixelFormat &pf);
148 virtual void setSize(int w, int h);
149
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000150 // Return the total number of bytes of pixel data in the buffer
151 int dataLen() const { return width_ * height_ * (format.bpp/8); }
152
153 protected:
154 unsigned long datasize;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000155 void checkDataSize();
156 };
157
158};
159
160#endif // __RFB_PIXEL_BUFFER_H__