Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 17 | #include "PixelBuffer.h" |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 18 | |
Romain Guy | 9e6f3ac | 2013-06-20 16:31:35 -0700 | [diff] [blame] | 19 | #include "Debug.h" |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 20 | #include "Extensions.h" |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 21 | #include "Properties.h" |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 22 | #include "renderstate/RenderState.h" |
| 23 | |
| 24 | #include <utils/Log.h> |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | // CPU pixel buffer |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | |
| 33 | class CpuPixelBuffer: public PixelBuffer { |
| 34 | public: |
| 35 | CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 36 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 37 | uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override; |
| 38 | void unmap() override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 39 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 40 | uint8_t* getMappedPointer() const override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 41 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 42 | void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 43 | |
| 44 | private: |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 45 | std::unique_ptr<uint8_t[]> mBuffer; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 48 | CpuPixelBuffer::CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height) |
| 49 | : PixelBuffer(format, width, height) |
| 50 | , mBuffer(new uint8_t[width * height * formatSize(format)]) { |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | uint8_t* CpuPixelBuffer::map(AccessMode mode) { |
| 54 | if (mAccessMode == kAccessMode_None) { |
| 55 | mAccessMode = mode; |
| 56 | } |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 57 | return mBuffer.get(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void CpuPixelBuffer::unmap() { |
| 61 | mAccessMode = kAccessMode_None; |
| 62 | } |
| 63 | |
| 64 | uint8_t* CpuPixelBuffer::getMappedPointer() const { |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 65 | return mAccessMode == kAccessMode_None ? nullptr : mBuffer.get(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void CpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) { |
| 69 | glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 70 | mFormat, GL_UNSIGNED_BYTE, &mBuffer[offset]); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | // GPU pixel buffer |
| 75 | /////////////////////////////////////////////////////////////////////////////// |
| 76 | |
| 77 | class GpuPixelBuffer: public PixelBuffer { |
| 78 | public: |
| 79 | GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height); |
| 80 | ~GpuPixelBuffer(); |
| 81 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 82 | uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override; |
| 83 | void unmap() override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 84 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 85 | uint8_t* getMappedPointer() const override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 86 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 87 | void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | GLuint mBuffer; |
| 91 | uint8_t* mMappedPointer; |
| 92 | Caches& mCaches; |
| 93 | }; |
| 94 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 95 | GpuPixelBuffer::GpuPixelBuffer(GLenum format, |
| 96 | uint32_t width, uint32_t height) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 97 | : PixelBuffer(format, width, height) |
| 98 | , mMappedPointer(nullptr) |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 99 | , mCaches(Caches::getInstance()){ |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 100 | glGenBuffers(1, &mBuffer); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 101 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 102 | mCaches.pixelBufferState().bind(mBuffer); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 103 | glBufferData(GL_PIXEL_UNPACK_BUFFER, getSize(), nullptr, GL_DYNAMIC_DRAW); |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 104 | mCaches.pixelBufferState().unbind(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | GpuPixelBuffer::~GpuPixelBuffer() { |
| 108 | glDeleteBuffers(1, &mBuffer); |
| 109 | } |
| 110 | |
| 111 | uint8_t* GpuPixelBuffer::map(AccessMode mode) { |
| 112 | if (mAccessMode == kAccessMode_None) { |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 113 | mCaches.pixelBufferState().bind(mBuffer); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 114 | mMappedPointer = (uint8_t*) glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, getSize(), mode); |
Romain Guy | 9e6f3ac | 2013-06-20 16:31:35 -0700 | [diff] [blame] | 115 | #if DEBUG_OPENGL |
| 116 | if (!mMappedPointer) { |
| 117 | GLenum status = GL_NO_ERROR; |
| 118 | while ((status = glGetError()) != GL_NO_ERROR) { |
| 119 | ALOGE("Could not map GPU pixel buffer: 0x%x", status); |
| 120 | } |
| 121 | } |
| 122 | #endif |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 123 | mAccessMode = mode; |
| 124 | } |
| 125 | |
| 126 | return mMappedPointer; |
| 127 | } |
| 128 | |
| 129 | void GpuPixelBuffer::unmap() { |
| 130 | if (mAccessMode != kAccessMode_None) { |
| 131 | if (mMappedPointer) { |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 132 | mCaches.pixelBufferState().bind(mBuffer); |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 133 | GLboolean status = glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); |
| 134 | if (status == GL_FALSE) { |
| 135 | ALOGE("Corrupted GPU pixel buffer"); |
| 136 | } |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 137 | } |
| 138 | mAccessMode = kAccessMode_None; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 139 | mMappedPointer = nullptr; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | uint8_t* GpuPixelBuffer::getMappedPointer() const { |
| 144 | return mMappedPointer; |
| 145 | } |
| 146 | |
| 147 | void GpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) { |
| 148 | // If the buffer is not mapped, unmap() will not bind it |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 149 | mCaches.pixelBufferState().bind(mBuffer); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 150 | unmap(); |
| 151 | glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, mFormat, |
Kévin PETIT | 73fc558 | 2014-02-13 11:03:40 +0000 | [diff] [blame] | 152 | GL_UNSIGNED_BYTE, reinterpret_cast<void*>(offset)); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /////////////////////////////////////////////////////////////////////////////// |
| 156 | // Factory |
| 157 | /////////////////////////////////////////////////////////////////////////////// |
| 158 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 159 | PixelBuffer* PixelBuffer::create(GLenum format, |
| 160 | uint32_t width, uint32_t height, BufferType type) { |
Romain Guy | f9f0016 | 2013-05-09 11:50:12 -0700 | [diff] [blame] | 161 | if (type == kBufferType_Auto && Caches::getInstance().gpuPixelBuffersEnabled) { |
| 162 | return new GpuPixelBuffer(format, width, height); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 163 | } |
| 164 | return new CpuPixelBuffer(format, width, height); |
| 165 | } |
| 166 | |
| 167 | }; // namespace uirenderer |
| 168 | }; // namespace android |