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 | |
| 17 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 19 | #include "PixelBuffer.h" |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 20 | |
Romain Guy | 9e6f3ac | 2013-06-20 16:31:35 -0700 | [diff] [blame] | 21 | #include "Debug.h" |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 22 | #include "Extensions.h" |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 23 | #include "Properties.h" |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 24 | #include "renderstate/RenderState.h" |
| 25 | |
| 26 | #include <utils/Log.h> |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | // CPU pixel buffer |
| 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | |
| 35 | class CpuPixelBuffer: public PixelBuffer { |
| 36 | public: |
| 37 | CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 38 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 39 | uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override; |
| 40 | void unmap() 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 | uint8_t* getMappedPointer() const override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 43 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 44 | 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] | 45 | |
| 46 | private: |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 47 | std::unique_ptr<uint8_t[]> mBuffer; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 50 | CpuPixelBuffer::CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height) |
| 51 | : PixelBuffer(format, width, height) |
| 52 | , mBuffer(new uint8_t[width * height * formatSize(format)]) { |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | uint8_t* CpuPixelBuffer::map(AccessMode mode) { |
| 56 | if (mAccessMode == kAccessMode_None) { |
| 57 | mAccessMode = mode; |
| 58 | } |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 59 | return mBuffer.get(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void CpuPixelBuffer::unmap() { |
| 63 | mAccessMode = kAccessMode_None; |
| 64 | } |
| 65 | |
| 66 | uint8_t* CpuPixelBuffer::getMappedPointer() const { |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 67 | return mAccessMode == kAccessMode_None ? nullptr : mBuffer.get(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void CpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) { |
| 71 | glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 72 | mFormat, GL_UNSIGNED_BYTE, &mBuffer[offset]); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /////////////////////////////////////////////////////////////////////////////// |
| 76 | // GPU pixel buffer |
| 77 | /////////////////////////////////////////////////////////////////////////////// |
| 78 | |
| 79 | class GpuPixelBuffer: public PixelBuffer { |
| 80 | public: |
| 81 | GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height); |
| 82 | ~GpuPixelBuffer(); |
| 83 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 84 | uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override; |
| 85 | void unmap() 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 | uint8_t* getMappedPointer() const override; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 88 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 89 | 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] | 90 | |
| 91 | private: |
| 92 | GLuint mBuffer; |
| 93 | uint8_t* mMappedPointer; |
| 94 | Caches& mCaches; |
| 95 | }; |
| 96 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 97 | GpuPixelBuffer::GpuPixelBuffer(GLenum format, |
| 98 | uint32_t width, uint32_t height) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 99 | : PixelBuffer(format, width, height) |
| 100 | , mMappedPointer(nullptr) |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 101 | , mCaches(Caches::getInstance()){ |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 102 | glGenBuffers(1, &mBuffer); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 103 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 104 | mCaches.pixelBufferState().bind(mBuffer); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 105 | glBufferData(GL_PIXEL_UNPACK_BUFFER, getSize(), nullptr, GL_DYNAMIC_DRAW); |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 106 | mCaches.pixelBufferState().unbind(); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | GpuPixelBuffer::~GpuPixelBuffer() { |
| 110 | glDeleteBuffers(1, &mBuffer); |
| 111 | } |
| 112 | |
| 113 | uint8_t* GpuPixelBuffer::map(AccessMode mode) { |
| 114 | if (mAccessMode == kAccessMode_None) { |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 115 | mCaches.pixelBufferState().bind(mBuffer); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 116 | mMappedPointer = (uint8_t*) glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, getSize(), mode); |
Romain Guy | 9e6f3ac | 2013-06-20 16:31:35 -0700 | [diff] [blame] | 117 | #if DEBUG_OPENGL |
| 118 | if (!mMappedPointer) { |
| 119 | GLenum status = GL_NO_ERROR; |
| 120 | while ((status = glGetError()) != GL_NO_ERROR) { |
| 121 | ALOGE("Could not map GPU pixel buffer: 0x%x", status); |
| 122 | } |
| 123 | } |
| 124 | #endif |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 125 | mAccessMode = mode; |
| 126 | } |
| 127 | |
| 128 | return mMappedPointer; |
| 129 | } |
| 130 | |
| 131 | void GpuPixelBuffer::unmap() { |
| 132 | if (mAccessMode != kAccessMode_None) { |
| 133 | if (mMappedPointer) { |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 134 | mCaches.pixelBufferState().bind(mBuffer); |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 135 | GLboolean status = glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); |
| 136 | if (status == GL_FALSE) { |
| 137 | ALOGE("Corrupted GPU pixel buffer"); |
| 138 | } |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 139 | } |
| 140 | mAccessMode = kAccessMode_None; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 141 | mMappedPointer = nullptr; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | uint8_t* GpuPixelBuffer::getMappedPointer() const { |
| 146 | return mMappedPointer; |
| 147 | } |
| 148 | |
| 149 | void GpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) { |
| 150 | // If the buffer is not mapped, unmap() will not bind it |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 151 | mCaches.pixelBufferState().bind(mBuffer); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 152 | unmap(); |
| 153 | glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, mFormat, |
Kévin PETIT | 73fc558 | 2014-02-13 11:03:40 +0000 | [diff] [blame] | 154 | GL_UNSIGNED_BYTE, reinterpret_cast<void*>(offset)); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /////////////////////////////////////////////////////////////////////////////// |
| 158 | // Factory |
| 159 | /////////////////////////////////////////////////////////////////////////////// |
| 160 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 161 | PixelBuffer* PixelBuffer::create(GLenum format, |
| 162 | uint32_t width, uint32_t height, BufferType type) { |
Romain Guy | f9f0016 | 2013-05-09 11:50:12 -0700 | [diff] [blame] | 163 | if (type == kBufferType_Auto && Caches::getInstance().gpuPixelBuffersEnabled) { |
| 164 | return new GpuPixelBuffer(format, width, height); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 165 | } |
| 166 | return new CpuPixelBuffer(format, width, height); |
| 167 | } |
| 168 | |
| 169 | }; // namespace uirenderer |
| 170 | }; // namespace android |