You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PixelBuffer.h 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2014 Pierre Ossman for Cendio AB
  3. *
  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. // -=- PixelBuffer.h
  20. //
  21. // The PixelBuffer class encapsulates the PixelFormat and dimensions
  22. // of a block of pixel data.
  23. #ifndef __RFB_PIXEL_BUFFER_H__
  24. #define __RFB_PIXEL_BUFFER_H__
  25. #include <rfb/PixelFormat.h>
  26. #include <rfb/Rect.h>
  27. #include <rfb/Pixel.h>
  28. #include <rfb/util.h>
  29. namespace rfb {
  30. class Region;
  31. class PixelBuffer {
  32. public:
  33. PixelBuffer(const PixelFormat& pf, int width, int height);
  34. virtual ~PixelBuffer();
  35. ///////////////////////////////////////////////
  36. // Format / Layout
  37. //
  38. public:
  39. // Get pixel format
  40. const PixelFormat &getPF() const { return format; }
  41. // Get width, height and number of pixels
  42. int width() const { return width_; }
  43. int height() const { return height_; }
  44. int area() const { return width_ * height_; }
  45. // Get rectangle encompassing this buffer
  46. // Top-left of rectangle is either at (0,0), or the specified point.
  47. Rect getRect() const { return Rect(0, 0, width_, height_); }
  48. Rect getRect(const Point& pos) const {
  49. return Rect(pos, pos.translate(Point(width_, height_)));
  50. }
  51. ///////////////////////////////////////////////
  52. // Access to pixel data
  53. //
  54. // Get a pointer into the buffer
  55. // The pointer is to the top-left pixel of the specified Rect.
  56. // The buffer stride (in pixels) is returned.
  57. virtual const rdr::U8* getBuffer(const Rect& r, int* stride) const = 0;
  58. // Get pixel data for a given part of the buffer
  59. // Data is copied into the supplied buffer, with the specified
  60. // stride. Try to avoid using this though as getBuffer() will in
  61. // most cases avoid the extra memory copy.
  62. void getImage(void* imageBuf, const Rect& r, int stride=0) const;
  63. // Get pixel data in a given format
  64. // Works just the same as getImage(), but guaranteed to be in a
  65. // specific format.
  66. void getImage(const PixelFormat& pf, void* imageBuf,
  67. const Rect& r, int stride=0) const;
  68. ///////////////////////////////////////////////
  69. // Framebuffer update methods
  70. //
  71. // Ensure that the specified rectangle of buffer is up to date.
  72. // Overridden by derived classes implementing framebuffer access
  73. // to copy the required display data into place.
  74. virtual void grabRegion(const Region& /*region*/) {}
  75. protected:
  76. PixelBuffer();
  77. virtual void setSize(int width, int height);
  78. protected:
  79. PixelFormat format;
  80. private:
  81. int width_, height_;
  82. };
  83. // ModifiablePixelBuffer
  84. class ModifiablePixelBuffer : public PixelBuffer {
  85. public:
  86. ModifiablePixelBuffer(const PixelFormat& pf, int width, int height);
  87. virtual ~ModifiablePixelBuffer();
  88. ///////////////////////////////////////////////
  89. // Access to pixel data
  90. //
  91. // Get a writeable pointer into the buffer
  92. // Like getBuffer(), the pointer is to the top-left pixel of the
  93. // specified Rect and the stride in pixels is returned.
  94. virtual rdr::U8* getBufferRW(const Rect& r, int* stride) = 0;
  95. // Commit the modified contents
  96. // Ensures that the changes to the specified Rect is properly
  97. // stored away and any temporary buffers are freed. The Rect given
  98. // here needs to match the Rect given to the earlier call to
  99. // getBufferRW().
  100. virtual void commitBufferRW(const Rect& r) = 0;
  101. ///////////////////////////////////////////////
  102. // Basic rendering operations
  103. // These operations DO NOT clip to the pixelbuffer area, or trap overruns.
  104. // Fill a rectangle
  105. void fillRect(const Rect &dest, const void* pix);
  106. // Copy pixel data to the buffer
  107. void imageRect(const Rect &dest, const void* pixels, int stride=0);
  108. // Copy pixel data from one PixelBuffer location to another
  109. void copyRect(const Rect &dest, const Point& move_by_delta);
  110. // Render in a specific format
  111. // Does the exact same thing as the above methods, but the given
  112. // pixel values are defined by the given PixelFormat.
  113. void fillRect(const PixelFormat& pf, const Rect &dest, const void* pix);
  114. void imageRect(const PixelFormat& pf, const Rect &dest,
  115. const void* pixels, int stride=0);
  116. protected:
  117. ModifiablePixelBuffer();
  118. };
  119. // FullFramePixelBuffer
  120. class FullFramePixelBuffer : public ModifiablePixelBuffer {
  121. public:
  122. FullFramePixelBuffer(const PixelFormat& pf, int width, int height,
  123. rdr::U8* data_, int stride);
  124. virtual ~FullFramePixelBuffer();
  125. public:
  126. virtual const rdr::U8* getBuffer(const Rect& r, int* stride) const;
  127. virtual rdr::U8* getBufferRW(const Rect& r, int* stride);
  128. virtual void commitBufferRW(const Rect& r);
  129. protected:
  130. FullFramePixelBuffer();
  131. virtual void setBuffer(int width, int height, rdr::U8* data, int stride);
  132. private:
  133. virtual void setSize(int w, int h);
  134. private:
  135. rdr::U8* data;
  136. int stride;
  137. };
  138. // -=- Managed pixel buffer class
  139. // Automatically allocates enough space for the specified format & area
  140. class ManagedPixelBuffer : public FullFramePixelBuffer {
  141. public:
  142. ManagedPixelBuffer();
  143. ManagedPixelBuffer(const PixelFormat& pf, int width, int height);
  144. virtual ~ManagedPixelBuffer();
  145. // Manage the pixel buffer layout
  146. virtual void setPF(const PixelFormat &pf);
  147. virtual void setSize(int w, int h);
  148. private:
  149. rdr::U8* data_; // Mirrors FullFramePixelBuffer::data
  150. unsigned long datasize;
  151. };
  152. };
  153. #endif // __RFB_PIXEL_BUFFER_H__